Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr012_polar.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview Create and draw a polar graph. See the [TGraphPolar documentation](https://root.cern/doc/master/classTGraphPolar.html)
5///
6/// Since TGraphPolar is a TGraphErrors, it is painted with
7/// [TGraphPainter](https://root.cern/doc/master/classTGraphPainter.html) options.
8///
9/// With GetPolargram we retrieve the polar axis to format it; see the
10/// [TGraphPolargram documentation](https://root.cern/doc/master/classTGraphPolargram.html)
11///
12/// \macro_image
13/// \macro_code
14/// \author Olivier Couet
15
16void gr012_polar()
17{
18 // Illustrates how to use TGraphPolar
19
20 TCanvas * CPol = new TCanvas("CPol","TGraphPolar Examples",1200,600);
21 CPol->Divide(2,1);
22
23 // Left-side pad. Two graphs without errors
24 CPol->cd(1);
25 Double_t xmin=0;
27
28 Double_t x[1000];
29 Double_t y[1000];
30 Double_t xval1[20];
31 Double_t yval1[20];
32
33 // Graph 1 to be drawn with line and fill
34 TF1 * fplot = new TF1("fplot","cos(2*x)*cos(20*x)",xmin,xmax);
35 for (Int_t ipt = 0; ipt < 1000; ipt++){
36 x[ipt] = ipt*(xmax-xmin)/1000 + xmin;
37 y[ipt] = fplot->Eval(x[ipt]);
38 }
39 TGraphPolar * grP = new TGraphPolar(1000,x,y);
40 grP->SetLineColor(2);
41 grP->SetLineWidth(2);
42 grP->SetFillStyle(3012);
43 grP->SetFillColor(2);
44 grP->Draw("AFL");
45
46 // Graph 2 to be drawn superposed over graph 1, with curve and polymarker
47 for (Int_t ipt = 0; ipt < 20; ipt++){
48 xval1[ipt] = x[1000/20*ipt];
49 yval1[ipt] = y[1000/20*ipt];
50 }
51 TGraphPolar * grP1 = new TGraphPolar(20,xval1,yval1);
52 grP1->SetMarkerStyle(29);
53 grP1->SetMarkerSize(2);
54 grP1->SetMarkerColor(4);
55 grP1->SetLineColor(4);
56 grP1->Draw("CP");
57
58 // To format the polar axis, we retrieve the TGraphPolargram.
59 // First update the canvas, otherwise GetPolargram returns 0
60 CPol->Update();
61 if (grP1->GetPolargram()) {
62 grP1->GetPolargram()->SetTextColor(8);
64 grP1->GetPolargram()->SetNdivPolar(703);
65 grP1->GetPolargram()->SetToRadian(); // tell ROOT that the x and xval1 are in radians
66 }
67
68 // Right-side pad. One graph with errors
69 CPol->cd(2);
70 Double_t x2[30];
71 Double_t y2[30];
72 Double_t ex[30];
73 Double_t ey[30];
74 for (Int_t ipt = 0; ipt < 30; ipt++){
75 x2[ipt] = x[1000/30*ipt];
76 y2[ipt] = 1.2 + 0.4*sin(TMath::Pi()*2*ipt/30);
77 ex[ipt] = 0.2+0.1*cos(2*TMath::Pi()/30*ipt);
78 ey[ipt] = 0.2;
79 }
80
81 // Grah to be drawn with polymarker and errors
82 TGraphPolar * grPE = new TGraphPolar(30,x2,y2,ex,ey);
83 grPE->SetMarkerStyle(22);
84 grPE->SetMarkerSize(1.5);
85 grPE->SetMarkerColor(5);
86 grPE->SetLineColor(6);
87 grPE->SetLineWidth(2);
88 grPE->Draw("EP");
89
90 // To format the polar axis, we retrieve the TGraphPolargram.
91 // First update the canvas, otherwise GetPolargram returns 0
92 CPol->Update();
93 if (grPE->GetPolargram()) {
94 grPE->GetPolargram()->SetTextSize(0.03);
95 grPE->GetPolargram()->SetTwoPi();
96 grPE->GetPolargram()->SetToRadian(); // tell ROOT that the x2 values are in radians
97 }
98}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
float xmin
float xmax
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:41
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:48
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:50
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2486
Definition TF1.h:182
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
TGraphPolargram * GetPolargram()
Definition TGraphPolar.h:39
void Draw(Option_t *options="") override
Default Draw method for all objects.
void SetRangePolar(Double_t tmin, Double_t tmax)
void SetNdivPolar(Int_t Ndiv=508)
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1295
RVec< PromoteType< T > > cos(const RVec< T > &v)
Definition RVec.hxx:1848
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1847
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t ey[n]
Definition legend1.C:17
Double_t ex[n]
Definition legend1.C:17
constexpr Double_t Pi()
Definition TMath.h:40