# loopdir.C for Pyroot

**URL:** <https://root-forum.cern.ch/t/loopdir-c-for-pyroot/27926>\
**Category:** ROOT\
**Created:** [February 14, 2018, 1:40am UTC](https://root-forum.cern.ch/t/loopdir-c-for-pyroot/27926 "2018-02-14T01:40:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![uperez25](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/uperez25/32/13779_2.png) [@uperez25](https://root-forum.cern.ch/u/uperez25)\
**Post date:** [February 14, 2018, 1:40am UTC](https://root-forum.cern.ch/t/loopdir-c-for-pyroot/27926/1 "2018-02-14T01:40:46Z")

</div>

Hi,

I am starting to learn how to plot with python and I was wondering if this loop can be done with python. My input is a simple root file with several branches containing histograms.

[loopdir.C](https://root.cern.ch/root/html/tutorials/io/loopdir.C.html)

```auto
void loopdir() {
   // example of script to loop on all the objects of a ROOT file directory
   // and print on Postscript all TH1 derived objects
   // This script uses the file generated by tutorial hsimple.C
   //Author: Rene Brun

   TFile *f1 = TFile::Open("hsimple.root");
   TIter next(f1->GetListOfKeys());
   TKey *key;
   TCanvas c1;
   c1.Print("hsimple.ps[");
   while ((key = (TKey*)next())) {
      TClass *cl = gROOT->GetClass(key->GetClassName());
      if (!cl->InheritsFrom("TH1")) continue;
      TH1 *h = (TH1*)key->ReadObj();
      h->Draw();
      c1.Print("hsimple.ps");
   }
   c1.Print("hsimple.ps]");
}

```

I would like to be able to rootfile.Get(“branchhistoname”) and then plot them in a loop like in the  
script above.

Thank you in advance!  
Uzziel

---

<div class="post-metadata">

**Author:** ![uperez25](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/uperez25/32/13779_2.png) [@uperez25](https://root-forum.cern.ch/u/uperez25)\
**Post date:** [February 14, 2018, 2:09am UTC](https://root-forum.cern.ch/t/loopdir-c-for-pyroot/27926/2 "2018-02-14T02:09:05Z")

</div>

Hi,

I think I got what I need from this link.

> [@Loop over all objects in a ROOT file](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/4):
>
> I just found this thread and thought I’d post an alternative, I think neater solution. All the intelligence is in the ~7 line getall function. I’ve not checked whether it’s particularly memory efficient since it runs perfectly fast for my current need, which is to just list ROOT all file contents… but being a generator it at least doesn’t try to put all the objects in memory at the same time! import ROOT def getall(d, basepath="/"): "Generator function to recurse into a ROOT file/dir and y…

Sharing it just in case google search leads people here.

Cheers,  
Uzzie

---

<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:** [February 28, 2018, 2:09am UTC](https://root-forum.cern.ch/t/loopdir-c-for-pyroot/27926/3 "2018-02-28T02:09:19Z")

</div>

Closed on February 28, 2018, 2:09am UTC

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