GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
TGo4FitModelFormula.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 "TGo4FitModelFormula.h"
15
16#include <iostream>
17
18#include "TFormula.h"
19#include "TGo4FitParameter.h"
20
25
26TGo4FitModelFormula::TGo4FitModelFormula(const char *iName, const char *iExpressionStr, Int_t iNPars,
27 Bool_t AddAmplitude)
28 : TGo4FitModel(iName, "based on TFormula line shape", AddAmplitude), fxExpression(iExpressionStr), fxPosIndex(),
29 fxWidthIndex(), fxFormula(nullptr)
30{
31
32 for (Int_t n = 0; n < iNPars; n++)
33 NewParameter(GetExprParName(n), "formula parameter", 0.);
34}
35
40
42{
43 Int_t num = TGo4FitParsList::NumPars();
44 if (GetAmplPar())
45 num--;
46 return num;
47}
48
50{
51 if ((n < 0) || (n >= GetNumberOfExprPar()))
52 return nullptr;
53 if ((GetAmplIndex() >= 0) && (n >= GetAmplIndex()))
54 n++;
55 return GetPar(n);
56}
57
59{
60 Int_t oldnum = GetNumberOfExprPar();
61 if ((num < 0) || (oldnum == num))
62 return kFALSE;
63 if (num < oldnum) {
64 for (Int_t n = oldnum - 1; n >= num; n--)
65 RemovePar(GetExprPar(n)->GetName());
66 } else {
67 for (Int_t n = oldnum; n < num; n++)
68 NewParameter(GetExprParName(n), "formula parameter", 0.);
69 }
70 return kTRUE;
71}
72
73void TGo4FitModelFormula::SetPosParIndex(Int_t naxis, Int_t indx)
74{
75 if (naxis < 0)
76 return;
77 Int_t oldsize = fxPosIndex.GetSize();
78 if (naxis >= oldsize) {
79 fxPosIndex.Set(naxis + 1);
80 for (Int_t n = oldsize; n < fxPosIndex.GetSize(); n++)
81 fxPosIndex[n] = -1;
82 }
83 fxPosIndex[naxis] = indx;
84}
85
86void TGo4FitModelFormula::SetWidthParIndex(Int_t naxis, Int_t indx)
87{
88 if (naxis < 0)
89 return;
90 Int_t oldsize = fxWidthIndex.GetSize();
91 if (naxis >= oldsize) {
92 fxWidthIndex.Set(naxis + 1);
93 for (Int_t n = oldsize; n < fxWidthIndex.GetSize(); n++)
94 fxWidthIndex[n] = -1;
95 }
96 fxWidthIndex[naxis] = indx;
97}
98
100{
101 if ((naxis < 0) || (naxis >= fxPosIndex.GetSize()))
102 return -1;
103 return GetParIndex(GetExprPar(fxPosIndex[naxis]));
104}
105
107{
108 if ((naxis < 0) || (naxis >= fxWidthIndex.GetSize()))
109 return -1;
110 return GetParIndex(GetExprPar(fxWidthIndex[naxis]));
111}
112
114{
115 TString res;
116 res.Form("Par%d", n);
117 return res;
118}
119
120Bool_t TGo4FitModelFormula::Initialize(Int_t UseBuffers)
121{
122 if (!CompileFormula())
123 return kFALSE;
124 CloseFormula();
125 return TGo4FitModel::Initialize(UseBuffers);
126}
127
129{
130 if (!CompileFormula())
131 return kFALSE;
132 Par_ndim = ndim;
133 for (Int_t n = 0; n < NumPars(); n++)
134 fxFormula->SetParameter(n, GetPar(n)->GetValue());
135 return kTRUE;
136}
137
138Double_t TGo4FitModelFormula::EvalN(const Double_t *v)
139{
140 switch (Par_ndim) {
141 case 0: return 0.;
142 case 1: return fxFormula->Eval(v[0]);
143 case 2: return fxFormula->Eval(v[0], v[1]);
144 case 3: return fxFormula->Eval(v[0], v[1], v[2]);
145 default: return fxFormula->EvalPar(v, nullptr);
146 }
147}
148
153
159
160void TGo4FitModelFormula::Print(Option_t *option) const
161{
162 TGo4FitModel::Print(option);
163 std::cout << " Expression = " << *fxExpression << std::endl;
164 for (Int_t naxis = 0; naxis < fxPosIndex.GetSize(); naxis++) {
165 TGo4FitParameter *par = ((TGo4FitModelFormula *)this)->GetExprPar(fxPosIndex[naxis]);
166 if (par)
167 std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
168 }
169 for (Int_t naxis = 0; naxis < fxWidthIndex.GetSize(); naxis++) {
170 TGo4FitParameter *par = ((TGo4FitModelFormula *)this)->GetExprPar(fxWidthIndex[naxis]);
171 if (par)
172 std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
173 }
174}
175
177{
178 CloseFormula();
179
180 TString Expr(fxExpression);
181 for (Int_t n = 0; n < NumPars(); n++) {
182 TString str;
183 str.Form("[%d]", n);
184 Expr.ReplaceAll(GetParName(n), str);
185 }
186
187 fxFormula = new TFormula("Expression", Expr);
188
189 Int_t err = fxFormula->Compile(Expr);
190 if (err != 0) {
191 std::cerr << "Error in formula: " << fxExpression.Data() << " code " << err << std::endl;
192 CloseFormula();
193 return kFALSE;
194 }
195 return kTRUE;
196}
197
199{
200 if (fxFormula) {
201 delete fxFormula;
202 fxFormula = nullptr;
203 }
204}
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.
virtual ~TGo4FitModelFormula()
Destroys TGo4FitModelFormula object.
TString fxExpression
String, containing formula expression.
void Print(Option_t *option="") const override
Print information on standard output.
Int_t Par_ndim
temporary variable for formula calculation in EvalN() method.
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
TFormula * fxFormula
formula for evaluation expression in EvalN() method.
Bool_t Initialize(Int_t UseBuffers=-1) override
Initialize model object.
void Finalize() override
Deletes all buffers, created during initialization.
Bool_t SetNumberOfExprPar(Int_t num)
Sets number of parameters, which can be used in expression.
TGo4FitParameter * GetExprPar(Int_t n)
TString GetExprParName(Int_t n)
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Int_t GetNumberOfExprPar()
Get number of parameters, which can be used in expression.
Int_t GetWidthParIndex(Int_t naxis) override
Return index of parameter (if exist), which represent width of model component for given axis.
Double_t EvalN(const Double_t *v) override
Calculates value of model according current parameters values and provided axes values.
TGo4FitModelFormula()
Default constructor.
Int_t GetPosParIndex(Int_t naxis) override
Return index of parameter (if exist), which represent position of model for given axis.
Bool_t BeforeEval(Int_t ndim) override
Prepares (if necessary) some intermediate variables to be able calculate values of model via EvalN() ...
void AfterEval() override
Clear buffers, which were created by BeforeEval() method.
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.
Int_t NumPars() override
Return number of parameters in list.
TGo4FitModel()
Default constructor.
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.
const char * GetParName(Int_t n)
Return name of parameter with given index.
TGo4FitParameter * GetPar(Int_t n)
Return parameter according given index.
virtual Int_t NumPars()
Return number of parameters in list.