GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4FitModelFunction.cxx
Go to the documentation of this file.
1 // $Id$
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 fuer 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 <iostream>
17 
18 #ifndef _WINDOWS
19 #include <dlfcn.h>
20 #endif
21 
22 #include "TGo4FitParameter.h"
23 
25  : TGo4FitModel(), fxLibraryName(), fxFunctionName(), fxUserFunction(nullptr), fxLibrary(nullptr)
26 {
27 }
28 
29 TGo4FitModelFunction::TGo4FitModelFunction(const char *iName, TUserFunction iUserFunction, Int_t iNPars,
30  Bool_t AddAmplitude)
31  : TGo4FitModel(iName, "Model, using user function", AddAmplitude), fxLibraryName(), fxFunctionName(), fxPosIndex(),
32  fxWidthIndex(), fxUserFunction(iUserFunction), fxLibrary(nullptr)
33 {
34  for (Int_t n = 0; n < iNPars; n++)
35  NewParameter(GetFuncParName(n), "user parameter", 0.);
36 }
37 
38 TGo4FitModelFunction::TGo4FitModelFunction(const char *iName, const char *iLibraryName, const char *iFunctionName,
39  Int_t iNPars, Bool_t AddAmplitude)
40  : TGo4FitModel(iName, "Model, using user function", AddAmplitude), fxLibraryName(iLibraryName),
41  fxFunctionName(iFunctionName), fxPosIndex(), fxWidthIndex(), fxUserFunction(nullptr), fxLibrary(nullptr)
42 {
43  for (Int_t n = 0; n < iNPars; n++)
44  NewParameter(GetFuncParName(n), "user parameter", 0.);
45 }
46 
48 {
49  CloseLibrary();
50 }
51 
53 {
54  CloseLibrary();
55  fxLibraryName = "";
56  fxFunctionName = "";
57  fxUserFunction = iUserFunction;
58 }
59 
60 void TGo4FitModelFunction::SetUserFunction(const char *iLibraryName, const char *iFunctionName)
61 {
62  CloseLibrary();
63  fxLibraryName = iLibraryName;
64  fxFunctionName = iFunctionName;
65  fxUserFunction = nullptr;
66 }
67 
69 {
70  Int_t num = TGo4FitParsList::NumPars();
71  if (GetAmplPar())
72  num--;
73  return num;
74 }
75 
77 {
78  if ((n < 0) || (n >= GetNumberOfFuncPar()))
79  return nullptr;
80  if ((GetAmplIndex() >= 0) && (n >= GetAmplIndex()))
81  n++;
82  return GetPar(n);
83 }
84 
86 {
87  Int_t oldnum = GetNumberOfFuncPar();
88  if ((num < 0) || (oldnum == num))
89  return kFALSE;
90  if (num < oldnum) {
91  for (Int_t n = oldnum - 1; n >= num; n--)
92  RemovePar(GetFuncPar(n)->GetName());
93  } else {
94  for (Int_t n = oldnum; n < num; n++)
95  NewParameter(GetFuncParName(n), "user parameter", 0.);
96  }
97 
98  return kTRUE;
99 }
100 
101 void TGo4FitModelFunction::SetPosParIndex(Int_t naxis, Int_t indx)
102 {
103  if (naxis < 0)
104  return;
105  Int_t oldsize = fxPosIndex.GetSize();
106  if (naxis >= oldsize) {
107  fxPosIndex.Set(naxis + 1);
108  for (Int_t n = oldsize; n < fxPosIndex.GetSize(); n++)
109  fxPosIndex[n] = -1;
110  }
111  fxPosIndex[naxis] = indx;
112 }
113 
114 void TGo4FitModelFunction::SetWidthParIndex(Int_t naxis, Int_t indx)
115 {
116  if (naxis < 0)
117  return;
118  Int_t oldsize = fxWidthIndex.GetSize();
119  if (naxis >= oldsize) {
120  fxWidthIndex.Set(naxis + 1);
121  for (Int_t n = oldsize; n < fxWidthIndex.GetSize(); n++)
122  fxWidthIndex[n] = -1;
123  }
124  fxWidthIndex[naxis] = indx;
125 }
126 
128 {
129  if ((naxis < 0) || (naxis >= fxPosIndex.GetSize()))
130  return -1;
131  return GetParIndex(GetFuncPar(fxPosIndex[naxis]));
132 }
133 
135 {
136  if ((naxis < 0) || (naxis >= fxWidthIndex.GetSize()))
137  return -1;
138  return GetParIndex(GetFuncPar(fxWidthIndex[naxis]));
139 }
140 
142 {
143  TString res;
144  res.Form("Par%d", n);
145  return res;
146 }
147 
148 Bool_t TGo4FitModelFunction::Initialize(Int_t UseBuffers)
149 {
150  if (!LoadLibrary(kTRUE))
151  return kFALSE;
152  return TGo4FitModel::Initialize(UseBuffers);
153 }
154 
156 {
157  CloseLibrary();
159 }
160 
162 {
163  if (!TGo4FitModel::BeforeEval(ndim))
164  return kFALSE;
165  return LoadLibrary(kFALSE);
166 }
167 
169 {
171 }
172 
173 Double_t TGo4FitModelFunction::UserFunction(Double_t *Coordinates, Double_t *Parameters)
174 {
175  if (fxUserFunction)
176  return (*fxUserFunction)(Coordinates, Parameters);
177  return 0.;
178 }
179 
180 #ifndef _WINDOWS
181 
182 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
183 {
184  if (fxLibraryName.IsNull() || fxFunctionName.IsNull()) {
185  if (!fxUserFunction)
186  std::cout << "TGo4FitModelFunction: user function not set" << std::endl;
187  return fxUserFunction != nullptr;
188  }
189 
190  if (CloseFirst)
191  CloseLibrary();
192  if (fxLibrary && fxUserFunction)
193  return kTRUE;
194 
195  fxLibrary = dlopen(fxLibraryName, RTLD_NOW | RTLD_GLOBAL);
196  if (!fxLibrary) {
197  std::cout << " TGo4FitModelFunction: failed to open " << fxLibraryName << ", " << dlerror() << std::endl;
198  return kFALSE;
199  }
201  if (!fxUserFunction)
202  fxUserFunction = (TUserFunction)dlsym(fxLibrary, fxFunctionName + "__FPdT0");
203  if (!fxUserFunction)
205  if (!fxUserFunction)
207  if (!fxUserFunction) {
208  std::cout << " TGo4FitModelFunction: failed to find " << fxFunctionName << ", " << dlerror() << std::endl;
209  CloseLibrary();
210  return kFALSE;
211  }
212  return kTRUE;
213 }
214 
216 {
217  if (fxLibrary) {
218  dlclose(fxLibrary);
219  fxLibrary = nullptr;
220  }
221 }
222 
223 #else
224 
225 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
226 {
227  // here must be win32 LoadLibrary() and GetProcAddress() calls
228  // when required, must be implemented
229 
230  fxLibrary = nullptr;
231  return kFALSE;
232 }
233 
235 {
236  // here must be FreeLibrary() calls
237  fxLibrary = nullptr;
238 }
239 
240 #endif
241 
242 void TGo4FitModelFunction::Print(Option_t *option) const
243 {
244  TGo4FitModel::Print(option);
245  if ((fxLibraryName.Length() > 0) || (fxFunctionName.Length() > 0))
246  std::cout << "Function " << fxFunctionName << " in " << fxLibraryName << std::endl;
247  else
248  std::cout << " Pointer on function " << fxUserFunction << std::endl;
249  for (Int_t naxis = 0; naxis < fxPosIndex.GetSize(); naxis++) {
250  TGo4FitParameter *par = ((TGo4FitModelFunction *)this)->GetFuncPar(fxPosIndex[naxis]);
251  if (par)
252  std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
253  }
254  for (Int_t naxis = 0; naxis < fxWidthIndex.GetSize(); naxis++) {
255  TGo4FitParameter *par = ((TGo4FitModelFunction *)this)->GetFuncPar(fxWidthIndex[naxis]);
256  if (par)
257  std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
258  }
259 }
Int_t GetWidthParIndex(Int_t naxis) override
void SetUserFunction(TUserFunction iUserFunction)
virtual void Finalize()
Bool_t SetNumberOfFuncPar(Int_t num)
virtual TString GetFuncParName(Int_t n)
TGo4FitParameter * GetPar(Int_t n)
Int_t GetAmplIndex() const
Bool_t BeforeEval(Int_t ndim) override
TGo4FitParameter * NewParameter(const char *Name, const char *Title, Double_t iValue=0., Bool_t Fixed=kFALSE, Int_t AtIndx=-1)
void Print(Option_t *option="") const override
Bool_t LoadLibrary(Bool_t CloseFirst)
Double_t UserFunction(Double_t *Coordinates, Double_t *Parameters) override
virtual Int_t NumPars()
Int_t GetPosParIndex(Int_t naxis) override
Bool_t Initialize(Int_t UseBuffers=-1) override
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Double_t(* TUserFunction)(Double_t *, Double_t *)
Bool_t RemovePar(const char *name)
TGo4FitParameter * GetFuncPar(Int_t n)
void Print(Option_t *option="") const override
virtual Bool_t Initialize(Int_t UseBuffers=-1)
virtual Bool_t BeforeEval(Int_t ndim)
virtual void AfterEval()
Definition: TGo4FitModel.h:253
Int_t GetParIndex(const TGo4FitParameter *par)
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
TGo4FitParameter * GetAmplPar()