GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
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
15
16#include <iostream>
17
18#ifndef _WINDOWS
19#include <dlfcn.h>
20#endif
21
22#include "TGo4FitParameter.h"
23
28
29TGo4FitModelFunction::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
38TGo4FitModelFunction::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
51
53{
55 fxLibraryName = "";
56 fxFunctionName = "";
57 fxUserFunction = iUserFunction;
58}
59
60void TGo4FitModelFunction::SetUserFunction(const char *iLibraryName, const char *iFunctionName)
61{
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
101void 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
114void 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
148Bool_t TGo4FitModelFunction::Initialize(Int_t UseBuffers)
149{
150 if (!LoadLibrary(kTRUE))
151 return kFALSE;
152 return TGo4FitModel::Initialize(UseBuffers);
153}
154
160
162{
163 if (!TGo4FitModel::BeforeEval(ndim))
164 return kFALSE;
165 return LoadLibrary(kFALSE);
166}
167
172
173Double_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
182Bool_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();
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)
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
225Bool_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
242void 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}
Double_t(* TUserFunction)(Double_t *, Double_t *)
TGo4FitParameter * GetAmplPar()
Return amplitude parameter object.
Int_t GetAmplIndex() const
Returns index of amplitude parameter.
TGo4FitParameter * NewParameter(const char *Name, const char *Title, Double_t iValue=0., Bool_t Fixed=kFALSE, Int_t AtIndx=-1)
Create new parameter with provided properties and add to parameters list.
void Finalize() override
Finalize object.
Bool_t LoadLibrary(Bool_t CloseFirst)
virtual TString GetFuncParName(Int_t n)
Int_t GetWidthParIndex(Int_t naxis) override
Return index of parameter (if exist), which represent width of model component for given axis.
TString fxFunctionName
Function name.
void SetUserFunction(TUserFunction iUserFunction)
Set address to user function.
virtual ~TGo4FitModelFunction()
Destroys TGo4FitModelFunction object.
TString fxLibraryName
String with library name, where function is situated.
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
TGo4FitParameter * GetFuncPar(Int_t n)
Int_t GetPosParIndex(Int_t naxis) override
Return index of parameter (if exist), which represent position of model for given axis.
void * fxLibrary
Pointer on loaded library descriptor.
void AfterEval() override
Clear buffers, which were created by BeforeEval() method.
TUserFunction fxUserFunction
Direct pointer on function.
Bool_t Initialize(Int_t UseBuffers=-1) override
Initialize object.
Bool_t SetNumberOfFuncPar(Int_t num)
Sets number of parameters, which can be used by function.
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Double_t UserFunction(Double_t *Coordinates, Double_t *Parameters) override
Another place, where user specific code can be placed for model values calculation.
Int_t GetNumberOfFuncPar()
Returns number of parameter, which can be used by function.
void Print(Option_t *option="") const override
TGo4FitModelFunction()
Default constructor.
Bool_t BeforeEval(Int_t ndim) override
Prepare object to evaluations.
virtual void Finalize()
Deletes all buffers, created during initialization.
virtual Bool_t Initialize(Int_t UseBuffers=-1)
Initialize model object.
void Print(Option_t *option="") const override
Print information about model object on standard output.
virtual Bool_t BeforeEval(Int_t ndim)
Prepares (if necessary) some intermediate variables to be able calculate values of model via EvalN() ...
TGo4FitModel()
Default constructor.
virtual void AfterEval()
Clear buffers, which were created by BeforeEval() method.
Model and data objects parameter.
Int_t GetParIndex(const TGo4FitParameter *par)
Return index of given parameter in list.
Bool_t RemovePar(const char *name)
Remove parameter from list with given name.
TGo4FitParameter * GetPar(Int_t n)
Return parameter according given index.
virtual Int_t NumPars()
Return number of parameters in list.