Looping over a TTree

Hi ROOT Experts,

I am having a hard time figuring how to loop over a tree and print out one of the entries. This seems like it should be an exceptionally simple thing to do since I can see exactly what I wan
t with TTree::Scan.

For a simple, concrete example, I would like to loop over and print out “px” from the tree created in the tree1.C tutorial example. Can you please help me to fill in the blanks in my example
macro?

root.exe $ROOTSYS/tutorials/tree/tree1.C
(then .q to exit)
root.exe Example.C

where Example.C is:

Example(){

  TFile *_file0 = TFile::Open("tree1.root");
  //t1->Scan()
  //t1->GetBranch("px")->Print()

  for(int i=0; i<t1->GetEntries(); i++){
    //t1->Print? t1->GentEntry(i)->? t1->GetBranch("px")->?
  }

}

Thanks,
Will

Hi Wile E Coyote,

Yes, I am fully aware of the documentation referenced by that thread and found it to be of no help. I have made a class for a TTree in the past using one of those tools. I am looking to do something far more simple. All I would like to do is to loop over a TTree and print one of the leaves. Is it not possible to do this in only a few simple lines?

I assume my simple macro is just missing one line inside the forloop.

Thanks,
Will

I found the answer in ftp://root.cern.ch/root/doc/ROOTUsersGu … 12s14.html

This is the six line example that I was striving for:

Example(){

  TFile *_file0 = TFile::Open("tree1.root");

  Float_t px;
  t1->SetBranchAddress("px",&px);
  for(int i=0; i<t1->GetEntries(); i++){
    t1->GetEntry(i);
    cout<<"px: "<<px<<endl;
  }

}

… which can be run as:

root.exe $ROOTSYS/tutorials/tree/tree1.C
(then .q to exit)
root.exe Example.C

Cheers,
Will

1 Like

Where is t1 defined?

TTree *t1 = (TTree *) _file0->Get("t1");

should be added