Home | News | Documentation | Download

Normalize a histogram

I am trying to normalize this histogram. I’ve seen some examples but none of them seems to work.

newcGs.C (15.4 KB)

For normalising an histogram you have two option:

  1. Normalize like histogram describe a probability density function :
histo->Scale( 1./histo->Integral(),"WIDTH");
  1. Normalize to a probability distribution. Sum of histogram content is equal to 1:
histo->Scale( 1./histo->Integral());

See also https://root.cern.ch/doc/master/classTH1.html#add929909dcb3745f6a52e9ae0860bfbd

Note that when scaling an histogram , the histogram has error different than sqrt(N) and by default it is plotted with the errors. If you don’t want to plot with errors, you need to add the “HIST” option to
TH1::Draw

Lorenzo

1 Like

Thank you so much @moneta. One more question, how can I plot a histogram from a txt file? I have tried this

void capgam_z025Mn()
{
  TH1F *hist = new TH1F("hist","histogram", 312,0,7.4);

  fstream file;
  file.open("capgam_z025_55Mn.txt", ios::in);

  double a,b;
 
  while(1){
    file >> a >> b;
    hist->Fill(a,b);
    if(file.eof()) break;
  }

  file.close();
  hist->GetXaxis()->SetTitle("Energy(MeV)");
  hist->GetYaxis()->SetTitle("Intensity");
  hist->SetLineColor(kRed);
  hist->Draw("HIST");
}

but the histogram has some differences from the original one. My txt file is this onecapgam_z025_55Mn.txt (4.6 KB)
Thanks in advance!

The code looks ok; I would change (but it’s not related to the issue) the loop a bit:

  while(file >> a >> b) {
    hist->Fill(a,b);
  }

The problem is that the datasets seem to be different. You need to check all values and how the first histogram was created, but for example, in the newcGs.C file the histo has 500 bins, from 0 to 7.8174, and there is data in bin 500 (i.e. above 7.8), while the data in your txt file only goes up to 7.27. In addition, the bin widths are not the same (7.8174/500 != 7.4/312).

Thank for your answer. Is there any way of average the values of the 500 bins? I do not know if my logic is right. Or what can I do so I can compare both histogram and plot them together?