Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist016_TH1_different_scales_canvas.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// \preview Example of a canvas showing two histograms with different scales.
5/// The second histogram is drawn in a transparent pad
6///
7/// \macro_image
8/// \macro_code
9///
10/// \date November 2022
11/// \author Rene Brun
12
13void hist016_TH1_different_scales_canvas()
14{
15 TCanvas *c1 = new TCanvas("c1", "transparent pad", 200, 10, 700, 500);
16 TPad *pad1 = new TPad("pad1", "", 0, 0, 1, 1);
17 TPad *pad2 = new TPad("pad2", "", 0, 0, 1, 1);
18 pad2->SetFillStyle(4000); // will be transparent
19 pad1->Draw();
20 pad1->cd();
21
22 TH1F *h1 = new TH1F("h1", "h1", 100, -3, 3);
23 TH1F *h2 = new TH1F("h2", "h2", 100, -3, 3);
24 TRandom r;
25 for (Int_t i = 0; i < 100000; i++) {
26 Double_t x1 = r.Gaus(-1, 0.5);
27 Double_t x2 = r.Gaus(1, 1.5);
28 if (i < 1000)
29 h1->Fill(x1);
30 h2->Fill(x2);
31 }
32 h1->Draw();
33 pad1->Update(); // this will force the generation of the "stats" box
34 TPaveStats *ps1 = (TPaveStats *)h1->GetListOfFunctions()->FindObject("stats");
35 ps1->SetX1NDC(0.4);
36 ps1->SetX2NDC(0.6);
37 pad1->Modified();
38 c1->cd();
39
40 // compute the pad range with suitable margins
41 Double_t ymin = 0;
42 Double_t ymax = 2000;
44 Double_t xmin = -3;
45 Double_t xmax = 3;
47 pad2->Range(xmin - 0.1 * dx, ymin - 0.1 * dy, xmax + 0.1 * dx, ymax + 0.1 * dy);
48 pad2->Draw();
49 pad2->cd();
50 h2->SetLineColor(kRed);
51 h2->Draw("][sames");
52 pad2->Update();
53 TPaveStats *ps2 = (TPaveStats *)h2->GetListOfFunctions()->FindObject("stats");
54 ps2->SetX1NDC(0.65);
55 ps2->SetX2NDC(0.85);
56 ps2->SetTextColor(kRed);
57
58 // draw axis on the right side of the pad
59 TGaxis *axis = new TGaxis(xmax, ymin, xmax, ymax, ymin, ymax, 50510, "+L");
60 axis->SetLabelColor(kRed);
61 axis->Draw();
62}
ROOT::R::TRInterface & r
Definition Object.C:4
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
@ kRed
Definition Rtypes.h:67
float xmin
float ymin
float xmax
float ymax
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:50
The Canvas class.
Definition TCanvas.h:23
void SetLabelColor(Int_t labelcolor)
Definition TGaxis.h:106
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3393
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
TList * GetListOfFunctions() const
Definition TH1.h:488
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:708
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
The most important graphics class in the ROOT system.
Definition TPad.h:28
void SetFillStyle(Style_t fstyle) override
Override TAttFill::FillStyle for TPad because we want to handle style=0 as style 4000.
Definition TPad.cxx:6036
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5344
void Update() override
Update pad.
Definition TPad.cxx:2962
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7351
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:694
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1512
virtual void SetX1NDC(Double_t x1)
Definition TPave.h:84
virtual void SetX2NDC(Double_t x2)
Definition TPave.h:85
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5