Skim events from a TChain into a new file

Using a TChain of several .root files containing events, I’d like to extract a subset of the events into a new .root file, depending on some calculated event properties. So I would like to make a “skim”.

I’ve looked around at various TTree and TChain cloning documentation, and came up with the following code:

TChain * chain = new TChain("CITTree");
chain->Add("Run123596/analysis_cit_1.root");

TFile *skimfile = new TFile("Skim.root","recreate"); 

TTree *newtree = chain->CloneTree(0);
newtree->Print("p");

So far so good … I see a nice print of the cloned tree. Now I try to extract what I need into the “Skim.root” file:

chain->SetBranchAddress("Run",&myRun);
chain->SetBranchAddress("Event",&myEvent);

(etc.etc. there are a lot of these!)

Int_t nevents = (Int_t)chain->GetEntries();
Int_t nbytes = 0;
Int_t nselected = 0;

for(int ievent=0; ievent<nevents; ievent++)
{
  nbytes += chain->GetEntry(ievent);

  if(nTracks <= 0) continue; // or something more complex

  nselected++;

  newtree->Fill();

}

newtree->Write();
skimfile->Close();

Root.exe crashes at the point I call “newtree->Fill()”.

What am I doing wrong?

(This is on Windows 7 running ROOT 5.21/04)

Thanks!
Julian

Hi Julian,

Proceed in the following way:

[code]TChain * chain = new TChain(“CITTree”);
chain->Add(“Run123596/analysis_cit_1.root”);
chain->SetBranchAddress(“Run”,&myRun);
chain->SetBranchAddress(“Event”,&myEvent);

(etc.etc. there are a lot of these!)
//branch addresses must be set before cloning

TFile *skimfile = new TFile(“Skim.root”,“recreate”);

chain->LoadTree(0); //force 1st tree to be loaded
TTree *newtree = chain->GetTree()->CloneTree(0);

//then as you do
[/code]
Rene

[quote=“brun”]Hi Julian,

Proceed in the following way:

[code]TChain * chain = new TChain(“CITTree”);
chain->Add(“Run123596/analysis_cit_1.root”);
chain->SetBranchAddress(“Run”,&myRun);
chain->SetBranchAddress(“Event”,&myEvent);

(etc.etc. there are a lot of these!)
//branch addresses must be set before cloning

TFile *skimfile = new TFile(“Skim.root”,“recreate”);

chain->LoadTree(0); //force 1st tree to be loaded
TTree *newtree = chain->GetTree()->CloneTree(0);

//then as you do
[/code]
Rene[/quote]

Rene,

Many thanks: this works a treat!

Julian