Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist010_TH1_two_scales.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Example of macro illustrating how to superimpose two histograms
5/// with different scales in the "same" pad.
6///
7/// \macro_image
8/// \macro_code
9///
10/// \date November 2024
11/// \author Rene Brun
12
13void hist010_TH1_two_scales()
14{
15 TCanvas *c1 = new TCanvas("c1", "hists with different scales", 600, 400);
16
17 // create/fill draw h1
19 TH1D *h1 = new TH1D("h1", "my histogram", 100, -3, 3);
20 TRandom3 rng;
21 for (int i = 0; i < 10000; i++)
22 h1->Fill(rng.Gaus(0, 1));
23 h1->Draw();
24 c1->Update();
25
26 // create hint1 filled with the bins integral of h1
27 TH1D *hint1 = new TH1D("hint1", "h1 bins integral", 100, -3, 3);
28 double sum = 0;
29 for (int i = 1; i <= 100; i++) {
30 sum += h1->GetBinContent(i);
31 hint1->SetBinContent(i, sum);
32 }
33
34 // scale hint1 to the pad coordinates
35 double rightmax = 1.1 * hint1->GetMaximum();
36 double scale = gPad->GetUymax() / rightmax;
37 hint1->SetLineColor(kRed);
38 hint1->Scale(scale);
39 hint1->Draw("same");
40
41 // draw an axis on the right side
42 TGaxis *axis =
43 new TGaxis(gPad->GetUxmax(), gPad->GetUymin(), gPad->GetUxmax(), gPad->GetUymax(), 0, rightmax, 510, "+L");
44 axis->SetLineColor(kRed);
45 axis->SetLabelColor(kRed);
46 axis->Draw();
47}
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
@ kRed
Definition Rtypes.h:66
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
The Canvas class.
Definition TCanvas.h:23
The axis painter class.
Definition TGaxis.h:26
void SetLabelColor(Int_t labelcolor)
Definition TGaxis.h:106
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:682
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8565
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3333
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3055
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9242
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5077
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6620
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:292
Random number generator class based on M.
Definition TRandom3.h:27
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1640
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345