Tree filling by <vector>double

Hello, in this file of a Geant4 simulation I’m filling a ROOT Tree.
Currently I’m adding the branches regarding beam

tree1->Branch("BeamX", &BeamX); 
tree1->Branch("BeamY", &BeamY); 
tree1->Branch("BeamZ", &BeamZ); 
tree1->Branch("BeamKinE", &BeamKinE);
tree1->Branch("BeamTheta", &BeamTheta);

}

being BeamX, BeamY, etc are the vectors

void AnalysisManager::FillhPrimaries(G4ThreeVector fPos,G4double fkinE,G4ThreeVector fmom)
{
....
  BeamX.push_back(fPos.x());
  BeamY.push_back(fPos.y());
  BeamZ.push_back(fPos.z());
  BeamKinE.push_back(fkinE);
  BeamTheta.push_back(fmom.theta()/deg); 
}

I used the vector push back as @Dilicus showed me and it worked for secondary particles.
In this case, I’ve a problem.
I.e. if I clean the variables

void AnalysisManager::ClearVariables()
...
  BeamX.resize(0);
  BeamY.resize(0);
  BeamZ.resize(0);
  BeamKinE.resize(0);
  BeamTheta.resize(0);

I get an empty graph when plotting the branches

If I don’t clean the variable, I get an uncorrect number of entries. (larger that simulated particles). Then, I don’t know how to save in the TTree fPos.x(), fPos.y(), fPos.z(), fkinE, fmom.theta .
Thanks

AnalysisManager.cc (10.5 KB)
AnalysisManager.hh (3.8 KB)

Hi @faca87,

After reading your post, it’s not immediately apparent to me where are you placing the call to ClearVariables(). In principle, you should do it before the first call to FillhPrimaries(), for each entry in the TTree.

OT: Additionally, instead of calling resize(0), you can do clear(), which I find more descriptive.

Cheers,
J.

Hi @jalopezg
ClearVariables is called in other file (EventAction)

void EventAction::BeginOfEventAction(const G4Event* evt)
{
...
`analysismanager->ClearVariables();`
}

Instead, fillprimary particles is called in primarygeneratoraction file.
Then following your suggestion to clear the variables before than fillprimaryparticles, I added the call to a new function in primarygeneratoraction file

void PrimaryGeneratorAction::Generate_reaction(G4Event* anEvent)
{

analysismanager->ClearPrimaries();
}

and I clear variables in the analysismessenger:

void AnalysisManager::ClearPrimaries(){
	 BeamX.clear();
  BeamY.clear();
  BeamZ.clear();
  BeamKinE.clear();
  BeamTheta.clear();
}

but now, when I fill the Tree, I get a lower number of entries than simulated particles

AnalysisManager.cc (10.5 KB)
AnalysisManager.hh (3.8 KB)
EventAction.cc (3.6 KB)

PrimaryGeneratorAction.cc (2.0 KB)

Hello, I solved myself

Hi @faca87,

Sorry for the delayed reply. Given that the problem was tied to your specific case, I don’t think it’s worth it if you share it with the rest of the community. Anyways, thanks for letting me know! :slight_smile:

Cheers,
J.

Hi @jalopezg

I call the clear variables function at the end of event action

void EventAction::EndOfEventAction(const G4Event* evt)
{
...

analysismanager->ClearPrimaries();

}

and it works.
The reason because initially I got a lower number of events was a flag that save all the events or just the detected ones! I forgot to select by the flag to store all the events…then I initially got a lower number of events because they were the detected one! switching on the storage of all simulated events, I got the right number of events!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.