Error in <TROOT::WriteTObject>: The current directory (Rint) is not associated with a file. The object (Graph) has not been written

_ROOT Version: 6.22/02
Platform: Ubuntu 20.04
_Compiler: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

Dear developers,

I am having some trouble to write a TGraph object into a root file. I obtain the following error:

Error in TROOT::WriteTObject: The current directory (Rint) is not associated with a file. The object (Graph) has not been written.

Please find attached the root macro I am using.

Thanks in advance!

Kind regards,

Diego
analyzer2.C (1.5 KB)

Before calling grph->Write(), set the current directory to the output file with graphs->cd().

Dear jblomer,

I have tried what you suggested and the error is still the same.

Best,

Diego

I simplified your macro (because we do not have you data files) and applied @jblomer recommendation . The macro is now:

int analyzer2()
{
   int n = 51;
   int n2 = 11;
   TTree * TreeM;
   TBranch * Ener;
   string file_name1 = "ExN02_";
   string file_name2 = "keV.root";
   int energy = 500;
   TFile * graphs = new TFile("graphs.root", "RECREATE");

   for(int j = 0; j<n2; j++) {
      int energy2 = energy - 50;
      string prim_energy = to_string(energy);

      int kin_ener_Bfield;
      int energy_arr[n];
      int counts[n];

//       for(int i = 0; i<n; i++) {
//
//          string  ene = to_string(energy2);
//          string str = file_name1 + prim_energy + "_"+ ene + file_name2;
//          const char * file_name = str.c_str();
//
//          cout << file_name << endl;
//          TFile *file = TFile::Open(file_name, "READ");
//          TreeM = (TTree*)file->Get("TreeM");
//          counts[i] = TreeM->GetEntries("(sqrt((X-18)*(X-18)+Z*Z) < 0.4) && PID==11");
//          energy_arr[i] = energy2;
//          file->Close();
//
//          energy2 = energy2 + 2;
//       }

      TGraph *grph = new TGraph(n,energy_arr,counts);

      grph->GetYaxis()->SetTitle("Counts");
      grph->GetXaxis()->SetTitle("Kinetic Energy for B-field [keV]");
      grph->SetMarkerStyle(21);
      grph->SetMarkerColor(2);
      grph->SetMarkerSize(.5);
      graphs->cd();
      grph->Write();
      energy = energy + 10;

   }

   graphs->Close();

   return 0;
}

And the graphs are saved in the file:

% rootls  graphs.root
Graph  Graph  Graph  Graph  Graph  Graph  Graph  Graph  Graph  Graph  Graph

Well they all have the same name because you did not change it.

Dear couet,

Thanks for the answer. You werre right, it works perfectly! It was me that was repeating a previous error.

Thanks all for the help.

Best,

Diego

Actually, I would ask very quickly how can I change the name of these graphs when they are saved in the file.

Thanks,

Diego

@couet a TGraph isn’t a TNamed so there’s no way to customize its name I suppose?

Here is a new version of the reduced macro showing how to proceed.

int analyzer2()
{
   int n = 51;
   int n2 = 11;
   TTree * TreeM;
   TBranch * Ener;
   string file_name1 = "ExN02_";
   string file_name2 = "keV.root";
   int energy = 500;
   TFile * graphs = new TFile("graphs.root", "RECREATE");

   for(int j = 0; j<n2; j++) {
      int energy2 = energy - 50;
      string prim_energy = to_string(energy);

      int kin_ener_Bfield;
      int energy_arr[n];
      int counts[n];

      TGraph *grph = new TGraph(n,energy_arr,counts);
      grph->SetName(Form("Graph_number_%d",j));
      grph->SetTitle(Form("This is the Graph number %d",j));

      grph->GetYaxis()->SetTitle("Counts");
      grph->GetXaxis()->SetTitle("Kinetic Energy for B-field [keV]");
      grph->SetMarkerStyle(21);
      grph->SetMarkerColor(2);
      grph->SetMarkerSize(.5);
      graphs->cd();
      grph->Write();
      energy = energy + 10;

   }

   graphs->Close();

   return 0;
}

It gives:

$ root -l analyzer2.C  
root [0] 
Processing analyzer2.C...
(int) 0
root [1] .q
$ rootls -l graphs.root
TGraph  May 25 17:16 2021 Graph_number_0   "This is the Graph number 0"
TGraph  May 25 17:16 2021 Graph_number_1   "This is the Graph number 1"
TGraph  May 25 17:16 2021 Graph_number_10  "This is the Graph number 10"
TGraph  May 25 17:16 2021 Graph_number_2   "This is the Graph number 2"
TGraph  May 25 17:16 2021 Graph_number_3   "This is the Graph number 3"
TGraph  May 25 17:16 2021 Graph_number_4   "This is the Graph number 4"
TGraph  May 25 17:16 2021 Graph_number_5   "This is the Graph number 5"
TGraph  May 25 17:16 2021 Graph_number_6   "This is the Graph number 6"
TGraph  May 25 17:16 2021 Graph_number_7   "This is the Graph number 7"
TGraph  May 25 17:16 2021 Graph_number_8   "This is the Graph number 8"
TGraph  May 25 17:16 2021 Graph_number_9   "This is the Graph number 9"
$

Thank you so much, it works very well!

Best,

Diego

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