/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Reconstruction/MdcPatRec/MdcxReco/MdcxReco-00-01-59/src/MdcxFittedHel.cxx

Go to the documentation of this file.
00001 // -------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: MdcxFittedHel.cxx,v 1.9 2010/09/26 00:31:13 zhangy Exp $
00004 //
00005 // Description:
00006 //      Class Implementation for |MdcxFittedHel|
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00010 //
00011 // Author List:
00012 //      S. Wagner
00013 //      Zhang Yao(zhangyao@ihep.ac.cn)  Migrate to BESIII
00014 //
00015 // Copyright Information:
00016 //      Copyright (C) 1995      BEPCII
00017 // 
00018 // History:
00019 //      Migration for BESIII MDC
00020 //
00021 //------------------------------------------------------------------------
00022 #include <math.h>
00023 #include "MdcxReco/Mdcxmatinv.h"
00024 #include "MdcxReco/MdcxFittedHel.h"
00025 #include "MdcxReco/MdcxHit.h"
00026 #include "MdcxReco/Mdcxprobab.h"
00027 
00028 #include "AIDA/IHistogram1D.h"
00029 #include "AIDA/IHistogram2D.h"
00030 using std::cout;
00031 using std::endl;
00032 using std::ostream;
00033 
00034 //extern AIDA::IHistogram1D*  g_fitOmega;
00035 int MdcxFittedHel::debug = 0;
00036 
00037 void MdcxFittedHel::basics() 
00038 {nhits=0; itofit=0; fittime=0.0;
00039   prob=0.0; chisq=1000000.0; fail=1300; quality=0; origin=-1; usedonhel=0;
00040   bailout=1; chidofbail=1200.0; niter=10;
00041 } // endof basics 
00042 
00043 void MdcxFittedHel::basics(const HepAList<MdcxHit> &e) {
00044   basics();
00045   nhits=e.length();
00046   xHitList=e;
00047   origin=OriginIncluded();
00048 } // endof basics
00049 
00050 //constructors
00051 MdcxFittedHel::MdcxFittedHel(){
00052   basics();
00053 }
00054 
00055 //points+guess
00056   MdcxFittedHel::MdcxFittedHel
00057 (HepAList<MdcxHit> &XHitList, MdcxHel& hel, double Sfac)
00058 { 
00059   basics(XHitList);
00060   sfac=Sfac;
00061   *this=hel;
00062   fail=IterateFit();
00063 }//endof MdcxFittedHel
00064 
00065 //destructor
00066 MdcxFittedHel::~MdcxFittedHel( ){ }//endof ~MdcxFittedHel
00067 
00068 //operators
00069 MdcxFittedHel& MdcxFittedHel::operator=(const MdcxHel& rhs){
00070   copy(rhs);
00071   return *this;
00072 } //endof MdcxFittedHel::operator=
00073 
00074 MdcxFittedHel& MdcxFittedHel::operator=(const MdcxFittedHel& rhs){
00075   // FIXME
00076   copy(rhs);
00077   fail=rhs.Fail();
00078   chisq=rhs.Chisq();
00079   rcs=rhs.Rcs();
00080   prob=rhs.Prob();
00081   fittime=rhs.Fittime();
00082   nhits=rhs.Nhits();
00083   itofit=rhs.Itofit();
00084   quality=rhs.Quality();
00085   origin=rhs.Origin();
00086   xHitList=rhs.XHitList();
00087   sfac=rhs.Sfac();               
00088   usedonhel=rhs.GetUsedOnHel();
00089   bailout=1; chidofbail=1200.0; niter=10;
00090   return *this;
00091 }//endof MdcxFittedHel::operator=
00092 
00093 MdcxFittedHel& MdcxFittedHel::Grow(const MdcxFittedHel& rhs, 
00094     const HepAList<MdcxHit> &ListOAdds) {
00095   copy(rhs);
00096   fail=rhs.Fail();
00097   chisq=rhs.Chisq();
00098   //rcs=rhs.Rcs();
00099   //prob=rhs.Prob();
00100   fittime=0.0;
00101   nhits=rhs.Nhits();
00102   itofit=0;
00103   quality=rhs.Quality();
00104   origin=rhs.Origin();
00105   xHitList=rhs.XHitList();
00106   sfac=rhs.Sfac();               
00107   usedonhel=rhs.GetUsedOnHel();
00108   bailout=1; chidofbail=1200.0; niter=10;
00109   int kkk=0; while (ListOAdds[kkk]){ListOAdds[kkk]->SetUsedOnHel(0); kkk++;}
00110   kkk=0; while (xHitList[kkk]){xHitList[kkk]->SetUsedOnHel(1); kkk++;}
00111   double spull;
00112   MdcxHel* temp = (MdcxHel*)(&rhs);
00113   kkk=0; while (ListOAdds[kkk]){
00114     if (ListOAdds[kkk]->GetUsedOnHel() == 0) {
00115       spull = ListOAdds[kkk]->pull(*temp)/sfac;
00116       chisq += spull*spull; 
00117       xHitList.append(ListOAdds[kkk]);
00118       nhits++; 
00119     }
00120     kkk++;
00121   }
00122 
00123   int ndof = nhits - nfree;
00124   prob = Mdcxprobab(ndof, chisq);
00125   rcs = chisq/ndof;
00126   return *this;
00127 }//endof MdcxFittedHel::Grow
00128 
00129 //accessors
00130 float MdcxFittedHel::Residual(int i){
00131   //float pull=xHitList[i]->pull(*this);
00132   //float E=xHitList[i]->e();
00133   //return pull*E;
00134   return xHitList[i]->residual(*this);
00135 }//endof Residual
00136 
00137 float MdcxFittedHel::Pull(int i){
00138   return xHitList[i]->pull(*this);
00139 }//endof Pulls
00140 
00141 int MdcxFittedHel::Fail(float Probmin)const {
00142   if(fail) return fail;
00143   if(prob < Probmin) return 1303;
00144   // now done in DoFit  if(fabs(omega)>omegmax) {return 1306;}
00145   return 0;
00146 } // endof Fail
00147 
00148 //utilities&workers
00149 
00150 void MdcxFittedHel::VaryRes() {
00151   int kbl = 0;
00152   while(xHitList[kbl]) xHitList[kbl++]->SetConstErr(0);
00153 }
00154 
00155 int MdcxFittedHel::ReFit(){
00156   fail = IterateFit();
00157   return fail;
00158 }//endof ReFit
00159 
00160 int MdcxFittedHel::IterateFit() {
00161   int ftemp = 1301; // not enough hits
00162   if (nfree > nhits) return ftemp;
00163   ftemp = 0;
00164 
00165   if(6 == debug) std::cout<<"IterateFit niter="<<niter<<  std::endl;
00166   if (niter >= 1) {
00167     float prevchisq = 0.0;
00168     for (int i = 0; i < niter; i++) {
00169       itofit = i + 1;
00170       ftemp = DoFit();
00171       if (6 == debug) {
00172         if (nfree == 5) {
00173           cout << " iteration number= " << i  << " chisq= " << chisq;
00174           cout << " nhits= " << nhits << " " << " fail= " << ftemp << endl;
00175         }
00176         print();
00177       }
00178       if (ftemp != 0) break;
00179       if(6 == debug)std::cout<<"in MdcxFittedHel::IterateFit() chisq="<<chisq<<" prechi2="<<prevchisq<<std::endl;//yzhang debug
00180       if ((fabs(chisq-prevchisq) < 0.01*chisq) || (chisq < 0.001)) break;  
00181       prevchisq = chisq;
00182     }//endof iter loop
00183   } else {
00184     float prevchisq = 0.0;
00185     chisq = 1000000.0;
00186     int iter = 0;
00187     while ((fabs(chisq-prevchisq) > 0.01) && (iter++ < 1000)) {
00188       prevchisq = chisq;
00189       ftemp = DoFit();
00190       if (ftemp != 0) break;
00191     }//endof (fabs(chisq-oldchisq).gt.0.01)
00192   }//endof (niter>=1)
00193   int ndof = nhits - nfree;
00194   prob = Mdcxprobab(ndof, chisq);
00195   rcs = chisq/ndof;
00196   return ftemp;
00197 }//endof IterateFit
00198 
00199 int MdcxFittedHel::DoFit() {
00200   int ftemp = 1301;
00201   // if(nfree>nhits) {return Fail;}
00202   if (nfree > nhits) return ftemp;
00203   double m_2pi = 2.0 * M_PI;
00204   ftemp = 0;
00205   //pointloop
00206   if (6 == debug) {
00207     std::cout << "in MdcxFittedHel::DoFit()  nfree = " << nfree
00208       << "  nhits = " << nhits << std::endl;
00209   }
00210 
00211   int norder = nfree;
00212   double A[10][10] = {{0.}}, B[10] = {0.}, D[10] = {0.}, det;
00213   chisq = 0.0;
00214 
00215   if (6 == debug) {
00216     std::cout << "xHitList.length " << xHitList.length() << "  ";
00217     for (int ii = 0; ii < xHitList.length(); ii++) {
00218       xHitList[ii]->print(std::cout, ii);
00219     }
00220     std::cout << std::endl << "sfac = " << sfac << std::endl;
00221   }
00222 
00223   for (int i = 0; i < nhits; i++) {
00224     std::vector<float> derivs = xHitList[i]->derivatives(*this);
00225     if (6 == debug) {
00226       cout << "derivs " << i<<" ";
00227       for (unsigned int ii = 0; ii < derivs.size(); ii++) {
00228         cout << setw(15)<< derivs[ii]; 
00229       }
00230       std::cout << std::endl;
00231     }
00232     if (sfac != 1.0) {
00233       for(unsigned int ipar = 0; ipar < derivs.size(); ipar++) {
00234         derivs[ipar] /= sfac;
00235         if(6 == debug) cout << " derivs[" << ipar << "] = " << derivs[ipar];
00236       }
00237       if(6 == debug) std::cout << std::endl;
00238     }
00239     chisq += derivs[0] * derivs[0];
00240     //outer parameter loop
00241     for (int ipar = 0; ipar < norder; ipar++) {
00242       D[ipar] += derivs[0] * derivs[ipar+1];
00243       //inner parameter loop
00244       for(int jpar = 0; jpar < norder; jpar++) {
00245         A[ipar][jpar] += derivs[ipar+1] * derivs[jpar+1];
00246       }//endof inner parameter loop
00247     }//endof outer parameter loop
00248   }//pointloop
00249   if (6 == debug) cout << "chisq = " << chisq << endl;
00250   if (chisq == 0 && nhits != 3) {  //zoujh: chisq is invalid??? FIXME
00251     ftemp = 1310;
00252     return ftemp;
00253   }
00254   if (6 == debug) {
00255     for (int ii = 0; ii < norder; ii++) {
00256       cout << "D["<< ii << "]: " << D[ii] << "     A:";
00257       for (int jj = 0; jj < norder; jj++) cout << "  " << A[ii][jj];
00258       cout << endl;
00259     }
00260   }
00261   //invert A
00262   int ierr;
00263   if (bailout) {
00264     ftemp = 1308;     // bailout
00265     int ndof = nhits - nfree;
00266     if (ndof > 0) {
00267       if (6 == debug){
00268         cout << "chisq " << chisq << " ndof " << ndof 
00269           << " chiperdof " << chisq/ndof 
00270           << " >?chidofbail " << chidofbail << endl;
00271       }
00272       float chiperdof = chisq/ndof;
00273       if(chiperdof > chidofbail) return ftemp;
00274     } else {
00275       if (6 == debug){
00276         cout << " ndof <=0 : chisq " << chisq 
00277           << " >? chidofbail/2.5 " << chidofbail/2.5 << endl;
00278       }
00279       if (chisq > chidofbail/2.5) return ftemp;  //FIXME
00280     }
00281   } // (bailout)
00282   ftemp = 0;
00283   ierr = Mdcxmatinv(&A[0][0], &norder, &det);
00284   if (6 == debug) cout << "ierr = " << ierr << endl;
00285   if (ierr == 0) {
00286     for(int ii = 0; ii < norder; ii++)
00287       for(int jj = 0; jj < norder; jj++)
00288         B[ii] += A[ii][jj] * D[jj];
00289     if (6 == debug) {
00290       for (int ii = 0; ii < norder; ii++) {
00291         cout << "B[" << ii << "]: " << B[ii] << "     A:";
00292         for (int jj = 0; jj < norder; jj++) cout << "  " << A[ii][jj];
00293         cout << endl;
00294       }
00295     }
00296     int bump = -1;
00297     if (qd0)   d0 -= B[++bump];
00298     if (qphi0) {
00299       phi0 -= B[++bump]; 
00300       if (phi0 > M_PI)  phi0 -= m_2pi;
00301       if (phi0 < -M_PI) phi0 += m_2pi;
00302       cphi0 = cos(phi0); sphi0 = sin(phi0);
00303     }
00304     if (qomega) {
00305       omega -= B[++bump];
00306       ominfl = (fabs(omega) < omin) ? 0 : 1;
00307     }
00308     if (qz0)   z0   -= B[++bump];
00309     if (qtanl) tanl -= B[++bump];
00310     if (qt0)   t0   -= B[++bump];
00311 
00312     x0 = X0(); y0 = Y0(); xc = Xc(); yc = Yc();
00313     if ( fabs(d0) > MdcxParameters::maxMdcRadius )   ftemp = 1305; 
00314     //if(g_fitOmega)g_fitOmega->fill(omega);
00315     if ( fabs(omega) > 10.0 ) ftemp = 1306; // Too tight (r < 1 cm)//yzhang FIXME 2009-11-03 
00316   } else {
00317     ftemp = ierr;
00318   }
00319   return ftemp;
00320 }//endof DoFit
00321 
00322 //is origin included in fit ?
00323 int MdcxFittedHel::OriginIncluded() {
00324   for(int i=0; xHitList[i]; i++) {
00325     int type=xHitList[i]->type();
00326     if(2==type) {               // origin "hit" ?
00327       //move to end, move fit point, return hit number
00328       xHitList.swap(i,nhits-1);
00329       return nhits-1;
00330     } 
00331   } 
00332   return -1;
00333 }
00334 
00335 int MdcxFittedHel::FitPrint() {
00336   cout << " d0 " << d0;
00337   cout << " phi0 " << phi0;
00338   cout << " omega " << omega;
00339   cout << " z0 " << z0;
00340   cout << " tanl " << tanl << endl;
00341   cout << " fail " << fail;
00342   cout << " chisq " << chisq;
00343   cout << " iter to fit " << itofit;
00344   cout << " sfac " << sfac;
00345   cout << " rcs " << rcs;
00346   cout << " prob " << prob;
00347   cout << " fittime " << fittime << endl;
00348   cout << " nhits= " << nhits << " xHitList.length " << xHitList.length() << endl;
00349   for (int ii = 0; ii < xHitList.length(); ii++) {
00350     xHitList[ii]->print(cout, ii);
00351   }
00352   cout<<endl;
00353 
00354   return 0;
00355 }//endof FitPrint
00356 
00357 int MdcxFittedHel::FitPrint(MdcxHel &hel, ostream &o) {
00358   double m_2pi=2.0*M_PI;
00359   double difphi0=phi0-hel.Phi0();
00360   if (difphi0>M_PI)difphi0-=m_2pi; if (difphi0<-M_PI)difphi0+=m_2pi; 
00361   cout << " difphi0= " << difphi0 << endl;
00362   cout << " difomega= " << omega-hel.Omega() << endl;
00363   cout << " difz0= " << z0-hel.Z0() << endl;
00364   cout << " diftanl= " << tanl-hel.Tanl() << endl;
00365   o << "FitPrint "; 
00366   o << "nhits "<< nhits << " fail " << fail << " chi2 " << chisq ;
00367   o << "rcs " << rcs << " prob " << prob <<endl;
00368   return 0;
00369 }//endof FitPrint
00370 
00371 //Find layer number of |hitno|
00372 int MdcxFittedHel::Layer(int hitno)const {
00373   if(hitno >= nhits) return 0;
00374   return xHitList[hitno]->Layer();
00375 } // endof Layer
00376 
00377 //Find superlayer numbber of |hitno|
00378 int MdcxFittedHel::SuperLayer(int hitno) const {
00379   if(hitno >= nhits) { return 0; }
00380   if(hitno < 0) { return 0; }
00381   return xHitList[hitno]->SuperLayer();
00382 } // endof SuperLayer
00383 

Generated on Tue Nov 29 23:13:37 2016 for BOSS_7.0.2 by  doxygen 1.4.7