ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
twoscales.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Example of macro illustrating how to superimpose two histograms
4 /// with different scales in the "same" pad.
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 #include "TCanvas.h"
12 #include "TStyle.h"
13 #include "TH1.h"
14 #include "TGaxis.h"
15 #include "TRandom.h"
16 
17 void twoscales()
18 {
19  TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
20 
21  //create/fill draw h1
23  TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
24  Int_t i;
25  for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
26  h1->Draw();
27  c1->Update();
28 
29  //create hint1 filled with the bins integral of h1
30  TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
31  Float_t sum = 0;
32  for (i=1;i<=100;i++) {
33  sum += h1->GetBinContent(i);
34  hint1->SetBinContent(i,sum);
35  }
36 
37  //scale hint1 to the pad coordinates
38  Float_t rightmax = 1.1*hint1->GetMaximum();
39  Float_t scale = gPad->GetUymax()/rightmax;
40  hint1->SetLineColor(kRed);
41  hint1->Scale(scale);
42  hint1->Draw("same");
43 
44  //draw an axis on the right side
45  TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
46  gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");
47  axis->SetLineColor(kRed);
48  axis->SetLabelColor(kRed);
49  axis->Draw();
50 }
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6174
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
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
Definition: Rtypes.h:61
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
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:8543
The axis painter class.
Definition: TGaxis.h:39
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:48
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:1252
void SetLabelColor(Int_t labelcolor)
Definition: TGaxis.h:115
#define gPad
Definition: TVirtualPad.h:288
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
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:7921