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