TTrees not hadding properly

I have an analysis script that takes as input a root file and creates a new root file containing a tree with one branch that my script fills. However, I noticed that if I hadd two of my input root files and run the script on that input, the output I obtain is different than what I get if I run my analysis script on those two individual input root files, and then hadd those two output root files. But these definitely should be the same; I have TH2Ds in my output files that match regardless of where the hadding occurs. I’m not sure how to hadd these output file trees properly?

The relevant code in my script:

int hitsPer1cmVoxel;
TFile* outfile = new TFile(Form("CRTana_%s",input_crtfile.c_str()),"recreate");
TTree *tree = new TTree("tree","tree of hits per voxel");
tree->Branch("hitsPer1cmVoxel", &hitsPer1cmVoxel, "hitsPer1cmVoxel/I");

...

for (int i = 1; i <= (xyzBins[0]/voxelSize[0]); i++) {                                                        
      for (int j = 1; j <= (xyzBins[1]/voxelSize[0]); j++) {
        for (int k = 1; k <= (xyzBins[2]/voxelSize[0]); k++) {                                                                           

          hitsPer1cmVoxel = hitcount_xyz_th3d[0]->GetBinContent(i, j, k);
          tree->Fill();

        }
      }
    }

outfile->Write();
outfile->Close();

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.16/00
Platform: Ubuntu
Compiler: g++


Can you describe in which way the two outputs differ (e.g. number of entries)? Would it be possible to attach sample files of the two outputs?

I first noticed that they different in the number of entries, but when I just now tried tree->Print() I get different output for other parameters too (e.g. baskets, compression):

For files hadded before running script (this is what I expect):

tree->Print()
******************************************************************************
*Tree    :tree      : tree of hits per voxel                                 *
*Entries : 62060544 : Total =       249036250 bytes  File  Size =    2131333 *
*        :          : Tree compression factor = 119.95                       *
******************************************************************************
*Br    0 :hitsPer1cmVoxel : hitsPer1cmVoxel/I                                *
*Entries : 62060544 : Total  Size=  249035877 bytes  File Size  =    2074828 *
*Baskets :     7778 : Basket Size=      32000 bytes  Compression= 119.95     *
*............................................................................*

For files hadded after running script:

root [2] tree->Print()
******************************************************************************
*Tree    :tree      : tree of hits per voxel                                 *
*Entries : 124121088 : Total =       498071782 bytes  File  Size =    4155411 *
*        :          : Tree compression factor = 122.89                       *
******************************************************************************
*Br    0 :hitsPer1cmVoxel : hitsPer1cmVoxel/I                                *
*Entries :124121088 : Total  Size=  498071409 bytes  File Size  =    4050560 *
*Baskets :    15556 : Basket Size=      32000 bytes  Compression= 122.89     *
*............................................................................*

Hm, the second case has exactly twice the number of entries than the first one. Could it be that for the second run, files were hadd’ed before and after running the script?

No, each hadd only occurred once.
I just redid the procedure and get the same result. It goes like this:

First output:
1). hadd haddedInput.root input1.root input2.root
2). ./scriptExecutable haddedInput.root output.root
I get one file (output.root) and open this

Second output:
1). ./scriptExecutable input1.root output1.root
2). ./scriptExecutable input2.root output2.root
3). hadd output.root output1.root output2.root

For some reason, the two output.root files from both methods don’t match.

Additionally, when I tried hadding more than 2 files with these methods, the result in output #2 was more than double the number of entries in output #1.

Apologies for having overlooked your reply!

Would it be possible for you to attach or send to me the input files and the scripts for debugging?

I have actually solved the problem, it was something to do with the rest of the code that’s specific to my analysis.