Reading all branches of TTree from file

Hello,
I am using a method I found here (I found this too, but it has the same “issue”) to read a root file. It works fine, but I need to open multiple branches and for each of them I have to define my own variable and assign to the values from the branch to it… Is it possible to open all the branches that are inside the root file and assigning them to variables with the same name?
Thank you,
Vangelis

This awkwardly possible. You could do (but I would not recommend it):

Double uniqueVariable;
TBranch *br1 = nullptr;
TBranch *br2 = nullptr;
tree->SetBranchAddress("br1",&uniqueVariable,br1);
tree->SetBranchAddress("br2",&uniqueVariable,br2);
for each entry {
    br1->GetEntry(entryNumber);
    // use the value retrieved for br1
    br2->GetEntry(entryNumber);
   // use same variable to use the value retrieved for br2. 
}

It is really impractical as you then can not combine (without introduce more variable) the values retrieved from various branches …

Cheers,
Philippe.

Now, you might have meant, is there a way to reduce the number of variables that are needed to be declared explicitly and the number of SetBranchAddress. If this is the case, checkout out either the TTreeReader (ROOT: TTreeReader Class Reference) or the result of TTree::MakeSelector (ROOT: TTree Class Reference)

Hi Vangi,
out of curiosity, why do you want to assign all branches to the same C++ variable? For each entry in the TTree this variable should take many different values; how would you expect that to work?

If you mean you want to have each branch of a TTree as an element of an array, that is definitely possible. For example you can assign branch “a” to “array[0]” and branch “b” to “array[1]”.

Here there are some more small snippets of code (and links to tutorials) that show various methods to read data from a root file. Maybe you’ll find one that suits your needs.

Cheers,
Enrico

Hi, Enrico and Philippe,
What I meant is like in the 1st link, the proposed solution contains:

double totE;
int pixel;
......
tree->SetBranchAddress(“pixel”,&pixel);
tree->SetBranchAddress(“totE”,&totE);

So the person who wrote it had to check what type pixel and totE are, create his own variables pixel, totE and assign them values from the ones inside the file. At least this is how I understand it. Now, I have about 30 such things to “load”, and I was wondering if there was an easier way for ROOT to see all of my branches and automatically assign the values to variables with the same name (as is manually done in the example above, instead of something like
double TotalEnergy;
tree->SetBranchAddress(“totE”,&TotalEnergy);

)
That would mean I won’t have to write 60 lines of code, keeping track of what type each of the variable is…
An example of the code would be, given a file containing branches of type float px,py,pz,mass, type bool is_electron,is_muon:

void readTree(){
TFile *myfile= new TFile(“test4pixels.root”,“READ”);
TTree tree= (TTree)myfile->Get(“tree”);
//The "magic" part:
tree->SetAllBranchAddresses; <- this creates variables 
float Energy=px*px+py*py+pz*pz+m*m;
if(is_electron==true)
    cout<<"electron with energy "<<Energy<<endl;
else if(is_muon==true)
    cout<<"muon with energy "<<Energy<<endl;
else 
    cout<<"Unknown particle!"<<endl;

Edit: an example code.
Cheers,
Vangi

Hi Vangi,
I’m afraid C++ itself does not allow what you want.

Depending on your actual use-case you might want to check out pyROOT (ROOT’s python interface): something similar to what you ask is available, see e.g. here.

Or check out the link I shared in my previous answer for higher-level ways of reading TTrees in C++.

Cheers,
Enrico

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