# TKey, TObject and GetListOfKeys for only newest ttrees

**URL:** <https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928>\
**Category:** ROOT\
**Created:** [August 16, 2017, 2:28pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928 "2017-08-16T14:28:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwsmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jwsmith](https://root-forum.cern.ch/u/jwsmith)\
**Post date:** [August 16, 2017, 2:28pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/1 "2017-08-16T14:28:07Z")

</div>

Hi there,

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

```auto
     // 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
      }

```

but this will loop over ALL trees.

Thanks in for the help!  
Josh

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 16, 2017, 2:30pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/2 "2017-08-16T14:30:25Z")

</div>

[Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2)

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [August 16, 2017, 3:36pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/3 "2017-08-16T15:36:48Z")

</div>

Try with something like:

```auto
   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();
          
           ....
      }

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![jwsmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jwsmith](https://root-forum.cern.ch/u/jwsmith)\
**Post date:** [August 16, 2017, 4:30pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/4 "2017-08-16T16:30:17Z")

</div>

Thanks for this, Philippe.

However, after testing, it seems `alreadyseen` will always evaluate to false given tree names I use, i.e.

```auto
treeA
treeA:1

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [August 16, 2017, 4:37pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/5 "2017-08-16T16:37:54Z")

</div>

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).

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![jwsmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jwsmith](https://root-forum.cern.ch/u/jwsmith)\
**Post date:** [August 16, 2017, 4:59pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/6 "2017-08-16T16:59:59Z")

</div>

The trees look like the attached.  
Yes, I’m also surprsed this isn’t working.

![](https://root-forum.cern.ch/uploads/default/original/2X/9/9de2c6ed13ad66f04ffbbbbd6ea8c72009b83c6f.png)

The loop looks like this. At no point do I ever reach “ERROR”.

```auto
 // 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 ;

      std::string oldkeyname;
      while ( (key = (TKey*)next()) ) {

        bool alreadyseen = (oldkeyname == key->GetName());

        std::cout <<alreadyseen <<std::endl;
        if (alreadyseen){
        std::cout << "ERROR"<<std::endl;
        continue;
        }
        alreadyseen = key->GetName();
        TChain *fChain=nullptr;
        TTree *newtree=nullptr;

        obj = key->ReadObj() ;
        if ( (strcmp(obj->IsA()->GetName(),"TTree")!=0) || (strcmp("sumWeights",obj->GetName()) == 0)
          || (strcmp("nominal",obj->GetName()) == 0) || (strcmp("nominal_Loose",obj->GetName()) == 0)) {
          printf("Not running over: %s \n",obj->GetName()); continue;
        }

        fChain = new TChain(obj->GetName());

        fChain->Add((file).c_str());

        newtree = fChain->CloneTree(0);
       if(fChain->GetEntries() == 0){
         std::cout<<"No events, skipping"<<std::endl;
         continue;
       }
        m_add_branches(fChain,newtree,m_neuralNet); // some external function

        newfile->cd();
        newtree->Write();

        delete newtree;
        delete fChain;

      } // end loop over trees

```

Cheers,  
Josh

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [August 16, 2017, 5:16pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/7 "2017-08-16T17:16:15Z")

</div>

There was a fatal bug in my code snippet ☹  
Replace

```auto
alreadyseen = key->GetName();

```

with

```auto
oldkeyname = key->GetName();

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![jwsmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jwsmith](https://root-forum.cern.ch/u/jwsmith)\
**Post date:** [August 16, 2017, 5:24pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/8 "2017-08-16T17:24:28Z")

</div>

Wow. I should have seen that, sorry!  
Thanks, it’s working now. 🙂  
Cheers,  
Josh

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 30, 2017, 5:24pm UTC](https://root-forum.cern.ch/t/tkey-tobject-and-getlistofkeys-for-only-newest-ttrees/25928/9 "2017-08-30T17:24:36Z")

</div>

Closed on August 30, 2017, 5:24pm UTC

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