Copy TTree

Hi,

I have a ntuple and I want to copy it with some cuts. The cut process is a little lengthy so I can not do simply CopyTree(selection). Here is my sample code where I want to take cut on x and y variables:

TChain *chain = new TChain("EVENT");
chain->Add("*.root");
int nEvent = chain->GetEntries();

TFile *file = new TFile("EventCut.root", "recreate");
TTree *newtree = chain->CloneTree(0);

Float_t x, y;
TBranch b_x, b_y;
chain->SetBranchAddress("x", &x, &b_x);
chain->SetBranchAddress("y", &y, &b_y);

for(int i=0; i<nEvent; i++){
    if(!cut(x, y)) continue;          // cut routine which return 1 if satisfied, otherwise 0
    newtree->Fill();
}

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

Now, my problem is the variables which I have accessed (here x and y) are not filled with actual values in the newtree. Either they are not filled or filled with some arbitrary values. Any idea will be appreciated.
Thanks.

Hi,

It may be a side-effect of the copy/paste but the code you have never reads the data from the input chain (i.e. it is missing a chain->GetEntry(i) );

Cheers,
Philippe.

Sorry, it is a typo. chain->GetEntry(i) is there.

Hi,

Then I don’t see any obvious problem. You could try to move the SetBranchAddress before the CloneTree. For further investigation, I would need a complete running example.

Cheers,
Philippe.