// @(#)root/minuit2:$Id$ // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005 /********************************************************************** * * * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT * * * **********************************************************************/ #include "Minuit2/FCNBase.h" #include "Minuit2/FunctionMinimum.h" #include "Minuit2/MnMigrad.h" #include "Minuit2/MnMinos.h" #include "Minuit2/MnUserParameterState.h" #include "Minuit2/MnPrint.h" #include "Minuit2/SimplexMinimizer.h" #include #include #ifdef USE_SEALBASE #include "SealBase/Filename.h" #include "SealBase/ShellEnvironment.h" #endif using namespace ROOT::Minuit2; class PowerLawFunc { public: PowerLawFunc(double p0, double p1) : fP0(p0), fP1(p1) {} ~PowerLawFunc() {} double operator()(double x) const { return p1()*exp(log(x)*p0()); } double p0() const {return fP0;} double p1() const {return fP1;} private: double fP0; double fP1; }; class PowerLawChi2FCN : public FCNBase { public: PowerLawChi2FCN(const std::vector& meas, const std::vector& pos, const std::vector& mvar) : fMeasurements(meas), fPositions(pos), fMVariances(mvar) {} ~PowerLawChi2FCN() {} double operator()(const std::vector& par) const { assert(par.size() == 2); PowerLawFunc pl(par[0], par[1]); double chi2 = 0.; for(unsigned int n = 0; n < fMeasurements.size(); n++) { chi2 += ((pl(fPositions[n]) - fMeasurements[n])*(pl(fPositions[n]) - fMeasurements[n])/fMVariances[n]); } return chi2; } double Up() const {return 1.;} private: std::vector fMeasurements; std::vector fPositions; std::vector fMVariances; }; class PowerLawLogLikeFCN : public FCNBase { public: PowerLawLogLikeFCN(const std::vector& meas, const std::vector& pos) : fMeasurements(meas), fPositions(pos) {} ~PowerLawLogLikeFCN() {} double operator()(const std::vector& par) const { assert(par.size() == 2); PowerLawFunc pl(par[0], par[1]); double logsum = 0.; for(unsigned int n = 0; n < fMeasurements.size(); n++) { double k = fMeasurements[n]; double mu = pl(fPositions[n]); logsum += (k*log(mu) - mu); } return -logsum; } double Up() const {return 0.5;} private: std::vector fMeasurements; std::vector fPositions; }; int main() { std::vector positions; std::vector measurements; std::vector var; { #ifdef USE_SEALBASE seal::Filename inputFile (seal::Filename ("$SEAL/src/MathLibs/Minuit/tests/MnSim/paul4.txt").substitute (seal::ShellEnvironment ())); std::ifstream in(inputFile.Name() ); #else std::ifstream in("paul4.txt"); #endif if (!in) { std::cerr << "Error opening input data file" << std::endl; return 1; } double x = 0., y = 0., err = 0.; while(in>>x>>y>>err) { // if(err < 1.e-8) continue; positions.push_back(x); measurements.push_back(y); var.push_back(err*err); } std::cout<<"size= "<>> test Chi2"<>> test log LikeliHood"<>> test Simplex"< par; par.push_back(-2.3); par.push_back(1100.); std::vector err; err.push_back(1.); err.push_back(1.); SimplexMinimizer simplex; std::cout<<"start simplex"<