Hi Lorenzo, thanks so much for the prompt reply!
So, in my HistoManager.hh header file (Geant4), I define a structure:
struct TreeInfo{
TTree* tree;
G4String Label;
G4String Title;
G4bool exist;
double rndmSeed;
double evtNumber;
TString particleName;
double x;
double y;
double z;
double px;
double py;
double pz;
double trackID;
double parentID;
bool isIncident;
bool isExit;
double kineticEnergy;
double edep;
double niel;
double pdgid;
double charge;
double processType;
};
Also ,I define
void SetHisto (const G4String& label, const G4String& title, G4int,G4double,G4double,const G4String& unit="none");
void FillHisto(const G4String& label, G4double e, G4double weight = 1.0);
functions as well as a a vector
std::vector<TreeInfo> treeInfo;
In my HistoManager.cc source file, the order is the following: first, trees/histograms are set, then booked, and finally filled.
SetTree method:
void HistoManager::SetTree(const G4String& label, const G4String& title)
{
for(unsigned i=0; i<treeInfo.size(); i++)
{
if( label == treeInfo[i].Label )
{
G4cout << "---> warning from HistoManager::SetTree() : tree " << treeInfo[i].Label
<< "already exists" << G4endl;
return;
}
}
TreeInfo ti;
ti.Label = label;
ti.Title = title;
ti.exist = true;
treeInfo.push_back(ti);
}
Book method:
void HistoManager::book()
{
...
for(G4int k=0; k<int(treeInfo.size()); k++) {
if(treeInfo[k].exist) {
treeInfo[k].tree = new TTree(treeInfo[k].Label, treeInfo[k].Title);
treeInfo[k].tree -> Branch( "rndmSeed", &treeInfo[k].rndmSeed);
treeInfo[k].tree -> Branch( "evtNumber" , &treeInfo[k].evtNumber );
treeInfo[k].tree -> Branch( "particleName", &treeInfo[k].particleName);
treeInfo[k].tree -> Branch( "x", &treeInfo[k].x);
treeInfo[k].tree -> Branch( "y", &treeInfo[k].y);
treeInfo[k].tree -> Branch( "z", &treeInfo[k].z);
treeInfo[k].tree -> Branch( "px", &treeInfo[k].px);
treeInfo[k].tree -> Branch( "py", &treeInfo[k].py);
treeInfo[k].tree -> Branch( "pz", &treeInfo[k].pz);
treeInfo[k].tree -> Branch( "trackID", &treeInfo[k].trackID);
treeInfo[k].tree -> Branch( "parentID", &treeInfo[k].parentID);
treeInfo[k].tree -> Branch( "isIncident", &treeInfo[k].isIncident);
treeInfo[k].tree -> Branch( "isExit", &treeInfo[k].isExit);
treeInfo[k].tree -> Branch( "kineticEnergy", &treeInfo[k].kineticEnergy);
treeInfo[k].tree -> Branch( "edep", &treeInfo[k].edep );
treeInfo[k].tree -> Branch( "niel", &treeInfo[k].niel);
treeInfo[k].tree -> Branch( "pdgid", &treeInfo[k].pdgid);
treeInfo[k].tree -> Branch( "charge", &treeInfo[k].charge);
treeInfo[k].tree -> Branch( "processSubType", &treeInfo[k].processType);
}
}
...
}
And FillTree method:
void HistoManager::FillTree(const G4String& label, G4double rndmSeed, G4double evtNumber, TString particleName,
G4double x, G4double y, G4double z,
G4double px, G4double py, G4double pz,
G4double trackID, G4double parentID,
bool isIncident, bool isExit,
G4double kineticEnergy, G4double edep, G4double niel,
G4double pdgid, G4double charge, G4int processSubType)
{
G4int ih = GetTreeAddress(label);
if (ih < 0) {
G4cout << "---> warning from HistoManager::FillTree() : tree " << ih
<< "does not exist;" << G4endl;
return;
}
if(treeInfo[ih].exist)
{
treeInfo[ih].rndmSeed = rndmSeed;
treeInfo[ih].evtNumber = evtNumber;
treeInfo[ih].particleName=particleName;
treeInfo[ih].x = x;
treeInfo[ih].y = y;
treeInfo[ih].z = z;
treeInfo[ih].px = px;
treeInfo[ih].py = py;
treeInfo[ih].pz = pz;
treeInfo[ih].trackID = trackID;
treeInfo[ih].parentID = parentID;
treeInfo[ih].isIncident = isIncident;
treeInfo[ih].isExit = isExit;
treeInfo[ih].kineticEnergy = kineticEnergy;
treeInfo[ih].edep = edep;
treeInfo[ih].niel = niel;
treeInfo[ih].pdgid = pdgid;
treeInfo[ih].charge = charge;
treeInfo[ih].processType = double(processSubType);
}
treeInfo[ih].tree->Fill();
}
Then I set a tree in my RunAction.cc file and fill in my SteppingAction.cc file. Just in case, I also convert G4String into TString:
G4String definition = part->GetDefinition()->GetParticleName();
TString str_def = definition;
But after all of that I get the aforementioned strange TTree filling for my particle names…
Hope it will help!