Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
splines_test.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// Examples of use of the spline classes.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author Federico Carminati
10
11void splines_test(Int_t np = 23, Double_t a = -0.5, Double_t b = 31)
12{
13 // array of points
14 Double_t *xx = 0, *yy = 0;
15 TSpline3 *spline3 = 0;
16 TSpline5 *spline5 = 0;
17 TLine *line5, *line3;
18 TText *text5, *text3, *textn;
19 char text[20];
20 const Double_t power = 0.75;
21
22 // Define the original function
23 TF1 *f = new TF1("f", "sin(x)*sin(x/10)", a - 0.05 * (b - a), b + 0.05 * (b - a));
24 // Draw function
25 f->Draw("lc");
26
27 // Create text and legend
28 Double_t xx1, yy1, xx2, yy2, dx, dy;
29 gPad->Update();
30 gPad->GetRangeAxis(xx1, yy1, xx2, yy2);
31 dx = xx2 - xx1;
32 dy = yy2 - yy1;
33 line5 = new TLine(xx1 + dx * 0.3, yy1 + dy * 1.02, xx1 + dx * 0.38, yy1 + dy * 1.02);
34 line5->SetLineColor(kRed);
35 line5->SetLineWidth(2);
36 text5 = new TText(xx1 + dx * 0.4, yy1 + dy * 1.03, "quintic spline");
37 text5->SetTextAlign(12);
38 text5->SetTextSize(0.04);
39 line3 = new TLine(xx1 + dx * 0.67, yy1 + dy * 1.02, xx1 + dx * 0.75, yy1 + dy * 1.02);
40 line3->SetLineColor(kGreen);
41 line3->SetLineWidth(2);
42 text3 = new TText(xx1 + dx * 0.77, yy1 + dy * 1.03, "third spline");
43 text3->SetTextAlign(12);
44 text3->SetTextSize(0.04);
45 textn = new TText(xx1 + dx * 0.8, yy1 + dy * 0.91, " ");
46 textn->SetTextAlign(12);
47 textn->SetTextSize(0.04);
48 textn->Draw();
49
50 // Draw legenda
51 line5->Draw();
52 text5->Draw();
53 line3->Draw();
54 text3->Draw();
55
56 for (Int_t nnp = 2; nnp <= np; ++nnp) {
57
58 // Calculate the knots
59 if (xx)
60 delete[] xx;
61 xx = new Double_t[nnp];
62 if (yy)
63 delete[] yy;
64 yy = new Double_t[nnp];
65 for (Int_t i = 0; i < nnp; ++i) {
66 xx[i] = a + (b - a) * TMath::Power(i / Double_t(nnp - 1), power);
67 yy[i] = f->Eval(xx[i]);
68 }
69
70 // Evaluate fifth spline coefficients
71 Double_t eps = (b - a) * 1.e-5;
72 if (spline5)
73 delete spline5;
74 spline5 = new TSpline5("Test", xx, f, nnp, "b1e1b2e2", f->Derivative(a), f->Derivative(b),
75 (f->Derivative(a + eps) - f->Derivative(a)) / eps,
76 (f->Derivative(b) - f->Derivative(b - eps)) / eps);
77
78 spline5->SetLineColor(kRed);
79 spline5->SetLineWidth(3);
80
81 // Draw the quintic spline
82 spline5->Draw("lcsame");
83
84 // Evaluate third spline coefficients
85 if (spline3)
86 delete spline3;
87 spline3 = new TSpline3("Test", xx, yy, nnp, "b1e1", f->Derivative(a), f->Derivative(b));
88
89 spline3->SetLineColor(kGreen);
90 spline3->SetLineWidth(3);
91 spline3->SetMarkerColor(kBlue);
92 spline3->SetMarkerStyle(20);
93 spline3->SetMarkerSize(1.5);
94
95 // Draw the third spline
96 spline3->Draw("lcpsame");
97
98 sprintf(text, "%3d knots", nnp);
99 textn->SetTitle(text);
100 gPad->Update();
101
102 //
103 gSystem->Sleep(500);
104 }
105}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char text
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gPad
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
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
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
1-Dim function class
Definition TF1.h:233
Use the TLine constructor to create a simple line.
Definition TLine.h:22
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:292
Class to create third splines to interpolate knots Arbitrary conditions can be introduced for first a...
Definition TSpline.h:182
Class to create quintic natural splines to interpolate knots Arbitrary conditions can be introduced f...
Definition TSpline.h:238
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TSpline.cxx:101
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:437
Base class for several text objects.
Definition TText.h:22
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:725