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.
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;
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.
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.
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.
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,}