Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr010_approx_smooth.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook -js
4/// \preview Create a TGraphSmooth and show the usage of the interpolation function Approx.
5///
6/// See the [TGraphSmooth documentation](https://root.cern/doc/master/classTGraphSmooth.html)
7///
8/// \macro_image
9/// \macro_code
10/// \author Christian Stratowa, Vienna, Austria.
11
12TCanvas *vC1;
13TGraph *grxy, *grin, *grout;
14
15void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
16{
17 vC1->cd(pad);
18 TH1F *vFrame = gPad->DrawFrame(0,0,15,150);
19 vFrame->SetTitle(title);
20 vFrame->SetTitleSize(0.2);
21 vFrame->SetXTitle(xt);
22 vFrame->SetYTitle(yt);
23 grxy->SetMarkerColor(kBlue);
24 grxy->SetMarkerStyle(21);
25 grxy->SetMarkerSize(0.5);
26 grxy->Draw("P");
27 grin->SetMarkerColor(kRed);
28 grin->SetMarkerStyle(5);
29 grin->SetMarkerSize(0.7);
30 grin->Draw("P");
31 grout->DrawClone("LP");
32}
33
34void gr010_approx_smooth()
35{
36// Test data (square)
37 Int_t n = 11;
38 Double_t x[] = {1,2,3,4,5,6,6,6,8,9,10};
39 Double_t y[] = {1,4,9,16,25,25,36,49,64,81,100};
40 grxy = new TGraph(n,x,y);
41
42// X values, for which y values should be interpolated
43 Int_t nout = 14;
44 Double_t xout[] =
45 {1.2,1.7,2.5,3.2,4.4,5.2,5.7,6.5,7.6,8.3,9.7,10.4,11.3,13};
46
47// Create Canvas
48 vC1 = new TCanvas("vC1","square",200,10,700,700);
49 vC1->Divide(2,2);
50
51// Initialize graph with data
52 grin = new TGraph(n,x,y);
53// Interpolate at equidistant points (use mean for tied x-values)
55 grout = gs->Approx(grin,"linear");
56 DrawSmooth(1,"Approx: ties = mean","X-axis","Y-axis");
57
58// Re-initialize graph with data
59// (since graph points were set to unique vales)
60 grin = new TGraph(n,x,y);
61// Interpolate at given points xout
62 grout = gs->Approx(grin,"linear", 14, xout, 0, 130);
63 DrawSmooth(2,"Approx: ties = mean","","");
64
65// Print output variables for given values xout
66 Int_t vNout = grout->GetN();
67 Double_t vXout, vYout;
68 for (Int_t k=0;k<vNout;k++) {
69 grout->GetPoint(k, vXout, vYout);
70 cout << "k= " << k << " vXout[k]= " << vXout
71 << " vYout[k]= " << vYout << endl;
72 }
73
74// Re-initialize graph with data
75 grin = new TGraph(n,x,y);
76// Interpolate at equidistant points (use min for tied x-values)
77// _grout = gs->Approx(grin,"linear", 50, 0, 0, 0, 1, 0, "min");_
78 grout = gs->Approx(grin,"constant", 50, 0, 0, 0, 1, 0.5, "min");
79 DrawSmooth(3,"Approx: ties = min","","");
80
81// Re-initialize graph with data
82 grin = new TGraph(n,x,y);
83// Interpolate at equidistant points (use max for tied x-values)
84 grout = gs->Approx(grin,"linear", 14, xout, 0, 0, 2, 0, "max");
85 DrawSmooth(4,"Approx: ties = max","","");
86
87// Cleanup
88 delete gs;
89}
90
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
@ kBlue
Definition Rtypes.h:67
#define gPad
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
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
A helper class to smooth TGraph.
Int_t GetN() const
Definition TGraph.h:131
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis' title size.
Definition Haxis.cxx:365
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6836
virtual void SetXTitle(const char *title)
Definition TH1.h:667
virtual void SetYTitle(const char *title)
Definition TH1.h:668
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
Definition TObject.cxx:319
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
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16