# Loop in TTree with selection

**URL:** <https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210>\
**Category:** Newbie\
**Created:** [August 6, 2021, 10:27am UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210 "2021-08-06T10:27:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [August 6, 2021, 10:27am UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/1 "2021-08-06T10:27:21Z")

</div>

Hi,

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 :

```auto
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.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 6, 2021, 10:57am UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/2 "2021-08-06T10:57:16Z")

</div>

Check out [Chapter: Trees](https://root.cern/root/htmldoc/guides/users-guide/Trees.html)  
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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 6, 2021, 11:00am UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/3 "2021-08-06T11:00:25Z")

</div>

[TTree::Draw](https://root.cern/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45) and [TTree::Project](https://root.cern/doc/master/classTTree.html#ac8709d1729aac2ff342dba3a06b4e006) use the same selections as [TTree::Scan](https://root.cern/doc/master/classTTree.html#af8a886acab51b16d8ddbf65667c035e4)

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [August 6, 2021, 1:09pm UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/4 "2021-08-06T13:09:27Z")

</div>

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 ?

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

```

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 6, 2021, 2:57pm UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/5 "2021-08-06T14:57:34Z")

</div>

```cpp
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() << " ";
  temp++;
}

```

---

<div class="post-metadata">

**Author:** ![Feeney](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/feeney/32/27276_2.png) [@Feeney](https://root-forum.cern.ch/u/Feeney)\
**Post date:** [September 2, 2021, 8:44am UTC](https://root-forum.cern.ch/t/loop-in-ttree-with-selection/46210/6 "2021-09-02T08:44:19Z")

</div>

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