# Looping over the Tree branches

**URL:** <https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232>\
**Category:** Newbie\
**Created:** [July 17, 2019, 4:18am UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232 "2019-07-17T04:18:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 17, 2019, 4:18am UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/1 "2019-07-17T04:18:31Z")

</div>

Hi,

I have a rootfile which has a tree and it has branches (energy, channel, board, energyshort, timestamp). I want to loop over all the channel and board number. How to do that. I have the macro which does for one channel and one board number.[psdB0C6.C](https://root-forum.cern.ch/uploads/short-url/kYFWAXfOQ33QnJ5GGYV1VwZ9Syp.C) (752 Bytes)

Here is the root file.

> **[compass\_run\_244.root](https://drive.google.com/file/d/1ZWEnOGk-xOU3KfKQMwa-ecoJS6oI4mn2/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [July 17, 2019, 6:43am UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/2 "2019-07-17T06:43:54Z")

</div>

You can try using [`TTree::GetListOfBranches()`](https://root.cern.ch/doc/master/classTTree.html#acd351ad0dda7aa50e2ac9254b3dc9f0e) to loop over all branches in your file.

---

<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 17, 2019, 7:21am UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/3 "2019-07-17T07:21:28Z")

</div>

I don’t know for what reason the [TTree::GetListOfBranches](https://root.cern.ch/doc/master/classTTree.html#acd351ad0dda7aa50e2ac9254b3dc9f0e) method would need to be used.

Here’s something that might really help:

```auto
{
  TFile *f = TFile::Open("compass_run_244.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  TTree *t; f->GetObject("Data", t);
  if (!t) { delete f; return; } // just a precaution
  gROOT->cd(); // newly created histograms will not be connected to "f"
  Int_t Board_min = Int_t(t->GetMinimum("Board") + 0.5);
  Int_t Board_max = Int_t(t->GetMaximum("Board") + 0.5);
  Int_t Channel_min = Int_t(t->GetMinimum("Channel") + 0.5);
  Int_t Channel_max = Int_t(t->GetMaximum("Channel") + 0.5);
#if 0 /* 0 or 1 */
  // each Board and Channel pair in a separate canvas
  for (Int_t i = Board_min; i <= Board_max; i++) {
    for (Int_t j = Channel_min; j <= Channel_max; j++) {
      TCanvas *c = new TCanvas(TString::Format("c_%d_%d", i, j),
                               TString::Format("Board %d Channel %d", i, j));
      t->Draw(TString::Format("(EnergyShort+rndm(1)-0.5)/(Energy+rndm(7)-0.5) : Energy+rndm(3)-0.5 >> hist_%d_%d(1500,0,1500,50,0.9,1.0)", i, j),
              TString::Format("Board==%d && Channel==%d && Energy>50", i, j),
              "colz");
      c->Modified(); c->Update(); // make sure it’s really (re)drawn
    }
  }
#else /* 0 or 1 */
  // each Board and Channel pair in a separate pad (all in one canvas)
  TCanvas *c = new TCanvas("c", "All boards and channels");
  c->DivideSquare((Board_max - Board_min + 1) * (Channel_max - Channel_min + 1));
  Int_t pad = 1;
  for (Int_t i = Board_min; i <= Board_max; i++) {
    for (Int_t j = Channel_min; j <= Channel_max; j++) {
      c->cd(pad++);
      t->Draw(TString::Format("(EnergyShort+rndm(1)-0.5)/(Energy+rndm(7)-0.5) : Energy+rndm(3)-0.5 >> hist_%d_%d(1500,0,1500,50,0.9,1.0)", i, j),
              TString::Format("Board==%d && Channel==%d && Energy>50", i, j),
              "colz");
      gPad->Modified(); gPad->Update(); // make sure it’s really (re)drawn
    }
  }
  c->cd(0);
  c->Modified(); c->Update(); // make sure it’s really (re)drawn
#endif /* 0 or 1 */
  delete f; // automatically deletes "t", too
}

```

---

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 17, 2019, 5:55pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/4 "2019-07-17T17:55:21Z")

</div>

Thanks a lot for the help. Can I save all the histograms in a root file instead of projecting it on the terminal?

---

<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 17, 2019, 6:03pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/5 "2019-07-17T18:03:21Z")

</div>

If you do not want to draw histograms, replace `"colz"` with `"goff"` (and do not create any canvases).

If you want to save all histograms in a ROOT file, replace `gROOT->cd();` with:

```cpp
TFile *fout = TFile::Open("output_file.root", "recreate");

```

and right before or after `delete f;` add:

```cpp
if (fout) fout->Write();
delete fout; // automatically deletes all histograms, too

```

---

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 17, 2019, 6:20pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/6 "2019-07-17T18:20:40Z")

</div>

I tried this but the file is empty.  
NO histograms are saved in the file.

---

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 17, 2019, 6:24pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/7 "2019-07-17T18:24:07Z")

</div>

It works now. Thanks

---

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 17, 2019, 6:26pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/8 "2019-07-17T18:26:19Z")

</div>

How t get the colored histograms? After saving it to file it is all black.

---

<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 17, 2019, 6:30pm UTC](https://root-forum.cern.ch/t/looping-over-the-tree-branches/35232/9 "2019-07-17T18:30:04Z")

</div>

`SomeHisto->Draw("colz"); // Draw Option: colz`
