Problem in saving data to NTuple at end of step

Hi,

In my Geant4 code, I am trying to save the energy/momentum/position at the end of the step in a Ntuple (as I want to get the energy/momentum/position distribution at the last step of the geometry boundary).
I have created 3 NTuples in the HistoManager file as follows,

analysisManager->CreateNtuple("Ntuple1","Flux of Primary and Secondaries");
analysisManager->CreateNtupleIColumn("Nevents");
...
analysisManager->FinishNtuple();
    analysisManager->CreateNtuple("Ntuple2","Tracking of Muons");
analysisManager->CreateNtupleDColumn("Muon Energy");
...
analysisManager->FinishNtuple();

    analysisManager->CreateNtuple("Ntuple3","Tracking of Neutrons");
    	  analysisManager->CreateNtupleDColumn("Neutron Energy"); 
...
analysisManager->FinishNtuple();

I am calling the first NTuple (using a function defined in HistoManager) in RunAction as follows:
fHistoManager->FillNtuple(nEvt, tflux, ...)
This runs fine and saves the values into the Ntuples.

However, when I call a similar function (again defined in HistoManager), in Stepping Action as follows:
fHistoManager->FillNtuple_step_muon(energy_mu, ...) defined in the exact same way as the previous function, it does not save any values to the NTuple, and outputs an empty NTuple (no syntax errors though).

Alternatively, I’ve defined the FillHisto function in HistoManager, which works file and can fill the histogram even in the stepping action.

void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
{
	auto analysisManager = G4AnalysisManager::Instance();
	analysisManager->FillH1(ih, xbin, weight); 
}

I am unable to figure out why I’m not able to fill the NTuple from the step.
Many thanks to anyone who can help.

-Aditya

Hello Aditya,

When working with more than Ntuple the syntax slightly changes as per the definition G4VNtupleManager.
You have to call the function with the ntuple id which corresponds to the one you are working with. I hope this helps.

// Methods to fill ntuples
// Methods for ntuple with id = FirstNtupleId                     
virtual G4bool FillNtupleIColumn(G4int id, G4int value) = 0;
// Methods for ntuple with id > FirstNtupleId (when more ntuples exist)                      
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) = 0;

Hi Michael,

Thanks for your reply.
Actually I am doing that, which is why it works fine in filling the first NTuple in RunAction provided by the following function in HistoManager
void HistoManager::FillNtuple(G4int nEvt, G4double tflux, …)
{
auto analysisManager = G4AnalysisManager::Instance();
analysisManager->FillNtupleIColumn(0, 0,nEvt);
analysisManager->FillNtupleDColumn(0, 1,tflux);

analysisManager->AddNtupleRow(0);
}

But, when I define a similar function to fill the Ntuple in step, it doesn’t fill it.

void HistoManager::FillNtuple_step_muon(G4double energy_mu, G4double dirx_mu,...)
{
	    auto analysisManager = G4AnalysisManager::Instance();
  	    analysisManager->FillNtupleDColumn(1, 0,energy_mu);
	    analysisManager->FillNtupleDColumn(1, 1,dirx_mu);
            ...
	    analysisManager->AddNtupleRow(1);
}

Thanks for all the help!
Aditya

Hello Aditya,

Once the ntuple and ntuple file are created, the ntuple can be filled from any user action, including stepping action.

Could you, please, check that the call to your HistoManager::FillNtuple_step_muon() is really executed (for example by adding a printing line in this method.

Best regards,

Hi Ivana,

Thanks for your reply.
The function FillNtuple_step_muon is being executed, I added a printing line, which prints out at each step. I also added an output of the function call for AddNtupleRow, which returns 1.

void HistoManager::FillNtuple_step_muon(G4double energy_mu, G4double dirx_mu,…)
{

    G4cout << "Inputting data from the step in Ntuple" << G4endl;
    auto analysisManager = G4AnalysisManager::Instance();
    analysisManager->FillNtupleDColumn(1, 0,energy_mu);
   ...
    analysisManager->FillNtupleFColumn(1, 17,Mass_mu);
    G4bool output_mu = analysisManager->AddNtupleRow(1);
    G4cout << "Output of function call is " << output_mu << G4endl;

}

From the log…
… open Root analysis file : Muon_Sim_new.root - done

----> Ntuples created Muon_Sim_new.root
–> Event 0 starts.
energy is 749.294
Inputting data from the step in Ntuple
Output of function call is 1
energy is 742.654
Inputting data from the step in Ntuple
Output of function call is 1
energy is 719.013

When I try to open the Ntuples, it gives the following error, (the histograms I saved in step action still work fine).
Error in TTreeFormula::Compile: Bad numerical expression : “MuonEnergy”
Info in TSelectorDraw::AbortProcess: Variable compilation failed: {Muon Energy,}

Thanks again for the help!
Aditya

Thank you for confirming this.
Could you also let me know in which user action (class & method):

  1. you create your ntuples
  2. you call OpenFile(), Write() and CloseFile()
  3. whether you run in multithreaded mode
  4. if you run in MT, whether you set ntuple merging on

This error may be related to the space in the name of your column.

2 Likes

Hi Anna,

That was it! The issue with spaces. Sorry about my ignorance for the spaces.

Thank you Anna, Ivana and Michael. The issue is resolved.

1 Like