Read in list of files, TCHain

Hi all,

so far I read in one file and looped through all events and did my plots.
That works fine.
so like this and then a loop
TFile* f = TFile::Open(file);

TTree* t_truth = (TTree*)f->Get("Truth");
TTree* t_indet = (TTree*)f->Get("InDet");

/* loop over all events */

Now I need to read in say 4 input files (same structure just more events then).
so I tried the above with TChain
TChain* f = new TChain(“test”);
for (std::vector::const_iterator itr = files.begin() ; itr != files.end() ; ++itr)
{
chain.Add(itr); // add filenames to chain
}
TTree
t_truth = (TTree*)f->Get(“Truth”);
TTree* t_indet = (TTree*)f->Get(“InDet”);

so “f” is a chain pointer now. That does not work however. I guess TChain is a sort of TTree already but I dont know how to use it now …

Or, is TChain the wrong thing for that prupose?

Thanks a lot!
Andi

Hi,
a TChain behaves like a TTree, only the TTree spans over multiple files. See root.cern.ch/root/html/TChain#TChain:TChain on how to use it. In your case you need two chains, one for each TTree, even though both TChains use the same set of files.
Axel.

Hi,

damn am I stupid! I was on the page you mentioned before I posted on the forum. But I did not read the line:

TChain ch(“T”); creates a chain to process a Tree called “T”

I called the chain “test” instead of the name of a tree …

Thanks it works now :slight_smile:
Andi

Hi,

the problem now is it only works if I add only 1 file to the chain …

When I add a second one it recognises it and shows twice the amount of events but all variables are 0 now …

Giving the two files separately it is ok, but not one after the other.

TChain* t_btag = new TChain(“BTag”);

std::vector<TString>::const_iterator firstFile = files.begin();
for (std::vector<TString>::const_iterator itr = firstFile ; itr != files.end() ; ++itr)
{
  std::cout << "Adding to chain: " << *itr << std::endl;
  if (t_btag!=0) t_btag->Add(*itr);
}

and later the event cycle. So if I add more than 1 file it does not work anymore.

What am I doing wrong now?

Thanks a lot!
Andi

Hi,

that might depend on your event loop. If you use SetBranchAddress you’ll have to call it each time the chain loads a new tree. Please post how you’re trying to get the data out of the trees.

Axel.

Hi,

I do something like this

t_btag->GetBranch(“nJets”)->SetAddress(&nJets);

and perevent I write:
t_btag->GetEvent(evt);

Cheers
Andi

Hi Andi,
Use TChain::SetBranchAddress(“nJets”, &nJets);
Axel.

Thanks a lot!
it works now

I have a problem now with histos and mem leak but I will open a separate thread about it.

Cheers
Andi