Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
twoscales.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webcanv
3/// \notebook -js
4/// Two fully interactive scales in web canvas.
5///
6/// Shows two scales drawing for X or Y axis
7/// Several objects can be drawn on the frame and one can select which axis is used for drawing
8/// Y+ means that Y drawn on right frame side, X+ - X will be drawn on top frame side
9/// Several objects can be add to the frame and associated with normal or opposite axis drawing -
10/// like using "same,Y+" option
11///
12/// Functionality available only in web-based graphics
13///
14/// \macro_image (tcanvas_js)
15/// \macro_code
16///
17/// \author Sergey Linev
18
19void twoscales()
20{
21 auto hpe = new TH1F("hpe", "Use left and right side for Y scale drawing", 100, 0, 10);
22 hpe->GetYaxis()->SetTitle("Expo");
23
24 auto hpg = new TH1F("hpg","Gaus distribution",100,0,10);
25 hpg->GetYaxis()->SetTitle("#color[2]{Gaus1} / #color[3]{Gaus2}");
26 hpg->GetYaxis()->SetAxisColor(kRed);
27 hpg->SetLineColor(kRed);
28
29 auto hpg2 = new TH1F("hpg2", "Narrow gaus distribution",100,0,10);
30 hpg2->SetLineColor(kGreen);
31
32 for (int i = 0; i < 25000; i++) {
33 hpe->Fill(gRandom->Exp(1.));
34 hpg->Fill(gRandom->Gaus(4, 1.));
35 if (i % 10 == 0)
36 hpg2->Fill(gRandom->Gaus(8, 0.25));
37 }
38
39 auto gr1 = new TGraph(1000);
40 auto gr2 = new TGraph(10000);
41 for (int i = 0; i < 10000; i++) {
42 auto x = 20. + i/100.;
43 if ((i >= 2000) && (i < 3000))
44 gr1->SetPoint(i - 2000, x, 1.5 + TMath::Sin(x));
45 gr2->SetPoint(i, x, 3.5 + TMath::Sin(x));
46 }
47
48 gr1->SetMinimum(0);
49 gr1->SetMaximum(5);
50 gr1->SetTitle("Tow graphs sharing same Y scale, but using different X scales");
51 gr1->GetXaxis()->SetTitle("Graph1");
52
53 gr2->SetLineColor(kBlue);
54 gr2->GetXaxis()->SetAxisColor(kBlue);
55 gr2->GetXaxis()->SetTitle("Graph2");
56 gr2->GetXaxis()->SetTitleColor(kBlue);
57
58 gStyle->SetStatX(0.88);
59
60 auto c1 = new TCanvas("c1", "Twoscales example", 1200, 800);
61
62 if (!gROOT->IsBatch() && !c1->IsWeb())
63 ::Warning("twoscales.cxx", "macro may not work without enabling web-based canvas");
64
65 c1->Divide(1,2);
66
67 c1->GetPad(1)->Add(hpe); // normal drawing
68 c1->GetPad(1)->Add(hpg, "Y+"); // draw independent Y axis on right side
69 c1->GetPad(1)->Add(hpg2, "same,Y+"); // use Y drawn on right side
70
71 c1->GetPad(2)->SetTopMargin(0.2); // need more space on top
72 c1->GetPad(2)->Add(gr1,"AL"); // draw as line
73 c1->GetPad(2)->Add(gr2,"AL,X+"); // draw as line with independent X axis on top of the frame
74}
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
#define gROOT
Definition TROOT.h:406
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
The Canvas class.
Definition TCanvas.h:23
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
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
virtual Double_t Exp(Double_t tau)
Returns an exponential deviate.
Definition TRandom.cxx:252
void SetStatX(Float_t x=0)
Definition TStyle.h:397
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:592