Make Graph from Data File

Hi I read a previous thread on here on a similar subject and used it for much help. But I now have a question of my own if you could help with. So I have a data file that I want to make into a Graph, the thing is that I would like to use this code over again for different data files that will each have different number of data. So first, I created a program that will count the number of data in the file:

void Ascii_count() {

   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("Ascii_count.C","");
   dir.ReplaceAll("/./","/");
   ifstream in;
   in.open(Form("%sDataFile1.dat",dir.Data()));

   Float_t x,y;
   Int_t nlines = 0;
   
      while (1) {
       in >> x >> y ;
      if (!in.good()) break;
  //    if (nlines < 5) printf("x=%8f",x);
      nlines++;
   }
   printf(" found %d points\n",nlines);


   in.close();

}

Then, AFTER it tells me the number (for example it would say “found 4710 points”) I use the number from above into the following program:

{
   TFile *h = new TFile("File.root","RECREATE");
   FILE *f = fopen("DataFile1.dat","r");
   Int_t i=0;
   Double_t x[4710],y[4710];
   while (!feof(f)){ 
      fscanf(f,"%lf %lf\n",&x[i],&y[i]);
      i++;
   }
   
     TGraph g1(4710,x,y); g1->SetLineColor(2);
        g1.Draw("AL*");
   h->Write();     
}

Which then creates a graph into root. So the two individual programs work, but how could I combine it into 1 program so I that the only line I would have to change is the input name of the data file (i.e. DataFile1.dat)

And also, I would like to save the graph into a root file. I thought I set up the correct parameters in the second program to write the file into a *.root file, but when I open the root file, it is empty. What am I doing wrong? Thank you for your help.

Cheers,
Edwin

Easy. Just use C++ standard library - std::vector.
So, in you first program (which now will be the only program)

......
std::vector<double> xs;
//if you have any guess about data size, you can do
xs.reserve(some_initial_guess);
std::vector<double> ys;
ys.reserve(some_initial_guess);
....
//you loop
double x, y;

while(1)
{
 ...... here you read into x and y and check the stream state
   xs.push_back(x);
   ys.push_back(y);
}
.....

//now, you create TGraph
TGraph * grp = new TGraph(xs.size(), &xs[0], &ys[0]);
....

vector::push_back probably has to reallocate, but not on every iteration and this is not terribly expensive, so, you do not need two “passes” on your data now. And you have only one program.

P.S. if I remember correctly, Rene implemented the ctor, which accepts file name with pairs (x,y) and does all work for you, so, this is obviously the best choise:

TGraph * grp = new TGraph(“myfile.txt”);

Thank you,

That worked perfectly. Now how would I correctly save this graph into a *.root file? Thank you again.

Cheers,
Edwin

[quote=“ebaldelo”]Thank you,

That worked perfectly. Now how would I correctly save this graph into a *.root file? Thank you again.

Cheers,
Edwin[/quote]

something like this?

void foo_bar()
{
....
TFile out("graph.root", "recreate");
....
TGraph g("asci.dat");
g.SetName("some_name");
g.SetTitle("some_title");
g.Write();
...
}

Thank you for your help, but I can’t seem to get it to work. It just comes out as a blank canvas when I try to implement your last code. Any Ideas? Thanks.

Cheers,
Edwin

[quote=“ebaldelo”]Thank you for your help, but I can’t seem to get it to work. It just comes out as a blank canvas when I try to implement your last code. Any Ideas? Thanks.

Cheers,
Edwin[/quote]

Hm, in my code I do not see any canvas at all, it just shows how to create TGraph from ascii data and save it into ROOT file.
TGraph’s object in my example has automatic storage duration, so it exists only during function execution. If you also want to draw the graph, you have to create it in dynamic memory using ‘new’ expression, so, it’ll be alive after macro execution is finished .

I have attached the sample macro and ascii file.
grp.C (200 Bytes)
ascii.txt (1.62 KB)

Dear, I am trying this code for text file. but shows “line found 0”. Is this code applicable for text file? If not applicable then how can I`ll do for the text file.
Thanks

Attach your text file here.

http://www.filedropper.com/image_22

Thank you dear.

With this data file you need to follow Drawing graph from a text file by skipping 1st 47 rows