Problem with CloneTree and filtering

I’m having problems merging and filtering a chain of root files. Basically the filtering is having no effect on the new tree being filled.

root version 4.00/08

The code is as follows

[code]
void merge_and_cut()
{
gROOT->Reset();

TChain *chain = new TChain(“h1”);
chain->Add(“run06459.root”);
chain->Add(“run06458.root”);
chain->Add(“run06457.root”);

chain->GetEntry(0);

TFile *newFile = new TFile(“merged.root”,“recreate”);

TTree tree = (TTree)chain->GetTree()->CloneTree(0);

Int_t n = Int_t(chain->GetEntries());

for (Int_t i=0; i<n; i++)
{
chain->GetEntry(i);
if ( selectionfunction() ) tree->Fill();
}

newFile->cd();
tree->Write();
}[/code]

I can remove the for (Int_t i=0; i<n; i++) {...} loop and still the new tree is filled. Any idea what is going on here?

Thanks

Ewan

gROOT->Reset();Should only be used in a unnamed macro (and never inside a function).

you probably meantTTree *tree = (TTree*)chain->CloneTree(0);

[quote]I can remove the … loop and still the new tree is filled. Any idea what is going on here?[/quote]I don’t see why this would be (unless there is somehow an ambiguity on which code you actually are using).

I noted:if ( selectionfunction() ) tree->Fill(); From the portion of code you provide I don’t see how selectionfunction can give a result that depends on the content of the entry … thus I assume that the code you copied is only a summary of what you do … and that you likely removed the portion that causes the problem.

If you can provide a running example showing your issue, I would be able to help you track it down.

Cheers,
Philippe.

Hi,
A working code example is as follows. Any suggestions as to why the entire tree is being copied would be most appreciated!

void mergesupport()
{
  
  TChain *chain = new TChain("h1");
  chain->Add("/work/area2/ewan/PSI/rootfiles/run06459.root");
  chain->Add("/work/area2/ewan/PSI/rootfiles/run06458.root");
  chain->Add("/work/area2/ewan/PSI/rootfiles/run06457.root");
  
  chain->GetEntry(0);
  
  TFile *newFile = new TFile("merged.root","recreate");
      
  //  TTree *tree = (TTree*)chain->GetTree()->CloneTree(0); changed to:

  TTree *tree = (TTree*)chain->CloneTree(0);

  Int_t n = Int_t(chain->GetEntries());

  TBranch        *b_Bdev0;
  Float_t         Bdev0;
  b_Bdev0 = chain->GetBranch("Bdev0");
  chain->SetBranchAddress("Bdev0",&Bdev0);

  for (Int_t i=0; i<n; i++) 
    {
      chain->GetEntry(i); 
  
      if ( (Int_t(Bdev0)) == 0 ) tree->Fill();
    }
  
  newFile->cd();
  tree->Write();

}

The data files can be found at

www.ph.ed.ac.uk/~ewan/data/run06459.root
www.ph.ed.ac.uk/~ewan/data/run06458.root
www.ph.ed.ac.uk/~ewan/data/run06457.root

Thanks

Ewan

When I run your code on the first file I get an output file containing only 63630 entries instead of 120893 entries. How do you assert that the entire chain is copied?
Anyway most likely this is a numerical error issue, you wrote

and Bdev0 is a float with value 0 or 1. If you any reason you machine assign/think that the value slightly less than 1, it will be rounded to 0.
In your case you should guarantee yourself the correct result by doing:

Cheers,
Philippe.

Hi,
It looks like it was the sorting function after all! And there was me thinking that I’d debugged that bit. I now have code functionaling as expected.

Thanks!

Ewan