Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
approx.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook -js
4/// Macro to test interpolation function Approx
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author Christian Stratowa, Vienna, Austria.
10
11TCanvas *vC1;
12TGraph *grxy, *grin, *grout;
13
14void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)
15{
16 vC1->cd(pad);
17 TH1F *vFrame = gPad->DrawFrame(0, 0, 15, 150);
18 vFrame->SetTitle(title);
19 vFrame->SetTitleSize(0.2);
20 vFrame->SetXTitle(xt);
21 vFrame->SetYTitle(yt);
22 grxy->SetMarkerColor(kBlue);
23 grxy->SetMarkerStyle(21);
24 grxy->SetMarkerSize(0.5);
25 grxy->Draw("P");
26 grin->SetMarkerColor(kRed);
27 grin->SetMarkerStyle(5);
28 grin->SetMarkerSize(0.7);
29 grin->Draw("P");
30 grout->DrawClone("LP");
31}
32
33void approx()
34{
35 // Test data (square)
36 Int_t n = 11;
37 Double_t x[] = {1, 2, 3, 4, 5, 6, 6, 6, 8, 9, 10};
38 Double_t y[] = {1, 4, 9, 16, 25, 25, 36, 49, 64, 81, 100};
39 grxy = new TGraph(n, x, y);
40
41 // X values, for which y values should be interpolated
42 Int_t nout = 14;
43 Double_t xout[] = {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};
44
45 // Create Canvas
46 vC1 = new TCanvas("vC1", "square", 200, 10, 700, 700);
47 vC1->Divide(2, 2);
48
49 // Initialize graph with data
50 grin = new TGraph(n, x, y);
51 // Interpolate at equidistant points (use mean for tied x-values)
52 TGraphSmooth *gs = new TGraphSmooth("normal");
53 grout = gs->Approx(grin, "linear");
54 DrawSmooth(1, "Approx: ties = mean", "X-axis", "Y-axis");
55
56 // Re-initialize graph with data
57 // (since graph points were set to unique vales)
58 grin = new TGraph(n, x, y);
59 // Interpolate at given points xout
60 grout = gs->Approx(grin, "linear", 14, xout, 0, 130);
61 DrawSmooth(2, "Approx: ties = mean", "", "");
62
63 // Print output variables for given values xout
64 Int_t vNout = grout->GetN();
65 Double_t vXout, vYout;
66 for (Int_t k = 0; k < vNout; k++) {
67 grout->GetPoint(k, vXout, vYout);
68 cout << "k= " << k << " vXout[k]= " << vXout << " vYout[k]= " << vYout << endl;
69 }
70
71 // Re-initialize graph with data
72 grin = new TGraph(n, x, y);
73 // Interpolate at equidistant points (use min for tied x-values)
74 // _grout = gs->Approx(grin,"linear", 50, 0, 0, 0, 1, 0, "min");_
75 grout = gs->Approx(grin, "constant", 50, 0, 0, 0, 1, 0.5, "min");
76 DrawSmooth(3, "Approx: ties = min", "", "");
77
78 // Re-initialize graph with data
79 grin = new TGraph(n, x, y);
80 // Interpolate at equidistant points (use max for tied x-values)
81 grout = gs->Approx(grin, "linear", 14, xout, 0, 0, 2, 0, "max");
82 DrawSmooth(4, "Approx: ties = max", "", "");
83
84 // Cleanup
85 delete gs;
86}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
#define gPad
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
A helper class to smooth TGraph.
TGraph * Approx(TGraph *grin, Option_t *option="linear", Int_t nout=50, Double_t *xout=nullptr, Double_t yleft=0, Double_t yright=0, Int_t rule=0, Double_t f=0, Option_t *ties="mean")
Approximate data points.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
Int_t GetN() const
Definition TGraph.h:132
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:833
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
Get x and y values for point number i.
Definition TGraph.cxx:1535
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
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:6747
virtual void SetXTitle(const char *title)
Definition TH1.h:420
virtual void SetYTitle(const char *title)
Definition TH1.h:421
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:317
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:1249
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16