GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4FitModelGaussN.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 "TGo4FitModelGaussN.h"
15 
16 #include <iostream>
17 
18 #include "TMath.h"
19 #include "TGo4FitParameter.h"
20 
22 {
23 }
24 
25 TGo4FitModelGaussN::TGo4FitModelGaussN(const char *iName, Int_t iNDimension)
26  : TGo4FitModel(iName, "N-dimensional Gaussian", kTRUE), fxIndexes(iNDimension)
27 {
28  for (Int_t n = 0; n < iNDimension; n++)
29  fxIndexes[n] = n;
30 
31  for (Int_t n = 0; n < iNDimension; n++)
32  NewParameter(GetPosParName(n), "position", 1.);
33 
34  for (Int_t n = 0; n < iNDimension; n++)
35  NewParameter(GetWidthParName(n), "width", .5);
36 
37  for (Int_t n1 = 0; n1 < iNDimension; n1++)
38  for (Int_t n2 = n1 + 1; n2 < iNDimension; n2++)
39  NewParameter(GetCovarParName(n1, n2), "covariation", 0.);
40 }
41 
43 {
44  if (Vect_mu) {
45  delete Vect_mu;
46  Vect_mu = nullptr;
47  }
48  if (Matr_sig) {
49  delete Matr_sig;
50  Matr_sig = nullptr;
51  }
52  if (Vect_x) {
53  delete Vect_x;
54  Vect_x = nullptr;
55  }
56  if (Vect_dx) {
57  delete Vect_dx;
58  Vect_dx = nullptr;
59  }
60 }
61 
63 {
64  TString res;
65  res.Form("Pos%d", naxis);
66  return res;
67 }
68 
70 {
71  TString res;
72  res.Form("Width%d", naxis);
73  return res;
74 }
75 
76 TString TGo4FitModelGaussN::GetCovarParName(Int_t naxis1, Int_t naxis2)
77 {
78  TString res;
79  res.Form("Cov%d_%d", naxis1, naxis2);
80  return res;
81 }
82 
84 {
85  Int_t oldnaxis = GetAxisNumbers();
86  if ((naxis < 0) || (oldnaxis == naxis))
87  return kFALSE;
88  if (naxis < oldnaxis) {
89  for (Int_t n = naxis; n < oldnaxis; n++) {
92  }
93  for (Int_t n1 = 0; n1 < oldnaxis; n1++)
94  for (Int_t n2 = n1 + 1; n2 < oldnaxis; n2++)
95  if ((n1 >= naxis) || (n2 >= naxis))
96  RemovePar(GetCovarParName(n1, n2));
97  fxIndexes.Set(naxis);
98  } else {
99  Int_t lastindx = 0;
100  for (Int_t n = oldnaxis; n < naxis; n++) {
101  lastindx = GetParIndex(FindPar(GetPosParName(n - 1))) + 1;
102  NewParameter(GetPosParName(n), "position", 1., kFALSE, lastindx);
103  lastindx = GetParIndex(FindPar(GetWidthParName(n - 1))) + 1;
104  NewParameter(GetWidthParName(n), "width", 5., kFALSE, lastindx);
105  }
106  lastindx++;
107  for (Int_t n1 = 0; n1 < naxis; n1++)
108  for (Int_t n2 = n1 + 1; n2 < naxis; n2++) {
109  TGo4FitParameter *par = FindPar(GetCovarParName(n1, n2));
110  if (par)
111  lastindx = GetParIndex(par) + 1;
112  else
113  NewParameter(GetCovarParName(n1, n2), "covariation", 0., kFALSE, lastindx++);
114  }
115 
116  fxIndexes.Set(naxis);
117  for (Int_t n = oldnaxis; n < naxis; n++) {
118  Int_t test = 0;
119  Bool_t find;
120  do {
121  find = kFALSE;
122  for (Int_t n1 = 0; n1 < n; n1++)
123  if (test == fxIndexes[n1]) {
124  find = kTRUE;
125  test++;
126  break;
127  }
128  } while (find);
129  fxIndexes[n] = test;
130  }
131  }
132 
133  return kTRUE;
134 }
135 
136 Bool_t TGo4FitModelGaussN::ResortIndexes(Int_t leaveaxis)
137 {
138  Bool_t res = kFALSE;
139  for (Int_t n = fxIndexes.GetSize() - 1; n >= 0; n--) {
140  if (n == leaveaxis)
141  continue;
142  Int_t test = fxIndexes[n];
143  Bool_t find, first = kTRUE;
144  do {
145  find = kFALSE;
146  for (Int_t n1 = 0; n1 < fxIndexes.GetSize(); n1++)
147  if ((n != n1) && (test == fxIndexes[n1])) {
148  if (first)
149  test = 0;
150  else
151  test++;
152  find = kTRUE;
153  first = kFALSE;
154  break;
155  }
156  } while (find);
157  fxIndexes[n] = test;
158  res = res || (!first);
159  }
160  return res;
161 }
162 
164 {
165  for (Int_t n = 0; n < fxIndexes.GetSize(); n++)
166  if (naxis == fxIndexes[n])
167  return 1 + n;
168  return -1;
169 }
170 
172 {
173  for (Int_t n = 0; n < fxIndexes.GetSize(); n++)
174  if (naxis == fxIndexes[n])
175  return 1 + fxIndexes.GetSize() + n;
176  return -1;
177 }
178 
180 {
181  Int_t ndim = fxIndexes.GetSize();
182  Mu.ResizeTo(ndim);
183  for (Int_t n = 0; n < ndim; n++)
184  Mu(n) = GetPar(1 + n)->GetValue();
185 }
186 
188 {
189  Int_t ndim = fxIndexes.GetSize();
190  Sigma.ResizeTo(ndim, ndim);
191  for (Int_t n = 0; n < ndim; n++)
192  Sigma(n, n) = GetPar(1 + ndim + n)->GetValue() * GetPar(1 + ndim + n)->GetValue();
193  Int_t indx = 1 + 2 * ndim;
194  for (Int_t n1 = 0; n1 < ndim; n1++)
195  for (Int_t n2 = n1 + 1; n2 < ndim; n2++) {
196  Double_t zn = GetPar(indx++)->GetValue();
197  Sigma(n1, n2) = zn;
198  Sigma(n2, n1) = zn;
199  }
200 }
201 
202 Bool_t TGo4FitModelGaussN::BeforeEval(Int_t NDimension)
203 {
204  Par_ndim = fxIndexes.GetSize();
205  Par_indx = fxIndexes.GetArray();
206  for (Int_t i = 0; i < Par_ndim; i++)
207  if (Par_indx[i] >= NDimension) {
208  std::cout << "TGo4FitModelGaussN:: invalid index " << std::endl;
209  return kFALSE;
210  }
211 
212  Vect_mu = new TVectorD(Par_ndim);
213  Matr_sig = new TMatrixD(Par_ndim, Par_ndim);
214  Vect_x = new TVectorD(Par_ndim);
215  Vect_dx = new TVectorD(Par_ndim);
216 
219  Double_t determ;
220  Matr_sig->Invert(&determ);
221  if (determ == 0.) {
222  std::cout << "TGo4FitModelGaussN:: Invalid sigma matrice " << std::endl;
223  AfterEval();
224  return kFALSE;
225  }
226 
227  return kTRUE;
228 }
229 
230 Double_t TGo4FitModelGaussN::EvalN(const Double_t *v)
231 {
232  for (Int_t n = 0; n < Par_ndim; n++)
233  (*Vect_x)(n) = v[Par_indx[n]];
234  *Vect_x -= *Vect_mu;
235  *Vect_dx = *Vect_x;
236  *Vect_dx *= *Matr_sig;
237  Double_t z = *Vect_dx * *Vect_x; // see 11.1
238  return TMath::Exp(-0.5 * z);
239 }
240 
242 {
243  if (Vect_mu) {
244  delete Vect_mu;
245  Vect_mu = nullptr;
246  }
247  if (Matr_sig) {
248  delete Matr_sig;
249  Matr_sig = nullptr;
250  }
251  if (Vect_x) {
252  delete Vect_x;
253  Vect_x = nullptr;
254  }
255  if (Vect_dx) {
256  delete Vect_dx;
257  Vect_dx = nullptr;
258  }
259 }
260 
261 void TGo4FitModelGaussN::Print(Option_t *option) const
262 {
263  TGo4FitModel::Print(option);
264  std::cout << " N-dimensional Gauss" << std::endl;
265  std::cout << " axis indexes:";
266  for (Int_t i = 0; i < fxIndexes.GetSize(); i++)
267  std::cout << " " << fxIndexes[i];
268  std::cout << std::endl;
269 }
Double_t GetValue() const
Bool_t ResortIndexes(Int_t leaveaxis=-1)
void AfterEval() override
TString GetPosParName(Int_t naxis)
TGo4FitParameter * GetPar(Int_t n)
void FillMuVector(TVectorD &Mu)
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
void FillSigmaMatrix(TMatrixD &Sigma)
Int_t GetPosParIndex(Int_t naxis) override
Bool_t SetAxisNumbers(Int_t naxis)
Bool_t RemovePar(const char *name)
TString GetCovarParName(Int_t naxis1, Int_t naxis2)
Int_t GetWidthParIndex(Int_t naxis) override
TGo4FitParameter * FindPar(const char *ParName)
TString GetWidthParName(Int_t naxis)
void Print(Option_t *option="") const override
Double_t EvalN(const Double_t *v) override
Int_t GetParIndex(const TGo4FitParameter *par)
Int_t GetAxisNumbers() const
Bool_t BeforeEval(Int_t) override