Reading data file to fill an histogram ignoring some lines

Hello, I should fill an histogram from data file.
The file has some text files before the data, then I get an empty plot.
Is there a way to automatically ignore the first lines without manually deleting them (i.e.

  1D histogram 0: Edep in crystal
 	     X 		     Y

then just using the data culomns?
Thank you

depenetxt.cpp (2.2 KB)
data.txt (82.8 KB)

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

a possible way:

void depenetxt()
   double DepEne; //Released energy
   int entry=1;
   gSystem->mkdir(TString::Format("simulazione" ), kTRUE);
   FILE * infile=fopen("data.txt", "r");

      printf("Error opening file");

   TCanvas *c0 = new TCanvas("c0","c0",1280,1024);

   int num=57;
   int bin;

   TH1F *histo = new TH1F("histo","DepositedEnergy",2500,0.,2500); //

   char line[40];
   Int_t i = -4; // skip 4 lines

   while (fgets(line,40,infile)) {
      if (i>=0) {
         sscanf(&line[0],"%d %lf",&bin,&DepEne);

   histo->GetXaxis()->SetTitle("Energy (keV)");


Note that your file is not very informative, the first column is an index n growing by step of 1 from 0 to 2500 and the 2nd one (the one you put in the histogram) is n+0.5.

Thank you it woks!

The first culomn is the bin number of histogram, the second one the mean value of the beam got by GEANT.
Unfortunately, my boss can’t use ROOT, then I’m going crazy using strange text files that he needs to use them with some scripts that it has (I don’t know for wich toolkit)

May be check because it seems the 2nd column is just the_first_column + 0.5

ROOT is working on many different architectures. I am sure a ROOT version is working on his PC.

because it is the middle of the bin

He has Mac…obviously ROOT works on his PC…the problem is that he doen’t know how to use it (I mean, he doesn’t know how to writes macros etc.)

Yes that’s what I mean, the first column is an index and the 2nd just the first column +0.5 (yes middle of the bin) . If you do an histogram with that yo will get a flat line … That’s why I said it is not very interesting.

Yes ROOT works well on Mac.

You can just show him… the very basic is simple:

$ root file.root
root[0] hist->Draw()

There is no need to write a macro for that.

No…I don’t get a flat graph, because the histogram should be done using first and third or second and third culomn! Not using first and second!

Truly, in the past I just saved GEANT4 outputs in a ROOT file and I wrote the macros for him, so that he just should run the command

.x macroname.cpp

but he said me that he need the text file (in the way that he wants, i.e. binned etc) because he can’t use ROOT…

Ok … :face_with_raised_eyebrow:

We might be able to help him :slight_smile:

Surely, but for the moment he wasn’t available to learn it

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