GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
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 
22  : TGo4FitModel(), fxExpression(), fxPosIndex(), fxWidthIndex(), fxFormula(nullptr)
23 {
24 }
25 
26 TGo4FitModelFormula::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 
37 {
38  CloseFormula();
39 }
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 
73 void 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 
86 void 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 
120 Bool_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 
138 Double_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 
150 {
151  CloseFormula();
152 }
153 
155 {
156  CloseFormula();
158 }
159 
160 void 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 }
Double_t EvalN(const Double_t *v) override
virtual void Finalize()
Int_t NumPars() override
void Print(Option_t *option="") const override
TString GetExprParName(Int_t n)
TGo4FitParameter * GetExprPar(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)
void Print(Option_t *option="") const override
virtual Int_t NumPars()
Bool_t BeforeEval(Int_t ndim) override
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Bool_t RemovePar(const char *name)
virtual Bool_t Initialize(Int_t UseBuffers=-1)
Bool_t SetNumberOfExprPar(Int_t num)
Bool_t Initialize(Int_t UseBuffers=-1) override
Int_t GetParIndex(const TGo4FitParameter *par)
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
Int_t GetPosParIndex(Int_t naxis) override
Int_t GetWidthParIndex(Int_t naxis) override
const char * GetParName(Int_t n)
TGo4FitParameter * GetAmplPar()