TGo4FitModelFormula.cxx

Go to the documentation of this file.
00001 // $Id: TGo4FitModelFormula.cxx 478 2009-10-29 12:26:09Z linev $
00002 //-----------------------------------------------------------------------
00003 //       The GSI Online Offline Object Oriented (Go4) Project
00004 //         Experiment Data Processing at EE department, GSI
00005 //-----------------------------------------------------------------------
00006 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
00007 //                     Planckstr. 1, 64291 Darmstadt, Germany
00008 // Contact:            http://go4.gsi.de
00009 //-----------------------------------------------------------------------
00010 // This software can be used under the license agreements as stated
00011 // in Go4License.txt file which is part of the distribution.
00012 //-----------------------------------------------------------------------
00013 
00014 #include "TGo4FitModelFormula.h"
00015 
00016 #include "Riostream.h"
00017 
00018 #include "TFormula.h"
00019 #include "TGo4FitParameter.h"
00020 
00021 TGo4FitModelFormula::TGo4FitModelFormula() : TGo4FitModel(),
00022    fxExpression(), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
00023 }
00024 
00025 TGo4FitModelFormula::TGo4FitModelFormula(const char* iName, const char* iExpressionStr, Int_t iNPars, Bool_t AddAmplitude) :
00026   TGo4FitModel(iName,"based on TFormula line shape",AddAmplitude),
00027   fxExpression(iExpressionStr), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
00028 
00029    for (Int_t n=0;n<iNPars;n++)
00030      NewParameter(GetExprParName(n),"formula parameter",0.);
00031 }
00032 
00033 
00034 TGo4FitModelFormula::~TGo4FitModelFormula() {
00035   CloseFormula();
00036 }
00037 
00038 Int_t TGo4FitModelFormula::GetNumberOfExprPar() {
00039    Int_t num = TGo4FitParsList::NumPars();
00040    if (GetAmplPar()) num--;
00041    return num;
00042 }
00043 
00044 TGo4FitParameter* TGo4FitModelFormula::GetExprPar(Int_t n) {
00045    if ((n<0) || (n>=GetNumberOfExprPar())) return 0;
00046    if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
00047    return GetPar(n);
00048 }
00049 
00050 Bool_t TGo4FitModelFormula::SetNumberOfExprPar(Int_t num) {
00051    Int_t oldnum = GetNumberOfExprPar();
00052    if ((num<0) || (oldnum==num)) return kFALSE;
00053    if(num<oldnum) {
00054       for(Int_t n=oldnum-1;n>=num;n--)
00055         RemovePar(GetExprPar(n)->GetName());
00056    } else {
00057       for(Int_t n=oldnum;n<num;n++)
00058         NewParameter(GetExprParName(n),"formula parameter",0.);
00059    }
00060    return kTRUE;
00061 }
00062 
00063 void TGo4FitModelFormula::SetPosParIndex(Int_t naxis, Int_t indx) {
00064   if (naxis<0) return;
00065   Int_t oldsize = fxPosIndex.GetSize();
00066   if (naxis>=oldsize) {
00067     fxPosIndex.Set(naxis+1);
00068     for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
00069   }
00070   fxPosIndex[naxis] = indx;
00071 }
00072 
00073 void TGo4FitModelFormula::SetWidthParIndex(Int_t naxis, Int_t indx) {
00074   if (naxis<0) return;
00075   Int_t oldsize = fxWidthIndex.GetSize();
00076   if (naxis>=oldsize) {
00077     fxWidthIndex.Set(naxis+1);
00078     for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
00079   }
00080   fxWidthIndex[naxis] = indx;
00081 }
00082 
00083 Int_t TGo4FitModelFormula::GetPosParIndex(Int_t naxis) {
00084    if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
00085    return GetParIndex(GetExprPar(fxPosIndex[naxis]));
00086 }
00087 
00088 Int_t TGo4FitModelFormula::GetWidthParIndex(Int_t naxis) {
00089    if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
00090    return GetParIndex(GetExprPar(fxWidthIndex[naxis]));
00091 }
00092 
00093 TString TGo4FitModelFormula::GetExprParName(Int_t n)
00094 {
00095    TString res;
00096    res.Form("Par%d",n);
00097    return res;
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   Par_ndim = 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 (Par_ndim) {
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      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     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 }

Generated on Thu Oct 28 15:54:12 2010 for Go4-Fitpackagev4.04-2 by  doxygen 1.5.1