Loop in TTree with selection


I am having a hard time with a Tree analysis, if someone can help me please, I will state my case :
I have a TTree containing 3 leaves : flux & energy & flag. I would like to do that request in C++ : “Show me all the fluxes associated with an energy with flag == 1”, several fluxes are associated with a single energy value.
In pseudo-code it would be something like :

for all energies in mytree //There are 162 different energies
    if flag
    for all fluxes associated with that energy -> store them in a TH1 to get the mean etc...

How can I pass a selection over my values to a loop in a C++ macro ? I know how to do selection with TTree::Scan but I don’t think it is of use here.

Hope I have been clear enough, Thanks a lot.

Check out Chapter: Trees
I particular, search for “void tree1r” to see a basic example that shows how to loop over the tree and fill histograms, just like you want.

1 Like

TTree::Draw and TTree::Project use the same selections as TTree::Scan

1 Like

Okok I managed to get a list of my values using a ‘set’ C++ container.
I have just another question, when a selection is in a loop (as follow), how could I tell cling that in “energy == i” , i is a variable and not a char ?

for (auto const &i: energies) {
                T->Draw("flux>>myhist[temp]","flux && flag == 1 && score == 1 && energy == i ","goff");
                cout << myhist[temp]->GetMean() << " ";

Well the question is the same for “flux>>myhist[temp]” where temp is a variable…
Thanks !

for (auto const &i: energies) {
  // TString hname = "myhist_"; hname += temp; // just a precaution ...
  TString hname = "h_flux_at_"; hname += i; // just a precaution ...
  myhist[temp]->SetName(hname); // ... different histograms, different names
  // if "i" is not an "int" variable, you will need to change the "%d" below
  T->Project(myhist[temp]->GetName(), "flux",
             TString::Format("flag == 1 && score == 1 && energy == %d", i));
  std::cout << myhist[temp]->GetMean() << " ";

Thanks for the information. I will try to figure it out for more.