Looping over the Tree branches

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 (752 Bytes)

Here is the root file.

You can try using TTree::GetListOfBranches() to loop over all branches in your file.

I don’t know for what reason the TTree::GetListOfBranches method would need to be used.

Here’s something that might really help:

{
  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
}
1 Like

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

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:

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

and right before or after delete f; add:

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

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

It works now. Thanks

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

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