# ROOT states the directory not writable even though it was opened with "UPDATE"

**URL:** <https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [January 23, 2020, 4:13pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691 "2020-01-23T16:13:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Galdiano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/galdiano/32/19626_2.png) [@Galdiano](https://root-forum.cern.ch/u/Galdiano)\
**Post date:** [January 23, 2020, 4:13pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/1 "2020-01-23T16:13:26Z")

</div>

Hello there! This is my first post here so thank you for the attention. Now, on to the problem at hand.

I’m currently trying to draw a series of histograms. To do so, I defined a function `Createf90Dist`  
, which I proceed to call numerous times inside a for loop. I then write the histograms in the desired file. Here’s a snippet of my code indicating what exactly is being done:

```auto
Createf90Dist(...){
  const char *previous_dir = gDirectory -> GetPath();

  TFile *file = new TFile(file_name); // opens the file with the desired data.

  //make the histogram named f90_dist
  // ...

  f90_dist -> SetDirectory(0);
  file -> Close();

  gDirectory -> cd(previous_dir); // return to the previous directory
  return f90_dist;
}

```

Then, in the main code, I call the function in the following way:

```auto
TFile *hist_file = new TFile(Form("hist_%d.root", run), "UPDATE");

  for (int j = 0; j < number_divisions; j++){
      if (i == 0){
        data_dir -> cd(); 
        f90hist_run[j] = Createf90Dist(Form("run_%d%s.root", run, file_suffix[i].c_str()), max_charge[i], 0., 1., number_divisions, j+1);
        f90hist_run[j] -> SetName(Form("f90_distribution%d", j));
        f90hist_run[j] -> SetTitle("f90 Distribution; f90");

        f90hist_run[j] -> Write(Form("f90_distribution%d", j), TObject::kOverwrite);
      }
  }

```

were `data_dir` is a subfolder inside the `hist_file`. When I check wether the current directory after calling the function is the desired one (`data_dir`) using `gDirectory -> pwd()` it returns the correct one. However, when actually loading the macro and running it, ROOT returns the following:

`Error in <TDirectoryFile::WriteTObject>: Directory hist_1220.root is not writable`

Note: if I, after calling the function, manually change the directory to the desired one, the code runs smoothly. I just wanted to see if there was a way of me including this step withing the function definition, as I believe I have attempted.

* * *

_ROOT Version:_ 6.16.00  
_Platform:_ Ubuntu 18.04.3 LTS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 23, 2020, 4:16pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/2 "2020-01-23T16:16:50Z")

</div>

I think @pcanal can help you.

---

<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:** [January 23, 2020, 7:06pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/3 "2020-01-23T19:06:15Z")

</div>

I am not sure why this does not work, I would need a reproducer. However there is several ways to improve the code.

```auto
const char *previous_dir = gDirectory -> GetPath();

```

rather than the “name” of the directory you could record the pointer value

```auto
auto previous_dir = gDirectory;
... 
previous_dir->cd();

```

There is also a construct to do this automatically (in one line)

```auto
TDirectory::TContext ctxt; // Will capture the current directory at construction and go back to it at destruction

```

Also you could simply ignore the current directory value and replace

```auto
f90hist_run[j] -> Write(Form("f90_distribution%d", j), TObject::kOverwrite);

```

with

```auto
data_dir->WriteObject( f90hist_run[j], Form("f90_distribution%d", j), TObject::kOverwrite);

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Galdiano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/galdiano/32/19626_2.png) [@Galdiano](https://root-forum.cern.ch/u/Galdiano)\
**Post date:** [January 27, 2020, 6:50pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/4 "2020-01-27T18:50:34Z")

</div>

Hello Philippe! Thank you for the quick answer and I apologize for taking so long to reply.

I attempted your fix, and rewrote the original code to the following:

```auto
data_dir->WriteObject( f90hist_run[j], Form("f90_distribution%d", j), TObject::kOverwrite);

```

However, doing so resulted in the following error:

```auto
error: no matching member function for call to 'WriteObject'
note: candidate function not viable: no known conversion from 'TObject::(anonymous enum at /home/miguel/Desktop/Mestrado/ROOT/root/include/TObject.h:86:4)' to 'Option_t *' (aka 'const char *') for 3rd argument
   template <class T> inline Int_t WriteObject(const T* obj, const char* name, Option_t *option="", Int_t bufsize=0) // see TDirectory::WriteTObject or TDirectoryWriteObjectAny for explanation
note: candidate function not viable: no known conversion from 'TObject::(anonymous enum at /home/miguel/Desktop/Mestrado/ROOT/root/include/TObject.h:86:4)' to 'Option_t *' (aka 'const char *') for 3rd argument
           Int_t WriteObject(void *obj, const char* name, Option_t *option="", Int_t bufsize=0); // Intentionaly not implemented.

```

Also: I’m quite new at this, so what exactly do you mean when you mention that you would “need a reproducer”?

Thank you very much for your attention,  
Miguel.

---

<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:** [January 27, 2020, 7:35pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/5 "2020-01-27T19:35:33Z")

</div>

> [@Galdiano](#):
>
> Also: I’m quite new at this, so what exactly do you mean when you mention that you would “need a reproducer”?

It means that you would to provide enough information (full source files, command lines, input data file), so that I can reproduce the problem you seen **exactly** and without any guessing.

---

<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:** [January 27, 2020, 7:40pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/6 "2020-01-27T19:40:15Z")

</div>

> [@Galdiano](#):
>
> However, doing so resulted in the following error:

My apologies, I had typos in the text, I meant:

```auto
data_dir->WriteObject( f90hist_run[j], Form("f90_distribution%d", j), "OverWrite");

```

i.e. those interface have not yet been upgrade to use the enum.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Galdiano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/galdiano/32/19626_2.png) [@Galdiano](https://root-forum.cern.ch/u/Galdiano)\
**Post date:** [January 28, 2020, 5:14pm UTC](https://root-forum.cern.ch/t/root-states-the-directory-not-writable-even-though-it-was-opened-with-update/37691/7 "2020-01-28T17:14:46Z")

</div>

I understand what you mean by “reconstruction” now. Thank you for being so clear!

I corrected the typo you mentioned. The code now runs smoothly.

Thank you for your help!  
Miguel.
