Clone a TTree into a separate file

When I am trying to copy a file to a different file, I end up getting an error that looks like this :


Error in <TFile::ReadBuffer>: error reading all requested bytes from file newFile.root, got 0 of 28863
Error in <TFile::ReadBuffer>: error reading all requested bytes from file newFile.root, got 0 of 28863
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

My attempt at copying a tree to a different file

    TFile *if = new TFile("somefile.root");

    TTree *tr = (TTree*)inf->Get("QCDAna/tree2");

    TFile *of = new TFile("newFile.root", "recreate");
    of->cd();
    tr->Write("blah");
    of->Close();
    if->Close();

Try

    TFile *if = new TFile("somefile.root");

    TTree *tr = (TTree*)inf->Get("QCDAna/tree2");

    TFile *of = new TFile("newFile.root", "recreate");
     tr->CloneTree(-1,"fast");

    of->Write();
    delete of; 
    delete if;

You also seem to have a typo in your example, it should be:

TFile *if = new TFile("somefile.root");
TTree *tr = (TTree*)iff->Get("QCDAna/tree2");

(if, not inf in the Get call).

“if” is probably quite a bad variable name, in many programming languages :slight_smile:

@Graipher, obviously a bad choice, :grin:
thanks for pointing out

@pcanal, do I have to also do
tr->CloneTree(-1,"fast")->Write();

The command you gave worked, but nothing shows up as copied. But, if I chain it with Write, it worked !

Indeed I was missing a call to Write in my code snippet (I update my post).

You can indeed either call Write on the TTree or the TFile in this case.

Cheers,
Philippe.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.