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