Problems Cloning a TTree

Hi all

After a lot of trying, reading the forum post and played with the root examples I want your help :slight_smile:
I produced with makeClass a .C and .h file based on an ntuple that I am having. I built my analysis
on the .C file and everything is working fine. Lately I thought about from the giving ntuple to build
a new one and write there only the events that are passing the cuts. So you can imagine I cannot :slight_smile:

I followed several examples on that with no luck. So what I am doing:
I am having a function called run in wich I pass all the files I want for the analysis. This builds the
chain of files and is calling the Loop function to loop over the entries. In the Loop function on Line
104-107 I make a new file with a clone tree from the old one. I put inside the filename fixed cause
I cannot do it dynamically. (in run function I usually enter something like run(mc00*) and gets all
the files in the directory). After that on line 123 I copy the branch addresses of the oldtree to the
newtree and on line 472 I have newtree->Fill() (after all the cuts). I write the tree close the file and etc

The result is a tree with the same branches but with zero values. I really cannot explain this. An help
is much appreciated. I attach the files you need to give a try here. (.C .h) The ntuple is placed in
afs/cern.ch/user/g/giakov/public/ called mcSample.root

I have root 5.27/04 on my mac with leopard 10.5.8 with gcc 4.2.1 Also to mention I classically compile
the .C file inside root .L file.C+ with no problem.

Many Thanks in advance

George
DataD3PDPhysicsOpt.h (875 KB)
DataD3PDPhysicsOpt.C (27.4 KB)

Hi,

The part that is missing in your code is a call to oldtree->GetEntry(iEntry);
Without this call, you never load/read the data you are intending on copying. Also in using the parameter iEntry I assume that the entries of the ‘oldtree’ are exactly aligned with the entry of the tree that you are using to decide the cut (i.e. the one pointed to by fChain). Also the call to CopyAddresses in your case is superfluous has it is already done by the call to CloneTree.

Cheers,
Philippe.

Dear Philippe

Thank you so much…it worked :slight_smile:)
I wanted also to ask if it’s possible to take the file dynamically. I pass the files inside the run function like this:

run(“MonteCarlo00*”)

because I have
MonteCarlo0001.root
MonteCarlo0002.root
MonteCarlo0003.root
…

and it’s working but when I’m trying to get the tree of the fChain Iike that
TTree oldtree = (TTree)fChain->GetCurrentFile()->Get(“physics”);
where physics it’s the name of my TTree I get *** Break *** segmentation violation

I modified the code like that:

............ TTree *oldtree = (TTree*)fChain->GetCurrentFile()->Get("physics"); TFile newfile("small.root","recreate"); TTree *newtree = oldtree->CloneTree(0); ........

I forgot, sorry

Best Regards

George

Replace the line

TTree *oldtree = (TTree*)fChain->GetCurrentFile()->Get("physics"); by

TTree *oldtree = fChain->GetTree(); If you still have a problem, post your latest code and data files such that we can investigate.

Rene

Dear Rene

Thanks for the response. I changed the code but still getting *** Break *** segmentation violation.
I attach the new one .C and the .h is the same as above. Also my ntuple is in
afs/cern.ch/user/g/giakov/public/ called mcSample.root

Thanks in advance

Best Regards
George
DataD3PDPhysicsOpt.C (27 KB)

You forgot to post DataD3PDPhysicsOpt.h
Could you post a file where you eliminate 99% of the unnecessary stuff?

Rene

Dear Rene I had posted in my first post. It’s the same.
I will attach it also here. I will provide you in a while also a smaller .C

Thanks

Best Regards
George
DataD3PDPhysicsOpt.h (875 KB)

Dear Rene

Here I attach a very small version of my .C file with irrelevant lines removed.

Thanks again

George
DataD3PDPhysicsOpt.C (1.69 KB)

Hi George,

The TTree object returns by TTree oldtree = (TTree)fChain->GetCurrentFile()->Get(“physics”);
or by fChain->GetTree() will be destroyed the next time the chain move to a new file and thus,
the oldtree->GetEntry() will segfault. What you will need to do is something like:

TTree *oldchain = fChain->Clone(); // Duplicate the chain itself) ... TTree *newtree = oldchain->CloneTree(0); // Duplicate the structure ... oldchain->GetEntry(iEntry); ... newtree->Fill();
Note this complication is necessary in your case because you can not directly (as this time) Clone a TTree/TChain that is set in MakeClass mode.

Cheers,
Philippe.

Dear Philippe

I tried your sugestion but I am getting an error during compilation:
error: invalid conversion from ‘TObject*’ to ‘TTree*’

I tried also TChain *oldchain = fChain->Clone(); but I am getting a similar error.

Any ideas?

Many Thanks

Best Regards

George

Hi,

Use:TChain *oldchain = (TChain*)fChain->Clone();.

Cheers,
Philippe.

Dear Philippe

It’s working as a charm

Thanks a lot