Branches with different number of entries

Dear all,
I’m trying to write a TTree with two branches, but these branches have a different number of entries.
In details, I would like to write informations about events in the first branch; in the second branch I would insert informations about each track of an event in every layer of my detector. To do this, I used the method TBranch::Fill(). Just to give you an idea of what I’m doing, I report a simple and short demonstration case:

[code]TFile *f1 = new TFile(“myfile.root”,“RECREATE”);
TTree *t = new TTree(“t”,"");

TBranch *ev;
TBranch *trk;

//definition of two structures: event, track

ev = t->Branch(“event”,&event,“Ep/D:px/d:py/D:pz/D:idev/I”);
trk = t->Branch(“trk”,&track,“px/D:py/D:pz/D:sx/D:sy/D:E/D:idev/I:id/I:layer/I:lp/I:mp/I:pp/I”);

//first loop on events
for(int i = 0 ; i < nEvents ; ++i) {

  //the structure event is filled
  ev->Fill();

 
 //second loop inside the first one on the tracks on each layer 
 for(int itrk=0 ; itrk<ntracks ; itrk++){
   
      //the structure track is filled
      trk->Fill(); 
 }

}

f1->Write();
f1->Close(); [/code]
I have no errors in executing this script, and if I look to the created tree in the file with t->Print(), I have this output:

[code]******************************************************************************
*Tree :t : *
*Entries : 0 : Total = 360905 bytes File Size = 186992 *

  •    :          : Tree compression factor =   1.93                       *
    

*Br 0 :event : Ep/D:px/d:py/D:pz/D:idev/I *
*Entries : 3 : Total Size= 982 bytes File Size = 170 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *

*Br 1 :trk : px/D:py/D:pz/D:sx/D:sy/D:E/D:idev/I:id/I:layer/I:lp/I: *

  •     | mp/I:pp/I                                                        *
    

*Entries : 4963 : Total Size= 359559 bytes File Size = 185932 *
*Baskets : 12 : Basket Size= 32000 bytes Compression= 1.93 *
[/code]

Notice that in this case the loop on events arrived just to nEvents=2.

How can I access data in the branches? The method t->Draw() doesn’t work, I suppose because the Entries in the tree are 0!
Is this the correct way to do the job? Do you have any suggestion?
Thank you.

Regards,
Fabio

1 Like

When you fill branches one by one you must also set the number of entries in teh Tree itself via
t->SetEntries(number of entries);
My recommendation: do not try to fill a tree with branches having different number of entries.
In your simple case, always call t->Fill(). The compression algorithm will take care of eliminating entries with teh same values.

Rene

Dear Rene,
thanks for the fast reply.
I’ve seen that, regarding the size of the branch, there are no problems in calling t->Fill().
The output of t->Print() is now this:

[code]******************************************************************************
*Tree :t : *
*Entries : 4966 : Total = 540305 bytes File Size = 184025 *

  •    :          : Tree compression factor =   2.78                       *
    

*Br 0 :event : Enu/D:px/d:py/D:pz/D:idev/I *
*Entries : 4966 : Total Size= 180100 bytes File Size = 1650 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 96.86 *

*Br 1 :micro : px/D:py/D:pz/D:sx/D:sy/D:E/D:idev/I:id/I:layer/I:lp/I: *

  •     | mp/I:pp/I                                                        *
    

*Entries : 4966 : Total Size= 359944 bytes File Size = 182375 *
*Baskets : 11 : Basket Size= 32000 bytes Compression= 1.93 *

[/code]
But the reason why I want to fill separately the branches is that the first one (called “event”) contains general informations regarding the event, like for exemple the energy of the incoming particle, and I don’t want to have these info replicated in this branch for each entry of the other one (called “trk”), which contains info related to the tracks of each event.

This is because I want an easy and fast access to the general info of the event, like energy distribution of the incoming particle, for example with something like t->Draw(“event.Ep”), but in this way the distribution is distorted by replicated entries for each event.

Do you have any suggestion?

Regards,
Fabio

As I said the compression algorithm (in your case 96) will eliminate the similar entries.
You can also create a separate Tree containing only the run dependent variables.

Rene

Hello, for me, I can see that the compression algorithm is working because I see the output when I print the tree. However if I try to do > t->Draw(“event.Enu”) (following the example below) I will see 4966 instead of the 3 event entries.

 When you said "As I said the compression algorithm (in your case 96) will eliminate the similar entries" (previous email), this means that, similar entries will be erased from the tree, no?. I have been playing with f1->SetCompressionLevel(1) (2,3,4), and still I have the same number of entries.