11 #include <TGraphErrors.h>
16 #include <Math/ProbFuncMathCore.h>
18 #include "../../BAT/BCLog.h"
19 #include "../../BAT/BCDataSet.h"
20 #include "../../BAT/BCDataPoint.h"
21 #include "../../BAT/BCMath.h"
23 #include "BCGraphFitter.h"
32 , fGraphFitFunction(0)
35 SetMarginalizationMethod(BCIntegrate::kMargMetropolis);
45 , fGraphFitFunction(0)
48 SetMarginalizationMethod(BCIntegrate::kMargMetropolis);
58 , fGraphFitFunction(0)
64 SetMarginalizationMethod(BCIntegrate::kMargMetropolis);
74 , fGraphFitFunction(0)
80 SetMarginalizationMethod(BCIntegrate::kMargMetropolis);
88 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetGraph() : TGraphErrors not created.");
92 int npoints = graph->GetN();
95 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetGraph() : TGraphErrors is empty.");
100 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetGraph() : TGraphErrors has only one point. Not able to fit.");
106 double * x = fGraph->GetX();
107 double * y = fGraph->GetY();
108 double * ex = fGraph->GetEX();
109 double * ey = fGraph->GetEY();
113 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetGraph() : TGraphErrors has NO errors set on Y. Not able to fit.");
125 for (
int i = 0; i < npoints; ++i)
128 double errx = ex ? ex[i] : 0.;
140 else if(x[i]+errx > xmax)
143 if(y[i] - 5.*ey[i] < ymin)
144 ymin = y[i] - 5.*ey[i];
145 else if(y[i] + 5.*ey[i] > ymax)
146 ymax = y[i] + 5.*ey[i];
152 SetDataBoundaries(0, xmin, xmax);
153 SetDataBoundaries(1, ymin, ymax);
166 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetFitFunction() : TF1 not created.");
171 int npar = func->GetNpar();
174 BCLog::Out(BCLog::error,BCLog::error,
"BCGraphFitter::SetFitFunction() : TF1 has zero parameters. Not able to fit.");
183 SetName(TString::Format(
"GraphFitter with %s",fFitFunction->GetName()));
186 ClearParameters(
true);
189 for (
int i = 0; i < npar; ++i)
193 fFitFunction->GetParLimits(i, xmin, xmax);
201 return GetNParameters();
224 fFitFunction->SetParameters(¶ms[0]);
235 double yexp = fFitFunction->Eval(x[0]);
255 fFitFunction->SetParameters(¶ms[0]);
257 return fFitFunction->Eval(x[0]);
266 BCLog::OutError(
"BCEfficiencyFitter::Fit : Graph not defined.");
273 BCLog::OutError(
"BCEfficiencyFitter::Fit : Fit function not defined.");
285 BCLog::OutError(
"BCEfficiencyFitter::Fit : Graph not defined.");
291 BCLog::OutError(
"BCEfficiencyFitter::Fit : Fit function not defined.");
297 Form(
"Fitting %d data points with function of %d parameters",
GetNDataPoints(),GetNParameters()));
301 Form(
"Number of parameters (%d) lower than or equal to number of points (%d)."
303 BCLog::Out(BCLog::warning,BCLog::warning,
"Fit doesn't have much meaning.");
311 BCIntegrate::BCOptimizationMethod method_temp = GetOptimizationMethod();
312 SetOptimizationMethod(BCIntegrate::kOptMinuit);
313 FindMode( GetBestFitParameters());
314 SetOptimizationMethod(method_temp);
320 GetPvalueFromChi2NDoF(GetBestFitParameters(), 3);
334 BCLog::Out(BCLog::error, BCLog::error,
"BCGraphFitter::DrawFit() : TGraphErrors not defined.");
340 BCLog::Out(BCLog::error, BCLog::error,
"BCGraphFitter::DrawFit() : Fit function not defined.");
345 TString opt = options;
349 if(!opt.Contains(
"same"))
353 fErrorBand = GetErrorBandGraph(0.16, 0.84);
354 fErrorBand->Draw(
"f same");
357 fGraphFitFunction = GetFitFunctionGraph( GetBestFitParameters() );
358 fGraphFitFunction->SetLineColor(kRed);
359 fGraphFitFunction->SetLineWidth(2);
360 fGraphFitFunction->Draw(
"l same");
364 fGraph->Draw(
"p same");
369 TLegend * legend =
new TLegend(0.25, 0.75, 0.55, 0.95);
370 legend->SetBorderSize(0);
371 legend->SetFillColor(kWhite);
372 legend->AddEntry(fGraph,
"Data",
"P");
373 legend->AddEntry(fGraphFitFunction,
"Best fit",
"L");
374 legend->AddEntry(fErrorBand,
"Error band",
"F");
389 BCLog::OutWarning(
"BCGraphFitter::CDF: Non-zero errors in x-direction are ignored!");
392 double yObs = values.at(1);
397 return ROOT::Math::normal_cdf(yObs, values.at(3), yExp);
void PrintShortFitSummary(int chi2flag=0)
A class representing a data point.
void SetName(const char *name)
void SetDataSet(BCDataSet *dataset)
int SetPriorConstantAll()
const std::vector< double > & GetValues() const
A class representing a set of data points.
void SetFitFunctionIndices(int indexx, int indexy)
double GetPvalueFromChi2(const std::vector< double > &par, int sigma_index)
int SetFitFunction(TF1 *func)
BCDataPoint * GetDataPoint(unsigned int index) const
virtual int AddParameter(BCParameter *parameter)
unsigned GetNDataPoints() const
double FitFunction(const std::vector< double > &x, const std::vector< double > ¶meters)
static void Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const char *message)
double LogGaus(double x, double mean, double sigma, bool norm)
void DrawFit(const char *options="", bool flaglegend=false)
double LogLikelihood(const std::vector< double > ¶meters)
BCDataPoint * GetDataPoint(unsigned int index)
virtual double CDF(const std::vector< double > ¶meters, int index, bool lower=false)
void AddDataPoint(BCDataPoint *datapoint)
A base class for all fitting classes.
void SetValue(unsigned index, double value)
int SetGraph(TGraphErrors *graph)