Read Data from file

Dear All,
1- How to create TH1D or TH1F for that data and read the files it one by one.

{
  ifstream inp; double x,x1,x2,y;
  inp.open("File1.txt");
  int i=0;
  while (inp >> x >> y) {
    if (i==0) x1 = x;
    i++;
  }
  x2 = x;
  printf("%g %g %d\n",x1,x2,i);
  inp.close();

  double bw = 1.;
  int    nb = i+1;
  auto h = new TH1D("h","AIST HPGe Detector Spectrum",nb,x1-bw/2.,x2+bw/2.);
  h->GetXaxis()->SetTitle("Energy (keV)");
  h->GetYaxis()->SetTitle("Count");
  inp.open("File1.txt");
  i = 0;
  while (inp >> x >> y) {
    i++;
    h->SetBinContent(i,y);
  }
  inp.close();
  h->Draw();
}

Dear Dr. Olivier,
I appreciate your effort, thank you very much. It is perfect. :grinning::grinning:

1- if I have 100 files.txt from file-000.txt to file-099.txt, can I call them one by one without modifying the code every time?

Best Regards

Yes you can make a function with the file name as parameter and call it in sequence.

File1.C (673 Bytes)

–> Error
If my files.txt in the path /home/Documents/file(1:100).txt,
what is the required command line i have to write in the root to do it ?
root [0] !!!

In the macro I posted simply give the full path name for each file.

it doses not work ???

Can you post what you are doing exactly ? and also the errors you get…

The macro File1 does not have any arguments and you call it with a parameter … “1” ? … what does that mean ?
That’s exactly what the error message says by the way.

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