GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitModelGaussN.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitModelGaussN.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 "TGo4FitModelGaussN.h"
15 
16 #include "Riostream.h"
17 #include "TMath.h"
18 #include "TGo4FitParameter.h"
19 
21  Vect_mu(0),Matr_sig(0), Vect_x(0), Vect_dx(0) {
22 }
23 
24 TGo4FitModelGaussN::TGo4FitModelGaussN(const char* iName, Int_t iNDimension) :
25  TGo4FitModel(iName,"N-dimensional Gaussian", kTRUE), fxIndexes(iNDimension),
26  Vect_mu(0),Matr_sig(0), Vect_x(0), Vect_dx(0) {
27  for(Int_t n=0;n<iNDimension;n++) fxIndexes[n] = n;
28 
29  for(Int_t n=0;n<iNDimension;n++)
30  NewParameter(GetPosParName(n),"position",1.);
31 
32  for(Int_t n=0;n<iNDimension;n++)
33  NewParameter(GetWidthParName(n),"width",.5);
34 
35  for(Int_t n1=0;n1<iNDimension;n1++)
36  for(Int_t n2=n1+1;n2<iNDimension;n2++)
37  NewParameter(GetCovarParName(n1,n2),"covariation",0.);
38 }
39 
41  if(Vect_mu) { delete Vect_mu; Vect_mu = 0; }
42  if(Matr_sig) { delete Matr_sig; Matr_sig = 0; }
43  if(Vect_x) { delete Vect_x; Vect_x = 0; }
44  if (Vect_dx) { delete Vect_dx; Vect_dx = 0; }
45 }
46 
48 {
49  TString res;
50  res.Form("Pos%d",naxis);
51  return res;
52 }
53 
55 {
56  TString res;
57  res.Form("Width%d",naxis);
58  return res;
59 }
60 
61 TString TGo4FitModelGaussN::GetCovarParName(Int_t naxis1, Int_t naxis2)
62 {
63  TString res;
64  res.Form("Cov%d_%d",naxis1,naxis2);
65  return res;
66 }
67 
68 
69 
71  Int_t oldnaxis = GetAxisNumbers();
72  if ((naxis<0) || (oldnaxis==naxis)) return kFALSE;
73  if(naxis<oldnaxis) {
74  for(Int_t n=naxis;n<oldnaxis;n++) {
77  }
78  for(Int_t n1=0;n1<oldnaxis;n1++)
79  for(Int_t n2=n1+1;n2<oldnaxis;n2++)
80  if((n1>=naxis) || (n2>=naxis))
81  RemovePar(GetCovarParName(n1,n2));
82  fxIndexes.Set(naxis);
83  } else {
84  Int_t lastindx = 0;
85  for(Int_t n=oldnaxis;n<naxis;n++) {
86  lastindx = GetParIndex(FindPar(GetPosParName(n-1)))+1;
87  NewParameter(GetPosParName(n),"position",1.,kFALSE,lastindx);
88  lastindx = GetParIndex(FindPar(GetWidthParName(n-1)))+1;
89  NewParameter(GetWidthParName(n),"width",5.,kFALSE,lastindx);
90  }
91  lastindx++;
92  for(Int_t n1=0;n1<naxis;n1++)
93  for(Int_t n2=n1+1;n2<naxis;n2++) {
95  if (par) lastindx = GetParIndex(par)+1;
96  else NewParameter(GetCovarParName(n1,n2),"covariation",0.,kFALSE,lastindx++);
97  }
98 
99  fxIndexes.Set(naxis);
100  for (Int_t n=oldnaxis;n<naxis;n++) {
101  Int_t test = 0;
102  Bool_t find;
103  do {
104  find = kFALSE;
105  for (Int_t n1=0;n1<n;n1++)
106  if(test==fxIndexes[n1]) { find=kTRUE; test++; break; }
107  } while(find);
108  fxIndexes[n] = test;
109  }
110  }
111 
112  return kTRUE;
113 }
114 
115 Bool_t TGo4FitModelGaussN::ResortIndexes(Int_t leaveaxis) {
116  Bool_t res = kFALSE;
117  for (Int_t n=fxIndexes.GetSize()-1;n>=0;n--) {
118  if (n==leaveaxis) continue;
119  Int_t test = fxIndexes[n];
120  Bool_t find, first = kTRUE;
121  do {
122  find = kFALSE;
123  for (Int_t n1=0;n1<fxIndexes.GetSize();n1++)
124  if((n!=n1) && (test==fxIndexes[n1])) {
125  if (first) test=0; else test++;
126  find = kTRUE;
127  first = kFALSE;
128  break;
129  }
130  } while(find);
131  fxIndexes[n] = test;
132  res = res || (!first);
133  }
134  return res;
135 }
136 
137 
139  for(Int_t n=0;n<fxIndexes.GetSize();n++)
140  if (naxis==fxIndexes[n]) return 1+n;
141  return -1;
142 }
143 
145  for(Int_t n=0;n<fxIndexes.GetSize();n++)
146  if (naxis==fxIndexes[n]) return 1+fxIndexes.GetSize()+n;
147  return -1;
148 }
149 
151  Int_t ndim = fxIndexes.GetSize();
152  Mu.ResizeTo(ndim);
153  for(Int_t n=0;n<ndim;n++)
154  Mu(n) = GetPar(1+n)->GetValue();
155 }
156 
158 {
159  Int_t ndim = fxIndexes.GetSize();
160  Sigma.ResizeTo(ndim,ndim);
161  for(Int_t n=0;n<ndim;n++)
162  Sigma(n,n) = GetPar(1+ndim+n)->GetValue() * GetPar(1+ndim+n)->GetValue();
163  Int_t indx = 1+2*ndim;
164  for(Int_t n1=0;n1<ndim;n1++)
165  for(Int_t n2=n1+1;n2<ndim;n2++) {
166  Double_t zn = GetPar(indx++)->GetValue();
167  Sigma(n1,n2) = zn;
168  Sigma(n2,n1) = zn;
169  }
170 }
171 
172 Bool_t TGo4FitModelGaussN::BeforeEval(Int_t NDimension)
173 {
174  Par_ndim = fxIndexes.GetSize();
175  Par_indx = fxIndexes.GetArray();
176  for (Int_t i=0;i<Par_ndim;i++)
177  if (Par_indx[i]>=NDimension)
178  { std::cout << "TGo4FitModelGaussN:: invalid index " << std::endl; return kFALSE; }
179 
180  Vect_mu = new TVectorD(Par_ndim);
181  Matr_sig = new TMatrixD(Par_ndim,Par_ndim);
182  Vect_x = new TVectorD(Par_ndim);
183  Vect_dx = new TVectorD(Par_ndim);
184 
187  Double_t determ;
188  Matr_sig->Invert(&determ);
189  if (determ==0.)
190  { std::cout << "TGo4FitModelGaussN:: Invalid sigma matrice " << std::endl; AfterEval(); return kFALSE; }
191 
192  return kTRUE;
193 }
194 
195 Double_t TGo4FitModelGaussN::EvalN(const Double_t* v) {
196  for (Int_t n=0;n<Par_ndim;n++)
197  (*Vect_x)(n) = v[Par_indx[n]];
198  *Vect_x -= *Vect_mu;
199  *Vect_dx = *Vect_x;
200  *Vect_dx *= *Matr_sig;
201  Double_t z = *Vect_dx * *Vect_x; // see 11.1
202  return TMath::Exp(-0.5*z);
203 }
204 
206  if(Vect_mu) { delete Vect_mu; Vect_mu = 0; }
207  if(Matr_sig) { delete Matr_sig; Matr_sig = 0; }
208  if(Vect_x) { delete Vect_x; Vect_x = 0; }
209  if (Vect_dx) { delete Vect_dx; Vect_dx = 0; }
210 }
211 
212 void TGo4FitModelGaussN::Print(Option_t* option) const
213 {
214  TGo4FitModel::Print(option);
215  std::cout << " N-dimensional Gauss" << std::endl;
216  std::cout << " axis indexes:";
217  for (Int_t i=0;i<fxIndexes.GetSize();i++)
218  std::cout << " " << fxIndexes[i];
219  std::cout << std::endl;
220 }
Bool_t ResortIndexes(Int_t leaveaxis=-1)
TString GetPosParName(Int_t naxis)
virtual Double_t EvalN(const Double_t *v)
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 FillSigmaMatrix(TMatrixD &Sigma)
Bool_t SetAxisNumbers(Int_t naxis)
virtual Int_t GetWidthParIndex(Int_t naxis)
Bool_t RemovePar(const char *name)
virtual Bool_t BeforeEval(Int_t)
TString GetCovarParName(Int_t naxis1, Int_t naxis2)
TGo4FitParameter * FindPar(const char *ParName)
virtual void Print(Option_t *option) const
TString GetWidthParName(Int_t naxis)
Int_t GetParIndex(const TGo4FitParameter *par)
Double_t GetValue() const
virtual Int_t GetPosParIndex(Int_t naxis)
virtual void Print(Option_t *option) const