# pyROOT changing and writing to subdirectory

**URL:** <https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217>\
**Category:** Newbie\
**Created:** [June 1, 2022, 1:47pm UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217 "2022-06-01T13:47:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntoikka](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ntoikka/32/29868_2.png) [@ntoikka](https://root-forum.cern.ch/u/ntoikka)\
**Post date:** [June 1, 2022, 1:47pm UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217/1 "2022-06-01T13:47:54Z")

</div>

Hi!

I’m currently trying to write a histogram from a dataframe to a .root files subdirectory. The creation of the directory works and writing of the histogram, but it doesn’t go to the subdirectory. The problem is probably when changing the working directory, so how should it be done?

The code looks like (there’s some minor stuff before this):

```auto
myFile = ROOT.TFile.Open(outFileName, "RECREATE")

eta_low = 0.5
eta_up = eta_low+0.5
limits = f"Jet_pt[(abs(Jet_eta) < {eta_up}) && (abs(Jet_eta) > {eta_low}) && (Jet_pt > {pt_val})]"
dir_name = f"eta_{str(eta_low)}-{str(eta_up)}"
hist = df.Define("pT", limits).Histo1D("pT")
    
etadir = myFile.mkdir(dir_name)
cd = myFile.cd(dir_name)
myFile.WriteObject(hist.GetPtr(), "pT")

```

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![ntoikka](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ntoikka/32/29868_2.png) [@ntoikka](https://root-forum.cern.ch/u/ntoikka)\
**Post date:** [June 1, 2022, 2:59pm UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217/2 "2022-06-01T14:59:09Z")

</div>

I now got it working as wanted with gDirectory by changing the last few lines such that:

```auto
etadir = myFile.mkdir(dir_name)
etadir.cd()
ROOT.gDirectory.WriteObject(hist.GetPtr(), "pT")
myFile.Close()

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 16, 2022, 7:26am UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217/3 "2022-06-16T07:26:45Z")

</div>

Hi @ntoikka ,

welcome to the ROOT forum and sorry for the high latency, this must have slipped through the cracks!

I _think_ `myFile.cd(dir_name)` should be `etadir.cd()` and then `etadir.WriteObject(...)`, @pcanal can confirm. I’m glad you found a workaround though.

Cheers,  
Enrico

---

<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:** [June 17, 2022, 3:59pm UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217/4 "2022-06-17T15:59:40Z")

</div>

```auto
cd = myFile.cd(dir_name)

```

makes `'dir_name'` the “current” directory (the value of `gDirectory`)

```auto
myFile.WriteObject(hist.GetPtr(), "pT")

```

explicitly request to (ignore `gDirectory` and to) write the object in the ‘directory’ `myFile` (the pointer to the file is also the pointer to the top level directory of the file).

---

<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:** [June 17, 2022, 4:15pm UTC](https://root-forum.cern.ch/t/pyroot-changing-and-writing-to-subdirectory/50217/5 "2022-06-17T16:15:37Z")

</div>

> [@eguiraud](#):
>
> should be `etadir.cd()` and then `etadir.WriteObject(...)`,

Yes that would work too.
