Logo ROOT   6.10/09
Reference Guide
twoscales.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 /// \author Rene Brun
11 
12 #include "TCanvas.h"
13 #include "TStyle.h"
14 #include "TH1.h"
15 #include "TGaxis.h"
16 #include "TRandom.h"
17 
18 void twoscales()
19 {
20  TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
21 
22  //create/fill draw h1
24  TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
25  Int_t i;
26  for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
27  h1->Draw();
28  c1->Update();
29 
30  //create hint1 filled with the bins integral of h1
31  TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
32  Float_t sum = 0;
33  for (i=1;i<=100;i++) {
34  sum += h1->GetBinContent(i);
35  hint1->SetBinContent(i,sum);
36  }
37 
38  //scale hint1 to the pad coordinates
39  Float_t rightmax = 1.1*hint1->GetMaximum();
40  Float_t scale = gPad->GetUymax()/rightmax;
41  hint1->SetLineColor(kRed);
42  hint1->Scale(scale);
43  hint1->Draw("same");
44 
45  //draw an axis on the right side
46  TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
47  gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");
48  axis->SetLineColor(kRed);
49  axis->SetLabelColor(kRed);
50  axis->Draw();
51 }
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5937
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
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:7666
static long int sum(long int i)
Definition: Factory.cxx:2162
float Float_t
Definition: RtypesCore.h:53
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:235
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4639
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:202
TH1F * h1
Definition: legend1.C:5
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
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:8325
The axis painter class.
Definition: TGaxis.h:24
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
const Bool_t kFALSE
Definition: RtypesCore.h:92
The Canvas class.
Definition: TCanvas.h:31
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:1267
void SetLabelColor(Int_t labelcolor)
Definition: TGaxis.h:105
#define gPad
Definition: TVirtualPad.h:284
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208