Logo ROOT  
Reference Guide
TF3.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Rene Brun 27/10/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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 "TROOT.h"
13#include "TF3.h"
14#include "TBuffer.h"
15#include "TMath.h"
16#include "TH3.h"
17#include "TVirtualPad.h"
18#include "TRandom.h"
19#include "TVectorD.h"
20#include "Riostream.h"
21#include "TColor.h"
22#include "TVirtualFitter.h"
23#include "TVirtualHistPainter.h"
25#include <cassert>
26
28
29/** \class TF3
30 \ingroup Functions
31A 3-Dim function with parameters
32*/
33
34////////////////////////////////////////////////////////////////////////////////
35/// F3 default constructor
36
38{
39 fNpz = 0;
40 fZmin = 0;
41 fZmax = 1;
42}
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// F3 constructor using a formula definition
47///
48/// See TFormula constructor for explanation of the formula syntax.
49
50TF3::TF3(const char *name,const char *formula, Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax, Option_t * opt)
51 :TF2(name,formula,xmin,xmax,ymax,ymin,opt)
52{
53 fZmin = zmin;
54 fZmax = zmax;
55 fNpz = 30;
56 Int_t ndim = GetNdim();
57 // accept 1-d or 2-d formula
58 if (ndim < 3) fNdim = 3;
59 if (ndim > 3 && xmin < xmax && ymin < ymax && zmin < zmax) {
60 Error("TF3","function: %s/%s has dimension %d instead of 3",name,formula,ndim);
61 MakeZombie();
62 }
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// F3 constructor using a pointer to real function
67///
68/// \param[in] name object name
69/// \param[in] fcn pointer to real function
70/// \param[in] xmin,xmax x axis limits
71/// \param[in] ymin,ymax y axis limits
72/// \param[in] zmin,zmax z axis limits
73/// \param[in] npar is the number of free parameters used by the function
74/// \param[in] ndim number of dimensions
75///
76/// For example, for a 3-dim function with 3 parameters, the user function
77/// looks like:
78///
79/// Double_t fun1(Double_t *x, Double_t *par)
80/// return par[0]*x[2] + par[1]*exp(par[2]*x[0]*x[1]);
81///
82/// \warning A function created with this constructor cannot be Cloned.
83
85 :TF2(name,fcn,xmin,xmax,ymin,ymax,npar,ndim)
86{
87 fZmin = zmin;
88 fZmax = zmax;
89 fNpz = 30;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// F3 constructor using a pointer to real function---
94///
95/// \param[in] name object name
96/// \param[in] fcn pointer to real function
97/// \param[in] xmin,xmax x axis limits
98/// \param[in] ymin,ymax y axis limits
99/// \param[in] zmin,zmax z axis limits
100/// \param[in] npar is the number of free parameters used by the function
101/// \param[in] ndim number of dimensions
102///
103/// For example, for a 3-dim function with 3 parameters, the user function
104/// looks like:
105///
106/// Double_t fun1(Double_t *x, Double_t *par)
107/// return par[0]*x[2] + par[1]*exp(par[2]*x[0]*x[1]);
108///
109/// WARNING! A function created with this constructor cannot be Cloned.
110
111TF3::TF3(const char *name,Double_t (*fcn)(const Double_t *, const Double_t *), Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax, Int_t npar, Int_t ndim)
112 : TF2(name,fcn,xmin,xmax,ymin,ymax,npar,ndim),
113 fZmin(zmin),
114 fZmax(zmax),
115 fNpz(30)
116{
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// F3 constructor using a ParamFunctor
121///
122/// a functor class implementing operator() (double *, double *)
123///
124/// \param[in] name object name
125/// \param[in] f parameter functor
126/// \param[in] xmin,xmax x axis limits
127/// \param[in] ymin,ymax y axis limits
128/// \param[in] zmin,zmax z axis limits
129/// \param[in] npar is the number of free parameters used by the function
130/// \param[in] ndim number of dimensions
131///
132/// \warning A function created with this constructor cannot be Cloned.
133
135 : TF2(name, f, xmin, xmax, ymin, ymax, npar, ndim),
136 fZmin(zmin),
137 fZmax(zmax),
138 fNpz(30)
139{
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Operator =
144
146{
147 if (this != &rhs) {
148 rhs.Copy(*this);
149 }
150 return *this;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// F3 default destructor
155
157{
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Copy constructor.
162
163TF3::TF3(const TF3 &f3) : TF2()
164{
165 ((TF3&)f3).Copy(*this);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Copy this F3 to a new F3
170
171void TF3::Copy(TObject &obj) const
172{
173 TF2::Copy(obj);
174 ((TF3&)obj).fZmin = fZmin;
175 ((TF3&)obj).fZmax = fZmax;
176 ((TF3&)obj).fNpz = fNpz;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Compute distance from point px,py to a function
181///
182/// Compute the closest distance of approach from point px,py to this function.
183/// The distance is computed in pixels units.
184
185
187{
188 return TF1::DistancetoPrimitive(px, py);
189
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Draw this function with its current attributes
194
196{
197 TString opt = option;
198 opt.ToLower();
199 if (gPad && !opt.Contains("same")) gPad->Clear();
200
202
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Execute action corresponding to one event
207///
208/// This member function is called when a F3 is clicked with the locator
209
211{
212 TF1::ExecuteEvent(event, px, py);
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Return minimum/maximum value of the function
217///
218/// To find the minimum on a range, first set this range via the SetRange function
219/// If a vector x of coordinate is passed it will be used as starting point for the minimum.
220/// In addition on exit x will contain the coordinate values at the minimuma
221/// If x is NULL or x is inifinity or NaN, first, a grid search is performed to find the initial estimate of the
222/// minimum location. The range of the function is divided into fNpx and fNpy
223/// sub-ranges. If the function is "good" (or "bad"), these values can be changed
224/// by SetNpx and SetNpy functions
225///
226/// Then, a minimization is used with starting values found by the grid search
227/// The minimizer algorithm used (by default Minuit) can be changed by callinga
228/// ROOT::Math::Minimizer::SetDefaultMinimizerType("..")
229/// Other option for the minimizer can be set using the static method of the MinimizerOptions class
230
232{
233 //First do a grid search with step size fNpx and fNpy
234
235 Double_t xx[3];
236 Double_t rsign = (findmax) ? -1. : 1.;
237 TF3 & function = const_cast<TF3&>(*this); // needed since EvalPar is not const
238 Double_t xxmin = 0, yymin = 0, zzmin = 0, ttmin = 0;
239 if (x == NULL || ( (x!= NULL) && ( !TMath::Finite(x[0]) || !TMath::Finite(x[1]) || !TMath::Finite(x[2]) ) ) ){
240 Double_t dx = (fXmax - fXmin)/fNpx;
241 Double_t dy = (fYmax - fYmin)/fNpy;
242 Double_t dz = (fZmax - fZmin)/fNpz;
243 xxmin = fXmin;
244 yymin = fYmin;
245 zzmin = fZmin;
246 ttmin = rsign * TMath::Infinity();
247 for (Int_t i=0; i<fNpx; i++){
248 xx[0]=fXmin + (i+0.5)*dx;
249 for (Int_t j=0; j<fNpy; j++){
250 xx[1]=fYmin+(j+0.5)*dy;
251 for (Int_t k=0; k<fNpz; k++){
252 xx[2] = fZmin+(k+0.5)*dz;
253 Double_t tt = function(xx);
254 if (rsign*tt < rsign*ttmin) {xxmin = xx[0], yymin = xx[1]; zzmin = xx[2]; ttmin=tt;}
255 }
256 }
257 }
258
259 xxmin = TMath::Min(fXmax, xxmin);
260 yymin = TMath::Min(fYmax, yymin);
261 zzmin = TMath::Min(fZmax, zzmin);
262 }
263 else {
264 xxmin = x[0];
265 yymin = x[1];
266 zzmin = x[2];
267 zzmin = function(x);
268 }
269 xx[0] = xxmin;
270 xx[1] = yymin;
271 xx[2] = zzmin;
272
273 double fmin = GetMinMaxNDim(xx,findmax);
274 if (rsign*fmin < rsign*zzmin) {
275 if (x) {x[0] = xx[0]; x[1] = xx[1]; x[2] = xx[2];}
276 return fmin;
277 }
278 // here if minimization failed
279 if (x) { x[0] = xxmin; x[1] = yymin; x[2] = zzmin; }
280 return ttmin;
281}
282
283////////////////////////////////////////////////////////////////////////////////
284/// Compute the X, Y and Z values corresponding to the minimum value of the function
285/// on its range.
286///
287/// Returns the function value at the minimum.
288/// To find the minimum on a subrange, use the SetRange() function first.
289///
290/// Method:
291/// First, a grid search is performed to find the initial estimate of the
292/// minimum location. The range of the function is divided
293/// into fNpx,fNpy and fNpz sub-ranges. If the function is "good" (or "bad"),
294/// these values can be changed by SetNpx(), SetNpy() and SetNpz() functions.
295/// Then, Minuit minimization is used with starting values found by the grid search
296///
297/// Note that this method will always do first a grid search in contrast to GetMinimum
298
300{
301 double xx[3] = { 0,0,0 };
302 xx[0] = TMath::QuietNaN(); // to force to do grid search in TF3::FindMinMax
303 double fmin = FindMinMax(xx, false);
304 x = xx[0]; y = xx[1]; z = xx[2];
305 return fmin;
306
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Compute the X, Y and Z values corresponding to the maximum value of the function
311/// on its range.
312///
313/// Return the function value at the maximum. See TF3::GetMinimumXYZ
314
316{
317 double xx[3] = { 0,0,0 };
318 xx[0] = TMath::QuietNaN(); // to force to do grid search in TF3::FindMinMax
319 double fmax = FindMinMax(xx, true);
320 x = xx[0]; y = xx[1]; z = xx[2];
321 return fmax;
322
323}
324
325////////////////////////////////////////////////////////////////////////////////
326/// Return 3 random numbers following this function shape
327///
328/// The distribution contained in this TF3 function is integrated
329/// over the cell contents.
330/// It is normalized to 1.
331/// Getting the three random numbers implies:
332/// - Generating a random number between 0 and 1 (say r1)
333/// - Look in which cell in the normalized integral r1 corresponds to
334/// - make a linear interpolation in the returned cell
335///
336/// IMPORTANT NOTE
337///
338/// The integral of the function is computed at fNpx * fNpy * fNpz points.
339/// If the function has sharp peaks, you should increase the number of
340/// points (SetNpx, SetNpy, SetNpz) such that the peak is correctly tabulated
341/// at several points.
342
343void TF3::GetRandom3(Double_t &xrandom, Double_t &yrandom, Double_t &zrandom, TRandom * rng)
344{
345 // Check if integral array must be built
346 Int_t i,j,k,cell;
347 Double_t dx = (fXmax-fXmin)/fNpx;
348 Double_t dy = (fYmax-fYmin)/fNpy;
349 Double_t dz = (fZmax-fZmin)/fNpz;
350 Int_t ncells = fNpx*fNpy*fNpz;
351 Double_t xx[3];
352 Double_t *parameters = GetParameters();
353 InitArgs(xx,parameters);
354 if (fIntegral.empty() ) {
355 fIntegral.resize(ncells+1);
356 //fIntegral = new Double_t[ncells+1];
357 fIntegral[0] = 0;
358 Double_t integ;
359 Int_t intNegative = 0;
360 cell = 0;
361 for (k=0;k<fNpz;k++) {
362 xx[2] = fZmin+(k+0.5)*dz;
363 for (j=0;j<fNpy;j++) {
364 xx[1] = fYmin+(j+0.5)*dy;
365 for (i=0;i<fNpx;i++) {
366 xx[0] = fXmin+(i+0.5)*dx;
367 integ = EvalPar(xx,parameters);
368 if (integ < 0) {intNegative++; integ = -integ;}
369 fIntegral[cell+1] = fIntegral[cell] + integ;
370 cell++;
371 }
372 }
373 }
374 if (intNegative > 0) {
375 Warning("GetRandom3","function:%s has %d negative values: abs assumed",GetName(),intNegative);
376 }
377 if (fIntegral[ncells] == 0) {
378 Error("GetRandom3","Integral of function is zero");
379 return;
380 }
381 for (i=1;i<=ncells;i++) { // normalize integral to 1
382 fIntegral[i] /= fIntegral[ncells];
383 }
384 }
385
386// return random numbers
387 Double_t r;
388 if (!rng) rng = gRandom;
389 r = rng->Rndm();
390 cell = TMath::BinarySearch(ncells,fIntegral.data(),r);
391 k = cell/(fNpx*fNpy);
392 j = (cell -k*fNpx*fNpy)/fNpx;
393 i = cell -fNpx*(j +fNpy*k);
394 xrandom = fXmin +dx*i +dx*rng->Rndm();
395 yrandom = fYmin +dy*j +dy*rng->Rndm();
396 zrandom = fZmin +dz*k +dz*rng->Rndm();
397}
398
399////////////////////////////////////////////////////////////////////////////////
400/// Return range of function
401
403{
404 xmin = fXmin;
405 xmax = fXmax;
406 ymin = fYmin;
407 ymax = fYmax;
408 zmin = fZmin;
409 zmax = fZmax;
410}
411
412
413////////////////////////////////////////////////////////////////////////////////
414/// Get value corresponding to X in array of fSave values
415
417{
418 //if (fNsave <= 0) return 0;
419 if (fSave.empty()) return 0;
420 Int_t np = fSave.size() - 9;
425 Double_t zmin = Double_t(fSave[np+4]);
426 Double_t zmax = Double_t(fSave[np+5]);
427 Int_t npx = Int_t(fSave[np+6]);
428 Int_t npy = Int_t(fSave[np+7]);
429 Int_t npz = Int_t(fSave[np+8]);
430 Double_t x = Double_t(xx[0]);
431 Double_t dx = (xmax-xmin)/npx;
432 if (x < xmin || x > xmax) return 0;
433 if (dx <= 0) return 0;
434 Double_t y = Double_t(xx[1]);
435 Double_t dy = (ymax-ymin)/npy;
436 if (y < ymin || y > ymax) return 0;
437 if (dy <= 0) return 0;
438 Double_t z = Double_t(xx[2]);
439 Double_t dz = (zmax-zmin)/npz;
440 if (z < zmin || z > zmax) return 0;
441 if (dz <= 0) return 0;
442
443 //we make a trilinear interpolation using the 8 points surrounding x,y,z
444 Int_t ibin = Int_t((x-xmin)/dx);
445 Int_t jbin = Int_t((y-ymin)/dy);
446 Int_t kbin = Int_t((z-zmin)/dz);
447 Double_t xlow = xmin + ibin*dx;
448 Double_t ylow = ymin + jbin*dy;
449 Double_t zlow = zmin + kbin*dz;
450 Double_t t = (x-xlow)/dx;
451 Double_t u = (y-ylow)/dy;
452 Double_t v = (z-zlow)/dz;
453 Int_t k1 = (ibin ) + (npx+1)*((jbin ) + (npy+1)*(kbin ));
454 Int_t k2 = (ibin+1) + (npx+1)*((jbin ) + (npy+1)*(kbin ));
455 Int_t k3 = (ibin+1) + (npx+1)*((jbin+1) + (npy+1)*(kbin ));
456 Int_t k4 = (ibin ) + (npx+1)*((jbin+1) + (npy+1)*(kbin ));
457 Int_t k5 = (ibin ) + (npx+1)*((jbin ) + (npy+1)*(kbin+1));
458 Int_t k6 = (ibin+1) + (npx+1)*((jbin ) + (npy+1)*(kbin+1));
459 Int_t k7 = (ibin+1) + (npx+1)*((jbin+1) + (npy+1)*(kbin+1));
460 Int_t k8 = (ibin ) + (npx+1)*((jbin+1) + (npy+1)*(kbin+1));
461 Double_t r = (1-t)*(1-u)*(1-v)*fSave[k1] + t*(1-u)*(1-v)*fSave[k2] + t*u*(1-v)*fSave[k3] + (1-t)*u*(1-v)*fSave[k4] +
462 (1-t)*(1-u)*v*fSave[k5] + t*(1-u)*v*fSave[k6] + t*u*v*fSave[k7] + (1-t)*u*v*fSave[k8];
463 return r;
464}
465
466////////////////////////////////////////////////////////////////////////////////
467/// Return Integral of a 3d function in range [ax,bx],[ay,by],[az,bz]
468/// with a desired relative accuracy.
469
471{
472 Double_t a[3], b[3];
473 a[0] = ax;
474 b[0] = bx;
475 a[1] = ay;
476 b[1] = by;
477 a[2] = az;
478 b[2] = bz;
479 Double_t relerr = 0;
480 Int_t n = 3;
482 Int_t nfnevl, ifail;
483 Double_t result = IntegralMultiple(n,a,b,maxpts,epsrel,epsrel, relerr,nfnevl,ifail);
484 if (ifail > 0) {
485 Warning("Integral","failed for %s code=%d, maxpts=%d, epsrel=%g, nfnevl=%d, relerr=%g ",GetName(),ifail,maxpts,epsrel,nfnevl,relerr);
486 }
487 if (gDebug) {
488 Info("Integral","Integral of %s using %d and tol=%f is %f , relerr=%f nfcn=%d",GetName(),maxpts,epsrel,result,relerr,nfnevl);
489 }
490 return result;
491}
492
493////////////////////////////////////////////////////////////////////////////////
494/// Return kTRUE is the point is inside the function range
495
497{
498 if (x[0] < fXmin || x[0] > fXmax) return kFALSE;
499 if (x[1] < fYmin || x[1] > fYmax) return kFALSE;
500 if (x[2] < fZmin || x[2] > fZmax) return kFALSE;
501 return kTRUE;
502}
503
504////////////////////////////////////////////////////////////////////////////////
505/// Create a histogram for axis range.
506
508{
509 TH1* h = new TH3F("R__TF3",(char*)GetTitle(),fNpx,fXmin,fXmax
511 ,fNpz,fZmin,fZmax);
512 h->SetDirectory(0);
513 return h;
514}
515
516////////////////////////////////////////////////////////////////////////////////
517/// Paint this 3-D function with its current attributes
518
520{
521
522 TString opt = option;
523 opt.ToLower();
524
525//- Create a temporary histogram and fill each channel with the function value
526 if (!fHistogram) {
527 fHistogram = new TH3F("R__TF3",(char*)GetTitle(),fNpx,fXmin,fXmax
529 ,fNpz,fZmin,fZmax);
531 }
532
534
535 if (opt.Index("tf3") == kNPOS)
536 opt.Append("tf3");
537
538 fHistogram->Paint(opt.Data());
539}
540
541////////////////////////////////////////////////////////////////////////////////
542/// Set the function clipping box (for drawing) "off".
543
545{
547 fClipBox[0] = fClipBox[1] = fClipBox[2] = 0;
548}
549
550////////////////////////////////////////////////////////////////////////////////
551/// Save values of function in array fSave
552
554{
555 if (!fSave.empty()) fSave.clear();
556 Int_t nsave = (fNpx+1)*(fNpy+1)*(fNpz+1);
557 Int_t fNsave = nsave+9;
558 assert(fNsave > 9);
559 //fSave = new Double_t[fNsave];
560 fSave.resize(fNsave);
561 Int_t i,j,k,l=0;
562 Double_t dx = (xmax-xmin)/fNpx;
563 Double_t dy = (ymax-ymin)/fNpy;
564 Double_t dz = (zmax-zmin)/fNpz;
565 if (dx <= 0) {
566 dx = (fXmax-fXmin)/fNpx;
567 xmin = fXmin +0.5*dx;
568 xmax = fXmax -0.5*dx;
569 }
570 if (dy <= 0) {
571 dy = (fYmax-fYmin)/fNpy;
572 ymin = fYmin +0.5*dy;
573 ymax = fYmax -0.5*dy;
574 }
575 if (dz <= 0) {
576 dz = (fZmax-fZmin)/fNpz;
577 zmin = fZmin +0.5*dz;
578 zmax = fZmax -0.5*dz;
579 }
580 Double_t xv[3];
581 Double_t *pp = GetParameters();
582 InitArgs(xv,pp);
583 for (k=0;k<=fNpz;k++) {
584 xv[2] = zmin + dz*k;
585 for (j=0;j<=fNpy;j++) {
586 xv[1] = ymin + dy*j;
587 for (i=0;i<=fNpx;i++) {
588 xv[0] = xmin + dx*i;
589 fSave[l] = EvalPar(xv,pp);
590 l++;
591 }
592 }
593 }
594 fSave[nsave+0] = xmin;
595 fSave[nsave+1] = xmax;
596 fSave[nsave+2] = ymin;
597 fSave[nsave+3] = ymax;
598 fSave[nsave+4] = zmin;
599 fSave[nsave+5] = zmax;
600 fSave[nsave+6] = fNpx;
601 fSave[nsave+7] = fNpy;
602 fSave[nsave+8] = fNpz;
603}
604
605////////////////////////////////////////////////////////////////////////////////
606/// Save primitive as a C++ statement(s) on output stream out
607
608void TF3::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
609{
610 char quote = '"';
611 out<<" "<<std::endl;
612 if (gROOT->ClassSaved(TF3::Class())) {
613 out<<" ";
614 } else {
615 out<<" TF3 *";
616 }
617 if (!fMethodCall) {
618 out<<GetName()<<" = new TF3("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote<<","<<fXmin<<","<<fXmax<<","<<fYmin<<","<<fYmax<<","<<fZmin<<","<<fZmax<<");"<<std::endl;
619 } else {
620 out<<GetName()<<" = new TF3("<<quote<<GetName()<<quote<<","<<GetTitle()<<","<<fXmin<<","<<fXmax<<","<<fYmin<<","<<fYmax<<","<<fZmin<<","<<fZmax<<","<<GetNpar()<<");"<<std::endl;
621 }
622
623 if (GetFillColor() != 0) {
624 if (GetFillColor() > 228) {
626 out<<" "<<GetName()<<"->SetFillColor(ci);" << std::endl;
627 } else
628 out<<" "<<GetName()<<"->SetFillColor("<<GetFillColor()<<");"<<std::endl;
629 }
630 if (GetLineColor() != 1) {
631 if (GetLineColor() > 228) {
633 out<<" "<<GetName()<<"->SetLineColor(ci);" << std::endl;
634 } else
635 out<<" "<<GetName()<<"->SetLineColor("<<GetLineColor()<<");"<<std::endl;
636 }
637 if (GetNpz() != 100) {
638 out<<" "<<GetName()<<"->SetNpz("<<GetNpz()<<");"<<std::endl;
639 }
640 if (GetChisquare() != 0) {
641 out<<" "<<GetName()<<"->SetChisquare("<<GetChisquare()<<");"<<std::endl;
642 }
643 Double_t parmin, parmax;
644 for (Int_t i=0;i<GetNpar();i++) {
645 out<<" "<<GetName()<<"->SetParameter("<<i<<","<<GetParameter(i)<<");"<<std::endl;
646 out<<" "<<GetName()<<"->SetParError("<<i<<","<<GetParError(i)<<");"<<std::endl;
647 GetParLimits(i,parmin,parmax);
648 out<<" "<<GetName()<<"->SetParLimits("<<i<<","<<parmin<<","<<parmax<<");"<<std::endl;
649 }
650 out<<" "<<GetName()<<"->Draw("
651 <<quote<<option<<quote<<");"<<std::endl;
652}
653
654////////////////////////////////////////////////////////////////////////////////
655/// Set the function clipping box (for drawing) "on" and define the clipping box.
656/// xclip, yclip and zclip is a point within the function range. All the
657/// function values having x<=xclip and y<=yclip and z>=zclip are clipped.
658
660{
662 fClipBox[0] = xclip;
663 fClipBox[1] = yclip;
664 fClipBox[2] = zclip;
665}
666
667////////////////////////////////////////////////////////////////////////////////
668/// Set the number of points used to draw the function
669///
670/// The default number of points along x is 30 for 2-d/3-d functions.
671/// You can increase this value to get a better resolution when drawing
672/// pictures with sharp peaks or to get a better result when using TF3::GetRandom2
673/// the minimum number of points is 4, the maximum is 10000 for 2-d/3-d functions
674
676{
677 if (npz < 4) {
678 Warning("SetNpz","Number of points must be >=4 && <= 10000, fNpz set to 4");
679 fNpz = 4;
680 } else if(npz > 10000) {
681 Warning("SetNpz","Number of points must be >=4 && <= 10000, fNpz set to 10000");
682 fNpz = 10000;
683 } else {
684 fNpz = npz;
685 }
686 Update();
687}
688
689////////////////////////////////////////////////////////////////////////////////
690/// Initialize the upper and lower bounds to draw the function
691
693{
694 fXmin = xmin;
695 fXmax = xmax;
696 fYmin = ymin;
697 fYmax = ymax;
698 fZmin = zmin;
699 fZmax = zmax;
700 Update();
701}
702
703////////////////////////////////////////////////////////////////////////////////
704/// Stream an object of class TF3.
705
707{
708 if (R__b.IsReading()) {
709 UInt_t R__s, R__c;
710 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
711 if (R__v > 0) {
712 R__b.ReadClassBuffer(TF3::Class(), this, R__v, R__s, R__c);
713 return;
714 }
715
716 } else {
717 Int_t saved = 0;
718 if (fType != EFType::kFormula && fSave.empty() ) { saved = 1; Save(fXmin,fXmax,fYmin,fYmax,fZmin,fZmax);}
719
720 R__b.WriteClassBuffer(TF3::Class(),this);
721
722 if (saved) { fSave.clear(); }
723 }
724}
725
726////////////////////////////////////////////////////////////////////////////////
727/// Return x^nx * y^ny * z^nz moment of a 3d function in range [ax,bx],[ay,by],[az,bz]
728/// \author Gene Van Buren <gene@bnl.gov>
729
731{
732 Double_t norm = Integral(ax,bx,ay,by,az,bz,epsilon);
733 if (norm == 0) {
734 Error("Moment3", "Integral zero over range");
735 return 0;
736 }
737
738 // define integrand function as a lambda : g(x,y,z)= x^(nx) * y^(ny) * z^(nz) * f(x,y,z)
739 auto integrand = [&](double *x, double *) {
740 return std::pow(x[0], nx) * std::pow(x[1], ny) * std::pow(x[2], nz) * this->EvalPar(x, nullptr);
741 };
742 // compute integral of g(x,y,z)
743 TF3 fnc("TF3_ExpValHelper", integrand, ax, bx, ay, by, az, bz, 0);
744 // set same points as current function to get correct max points when computing the integral
745 fnc.fNpx = fNpx;
746 fnc.fNpy = fNpy;
747 fnc.fNpz = fNpz;
748 return fnc.Integral(ax, bx, ay, by, az, bz, epsilon) / norm;
749}
750
751////////////////////////////////////////////////////////////////////////////////
752/// Return x^nx * y^ny * z^nz central moment of a 3d function in range [ax,bx],[ay,by],[az,bz]
753/// \author Gene Van Buren <gene@bnl.gov>
754
756{
757 Double_t norm = Integral(ax,bx,ay,by,az,bz,epsilon);
758 if (norm == 0) {
759 Error("CentralMoment3", "Integral zero over range");
760 return 0;
761 }
762
763 Double_t xbar = 0;
764 Double_t ybar = 0;
765 Double_t zbar = 0;
766 if (nx!=0) {
767 // compute first momentum in x
768 auto integrandX = [&](double *x, double *) { return x[0] * this->EvalPar(x, nullptr); };
769 TF3 fncx("TF3_ExpValHelperx", integrandX, ax, bx, ay, by, az, bz, 0);
770 fncx.fNpx = fNpx;
771 fncx.fNpy = fNpy;
772 fncx.fNpz = fNpz;
773 xbar = fncx.Integral(ax, bx, ay, by, az, bz, epsilon) / norm;
774 }
775 if (ny!=0) {
776 auto integrandY = [&](double *x, double *) { return x[1] * this->EvalPar(x, nullptr); };
777 TF3 fncy("TF3_ExpValHelpery", integrandY, ax, bx, ay, by, az, bz, 0);
778 fncy.fNpx = fNpx;
779 fncy.fNpy = fNpy;
780 fncy.fNpz = fNpz;
781 ybar = fncy.Integral(ax,bx,ay,by,az,bz,epsilon)/norm;
782 }
783 if (nz!=0) {
784 auto integrandZ = [&](double *x, double *) { return x[2] * this->EvalPar(x, nullptr); };
785 TF3 fncz("TF3_ExpValHelperz", integrandZ, ax, bx, ay, by, az, bz, 0);
786 fncz.fNpx = fNpx;
787 fncz.fNpy = fNpy;
788 fncz.fNpz = fNpz;
789 zbar = fncz.Integral(ax,bx,ay,by,az,bz,epsilon)/norm;
790 }
791 // define integrand function as a lambda : g(x,y)= (x-xbar)^(nx) * (y-ybar)^(ny) * f(x,y)
792 auto integrand = [&](double *x, double *) {
793 double xxx = (nx != 0) ? std::pow(x[0] - xbar, nx) : 1.;
794 double yyy = (ny != 0) ? std::pow(x[1] - ybar, ny) : 1.;
795 double zzz = (nz != 0) ? std::pow(x[2] - zbar, nz) : 1.;
796 return xxx * yyy * zzz * this->EvalPar(x, nullptr);
797 };
798 // compute integral of g(x,y, z)
799 TF3 fnc("TF3_ExpValHelper",integrand,ax,bx,ay,by,az,bz,0) ;
800 fnc.fNpx = fNpx;
801 fnc.fNpy = fNpy;
802 fnc.fNpz = fNpz;
803 return fnc.Integral(ax,bx,ay,by,az,bz,epsilon)/norm;
804}
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
const Ssiz_t kNPOS
Definition: RtypesCore.h:124
int Int_t
Definition: RtypesCore.h:45
short Version_t
Definition: RtypesCore.h:65
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
const char Option_t
Definition: RtypesCore.h:66
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t option
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 b
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 GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
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 result
char name[80]
Definition: TGX11.cxx:110
float xmin
Definition: THbookFile.cxx:95
float ymin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
float ymax
Definition: THbookFile.cxx:95
Int_t gDebug
Definition: TROOT.cxx:585
#define gROOT
Definition: TROOT.h:404
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gPad
Definition: TVirtualPad.h:288
Param Functor class for Multidimensional functions.
Definition: ParamFunctor.h:273
virtual Color_t GetFillColor() const
Return the fill area color.
Definition: TAttFill.h:30
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Bool_t IsReading() const
Definition: TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
static void SaveColor(std::ostream &out, Int_t ci)
Save a color with index > 228 as a C++ statement(s) on output stream out.
Definition: TColor.cxx:2186
Int_t fNdim
Function dimension.
Definition: TF1.h:246
virtual void GetParLimits(Int_t ipar, Double_t &parmin, Double_t &parmax) const
Return limits for parameter ipar.
Definition: TF1.cxx:1955
virtual Double_t GetParError(Int_t ipar) const
Return value of parameter number ipar.
Definition: TF1.cxx:1945
Double_t GetChisquare() const
Definition: TF1.h:449
Double_t fXmin
Lower bounds for the range.
Definition: TF1.h:243
std::unique_ptr< TMethodCall > fMethodCall
! Pointer to MethodCall in case of interpreted function
Definition: TF1.h:264
virtual void Update()
Called by functions such as SetRange, SetNpx, SetParameters to force the deletion of the associated h...
Definition: TF1.cxx:3653
virtual Int_t GetNpar() const
Definition: TF1.h:486
TH1 * fHistogram
! Pointer to histogram used for visualisation
Definition: TF1.h:263
virtual Double_t * GetParameters() const
Definition: TF1.h:525
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=0)
Evaluate function with given coordinates and parameters.
Definition: TF1.cxx:1483
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition: TF1.cxx:2492
virtual Double_t IntegralMultiple(Int_t n, const Double_t *a, const Double_t *b, Int_t maxpts, Double_t epsrel, Double_t epsabs, Double_t &relerr, Int_t &nfnevl, Int_t &ifail)
This function computes, to an attempted specified accuracy, the value of the integral.
Definition: TF1.cxx:2860
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a function.
Definition: TF1.cxx:1302
EFType fType
Definition: TF1.h:248
Int_t fNpx
Number of points used for the graphical representation.
Definition: TF1.h:247
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition: TF1.cxx:1551
std::vector< Double_t > fSave
Array of fNsave function values.
Definition: TF1.h:257
virtual Double_t GetMinMaxNDim(Double_t *x, Bool_t findmax, Double_t epsilon=0, Int_t maxiter=0) const
Find the minimum of a function of whatever dimension.
Definition: TF1.cxx:1738
std::vector< Double_t > fIntegral
! Integral of function binned on fNpx bins
Definition: TF1.h:258
Double_t fXmax
Upper bounds for the range.
Definition: TF1.h:244
virtual Int_t GetNdim() const
Definition: TF1.h:490
virtual Double_t GetParameter(Int_t ipar) const
Definition: TF1.h:517
A 2-Dim function with parameters.
Definition: TF2.h:29
void Copy(TObject &f2) const override
Copy this F2 to a new F2.
Definition: TF2.cxx:211
Int_t fNpy
Number of points along y used for the graphical representation.
Definition: TF2.h:34
Double_t fYmax
Upper bound for the range in y.
Definition: TF2.h:33
Double_t fYmin
Lower bound for the range in y.
Definition: TF2.h:32
A 3-Dim function with parameters.
Definition: TF3.h:28
void GetRange(Double_t &xmin, Double_t &xmax) const override
Return range of a 1-D function.
Definition: TF3.h:141
static TClass * Class()
TF3()
F3 default constructor.
Definition: TF3.cxx:37
void Paint(Option_t *option="") override
Paint this 3-D function with its current attributes.
Definition: TF3.cxx:519
Int_t GetNpz() const
Definition: TF3.h:93
virtual void GetRandom3(Double_t &xrandom, Double_t &yrandom, Double_t &zrandom, TRandom *rng=nullptr)
Return 3 random numbers following this function shape.
Definition: TF3.cxx:343
Bool_t IsInside(const Double_t *x) const override
Return kTRUE is the point is inside the function range.
Definition: TF3.cxx:496
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition: TF3.cxx:210
Double_t fZmin
Lower bound for the range in z.
Definition: TF3.h:31
TF3 & operator=(const TF3 &rhs)
Operator =.
Definition: TF3.cxx:145
virtual void SetNpz(Int_t npz=30)
Set the number of points used to draw the function.
Definition: TF3.cxx:675
virtual Double_t GetMaximumXYZ(Double_t &x, Double_t &y, Double_t &z)
Compute the X, Y and Z values corresponding to the maximum value of the function on its range.
Definition: TF3.cxx:315
virtual Double_t CentralMoment3(Double_t nx, Double_t ax, Double_t bx, Double_t ny, Double_t ay, Double_t by, Double_t nz, Double_t az, Double_t bz, Double_t epsilon=0.000001)
Return x^nx * y^ny * z^nz central moment of a 3d function in range [ax,bx],[ay,by],...
Definition: TF3.cxx:755
void Copy(TObject &f3) const override
Copy this F3 to a new F3.
Definition: TF3.cxx:171
Double_t FindMinMax(Double_t *x, bool findmax) const override
Return minimum/maximum value of the function.
Definition: TF3.cxx:231
~TF3() override
F3 default destructor.
Definition: TF3.cxx:156
Bool_t fClipBoxOn
! Is clip box on
Definition: TF3.h:34
virtual Double_t GetMinimumXYZ(Double_t &x, Double_t &y, Double_t &z)
Compute the X, Y and Z values corresponding to the minimum value of the function on its range.
Definition: TF3.cxx:299
virtual Double_t Moment3(Double_t nx, Double_t ax, Double_t bx, Double_t ny, Double_t ay, Double_t by, Double_t nz, Double_t az, Double_t bz, Double_t epsilon=0.000001)
Return x^nx * y^ny * z^nz moment of a 3d function in range [ax,bx],[ay,by],[az,bz].
Definition: TF3.cxx:730
Int_t fNpz
Number of points along z used for the graphical representation.
Definition: TF3.h:33
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition: TF3.cxx:195
virtual void SetClippingBoxOn(Double_t xclip=0, Double_t yclip=0, Double_t zclip=0)
Set the function clipping box (for drawing) "on" and define the clipping box.
Definition: TF3.cxx:659
virtual void SetClippingBoxOff()
Set the function clipping box (for drawing) "off".
Definition: TF3.cxx:544
Double_t fZmax
Upper bound for the range in z.
Definition: TF3.h:32
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a function.
Definition: TF3.cxx:186
virtual Double_t Integral(Double_t ax, Double_t bx, Double_t ay, Double_t by, Double_t az, Double_t bz, Double_t epsrel=1.e-6)
Return Integral of a 3d function in range [ax,bx],[ay,by],[az,bz] with a desired relative accuracy.
Definition: TF3.cxx:470
void Streamer(TBuffer &) override
Stream an object of class TF3.
Definition: TF3.cxx:706
TH1 * CreateHistogram() override
Create a histogram for axis range.
Definition: TF3.cxx:507
void Save(Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax) override
Save values of function in array fSave.
Definition: TF3.cxx:553
Double_t GetSave(const Double_t *x) override
Get value corresponding to X in array of fSave values.
Definition: TF3.cxx:416
Double_t fClipBox[3]
! Coordinates of clipbox
Definition: TF3.h:35
void SetRange(Double_t xmin, Double_t xmax) override
Initialize the upper and lower bounds to draw the function.
Definition: TF3.h:145
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition: TF3.cxx:608
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8812
TVirtualHistPainter * GetPainter(Option_t *option="")
Return pointer to painter.
Definition: TH1.cxx:4494
void Paint(Option_t *option="") override
Control routine to paint any kind of histograms.
Definition: TH1.cxx:6198
3-D histogram with a float per channel (see TH1 documentation)}
Definition: TH3.h:273
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:107
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
void MakeZombie()
Definition: TObject.h:49
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:867
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
Double_t Rndm() override
Machine independent random number generator.
Definition: TRandom.cxx:552
Basic string class.
Definition: TString.h:136
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1150
const char * Data() const
Definition: TString.h:369
TString & Append(const char *cs)
Definition: TString.h:564
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:639
virtual void ProcessMessage(const char *mess, const TObject *obj)=0
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition: RVec.hxx:1753
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:167
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:208
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754
Definition: TMath.h:851
Int_t Finite(Double_t x)
Check if it is finite with a mask in order to be consistent in presence of fast math.
Definition: TMath.h:721
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:176
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMathBase.h:274
Double_t Infinity()
Returns an infinity as defined by the IEEE standard.
Definition: TMath.h:864
auto * tt
Definition: textangle.C:16
auto * l
Definition: textangle.C:4
auto * a
Definition: textangle.C:12
double epsilon
Definition: triangle.c:618