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