Read and rename tree

Dear ROOTers,

I’m trying to read a tree from a ROOT file and rename this tree, which I did as following:

TFile *f = TFile::Open(“inputfile.root”);
TTree newtree = new TTree(“newtree”, “”);
newtree -> (TTree
)f->Get(“oldtree”); //oldtree is the tree name in the inputfile.root

If this code is in a Macro, it works, and I can access the newtree,
e.g.: newtree -> Draw(“branch”);

But when running it with a standalone compiled application, it’s not possible.
The compilation is ok , but any accession to the newtree will crash the program.

The following message I got when I running the application:

*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

#0 0x00007f056d1989be in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007f056d11df4e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007f056fcb2ac3 in TUnixSystem::StackTrace() () from /home/lin/jobs/programs/root/lib/libCore.so
#3 0x00007f056fcb479c in TUnixSystem::DispatchSignals(ESignals) () from /home/lin/jobs/programs/root/lib/libCore.so
#4
#5 0x0000000000402fd1 in main ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#5 0x0000000000402fd1 in main ()

Does anyone know what is the problem?
Thanks a lot in advance!

Try:
TFile *f = TFile::Open(“inputfile.root”);
TTree *newtree;
f->GetObject(“oldtree”, newtree);
if (!newtree) std::cout << “Error: oldtree not found.” << std::endl;
and then, if the “oldtree” has been found (so that the newtree pointer is not NULL), try:
newtree -> Draw(“branch”);

[quote=“Wile E. Coyote”]Try:
TFile *f = TFile::Open(“inputfile.root”);
TTree *newtree;
f->GetObject(“oldtree”, newtree);
if (!newtree) std::cout << “Error: oldtree not found.” << std::endl;
and then, if the “oldtree” has been found (so that the newtree pointer is not NULL), try:
newtree -> Draw(“branch”);[/quote]
Thanks a lot!
It works well now!