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