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:
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:
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