Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
NormalizeHistogram.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// Normalizing a Histogram
5///
6/// Image produced by `.x NormalizeHistogram.C`
7/// Two different methods of normalizing histograms
8/// are shown, each with the original histogram.
9/// next to the normalized one.
10/// \macro_image
11/// \macro_code
12///
13/// \author Advait Dhingra
14
15#include "TH2F.h"
16#include "TRandom.h"
17#include "TCanvas.h"
18
19void NormalizeHistogram()
20{
21
22 std::array<double, 6> binsx{0, 5, 10, 20, 50, 100};
23 TH1F *orig = new TH1F("orig", "Original histogram before normalization", binsx.size() - 1, &binsx[0]);
24
26
27 TRandom2 rand;
28
29 // Filling histogram with random entries
30 for (int i = 0; i < 100'000; ++i) {
31 double r = rand.Rndm() * 100;
32 orig->Fill(r);
33 }
34
35 TH1F *norm = (TH1F *)orig->Clone("norm");
36 norm->SetTitle("Normalized Histogram");
37
38 // Normalizing the Histogram by scaling by 1 / the integral and taking width into account
39 norm->Scale(1. / norm->Integral(), "width");
40
41 // Drawing everything
42 TCanvas *c1 = new TCanvas("c1", "Histogram Normalization", 700, 900);
43 c1->Divide(1, 2);
44
45 c1->cd(1);
46 orig->Draw();
47 c1->cd(2);
48 norm->Draw();
49}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6686
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3344
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition TH1.cxx:7909
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6572
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2752
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition TRandom2.h:27
Double_t Rndm() override
TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers Use a mask of 0xffffffffUL to make ...
Definition TRandom2.cxx:55
void SetTitleFontSize(Float_t size=0)
Definition TStyle.h:404
return c1
Definition legend1.C:41