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