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