Home | News | Documentation | Download

Creating and filling root tree in separate functions

I am struggling with filling a TTree which I create with one function, then pass it back to another function which fills it. It seems as though the tree is being filled the correct number of times, but only with the initialised values (all zeroes).

I’m adding some code exceprts to illustrate my problem, hopefully they are clear.

This is the function that creates the tree with its branches

TTree* CreateTree(RAW raw_new, CUBENEW cube_new, MONITORNEW monitor_new){
TTree* CubeTree = new TTree("CubeTree","CubeTree");
CubeTree->Branch("Raw",&raw_new,"rs_x1b/s:rs_x1f:rs_y1b:rs_y1t:rs_x2b:rs_x2f:rs_y2b:rs_y2t:rs_x3b:rs_x3f:rs_y3b:rs_y3t:rs_t1:rs_t2:rs_t3:rs_t3a:rs_t3b:rs_t1a:rs_t1b:rs_t1c:rs_t1d:RF");
CubeTree->Branch("Cube",&cube_new,"EBack/S:EFront:EBackSmall:TBack:TFront:TBackSmall:XBack:YBack:XFront:YFront:XBackSmall:YBackSmall:TBackSmallA:TBackSmallB:TBack_A:TBack_B:TBack_C:TBack_D");
CubeTree->Branch("Monitors",&monitor_new,"FissionPulser/s:Monitor1:Monitor2:Mon_Tac:T1T2_Tac");
return CubeTree;
}

Which is then called in a function ‘eventgen’, and filled with simulated data:

int eventgen(char* sortfile, int Nevents){

RAW raw_new = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
CUBENEW cube_new = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
MONITORNEW monitor_new = {0,0,0,0,0};
TTree* CubeTree = CreateTree(raw_new,cube_new,monitor_new);

while(Nbevent<Nevents){
//some calculations are done and variables populated
cube_new.XBack = x_ch;
cube_new.YBack = y_ch;
cube_new.EBack = 0.5*mwpcBin.mass[i]*pow(mwpcBin.Vlab[i],2);
t_ns -= (rxn.T0 + rxn.dT);
cube_new.TBack = quadCorr(t_ns,&mwpcCorr[mwpcBin.detID[i]],&mwpcBin,i);
cube_new.XBackSmall = -10000;
cube_new.YBackSmall = -10000;
cube_new.EBackSmall = -10000;
cube_new.TBackSmall = -10000;
CubeTree->Fill();
CubeTree->Write("",TObject::kOverwrite);
CubeOutput->Close();
}

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.10/08
Platform: Ubuntu 18.04
Compiler: gcc v4:7.4.0


Use

(and/or passing by pointer), the original version is passing copies of the object to the function.

Thank you for the prompt reply and solution to my beginners problem!