00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4FitModelGaussN.h"
00017
00018 #include "Riostream.h"
00019 #include "TMath.h"
00020 #include "TGo4FitParameter.h"
00021
00022 TGo4FitModelGaussN::TGo4FitModelGaussN() : TGo4FitModel(), fxIndexes(),
00023 Vect_mu(0),Matr_sig(0), Vect_x(0), Vect_dx(0) {
00024 }
00025
00026 TGo4FitModelGaussN::TGo4FitModelGaussN(const char* iName, Int_t iNDimension) :
00027 TGo4FitModel(iName,"N-dimensional Gaussian", kTRUE), fxIndexes(iNDimension),
00028 Vect_mu(0),Matr_sig(0), Vect_x(0), Vect_dx(0) {
00029 for(Int_t n=0;n<iNDimension;n++) fxIndexes[n] = n;
00030
00031 for(Int_t n=0;n<iNDimension;n++)
00032 NewParameter(GetPosParName(n),"position",1.);
00033
00034 for(Int_t n=0;n<iNDimension;n++)
00035 NewParameter(GetWidthParName(n),"width",.5);
00036
00037 for(Int_t n1=0;n1<iNDimension;n1++)
00038 for(Int_t n2=n1+1;n2<iNDimension;n2++)
00039 NewParameter(GetCovarParName(n1,n2),"covariation",0.);
00040 }
00041
00042 TGo4FitModelGaussN::~TGo4FitModelGaussN() {
00043 if(Vect_mu) { delete Vect_mu; Vect_mu = 0; }
00044 if(Matr_sig) { delete Matr_sig; Matr_sig = 0; }
00045 if(Vect_x) { delete Vect_x; Vect_x = 0; }
00046 if (Vect_dx) { delete Vect_dx; Vect_dx = 0; }
00047 }
00048
00049 TString TGo4FitModelGaussN::GetPosParName(Int_t naxis)
00050 {
00051 TString res;
00052 res.Form("Pos%d",naxis);
00053 return res;
00054 }
00055
00056 TString TGo4FitModelGaussN::GetWidthParName(Int_t naxis)
00057 {
00058 TString res;
00059 res.Form("Width%d",naxis);
00060 return res;
00061 }
00062
00063 TString TGo4FitModelGaussN::GetCovarParName(Int_t naxis1, Int_t naxis2)
00064 {
00065 TString res;
00066 res.Form("Cov%d_%d",naxis1,naxis2);
00067 return res;
00068 }
00069
00070
00071
00072 Bool_t TGo4FitModelGaussN::SetAxisNumbers(Int_t naxis) {
00073 Int_t oldnaxis = GetAxisNumbers();
00074 if ((naxis<0) || (oldnaxis==naxis)) return kFALSE;
00075 if(naxis<oldnaxis) {
00076 for(Int_t n=naxis;n<oldnaxis;n++) {
00077 RemovePar(GetPosParName(n));
00078 RemovePar(GetWidthParName(n));
00079 }
00080 for(Int_t n1=0;n1<oldnaxis;n1++)
00081 for(Int_t n2=n1+1;n2<oldnaxis;n2++)
00082 if((n1>=naxis) || (n2>=naxis))
00083 RemovePar(GetCovarParName(n1,n2));
00084 fxIndexes.Set(naxis);
00085 } else {
00086 Int_t lastindx = 0;
00087 for(Int_t n=oldnaxis;n<naxis;n++) {
00088 lastindx = GetParIndex(FindPar(GetPosParName(n-1)))+1;
00089 NewParameter(GetPosParName(n),"position",1.,kFALSE,lastindx);
00090 lastindx = GetParIndex(FindPar(GetWidthParName(n-1)))+1;
00091 NewParameter(GetWidthParName(n),"width",5.,kFALSE,lastindx);
00092 }
00093 lastindx++;
00094 for(Int_t n1=0;n1<naxis;n1++)
00095 for(Int_t n2=n1+1;n2<naxis;n2++) {
00096 TGo4FitParameter* par = FindPar(GetCovarParName(n1,n2));
00097 if (par) lastindx = GetParIndex(par)+1;
00098 else NewParameter(GetCovarParName(n1,n2),"covariation",0.,kFALSE,lastindx++);
00099 }
00100
00101 fxIndexes.Set(naxis);
00102 for (Int_t n=oldnaxis;n<naxis;n++) {
00103 Int_t test = 0;
00104 Bool_t find;
00105 do {
00106 find = kFALSE;
00107 for (Int_t n1=0;n1<n;n1++)
00108 if(test==fxIndexes[n1]) { find=kTRUE; test++; break; }
00109 } while(find);
00110 fxIndexes[n] = test;
00111 }
00112 }
00113
00114 return kTRUE;
00115 }
00116
00117 Bool_t TGo4FitModelGaussN::ResortIndexes(Int_t leaveaxis) {
00118 Bool_t res = kFALSE;
00119 for (Int_t n=fxIndexes.GetSize()-1;n>=0;n--) {
00120 if (n==leaveaxis) continue;
00121 Int_t test = fxIndexes[n];
00122 Bool_t find, first = kTRUE;
00123 do {
00124 find = kFALSE;
00125 for (Int_t n1=0;n1<fxIndexes.GetSize();n1++)
00126 if((n!=n1) && (test==fxIndexes[n1])) {
00127 if (first) test=0; else test++;
00128 find = kTRUE;
00129 first = kFALSE;
00130 break;
00131 }
00132 } while(find);
00133 fxIndexes[n] = test;
00134 res = res || (!first);
00135 }
00136 return res;
00137 }
00138
00139
00140 Int_t TGo4FitModelGaussN::GetPosParIndex(Int_t naxis) {
00141 for(Int_t n=0;n<fxIndexes.GetSize();n++)
00142 if (naxis==fxIndexes[n]) return 1+n;
00143 return -1;
00144 }
00145
00146 Int_t TGo4FitModelGaussN::GetWidthParIndex(Int_t naxis) {
00147 for(Int_t n=0;n<fxIndexes.GetSize();n++)
00148 if (naxis==fxIndexes[n]) return 1+fxIndexes.GetSize()+n;
00149 return -1;
00150 }
00151
00152 void TGo4FitModelGaussN::FillMuVector(TVectorD& Mu) {
00153 Int_t ndim = fxIndexes.GetSize();
00154 Mu.ResizeTo(ndim);
00155 for(Int_t n=0;n<ndim;n++)
00156 Mu(n) = GetPar(1+n)->GetValue();
00157 }
00158
00159 void TGo4FitModelGaussN::FillSigmaMatrix(TMatrixD& Sigma)
00160 {
00161 Int_t ndim = fxIndexes.GetSize();
00162 Sigma.ResizeTo(ndim,ndim);
00163 for(Int_t n=0;n<ndim;n++)
00164 Sigma(n,n) = GetPar(1+ndim+n)->GetValue() * GetPar(1+ndim+n)->GetValue();
00165 Int_t indx = 1+2*ndim;
00166 for(Int_t n1=0;n1<ndim;n1++)
00167 for(Int_t n2=n1+1;n2<ndim;n2++) {
00168 Double_t zn = GetPar(indx++)->GetValue();
00169 Sigma(n1,n2) = zn;
00170 Sigma(n2,n1) = zn;
00171 }
00172 }
00173
00174 Bool_t TGo4FitModelGaussN::BeforeEval(Int_t NDimension)
00175 {
00176 Par_ndim = fxIndexes.GetSize();
00177 Par_indx = fxIndexes.GetArray();
00178 for (Int_t i=0;i<Par_ndim;i++)
00179 if (Par_indx[i]>=NDimension)
00180 { cout << "TGo4FitModelGaussN:: invalid index "; return kFALSE; }
00181
00182 Vect_mu = new TVectorD(Par_ndim);
00183 Matr_sig = new TMatrixD(Par_ndim,Par_ndim);
00184 Vect_x = new TVectorD(Par_ndim);
00185 Vect_dx = new TVectorD(Par_ndim);
00186
00187 FillMuVector(*Vect_mu);
00188 FillSigmaMatrix(*Matr_sig);
00189 Double_t determ;
00190 Matr_sig->Invert(&determ);
00191 if (determ==0.)
00192 { cout << "TGo4FitModelGaussN:: Invalid sigma matrice " << endl; AfterEval(); return kFALSE; }
00193
00194 return kTRUE;
00195 }
00196
00197 Double_t TGo4FitModelGaussN::EvalN(const Double_t* v) {
00198 for (Int_t n=0;n<Par_ndim;n++)
00199 (*Vect_x)(n) = v[Par_indx[n]];
00200 *Vect_x -= *Vect_mu;
00201 *Vect_dx = *Vect_x;
00202 *Vect_dx *= *Matr_sig;
00203 Double_t z = *Vect_dx * *Vect_x;
00204 return TMath::Exp(-0.5*z);
00205 }
00206
00207 void TGo4FitModelGaussN::AfterEval() {
00208 if(Vect_mu) { delete Vect_mu; Vect_mu = 0; }
00209 if(Matr_sig) { delete Matr_sig; Matr_sig = 0; }
00210 if(Vect_x) { delete Vect_x; Vect_x = 0; }
00211 if (Vect_dx) { delete Vect_dx; Vect_dx = 0; }
00212 }
00213
00214 void TGo4FitModelGaussN::Print(Option_t* option) const
00215 {
00216 TGo4FitModel::Print(option);
00217 cout << " N-dimensional Gauss" << endl;
00218 cout << " axis indexes:";
00219 for (Int_t i=0;i<fxIndexes.GetSize();i++)
00220 cout << " " << fxIndexes[i];
00221 cout << endl;
00222 }
00223
00224