In one file, I have two dirs and each one has a branch named the same. What I want, is to get the TBranch from dir1 and replace it over the dir2. I ve tried something similar to the copytree example but without much success. So in the code below, the “genweight” is the commonly existing branch, which I want to copy from the “initroottree” to the makerootree dir… How can I do this ?
[code]
//Get old file, old tree and set top branch address
TFile oldfile = new TFile(“output.root”);
TTree oldtree = (TTree)oldfile->Get(“makeroottree/AC1B”);
TTree oldtree2 = (TTree)oldfile->Get(“initroottree/AC1B”);
oldtree->SetBranchStatus("",1);
oldtree->SetBranchStatus(“genweight”,0);
//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile(“small.root”,“recreate”);
newfile->mkdir(“makeroottree”);
newfile->cd(“makeroottree”);
//oldtree2->AddFriend(oldtree);
TTree *newtree = oldtree->CloneTree();
//TTree *newtree2 = oldtree2->CloneTree();
newtree= oldtree2->CloneTree();
//newtree->Print();
newfile->Write();
delete oldfile;
delete newfile;[/code]
The code is the one I pasted in my first post (slightly different from the copytree.C example). I also attach a small .root file to be used as input output.root (79.4 KB)
The usual way to handle this is to use a Friend tree rather than rewrite the TTree. Nonetheless the way to strickly do want you want (assume genweight is a double) is:[code]//Get old file, old tree and set top branch address
TFile *oldfile = new TFile(“output.root”);
TTree oldtree = (TTree)oldfile->Get(“makeroottree/AC1B”);
TTree oldtree2 = (TTree)oldfile->Get(“initroottree/AC1B”);
//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile(“small.root”,“recreate”);
newfile->mkdir(“makeroottree”);
newfile->cd(“makeroottree”);
//oldtree2->AddFriend(oldtree);
TTree *newtree = oldtree->CloneTree(-1);
Thanks - Another approach I found is to simply fill a histogram from the initroottree and just save it in the new.root. Could you please point me to how to read the tree with the highest cycle in a given TKEY ? I have two branches with “;1” “;2” or “;2” “;3” and I want to take the one with the highest key - I am trying something like this [*] but I still always get the branch with the lower cycle.
Thanks in advance
[code] oldfile->cd();
TDirectory *current_sourcedir = gDirectory;
//gain time, do not add the objects in the list in memory
Bool_t status = TH1::AddDirectoryStatus ();
TH1::AddDirectory (kFALSE);
// loop over all keys in this directory
TChain *globChain = 0;
//TIter nextkey (current_sourcedir->GetListOfKeys ());
TIter nextkey (((TDirectory *) current_sourcedir->Get (“makeroottree”))->GetListOfKeys ());
TKey *key, *oldkey = 0;
while ((key = (TKey *) nextkey ())) {
This is the default behavior of GetObject:dir = ((TDirectory *) current_sourcedir->Get ("makeroottree");
TTree *tree; dir->GetObject("treename",tree");
TH1F *hist; dir->GetObject("histoname",hist);
I want to get the “;2” one - and in advance if it is “2” the highest or maybe “3” … so, the default behaviour that you are giving me takes the very first one ie the “1”…
[quote] so, the default behaviour that you are giving me takes the very first one ie the “1”…[/quote]This should not be the behavior, GetObject should return the highest cycle. If this is the case for your use case, please let us know how to reproduce it.
It seems to me that you need to search for “cycle” and/or “namecycle”, for example, in the “ROOT User’s Guide” -> “Input/Output” chapter.
BTW. If you write an object with a specific “name” to a file, and no object with such a “name” exists in this file yet, then it’s newly assigned “cycle” will be “1” (so that you will see “name;1”, when listing the new contents of this file).
The file has only one cycle for each keys:root [1] gFile->ls()
TFile** /Users/pcanal/Downloads/cycles.root
TFile* /Users/pcanal/Downloads/cycles.root
KEY: TDirectoryFile initroottree;1 initroottree
KEY: TDirectoryFile makeroottree;1 makeroottreeIn order for us to provide you the best help possible, please provide exactly why you try, the result you see and how it differs from your expectation.
if I run the code I pasted in my message at 14:46 , you will see that the small.root file has indeed a makeroottree/AC1B but if you open any leaf, you will see that has it has ~35k entries like the “AC1B;2” and not 50k like the “AC1B;3”. So, what I asked is how to get the branch with the “latest” branch AC1B (probably I confused the “cycle” with namecycle"
You of course need both the CloneTree (to ask for the cloning of the tree) and the newfile->Write (to finalize the writing of the file) [Please re-read the I/O and TTree chapter in the User’s guide for more details).
And yes, this will (in your case) result in 2 cycles on the file. The data is not duplicated, just the meta-data (the number of entries), the cycle is backup information in case something goes wrong in the middle of the process.