Reproduce a histo from one column txt file

Hi ROOTers,

I’m trying to plot a histogram from ADC 13 bit (couts vs ADC) from a one column txt file Run1_PHA_LG_0_44.txt (27,5 KB).

Explanation of file: The histograms (saved as a single column
text file) can be that from the HG or that from the LG amplification chain depending on the selected value
of the Gain Selection parameter. One file per channel and for each amplification chain is saved.

I wrote this code:

{
	TCanvas c("c", "Histo", 700, 500);

	TFile f("h1_input.root","RECREATE");

	TTree t("t","a simple tree");

	TH1F h1("h1", "Histo", 8192,0,8192);

		 t.ReadFile("Run1_PHA_LG_0_44.txt","Entries");
		 t.Project("h1", "Entries");

		 h1.GetXaxis()->SetTitle("ADC [ch]");
		 h1.GetYaxis()->SetTitle("Counts");
		 h1.Draw();

		 t.Write();

	     f.Close();
}

But i don’t see the gaussian distribution that i expected. Why happen this?

t.Project("h1", "Entry$", "Entries"); h1.Sumw2(kFALSE); h1.ResetStats();

Works, thanks.

Why i need the line “h1.Sumw2(kFALSE); h1.ResetStats();” ?

TH1

1 Like

You do not need a tree for a such simple task. The following macro does the job:

void Run1_PHA_LG_0_44()
{
   int    i=0;
   float  x;
   FILE   *fp;
   fp = fopen("Run1_PHA_LG_0_44.txt","r");

   auto h1 = new TH1F("h1", "Histo", 8192,0,8192);

   while (!feof(fp)) {
      fscanf(fp,"%g\n",&x);
      i++;
      h1->SetBinContent(i,x);
   }
   h1->Draw();
}

1 Like

This code is more flexible and simple. I mean, i can use this also for file acquired with 12 Bit ADC. Thanks so much @couet.

1 Like

Of course this little script can be improved to compute automatically the number of bins.

1 Like

For instance:

void ascii2hist(const char *filename)
{
   int    i=0;
   FILE   *fp;
   double x;

   int n =  0;
   fp = fopen(filename,"r");
   while (!feof(fp)) {fscanf(fp,"%lg\n",&x); n++;}
   fclose(fp);

   auto h = new TH1D("h","h",n,0.,(double)n);

   fp = fopen(filename,"r");
   while (!feof(fp)) {
      fscanf(fp,"%lg\n",&x);
      i++;
      h->SetBinContent(i,x);
   }
   fclose(fp);
   h->Draw();
}

then:

root [0] .x ascii2hist.C("Run1_PHA_LG_0_44.txt");
1 Like

It’s easier to use “a tree for a such simple task”:

TH1F *ascii2hist(const char *filename = "Run1_PHA_LG_0_44.txt") {
  TTree *t = new TTree("t", "temporary tree");
  t->ReadFile(filename, "v");
  TH1F *h = new TH1F("h", "Histo;ADC [ch];Counts",
                     t->GetEntries(), 0., (Double_t)t->GetEntries());
  t->Project("h", "Entry$", "v");
  delete t; // no longer needed
  h->Sumw2(kFALSE); h->ResetStats();
  h->Draw();
  return h;
}
1 Like

Thanks for this other point of view @Wile_E_Coyote.

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