65 for (
unsigned int i = 0; i < data.
Size(); ++i) {
66 std::cout << data.
Coords(i)[0] <<
"\t";
68 std::cout <<
"\ndata size is " << data.
Size() << std::endl;
72 std::cout <<
"\n************************************************************\n";
73 std::cout <<
"Test\t\t\t\t";
74 if (iret == 0) std::cout <<
"OK";
75 else std::cout <<
"FAILED";
76 std::cout <<
"\n************************************************************\n";
85 if (std::string(tree->
GetName()) ==
"t2") {
90 std::cout <<
"number of unbin data is " << n <<
" of dim " <<
N << std::endl;
96 std::vector<double>
m(
N);
97 for (
int unsigned i = 0; i <
n; ++i) {
100 for (
int j = 0; j <
N; ++j)
105 std::cout <<
"average values of means :\n";
106 for (
int j = 0; j <
N; ++j)
107 std::cout << m[j]/n <<
" ";
126 for (
int unsigned i = 0; i <
n; ++i) {
137 for (
int unsigned i = 0; i <
n; ++i) {
152 tree->
Draw(
"x:y",0,
"goff");
153 double *
x = tree->
GetV1();
154 double *
y = tree->
GetV2();
156 if (x == 0 || y == 0) {
168 for (
int unsigned i = 0; i <
n; ++i) {
178 else if ( dim == 1) {
180 tree->
Draw(
"x",0,
"goff");
181 double *
x = tree->
GetV1();
191 for (
int unsigned i = 0; i <
n; ++i) {
211 for (
typename T::const_iterator itr = data.begin(); itr != data.end(); ++itr) {
212 std::cout << itr->Coords()[0] <<
" " << itr->Value() <<
" " << itr->Error() << std::endl;
214 std::cout <<
"\ndata size is " << data.Size() << std::endl;
221 template <
class MinType,
class T>
250 #ifdef USE_MATHMORE_FUNC 253 assert(pol.
NPar() == func->GetNpar());
263 bool ret = fitter.
Fit(d);
265 std::cout <<
" Fit Failed " << std::endl;
274 template <
class MinType,
class T>
283 std::cout <<
"\tcopy data in FitData\n";
285 std::cout <<
"\tre-use original data \n";
310 bool ret = fitter.
Fit(*d);
312 std::cout <<
" Fit Failed " << std::endl;
324 template <
class MinType>
326 return DoUnBinFit<MinType, TTree>(
tree,
func,
debug, copyData);
328 template <
class MinType>
330 return DoBinFit<MinType, TH1>(
h1,
func,
debug, copyData);
332 template <
class MinType>
334 return DoBinFit<MinType, TGraph>(
gr,
func,
debug, copyData);
337 template <
class MinType,
class FitObj>
340 std::cout <<
"\n************************************************************\n";
341 std::cout <<
"\tFit using new Fit::Fitter " <<
typeid(*fitobj).name() << std::endl;
342 std::cout <<
"\tMinimizer is " <<
MinType::name() <<
" " << MinType::name2() <<
" func dim = " << func.
NDim() << std::endl;
350 iret |= DoFit<MinType>(fitobj,
func,
true, useGrad);
352 std::cout <<
"Fit failed " << std::endl;
356 for (
int i = 0; i <
nfit; ++i) {
357 func.SetParameters(
iniPar);
358 iret = DoFit<MinType>(fitobj,
func,
false, useGrad);
360 std::cout <<
"Fit failed " << std::endl;
366 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
367 std::cout <<
"\n************************************************************\n";
376 template <
class T,
class MinType>
379 std::cout <<
"\n************************************************************\n";
380 std::cout <<
"\tFit using " << hist->ClassName() <<
"::Fit\n";
381 std::cout <<
"\tMinimizer is " <<
MinType::name() << std::endl;
383 std::string opt =
"BFQ0";
399 for (
int i = 0; i <
nfit; ++i) {
401 iret |= hist->Fit(func,opt.c_str());
403 std::cout <<
"Fit failed " << std::endl;
418 iret |= hist->Fit(func,
"BFV0");
420 iret |= hist->Fit(func,
"V0");
422 int pr = std::cout.precision(18);
423 std::cout <<
"Chi2 value = " << func->
GetChisquare() << std::endl;
424 std::cout.precision(pr);
428 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
429 std::cout <<
"\n************************************************************\n";
436 template <
class MinType>
439 std::cout <<
"\n************************************************************\n";
440 std::cout <<
"\tFit using TTree::UnbinnedFit\n";
441 std::cout <<
"\tMinimizer is " <<
MinType::name() << std::endl;
444 std::string sel =
"";
450 for (
int i = 0; i <
nfit; ++i) {
454 std::cout <<
"Fit failed : iret = " << iret << std::endl;
466 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
467 std::cout <<
"\n************************************************************\n";
487 std::cout <<
"\n************************************************************\n";
488 std::cout <<
"\tFit using RooFit (Chi2 Fit)\n";
489 std::cout <<
"\twith function " << func->
GetName() <<
"\n";
495 for (
int i = 0; i <
nfit; ++i) {
506 std::string fname = func->
GetName();
507 if (fname ==
"gaussian") {
508 double val,pmin,pmax;
510 mean =
new RooRealVar(
"mean",
"Mean of Gaussian",val) ;
512 sigma =
new RooRealVar(
"sigma",
"Width of Gaussian",val) ;
514 pdf =
new RooGaussian(
"gauss",
"gauss(x,mean,sigma)",x,*mean,*sigma) ;
529 delete mean;
delete sigma;
533 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
534 std::cout <<
"\n************************************************************\n";
542 std::cout <<
"\n************************************************************\n";
543 std::cout <<
"\tFit using RooFit (Likelihood Fit)\n";
544 std::cout <<
"\twith function " << func->
GetName() <<
"\n";
550 for (
int i = 0; i <
nfit; ++i) {
561 RooGaussian pdfx(
"gaussx",
"gauss(x,mean,sigma)",x,mean,sigma);
566 RooGaussian pdfy(
"gaussy",
"gauss(y,meanx,sigmay)",y,meany,sigmay);
573 std::cout <<
"num entries = " << data.
numEntries() << std::endl;
591 std::cout <<
" Roofit status " << result->
status() << std::endl;
594 if (save) iret |= (result == 0);
597 std::cout <<
"Fit failed " << std::endl;
604 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
605 std::cout <<
"\n************************************************************\n";
613 std::cout <<
"\n************************************************************\n";
614 std::cout <<
"\tFit using RooFit (Likelihood Fit)\n";
620 for (
int i = 0; i <
nfit; ++i) {
623 std::vector<RooRealVar *>
x(
N);
624 std::vector<RooRealVar *>
m(
N);
625 std::vector<RooRealVar *> s(
N);
627 std::vector<RooGaussian *> g(
N);
628 std::vector<RooProdPdf *> pdf(
N);
630 for (
int j = 0; j <
N; ++j) {
632 x[j] =
new RooRealVar(xname.c_str(),xname.c_str(),-10000,10000) ;
640 for (
int j = 0; j <
N; ++j) {
649 g[j] =
new RooGaussian(gname.c_str(),
"gauss(x,mean,sigma)",*x[j],*m[j],*s[j]);
668 std::cout <<
"num entries = " << data.
numEntries() << std::endl;
670 (pdf[N-1]->getVariables())->
Print(
"v");
671 std::cout <<
"\n\nDo the fit now \n\n";
678 #ifndef _WIN32 // until a bug 30762 is fixed 686 std::cout <<
" Roofit status " << result->
status() << std::endl;
691 iret |= (result != 0);
694 for (
int j = 0; j <
N; ++j) {
699 if (j> 0)
delete pdf[j];
702 if (iret != 0)
return iret;
709 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
710 std::cout <<
"\n************************************************************\n";
715 double poly2(
const double *
x,
const double *p) {
716 return p[0] + (p[1]+p[2]*x[0] ) * x[0];
724 std::cout <<
"\n\n************************************************************\n";
725 std::cout <<
"\t POLYNOMIAL FIT\n";
726 std::cout <<
"************************************************************\n";
728 std::string fname(
"pol2");
730 TF1 *
f1 =
new TF1(
"pol2",fname.c_str(),-5,5.);
740 for (
int i = 0; i <1000; ++i)
748 iret |= FitUsingTFit<TH1,TMINUIT>(
h1,
f1);
749 iret |= FitUsingTFit<TH1,MINUIT2>(
h1,
f1);
759 iret |= FitUsingNewFitter<MINUIT2>(
h1,
f2);
760 iret |= FitUsingNewFitter<TMINUIT>(
h1,
f2);
766 iret |= FitUsingNewFitter<LINEAR>(
h1,lfunc,
true);
767 iret |= FitUsingTFit<TH1,LINEAR>(
h1,
f1);
773 iret |= FitUsingTFit<TGraph,TMINUIT>(
gr,
f1);
775 iret |= FitUsingTFit<TGraph,MINUIT2>(
gr,
f1);
777 iret |= FitUsingNewFitter<MINUIT2>(
gr,
f2);
780 std::cout <<
"\n-----> test now TGraphErrors with errors in X coordinates\n\n";
784 iret |= FitUsingTFit<TGraph,TMINUIT>(gr2,
f1);
785 iret |= FitUsingTFit<TGraph,MINUIT2>(gr2,
f1);
787 iret |= FitUsingNewFitter<MINUIT2>(gr2,
f2);
796 double tmp = (x[0]-p[1])/p[2];
802 double invsig = 1./p[1];
803 double tmp = (x[0]-p[0]) * invsig;
804 const double sqrt_2pi = 1./
std::sqrt(2.* 3.14159 );
805 return std::exp(-0.5 * tmp*tmp ) * sqrt_2pi * invsig;
814 for (
int i = 0; i <
N; ++i)
824 std::cout <<
"\n\n************************************************************\n";
825 std::cout <<
"\t GAUSSIAN FIT\n";
826 std::cout <<
"************************************************************\n";
838 TH1D * h2 =
new TH1D(
"h2",
"h2",nbin,-5.,5.);
840 for (
int i = 0; i < 10000000; ++i)
851 iret |= FitUsingNewFitter<MINUIT2>(h2,
f2);
852 iret |= FitUsingNewFitter<TMINUIT>(h2,
f2);
857 iret |= FitUsingTFit<TH1,TMINUIT>(h2,
f1);
858 iret |= FitUsingTFit<TH1,MINUIT2>(h2,
f1);
861 iret |= FitUsingNewFitter<GSL_FR>(h2,
f2);
862 iret |= FitUsingNewFitter<GSL_PR>(h2,
f2);
863 iret |= FitUsingNewFitter<GSL_BFGS>(h2,
f2);
864 iret |= FitUsingNewFitter<GSL_BFGS2>(h2,
f2);
871 iret |= FitUsingTFit<TGraph,TMINUIT>(
gr,
f1);
872 iret |= FitUsingTFit<TGraph,MINUIT2>(
gr,
f1);
874 iret |= FitUsingNewFitter<MINUIT2>(
gr,
f2);
879 iret |= FitUsingTFit<TGraph,TMINUIT>(gr2,
f1);
881 iret |= FitUsingNewFitter<MINUIT2>(gr2,
f2);
887 std::cout <<
"\n\nTest Using pre-calculated gradients\n\n";
889 iret |= FitUsingNewFitter<MINUIT2>(h2,
f2,useGrad);
890 iret |= FitUsingNewFitter<TMINUIT>(h2,
f2,useGrad);
891 iret |= FitUsingNewFitter<GSL_FR>(h2,
f2,useGrad);
892 iret |= FitUsingNewFitter<GSL_PR>(h2,
f2,useGrad);
893 iret |= FitUsingNewFitter<GSL_BFGS>(h2,
f2,useGrad);
894 iret |= FitUsingNewFitter<GSL_BFGS2>(h2,
f2,useGrad);
898 std::cout <<
"\n\nTest Least Square algorithms\n\n";
899 iret |= FitUsingNewFitter<GSL_NLS>(h2,
f2);
900 iret |= FitUsingNewFitter<FUMILI2>(h2,
f2);
901 iret |= FitUsingNewFitter<TFUMILI>(h2,
f2);
916 std::cout <<
"\n\n************************************************************\n";
917 std::cout <<
"\t UNBINNED TREE (GAUSSIAN) FIT\n";
918 std::cout <<
"************************************************************\n";
921 TTree t1(
"t1",
"a simple Tree with simple variables");
928 t1.
Branch(
"ev",&ev,
"ev/I");
935 for (
Int_t i=0;i<nrows;i++) {
966 iret |= FitUsingTTreeFit<MINUIT2>(&
t1,
f1,
"x");
967 iret |= FitUsingTTreeFit<MINUIT2>(&
t1,
f1,
"x");
969 iret |= FitUsingTTreeFit<TMINUIT>(&
t1,
f1,
"x");
971 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf1,
false);
972 iret |= FitUsingNewFitter<TMINUIT>(&
t1,wf1,
false);
973 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf1,
true);
974 iret |= FitUsingNewFitter<TMINUIT>(&
t1,wf1,
true);
980 iret |= FitUsingTTreeFit<MINUIT2>(&
t1,
f2,
"x:y");
981 iret |= FitUsingTTreeFit<TMINUIT>(&
t1,
f2,
"x:y");
983 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf2,
true);
984 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf2,
false);
997 std::cout <<
"\n\n************************************************************\n";
998 std::cout <<
"\t UNBINNED TREE (GAUSSIAN MULTI-DIM) FIT\n";
999 std::cout <<
"************************************************************\n";
1001 TTree t1(
"t2",
"a large Tree with simple variables");
1004 t1.
Branch(
"x",x,
"x[20]/D");
1005 t1.
Branch(
"ev",&ev,
"ev/I");
1009 for (
Int_t i=0;i<nevt;i++) {
1010 for (
int j = 0; j <
N; ++j) {
1011 double mu = double(j)/10.;
1012 double s = 1.0 + double(j)/10.;
1013 x[j] = r.
Gaus(mu,s);
1023 for (
int i = 0; i <
N; ++i) {
1033 iret |= FitUsingNewFitter<MINUIT2>(&
t1,
f2);
1034 iret |= FitUsingNewFitter<TMINUIT>(&
t1,
f2);
1035 iret |= FitUsingNewFitter<GSL_BFGS2>(&
t1,
f2);
1047 TTree t2(
"t2b",
"a large Tree with simple variables");
1050 for (
int j = 0; j <
N; ++j) {
1053 t2.
Branch(xname.c_str(),&x[j],xname2.c_str());
1055 t2.
Branch(
"ev",&ev,
"ev/I");
1058 for (
Int_t i=0;i<nevt;i++) {
1059 for (
int j = 0; j <
N; ++j) {
1060 double mu = double(j)/10.;
1061 double s = 1.0 + double(j)/10.;
1062 x[j] = r.
Gaus(mu,s);
1070 for (
int i = 0; i <
N; ++i) {
1124 std::cerr <<
"testFitPerf :\t FAILED " << std::endl;
1126 std::cerr <<
"testFitPerf :\t OK " << std::endl;
double poly2(const double *x, const double *p)
RooArgSet * getVariables(Bool_t stripDisconnected=kTRUE) const
Return RooArgSet with all variables (tree leaf nodes of expresssion tree)
virtual const char * GetName() const
Returns name of object.
virtual const double * Parameters() const =0
Access the parameter values.
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.
double gaussian(const double *x, const double *p)
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
void SetPrintLevel(int level)
set print level
Random number generator class based on M.
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
virtual void SetAddress(void *add)
Set address of this branch.
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void SetTolerance(double tol)
set the tolerance
double gausnorm(const double *x, const double *p)
int DoBinFit(T *hist, Func &func, bool debug=false, bool useGrad=false)
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...
RooCmdArg PrintLevel(Int_t code)
RooProdPdf is an efficient implementation of a product of PDFs of the form.
RooCmdArg Minos(Bool_t flag=kTRUE)
R__EXTERN TStyle * gStyle
virtual Int_t Fill()
Fill all branches.
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 ...
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
int testLargeTreeRooFit(int nevt=1000)
const double * Coords(unsigned int ipoint) const
return a pointer to the coordinates data for the given fit point
ROOT::Math::IParamMultiFunction Func
const FitResult & Result() const
get fit result
RooDataSet is a container class to hold N-dimensional binned data.
void Stop()
Stop the stopwatch.
virtual Long64_t GetSelectedRows()
virtual Int_t UnbinnedFit(const char *funcname, const char *varexp, const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Unbinned fit of one or more variable(s) from a tree.
Int_t setPrintLevel(Int_t newLevel)
Change the MINUIT internal printing level.
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
void printResult(int iret)
double gausnormN(const double *x, const double *p)
const FitConfig & Config() const
access to the fit configuration (const method)
unsigned int Size() const
return number of fit points
RooRealVar represents a fundamental (non-derived) real valued object.
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
ROOT::Fit::UnBinData * FillUnBinData(TTree *tree, bool copyData=true, unsigned int dim=1)
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...
virtual void SetParameters(const double *p)
Set the parameter values.
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.
virtual void SetEstimate(Long64_t nentries=1000000)
Set number of entries to estimate variable limits.
virtual Double_t * GetV2()
Fitter class, entry point for performing all type of fits.
int FitUsingNewFitter(FitObj *fitobj, Func &func, bool useGrad=false)
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
A 2-Dim function with parameters.
R__EXTERN TRandom * gRandom
tomato 1-D histogram with a double per channel (see TH1 documentation)}
RooDataSet is a container class to hold unbinned data.
void SetFunction(const IModelFunction &func, bool useGradient=false)
Set the fitted function (model function) from a parametric function interface.
Parametric Function class describing polynomials of order n.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
int FitUsingTTreeFit(TTree *tree, TF1 *func, const std::string &vars="x")
Double_t GetChisquare() const
virtual void SetParameters(const double *p)=0
Set the parameter values.
RooCmdArg Hesse(Bool_t flag=kTRUE)
void Print(std::ostream &os, const OptionType &opt)
int DoUnBinFit(T *tree, Func &func, bool debug=false, bool copyData=false)
void Add(double x)
preallocate a data set given size and dimension of the coordinates if a vector already exists with co...
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
double func(double *x, double *p)
void printData(const ROOT::Fit::UnBinData &data)
double gausnorm2D(const double *x, const double *p)
void Append(unsigned int newPoints, unsigned int dim=1, bool isWeighted=false)
virtual Long64_t GetEntries() const
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
RooCmdArg Save(Bool_t flag=kTRUE)
virtual void GetParLimits(Int_t ipar, Double_t &parmin, Double_t &parmax) const
Return limits for parameter ipar.
MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in a mu...
RooFitResult * fit(const char *options)
Parse traditional RooAbsPdf::fitTo driver options.
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
std::string ToString(const T &val)
Utility function for conversion to strings.
double f2(const double *x)
virtual Double_t GetParameter(Int_t ipar) const
A Graph is a graphics object made of two arrays X and Y with npoints each.
A TGraphErrors is a TGraph with error bars.
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
virtual RooFitResult * fitTo(RooAbsData &data, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Fit PDF to given dataset.
A TTree object has a header with a name and a title.
void SetParameters(const double *p)
Set the parameter values.
virtual void SetParameter(Int_t param, Double_t value)
int FitUsingTFit(T *hist, TF1 *func)
A TTree is a list of TBranches.
void SetErrorX(Float_t errorx=0.5)
unsigned int NPar() const
Return the number of parameters.
int FitUsingRooFit2(TTree *tree)
int FitUsingRooFit(TH1 *hist, TF1 *func)
virtual Double_t * GetV1()
virtual Int_t numEntries() const
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
int DoFit(TTree *tree, Func &func, bool debug=false, bool copyData=false)
RooMinuit is a wrapper class around TFitter/TMinuit that provides a seamless interface between the MI...
int testLargeTreeFit(int nevt=1000)