49namespace HFitInterface {
54 if (func ==
nullptr)
return false;
72 }
else if (
option.fErrors1)
77void ExamineRange(
const TAxis * axis, std::pair<double,double> range,
int &hxfirst,
int &hxlast) {
80 double xlow = range.first;
81 double xhigh = range.second;
83 std::cout <<
"xlow " << xlow <<
" xhigh = " << xhigh << std::endl;
88 if (ilow > hxlast || ihigh < hxfirst) {
89 Warning(
"ROOT::Fit::FillData",
"fit range is outside histogram range, no fit data for %s",axis->
GetName());
92 hxfirst = std::min( std::max( ilow, hxfirst), hxlast+1) ;
93 hxlast = std::max( std::min( ihigh, hxlast), hxfirst-1) ;
95 if (hxfirst < hxlast) {
121 assert(hfit !=
nullptr);
135 auto selectedBins = [&range](
const TAxis *axis,
unsigned int icoord,
bool useRange) {
136 std::vector<int> bins;
137 const unsigned int nRanges = useRange ? range.
Size(icoord) : 0;
138 for (
unsigned int i = 0; i < std::max(nRanges, 1u); ++i) {
148 first = std::max(first, bins.back() + 1);
149 for (
int bin = first; bin <= last; ++bin)
154 const std::vector<int> xbins = selectedBins(hfit->
GetXaxis(), 0,
true);
158 int n =
static_cast<int>(xbins.size() * ybins.size() * zbins.size());
161 std::cout <<
"THFitInterface: bins in x = " << xbins.size()
162 <<
" total bins " <<
n
172 if (func !=
nullptr && func->
GetNdim() == hdim-1) ndim = hdim-1;
186 for (
int binx : xbins) {
195 for (
int biny : ybins) {
203 for (
int binz : zbins) {
213 if (func !=
nullptr) {
221 double error = hfit->
GetBinError(binx, biny, binz);
224 if (ndim == hdim -1) {
242 std::cout <<
"bin " << binx <<
" add point " <<
x[0] <<
" " << hfit->
GetBinContent(binx) << std::endl;
251 std::cout <<
"THFitInterface::FillData: Hist FitData size is " << dv.
Size() << std::endl;
261 unsigned int n =
data.Size();
266 double xmin = *(
data.GetPoint(0,valxmin));
268 double valxmax = valxmin;
270 for (
unsigned int i = 1; i <
n; ++ i) {
272 double x = *(
data.GetPoint(i,val) );
284 if (valxmin <= 0 && valxmax > 0 ) valxmin = valxmax;
285 else if (valxmax <=0 && valxmin > 0) valxmax = valxmin;
286 else if (valxmin <=0 && valxmax <= 0) { valxmin = 1; valxmax = 1; }
288 double slope = std::log( valxmax/valxmin) / (
xmax -
xmin);
289 double constant = std::log(valxmin) - slope *
xmin;
305 unsigned int n =
data.Size();
309 double sumX = 0, sumY = 0, sumXSq = 0, sumXY = 0;
310 for (
unsigned int i = 0; i <
n; ++i) {
312 double x = *(
data.GetPoint(i, val));
320 double det =
n * sumXSq - sumX * sumX;
324 double slope = (
n * sumXY - sumX * sumY) / det;
335 static const double sqrtpi = 2.506628;
338 unsigned int n =
data.Size();
344 double rangex =
data.Coords(
n-1)[0] -
data.Coords(0)[0];
347 if ( rangex > 0) binwidth = rangex;
349 for (
unsigned int i = 0; i <
n; ++ i) {
351 double x = *(
data.GetPoint(i,val) );
355 if (val > valmax) valmax = val;
358 if (dx < binwidth) binwidth = dx;
363 if (allcha <= 0)
return;
364 double mean = sumx/allcha;
365 double rms = sumx2/allcha - mean*mean;
369 rms = std::sqrt(rms);
384 double constant = 0.5*(valmax+ binwidth*allcha/(sqrtpi*rms));
405 std::cout <<
"Gaussian initial par values" << constant <<
" " << mean <<
" " << rms << std::endl;
417 static const double sqrtpi = 2.506628;
420 unsigned int n =
data.Size();
422 double sumx = 0, sumy = 0;
423 double sumx2 = 0, sumy2 = 0;
426 double rangex =
data.Coords(
n-1)[0] -
data.Coords(0)[0];
427 double rangey =
data.Coords(
n-1)[1] -
data.Coords(0)[1];
429 double binwidthx = 1, binwidthy = 1;
430 if ( rangex > 0) binwidthx = rangex;
431 if ( rangey > 0) binwidthy = rangey;
432 double x0 = 0, y0 = 0;
433 for (
unsigned int i = 0; i <
n; ++i) {
435 const double *coords =
data.GetPoint(i,val);
436 double x = coords[0],
y = coords[1];
442 if (val > valmax) valmax = val;
445 if (dx < binwidthx) binwidthx = dx;
447 if (dy < binwidthy) binwidthy = dy;
453 if (allcha <= 0)
return;
454 double meanx = sumx/allcha, meany = sumy/allcha;
455 double rmsx = sumx2/allcha - meanx*meanx;
456 double rmsy = sumy2/allcha - meany*meany;
460 rmsx = std::sqrt(rmsx);
462 rmsx = binwidthx*
n/4;
465 rmsy = std::sqrt(rmsy);
467 rmsy = binwidthy*
n/4;
477 double constant = 0.5 * (valmax+ binwidthx*allcha/(sqrtpi*rmsx))*
478 (valmax+ binwidthy*allcha/(sqrtpi*rmsy));
489 std::cout <<
"2D Gaussian initial par values"
513 if (fitOpt.
fErrors1 || (
ey ==
nullptr && ( eyl ==
nullptr || eyh ==
nullptr ) ) ) {
527 else if ( ( eyl !=
nullptr && eyh !=
nullptr) && fitOpt.
fAsymErrors) {
530 bool zeroErrorX =
true;
531 bool zeroErrorY =
true;
532 while (i < gr->GetN() && (zeroErrorX || zeroErrorY)) {
534 double e2Y = eyl[i] + eyh[i];
535 zeroErrorX &= (e2X <= 0);
536 zeroErrorY &= (e2Y <= 0);
539 if (zeroErrorX && zeroErrorY)
541 else if (!zeroErrorX && zeroErrorY)
543 else if (zeroErrorX && !zeroErrorY) {
555 bool zeroError =
true;
556 while (i < gr->GetN() && zeroError) {
557 if (
ey[i] > 0) zeroError =
false;
565 std::cout <<
"type is " <<
type <<
" graph type is " <<
gr->
IsA()->
GetName() << std::endl;
575 double *ez =
gr->GetEZ();
580 if (fitOpt.
fErrors1 || ez ==
nullptr ) {
594 std::cout <<
"type is " <<
type <<
" graph2D type is " <<
gr->
IsA()->
GetName() << std::endl;
614 bool useRange = ( range.
Size(0) > 0);
622 std::cout <<
"DoFillData: graph npoints = " << nPoints <<
" type " <<
type << std::endl;
624 double a1,a2; func->
GetRange(a1,a2); std::cout <<
"func range " << a1 <<
" " << a2 << std::endl;
632 std::vector<std::pair<double, int>> indexRemap;
633 for (
int i = 0; i < nPoints; ++i) {
634 indexRemap.emplace_back(gx[i], i);
636 std::sort(indexRemap.begin(), indexRemap.end());
639 for (
int j = 0; j < nPoints; ++j) {
641 int i = indexRemap[j].second;
645 if (useRange && (
x[0] <
xmin ||
x[0] >
xmax) )
continue;
657 dv.
Add( gx[i], gy[i] );
666 dv.
Add( gx[i], gy[i], errorY );
669 std::cout <<
"Point " << i <<
" " << gx[i] <<
" " << gy[i] <<
" " << errorY << std::endl;
689 if ( errorX <=0 && errorY <= 0 )
continue;
698 dv.
Add( gx[i], gy[i], errorX, errorY );
705 std::cout <<
"TGraphFitInterface::FillData Graph FitData size is " << dv.
Size() << std::endl;
713 std::vector<double> min(dim);
714 std::vector<double> max(dim);
717 for (
int i = 0; i < ncells; ++i ) {
756 const int dim =
h1->GetNdimensions();
757 std::vector<double> min(dim);
758 std::vector<double> max(dim);
759 std::vector<Int_t> coord(dim);
762 for (
ULong64_t i = 0; i < nEntries; i++ )
765 if ( !
value )
continue;
770 bool insertBox =
true;
771 for (
int j = 0; j < dim && insertBox; ++j )
779 max[j] =
h1->GetAxis(j)->GetBinUpEdge(coord[j]);
800 unsigned int const ndim =
s1->GetNdimensions();
801 std::vector<double>
xmin(ndim);
802 std::vector<double>
xmax(ndim);
803 for (
unsigned int i = 0; i < ndim; ++i ) {
825 d.GetBinDataIntegral(dv);
832 assert(
gr !=
nullptr);
849 if (dv.
Size() > 0 && dv.
NDim() == 1 ) {
852 Error(
"FillData",
"Inconsistent TGraph with previous data set- skip all graph data");
864 assert(mg !=
nullptr);
867 assert(grList !=
nullptr);
873 std::cout <<
"multi-graph list of graps: " << std::endl;
874 while ((obj = itr())) {
875 std::cout << obj->
IsA()->
GetName() << std::endl;
900 std::cout <<
"Fitting MultiGraph of type " <<
type << std::endl;
910 std::cout <<
"TGraphFitInterface::FillData MultiGraph FitData size is " << dv.
Size() << std::endl;
919 assert(
gr !=
nullptr);
932 double *gz =
gr->GetZ();
935 if (
gr->GetEZ() ==
nullptr) fitOpt.
fErrors1 =
true;
942 bool useRangeX = ( range.
Size(0) > 0);
943 bool useRangeY = ( range.
Size(1) > 0);
952 for (
int i = 0; i < nPoints; ++i) {
958 if (useRangeX && (
x[0] <
xmin ||
x[0] >
xmax) )
continue;
959 if (useRangeY && (
x[1] <
ymin ||
x[1] >
ymax) )
continue;
974 double errorZ =
gr->GetErrorZ(i);
978 dv.
Add(
x, gz[i], errorZ );
983 dv.
Add(
x, gz[i],
ex, errorZ );
989 std::cout <<
"Point " << i <<
" " << gx[i] <<
" " << gy[i] <<
" " << errorZ << std::endl;
995 std::cout <<
"THFitInterface::FillData Graph2D FitData size is " << dv.
Size() << std::endl;
1004 Error(
"GetConfidenceIntervals",
"Invalid object used for storing confidence intervals");
1012 result.GetConfidenceIntervals(
d,ci,cl);
1014 for (
unsigned int ipoint = 0; ipoint <
d.NPoints(); ++ipoint) {
1015 const double *
x =
d.Coords(ipoint);
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
const Bool_t kIterBackward
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
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
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
void AddBinUpEdge(const double *xup)
add the bin width data, a pointer to an array with the bin upper edge information.
ErrorType GetErrorType() const
retrieve the errortype
void Add(double x, double y)
add one dim data with only coordinate and values
void Initialize(unsigned int newPoints, unsigned int dim=1, ErrorType err=kValueError)
Preallocate a data set with given size, dimension and error type.
class describing the range in the coordinates it supports multiple range in a coordinate.
unsigned int Size(unsigned int icoord=0) const
return range size for coordinate icoord (starts from zero) Size == 0 indicates no range is present [-...
void GetRange(unsigned int irange, unsigned int icoord, double &xmin, double &xmax) const
get the i-th range for given coordinate.
unsigned int Size() const
return number of fit points
unsigned int NDim() const
return coordinate data dimension
const DataOptions & Opt() const
access to options
const DataRange & Range() const
access to range
class containing the result of the fit and all the related information (fitted parameter values,...
SparseData class representing the data of a THNSparse histogram The data needs to be converted to a B...
void Add(std::vector< double > &min, std::vector< double > &max, const double content, const double error=1.0)
Adds a new bin specified by the vectors.
Class to manage histogram axis.
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Int_t GetLast() const
Return last bin on the axis i.e.
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Int_t GetFirst() const
Return first bin on the axis i.e.
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...
virtual Int_t GetNpar() const
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set lower and upper limits for parameter ipar.
static Bool_t RejectedPoint()
See TF1::RejectPoint above.
virtual void SetParameters(const Double_t *params)
virtual void SetParameter(Int_t param, Double_t value)
virtual Bool_t IsInside(const Double_t *x) const
return kTRUE if the point is inside the function range
virtual Int_t GetNdim() const
Graphics object made of three arrays X, Y and Z with the same number of points each.
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.
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
virtual Double_t * GetEYlow() const
virtual Double_t * GetEYhigh() const
virtual void Set(Int_t n)
Set number of points in the graph Existing coordinates are preserved New coordinates above fNpoints a...
TH1 is the base class of all histogram classes in ROOT.
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
virtual Int_t GetDimension() const
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...
virtual Int_t GetNcells() const
Bool_t IsBinUnderflow(Int_t bin, Int_t axis=0) const
Return true if the bin is underflow.
Bool_t IsBinOverflow(Int_t bin, Int_t axis=0) const
Return true if the bin is overflow.
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Multidimensional histogram base.
A TMultiGraph is a collection of TGraph (or derived) objects.
TList * GetListOfGraphs() const
const char * GetName() const override
Returns name of object.
Mother of all ROOT objects.
virtual TClass * IsA() const
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
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 InitPolynom(const ROOT::Fit::BinData &data, TF1 *f1)
compute initial parameter for a polynomial function given the fit data Set the parameters to an unwei...
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
DataOptions : simple structure holding the options on how the data are filled.
bool fErrors1
use all errors equal to 1, i.e. fit without errors (default is false)
bool fAsymErrors
use asymmetric errors in the value when available, selecting them according to the on sign of residua...
bool fNormBinVolume
normalize data by a normalized the bin volume (bin volume divided by a reference value)
bool fIntegral
use integral of bin content instead of bin center (default is false)
bool fBinVolume
normalize data by the bin volume (it is used in the Poisson likelihood fits)
bool fCoordErrors
use errors on the x coordinates when available (default is true)