Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist010_TH1_two_scales.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Example of macro illustrating how to superimpose two histograms with different scales in the "same" pad.

void hist010_TH1_two_scales()
{
TCanvas *c1 = new TCanvas("c1", "hists with different scales", 600, 400);
// create/fill draw h1
gStyle->SetOptStat(kFALSE);
TH1D *h1 = new TH1D("h1", "my histogram", 100, -3, 3);
TRandom3 rng;
for (int i = 0; i < 10000; i++)
h1->Fill(rng.Gaus(0, 1));
h1->Draw();
c1->Update();
// create hint1 filled with the bins integral of h1
TH1D *hint1 = new TH1D("hint1", "h1 bins integral", 100, -3, 3);
double sum = 0;
for (int i = 1; i <= 100; i++) {
sum += h1->GetBinContent(i);
hint1->SetBinContent(i, sum);
}
// scale hint1 to the pad coordinates
double rightmax = 1.1 * hint1->GetMaximum();
double scale = gPad->GetUymax() / rightmax;
hint1->SetLineColor(kRed);
hint1->Scale(scale);
hint1->Draw("same");
// draw an axis on the right side
TGaxis *axis =
new TGaxis(gPad->GetUxmax(), gPad->GetUymin(), gPad->GetUxmax(), gPad->GetUymax(), 0, rightmax, 510, "+L");
axis->Draw();
}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
@ kRed
Definition Rtypes.h:67
externTStyle * gStyle
Definition TStyle.h:442
#define gPad
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
The Canvas class.
Definition TCanvas.h:23
void SetLabelColor(Int_t labelcolor)
Definition TGaxis.h:106
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
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:8682
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
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:9356
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6719
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
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:274
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338
Date
November 2024
Author
Rene Brun

Definition in file hist010_TH1_two_scales.C.