Problems with reading root file with branches having different basket sizes

Hello everyone,
I need some help for reading a root file with branches having different basket sizes. I have 4 branches for 4 variables: njet (Int_t), jet_pt (Double_t), jet_eta (Double_t), and jet_phi(Double_t). The njet branch has 1 basket and the others have two baskets.

When I try to read the root file, for example the first row,
for(Int_t = 0; i < njet; i++){
cout << njet << " " << jet_pt << " " << jet_eta << " " << jet_phi << endl;

I find I can only read the data for the 0th instance, but the for loop does not iterate the remaining instances, I get:
3 196.61381 -1.626381 -1.071998
3 196.61381 -1.626381 -1.071998
3 196.61381 -1.626381 -1.071998

But what I really expect is to have:
3 196.61381 -1.626381 -1.071998
3 76.989097 -2.951812 1.4268599
3 28.976756 -2.460338 0.0617163

The variables are not of the type array, I have tried something like e.g. jet_pt[i] but it does not work. However, in each entry, it is spanned by the value of njet, so with instance 0,1,2 as shown in the picture.
Do the different basket sizes matter, and how can I read the values properly ?


Please provide the following information:

ROOT Version (e.g.5.34/36):
Platform, compiler (e.g. CentOS 7.3, gcc6.2):

well, the jet_pt, jet_eta and jet_phi variables really should be either C arrays (e.g.: Double_t jet_pt[NMAX]; where NMAX is a compile-time constant) or pointers to C dynamic arrays (e.g.: Double_t *jet_pt;)

  • could you show us (more of) the code that does the reading?
  • could you post somewhere accessible an example ROOT data file?

Hi sbinet,
This is my code:

void Hinv_Signal_MC(){
TFile *f1 = new TFile("tree.root");
TTree *tree; f1.GetObject("tree", tree);
tree->Print();  //This shows the branch in the figure in my original post
tree->Scan("njet : jet_pt : jet_eta : jet_phi");   //This shows the data in the figure in my original post

for(Int_t i=0; i< njet; i++){
  cout << njet << "\t" << jet_pt  << "\t" << jet_eta << "\t" << jet_phi << endl;

And this gives
3 196.61381 -1.626381 -1.071998
3 196.61381 -1.626381 -1.071998
3 196.61381 -1.626381 -1.071998

From the tree->Print() results it shows that the jet_pt, jet_eta, jet_phi variables are Double_t but not Double_t array[njet], not sure how it can give 0,1,2 instances.


Hi Benjy,

How are the variable ‘njet’ (etc.) connected to the TTree? [In the code snippet they are not and thus can not possibly be updated with values coming from the TTree]


another to ask what I am getting at:

if you can’t post a minimal ROOT data file exposing the same issue, could you post the output file coming from:

root> tree->MakeClass("foo");

(so we could reason about what ROOT thinks the various branches are…)

This is the zipped root file. It can be extracted by tar -zxvf tree.tar.gz . It contains the tree.root file. tree.tar.gz (3.0 MB)


well, running TTree::MakeClass("foo"); on that tree, I do get (excerpt):

   Int_t           njet;
   Double_t        jet_pt[8];   //[njet]
   Double_t        jet_eta[8];   //[njet]
   Double_t        jet_phi[8];   //[njet]
   Double_t        jet_mass[8];   //[njet]
   Int_t           jet_newId[8];   //[njet]
   Int_t           jet_puId[8];   //[njet]
   Double_t        jet_btagCSV[8];   //[njet]
   Double_t        jet_rawPt[8];   //[njet]
   Double_t        jet_mcPt[8];   //[njet]

so: definitely an array :slight_smile:

here is the complete definition (in Go).
foo.go.txt (29.5 KB)

Thank you very much! It works now!



just for fun, here is the output of my root-dump command that can inspect the content of (almost) any ROOT TFile+(TTree,TH1x,TH2x,TGraphXyz):

$> root-dump ./tree.root | head -n 20
>>> file[./tree.root]
key[000]: tree;1 "treeProducerSusyAlphaT" (TTree)
[000][run]: 1
[000][lumi]: 153
[000][evt]: 115789
[000][isData]: 0
[000][xsec]: 3.782
[000][puWeight]: 1
[000][nTrueInt]: 59.30733871459961
[000][genWeight]: 3.74429
[000][rho]: 31.17837129589326
[000][nVert]: 41
[000][Flag_badMuonFilter]: 1
[000][Flag_badChargedHadronFilter]: 1
[000][Flag_badMuonMoriond2017]: 1
[000][Flag_badCloneMuonMoriond2017]: 1
[000][badCloneMuonMoriond2017_maxPt]: 0
[000][badNotCloneMuonMoriond2017_maxPt]: 0
[000][nBJetMedium40]: 1
[000][isrBackgroundWeight]: 1

$> root-dump ./tree.root | egrep '\[(njet|jet_(pt|eta|phi))\].' | head -n 20
[000][njet]: 3
[000][jet_pt]: [196.61381312908495 76.98909734069169 28.97675647797403]
[000][jet_eta]: [-1.6263817548751822 -2.9518125057220486 -2.4603381156921404]
[000][jet_phi]: [-1.0719984769821167 1.426859974861145 0.061716306954622276]
[001][njet]: 2
[001][jet_pt]: [156.2645418534702 28.89591678034609]
[001][jet_eta]: [-2.647308826446539 2.3680179119110107]
[001][jet_phi]: [3.086350917816162 1.4642609357833862]
[002][njet]: 2
[002][jet_pt]: [143.6024769182453 87.69304722825161]
[002][jet_eta]: [-1.3077993392944338 2.6002755165100098]
[002][jet_phi]: [-0.13026915490627292 -2.498077392578125]
[003][njet]: 2
[003][jet_pt]: [93.90415031256983 90.60436925234518]
[003][jet_eta]: [-1.7082693576812762 1.0746610164642334]
[003][jet_phi]: [2.6499032974243164 -0.7656230330467224]
[004][njet]: 2
[004][jet_pt]: [92.28848793505415 72.44200765348626]
[004][jet_eta]: [-2.5379352569580154 2.6085867881774902]
[004][jet_phi]: [-1.666649341583252 -2.5946624279022217]

Interesting! It looks very useful!

great :slight_smile:

you might also be interested in:

that can compare the contents of 2 trees in 2 files.
pretty useful, if I may say so myself :stuck_out_tongue:

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