GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitModelFunction.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitModelFunction.cxx 2749 2020-04-16 10:05:15Z 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 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 
24 TGo4FitModelFunction::TGo4FitModelFunction() : TGo4FitModel(), fxLibraryName(), fxFunctionName(), fxUserFunction(0), fxLibrary(0)
25 {
26 }
27 
28 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, TUserFunction iUserFunction, Int_t iNPars, Bool_t AddAmplitude) :
29  TGo4FitModel(iName,"Model, using user function",AddAmplitude),
30  fxLibraryName(), fxFunctionName(), fxPosIndex(), fxWidthIndex(),
31  fxUserFunction(iUserFunction), fxLibrary(0)
32 {
33  for (Int_t n=0;n<iNPars;n++)
34  NewParameter(GetFuncParName(n),"user parameter",0.);
35 }
36 
37 TGo4FitModelFunction::TGo4FitModelFunction(const char* iName, const char* iLibraryName, const char* iFunctionName, Int_t iNPars, Bool_t AddAmplitude) :
38  TGo4FitModel(iName,"Model, using user function",AddAmplitude),
39  fxLibraryName(iLibraryName), fxFunctionName(iFunctionName), fxPosIndex(), fxWidthIndex(),
40  fxUserFunction(0), fxLibrary(0)
41 {
42  for (Int_t n=0;n<iNPars;n++)
43  NewParameter(GetFuncParName(n),"user parameter",0.);
44 }
45 
47 {
48  CloseLibrary();
49 }
50 
52 {
53  CloseLibrary();
54  fxLibraryName = "";
55  fxFunctionName = "";
56  fxUserFunction = iUserFunction;
57 }
58 
59 void TGo4FitModelFunction::SetUserFunction(const char* iLibraryName, const char* iFunctionName)
60 {
61  CloseLibrary();
62  fxLibraryName = iLibraryName;
63  fxFunctionName = iFunctionName;
64  fxUserFunction = 0;
65 }
66 
68 {
69  Int_t num = TGo4FitParsList::NumPars();
70  if (GetAmplPar()) num--;
71  return num;
72 }
73 
75 {
76  if ((n<0) || (n>=GetNumberOfFuncPar())) return 0;
77  if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
78  return GetPar(n);
79 }
80 
82 {
83  Int_t oldnum = GetNumberOfFuncPar();
84  if ((num<0) || (oldnum==num)) return kFALSE;
85  if(num<oldnum) {
86  for(Int_t n=oldnum-1;n>=num;n--)
87  RemovePar(GetFuncPar(n)->GetName());
88  } else {
89  for(Int_t n=oldnum;n<num;n++)
90  NewParameter(GetFuncParName(n),"user parameter",0.);
91  }
92 
93  return kTRUE;
94 }
95 
96 void TGo4FitModelFunction::SetPosParIndex(Int_t naxis, Int_t indx)
97 {
98  if (naxis<0) return;
99  Int_t oldsize = fxPosIndex.GetSize();
100  if (naxis>=oldsize) {
101  fxPosIndex.Set(naxis+1);
102  for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
103  }
104  fxPosIndex[naxis] = indx;
105 }
106 
107 void TGo4FitModelFunction::SetWidthParIndex(Int_t naxis, Int_t indx)
108 {
109  if (naxis<0) return;
110  Int_t oldsize = fxWidthIndex.GetSize();
111  if (naxis>=oldsize) {
112  fxWidthIndex.Set(naxis+1);
113  for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
114  }
115  fxWidthIndex[naxis] = indx;
116 }
117 
119 {
120  if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
121  return GetParIndex(GetFuncPar(fxPosIndex[naxis]));
122 }
123 
125 {
126  if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
127  return GetParIndex(GetFuncPar(fxWidthIndex[naxis]));
128 }
129 
131 {
132  TString res;
133  res.Form("Par%d",n);
134  return res;
135 }
136 
137 Bool_t TGo4FitModelFunction::Initialize(Int_t UseBuffers)
138 {
139  if (!LoadLibrary(kTRUE)) return kFALSE;
140  return TGo4FitModel::Initialize(UseBuffers);
141 }
142 
144 {
145  CloseLibrary();
147 }
148 
150 {
151  if (!TGo4FitModel::BeforeEval(ndim)) return kFALSE;
152  return LoadLibrary(kFALSE);
153 }
154 
156 {
158 }
159 
160 Double_t TGo4FitModelFunction::UserFunction(Double_t* Coordinates, Double_t* Parameters)
161 {
162  if (fxUserFunction) return (*fxUserFunction)(Coordinates,Parameters);
163  else return 0.;
164 }
165 
166 #ifndef _WINDOWS
167 
168 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
169 {
170  if ((fxLibraryName.Length()==0) || (fxFunctionName.Length()==0)) {
171  if (fxUserFunction==0) std::cout << "TGo4FitModelFunction: user function not set" << std::endl;
172  return (fxUserFunction!=0);
173  }
174 
175  if (CloseFirst) CloseLibrary();
176  if ((fxLibrary!=0) && (fxUserFunction!=0)) return kTRUE;
177 
178  fxLibrary = dlopen(fxLibraryName, RTLD_NOW | RTLD_GLOBAL);
179  if (fxLibrary==0) {
180  std::cout << " TGo4FitModelFunction: failed to open " << fxLibraryName << ", " << dlerror() << std::endl;
181  return kFALSE;
182  }
187  if(fxUserFunction==0) {
188  std::cout << " TGo4FitModelFunction: failed to find " << fxFunctionName << ", " << dlerror() << std::endl;
189  CloseLibrary();
190  return kFALSE;
191  }
192  return kTRUE;
193 }
194 
196 {
197  if (fxLibrary!=0) {
198  dlclose(fxLibrary);
199  fxLibrary = 0;
200  }
201 }
202 
203 #else
204 
205 Bool_t TGo4FitModelFunction::LoadLibrary(Bool_t CloseFirst)
206 {
207  // here must be win32 LoadLibrary() and GetProcAddress() calls
208  // when required, must be implemented
209 
210  fxLibrary = 0;
211  return kFALSE;
212 }
213 
215 {
216  // here must be FreeLibrary() calls
217  fxLibrary = 0;
218 }
219 
220 #endif
221 
222 void TGo4FitModelFunction::Print(Option_t* option) const
223 {
224  TGo4FitModel::Print(option);
225  if ((fxLibraryName.Length()>0) || (fxFunctionName.Length()>0))
226  std::cout << "Function " << fxFunctionName << " in " << fxLibraryName << std::endl;
227  else
228  std::cout << " Pointer on function " << fxUserFunction << std::endl;
229  for (Int_t naxis=0;naxis<fxPosIndex.GetSize();naxis++) {
230  TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxPosIndex[naxis]);
231  if (par)
232  std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
233  }
234  for (Int_t naxis=0;naxis<fxWidthIndex.GetSize();naxis++) {
235  TGo4FitParameter* par = ((TGo4FitModelFunction*) this)->GetFuncPar(fxWidthIndex[naxis]);
236  if (par)
237  std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
238  }
239 }
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)
Int_t GetAmplIndex() const
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()