Writing text file

I have the root file,now i need to generate the text file from it i.e. the graph contents which is the x and the y values
which would be seen as two columns in that text file like saying getting back the ASCII values of the data in the .txt format from the .root file.
I am doing the following :-

void use()
TFile *f=new TFile("10th nov 04.root"); // opens the root file
TH1F * tr=(TH1F *)f->Get("htemp");

float a,b;

ofstream myfile;
myfile.open ("new example.txt");
myfile << "a  b\n";

for (int idx=0; idx <tr->GetNbinsX();idx++) {
   a = tr->GetBinCenter(idx);
   b = tr->GetBinContent(idx);
   cout << a << "  " << b << endl; //print to the screen
   myfile << a << "  " << b << "\n"; //write to file



the .txt file is getting generated but its blank,it doesn’t have any data in it from the graph.
Now what’s the error in this ? yours help is welcome !
I am attaching the .root fie which you can see. 10th nov 04 N.root (19.9 KB)

sorry for the misprint there,its actually 10th nov 04 N.root in the above program,so make the changes accordingly
although i did it right while performing the above.

now i am running it again,i am getting the following the error :-

Error: illegal pointer to class object tr 0x0 1436 F:\root\2017project\use.c(31)
Error: Binary operator oprand missing F:\root\2017project\use.c(31)


I only see a TCanvas in that file:

$> root-ls -t ./nice-26068.root 
=== [./nice-26068.root] ===
version: 53436
2017/08/31 08:53:51 rootio: adding dummy factory for "TCanvas"
TCanvas c1      canvas  (cycle=1)
  TFile *f = TFile::Open("10th nov 04 N.root");
  TCanvas *c; f->GetObject("c1", c);
  TGraph *g = ((TGraph *)(c->FindObject("Graph")));
  // g->Print();
  for (int i = 0; i < g->GetN(); i++)
    std::cout << i << " " << g->GetX()[i] << " " << g->GetY()[i] << std::endl;

Thanks dear,now its working.

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