How to access each Tree in a TChain

Dears experts,

I have several trees, one for each sample (backgrounds + signal), I want to be able to put all of them in a chain like the TChain does, but then I want to be able to access independently each tree sample in the .C file, so that in one run I can make separate histogram for backgrounds and signal. For example, in one run I want to be able to:
loop over background 1 -> Fill histogram bkg1 -> save root file 1
loop over background 2 -> Fill histogram bkg2 -> save root file 2

loop over background n -> Fill histogram bkgn -> save root file n

As I understood the TChain will add all the samples in one Tree, so I can´t access independently each tree. Does anyone have an idea how I can do that with root?

Best regards

If you want to access individual ROOT files, do NOT create a TChain. Simply loop over your files, and analyse one at a time.

Dear Coyote,

but can I still make a MakeClass on one tree and then do not specify this tree to be read in the header, but instead to make a loop on all the tree directly in the .C file?

Best

If you look at the skeleton source code generated by TTree::MakeClass, you’ll notice that the class constructor has one parameter “(TTree *tree)”. You can use your own “tree” (you open ROOT files yourself and pass a pointer to an existing “tree” to the class constructor).
Of course, you can also “enhance” the source code, adding additional parameters here and there and/or adding class methods (e.g. which would allow you to “open” and “close” files one by one on demand) and so on.

Dear Coyote,

  • I was wondering if I couldn´t use the Tree friends as well for that? Is there some constraint? did I misundertood the Tree friends function?
  • Actually I want to know if ROOT provide a code to analyse all the NTuples in parallel so that I do not need to modify the initial code.

Best

Well, you can simply run multiple independent ROOT sessions simultaneously (e.g. start one “root” per each ROOT file).
Otherwise, see PROOF and PROOF: Parallel Processing, and in particular PROOF-Lite.
Note: you will need to use TTree::MakeSelector or TTree::MakeProxy (I don’t know which one is preferred) instead of TTree::MakeClass.

Dear Coyote,

thank you very much for you help, I managed to make what I needed with one of the idea you proposed.

Best