Root write what I did not ask

Dear experts,
I want to write a tree in a file, but root write other trees in that file. Can you please tell me how to tell root to write only the tree that I want in the file (tree->Write())? Indeed if you run this code you’ll see that it writes 2 trees in the output file.
This is the code that I’m using: calpas.web.cern.ch/calpas/h2taus/copytree.C
and the root file that it reads: calpas.web.cern.ch/calpas/h2taus … uM125.root
Regards

Hi,

you can “detach” trees from the current file (actually, directory) with root.cern.ch/doc/master/classTT … 06bd806b4b

Cheers,
Danilo

Dear Danilo,
does it mean that I can’t disable what makes root write tree that I did not ask?
Regards

Try: itree->SetName("atree"); TTree *newtree = itree->CloneTree();

Hi Pepe,
it does not work, I still have 2 trees write.
Regards

Hi,

no, it does not. That is the way to detach from the current file the TTree you don’t want to write.

Cheers,
Danilo

Dear Danilo,

sorry but I’m confused with the “detach”. Maybe do you have a few line of code to illustrate what you mean?

Regards

Hi,

mytree.SetDirectory(nullptr);

Cheers,
D

Dear Danilo,
is there a specific place in the code to put that line?
If I do [1] it does not write the tree in the mt directory.
If I do [2] it does not write any of them.
Regards

[1]
void copytree(){

string path{"/lstore/cms/calpas/h2taus/CMSSW_8_2_0/src/miniAOD/miniAnalyzer/ntuple/"};

vector vfile {“VBFHToTauTauM125.root”};
vector vdir{“mt”, “et”};

for( auto file : vfile){
string input {path+file};
TFile* ifile = new TFile(input.c_str(),“READ”);
if (!ifile->IsOpen()) { cout<<"can’t open "<<input<<endl; return; }

for(auto dir : vdir){
  // get the input file tree
  string treename{"h2taus/sync/"+dir};
  TTree *itree = (TTree*) ifile->Get(treename.c_str());

  // output
  string outputfile{"syncntuple/calpas_vbf_"+dir+".root"};
  TFile* ofile = new TFile(outputfile.c_str(),"RECREATE");

  itree->SetName("atree");
  TTree *newtree = itree->CloneTree();
   newtree->SetDirectory(nullptr);   // call it here!!
  ofile->Write();
  delete ofile;
}
delete ifile;

}
}

[2]
void copytree(){
string path{"/lstore/cms/calpas/h2taus/CMSSW_8_2_0/src/miniAOD/miniAnalyzer/ntuple/"};

vector vfile {“VBFHToTauTauM125.root”,};
vector vdir{“mt”, “et”};

for( auto file : vfile){
string input {path+file};
TFile* ifile = new TFile(input.c_str(),“READ”);

for(auto dir : vdir){
  // get the input file tree
  string treename{"h2taus/sync/"+dir};
  TTree *itree = (TTree*) ifile->Get(treename.c_str());

  // output
  string outputfile{"syncntuple/calpas_vbf_"+dir+".root"};
  TFile* ofile = new TFile(outputfile.c_str(),"RECREATE");

  itree->SetName("atree");
  TTree *newtree = itree->CloneTree();
  ofile->Write();
   newtree->SetDirectory(nullptr);   // call it here!!
  delete ofile;
}
delete ifile;

}
}

Hi,

what do you want to achieve exactly? Copy a tree out of a file and put it into another one?

Cheers,
D

Dear Danilo,
yes. I used these files to do that [1] but it copies trees that I did not ask.
Regards

[1]
This is the code that I’m using: calpas.web.cern.ch/calpas/h2taus/copytree.C
and the root file that it reads: calpas.web.cern.ch/calpas/h2taus … uM125.root

Hi,

we can have a look to the macro, but if you just need to move a tree from a file to another, did you consider the rootcp commandline tool?

Cheers,
D

Dear Danilo,
no I did not because I want to use the macro to do that. But I’ll try other ways and see.
Thank you all for your help again.
Regards

As far as I can tell, you original script is doing exactly what you want except for a minor detail.

The order the script does thinks is

     TTree *newtree = itree->CloneTree();
     newtree->SetName("atree");

which means that any backup copies of the meta data will be stored under the old name. (Those are cycles) and then you rename the TTree and (are likely) to get the final copy of the meta data under that name.
Try

     itree->SetName("atree");
     TTree *newtree = itree->CloneTree();

Cheers,
Philippe.

Dear Philippe,

ok, thank you for your answer, I’ll try this.

Regards

I’m having the same problem. I create a vector of trees (setting each of their directories to 0), fill these, then open a file, merge these trees, and write the merged tree. The unmerged trees also get written to the file.