Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
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
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)
54 TGraphSmooth *gs = new TGraphSmooth("normal");
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();
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
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gPad
The Canvas class.
Definition TCanvas.h:23
A helper class to smooth TGraph.
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:645
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16