Root macro to write the .root file in the currently directory

I have just posted half of my macro. I want to write a root file in the currently directory where macro is but i got falied to write and got this Errors i.e

Error in <TROOT::WriteTObject>: The current directory (Rint) is not associated with a file. The object (new) has not been written.
Error in <TROOT::WriteTObject>: The current directory (Rint) is not associated with a file. The object (difference) has not been written.
#include <iostream>
#include <sstream>
#include <fstream>
#include <TGraphErrors.h>
void rt_copy_new()
{
   TCanvas *c1 = new TCanvas("c1","graph1",200,10,800,600);

  std::ifstream inputFile1("/atlashome/msohail/calibration/athena/MuonCalibration_20.7.5/WorkArea/run/calibration/rts/Rt_BIL_5_-4.dat");
   std::string line1;
   double xarr1[105], yarr1[105], zarr1[105];
   double xarr_err1[105]={105*0};
   double yarr_err1[105]={105*0};
   double zarr_err1[105]={105*0};
   int i =0;
   while(getline(inputFile1, line1))
   {
      if (!line1.length() || line1[0] == '#')
         continue;
      std::istringstream iss(line1);
      double x = 20., y = 20., z= 20.;

      iss>>z>>y>>x;
      zarr1[i]=x;
      yarr1[i]=y;
      xarr1[i]=z;
      i++;
      std::cout<<"point:"<<x<<' '<<y<<std::endl;
   }
   TGraphErrors* gr_xy1= new TGraphErrors(i,yarr1,xarr1,yarr_err1,xarr_err1);
   TGraphErrors* gr_yz1= new TGraphErrors(i,yarr1,zarr1,yarr_err1,zarr_err1);
   gr_xy1->Draw("apl");
   gr_xy1->Write("old");
   gr_xy1->GetXaxis()->SetTitle("Time");
   gr_xy1->GetYaxis()->SetTitle("Radius");
//second file
   std::ifstream inputFile("/atlashome/msohail/calibration/athena_21/run/calibration/rts/Rt_BOL_5_-5.dat");
   std::string line;
   double xarr2[105], yarr2[105], zarr2[105];
   double xarr_err2[105]={105*0};
   double yarr_err2[105]={105*0};
   double zarr_err2[105]={105*0};
   int j =0;
   while(getline(inputFile, line))
   {
      if (!line.length() || line[0] == '#')
         continue;
      std::istringstream iss(line);
      double x = 20., y = 20., z= 20.;

      iss>>z>>y>>x;
      zarr2[j]=x;
      yarr2[j]=y;
      xarr2[j]=z;
      j++;
      std::cout<<"point:"<<x<<' '<<y<<std::endl;
   }
   TGraphErrors* gr_xy= new TGraphErrors(j,yarr2,xarr2,yarr_err2,xarr_err2);
   TGraphErrors* gr_yz= new TGraphErrors(j,yarr2,zarr2,yarr_err2,zarr_err2);
   gr_xy->Draw("same");
   gr_xy->Write("new");
   gr_xy->GetXaxis()->SetTitle("Time");
   gr_xy->GetYaxis()->SetTitle("Radius");
//difference
int maxCount = 105;
double diff_xarr3[105]= {0};
double diff_yarr3[105]= {0};
   for(int i=0 ; i<maxCount ; i++)
 {
  diff_xarr3[i]=xarr1[i]-yarr1[i];
  diff_yarr3[i]=xarr1[i]-yarr2[i];
 }
TGraphErrors* diff_arr3= new TGraphErrors(i,diff_xarr3,diff_yarr3);
diff_arr3->Draw("same");
diff_arr3->Write("difference");
}

Hi,

where did you open a TFile? You need to open one before writing your graphs.

Cheers,
D

Hi,
could you please specify me the line where to write TFile?
Cheers,

TFile outFile("output.root", "RECREATE");
gr_xy->Write("gr_xy");

Note that new is a bad name for an object; you cannot do new->Draw() when opening the file for reading later because new has a different meaning in C++.

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