77 for (
unsigned int i = 0; i < data.
Size(); ++i) {
78 std::cout << data.
Coords(i)[0] <<
"\t";
80 std::cout <<
"\ndata size is " << data.
Size() << std::endl;
84 std::cout <<
"\n************************************************************\n";
85 std::cout <<
"Test\t\t\t\t";
86 if (iret == 0) std::cout <<
"OK";
87 else std::cout <<
"FAILED";
88 std::cout <<
"\n************************************************************\n";
97 if (std::string(tree->
GetName()) ==
"t2") {
102 std::cout <<
"number of unbin data is " << n <<
" of dim " << N << std::endl;
108 std::vector<double>
m(N);
109 for (
int unsigned i = 0; i <
n; ++i) {
112 for (
int j = 0; j <
N; ++j)
117 std::cout <<
"average values of means :\n";
118 for (
int j = 0; j <
N; ++j)
119 std::cout << m[j]/n <<
" ";
138 for (
int unsigned i = 0; i <
n; ++i) {
149 for (
int unsigned i = 0; i <
n; ++i) {
164 tree->
Draw(
"x:y",0,
"goff");
165 double * x = tree->
GetV1();
166 double *
y = tree->
GetV2();
168 if (x == 0 || y == 0) {
180 for (
int unsigned i = 0; i <
n; ++i) {
190 else if ( dim == 1) {
192 tree->
Draw(
"x",0,
"goff");
193 double * x = tree->
GetV1();
203 for (
int unsigned i = 0; i <
n; ++i) {
223 for (
typename T::const_iterator itr = data.begin(); itr != data.end(); ++itr) {
224 std::cout << itr->Coords()[0] <<
" " << itr->Value() <<
" " << itr->Error() << std::endl;
226 std::cout <<
"\ndata size is " << data.Size() << std::endl;
237 fData(&data), fFunc(f)
264 unsigned int n = data.
Size();
267 std::cout <<
"\n\nFit data size = " << n << std::endl;
268 std::cout <<
"func pointer is " <<
typeid(
func).
name() << std::endl;
275 #if !defined(USE_VDT) && !defined(USE_AOS)
277 for (
unsigned int i = 0; i <
n; i++) {
279 const double * x = data.
Coords(i);
281 double fval = fFunc(x,p);
300 std::vector<double> fval(n);
301 std::vector<double> logval(n);
304 for (
unsigned int i = 0; i <
n; i++) {
306 const double * x = data.
Coords(i);
308 fval[i] = fFunc(x,p);
312 const double * x = data.
Coords(0);
314 fFunc(n, data.
NDim(),
x, p, &fval[0] );
323 vdt::fast_logv(n, &fval[0], &logval[0]);
326 for (
unsigned int i = 0; i <
n; i++) {
352 std::vector <Vc::double_v>
x(ndimObs);
355 for (
unsigned int j = 0; j < data.
NDim(); ++j) {
358 x[j][k] = *(data.
Coords(ipoint)+j);
394 template <
class MinType,
class T>
423 #ifdef USE_MATHMORE_FUNC
436 bool ret = fitter.
Fit(d);
438 std::cout <<
" Fit Failed " << std::endl;
447 template <
class MinType,
class T>
457 std::cout <<
"\tcopy data in FitData\n";
459 std::cout <<
"\tre-use original data \n";
484 bool ret = fitter.
Fit(*d);
487 std::cout <<
" Fit Failed " << std::endl;
500 template <
class MinType,
class T,
class VFunc>
510 std::cout <<
"\tcopy data in FitData\n";
512 std::cout <<
"\tre-use original data \n";
536 VecNLL<VFunc>
fcn(*d, func);
537 bool ret = fitter.
FitFCN(npar, fcn, p0);
540 std::cout <<
" Fit Failed " << std::endl;
546 std::cout <<
"use vec nll : nll = " << fitter.
Result().
MinFcnValue() << std::endl;
556 template <
class MinType>
558 return DoUnBinFit<MinType, TTree>(
tree,
func,
debug, copyData);
561 template <
class MinType>
563 return DoBinFit<MinType, TH1>(
h1,
func,
debug, copyData);
565 template <
class MinType>
567 return DoBinFit<MinType, TGraph>(
gr,
func,
debug, copyData);
570 template <
class MinType,
class F>
572 return DoUnBinFitVec<MinType, TTree>(
tree,
func, n1,n2,p,
debug, copyData);
575 template <
class MinType,
class F>
577 return DoBinFit<MinType, TH1>(
h1,
func,
debug, copyData);
579 template <
class MinType,
class F>
581 return DoBinFit<MinType, TGraph>(
gr,
func,
debug, copyData);
585 template <
class MinType,
class FitObj,
class FuncObj>
588 std::cout <<
"\n************************************************************\n";
589 std::cout <<
"\tFit using new Fit::Fitter " <<
typeid(*fitobj).name() << std::endl;
590 std::cout <<
"\tMinimizer is " <<
MinType::name() <<
" " << MinType::name2() <<
" func dim = " << ndimObs << std::endl;
600 func.SetParameters(iniPar);
601 iret |= DoFit<MinType>(fitobj,
func,
true, useGrad);
603 std::cout <<
"Fit failed " << std::endl;
607 for (
int i = 0; i <
nfit; ++i) {
608 func.SetParameters(iniPar);
609 iret = DoFit<MinType>(fitobj,
func,
false, useGrad);
611 std::cout <<
"Fit failed " << std::endl;
621 for (
int i = 0; i <
nfit; ++i) {
624 std::cout <<
"Fit failed " << std::endl;
632 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
633 std::cout <<
"\n************************************************************\n";
640 double poly2(
const double *x,
const double *p) {
641 return p[0] + (p[1]+p[2]*x[0] ) * x[0];
649 std::cout <<
"\n\n************************************************************\n";
650 std::cout <<
"\t POLYNOMIAL FIT\n";
651 std::cout <<
"************************************************************\n";
653 std::string fname(
"pol2");
655 TF1 *
f1 =
new TF1(
"pol2",fname.c_str(),-5,5.);
657 f1->SetParameter(0,1);
658 f1->SetParameter(1,0.0);
659 f1->SetParameter(2,1.0);
665 for (
int i = 0; i <1000; ++i)
666 h1->
Fill( f1->GetRandom() );
670 iniPar[0] = 2.; iniPar[1] = 2.; iniPar[2] = 2.;
682 iret |= FitUsingNewFitter<MINUIT2>(
h1,
f2);
683 iret |= FitUsingNewFitter<TMINUIT>(
h1,
f2);
689 iret |= FitUsingNewFitter<LINEAR>(
h1,lfunc,
true);
694 TGraphErrors *
gr =
new TGraphErrors(h1);
696 iret |= FitUsingNewFitter<MINUIT2>(
gr,
f2);
699 std::cout <<
"\n-----> test now TGraphErrors with errors in X coordinates\n\n";
702 TGraphErrors * gr2 =
new TGraphErrors(h1);
704 iret |= FitUsingNewFitter<MINUIT2>(gr2,
f2);
712 struct GausFunctions {
713 typedef T value_type;
715 static T gaussian(
const T *x,
const double *p) {
717 T tmp = (x[0]-p[1])/p[2];
721 static T gausnorm(
const T *x,
const double *p) {
724 T tmp = (x[0]-p[0]) * invsig;
725 const T sqrt_2pi = 1./
std::sqrt(2.* 3.14159 );
726 return std::exp(-0.5 * tmp*tmp ) * sqrt_2pi * invsig;
735 for (
int i = 0; i <
N; ++i)
741 static void gausnorm_v(
unsigned int n,
unsigned int stride,
const double *x,
const double *p,
double * res) {
743 double invsig = 1./p[1];
744 std::vector<double> arg(n);
745 for (
unsigned int i = 0; i<
n; ++i){
746 double tmp = (x[i*stride]-p[0]) * invsig;
747 arg[i] = -0.5 *tmp*tmp;
749 const double sqrt_2pi = 1./
std::sqrt(2.* 3.14159 );
751 vdt::fast_expv(n,&arg[0],res);
752 for (
unsigned int i = 0; i<
n; ++i){
754 for (
unsigned int i = 0; i<
n; ++i){
757 res[i] *= sqrt_2pi * invsig;
762 static void gausnorm2D_v(
unsigned int n,
unsigned int stride,
const double *x,
const double *p,
double * res) {
763 std::vector<double> tmp(n);
764 gausnorm_v(n,2,x,p,&tmp[0]);
765 gausnorm_v(n,2,x+1,p+2,res);
766 for (
unsigned int i = 0; i<
n; ++i){
770 static void gausnormN_v(
unsigned int n,
unsigned int stride,
const double *x,
const double *p,
double * res) {
771 std::vector<double> tmp(n);
772 gausnorm_v(n,stride,x,p,res);
773 for (
int j = 1; j < stride; ++j) {
774 gausnorm_v(n,stride,x+j,p+2*j,&tmp[0]);
775 for (
unsigned int i = 0; i<
n; ++i){
783 double gaussian (
const double *x,
const double *p) {
786 double gausnorm (
const double *x,
const double *p) {
800 std::cout <<
"\n\n************************************************************\n";
801 std::cout <<
"\t GAUSSIAN FIT\n";
802 std::cout <<
"************************************************************\n";
816 for (
int i = 0; i < 10000000; ++i)
819 iniPar[0] = 100.; iniPar[1] = 2.; iniPar[2] = 2.;
827 iret |= FitUsingNewFitter<MINUIT2>(
h2,
f2);
828 iret |= FitUsingNewFitter<TMINUIT>(
h2,
f2);
835 iret |= FitUsingNewFitter<GSL_FR>(
h2,
f2);
836 iret |= FitUsingNewFitter<GSL_PR>(
h2,
f2);
837 iret |= FitUsingNewFitter<GSL_BFGS>(
h2,
f2);
838 iret |= FitUsingNewFitter<GSL_BFGS2>(
h2,
f2);
843 TGraphErrors *
gr =
new TGraphErrors(h2);
846 iret |= FitUsingNewFitter<MINUIT2>(
gr,
f2);
850 TGraphErrors * gr2 =
new TGraphErrors(h2);
852 iret |= FitUsingNewFitter<MINUIT2>(gr2,
f2);
858 std::cout <<
"\n\nTest Using pre-calculated gradients\n\n";
860 iret |= FitUsingNewFitter<MINUIT2>(
h2,
f2,useGrad);
861 iret |= FitUsingNewFitter<TMINUIT>(
h2,
f2,useGrad);
862 iret |= FitUsingNewFitter<GSL_FR>(
h2,
f2,useGrad);
863 iret |= FitUsingNewFitter<GSL_PR>(
h2,
f2,useGrad);
864 iret |= FitUsingNewFitter<GSL_BFGS>(
h2,
f2,useGrad);
865 iret |= FitUsingNewFitter<GSL_BFGS2>(
h2,
f2,useGrad);
869 std::cout <<
"\n\nTest Least Square algorithms\n\n";
870 iret |= FitUsingNewFitter<GSL_NLS>(
h2,
f2);
871 iret |= FitUsingNewFitter<FUMILI2>(
h2,
f2);
872 iret |= FitUsingNewFitter<TFUMILI>(
h2,
f2);
887 std::cout <<
"\n\n************************************************************\n";
888 std::cout <<
"\t UNBINNED TREE (GAUSSIAN) FIT\n";
889 std::cout <<
"************************************************************\n";
892 TTree t1(
"t1",
"a simple Tree with simple variables");
899 t1.
Branch(
"ev",&ev,
"ev/I");
906 for (
Int_t i=0;i<nrows;i++) {
942 ndimObs = wf1.
NDim();
943 ndimPars = wf1.
NPar();
946 iret |= FitUsingNewFitter<MINUIT2>(&
t1,&GausFunctions<double>::gausnorm_v,
true);
951 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf1,
true);
961 ndimObs = wf2.
NDim();
962 ndimPars = wf2.
NPar();
967 iret |= FitUsingNewFitter<MINUIT2>(&
t1,wf2,
true);
974 iret |= FitUsingNewFitter<MINUIT2>(&
t1,&GausFunctions<double>::gausnorm2D_v,
true);
991 std::cout <<
"\n\n************************************************************\n";
992 std::cout <<
"\t UNBINNED TREE (GAUSSIAN MULTI-DIM) FIT\n";
993 std::cout <<
"************************************************************\n";
995 TTree t1(
"t2",
"a large Tree with simple variables");
998 t1.
Branch(
"x",x,
"x[20]/D");
999 t1.
Branch(
"ev",&ev,
"ev/I");
1003 for (
Int_t i=0;i<nevt;i++) {
1004 for (
int j = 0; j <
N; ++j) {
1005 double mu =
double(j)/10.;
1006 double s = 1.0 +
double(j)/10.;
1007 x[j] = r.
Gaus(mu,s);
1017 for (
int i = 0; i <
N; ++i) {
1027 ndimObs = f2.
NDim();
1028 ndimPars = f2.
NPar();
1037 iret |= FitUsingNewFitter<MINUIT2>(&
t1,
f2);
1040 iret |= FitUsingNewFitter<MINUIT2>(&
t1,&GausFunctions<double>::gausnormN_v,
true);
1096 std::cerr <<
"testFitPerf :\t FAILED " << std::endl;
1098 std::cerr <<
"testFitPerf :\t OK " << std::endl;
const FitConfig & Config() const
access to the fit configuration (const method)
virtual const double * Parameters() const =0
Access the parameter values.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
unsigned int NDim() const
Retrieve the dimension of the function.
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.
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void SetTolerance(double tol)
set the tolerance
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...
R__EXTERN TStyle * gStyle
virtual Int_t Fill()
Fill all branches.
bool FitFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
unsigned int NDim() const
return coordinate data dimension
double gausnormN(const double *x, const double *p)
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)
void printResult(int iret)
int DoFit(TTree *tree, Func &func, bool debug=false, bool copyData=false)
void Stop()
Stop the stopwatch.
virtual Long64_t GetSelectedRows()
ROOT::Math::IParamMultiFunction Func
const FitResult & Result() const
get fit result
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
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
ROOT::Fit::UnBinData * FillUnBinData(TTree *tree, bool copyData=true, unsigned int dim=1)
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.
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
virtual Double_t * GetV2()
VECTOR_NAMESPACE::double_v double_v
double gausnorm2D(const double *x, const double *p)
double gaussian(const double *x, const double *p)
int FitUsingNewFitter(FitObj *fitobj, Func &func, bool useGrad=false)
Fitter class, entry point for performing all type of fits.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
virtual const char * GetName() const
Returns name of object.
int testLargeTreeFit(int nevt=1000)
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
int DoBinFit(T *hist, Func &func, bool debug=false, bool useGrad=false)
R__EXTERN TRandom * gRandom
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.
Parametric Function class describing polynomials of order n.
int DoFitVec(TTree *tree, F &func, int n1, int n2, const double *p, bool debug=false, bool copyData=false)
unsigned int NPar() const
Return the number of parameters.
TRObject operator()(const T1 &t1) const
void printData(const ROOT::Fit::UnBinData &data)
const double * Coords(unsigned int ipoint) const
return pointer to coordinate data
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
void Add(double x)
add one dim coordinate data (unweighted)
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
double func(double *x, double *p)
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
double MinFcnValue() const
Return value of the objective function (chi2 or likelihood) used in the fit.
int DoUnBinFitVec(T *tree, VFunc &func, int ndim, int npar, const double *p0, bool debug=false, bool copyData=false)
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.
MultiDimParamGradFunctionAdapter class to wrap a one-dimensional parametric gradient function in a mu...
double gausnorm(const double *x, const double *p)
unsigned int Size() const
return number of contained points
virtual Long64_t GetEntries() const
A TTree object has a header with a name and a title.
void SetParameters(const double *p)
Set the parameter values.
unsigned int NPar() const
Return the number of Parameters.
A TTree is a list of TBranches.
void SetErrorX(Float_t errorx=0.5)
virtual Double_t * GetV1()
double poly2(const double *x, const double *p)
WrappedParamFunction class to wrap any multi-dimensional function pbject implementing the operator()(...
int DoUnBinFit(T *tree, Func &func, bool debug=false, bool copyData=false)
void Initialize(unsigned int maxpoints, unsigned int dim=1, bool isWeighted=false)
preallocate a data set given size and dimension of the coordinates if a vector already exists with co...