You are here

How to Use Chains (Lists of Files)?

TChain object is a list of Root files containing the same tree. As an example, assume we have 3 Root files called file1.root, file2.root, file3.root. Each file contains one tree called "T". We can create a chain with the following statements:

   TChain chain("T");

The class TChain is derived from the class TTree. For example, to generate an histogram corresponding to the attribute "x" in tree "T" by processing sequentially the 3 files of this chain, we can do:


The following statements illustrate how to set the address of the object to be read and how to loop on all events of all files of the chain.

TH1F* hnseg(nullptr);
void processChain(){
//   Create the chain as above
   TChain chain("T");

//   Create an histogram
   hnseg = new TH1F("hnseg","Number of segments for selected tracks",4096,0,8192);

//   Specify address where to read the event object
//   In the program writing the files, the event was stored in a branch called "event"
   Event *event = new Event();   //object must be created before setting the branch address
   chain.SetBranchAddress("event", &event);

//   Start main loop on all events
//   In case you want to read only a few branches, use TChain::SetBranchStatus
//   to activate/deactivate a branch.
   Int_t nevent = chain.GetEntries();
   for (Int_t i=0;i<nevent;i++) {
      chain.GetEvent(i);              //read complete accepted event in memory
      hnseg->Fill(event->GetNseg());  //Fill histogram with number of segments

//  Draw the histogram