22 #include "RConfigure.h" 30 std::cout <<
"Bin data, point size is " << data.
PointSize() <<
" data dimension is " << data.
NDim() <<
" type is " << data.
GetErrorType() << std::endl;
31 for (
unsigned int i = 0; i < data.
Size(); ++i) {
33 std::cout << data.
Coords(i)[0] <<
" " << data.
Value(i) << std::endl;
35 std::cout << data.
Coords(i)[0] <<
" " << data.
Value(i) <<
" +/- " << data.
Error(i) << std::endl;
39 std::cout << data.
Coords(i)[0] <<
" +/- " << ex[0] <<
" " << data.
Value(i) <<
" +/- " << ey << std::endl;
42 double eyl = 0;
double eyh = 0;
44 std::cout << data.
Coords(i)[0] <<
" +/- " << ex[0] <<
" " << data.
Value(i) <<
" - " << eyl <<
" + " << eyh << std::endl;
48 std::cout <<
"\ndata size is " << data.
Size() << std::endl;
51 for (
unsigned int i = 0; i < data.
Size(); ++i) {
52 std::cout << data.
Coords(i)[0] <<
"\t";
54 std::cout <<
"\ndata size is " << data.
Size() << std::endl;
60 if (std::abs(v1-v2) <
tol * std::abs(v2) )
return 0;
61 std::cerr << s <<
" Failed comparison of fit results \t chi2 = " << v1 <<
" it should be = " << v2 << std::endl;
74 std::string fname(
"gaus");
87 for (
int i = 0; i <1000; ++i)
110 double p[3] = {100,0,3.};
117 bool ret = fitter.
Fit(d, f);
121 std::cout <<
"Chi2 Fit Failed " << std::endl;
128 std::cout <<
"\n******************************\n\t TH1::Fit Result \n" << std::endl;
136 std::cout <<
"\n\nTest Binned Likelihood Fit" << std::endl;
148 std::cout <<
"Binned Likelihood Fit Failed " << std::endl;
154 std::cout <<
"\n******************************\n\t TH1::Fit Result \n" << std::endl;
161 std::cout <<
"\n\nTest Chi2 Fit using integral option" << std::endl;
170 ret = fitter.
Fit(d2, f);
174 std::cout <<
"Integral Chi2 Fit Failed " << std::endl;
180 std::cout <<
"\n******************************\n\t TH1::Fit Result \n" << std::endl;
196 std::cout <<
"Integral Likelihood Fit Failed " << std::endl;
202 std::cout <<
"\n******************************\n\t TH1::Fit Result \n" << std::endl;
211 std::cout <<
"\n\nRedo Chi2 Hist Fit" << std::endl;
213 ret = fitter.
Fit(d, f);
217 std::cout <<
"Chi2 Fit Failed " << std::endl;
225 std::cout <<
"\n\nTest Same Fit from a TGraphErrors - no coord errors" << std::endl;
232 ret = fitter.
Fit(dg, f);
236 std::cout <<
"Chi2 Graph Errors Fit Failed " << std::endl;
243 std::cout <<
"\n\nTest Same Fit from a TGraphErrors - use coord errors" << std::endl;
251 ret = fitter.
Fit(dger, f);
255 std::cout <<
"Chi2 Graph Errors Fit Failed " << std::endl;
261 std::cout <<
"\n******************************\n\t TGraphErrors::Fit Result \n" << std::endl;
264 for (
int ip = 0; ip < gr.
GetN(); ++ip)
268 std::cout <<
"Ndf of TGraphErrors::Fit = " << func->
GetNDF() << std::endl;
273 std::cout <<
"\n\nTest Same Fit from a TGraph" << std::endl;
280 ret = fitter.
Fit(dg2, f);
284 std::cout <<
"Chi2 Graph Fit Failed " << std::endl;
291 std::cout <<
"\n******************************\n\t TGraph::Fit Result \n" << std::endl;
294 std::cout <<
"Ndf of TGraph::Fit = " << func->
GetNDF() << std::endl;
300 std::cout <<
"\n\nRedo Chi2 Hist Fit using FUMILI2" << std::endl;
303 ret = fitter.
Fit(d, f);
307 std::cout <<
"Chi2 Fit Failed " << std::endl;
313 std::cout <<
"\n\nRedo Chi2 Hist Fit using FUMILI" << std::endl;
316 ret = fitter.
Fit(d, f);
320 std::cout <<
"Chi2 Fit Failed " << std::endl;
326 std::cout <<
"\n\nRedo Chi2 Hist Fit using GSLMultiFit" << std::endl;
329 ret = fitter.
Fit(d, f);
333 std::cout <<
"Chi2 Fit Failed " << std::endl;
339 std::cout <<
"\n\nRedo Chi2 Hist Fit using GSLMultiMin" << std::endl;
342 ret = fitter.
Fit(d, f);
346 std::cout <<
"Chi2 Fit Failed " << std::endl;
358 const double *
Parameters()
const {
return fp; }
360 Func1D *
f =
new Func1D();
361 f->SetParameters(fp);
364 unsigned int NPar()
const {
return 3; }
366 double DoEvalPar(
double x,
const double *p)
const {
367 return p[0]*x*x + p[1]*x + p[2];
377 const double *
Parameters()
const {
return fp; }
379 GradFunc2D *
f =
new GradFunc2D();
380 f->SetParameters(fp);
383 unsigned int NDim()
const {
return 2; }
384 unsigned int NPar()
const {
return 5; }
386 void ParameterGradient(
const double *
x,
const double * ,
double * grad)
const {
396 double DoEvalPar(
const double *x,
const double * p)
const {
397 return p[0]*x[0]*x[0] + p[1]*x[0] + p[2]*x[1]*x[1] + p[3]*x[1] + p[4];
407 double DoParameterDerivative(
const double * x,
const double * p,
unsigned int ipar)
const {
408 std::vector<double> grad(
NPar());
409 ParameterGradient(x, p, &grad[0] );
421 std::string fname(
"pol2");
432 TH1D * h2 =
new TH1D(
"h2",
"h2",30,-5.,5.);
434 for (
int i = 0; i <1000; ++i)
447 double p[3] = {100,0,3.};
453 std::cout <<
"\n\nTest histo polynomial fit using Fitter" << std::endl;
456 bool ret = fitter.
Fit(d, f);
460 std::cout <<
" Fit Failed " << std::endl;
466 std::cout <<
"\n******************************\n\t TH1::Fit(pol2) Result \n" << std::endl;
472 std::cout <<
"\n\nTest histo polynomial linear fit " << std::endl;
479 ret = fitter.
Fit(d, pf);
485 std::cout <<
" Fit Failed " << std::endl;
491 std::cout <<
"\n******************************\n\t TH1::Fit(pol2) Result with TLinearFitter \n" << std::endl;
506 std::string fname(
"pol2");
508 double p0[5] = { 1.,2.,0.5,1.,3. };
517 TH2D * h2 =
new TH2D(
"h2d",
"h2d",30,0,10.,30,0.,10.);
519 for (
int i = 0; i <10000; ++i) {
534 double p[5] = { 2.,1.,1,2.,100. };
543 std::cout <<
"\ntest 2D histo fit using gradient" << std::endl;
544 bool ret = fitter.
Fit(d, f);
548 std::cout <<
"Gradient Fit Failed " << std::endl;
554 std::cout <<
"\ntest result without using gradient" << std::endl;
556 ret = fitter.
Fit(d, f2);
560 std::cout <<
" Chi2 Fit Failed " << std::endl;
566 std::cout <<
"\ntest result without gradient and binned likelihood" << std::endl;
568 fitter.
SetFunction(static_cast<const ROOT::Math::IParamMultiFunction &>(f) );
579 std::cout <<
"Poisson 2D Bin Likelihood Fit Failed " << std::endl;
584 std::cout <<
"\ntest result using gradient and binned likelihood" << std::endl;
602 std::cout <<
"Gradient Bin Likelihood Fit Failed " << std::endl;
607 std::cout <<
"\ntest result using linear fitter" << std::endl;
610 ret = fitter.
Fit(d, f);
614 std::cout <<
"Linear 2D Fit Failed " << std::endl;
623 std::cout <<
"\ntest using TGraph2D" << std::endl;
627 std::cout <<
"data size from graph " << d2.
Size() << std::endl;
631 ret = fitter.
Fit(d2, f2);
635 std::cout <<
" TGraph2D Fit Failed " << std::endl;
641 std::cout <<
"\n******************************\n\t TGraph::Fit Result \n" << std::endl;
645 std::cout <<
"\ntest using TGraph2D and gradient function" << std::endl;
647 ret = fitter.
Fit(d2, f);
651 std::cout <<
" TGraph2D Grad Fit Failed " << std::endl;
656 std::cout <<
"\ntest using TGraph2DErrors - error only in Z" << std::endl;
659 for (
int i = 0; i < g2.GetN(); ++i) {
660 double x = g2.GetX()[i];
661 double y = g2.GetY()[i];
662 g2err.SetPoint(i,x,y,g2.GetZ()[i]);
670 ret = fitter.
Fit(d3, f);
674 std::cout <<
" TGraph2DErrors Fit Failed " << std::endl;
682 std::cout <<
"\ntest using TGraph2DErrors - with error in X,Y,Z" << std::endl;
683 for (
int i = 0; i < g2err.GetN(); ++i) {
684 double x = g2.GetX()[i];
685 double y = g2.GetY()[i];
688 std::cout <<
"\n******************************\n\t TGraph2DErrors::Fit Result \n" << std::endl;
701 std::string fname(
"gausn");
709 for (
int i = 0; i <
n; ++i)
719 double p[3] = {1,2,10.};
727 std::cout <<
"fix parameter 0 " <<
" to value " << f.
Parameters()[0] << std::endl;
730 std::cout <<
"set lower range to 0 for sigma " << std::endl;
746 bool ret = fitter.
Fit(d);
750 std::cout <<
"Unbinned Likelihood Fit Failed " << std::endl;
755 std::cout <<
"\n\nRedo Fit using FUMILI2" << std::endl;
768 std::cout <<
"Unbinned Likelihood Fit using FUMILI2 Failed " << std::endl;
774 std::cout <<
"\n\nRedo Fit using FUMILI" << std::endl;
788 std::cout <<
"Unbinned Likelihood Fit using FUMILI Failed " << std::endl;
804 double x[5] = {1,2,3,4,5};
805 double y[5] = {2.1, 3.5, 6.5, 8.8, 9.5};
806 double ex[5] = {.3,.3,.3,.3,.3};
807 double ey[5] = {.5,.5,.5,.5,.5};
808 double eyl[5] = {.2,.2,.2,.2,.2};
809 double eyh[5] = {.8,.8,.8,.8,.8};
811 std::cout <<
"\n********************************************************\n";
812 std::cout <<
"Test simple fit of Tgraph of 5 points" << std::endl;
813 std::cout <<
"\n********************************************************\n";
829 std::cout <<
"\ntest TGraph (no errors) " << std::endl;
838 bool ret = fitter.
Fit(dgr, f);
842 std::cout <<
"Chi2 Graph Fit Failed " << std::endl;
849 std::cout <<
"\n******************************\n\t TGraph::Fit Result \n" << std::endl;
856 std::cout <<
"\ntest TGraphErrors " << std::endl;
866 ret = fitter.
Fit(dgrer, f);
870 std::cout <<
"Chi2 Graph Fit Failed " << std::endl;
878 std::cout <<
"\n******************************\n\t TGraphErrors::Fit Result \n" << std::endl;
885 std::cout <<
"\ntest TGraphAsymmErrors " << std::endl;
896 ret = fitter.
Fit(dgraer, f);
900 std::cout <<
"Chi2 Graph Fit Failed " << std::endl;
906 std::cout <<
"\n******************************\n\t TGraphAsymmErrors::Fit Result \n" << std::endl;
917 template<
typename Test>
919 std::cout << name <<
"\n\t\t";
921 std::cout <<
"\n" << name <<
":\t\t";
923 std::cout <<
"OK" << std::endl;
925 std::cout <<
"Failed" << std::endl;
938 std::cout <<
"\n******************************\n";
939 if (iret) std::cerr <<
"\n\t testFit FAILED !!!!!!!!!!!!!!!! \n";
940 else std::cout <<
"\n\t testFit all OK \n";
unsigned int Size() const
return number of fit points
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
virtual const double * Parameters() const =0
Access the parameter values.
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
virtual void SetParameters(const Double_t *params)
virtual Double_t GetRandom()
Return a random number following this function shape.
void SetPrintLevel(int level)
set print level
Random number generator class based on M.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
double Chisquare(const TH1 &h1, TF1 &f1, bool useRange, bool usePL=false)
compute the chi2 value for an histogram given a function (see TH1::Chisquare for the documentation) ...
ErrorType GetErrorType() const
int compareResult(double v1, double v2, std::string s="", double tol=0.01)
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
double MinFcnValue() const
Return value of the objective function (chi2 or likelihood) used in the fit.
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
const FitResult & Result() const
get fit result
virtual Int_t GetNDF() const
Return the number of degrees of freedom in the fit the fNDF parameter has been previously computed du...
TGraph with asymmetric error bars.
void SetParameters(const double *p)
set parameter values
void SetParameters(TFitEditor::FuncParams_t &pars, TF1 *func)
Restore the parameters from pars into the function.
void Fix()
fix the parameter
const FitConfig & Config() const
access to the fit configuration (const method)
double Error(unsigned int ipoint) const
return error on the value for the given fit point Safe (but slower) method returning correctly the er...
Double_t GetErrorX(Int_t bin) const
This function is called by GraphFitChisquare.
double chi2FromFit(const TF1 *func)
void SetLowerLimit(double low)
set a single lower limit
virtual unsigned int NPar() const =0
Return the number of Parameters.
bool Fit(const Data &data, const Function &func)
fit a data set using any generic model function If data set is binned a least square fit is performed...
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
void FillData(BinData &dv, const TH1 *hist, TF1 *func=0)
fill the data vector from a TH1.
DataOptions : simple structure holding the options on how the data are filled.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
RooCmdArg Parameters(const RooArgSet ¶ms)
Fitter class, entry point for performing all type of fits.
const double * Coords(unsigned int ipoint) const
return a pointer to the coordinates data for the given fit point
static TVirtualFitter * GetFitter()
static: return the current Fitter
const DataOptions & Opt() const
access to options
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
unsigned int PointSize() const
return the size of a fit point (is the coordinate dimension + 1 for the value and eventually the numb...
A 2-Dim function with parameters.
tomato 1-D histogram with a double per channel (see TH1 documentation)}
void SetFunction(const IModelFunction &func, bool useGradient=false)
Set the fitted function (model function) from a parametric function interface.
bool LikelihoodFit(const BinData &data, bool extended=true)
Binned Likelihood fit.
Double_t GetChisquare() const
virtual void SetParameters(const double *p)=0
Set the parameter values.
const double * Coords(unsigned int ipoint) const
return pointer to coordinate data
void PrintCovMatrix(std::ostream &os) const
print error matrix and correlations
Double_t GetErrorY(Int_t bin) const
This function is called by GraphFitChisquare.
Specialized IParamFunction interface (abstract class) for one-dimensional parametric functions It is ...
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom)
Return 2 random numbers following this function shape.
void Add(double x)
add one dim coordinate data (unweighted)
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
const std::vector< double > & Parameters() const
parameter values (return std::vector)
double func(double *x, double *p)
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
int testFit(Test t, std::string name)
double Value(unsigned int ipoint) const
return the value for the given fit point
unsigned int NDim() const
return coordinate data dimension
virtual Int_t GetNpar() const
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
double f2(const double *x)
const double * GetPointError(unsigned int ipoint, double &errvalue) const
Retrieve the errors on the point (coordinate and value) for the given fit point It must be called onl...
void SetNormErrors(bool on=true)
set the option to normalize the error on the result according to chi2/ndf
A Graph is a graphics object made of two arrays X and Y with npoints each.
Param Functor class for Multidimensional functions.
A TGraphErrors is a TGraph with error bars.
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
virtual Double_t * GetParameters() const
void SetLimits(double low, double up)
set a double side limit, if low == up the parameter is fixed if low > up the limits are removed ...
void SetParameters(const double *p)
Set the parameter values.
virtual void SetParameter(Int_t param, Double_t value)
virtual void SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
Graphics object made of three arrays X, Y and Z with the same number of points each.
Documentation for the abstract class IBaseFunctionMultiDim.
virtual double DoEvalPar(double x, const double *p) const =0
Implementation of the evaluation function using the x value and the parameters.
Int_t Fill(Double_t)
Invalid Fill method.
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
double Chi2() const
Chi2 fit value in case of likelihood must be computed ?
unsigned int Size() const
return number of contained points
void printData(const ROOT::Fit::BinData &data)
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Graph 2D class with errors.
tomato 2-D histogram with a double per channel (see TH1 documentation)}