TGo4FitModelFunction.cxx

Go to the documentation of this file.
00001 // $Id: TGo4FitModelFunction.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 "TGo4FitModelFunction.h"
00015 
00016 #include "Riostream.h"
00017 
00018 #ifndef _WINDOWS
00019 #include <dlfcn.h>
00020 #endif
00021 
00022 #include "TString.h"
00023 
00024 #include "TGo4FitParameter.h"
00025 
00026 TGo4FitModelFunction::TGo4FitModelFunction() : TGo4FitModel(), fxLibraryName(), fxFunctionName(), fxUserFunction(0), fxLibrary(0)
00027 {
00028 }
00029 
00030 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, TUserFunction iUserFunction, Int_t iNPars, Bool_t AddAmplitude) :
00031    TGo4FitModel(iName,"Model, using user function",AddAmplitude),
00032    fxLibraryName(), fxFunctionName(), fxPosIndex(), fxWidthIndex(),
00033    fxUserFunction(iUserFunction), fxLibrary(0)
00034 {
00035    for (Int_t n=0;n<iNPars;n++)
00036        NewParameter(GetFuncParName(n),"user parameter",0.);
00037 }
00038 
00039 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, const char* iLibraryName, const char* iFunctionName, Int_t iNPars, Bool_t AddAmplitude) :
00040    TGo4FitModel(iName,"Model, using user function",AddAmplitude),
00041    fxLibraryName(iLibraryName), fxFunctionName(iFunctionName), fxPosIndex(), fxWidthIndex(),
00042    fxUserFunction(0), fxLibrary(0)
00043 {
00044    for (Int_t n=0;n<iNPars;n++)
00045        NewParameter(GetFuncParName(n),"user parameter",0.);
00046 }
00047 
00048 TGo4FitModelFunction::~TGo4FitModelFunction()
00049 {
00050    CloseLibrary();
00051 }
00052 
00053 void TGo4FitModelFunction::SetUserFunction(TUserFunction iUserFunction)
00054 {
00055    CloseLibrary();
00056    fxLibraryName = "";
00057    fxFunctionName = "";
00058    fxUserFunction = iUserFunction;
00059 }
00060 
00061 void TGo4FitModelFunction::SetUserFunction(const char* iLibraryName, const char* iFunctionName)
00062 {
00063    CloseLibrary();
00064    fxLibraryName = iLibraryName;
00065    fxFunctionName = iFunctionName;
00066    fxUserFunction = 0;
00067 }
00068 
00069 Int_t TGo4FitModelFunction::GetNumberOfFuncPar()
00070 {
00071    Int_t num = TGo4FitParsList::NumPars();
00072    if (GetAmplPar()) num--;
00073    return num;
00074 }
00075 
00076 TGo4FitParameter* TGo4FitModelFunction::GetFuncPar(Int_t n)
00077 {
00078    if ((n<0) || (n>=GetNumberOfFuncPar())) return 0;
00079    if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
00080    return GetPar(n);
00081 }
00082 
00083 Bool_t TGo4FitModelFunction::SetNumberOfFuncPar(Int_t num)
00084 {
00085    Int_t oldnum = GetNumberOfFuncPar();
00086    if ((num<0) || (oldnum==num)) return kFALSE;
00087    if(num<oldnum) {
00088       for(Int_t n=oldnum-1;n>=num;n--)
00089         RemovePar(GetFuncPar(n)->GetName());
00090    } else {
00091       for(Int_t n=oldnum;n<num;n++)
00092         NewParameter(GetFuncParName(n),"user parameter",0.);
00093    }
00094 
00095    return kTRUE;
00096 }
00097 
00098 void TGo4FitModelFunction::SetPosParIndex(Int_t naxis, Int_t indx)
00099 {
00100   if (naxis<0) return;
00101   Int_t oldsize = fxPosIndex.GetSize();
00102   if (naxis>=oldsize) {
00103     fxPosIndex.Set(naxis+1);
00104     for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
00105   }
00106   fxPosIndex[naxis] = indx;
00107 }
00108 
00109 void TGo4FitModelFunction::SetWidthParIndex(Int_t naxis, Int_t indx)
00110 {
00111   if (naxis<0) return;
00112   Int_t oldsize = fxWidthIndex.GetSize();
00113   if (naxis>=oldsize) {
00114     fxWidthIndex.Set(naxis+1);
00115     for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
00116   }
00117   fxWidthIndex[naxis] = indx;
00118 }
00119 
00120 Int_t TGo4FitModelFunction::GetPosParIndex(Int_t naxis)
00121 {
00122    if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
00123    return GetParIndex(GetFuncPar(fxPosIndex[naxis]));
00124 }
00125 
00126 Int_t TGo4FitModelFunction::GetWidthParIndex(Int_t naxis)
00127 {
00128    if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
00129    return GetParIndex(GetFuncPar(fxWidthIndex[naxis]));
00130 }
00131 
00132 TString TGo4FitModelFunction::GetFuncParName(Int_t n)
00133 {
00134    TString res;
00135    res.Form("Par%d",n);
00136    return res;
00137 }
00138 
00139 Bool_t TGo4FitModelFunction::Initialize(Int_t UseBuffers)
00140 {
00141    if (!LoadLibrary(kTRUE)) return kFALSE;
00142    return TGo4FitModel::Initialize(UseBuffers);
00143 }
00144 
00145 void TGo4FitModelFunction::Finalize()
00146 {
00147    CloseLibrary();
00148    TGo4FitModel::Finalize();
00149 }
00150 
00151 Bool_t TGo4FitModelFunction::BeforeEval(Int_t ndim)
00152 {
00153    if (!TGo4FitModel::BeforeEval(ndim)) return kFALSE;
00154    return LoadLibrary(kFALSE);
00155 }
00156 
00157 void TGo4FitModelFunction::AfterEval()
00158 {
00159    TGo4FitModel::AfterEval();
00160 }
00161 
00162 Double_t TGo4FitModelFunction::UserFunction(Double_t* Coordinates, Double_t* Parameters)
00163 {
00164    if (fxUserFunction) return (*fxUserFunction)(Coordinates,Parameters);
00165                   else return 0.;
00166 }
00167 
00168 #ifndef _WINDOWS
00169 
00170 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
00171 {
00172    if ((fxLibraryName.Length()==0) || (fxFunctionName.Length()==0)) {
00173      if (fxUserFunction==0) cout << "TGo4FitModelFunction: user function not set" << endl;
00174      return (fxUserFunction!=0);
00175    }
00176 
00177    if (CloseFirst) CloseLibrary();
00178    if ((fxLibrary!=0) && (fxUserFunction!=0)) return kTRUE;
00179 
00180    fxLibrary = dlopen(fxLibraryName, RTLD_NOW | RTLD_GLOBAL);
00181    if (fxLibrary==0) {
00182       cout << " TGo4FitModelFunction: failed to open " << fxLibraryName << ",  " << dlerror() << endl;
00183       return kFALSE;
00184    }
00185    fxUserFunction = (TUserFunction) dlsym(fxLibrary, fxFunctionName);
00186    if(fxUserFunction==0) fxUserFunction = (TUserFunction) dlsym(fxLibrary, fxFunctionName+"__FPdT0");
00187    if(fxUserFunction==0) fxUserFunction = (TUserFunction) dlsym(fxLibrary, fxFunctionName+"__");
00188    if(fxUserFunction==0) fxUserFunction = (TUserFunction) dlsym(fxLibrary, fxFunctionName+"_");
00189    if(fxUserFunction==0) {
00190        cout << " TGo4FitModelFunction: failed to find " << fxFunctionName << ",  " << dlerror() << endl;
00191        CloseLibrary();
00192        return kFALSE;
00193    }
00194    return kTRUE;
00195 }
00196 
00197 void TGo4FitModelFunction::CloseLibrary()
00198 {
00199    if (fxLibrary!=0) {
00200       dlclose(fxLibrary);
00201       fxLibrary = 0;
00202    }
00203 }
00204 
00205 #else
00206 
00207 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
00208 {
00209    // here must be win32 LoadLibrary() and GetProcAddress() calls
00210    // when required, must be implemented
00211 
00212    fxLibrary = 0;
00213    return kFALSE;
00214 }
00215 
00216 void TGo4FitModelFunction::CloseLibrary()
00217 {
00218    // here must be FreeLibrary() calls
00219    fxLibrary = 0;
00220 }
00221 
00222 #endif
00223 
00224 void TGo4FitModelFunction::Print(Option_t* option) const
00225 {
00226    TGo4FitModel::Print(option);
00227    if ((fxLibraryName.Length()>0) || (fxFunctionName.Length()>0))
00228      cout << "Function " << fxFunctionName << "  in " << fxLibraryName << endl;
00229    else
00230      cout << " Pointer on function " << fxUserFunction << endl;
00231    for (Int_t naxis=0;naxis<fxPosIndex.GetSize();naxis++) {
00232      TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxPosIndex[naxis]);
00233      if (par)
00234         cout << "  Position on " << naxis << " axis is " << par->GetName() << endl;
00235    }
00236    for (Int_t naxis=0;naxis<fxWidthIndex.GetSize();naxis++) {
00237      TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxWidthIndex[naxis]);
00238      if (par)
00239         cout << "  Width on " << naxis << " axis is " << par->GetName() << endl;
00240    }
00241 }

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