[ROOT] Re: filter out the chain

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Feb 01 2002 - 08:03:47 MET


Hi Dmitriy,

TTree::CloneTree returns a clone of the original object. In case of a TChain,
CloneTree returns a TChain*. You cannot fill a TChain, only a TTree.
I suggest the following changes in your code:


void filter()
{
TChain *chain = new TChain("Global");
chain->Add("file1.root");
chain->Add("file2.root");
chain->Add("file3.root"); 

  cout << " Creating new root-file ..."<< endl;
  TFile *newFile = new TFile("small.root","recreate");
  cout << " Creating new tree ..."<< endl;
  TChain *newchain = (TChain*)chain->CloneTree(0); //<====
  TTree *tree = newchain->GetTree(); //<=====

  if (fChain == 0) return;
  Int_t nentries = Int_t(fChain->GetEntries());

  Int_t nbytes = 0, nb = 0;
  for (Int_t jentry=0; jentry<nentries;jentry++) {
     Int_t ientry = LoadTree(jentry); //in case of a TChain, 
     nb = fChain->GetEntry(jentry);   nbytes += nb;

    if ( SelectionFunction() ) tree->Fill();
  
  }// end loop over events

  newFile->cd();
  tree->Write();
}

Rene Brun

Dmitriy Bandourine wrote:
> 
> Hi rooters,
> 
> I need to filter out the events containing in the chain
> of root-files using some function INSIDE the loop over events
> and then save them in new tree with analogous branch structure.
> The method with
> TTree *tree = chain.CloneTree(Tcut);
> does not suit me because, as mentioned above, I need to use a
> selection function, which return value depends on the event parameters.
> 
> My script looks like this:
> #===========================================
> void filter()
> {
> TChain *chain = new TChain("Global");
> chain->Add("file1.root");
> chain->Add("file2.root");
> chain->Add("file3.root");
> 
>   cout << " Creating new root-file ..."<< endl;
>   TFile *newFile = new TFile("small.root","recreate");
>   cout << " Creating new tree ..."<< endl;
>   TTree *tree = chain.CloneTree(0);
> 
>   if (fChain == 0) return;
>   Int_t nentries = Int_t(fChain->GetEntries());
> 
>   Int_t nbytes = 0, nb = 0;
>   for (Int_t jentry=0; jentry<nentries;jentry++) {
>      Int_t ientry = LoadTree(jentry); //in case of a TChain,
>      nb = fChain->GetEntry(jentry);   nbytes += nb;
> 
>     if ( SelectionFunction() ) tree->Fill();
> 
>   }// end loop over events
> 
>   newFile->cd();
>   tree->Write();
> }
> #==============================================
> 
> For 3.01.06 in this case I get:
> Warning in <TChain::Fill()>: may not use this method
> and nothing written to "tree".
> For 3.02.07 I get:
> *** Break *** segmentation violation
> 
> How to initialize properly "tree"?
> Obviously, the string:
>  TTree *tree = chain.CloneTree(0);
> does not help
> 
> Thanks in advance,
> Dmitri



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET