Filtering a chain

I wanted to do exactly what is described here:

root.cern.ch/root/roottalk/roottalk02/0464.html

i.e. run over a TChain and make a new file containing the same TTree but only for entries passing a selection cut.

I followed the prescription described at:
root.cern.ch/root/roottalk/roottalk02/0466.html and
root.cern.ch/root/roottalk/roottalk02/0481.html

It appears to work and writes the correct number of events to the new file. However these all turn out to be copies of a single event.

Martin

Your code, please.
ROOT version number?

Rene

Sorry. Version 3.05/04.

{
TChain chain(“geg”);
chain.Add(“file1.root”); etc.

chain.GetEntry(0);

TFile *WEvents = new TFile(“WEvents.root”,“recreate”);
TTree WTree = (TTree)chain.GetTree()->CloneTree(0);

TIter eiter(elecs);

Int_t nentries = Int_t(tree.GetEntries());
Int_t nbytes = 0, nb = 0;

for (Int_t ie = 0 ; ie < nentries ; ie++) { //event loop
nb = tree.GetEntry(ie); nbytes += nb;

eiter.Reset();
  while(elec = (GegElec*) eiter()) { //loop over electrons in event

  if (elec) {
    if(selection on electron) {
      WTree->Fill(); //filling a histogram here produces desired distribution
    }
  }
} 

} //end event loop

WEvents->cd();
WTree->Write();
}

Martin

This code cannot work. What is the variable tree?
You should use chain.GetEntry instead of tree.GetEntry

Rene

Sorry, my mistake. The chain was called ‘tree’ in the code I inherited, and I changed it to ‘chain’ to make it clearer for posting.

I do have chain.GetEntry in my code.

When I run this, the new file contains the correct number of entries (ie the number of events passing the selection), but they are all copies of one entry.

Martin

This usually due to a missing

place inside the loop.

Cheers,
Philippe.

[quote]
This usually due to a missing

place inside the loop.[/quote]

I have

nb = chain.GetEntry(ie); inside my event loop.

Also, if I fill a histogram with any variable at the same point that I ask the smaller tree to be filled, then the histogram has all the different entries as expected. So it seems that I have got the entry successfully, but the new tree is being filled over and over with only one entry.

Cheers,
Martin

Hi Martin,

For any version of ROOT before 3.05/06, you have to set the branch address on the chain BEFORE cloning it. Also you HAVE TO set the addresses for ALL the branches.

For any version of ROOT on and after 3.05/06, those restriction are lifter and you set the addresses when ever you want and set a few as you need.

Cheers,
Philippe.