# Loop over TList of TTrees

**URL:** <https://root-forum.cern.ch/t/loop-over-tlist-of-ttrees/50978>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [July 27, 2022, 7:59am UTC](https://root-forum.cern.ch/t/loop-over-tlist-of-ttrees/50978 "2022-07-27T07:59:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@Cas](https://root-forum.cern.ch/u/Cas)\
**Post date:** [July 27, 2022, 7:59am UTC](https://root-forum.cern.ch/t/loop-over-tlist-of-ttrees/50978/1 "2022-07-27T07:59:02Z")

</div>

Dear all,

I cannot seem to figure out how to loop over a TList that contains TTrees.  
On  
[https://root.cern.ch/doc/master/classTList.html](https://root.cern.ch/doc/master/classTList.html)  
there are five ways provided to loop over a TList, but none seem to help me.

The code I use

```auto
    TString input_path_hits_FCT = "MyFCTPath";
    TFile *inputFCT_hits = new TFile(input_path_hits_FCT, "read");
    TTree *hitTreeFCT = (TTree*)inputFCT_hits->Get("o2sim");

    TString input_path_hits_FT3 = "MyFT3Path";
    TFile *inputFT3_hits = new TFile(input_path_hits_FT3, "read");
    TTree *hitTreeFT3 = (TTree*)inputFT3_hits->Get("o2sim");

    // Set branch addresses - hits files
    std::vector<Hit>* hitFCT = nullptr;
    hitTreeFCT->SetBranchAddress("FCTHit", &hitFCT);
    Int_t nFCTEvents = hitTreeFCT->GetEntries();

    // FT3
    std::vector<Hit>* hitFT3 = nullptr;
    hitTreeFT3->SetBranchAddress("FCTHit", &hitFT3);
    Int_t nFT3Events = hitTreeFT3->GetEntries();

    // List for TTrees of detectors hit files
    TList *detectorTrees = new TList;
    detectorTrees->Add(hitTreeFCT);
    detectorTrees->Add(hitTreeFT3);

```

How do I then loop over this TList?  
If I take the first example from the TList documentation page

```auto
for(const auto&& obj: *GetListOfPrimitives())
   obj->Write();

```

How does this piece of code understand about which TList we are talking about?  
An example of how to loop over a TList would help a lot!

Thanks in advance.  
Cas

\_Platform: C++

---

<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:** [July 27, 2022, 8:09am UTC](https://root-forum.cern.ch/t/loop-over-tlist-of-ttrees/50978/2 "2022-07-27T08:09:43Z")

</div>

`grep -r GetListOfPrimitives ${ROOTSYS}/t[eu]*`

BTW. In your case, you would probably need something like this: `for(const auto&& obj: *detectorTrees)`

---

<div class="post-metadata">

**Author:** ![Cas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@Cas](https://root-forum.cern.ch/u/Cas)\
**Post date:** [July 27, 2022, 8:26am UTC](https://root-forum.cern.ch/t/loop-over-tlist-of-ttrees/50978/3 "2022-07-27T08:26:33Z")

</div>

I see, so the answer was quite simple after all. It worked, thanks!
