Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TF12.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Rene Brun 05/04/2003
3
4/*************************************************************************
5 * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TF12.h"
13#include "TH1.h"
14#include "TVirtualPad.h"
15
16#include <ostream>
17
18/** \class TF12
19 \ingroup Functions
20 A projection of a TF2 along X or Y
21
22It has the same behaviour as a TF1
23
24Example of a function
25
26Begin_Macro(source)
27{
28 auto f2 = new TF2("f2","sin(x)*sin(y)/(x*y)",0,5,0,5);
29 auto f12 = new TF12("f12",f2,0.1,"y");
30 f12->Draw();
31}
32End_Macro
33
34Side note: TF12 derives from TF1 but it does not store the number of parameters:
35so `GetNpar()` will be 0. Instead, it contains a private member pointing to the
36original TF2. Hence, one should not modify the (zero) parameters of the TF12
37but rather modify the original TF2 the TF12 is pointing to.
38
39*/
40
41////////////////////////////////////////////////////////////////////////////////
42/// TF12 default constructor
43
45{
46 fCase = 0;
47 fF2 = nullptr;
48 fXY = 0;
49}
50
51
52////////////////////////////////////////////////////////////////////////////////
53/// TF12 normal constructor.
54///
55/// Create a TF12 (special TF1) from a projection of a TF2
56/// for a fix value `xy` of Y if option="X" or X if option="Y"
57/// This value may be changed at any time via TF12::SetXY(xy)
58
59TF12::TF12(const char *name, TF2 *f2, Double_t xy, Option_t *option, EAddToList addToGlobList)
60 :TF1(name, "x", 0, 0, addToGlobList)
61{
63 fF2 = f2;
64 TString opt=option;
65 opt.ToLower();
66 if (!f2) {
67 Error("TF12","Pointer to TF2 is null");
68 return;
69 }
70 if (opt.Contains("y")) {
71 fXmin = f2->GetYmin();
72 fXmax = f2->GetYmax();
73 fCase = 1;
74 } else {
75 fXmin = f2->GetXmin();
76 fXmax = f2->GetXmax();
77 fCase = 0;
78 }
79 SetXY(xy); // has to be called after fCase is set, since it also sets the title
80}
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// F2 default destructor.
85
87{
88}
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// Copy constructor.
93
94TF12::TF12(const TF12 &f12) : TF1(f12)
95{
96 f12.TF12::Copy(*this);
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Copy this F2 to a new F2.
102
103void TF12::Copy(TObject &obj) const
104{
105 TF1::Copy(obj);
106 ((TF12&)obj).fXY = fXY;
107 ((TF12&)obj).fCase = fCase;
108 ((TF12&)obj).fF2 = fF2;
109}
110
111
112////////////////////////////////////////////////////////////////////////////////
113/// Draw a copy of this function with its current attributes.
114///
115/// This function MUST be used instead of Draw when you want to draw
116/// the same function with different TF12 options (settings) in the same canvas.
117///
118/// Possible option values are:
119///
120/// option | description
121/// -------|----------------------------------------
122/// "SAME" | superimpose on top of existing picture
123/// "L" | connect all computed points with a straight line
124/// "C" | connect all computed points with a smooth curve
125///
126/// Note that the default value is "F". Therefore to draw on top
127/// of an existing picture, specify option "SL"
128///
129/// Beware: this function does not clone the underlying TF2 instance,
130/// it just clones the TF12 objects but the all point to the same
131/// TF2. As a result, this function is only useful if you change
132/// the TF12 options (settings).
133/// If you want to draw projections of the same function with
134/// different TF2 parameters, you can do so by previously cloning
135/// the TF2 object for each variation.
136/// A clone-less alternative is to create a snapshot of the TF12 by
137/// creating a `new TGraph(tf12)` after each variation and then
138/// adding it to a TMultiGraph.
139
141{
142 TF12 *newf2 = new TF12();
143 Copy(*newf2);
144 newf2->AppendPad(option);
145 newf2->SetBit(kCanDelete);
146 return newf2;
147}
148
149
150////////////////////////////////////////////////////////////////////////////////
151/// Evaluate this formula
152///
153/// Computes the value of the referenced TF2 for a fix value of X or Y
154
156{
157 if (!fF2) return 0;
158 if (fCase == 0) {
159 return fF2->Eval(x,fXY,0);
160 } else {
161 return fF2->Eval(fXY,x,0);
162 }
163}
164
165
166////////////////////////////////////////////////////////////////////////////////
167/// Evaluate this function at point x[0]
168///
169/// x[0] is the value along X if fCase =0, the value along Y if fCase=1
170/// if params is non null, the array will be used instead of the internal TF2
171/// parameters
172
174{
175 if (!fF2) return 0;
176 Double_t xx[2];
177 if (fCase == 0) {
178 xx[0] = x[0];
179 xx[1] = fXY;
180 } else {
181 xx[0] = fXY;
182 xx[1] = x[0];
183 }
184 fF2->InitArgs(xx,params);
185 return fF2->EvalPar(xx,params);
186}
187
188
189////////////////////////////////////////////////////////////////////////////////
190/// Save primitive as a C++ statement(s) on output stream out
191
192void TF12::SavePrimitive(std::ostream &out, Option_t *option)
193{
194 thread_local Int_t save_f2_id = 9000;
195
196 fF2->SavePrimitive(out, TString::Format("nodraw#%d", ++save_f2_id));
197
198 TString f2Name = gInterpreter->MapCppName(TString::Format("%s%d", fF2->GetName(), save_f2_id));
199
201
202 out << " \n";
203 out << " TF12 *" << f12Name << " = new TF12(\"" << "*" << GetName() << "\", " << f2Name << ", " << fXY << ", "
204 << (fCase ? "\"y\"" : "\"x\"") << ");\n";
205
206 SavePrimitiveNameTitle(out, f12Name);
207
208 SaveFillAttributes(out, f12Name, -1, 0);
209 SaveMarkerAttributes(out, f12Name, -1, -1, -1);
210 SaveLineAttributes(out, f12Name, -1, -1, -1);
211
212 if (fHistogram && !strstr(option, "same")) {
213 GetXaxis()->SaveAttributes(out, f12Name, "->GetXaxis()");
214 GetYaxis()->SaveAttributes(out, f12Name, "->GetYaxis()");
215 }
216
217 SavePrimitiveDraw(out, f12Name, option);
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Set the value of the constant for the TF2
222///
223/// constant in X when projecting along Y
224/// constant in Y when projecting along X
225/// The function title is set to include the value of the constant
226/// The current pad is updated
227
229{
230 fXY = xy;
231 if (!fF2) return;
232 // option == "X" --> fCase = 0, project on X axis, X varies, Y constant
233 // option == "Y" --> fCase = 1, project on Y axis, Y varies, X constant
234 if (fCase == 0) SetTitle(TString::Format("%s (y=%g)",fF2->GetTitle(),xy));
235 else SetTitle(TString::Format("%s (x=%g)",fF2->GetTitle(),xy));
237 if (gPad) gPad->Modified();
238}
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
Option_t Option_t option
Option_t Option_t TPoint xy
char name[80]
Definition TGX11.cxx:142
#define gInterpreter
#define gPad
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:240
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:289
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
void SaveAttributes(std::ostream &out, const char *name, const char *subname) override
Save axis attributes as C++ statement(s) on output stream out.
Definition TAxis.cxx:722
A projection of a TF2 along X or Y.
Definition TF12.h:25
Int_t fCase
Projection along X(0), or Y(1)
Definition TF12.h:29
Double_t fXY
Value along Y (if projection X) or X (if projection Y)
Definition TF12.h:28
Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr) override
Evaluate this function at point x[0].
Definition TF12.cxx:173
TF12()
TF12 default constructor.
Definition TF12.cxx:44
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TF12.cxx:192
void Copy(TObject &f12) const override
Copy this F2 to a new F2.
Definition TF12.cxx:103
~TF12() override
F2 default destructor.
Definition TF12.cxx:86
virtual void SetXY(Double_t xy)
Set the value of the constant for the TF2.
Definition TF12.cxx:228
TF1 * DrawCopy(Option_t *option="") const override
Draw a copy of this function with its current attributes.
Definition TF12.cxx:140
TF2 * fF2
Pointer to the mother TF2.
Definition TF12.h:30
Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const override
Evaluate this formula.
Definition TF12.cxx:155
1-Dim function class
Definition TF1.h:182
virtual Double_t GetXmax() const
Definition TF1.h:525
EAddToList
Add to list behavior.
Definition TF1.h:189
TAxis * GetYaxis() const
Get y axis of the function.
Definition TF1.cxx:2460
Double_t fXmin
Lower bounds for the range.
Definition TF1.h:212
TString ProvideSaveName(Option_t *option)
Provide variable name for function for saving as primitive When TH1 or TGraph stores list of function...
Definition TF1.cxx:3270
TH1 * fHistogram
! Pointer to histogram used for visualisation
Definition TF1.h:232
void SetTitle(const char *title="") override
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition TF1.cxx:3614
void Copy(TObject &f1) const override
Copy this F1 to a new F1.
Definition TF1.cxx:1007
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition TF1.cxx:2531
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr)
Evaluate function with given coordinates and parameters.
Definition TF1.cxx:1499
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition TF1.cxx:1447
Double_t fXmax
Upper bounds for the range.
Definition TF1.h:213
virtual Double_t GetXmin() const
Definition TF1.h:521
TAxis * GetXaxis() const
Get x axis of the function.
Definition TF1.cxx:2449
A 2-Dim function with parameters.
Definition TF2.h:29
virtual Double_t GetYmax() const
Definition TF2.h:97
virtual Double_t GetYmin() const
Definition TF2.h:96
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TF2.cxx:917
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6932
void SavePrimitiveNameTitle(std::ostream &out, const char *variable_name)
Save object name and title into the output stream "out".
Definition TNamed.cxx:135
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:844
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
Basic string class.
Definition TString.h:137
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
Double_t x[n]
Definition legend1.C:17