Filling std::vector type ntuples using G4root

Dear experts

I want to output the simulation results to a root file using G4AnalysisManager. The format that I want to implement is ntuple columns containing vector entries. The vector will be the collection of hits from one event. I was able to create a std::vector<G4double> type ntuple column, but cannot find any method to fill this. Can you please suggest a way for this?

Regards
Shubham

An example of an ntuple with std::vector ntuple columns types is provided at the basic B5 example.
See:
http://geant4-userdoc.web.cern.ch/geant4-userdoc/Doxygen/examples_doc/html/ExampleB5.html
and the ANALYSIS section.
There is indeed missing mentioning this example in the Application Developers Guide.
As the ntuple column takes the std::vector reference, there is no need to call an extra Fill method to fill a column. The vector content is automatically saved in the ntuple when calling AddNtupleRow .

1 Like

Thank you very much. That works.

Dear Ivana,

I am using Geant4.10.06.p02.
I have problems by filling Ntuples with some variables of vector type.
I am trying to add rows with the following data:
G4int,G4int,vector,vector,vector

It seems that I have problems with columnID = 0.
I list 5 cases below showing some information.

The output shows always warnings concerning columnId=0.
Sometimes as “Column type does not match: ntupleId 0 columnId 0”
and sometimes as “ntupleId 0 columnId 0 does not exist”.

The CASES have the following information:

  1. Create Ntuple (the way I have created the Ntuples)
  2. Fill Ntuple ( I have filled them with 2 events)
  3. Warning message (the output warning)
  4. ROOT file (the Scan() of the produced ROOT file)
    The output of the ROOT file in CASE 1 is the only one with wrong contents.
    All runs give Warning messages about columnId = 0 that take much processing time.

Thank you for your attention.
Mauricio Moralles

Variable definitions:

G4int ntupleID;
G4int eventID, NhitsID, PDGcodeID, energyID, timeID;
G4int event, Nhits;
std::vector<G4int>    PDG;
std::vector<G4double> En;
std::vector<G4double> Time;

CASE 1:
Create Ntuple:

ntupleID = analysisManager->CreateNtuple(“tree”, “Test”);
// analysisManager->SetFirstNtupleColumnId(1); // eventID should be 1
// G4int type
eventID = analysisManager->CreateNtupleIColumn(“Nevent”); // column Id = 0
NhitsID = analysisManager->CreateNtupleIColumn(“Nhits”); // column Id = 1
// vector
PDGcodeID = analysisManager->CreateNtupleIColumn(“PDGchit”,PDG); // column Id = 2
// vector
energyID = analysisManager->CreateNtupleDColumn(“Enhit”,En); // column Id = 3
timeID = analysisManager->CreateNtupleDColumn(“thit”,Time); // column Id = 4
analysisManager->FinishNtuple();

Fill Ntuple with 2 events
// ---------------------------- Clear vectors
PDG.assign(PDG.size(),0);
PDG.clear();
En.assign(En.size(),0);
En.clear();
Time.assign(Time.size(),0);
Time.clear();

// ------------------------------------------ event1
event = 1000;
Nhits = 4;
// fill the vectors with some values
for(int i = 0; i < Nhits; i++)
{
PDG.push_back( i + 1 );
En.push_back( i + 10.5 );
Time.push_back( i + 100.8);
}

G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleIColumn(ntupleID, eventID, event);
analysisManager->FillNtupleIColumn(ntupleID, NhitsID, Nhits);
analysisManager->FillNtupleIColumn(ntupleID, PDGcodeID);
analysisManager->FillNtupleDColumn(ntupleID, energyID);
analysisManager->FillNtupleDColumn(ntupleID, timeID);
analysisManager->AddNtupleRow(ntupleID); // one Row per event, with Nhits particles

// ---------------------------- Clear vectors
PDG.assign(PDG.size(),0);
PDG.clear();
En.assign(En.size(),0);
En.clear();
Time.assign(Time.size(),0);
Time.clear();

// ------------------------------------------ event2
event = 1001;
Nhits = 3;
// fill the vectors with some values
for(int i = 0; i < Nhits; i++)
{
PDG.push_back( i + 6 );
En.push_back( i + 20.3 );
Time.push_back( i + 200.7);
}

analysisManager->FillNtupleIColumn(ntupleID, eventID, event);
analysisManager->FillNtupleIColumn(ntupleID, NhitsID, Nhits);  
analysisManager->FillNtupleIColumn(ntupleID, PDGcodeID);
analysisManager->FillNtupleDColumn(ntupleID, energyID);
analysisManager->FillNtupleDColumn(ntupleID, timeID);

analysisManager->AddNtupleRow(ntupleID); // one Row per event, with Nhits particles

Warning message:
THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

ROOT file:
tree->Scan();


  • Row * Instance * Nevent * Nhits * PDGchit * Enhit * thit *

  •    0 *        0 *         2 *         4 *         1 *      10.5 *     100.8 *
    
  •    0 *        1 *         2 *         4 *         2 *      11.5 *     101.8 *
    
  •    0 *        2 *         2 *         4 *         3 *      12.5 *     102.8 *
    
  •    0 *        3 *         2 *         4 *         4 *      13.5 *     103.8 *
    
  •    1 *        0 *         2 *         3 *         6 *      20.3 *     200.7 *
    
  •    1 *        1 *         2 *         3 *         7 *      21.3 *     201.7 *
    
  •    1 *        2 *         2 *         3 *         8 *      22.3 *     202.7 *
    

—> WRONG values for Nevent
Nevent is always 2. Should be 1000 (4x) and 1001 (3x).


CASE 2:

Create Ntuple change: SetFirstNtupleColumnId(1)
ntupleID = analysisManager->CreateNtuple(“tree”, “Test”);
analysisManager->SetFirstNtupleColumnId(1); // eventID should be 1
// G4int type
eventID = analysisManager->CreateNtupleIColumn(“Nevent”); // column Id = 1
NhitsID = analysisManager->CreateNtupleIColumn(“Nhits”); // column Id = 2
// vector
PDGcodeID = analysisManager->CreateNtupleIColumn(“PDGchit”,PDG); // column Id = 3
// vector
energyID = analysisManager->CreateNtupleDColumn(“Enhit”,En); // column Id = 4
timeID = analysisManager->CreateNtupleDColumn(“thit”,Time); // column Id = 5
analysisManager->FinishNtuple();

Fill Ntuple: same as CASE 1

Warning Message: (6x)
THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager::FillNtupleTColumn()
ntupleId 0 columnId 0 does not exist.
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

ROOT file:
tree->Scan();


  • Row * Instance * Nevent * Nhits * PDGchit * Enhit * thit *

  •    0 *        0 *      1000 *         4 *         1 *      10.5 *     100.8 *
    
  •    0 *        1 *      1000 *         4 *         2 *      11.5 *     101.8 *
    
  •    0 *        2 *      1000 *         4 *         3 *      12.5 *     102.8 *
    
  •    0 *        3 *      1000 *         4 *         4 *      13.5 *     103.8 *
    
  •    1 *        0 *      1001 *         3 *         6 *      20.3 *     200.7 *
    
  •    1 *        1 *      1001 *         3 *         7 *      21.3 *     201.7 *
    
  •    1 *        2 *      1001 *         3 *         8 *      22.3 *     202.7 *
    

—> Now it is correct, but the run produces a lot of Warnings


CASE 3:

Create Ntuple change: vector is the columnId = 0
ntupleID = analysisManager->CreateNtuple(“tree”, “Test”);
// analysisManager->SetFirstNtupleColumnId(1); // eventID should be 1
// vector
PDGcodeID = analysisManager->CreateNtupleIColumn(“PDGchit”,PDG); // column Id = 0
// G4int type
eventID = analysisManager->CreateNtupleIColumn(“Nevent”); // column Id = 1
NhitsID = analysisManager->CreateNtupleIColumn(“Nhits”); // column Id = 2
// vector
energyID = analysisManager->CreateNtupleDColumn(“Enhit”,En); // column Id = 3
timeID = analysisManager->CreateNtupleDColumn(“thit”,Time); // column Id = 4
analysisManager->FinishNtuple();

Fill Ntuple: same as CASE 1 and CASE 2

Warning Message:
THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 0
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 0
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

ROOT file:
tree->Scan();


  • Row * Instance * PDGchit * Nevent * Nhits * Enhit * thit *

  •    0 *        0 *         1 *      1000 *         4 *      10.5 *     100.8 *
    
  •    0 *        1 *         2 *      1000 *         4 *      11.5 *     101.8 *
    
  •    0 *        2 *         3 *      1000 *         4 *      12.5 *     102.8 *
    
  •    0 *        3 *         4 *      1000 *         4 *      13.5 *     103.8 *
    
  •    1 *        0 *         6 *      1001 *         3 *      20.3 *     200.7 *
    
  •    1 *        1 *         7 *      1001 *         3 *      21.3 *     201.7 *
    
  •    1 *        2 *         8 *      1001 *         3 *      22.3 *     202.7 *
    

—> It is correct, but the run produces a lot of Warnings


CASE 4:

Create Ntuple: SetFirstNtupleColumnId(1) vector is the columnId = 1
ntupleID = analysisManager->CreateNtuple(“tree”, “Test”);
analysisManager->SetFirstNtupleColumnId(1); // eventID should be 1
// vector
PDGcodeID = analysisManager->CreateNtupleIColumn(“PDGchit”,PDG); // column Id = 1
// G4int type
eventID = analysisManager->CreateNtupleIColumn(“Nevent”); // column Id = 2
NhitsID = analysisManager->CreateNtupleIColumn(“Nhits”); // column Id = 3
// vector
energyID = analysisManager->CreateNtupleDColumn(“Enhit”,En); // column Id = 4
timeID = analysisManager->CreateNtupleDColumn(“thit”,Time); // column Id = 5
analysisManager->FinishNtuple();

Fill Ntuple: same as CASE 1, CASE 2 and CASE 3

Warning Message: (6x)
THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager::FillNtupleTColumn()
ntupleId 0 columnId 0 does not exist.
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

ROOT file
tree->Scan();


  • Row * Instance * PDGchit * Nevent * Nhits * Enhit * thit *

  •    0 *        0 *         1 *      1000 *         4 *      10.5 *     100.8 *
    
  •    0 *        1 *         2 *      1000 *         4 *      11.5 *     101.8 *
    
  •    0 *        2 *         3 *      1000 *         4 *      12.5 *     102.8 *
    
  •    0 *        3 *         4 *      1000 *         4 *      13.5 *     103.8 *
    
  •    1 *        0 *         6 *      1001 *         3 *      20.3 *     200.7 *
    
  •    1 *        1 *         7 *      1001 *         3 *      21.3 *     201.7 *
    
  •    1 *        2 *         8 *      1001 *         3 *      22.3 *     202.7 *
    

—> It is correct, but the run produces a lot of Warnings


CASE 5:

Create Ntuple: same as CASE 1
ntupleID = analysisManager->CreateNtuple(“tree”, “Test”);
// analysisManager->SetFirstNtupleColumnId(1); // eventID should be 1
// G4int type
eventID = analysisManager->CreateNtupleIColumn(“Nevent”); // column Id = 0
NhitsID = analysisManager->CreateNtupleIColumn(“Nhits”); // column Id = 1
// vector
PDGcodeID = analysisManager->CreateNtupleIColumn(“PDGchit”,PDG); // column Id = 2
// vector
energyID = analysisManager->CreateNtupleDColumn(“Enhit”,En); // column Id = 3
timeID = analysisManager->CreateNtupleDColumn(“thit”,Time); // column Id = 4
analysisManager->FinishNtuple();

Fill Ntuple change: first line with a vector
G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleIColumn(ntupleID, PDGcodeID); // this line was below
analysisManager->FillNtupleIColumn(ntupleID, eventID, event);
analysisManager->FillNtupleIColumn(ntupleID, NhitsID, Nhits);
// this line was shifted to the first position: analysisManager->FillNtupleIColumn(ntupleID, PDGcodeID);
analysisManager->FillNtupleDColumn(ntupleID, energyID);
analysisManager->FillNtupleDColumn(ntupleID, timeID);
analysisManager->AddNtupleRow(ntupleID); // one Row per event, with Nhits particles

Warning Message:
THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 3
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

THREAD_0 >
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Analysis_W011
issued by : G4TNtupleManager:FillNtupleTColumn
Column type does not match: ntupleId 0 columnId 0 value 4
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

ROOT file
tree->Scan();


  • Row * Instance * Nevent * Nhits * PDGchit * Enhit * thit *

  •    0 *        0 *      1000 *         4 *         1 *      10.5 *     100.8 *
    
  •    0 *        1 *      1000 *         4 *         2 *      11.5 *     101.8 *
    
  •    0 *        2 *      1000 *         4 *         3 *      12.5 *     102.8 *
    
  •    0 *        3 *      1000 *         4 *         4 *      13.5 *     103.8 *
    
  •    1 *        0 *      1001 *         3 *         6 *      20.3 *     200.7 *
    
  •    1 *        1 *      1001 *         3 *         7 *      21.3 *     201.7 *
    
  •    1 *        2 *      1001 *         3 *         8 *      22.3 *     202.7 *
    

—> It is correct, but the run produces a lot of Warnings

Hello,

The Fill…() function should not be called for the columns associated with vectors. You need just to remove these lines from your code:

analysisManager->FillNtupleIColumn(ntupleID, PDGcodeID);
analysisManager->FillNtupleDColumn(ntupleID, energyID);
analysisManager->FillNtupleDColumn(ntupleID, timeID);

I will add this information in the documentation, as actually it is missing here.

Best regards,

Thank you.

–> I will add this information in the documentation, as actually it is missing here.
This is very important. I spent a lot of time trying to understand why it was not working.

Best regards,
Mauricio