Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HFitInterface.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: L. Moneta Thu Aug 31 10:40:20 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Implementation file for class TH1Interface
12
13#include "HFitInterface.h"
14
15#include "Fit/BinData.h"
16#include "Fit/SparseData.h"
17#include "Fit/FitResult.h"
18#include "Math/IParamFunction.h"
19
20#include <cassert>
21#include <cmath>
22#include <utility>
23#include <vector>
24
25#include "TH1.h"
26#include "THnBase.h"
27#include "TF1.h"
28#include "TGraph2D.h"
29#include "TGraph.h"
30#include "TGraphErrors.h"
31#include "TMultiGraph.h"
32#include "TList.h"
33#include "TError.h"
34
35
36//#define DEBUG
37#ifdef DEBUG
38#include "TClass.h"
39#include <iostream>
40#endif
41
42
43namespace ROOT {
44
45namespace Fit {
46
47// add a namespace to distinguish from the Graph functions
48namespace HFitInterface {
49
50
51bool IsPointOutOfRange(const TF1 * func, const double * x) {
52 // function to check if a point is outside range
53 if (func ==nullptr) return false;
54 return !func->IsInside(x);
55}
56
57bool AdjustError(const DataOptions & option, double & error, double value = 1) {
58 // adjust the given error according to the option
59 // return false when point must be skipped.
60 // When point error = 0, the point is kept if the option UseEmpty is set or if
61 // fErrors1 is set and the point value is not zero.
62 // The value should be used only for points representing counts (histograms), not for the graph.
63 // In the graph points with zero errors are by default skipped indepentently of the value.
64 // If one wants to keep the points, the option fUseEmpty must be set
65
66 if (error <= 0) {
67 if (option.fUseEmpty || (option.fErrors1 && std::abs(value) > 0 ) )
68 error = 1.; // set error to 1
69 else
70 return false; // skip bins with zero errors or empty
71 } else if (option.fErrors1)
72 error = 1; // set all error to 1 for non-empty bins
73 return true;
74}
75
76void ExamineRange(const TAxis * axis, std::pair<double,double> range,int &hxfirst,int &hxlast) {
77 // examine the range given with the pair on the given histogram axis
78 // correct in case the bin values hxfirst hxlast
79 double xlow = range.first;
80 double xhigh = range.second;
81#ifdef DEBUG
82 std::cout << "xlow " << xlow << " xhigh = " << xhigh << std::endl;
83#endif
84 // ignore ranges specified outside histogram range
85 int ilow = axis->FindFixBin(xlow);
86 int ihigh = axis->FindFixBin(xhigh);
87 if (ilow > hxlast || ihigh < hxfirst) {
88 Warning("ROOT::Fit::FillData","fit range is outside histogram range, no fit data for %s",axis->GetName());
89 }
90 // consider only range defined with-in histogram not oustide. Always exclude underflow/overflow
91 hxfirst = std::min( std::max( ilow, hxfirst), hxlast+1) ;
92 hxlast = std::max( std::min( ihigh, hxlast), hxfirst-1) ;
93 // exclude bins where range coverage is less than half bin width
94 if (hxfirst < hxlast) {
95 if ( axis->GetBinCenter(hxfirst) < xlow) hxfirst++;
96 if ( axis->GetBinCenter(hxlast) > xhigh) hxlast--;
97 }
98}
99
100
101} // end namespace HFitInterface
102
103
104void FillData(BinData & dv, const TH1 * hfit, TF1 * func)
105{
106 // Function to fill the binned Fit data structure from a TH1
107 // The dimension of the data is the same of the histogram dimension
108 // The function pointer is need in case of integral is used and to reject points
109 // rejected in the function
110
111 // the TF1 pointer cannot be constant since EvalPar and InitArgs are not const methods
112
113 // get fit option
114 const DataOptions & fitOpt = dv.Opt();
115
116
117 // store instead of bin center the bin edges
118 bool useBinEdges = fitOpt.fIntegral || fitOpt.fBinVolume;
119
120 assert(hfit != nullptr);
121
122 //std::cout << "creating Fit Data from histogram " << hfit->GetName() << std::endl;
123
124 int hxfirst = hfit->GetXaxis()->GetFirst();
125 int hxlast = hfit->GetXaxis()->GetLast();
126
127 int hyfirst = hfit->GetYaxis()->GetFirst();
128 int hylast = hfit->GetYaxis()->GetLast();
129
130 int hzfirst = hfit->GetZaxis()->GetFirst();
131 int hzlast = hfit->GetZaxis()->GetLast();
132
133 // function by default has same range (use that one if requested otherwise use data one)
134
135
136 // get the range (add the function range ??)
137 // to check if inclusion/exclusion at end/point
138 const DataRange & range = dv.Range();
139 if (range.Size(0) != 0) {
141 if (range.Size(0) > 1 ) {
142 Warning("ROOT::Fit::FillData","support only one range interval for X coordinate");
143 }
144 }
145
146 if (hfit->GetDimension() > 1 && range.Size(1) != 0) {
148 if (range.Size(1) > 1 )
149 Warning("ROOT::Fit::FillData","support only one range interval for Y coordinate");
150 }
151
152 if (hfit->GetDimension() > 2 && range.Size(2) != 0) {
154 if (range.Size(2) > 1 )
155 Warning("ROOT::Fit::FillData","support only one range interval for Z coordinate");
156 }
157
158
159 int n = (hxlast-hxfirst+1)*(hylast-hyfirst+1)*(hzlast-hzfirst+1);
160
161#ifdef DEBUG
162 std::cout << "THFitInterface: ifirst = " << hxfirst << " ilast = " << hxlast
163 << " total bins " << n
164 << std::endl;
165#endif
166
167 // reserve n for more efficient usage
168 //dv.Data().reserve(n);
169
170 int hdim = hfit->GetDimension();
171 int ndim = hdim;
172 // case of function dimension less than histogram
173 if (func !=nullptr && func->GetNdim() == hdim-1) ndim = hdim-1;
174
175 assert( ndim > 0 );
176 //typedef BinPoint::CoordData CoordData;
177 //CoordData x = CoordData( hfit->GetDimension() );
179
180 double x[3];
181 double s[3];
182
183 int binx = 0;
184 int biny = 0;
185 int binz = 0;
186
187 const TAxis *xaxis = hfit->GetXaxis();
188 const TAxis *yaxis = hfit->GetYaxis();
189 const TAxis *zaxis = hfit->GetZaxis();
190
191 for ( binx = hxfirst; binx <= hxlast; ++binx) {
192 if (useBinEdges) {
193 x[0] = xaxis->GetBinLowEdge(binx);
194 s[0] = xaxis->GetBinUpEdge(binx);
195 }
196 else
197 x[0] = xaxis->GetBinCenter(binx);
198
199
200 for ( biny = hyfirst; biny <= hylast; ++biny) {
201 if (useBinEdges) {
202 x[1] = yaxis->GetBinLowEdge(biny);
203 s[1] = yaxis->GetBinUpEdge(biny);
204 }
205 else
206 x[1] = yaxis->GetBinCenter(biny);
207
208 for ( binz = hzfirst; binz <= hzlast; ++binz) {
209 if (useBinEdges) {
210 x[2] = zaxis->GetBinLowEdge(binz);
211 s[2] = zaxis->GetBinUpEdge(binz);
212 }
213 else
214 x[2] = zaxis->GetBinCenter(binz);
215
216 // need to evaluate function to know about rejected points
217 // hugly but no other solutions
218 if (func != nullptr) {
219 TF1::RejectPoint(false);
220 (*func)( &x[0] ); // evaluate using stored function parameters
221 if (TF1::RejectedPoint() ) continue;
222 }
223
224
225 double value = hfit->GetBinContent(binx, biny, binz);
226 double error = hfit->GetBinError(binx, biny, binz);
227 if (!HFitInterface::AdjustError(fitOpt,error,value) ) continue;
228
229 if (ndim == hdim -1) {
230 // case of fitting a function with dimension -1
231 // point error is bin width y / sqrt(N) where N is the number of entries in the bin
232 // normalization of error will be wrong - but they will be rescaled in the fit
233 if (hdim == 2) dv.Add( x, x[1], yaxis->GetBinWidth(biny) / error );
234 if (hdim == 3) dv.Add( x, x[2], zaxis->GetBinWidth(binz) / error );
235 } else {
236 if (fitOpt.fErrors1)
237 dv.Add( x, value );
238 else
239 dv.Add( x, value, error );
240 if (useBinEdges) {
241 dv.AddBinUpEdge( s );
242 }
243 }
244
245
246#ifdef DEBUG
247 std::cout << "bin " << binx << " add point " << x[0] << " " << hfit->GetBinContent(binx) << std::endl;
248#endif
249
250 } // end loop on z bins
251 } // end loop on y bins
252 } // end loop on x axis
253
254
255#ifdef DEBUG
256 std::cout << "THFitInterface::FillData: Hist FitData size is " << dv.Size() << std::endl;
257#endif
258
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Compute rough values of parameters for an exponential
263
265{
266 unsigned int n = data.Size();
267 if (n == 0) return;
268
269 // find xmin and xmax of the data
270 double valxmin;
271 double xmin = *(data.GetPoint(0,valxmin));
272 double xmax = xmin;
273 double valxmax = valxmin;
274
275 for (unsigned int i = 1; i < n; ++ i) {
276 double val;
277 double x = *(data.GetPoint(i,val) );
278 if (x < xmin) {
279 xmin = x;
280 valxmin = val;
281 }
282 else if (x > xmax) {
283 xmax = x;
284 valxmax = val;
285 }
286 }
287
288 // avoid negative values of valxmin/valxmax
291 else if (valxmin <=0 && valxmax <= 0) { valxmin = 1; valxmax = 1; }
292
293 double slope = std::log( valxmax/valxmin) / (xmax - xmin);
294 double constant = std::log(valxmin) - slope * xmin;
296}
297
298
299////////////////////////////////////////////////////////////////////////////////
300/// Compute Initial values of parameters for a gaussian
301/// derived from function H1InitGaus defined in TH1.cxx
302
304{
305
306 static const double sqrtpi = 2.506628;
307
308 // - Compute mean value and RMS of the data
309 unsigned int n = data.Size();
310 if (n == 0) return;
311 double sumx = 0;
312 double sumx2 = 0;
313 double allcha = 0;
314 double valmax = 0;
315 double rangex = data.Coords(n-1)[0] - data.Coords(0)[0];
316 // to avoid binwidth = 0 set arbitrarly to 1
317 double binwidth = 1;
318 if ( rangex > 0) binwidth = rangex;
319 double x0 = 0;
320 for (unsigned int i = 0; i < n; ++ i) {
321 double val;
322 double x = *(data.GetPoint(i,val) );
323 sumx += val*x;
324 sumx2 += val*x*x;
325 allcha += val;
326 if (val > valmax) valmax = val;
327 if (i > 0) {
328 double dx = x - x0;
329 if (dx < binwidth) binwidth = dx;
330 }
331 x0 = x;
332 }
333
334 if (allcha <= 0) return;
335 double mean = sumx/allcha;
336 double rms = sumx2/allcha - mean*mean;
337
338
339 if (rms > 0)
340 rms = std::sqrt(rms);
341 else
342 rms = binwidth*n/4;
343
344
345 //if the distribution is really gaussian, the best approximation
346 //is binwidx*allcha/(sqrtpi*rms)
347 //However, in case of non-gaussian tails, this underestimates
348 //the normalisation constant. In this case the maximum value
349 //is a better approximation.
350 //We take the average of both quantities
351
352// printf("valmax %f other %f bw %f allcha %f rms %f \n",valmax, binwidth*allcha/(sqrtpi*rms),
353// binwidth, allcha,rms );
354
355 double constant = 0.5*(valmax+ binwidth*allcha/(sqrtpi*rms));
356
357
358 //In case the mean value is outside the histo limits and
359 //the RMS is bigger than the range, we take
360 // mean = center of bins
361 // rms = half range
362// Double_t xmin = curHist->GetXaxis()->GetXmin();
363// Double_t xmax = curHist->GetXaxis()->GetXmax();
364// if ((mean < xmin || mean > xmax) && rms > (xmax-xmin)) {
365// mean = 0.5*(xmax+xmin);
366// rms = 0.5*(xmax-xmin);
367// }
368
370 f1->SetParameter(1,mean);
371 f1->SetParameter(2,rms);
372 f1->SetParLimits(2,0,10*rms);
373
374
375#ifdef DEBUG
376 std::cout << "Gaussian initial par values" << constant << " " << mean << " " << rms << std::endl;
377#endif
378
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Compute Initial values of parameters for a gaussian
383/// derived from function H1InitGaus defined in TH1.cxx
384
386{
387
388 static const double sqrtpi = 2.506628;
389
390 // - Compute mean value and RMS of the data
391 unsigned int n = data.Size();
392 if (n == 0) return;
393 double sumx = 0, sumy = 0;
394 double sumx2 = 0, sumy2 = 0;
395 double allcha = 0;
396 double valmax = 0;
397 double rangex = data.Coords(n-1)[0] - data.Coords(0)[0];
398 double rangey = data.Coords(n-1)[1] - data.Coords(0)[1];
399 // to avoid binwidthx = 0 set arbitrarly to 1
400 double binwidthx = 1, binwidthy = 1;
401 if ( rangex > 0) binwidthx = rangex;
402 if ( rangey > 0) binwidthy = rangey;
403 double x0 = 0, y0 = 0;
404 for (unsigned int i = 0; i < n; ++i) {
405 double val;
406 const double *coords = data.GetPoint(i,val);
407 double x = coords[0], y = coords[1];
408 sumx += val*x;
409 sumy += val*y;
410 sumx2 += val*x*x;
411 sumy2 += val*y*y;
412 allcha += val;
413 if (val > valmax) valmax = val;
414 if (i > 0) {
415 double dx = x - x0;
416 if (dx < binwidthx) binwidthx = dx;
417 double dy = y - y0;
418 if (dy < binwidthy) binwidthy = dy;
419 }
420 x0 = x;
421 y0 = y;
422 }
423
424 if (allcha <= 0) return;
425 double meanx = sumx/allcha, meany = sumy/allcha;
426 double rmsx = sumx2/allcha - meanx*meanx;
427 double rmsy = sumy2/allcha - meany*meany;
428
429
430 if (rmsx > 0)
431 rmsx = std::sqrt(rmsx);
432 else
433 rmsx = binwidthx*n/4;
434
435 if (rmsy > 0)
436 rmsy = std::sqrt(rmsy);
437 else
438 rmsy = binwidthy*n/4;
439
440
441 //if the distribution is really gaussian, the best approximation
442 //is binwidx*allcha/(sqrtpi*rmsx)
443 //However, in case of non-gaussian tails, this underestimates
444 //the normalisation constant. In this case the maximum value
445 //is a better approximation.
446 //We take the average of both quantities
447
448 double constant = 0.5 * (valmax+ binwidthx*allcha/(sqrtpi*rmsx))*
450
453 f1->SetParameter(2,rmsx);
454 f1->SetParLimits(2,0,10*rmsx);
456 f1->SetParameter(4,rmsy);
457 f1->SetParLimits(4,0,10*rmsy);
458
459#ifdef DEBUG
460 std::cout << "2D Gaussian initial par values"
461 << constant << " "
462 << meanx << " "
463 << rmsx
464 << meany << " "
465 << rmsy
466 << std::endl;
467#endif
468
469}
470
471// filling fit data from TGraph objects
472
474 // get type of data for TGraph objects
475 double *ex = gr->GetEX();
476 double *ey = gr->GetEY();
477 double * eyl = gr->GetEYlow();
478 double * eyh = gr->GetEYhigh();
479
480
481 // default case for graphs (when they have errors)
483 // if all errors are zero set option of using errors to 1
484 if (fitOpt.fErrors1 || ( ey == nullptr && ( eyl == nullptr || eyh == nullptr ) ) ) {
486 }
487 // need to treat case when all errors are zero
488 // note that by default fitOpt.fCoordError is true
489 else if ( ex != nullptr && fitOpt.fCoordErrors) {
490 // check that all errors are not zero
491 int i = 0;
492 while (i < gr->GetN() && type != BinData::kCoordError) {
493 if (ex[i] > 0) type = BinData::kCoordError;
494 ++i;
495 }
496 }
497 // case of asymmetric errors (by default fAsymErrors is true)
498 else if ( ( eyl != nullptr && eyh != nullptr) && fitOpt.fAsymErrors) {
499 // check also if that all errors are non zero's
500 int i = 0;
501 bool zeroErrorX = true;
502 bool zeroErrorY = true;
503 while (i < gr->GetN() && (zeroErrorX || zeroErrorY)) {
504 double e2X = ( gr->GetErrorXlow(i) + gr->GetErrorXhigh(i) );
505 double e2Y = eyl[i] + eyh[i];
506 zeroErrorX &= (e2X <= 0);
507 zeroErrorY &= (e2Y <= 0);
508 ++i;
509 }
510 if (zeroErrorX && zeroErrorY)
512 else if (!zeroErrorX && zeroErrorY)
514 else if (zeroErrorX && !zeroErrorY) {
516 fitOpt.fCoordErrors = false;
517 }
518 else {
520 }
521 }
522
523 // need to look also a case when all errors in y are zero
524 if ( ey != nullptr && type != BinData::kCoordError ) {
525 int i = 0;
526 bool zeroError = true;
527 while (i < gr->GetN() && zeroError) {
528 if (ey[i] > 0) zeroError = false;
529 ++i;
530 }
532 }
533
534
535#ifdef DEBUG
536 std::cout << "type is " << type << " graph type is " << gr->IsA()->GetName() << std::endl;
537#endif
538
539 return type;
540}
541
543 // get type of data for TGraph2D object
544 double *ex = gr->GetEX();
545 double *ey = gr->GetEY();
546 double *ez = gr->GetEZ();
547
548 // default case for graphs (when they have errors)
550 // if all errors are zero set option of using errors to 1
551 if (fitOpt.fErrors1 || ez == nullptr ) {
553 }
554 else if ( ex != nullptr && ey!=nullptr && fitOpt.fCoordErrors) {
555 // check that all errors are not zero
556 int i = 0;
557 while (i < gr->GetN() && type != BinData::kCoordError) {
558 if (ex[i] > 0 || ey[i] > 0) type = BinData::kCoordError;
559 ++i;
560 }
561 }
562
563
564#ifdef DEBUG
565 std::cout << "type is " << type << " graph2D type is " << gr->IsA()->GetName() << std::endl;
566#endif
567
568 return type;
569}
570
571
572
573void DoFillData ( BinData & dv, const TGraph * gr, BinData::ErrorType type, TF1 * func ) {
574 // internal method to do the actual filling of the data
575 // given a graph and a multigraph
576
577 // get fit option
578 DataOptions & fitOpt = dv.Opt();
579
580 int nPoints = gr->GetN();
581 double *gx = gr->GetX();
582 double *gy = gr->GetY();
583
584 const DataRange & range = dv.Range();
585 bool useRange = ( range.Size(0) > 0);
586 double xmin = 0;
587 double xmax = 0;
588 range.GetRange(xmin,xmax);
589
590 dv.Initialize(nPoints,1, type);
591
592#ifdef DEBUG
593 std::cout << "DoFillData: graph npoints = " << nPoints << " type " << type << std::endl;
594 if (func) {
595 double a1,a2; func->GetRange(a1,a2); std::cout << "func range " << a1 << " " << a2 << std::endl;
596 }
597#endif
598
599 // Unlike histograms, graphs may have array of points created in
600 // non-ascending order along the X axis. This breaks fitting algorithm. We
601 // create a "remap" for the TGraph point indexes that provides ascending
602 // order of X values for the BinData
603 std::vector<std::pair<double, int>> indexRemap;
604 for (int i = 0; i < nPoints; ++i) {
605 indexRemap.emplace_back(gx[i], i);
606 }
607 std::sort(indexRemap.begin(), indexRemap.end());
608
609 double x[1];
610 for (int j = 0; j < nPoints; ++j) {
611
612 int i = indexRemap[j].second;
613 x[0] = gx[i];
614
615
616 if (useRange && ( x[0] < xmin || x[0] > xmax) ) continue;
617
618 // need to evaluate function to know about rejected points
619 // hugly but no other solutions
620 if (func) {
621 TF1::RejectPoint(false);
622 (*func)( x ); // evaluate using stored function parameters
623 if (TF1::RejectedPoint() ) continue;
624 }
625
626
627 if (fitOpt.fErrors1)
628 dv.Add( gx[i], gy[i] );
629
630 // for the errors use the getters by index to avoid cases when the arrays are zero
631 // (like in a case of a graph)
632 else if (type == BinData::kValueError) {
633 double errorY = gr->GetErrorY(i);
634 // should consider error = 0 as 1 ? Decide to skip points with zero errors
635 // in case want to keep points with error = 0 as errrors=1 need to set the option UseEmpty
637 dv.Add( gx[i], gy[i], errorY );
638
639#ifdef DEBUG
640 std::cout << "Point " << i << " " << gx[i] << " " << gy[i] << " " << errorY << std::endl;
641#endif
642
643
644 }
645 else { // case use error in x or asym errors
646 double errorX = 0;
647 if (fitOpt.fCoordErrors)
648 // shoulkd take combined average (sqrt(0.5(e1^2+e2^2)) or math average ?
649 // gr->GetErrorX(i) returns combined average
650 // use math average for same behaviour as before
651 errorX = std::max( 0.5 * ( gr->GetErrorXlow(i) + gr->GetErrorXhigh(i) ) , 0. ) ;
652
653
654 // adjust error in y according to option
655 double errorY = std::max(gr->GetErrorY(i), 0.);
656 // we do not check the return value since we check later if error in X and Y is zero for skipping the point
658
659 // skip points with total error = 0
660 if ( errorX <=0 && errorY <= 0 ) continue;
661
662
663 if (type == BinData::kAsymError) {
664 // asymmetric errors
665 dv.Add( gx[i], gy[i], errorX, gr->GetErrorYlow(i), gr->GetErrorYhigh(i) );
666 }
667 else {
668 // case symmetric Y errors
669 dv.Add( gx[i], gy[i], errorX, errorY );
670 }
671 }
672
673 }
674
675#ifdef DEBUG
676 std::cout << "TGraphFitInterface::FillData Graph FitData size is " << dv.Size() << std::endl;
677#endif
678
679}
680
681void FillData(SparseData & dv, const TH1 * h1, TF1 * /*func*/)
682{
683 const int dim = h1->GetDimension();
684 std::vector<double> min(dim);
685 std::vector<double> max(dim);
686
687 int ncells = h1->GetNcells();
688 for ( int i = 0; i < ncells; ++i ) {
689// printf("i: %d; OF: %d; UF: %d; C: %f\n"
690// , i
691// , h1->IsBinOverflow(i) , h1->IsBinUnderflow(i)
692// , h1->GetBinContent(i));
693 if ( !( h1->IsBinOverflow(i) || h1->IsBinUnderflow(i) )
694 && h1->GetBinContent(i))
695 {
696 int x,y,z;
697 h1->GetBinXYZ(i, x, y, z);
698
699// std::cout << "FILLDATA: h1(" << i << ")"
700// << "[" << h1->GetXaxis()->GetBinLowEdge(x) << "-" << h1->GetXaxis()->GetBinUpEdge(x) << "]";
701// if ( dim >= 2 )
702// std::cout << "[" << h1->GetYaxis()->GetBinLowEdge(y) << "-" << h1->GetYaxis()->GetBinUpEdge(y) << "]";
703// if ( dim >= 3 )
704// std::cout << "[" << h1->GetZaxis()->GetBinLowEdge(z) << "-" << h1->GetZaxis()->GetBinUpEdge(z) << "]";
705
706// std::cout << h1->GetBinContent(i) << std::endl;
707
708 min[0] = h1->GetXaxis()->GetBinLowEdge(x);
709 max[0] = h1->GetXaxis()->GetBinUpEdge(x);
710 if ( dim >= 2 )
711 {
712 min[1] = h1->GetYaxis()->GetBinLowEdge(y);
713 max[1] = h1->GetYaxis()->GetBinUpEdge(y);
714 }
715 if ( dim >= 3 ) {
716 min[2] = h1->GetZaxis()->GetBinLowEdge(z);
717 max[2] = h1->GetZaxis()->GetBinUpEdge(z);
718 }
719
720 dv.Add(min, max, h1->GetBinContent(i), h1->GetBinError(i));
721 }
722 }
723}
724
725void FillData(SparseData & dv, const THnBase * h1, TF1 * /*func*/)
726{
727 const int dim = h1->GetNdimensions();
728 std::vector<double> min(dim);
729 std::vector<double> max(dim);
730 std::vector<Int_t> coord(dim);
731
732 ULong64_t nEntries = h1->GetNbins();
733 for ( ULong64_t i = 0; i < nEntries; i++ )
734 {
735 double value = h1->GetBinContent( i, &coord[0] );
736 if ( !value ) continue;
737
738// std::cout << "FILLDATA(SparseData): h1(" << i << ")";
739
740 // Exclude underflows and overflows! (defect behaviour with the TH1*)
741 bool insertBox = true;
742 for ( int j = 0; j < dim && insertBox; ++j )
743 {
744 TAxis* axis = h1->GetAxis(j);
745 if ( ( axis->GetBinLowEdge(coord[j]) < axis->GetXmin() ) ||
746 ( axis->GetBinUpEdge(coord[j]) > axis->GetXmax() ) ) {
747 insertBox = false;
748 }
749 min[j] = h1->GetAxis(j)->GetBinLowEdge(coord[j]);
750 max[j] = h1->GetAxis(j)->GetBinUpEdge(coord[j]);
751 }
752 if ( !insertBox ) {
753// std::cout << "NOT INSERTED!"<< std::endl;
754 continue;
755 }
756
757// for ( int j = 0; j < dim; ++j )
758// {
759// std::cout << "[" << h1->GetAxis(j)->GetBinLowEdge(coord[j])
760// << "-" << h1->GetAxis(j)->GetBinUpEdge(coord[j]) << "]";
761// }
762// std::cout << h1->GetBinContent(i) << std::endl;
763
764 dv.Add(min, max, value, h1->GetBinError(i));
765 }
766}
767
768void FillData(BinData & dv, const THnBase * s1, TF1 * func)
769{
770 // Fill the Range of the THnBase
771 unsigned int const ndim = s1->GetNdimensions();
772 std::vector<double> xmin(ndim);
773 std::vector<double> xmax(ndim);
774 for ( unsigned int i = 0; i < ndim; ++i ) {
775 TAxis* axis = s1->GetAxis(i);
776 xmin[i] = axis->GetXmin();
777 xmax[i] = axis->GetXmax();
778 }
779
780 // Put default options, needed for the likelihood fitting of sparse
781 // data.
783 //dopt.fUseEmpty = true;
784 // when using sparse data need to set option to use normalized bin volume, because sparse bins are merged together
785 //if (!dopt.fIntegral) dopt.fBinVolume = true;
786 dopt.fBinVolume = true;
787 dopt.fNormBinVolume = true;
788
789 // Get the sparse data
790 ROOT::Fit::SparseData d(ndim, &xmin[0], &xmax[0]);
791 ROOT::Fit::FillData(d, s1, func);
792
793// std::cout << "FillData(BinData & dv, const THnBase * s1, TF1 * func) (1)" << std::endl;
794
795 // Create the bin data from the sparse data
796 d.GetBinDataIntegral(dv);
797
798}
799
800void FillData ( BinData & dv, const TGraph * gr, TF1 * func ) {
801 // fill the data vector from a TGraph. Pass also the TF1 function which is
802 // needed in case to exclude points rejected by the function
803 assert(gr != nullptr);
804
805 // get fit option
806 DataOptions & fitOpt = dv.Opt();
807
809 // adjust option according to type
810 fitOpt.fErrors1 = (type == BinData::kNoError);
811 // set this if we want to have error=1 for points with zero errors (by default they are skipped)
812 // fitOpt.fUseEmpty = true;
813
814 // use coordinate or asym errors in case option is set and type is consistent
815 fitOpt.fCoordErrors &= (type == BinData::kCoordError) || (type == BinData::kAsymError) ;
816 fitOpt.fAsymErrors &= (type == BinData::kAsymError);
817
818
819 // if data are filled already check if there are consistent - otherwise do nothing
820 if (dv.Size() > 0 && dv.NDim() == 1 ) {
821 // check if size is correct otherwise flag an errors
822 if ( dv.GetErrorType() != type ) {
823 Error("FillData","Inconsistent TGraph with previous data set- skip all graph data");
824 return;
825 }
826 }
827
828 DoFillData(dv, gr, type, func);
829
830}
831
832void FillData ( BinData & dv, const TMultiGraph * mg, TF1 * func ) {
833 // fill the data vector from a TMultiGraph. Pass also the TF1 function which is
834 // needed in case to exclude points rejected by the function
835 assert(mg != nullptr);
836
837 TList * grList = mg->GetListOfGraphs();
838 assert(grList != nullptr);
839
840#ifdef DEBUG
841// grList->Print();
843 TObject *obj;
844 std::cout << "multi-graph list of graps: " << std::endl;
845 while ((obj = itr())) {
846 std::cout << obj->IsA()->GetName() << std::endl;
847 }
848
849#endif
850
851 // get fit option
852 DataOptions & fitOpt = dv.Opt();
853
854 // loop on the graphs to get the data type (use maximum)
855 TIter next(grList);
856
858 TGraph *gr = nullptr;
859 while ((gr = (TGraph*) next())) {
861 if (t > type ) type = t;
862 }
863 // adjust option according to type
864 fitOpt.fErrors1 = (type == BinData::kNoError);
865 // use coordinate or asym errors in case option is set and type is consistent
866 fitOpt.fCoordErrors &= (type == BinData::kCoordError) || (type == BinData::kAsymError);
867 fitOpt.fAsymErrors &= (type == BinData::kAsymError);
868
869
870#ifdef DEBUG
871 std::cout << "Fitting MultiGraph of type " << type << std::endl;
872#endif
873
874 // fill the data now
875 next = grList;
876 while ((gr = (TGraph*) next())) {
877 DoFillData( dv, gr, type, func);
878 }
879
880#ifdef DEBUG
881 std::cout << "TGraphFitInterface::FillData MultiGraph FitData size is " << dv.Size() << std::endl;
882#endif
883
884}
885
886void FillData ( BinData & dv, const TGraph2D * gr, TF1 * func ) {
887 // fill the data vector from a TGraph2D. Pass also the TF1 function which is
888 // needed in case to exclude points rejected by the function
889 // in case of a pure TGraph
890 assert(gr != nullptr);
891
892 // get fit option
893 DataOptions & fitOpt = dv.Opt();
895 // adjust option according to type
896 fitOpt.fErrors1 = (type == BinData::kNoError);
897 fitOpt.fCoordErrors = (type == BinData::kCoordError);
898 fitOpt.fAsymErrors = false; // a TGraph2D with asymmetric errors does not exist
899
900 int nPoints = gr->GetN();
901 double *gx = gr->GetX();
902 double *gy = gr->GetY();
903 double *gz = gr->GetZ();
904
905 // if all errors are zero set option of using errors to 1
906 if ( gr->GetEZ() == nullptr) fitOpt.fErrors1 = true;
907
908 double x[2];
909 double ex[2];
910
911 // look at data range
912 const DataRange & range = dv.Range();
913 bool useRangeX = ( range.Size(0) > 0);
914 bool useRangeY = ( range.Size(1) > 0);
915 double xmin = 0;
916 double xmax = 0;
917 double ymin = 0;
918 double ymax = 0;
919 range.GetRange(xmin,xmax,ymin,ymax);
920
921 dv.Initialize(nPoints,2, type);
922
923 for ( int i = 0; i < nPoints; ++i) {
924
925 x[0] = gx[i];
926 x[1] = gy[i];
927
928 //if (fitOpt.fUseRange && HFitInterface::IsPointOutOfRange(func, x) ) continue;
929 if (useRangeX && ( x[0] < xmin || x[0] > xmax) ) continue;
930 if (useRangeY && ( x[1] < ymin || x[1] > ymax) ) continue;
931
932 // need to evaluate function to know about rejected points
933 // hugly but no other solutions
934 if (func) {
935 TF1::RejectPoint(false);
936 (*func)( x ); // evaluate using stored function parameters
937 if (TF1::RejectedPoint() ) continue;
938 }
939
940 if (type == BinData::kNoError) {
941 dv.Add( x, gz[i] );
942 continue;
943 }
944
945 double errorZ = gr->GetErrorZ(i);
947
948 if (type == BinData::kValueError) {
949 dv.Add( x, gz[i], errorZ );
950 }
951 else if (type == BinData::kCoordError) { // case use error in coordinates (x and y)
952 ex[0] = std::max(gr->GetErrorX(i), 0.);
953 ex[1] = std::max(gr->GetErrorY(i), 0.);
954 dv.Add( x, gz[i], ex, errorZ );
955 }
956 else
957 assert(0); // should not go here
958
959#ifdef DEBUG
960 std::cout << "Point " << i << " " << gx[i] << " " << gy[i] << " " << errorZ << std::endl;
961#endif
962
963 }
964
965#ifdef DEBUG
966 std::cout << "THFitInterface::FillData Graph2D FitData size is " << dv.Size() << std::endl;
967#endif
968
969}
970
971
972// confidence intervals
974 if (h1->GetDimension() != 1) {
975 Error("GetConfidenceIntervals","Invalid object used for storing confidence intervals");
976 return false;
977 }
978 // fill fit data sets with points to estimate cl.
979 BinData d;
980 FillData(d,h1,nullptr);
981 gr->Set(d.NPoints() );
982 double * ci = gr->GetEY(); // make CL values error of the graph
983 result.GetConfidenceIntervals(d,ci,cl);
984 // put function value as abscissa of the graph
985 for (unsigned int ipoint = 0; ipoint < d.NPoints(); ++ipoint) {
986 const double * x = d.Coords(ipoint);
987 const ROOT::Math::IParamMultiFunction * func = result.FittedFunction();
988 gr->SetPoint(ipoint, x[0], (*func)(x) );
989 }
990 return true;
991}
992
993} // end namespace Fit
994
995} // end namespace ROOT
#define d(i)
Definition RSha256.hxx:102
#define s1(x)
Definition RSha256.hxx:91
const Bool_t kIterBackward
Definition TCollection.h:43
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
float xmin
float ymin
float xmax
float ymax
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition DataRange.h:35
class containing the result of the fit and all the related information (fitted parameter values,...
Definition FitResult.h:47
SparseData class representing the data of a THNSparse histogram The data needs to be converted to a B...
Definition SparseData.h:35
const_iterator begin() const
const_iterator end() const
Class to manage histogram axis.
Definition TAxis.h:32
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:473
Double_t GetXmax() const
Definition TAxis.h:142
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:513
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:414
Double_t GetXmin() const
Definition TAxis.h:141
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:523
1-Dim function class
Definition TF1.h:233
static void RejectPoint(Bool_t reject=kTRUE)
Static function to set the global flag to reject points the fgRejectPoint global flag is tested by al...
Definition TF1.cxx:3684
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
Definition TF1.cxx:2281
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set lower and upper limits for parameter ipar.
Definition TF1.cxx:3508
static Bool_t RejectedPoint()
See TF1::RejectPoint above.
Definition TF1.cxx:3693
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:667
virtual Bool_t IsInside(const Double_t *x) const
return kTRUE if the point is inside the function range
Definition TF1.h:626
virtual Int_t GetNdim() const
Definition TF1.h:513
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
A TGraphErrors is a TGraph with error bars.
Double_t GetErrorY(Int_t bin) const override
It returns the error along Y at point i.
Double_t * GetEX() const override
Double_t GetErrorX(Int_t bin) const override
It returns the error along X at point i.
Double_t * GetEY() const override
Double_t GetErrorXhigh(Int_t bin) const override
It returns the error along X at point i.
Double_t GetErrorYlow(Int_t bin) const override
It returns the error along Y at point i.
Double_t GetErrorYhigh(Int_t bin) const override
It returns the error along Y at point i.
TClass * IsA() const override
Double_t GetErrorXlow(Int_t bin) const override
It returns the error along X at point i.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2347
Double_t * GetY() const
Definition TGraph.h:140
virtual Double_t * GetEYlow() const
Definition TGraph.h:146
Int_t GetN() const
Definition TGraph.h:132
Double_t * GetX() const
Definition TGraph.h:139
virtual Double_t * GetEYhigh() const
Definition TGraph.h:145
virtual void Set(Int_t n)
Set number of points in the graph Existing coordinates are preserved New coordinates above fNpoints a...
Definition TGraph.cxx:2282
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
TAxis * GetZaxis()
Definition TH1.h:342
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition TH1.cxx:9065
virtual Int_t GetDimension() const
Definition TH1.h:299
TAxis * GetXaxis()
Definition TH1.h:340
virtual void GetBinXYZ(Int_t binglobal, Int_t &binx, Int_t &biny, Int_t &binz) const
Return binx, biny, binz corresponding to the global bin number globalbin see TH1::GetBin function abo...
Definition TH1.cxx:4972
virtual Int_t GetNcells() const
Definition TH1.h:316
TAxis * GetYaxis()
Definition TH1.h:341
Bool_t IsBinUnderflow(Int_t bin, Int_t axis=0) const
Return true if the bin is underflow.
Definition TH1.cxx:5212
Bool_t IsBinOverflow(Int_t bin, Int_t axis=0) const
Return true if the bin is overflow.
Definition TH1.cxx:5180
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9154
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5059
Multidimensional histogram base.
Definition THnBase.h:45
A doubly linked list.
Definition TList.h:38
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:34
TList * GetListOfGraphs() const
Definition TMultiGraph.h:68
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual TClass * IsA() const
Definition TObject.h:243
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t ey[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
Double_t ex[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition HFitImpl.cxx:133
void ExamineRange(const TAxis *axis, std::pair< double, double > range, int &hxfirst, int &hxlast)
bool AdjustError(const DataOptions &option, double &error, double value=1)
bool IsPointOutOfRange(const TF1 *func, const double *x)
void Init2DGaus(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for 2D gaussian function given the fit data Set the sigma limits for zero t...
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.
void InitExpo(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for an exponential function given the fit data Set the constant and slope a...
void InitGaus(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for gaussian function given the fit data Set the sigma limits for zero top ...
void DoFillData(BinData &dv, const TGraph *gr, BinData::ErrorType type, TF1 *func)
BinData::ErrorType GetDataType(const TGraph *gr, DataOptions &fitOpt)
bool GetConfidenceIntervals(const TH1 *h1, const ROOT::Fit::FitResult &r, TGraphErrors *gr, double cl=0.95)
compute confidence intervals at level cl for a fitted histogram h1 in a TGraphErrors gr
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
DataOptions : simple structure holding the options on how the data are filled.
Definition DataOptions.h:28