Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4FitModelFormula.h"
00015
00016 #include "Riostream.h"
00017 #include "TFormula.h"
00018 #include "TGo4FitParameter.h"
00019
00020 TGo4FitModelFormula::TGo4FitModelFormula() : TGo4FitModel(),
00021 fxExpression(), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
00022 }
00023
00024 TGo4FitModelFormula::TGo4FitModelFormula(const char* iName, const char* iExpressionStr, Int_t iNPars, Bool_t AddAmplitude) :
00025 TGo4FitModel(iName,"based on TFormula line shape",AddAmplitude),
00026 fxExpression(iExpressionStr), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
00027
00028 for (Int_t n=0;n<iNPars;n++)
00029 NewParameter(GetExprParName(n),"formula parameter",0.);
00030 }
00031
00032
00033 TGo4FitModelFormula::~TGo4FitModelFormula() {
00034 CloseFormula();
00035 }
00036
00037 Int_t TGo4FitModelFormula::GetNumberOfExprPar() {
00038 Int_t num = TGo4FitParsList::NumPars();
00039 if (GetAmplPar()) num--;
00040 return num;
00041 }
00042
00043 TGo4FitParameter* TGo4FitModelFormula::GetExprPar(Int_t n) {
00044 if ((n<0) || (n>=GetNumberOfExprPar())) return 0;
00045 if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
00046 return GetPar(n);
00047 }
00048
00049 Bool_t TGo4FitModelFormula::SetNumberOfExprPar(Int_t num) {
00050 Int_t oldnum = GetNumberOfExprPar();
00051 if ((num<0) || (oldnum==num)) return kFALSE;
00052 if(num<oldnum) {
00053 for(Int_t n=oldnum-1;n>=num;n--)
00054 RemovePar(GetExprPar(n)->GetName());
00055 } else {
00056 for(Int_t n=oldnum;n<num;n++)
00057 NewParameter(GetExprParName(n),"formula parameter",0.);
00058 }
00059 return kTRUE;
00060 }
00061
00062 void TGo4FitModelFormula::SetPosParIndex(Int_t naxis, Int_t indx) {
00063 if (naxis<0) return;
00064 Int_t oldsize = fxPosIndex.GetSize();
00065 if (naxis>=oldsize) {
00066 fxPosIndex.Set(naxis+1);
00067 for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
00068 }
00069 fxPosIndex[naxis] = indx;
00070 }
00071
00072 void TGo4FitModelFormula::SetWidthParIndex(Int_t naxis, Int_t indx) {
00073 if (naxis<0) return;
00074 Int_t oldsize = fxWidthIndex.GetSize();
00075 if (naxis>=oldsize) {
00076 fxWidthIndex.Set(naxis+1);
00077 for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
00078 }
00079 fxWidthIndex[naxis] = indx;
00080 }
00081
00082 Int_t TGo4FitModelFormula::GetPosParIndex(Int_t naxis) {
00083 if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
00084 return GetParIndex(GetExprPar(fxPosIndex[naxis]));
00085 }
00086
00087 Int_t TGo4FitModelFormula::GetWidthParIndex(Int_t naxis) {
00088 if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
00089 return GetParIndex(GetExprPar(fxWidthIndex[naxis]));
00090 }
00091
00092 TString TGo4FitModelFormula::GetExprParName(Int_t n)
00093 {
00094 TString res;
00095 res.Form("Par%d",n);
00096 return res;
00097 }
00098
00099 Bool_t TGo4FitModelFormula::Initialize(Int_t UseBuffers) {
00100 if (!CompileFormula()) return kFALSE;
00101 CloseFormula();
00102 return TGo4FitModel::Initialize(UseBuffers);
00103 }
00104
00105 Bool_t TGo4FitModelFormula::BeforeEval(Int_t ndim) {
00106 if (!CompileFormula()) return kFALSE;
00107 Par_ndim = ndim;
00108 for(Int_t n=0;n<NumPars();n++)
00109 fxFormula->SetParameter(n, GetPar(n)->GetValue());
00110 return kTRUE;
00111 }
00112
00113 Double_t TGo4FitModelFormula::EvalN(const Double_t* v) {
00114 switch (Par_ndim) {
00115 case 0: return 0.;
00116 case 1: return fxFormula->Eval(v[0]);
00117 case 2: return fxFormula->Eval(v[0],v[1]);
00118 case 3: return fxFormula->Eval(v[0],v[1],v[2]);
00119 default: return fxFormula->EvalPar(v, 0);
00120 }
00121 }
00122
00123 void TGo4FitModelFormula::AfterEval() {
00124 CloseFormula();
00125 }
00126
00127 void TGo4FitModelFormula::Finalize() {
00128 CloseFormula();
00129 TGo4FitModel::Finalize();
00130 }
00131
00132 void TGo4FitModelFormula::Print(Option_t* option) const {
00133 TGo4FitModel::Print(option);
00134 std::cout << " Expression = " << *fxExpression << std::endl;
00135 for (Int_t naxis=0;naxis<fxPosIndex.GetSize();naxis++) {
00136 TGo4FitParameter* par = ((TGo4FitModelFormula*) this)->GetExprPar(fxPosIndex[naxis]);
00137 if (par)
00138 std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
00139 }
00140 for (Int_t naxis=0;naxis<fxWidthIndex.GetSize();naxis++) {
00141 TGo4FitParameter* par = ((TGo4FitModelFormula*) this)->GetExprPar(fxWidthIndex[naxis]);
00142 if (par)
00143 std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
00144 }
00145 }
00146
00147 Bool_t TGo4FitModelFormula::CompileFormula()
00148 {
00149 CloseFormula();
00150
00151 TString Expr(fxExpression);
00152 for (Int_t n=0;n<NumPars();n++) {
00153 TString str;
00154 str.Form("[%d]",n);
00155 Expr.ReplaceAll(GetParName(n), str);
00156 }
00157
00158 fxFormula = new TFormula("Expression", Expr);
00159
00160 Int_t err = fxFormula->Compile(Expr);
00161 if (err!=0) {
00162 std::cerr << "Error in formula: " << fxExpression.Data() << " code " << err << std::endl;
00163 CloseFormula();
00164 return kFALSE;
00165 }
00166 return kTRUE;
00167 }
00168
00169 void TGo4FitModelFormula::CloseFormula() {
00170 if (fxFormula) { delete fxFormula; fxFormula = 0; }
00171 }