I have the requirement that I have root files with a large number of trees that I need to loop over and insert a branch in each (systematics trees).
The problem arises if there are multiples trees of the same name (probably due to some autosave feature outside of my control), i.e.
treeA
treeA:1
…
I don’t want to loop and save both of them, so how do I just get the newest ones?
At the moment I have something like
// Do a loop over all trees that are not nominal
TList* list = oldFile->GetListOfKeys() ;
if (!list) { printf("<E> No keys found in file\n") ; exit(1) ; }
TIter next(list) ;
TKey* key ;
TObject* obj ;
while ( key = (TKey*)next() ) {
obj = key->ReadObj() ;
// skip if not a TTree or sumWeights TTree
if ( (strcmp(obj->IsA()->GetName(),"TTree")!=0) || (strcmp("sumWeights",obj->GetName()) == 0)) {
printf("Not running over: %s \n",obj->GetName()); continue;
}
newtree = fChain->CloneTree(0); // some stuff with tree
}
TString oldkeyname;
while ( (key = (TKey*)nextkey())) {
// Keep only the highest cycle number for each key for mergeable objects. They are stored
// in the (hash) list consecutively and in decreasing order of cycles, so we can continue
// until the name changes. We flag the case here and we act consequently later.
bool alreadyseen = (oldkeyname == key->GetName());
if (alreadyseen) continue;
alreadyseen = key->GetName();
....
}
I do not understand. What is the :1 suffix? ‘key->GetName()’ returns the name of the TTree without the cycle and should be self consistent (i.e. alreadyseen is assigned the value of key->GetName() and then it is compared with the same).