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