GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitModelFormula.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitModelFormula.cxx 2769 2020-04-16 14:54:23Z 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 "TGo4FitModelFormula.h"
15 
16 #include <iostream>
17 
18 #include "TFormula.h"
19 #include "TGo4FitParameter.h"
20 
22  fxExpression(), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
23 }
24 
25 TGo4FitModelFormula::TGo4FitModelFormula(const char* iName, const char* iExpressionStr, Int_t iNPars, Bool_t AddAmplitude) :
26  TGo4FitModel(iName,"based on TFormula line shape",AddAmplitude),
27  fxExpression(iExpressionStr), fxPosIndex(), fxWidthIndex(), fxFormula(0) {
28 
29  for (Int_t n=0;n<iNPars;n++)
30  NewParameter(GetExprParName(n),"formula parameter",0.);
31 }
32 
33 
35  CloseFormula();
36 }
37 
39  Int_t num = TGo4FitParsList::NumPars();
40  if (GetAmplPar()) num--;
41  return num;
42 }
43 
45  if ((n<0) || (n>=GetNumberOfExprPar())) return 0;
46  if ((GetAmplIndex()>=0) && (n>=GetAmplIndex())) n++;
47  return GetPar(n);
48 }
49 
51  Int_t oldnum = GetNumberOfExprPar();
52  if ((num<0) || (oldnum==num)) return kFALSE;
53  if(num<oldnum) {
54  for(Int_t n=oldnum-1;n>=num;n--)
55  RemovePar(GetExprPar(n)->GetName());
56  } else {
57  for(Int_t n=oldnum;n<num;n++)
58  NewParameter(GetExprParName(n),"formula parameter",0.);
59  }
60  return kTRUE;
61 }
62 
63 void TGo4FitModelFormula::SetPosParIndex(Int_t naxis, Int_t indx) {
64  if (naxis<0) return;
65  Int_t oldsize = fxPosIndex.GetSize();
66  if (naxis>=oldsize) {
67  fxPosIndex.Set(naxis+1);
68  for (Int_t n=oldsize;n<fxPosIndex.GetSize();n++) fxPosIndex[n] = -1;
69  }
70  fxPosIndex[naxis] = indx;
71 }
72 
73 void TGo4FitModelFormula::SetWidthParIndex(Int_t naxis, Int_t indx) {
74  if (naxis<0) return;
75  Int_t oldsize = fxWidthIndex.GetSize();
76  if (naxis>=oldsize) {
77  fxWidthIndex.Set(naxis+1);
78  for (Int_t n=oldsize;n<fxWidthIndex.GetSize();n++) fxWidthIndex[n] = -1;
79  }
80  fxWidthIndex[naxis] = indx;
81 }
82 
84  if ((naxis<0) || (naxis>=fxPosIndex.GetSize())) return -1;
85  return GetParIndex(GetExprPar(fxPosIndex[naxis]));
86 }
87 
89  if ((naxis<0) || (naxis>=fxWidthIndex.GetSize())) return -1;
90  return GetParIndex(GetExprPar(fxWidthIndex[naxis]));
91 }
92 
94 {
95  TString res;
96  res.Form("Par%d",n);
97  return res;
98 }
99 
100 Bool_t TGo4FitModelFormula::Initialize(Int_t UseBuffers) {
101  if (!CompileFormula()) return kFALSE;
102  CloseFormula();
103  return TGo4FitModel::Initialize(UseBuffers);
104 }
105 
107  if (!CompileFormula()) return kFALSE;
108  Par_ndim = ndim;
109  for(Int_t n=0;n<NumPars();n++)
110  fxFormula->SetParameter(n, GetPar(n)->GetValue());
111  return kTRUE;
112 }
113 
114 Double_t TGo4FitModelFormula::EvalN(const Double_t* v) {
115  switch (Par_ndim) {
116  case 0: return 0.;
117  case 1: return fxFormula->Eval(v[0]);
118  case 2: return fxFormula->Eval(v[0],v[1]);
119  case 3: return fxFormula->Eval(v[0],v[1],v[2]);
120  default: return fxFormula->EvalPar(v, 0);
121  }
122 }
123 
125  CloseFormula();
126 }
127 
129  CloseFormula();
131 }
132 
133 void TGo4FitModelFormula::Print(Option_t* option) const {
134  TGo4FitModel::Print(option);
135  std::cout << " Expression = " << *fxExpression << std::endl;
136  for (Int_t naxis=0;naxis<fxPosIndex.GetSize();naxis++) {
137  TGo4FitParameter* par = ((TGo4FitModelFormula*) this)->GetExprPar(fxPosIndex[naxis]);
138  if (par)
139  std::cout << " Position on " << naxis << " axis is " << par->GetName() << std::endl;
140  }
141  for (Int_t naxis=0;naxis<fxWidthIndex.GetSize();naxis++) {
142  TGo4FitParameter* par = ((TGo4FitModelFormula*) this)->GetExprPar(fxWidthIndex[naxis]);
143  if (par)
144  std::cout << " Width on " << naxis << " axis is " << par->GetName() << std::endl;
145  }
146 }
147 
149 {
150  CloseFormula();
151 
152  TString Expr(fxExpression);
153  for (Int_t n=0;n<NumPars();n++) {
154  TString str;
155  str.Form("[%d]",n);
156  Expr.ReplaceAll(GetParName(n), str);
157  }
158 
159  fxFormula = new TFormula("Expression", Expr);
160 
161  Int_t err = fxFormula->Compile(Expr);
162  if (err!=0) {
163  std::cerr << "Error in formula: " << fxExpression.Data() << " code " << err << std::endl;
164  CloseFormula();
165  return kFALSE;
166  }
167  return kTRUE;
168 }
169 
171  if (fxFormula) { delete fxFormula; fxFormula = 0; }
172 }
virtual void Finalize()
TString GetExprParName(Int_t n)
virtual Int_t GetWidthParIndex(Int_t naxis)
TGo4FitParameter * GetExprPar(Int_t n)
TGo4FitParameter * GetPar(Int_t n)
Int_t GetAmplIndex() const
virtual Bool_t Initialize(Int_t UseBuffers=-1)
TGo4FitParameter * NewParameter(const char *Name, const char *Title, Double_t iValue=0., Bool_t Fixed=kFALSE, Int_t AtIndx=-1)
virtual Bool_t BeforeEval(Int_t ndim)
virtual Int_t NumPars()
virtual Int_t GetPosParIndex(Int_t naxis)
void SetPosParIndex(Int_t naxis, Int_t indx=-1)
Bool_t RemovePar(const char *name)
virtual void Print(Option_t *option) const
virtual Bool_t Initialize(Int_t UseBuffers=-1)
Bool_t SetNumberOfExprPar(Int_t num)
virtual Double_t EvalN(const Double_t *v)
Int_t GetParIndex(const TGo4FitParameter *par)
void SetWidthParIndex(Int_t naxis, Int_t indx=-1)
virtual Int_t NumPars()
const char * GetParName(Int_t n)
TGo4FitParameter * GetAmplPar()
virtual void Print(Option_t *option) const