TTree branch type detection

Hello Guys,

I would like to find the type of the object associated to a branch.
I do have a tree with 5 branches:

  • Xvtx (float)
  • Yvtx (float)
  • Zvtx (float)
  • EPxPyPz (TLorentzVector)
  • extra (double)

Basically I would like to loop over branches and detect if it’s a float, double or TLorentzVector. Depending on that I would call different methods… Is there a way to access this ?

Thank you very much for your help !


we do this in the RDataFrame internals. A good starting point (if not the solution) can be


1 Like

This is exactly what I needed ! Thank you so much :slight_smile:

I think the solution to my problem is a mix between TTreeFormula, TTree::GetLeaf and TLeaf::GetTypeName :

TTreeFormula *tf3 = new TTreeFormula("ttree-formula", "EPxPyPz.M()", t)

I am trying to play with TTreeFormula, with the following script :

TTree *t = _file0->Get("mytree");
TTreeFormula *tf3 = new TTreeFormula("ttree-formula", "EPxPyPz.M()", t)
cout << tf3->PrintValue() << endl;

mytree does have a branch called EPxPyPz. Doing this in interpreted ROOT this is ok, I can get back my mass from TLorentzVector. But… doing the exact same in compiled ROOT doesn’t work. Do you have any guess ?

NB: I did use t->SetMakeClass(1), but I removed it and no improvement. :frowning:


did you try to process your dataset with RDataFrame?
Your code would look like:

ROOT::RDataFrame df("mytree","myfile.root");
auto M = df.Define("M","EPxPyPz.M()").Take("M");
for (auto m : M) {
   std::cout << m << std::endl;


RDataFrame is used only with ROOT6, but ok.
I don’t have found documentation yet in order to loop using t->GetEntry(i)

Reason why… I didn’t use it. I do have to loop on my tree in a more complex way.

Finally TTreeFormula is working ! The problem was due to tree->SetMakeClass(1);
On the other hand I found even much simpler… Just using:

TTree::Draw(“EPxPyPz.M()”, “”, “goff”)
And TTree::GetV1();


glad you could solve this. In order to leave a trace of this TTree::Draw / dataframe comparison, I’ll just leave the translation of the syntax above in terms of RDataFrame:

auto rdfWithMass = rdf.Define("M", "EPxPyPz.M()");
auto massH = rdfWithMass.Histo1D("M");
auto mValues = rdfWithMass.Take("M");
for (auto m : mValues) cout << m << endl;


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