GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitModelFunction.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitModelFunction.cxx 933 2013-01-29 15:27:58Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4FitModelFunction.h"
15 
16 #include "Riostream.h"
17 
18 #ifndef _WINDOWS
19 #include <dlfcn.h>
20 #endif
21 
22 #include "TString.h"
23 
24 #include "TGo4FitParameter.h"
25 
26 TGo4FitModelFunction::TGo4FitModelFunction() : TGo4FitModel(), fxLibraryName(), fxFunctionName(), fxUserFunction(0), fxLibrary(0)
27 {
28 }
29 
30 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, TUserFunction iUserFunction, Int_t iNPars, Bool_t AddAmplitude) :
31  TGo4FitModel(iName,"Model, using user function",AddAmplitude),
32  fxLibraryName(), fxFunctionName(), fxPosIndex(), fxWidthIndex(),
33  fxUserFunction(iUserFunction), fxLibrary(0)
34 {
35  for (Int_t n=0;n<iNPars;n++)
36  NewParameter(GetFuncParName(n),"user parameter",0.);
37 }
38 
39 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, const char* iLibraryName, const char* iFunctionName, Int_t iNPars, Bool_t AddAmplitude) :
40  TGo4FitModel(iName,"Model, using user function",AddAmplitude),
41  fxLibraryName(iLibraryName), fxFunctionName(iFunctionName), fxPosIndex(), fxWidthIndex(),
42  fxUserFunction(0), fxLibrary(0)
43 {
44  for (Int_t n=0;n<iNPars;n++)
45  NewParameter(GetFuncParName(n),"user parameter",0.);
46 }
47 
49 {
50  CloseLibrary();
51 }
52 
54 {
55  CloseLibrary();
56  fxLibraryName = "";
57  fxFunctionName = "";
58  fxUserFunction = iUserFunction;
59 }
60 
61 void TGo4FitModelFunction::SetUserFunction(const char* iLibraryName, const char* iFunctionName)
62 {
63  CloseLibrary();
64  fxLibraryName = iLibraryName;
65  fxFunctionName = iFunctionName;
66  fxUserFunction = 0;
67 }
68 
70 {
71  Int_t num = TGo4FitParsList::NumPars();
72  if (GetAmplPar()) num--;
73  return num;
74 }
75 
77 {
78  if ((n<0) || (n>=GetNumberOfFuncPar())) return 0;
79  if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
80  return GetPar(n);
81 }
82 
84 {
85  Int_t oldnum = GetNumberOfFuncPar();
86  if ((num<0) || (oldnum==num)) return kFALSE;
87  if(num<oldnum) {
88  for(Int_t n=oldnum-1;n>=num;n--)
89  RemovePar(GetFuncPar(n)->GetName());
90  } else {
91  for(Int_t n=oldnum;n<num;n++)
92  NewParameter(GetFuncParName(n),"user parameter",0.);
93  }
94 
95  return kTRUE;
96 }
97 
98 void TGo4FitModelFunction::SetPosParIndex(Int_t naxis, Int_t indx)
99 {
100  if (naxis<0) return;
101  Int_t oldsize = fxPosIndex.GetSize();
102  if (naxis>=oldsize) {
103  fxPosIndex.Set(naxis+1);
104  for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
105  }
106  fxPosIndex[naxis] = indx;
107 }
108 
109 void TGo4FitModelFunction::SetWidthParIndex(Int_t naxis, Int_t indx)
110 {
111  if (naxis<0) return;
112  Int_t oldsize = fxWidthIndex.GetSize();
113  if (naxis>=oldsize) {
114  fxWidthIndex.Set(naxis+1);
115  for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
116  }
117  fxWidthIndex[naxis] = indx;
118 }
119 
121 {
122  if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
123  return GetParIndex(GetFuncPar(fxPosIndex[naxis]));
124 }
125 
127 {
128  if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
129  return GetParIndex(GetFuncPar(fxWidthIndex[naxis]));
130 }
131 
133 {
134  TString res;
135  res.Form("Par%d",n);
136  return res;
137 }
138 
139 Bool_t TGo4FitModelFunction::Initialize(Int_t UseBuffers)
140 {
141  if (!LoadLibrary(kTRUE)) return kFALSE;
142  return TGo4FitModel::Initialize(UseBuffers);
143 }
144 
146 {
147  CloseLibrary();
149 }
150 
152 {
153  if (!TGo4FitModel::BeforeEval(ndim)) return kFALSE;
154  return LoadLibrary(kFALSE);
155 }
156 
158 {
160 }
161 
162 Double_t TGo4FitModelFunction::UserFunction(Double_t* Coordinates, Double_t* Parameters)
163 {
164  if (fxUserFunction) return (*fxUserFunction)(Coordinates,Parameters);
165  else return 0.;
166 }
167 
168 #ifndef _WINDOWS
169 
170 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
171 {
172  if ((fxLibraryName.Length()==0) || (fxFunctionName.Length()==0)) {
173  if (fxUserFunction==0) std::cout << "TGo4FitModelFunction: user function not set" << std::endl;
174  return (fxUserFunction!=0);
175  }
176 
177  if (CloseFirst) CloseLibrary();
178  if ((fxLibrary!=0) && (fxUserFunction!=0)) return kTRUE;
179 
180  fxLibrary = dlopen(fxLibraryName, RTLD_NOW | RTLD_GLOBAL);
181  if (fxLibrary==0) {
182  std::cout << " TGo4FitModelFunction: failed to open " << fxLibraryName << ", " << dlerror() << std::endl;
183  return kFALSE;
184  }
189  if(fxUserFunction==0) {
190  std::cout << " TGo4FitModelFunction: failed to find " << fxFunctionName << ", " << dlerror() << std::endl;
191  CloseLibrary();
192  return kFALSE;
193  }
194  return kTRUE;
195 }
196 
198 {
199  if (fxLibrary!=0) {
200  dlclose(fxLibrary);
201  fxLibrary = 0;
202  }
203 }
204 
205 #else
206 
207 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
208 {
209  // here must be win32 LoadLibrary() and GetProcAddress() calls
210  // when required, must be implemented
211 
212  fxLibrary = 0;
213  return kFALSE;
214 }
215 
217 {
218  // here must be FreeLibrary() calls
219  fxLibrary = 0;
220 }
221 
222 #endif
223 
224 void TGo4FitModelFunction::Print(Option_t* option) const
225 {
226  TGo4FitModel::Print(option);
227  if ((fxLibraryName.Length()>0) || (fxFunctionName.Length()>0))
228  std::cout << "Function " << fxFunctionName << " in " << fxLibraryName << std::endl;
229  else
230  std::cout << " Pointer on function " << fxUserFunction << std::endl;
231  for (Int_t naxis=0;naxis<fxPosIndex.GetSize();naxis++) {
232  TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxPosIndex[naxis]);
233  if (par)
234  std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
235  }
236  for (Int_t naxis=0;naxis<fxWidthIndex.GetSize();naxis++) {
237  TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxWidthIndex[naxis]);
238  if (par)
239  std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
240  }
241 }
virtual Bool_t Initialize(Int_t UseBuffers=-1)
void SetUserFunction(TUserFunction iUserFunction)
virtual void Finalize()
Bool_t SetNumberOfFuncPar(Int_t num)
Double_t(* TUserFunction)(Double_t *, Double_t *)
virtual TString GetFuncParName(Int_t n)
TGo4FitParameter * GetPar(Int_t n)
TGo4FitParameter * NewParameter(const char *Name, const char *Title, Double_t iValue=0., Bool_t Fixed=kFALSE, Int_t AtIndx=-1)
virtual Int_t GetWidthParIndex(Int_t naxis)
virtual Bool_t BeforeEval(Int_t ndim)
Bool_t LoadLibrary(Bool_t CloseFirst)
virtual Int_t NumPars()
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Bool_t RemovePar(const char *name)
TGo4FitParameter * GetFuncPar(Int_t n)
virtual void Print(Option_t *option) const
virtual void Print(Option_t *option) const
virtual Bool_t Initialize(Int_t UseBuffers=-1)
virtual Bool_t BeforeEval(Int_t ndim)
virtual void AfterEval()
Definition: TGo4FitModel.h:253
virtual Int_t GetPosParIndex(Int_t naxis)
Int_t GetParIndex(const TGo4FitParameter *par)
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
virtual Double_t UserFunction(Double_t *Coordinates, Double_t *Parameters)
TGo4FitParameter * GetAmplPar()