ROOT Version: 6.24/06
code:
#include
#include “TF1.h”
#include “TCanvas.h”
#include “Math/Minimizer.h”
#include “Math/Factory.h”
#include “Math/Functor.h”
#include
// 定义方程N(x)
double N(double x, const double* params) {
double A = params[0];
double B = params[1];
return A * std::exp(B * x);
}
// 定义方程Y(x)
double Y(double x, const double* params) {
double C = params[2];
double D = params[3];
double E = params[4];
double F = params[5];
return C * (1 - std::exp(D + x)) + E * (1 - std::exp(F - x));
}
// 定义目标函数,用于寻找N(Y)的关系
double TargetFunction(const double* x, const double* params) {
return N(Y(x[0], params), params) - x[1];
}
int ss() {
// 创建TF1对象
TF1 *nFunc = new TF1(“nFunc”, N, 0, 10, 2);
TF1 *yFunc = new TF1(“yFunc”, Y, 0, 10, 6);
// 设置参数初始值
double params[6] = {2.0, 0.5, 1.0, 0.1, 0.8, 0.2}; // 这里使用示例参数
// 使用ROOT的数值优化器查找N(Y)的关系
ROOT::Math::Functor fcn(&TargetFunction, 2);
ROOT::Math::Minimizer* minimizer = ROOT::Math::Factory::CreateMinimizer("Minuit2", "Migrad");
minimizer->SetFunction(fcn);
minimizer->SetLimitedVariable(0, "x", 0.0, 0.01, 0.0, 10.0);
minimizer->Minimize();
// 获取最优参数
const double* bestFitParams = minimizer->X();
double newY = N(Y(bestFitParams[0], params), params);
std::cout << "Best-fit parameter for x: " << bestFitParams[0] << std::endl;
std::cout << "N(Y) value: " << newY << std::endl;
delete nFunc;
delete yFunc;
delete minimizer;
return 0;
}
error:
root [0]
Processing ss.C…
In module ‘Hist’:
/opt/root/include/TF1.h:732:104: error: type ‘double (double, const double )’ cannot be used prior to ‘::’ because it has no members
using Fnc_t = typename ROOT::Internal::GetFunctorType<decltype(ROOT::Internal::GetTheRightOp(&Func::operator()))>::type;
^
/opt/root/include/TF1.h:376:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder<double ()(double, const double )>::Build’ requested here
ROOT::Internal::TF1Builder::Build(this, f);
^
/home/geant4/root/SPEC/ss.C:32:22: note: in instantiation of function template specialization 'TF1::TF1<double ()(double, const double *)>’ requested here
TF1 nFunc = new TF1(“nFunc”, N, 0, 10, 2);
^
In module ‘MathCore’:
/opt/root/include/Math/ParamFunctor.h:124:22: error: cannot initialize a parameter of type ‘double’ with an rvalue of type 'int ’
return (f)((T)x, (double)p);
^~~~~
/opt/root/include/Math/ParamFunctor.h:94:45: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::FuncEvaluator<double ()(double, const double ), int>::EvalConst’ requested here
return FuncEvaluator<Func, EvalType>::EvalConst(fFunc,x,p);
^
/opt/root/include/Math/ParamFunctor.h:72:4: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::operator()’ requested here
ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
^
/opt/root/include/Math/ParamFunctor.h:303:17: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::ParamFunctorHandler’ requested here
fImpl(new ParamFunctorHandler<ParamFunctorTempl,Func>(f) )
^
/opt/root/include/TF1.h:734:66: note: in instantiation of function template specialization 'ROOT::Math::ParamFunctorTempl::ParamFunctorTempl<double ()(double, const double )>’ requested here
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl<Fnc_t>(ROOT::Math::ParamFunctorTempl<Fnc_t>(func)));
^
/opt/root/include/TF1.h:376:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder<double ()(double, const double )>::Build’ requested here
ROOT::Internal::TF1Builder::Build(this, f);
^
/home/geant4/root/SPEC/ss.C:32:22: note: in instantiation of function template specialization 'TF1::TF1<double ()(double, const double *)>’ requested here
TF1 *nFunc = new TF1(“nFunc”, N, 0, 10, 2);
^
In module ‘MathCore’:
/opt/root/include/Math/ParamFunctor.h:120:22: error: cannot initialize a parameter of type ‘double’ with an lvalue of type ‘int *’
return (f)(x, p);
^
/opt/root/include/Math/ParamFunctor.h:90:45: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::FuncEvaluator<double ()(double, const double ), int>::Eval’ requested here
return FuncEvaluator<Func, EvalType>::Eval(fFunc,x,p);
^
/opt/root/include/Math/ParamFunctor.h:72:4: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::operator()’ requested here
ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
^
/opt/root/include/Math/ParamFunctor.h:303:17: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl, double ()(double, const double )>::ParamFunctorHandler’ requested here
fImpl(new ParamFunctorHandler<ParamFunctorTempl,Func>(f) )
^
/opt/root/include/TF1.h:734:66: note: in instantiation of function template specialization 'ROOT::Math::ParamFunctorTempl::ParamFunctorTempl<double ()(double, const double )>’ requested here
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl<Fnc_t>(ROOT::Math::ParamFunctorTempl<Fnc_t>(func)));
^
/opt/root/include/TF1.h:376:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder<double ()(double, const double )>::Build’ requested here
ROOT::Internal::TF1Builder::Build(this, f);
^
/home/geant4/root/SPEC/ss.C:32:22: note: in instantiation of function template specialization 'TF1::TF1<double ()(double, const double *)>’ requested here
TF1 nFunc = new TF1(“nFunc”, N, 0, 10, 2);
^
In module ‘MathCore’:
/opt/root/include/Math/Functor.h:120:21: error: too few arguments to function call, expected 2, have 1
return fFunc(x);
~~~~~ ^
/opt/root/include/Math/Functor.h:90:4: note: in instantiation of member function 'ROOT::Math::FunctorHandler<ROOT::Math::Functor, double ()(const double *, const double )>::DoEval’ requested here
FunctorHandler(unsigned int dim, const Func & fun ) :
^
/opt/root/include/Math/Functor.h:428:17: note: in instantiation of member function 'ROOT::Math::FunctorHandler<ROOT::Math::Functor, double ()(const double *, const double )>::FunctorHandler’ requested here
fImpl(new FunctorHandler<Functor,Func>(dim,f) )
^
/home/geant4/root/SPEC/ss.C:39:25: note: in instantiation of function template specialization 'ROOT::Math::Functor::Functor<double ()(const double *, const double *)>’ requested here
ROOT::Math::Functor fcn(&TargetFunction, 2);