/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Reconstruction/TrkReco/TrkReco-00-08-59-patch4-slc6tag/src/TTrackManager.cxx

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // $Id: TTrackManager.cxx,v 1.68.14.1 2013/03/04 07:22:52 xuqn Exp $
00003 //-----------------------------------------------------------------------------
00004 // Filename : TTrackManager.cc
00005 // Section  : Tracking
00006 // Owner    : Yoshi Iwasaki
00007 // Email    : yoshihito.iwasaki@kek.jp
00008 //-----------------------------------------------------------------------------
00009 // Description : A manager of TTrack information to make outputs as Reccdc_trk.
00010 //               See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
00011 //-----------------------------------------------------------------------------
00012 
00013 /* for isnan */
00014 #if defined(__sparc)
00015 #  if defined(__EXTENSIONS__)
00016 #    include <cmath>
00017 #  else
00018 #    define __EXTENSIONS__
00019 #    include <cmath>
00020 #    undef __EXTENSIONS__
00021 #  endif
00022 #elif defined(__GNUC__)
00023 #  if defined(_XOPEN_SOURCE)
00024 #    include <cmath>
00025 #  else
00026 #    define _XOPEN_SOURCE
00027 #    include <cmath>
00028 #    undef _XOPEN_SOURCE
00029 #  endif
00030 #endif
00031 
00032 #define TTRACKMANAGER_INLINE_DEFINE_HERE
00033 #include <values.h>
00034 #include <cstring>
00035 #include <cstdlib>
00036 
00037 //#include "belle.h"
00038 #include "CLHEP/String/Strings.h"
00039 //#include "basf/basfshm.h"
00040 #include "TrkReco/TrkReco.h"
00041 #include "TrkReco/TMDCWireHit.h"
00042 #include "TrkReco/TMDCWireHitMC.h"
00043 #include "TrkReco/TTrack.h"
00044 #include "TrkReco/TTrackMC.h"
00045 #include "TrkReco/TTrackHEP.h"
00046 #include "TrkReco/TTrackManager.h"
00047 #include "MdcTables/MdcTables.h"
00048 #include "MdcTables/TrkTables.h"
00049 
00050 #include "GaudiKernel/MsgStream.h"
00051 #include "GaudiKernel/AlgFactory.h"
00052 #include "GaudiKernel/ISvcLocator.h"
00053 #include "GaudiKernel/IMessageSvc.h"
00054 #include "GaudiKernel/IDataProviderSvc.h"
00055 #include "GaudiKernel/IDataManagerSvc.h"
00056 #include "GaudiKernel/SmartDataPtr.h"
00057 #include "GaudiKernel/IDataProviderSvc.h"
00058 #include "GaudiKernel/PropertyMgr.h"
00059 #include "GaudiKernel/IJobOptionsSvc.h"
00060 #include "GaudiKernel/Bootstrap.h"
00061 #include "GaudiKernel/StatusCode.h"
00062 
00063 #include "GaudiKernel/ContainedObject.h"
00064 #include "GaudiKernel/SmartRef.h"
00065 #include "GaudiKernel/SmartRefVector.h"
00066 #include "GaudiKernel/ObjectVector.h"
00067 #include "EventModel/EventModel.h"
00068 
00069 #include  "EvTimeEvent/RecEsTime.h"
00070 
00071 
00072 #include "Identifier/Identifier.h"
00073 #include "Identifier/MdcID.h"
00074 
00075 //#include "TrkReco/TSvdAssociator.h" // jtanaka 000925
00076 
00077 //extern BasfSharedMem * BASF_Sharedmem;
00078 
00079 TTrackManager::TTrackManager()
00080 : _maxMomentum(10.),
00081   _sigmaCurlerMergeTest(sqrt(100.)),
00082   _nCurlerMergeTest(4),
00083   _debugLevel(0),
00084   _fitter("TTrackManager Fitter"),
00085   _cFitter("TTrackManager 2D Fitter"),
00086   _s(0) {
00087 //    BASF_Sharedmem->allocate("TrkMgrSum", sizeof(struct summary));
00088 
00089   StatusCode scmgn = Gaudi::svcLocator()->service("MagneticFieldSvc",m_pmgnIMF); 
00090   if(scmgn!=StatusCode::SUCCESS) { 
00091     std::cout<< "ERROR: Unable to open Magnetic field service"<<std::endl;
00092   }
00093   // Get RawDataProviderSvc
00094   IRawDataProviderSvc* irawDataProviderSvc;
00095   StatusCode sc = Gaudi::svcLocator()->service("RawDataProviderSvc",irawDataProviderSvc); 
00096   m_rawDataProviderSvc = dynamic_cast<RawDataProviderSvc*> (irawDataProviderSvc);
00097   if(sc!=StatusCode::SUCCESS) { 
00098     std::cout<< "ERROR: Unable to load RawDataProviderSvc"<<std::endl;
00099   }
00100 }
00101 
00102 TTrackManager::~TTrackManager() {
00103 }
00104 
00105 std::string
00106 TTrackManager::version(void) const {
00107     return std::string("2.27");
00108 }
00109 
00110 void
00111 TTrackManager::dump(const std::string & msg, const std::string & pre) const {
00112     bool def = (msg == "") ? true : false;
00113 /*
00114     if (msg.find("summary") != std::string::npos || msg.find("detail") != std::string::npos) {
00115         struct summary s;
00116         //              bzero((char*)& s, sizeof(struct summary));
00117         memset((char*)& s, 0, sizeof(struct summary));
00118         for (int i = 0; i < BASF_Sharedmem->nprocess(); i++) {
00119             int size;
00120             struct summary & r = * (struct summary *)
00121                 BASF_Sharedmem->get_pointer(i, "TrkMgrSum", & size);
00122             s._nEvents += r._nEvents;
00123             s._nTracks += r._nTracks;
00124             s._nTracksAll += r._nTracksAll;
00125             s._nTracks2D += r._nTracks2D;
00126             s._nTracksFinal += r._nTracksFinal;
00127             s._nSuperMoms += r._nSuperMoms;
00128             s._nToBeMerged += r._nToBeMerged;
00129             s._nToBeMergedMoreThanTwo += r._nToBeMergedMoreThanTwo;
00130         }
00131 
00132         std::cout << pre;
00133         std::cout << "all events : " << s._nEvents << std::endl;
00134         std::cout << pre;
00135         std::cout << "all tracks : " << s._nTracksAll << std::endl;
00136         std::cout << pre;
00137         std::cout << "    good tracks  : " << s._nTracks << std::endl;
00138         std::cout << pre;
00139         std::cout << "    2D tracks    : " << s._nTracks2D << std::endl;
00140         std::cout << pre;
00141         std::cout << "    final tracks : " << s._nTracksFinal << std::endl;
00142         std::cout << pre;
00143         std::cout << "    super mom.   : " << s._nSuperMoms << std::endl;
00144         std::cout << pre;
00145         std::cout << "    to be mreged : " << s._nToBeMerged << std::endl;
00146         std::cout << pre;
00147         std::cout << "    to be mreged2: " << s._nToBeMergedMoreThanTwo
00148                   << std::endl;
00149     }
00150 */
00151     if (def || msg.find("eventSummary") != std::string::npos || msg.find("detail") != std::string::npos) {
00152         std::cout << pre
00153                   << "tracks reconstructed : " << _tracksAll.length()
00154                   << std::endl;
00155         std::cout << pre
00156                   << "good tracks          : " << _tracks.length()
00157                   << std::endl;
00158         std::cout << pre
00159                   << "2D tracks            : " << _tracks2D.length()
00160                   << std::endl;
00161         std::cout << pre
00162                   << "Track list:" << std::endl;
00163 
00164         std::string tab = pre;
00165         std::string spc = " ";
00166         for (unsigned i = 0; i < _tracksAll.length(); i++) {
00167             std::cout << tab << TrackDump(* _tracksAll[i]) << std::endl;
00168             if (msg.find("hits") != std::string::npos || msg.find("detail") != std::string::npos)
00169                 Dump(_tracksAll[i]->links(), "hits sort flag");
00170         }
00171     }
00172 }
00173 
00174 void 
00175 TTrackManager::maskCurlHits(const AList<TMDCWireHit> &axial,
00176                             const AList<TMDCWireHit> &stereo,
00177                             const AList<TTrack> &tracks) const {
00178 //...Coded by jtanaka...
00179 
00180   int i = 0;
00181   while(const TTrack *t = tracks[i++]){
00182     int j = 0;
00183     while(TMDCWireHit *a = axial[j++]){
00184       double x = t->helix().center().x() - a->xyPosition().x();
00185       double y = t->helix().center().y() - a->xyPosition().y();
00186       double r = sqrt(x*x+y*y);
00187       double R = fabs(t->helix().radius());
00188         double q = t->helix().center().x()*a->xyPosition().y() - 
00189           t->helix().center().y()*a->xyPosition().x();
00190         double qq = q*t->charge();
00191       if(R-2. < r && r < R+2. && qq > 0.){
00192         a->state(a->state() | WireHitUsed);
00193       }
00194     }
00195     j = 0;
00196     while(TMDCWireHit *s = stereo[j++]){
00197       double x = t->helix().center().x() - s->xyPosition().x();
00198       double y = t->helix().center().y() - s->xyPosition().y();
00199       double r = sqrt(x*x+y*y);
00200       double R = fabs(t->helix().radius());
00201         double q = t->helix().center().x()*s->xyPosition().y() - 
00202           t->helix().center().y()*s->xyPosition().x();
00203         double qq = q*t->charge();
00204       if(R-2.5 < r && r < R+2.5 && qq > 0.){
00205         s->state(s->state() | WireHitUsed);
00206       }
00207     }
00208   }
00209 }
00210 
00211 void
00212 TTrackManager::salvage(const AList<TMDCWireHit> & hits) const {
00213 
00214 #ifdef TRKRECO_DEBUG_DETAIL
00215     std::cout << name() << " ... salvaging" << std::endl;
00216     std::cout << "    # of given hits=" << hits.length() << std::endl;
00217 #endif
00218 
00219     //...Check arguments...
00220     unsigned nTracks = _tracks.length();
00221     if (nTracks == 0) return;
00222     unsigned nHits = hits.length();
00223     if (nHits == 0) return;
00224 
00225     //...Hit loop...
00226     for (unsigned i = 0; i < nHits; i++) {
00227         TMDCWireHit & h = * hits[i];
00228 
00229         //...Already used?...
00230         if (h.state() & WireHitUsed) continue;
00231 #ifdef TRKRECO_DEBUG_DETAIL
00232         std::cout << "    checking " << h.wire()->name() << std::endl;
00233 #endif
00234 
00235         //...Select the closest track to a hit...
00236         TTrack * best = closest(_tracks, h);
00237 #ifdef TRKRECO_DEBUG_DETAIL
00238         if (! best) {
00239             std::cout << "        no track candidate returned";
00240             std::cout << "by TTrackManager::closest" << std::endl;
00241         }
00242 #endif
00243         if (! best) continue;
00244 
00245         //...Try to append this hit...
00246         AList<TMLink> link;
00247         link.append(new TMLink(0, & h));
00248         best->appendByApproach(link, 30.);
00249         // best->assign(WireHitConformalFinder);
00250         best->finder(TrackTrackManager);
00251     }
00252 }
00253 
00254 TTrack *
00255 TTrackManager::closest(const AList<TTrack> & tracks,
00256                        const TMDCWireHit & hit) const {
00257 
00258     TMLink t;
00259     t.hit(& hit);
00260     unsigned n = tracks.length();
00261     double minDistance = MAXDOUBLE;
00262     TTrack * minTrk = NULL;
00263 
00264     //...Loop over all tracks...
00265     for (unsigned i = 0; i < n; i++) {
00266         TTrack & trk = * tracks[i];
00267         int err = trk.approach(t);
00268         if (err < 0) continue;
00269         if (minDistance > t.distance()) {
00270             minDistance = t.distance();
00271             minTrk = & trk;
00272         }
00273     }
00274 
00275     return minTrk;
00276 }
00277 
00278 
00279 StatusCode
00280 //TTrackManager::makeTds(bool doClear, int tkStat) { //yzhang change interface
00281 TTrackManager::makeTds(RecMdcTrackCol* trkcol, RecMdcHitCol* hitcol, int tkStat, int brunge , int cal) { //yzhang change interface 2010-05-14 
00282   IMessageSvc *msgSvc;
00283   Gaudi::svcLocator()->service("MessageSvc", msgSvc);
00284 
00285   MsgStream log(msgSvc, "TrkReco");
00286 
00287   IDataProviderSvc* eventSvc = NULL;
00288   Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
00289 
00290 #ifdef TRKRECO_DEBUG
00291   if (eventSvc) {
00292      log << MSG::INFO << "makeTds:event Svc has been found" << endreq;
00293   } else {
00294      log << MSG::FATAL << "makeTds:Could not find eventSvc" << endreq;
00295     return StatusCode::FAILURE ;
00296   }
00297 #endif
00298 //check whether  Recon  already registered
00299 //   DataObject *aReconEvent;
00300 //   eventSvc->findObject("/Event/Recon",aReconEvent);
00301 //   if(!aReconEvent) {
00302 //      ReconEvent* recevt = new ReconEvent;
00303 //      StatusCode sc = eventSvc->registerObject("/Event/Recon",recevt );
00304 //      if(sc!=StatusCode::SUCCESS) {
00305 //       log << MSG::FATAL << "Could not register ReconEvent" <<endreq;
00306 //       return( StatusCode::FAILURE);
00307 //     }
00308 //  }
00309 //
00310 //  /// Unregister Tracks
00311 //  IDataManagerSvc *dataManSvc;
00312 //  if(doClear){//yzhang add, do NOT clear Tds when associate rec
00313 //    dataManSvc= dynamic_cast<IDataManagerSvc*>(eventSvc);
00314 //    DataObject *aTrackCol;
00315 //    eventSvc->findObject("/Event/Recon/RecMdcTrackCol",aTrackCol);
00316 //    if(aTrackCol != NULL) {
00317 //      dataManSvc->clearSubTree("/Event/Recon/RecMdcTrackCol");
00318 //      eventSvc->unregisterObject("/Event/Recon/RecMdcTrackCol");
00319 //    }
00320 //    DataObject *aRecHitCol;
00321 //    eventSvc->findObject("/Event/Recon/RecMdcHitCol",aRecHitCol);
00322 //    if(aRecHitCol != NULL) {
00323 //      dataManSvc->clearSubTree("/Event/Recon/RecMdcHitCol");
00324 //      eventSvc->unregisterObject("/Event/Recon/RecMdcHitCol");
00325 //    }
00326 //  }
00327 //  /// Writing
00328 //  RecMdcTrackCol* trkcol = new RecMdcTrackCol;   
00329 //  RecMdcHitCol* hitcol = new RecMdcHitCol;
00330 
00331   unsigned n = _tracks.length();
00332   int nTdsTk = trkcol->size(); 
00333   for (int i =0; i < n; i++) {
00334     RecMdcTrack* trk = new RecMdcTrack;
00335     TTrack* t = _tracks[i];
00336     int trackindex = i + nTdsTk;//for combination
00337 
00338     HitRefVec  hitrefvec;
00339     AList<TMLink> hits = t->links();
00340     float chisq=0;
00341 
00342     HepPoint3D pos = t->helix().pivot();
00343     int charge = -1;
00344     HepVector m_a(5,0);
00345     m_a = t->helix().a();
00346     int statfinder=t->getFinderType();
00347     if(abs(statfinder)>1000&&brunge)statfinder=103;
00348     if(abs(statfinder)>1000&&!brunge)statfinder=3;
00349     //be cautious
00350     if(!brunge)m_a[2] = -m_a[2];
00351     if(abs(m_a[1])>999)m_a[1]=0;
00352     while(m_a[1]<0){m_a[1]+=2*pi;}
00353     while(m_a[1]>2*pi){m_a[1]-=2*pi;}  
00355     const double x0 = t->helix().pivot().x();
00356     const double y0 = t->helix().pivot().y();
00357 
00358     const double dr    =  m_a[0];
00359     const double phi0  =  m_a[1];
00360     const double kappa =  m_a[2]; 
00361     const double dz    =  m_a[3];
00362     const double tanl  =  m_a[4];
00363    
00364     const double Bz = -1000*(m_pmgnIMF->getReferField());
00365     const double alpha = 333.564095 / Bz;
00366 
00367     const double cox = x0 + dr*cos(phi0) - alpha*cos(phi0)/kappa;
00368     const double coy = y0 + dr*sin(phi0) - alpha*sin(phi0)/kappa; 
00369 
00370 
00371     unsigned nHits = t->links().length();
00372     unsigned nStereos = 0;
00373     unsigned firstLyr = 44;
00374     unsigned lastLyr = 0;
00375     for (unsigned j=0; j<nHits; j++){
00376 
00377            TMLink * l = hits[j];
00378 
00379            HepPoint3D ontrack = l->positionOnTrack();
00380            HepPoint3D onwire = l->positionOnWire();
00381            HepPoint3D dir(ontrack.y()-coy,cox-ontrack.x(),0);
00382            double pos_phi=onwire.phi();
00383            double dir_phi=dir.phi();
00384            while(pos_phi>pi){pos_phi-=pi;}
00385            while(pos_phi<0){pos_phi+=pi;}
00386            while(dir_phi>pi){dir_phi-=pi;}
00387            while(dir_phi<0){dir_phi+=pi;}
00388            double entrangle=dir_phi-pos_phi;
00389            while(entrangle>pi/2)entrangle-=pi;
00390            while(entrangle<(-1)*pi/2)entrangle+=pi; 
00391            
00392            //jialk setFltLen to tds
00393             int imass = 3;
00394             float tl = t->helix().a()[4];
00395             float f = sqrt(1. + tl * tl);
00396             float s = fabs(t->helix().curv()) * fabs(l->dPhi()) * f;
00397             float p = f / fabs(t->helix().a()[2]);
00398             float Mass[5] = {0.000511,0.105,0.140,0.493677,0.98327};
00399             double tof = s/29.98*sqrt(1.0+(Mass[imass-1]/p)*(Mass[imass-1]/p));
00400             
00401            RecMdcHit*  hit = new RecMdcHit;
00402            hit->setId(l->hit()->reccdc()->id);
00403 //         hit->setTrkId(i);
00404            hit->setTrkId(trackindex); //for combination
00405            hit->setDriftDistLeft(l->drift(0));
00406            hit->setDriftDistRight(l->drift(1));
00407            hit->setErrDriftDistLeft(l->dDrift(0));
00408            hit->setErrDriftDistRight(l->dDrift(1));
00409            hit->setChisqAdd(l->pull());
00410            hit->setFlagLR(l->leftRight());
00411          //  hit->setStat(l->hit()->state());
00412            hit->setStat(1);
00413          //  std::cout<<"state :"<< l->hit()->state() << std::endl;
00414            hit->setAdc(l->hit()->reccdc()->adc);
00415 //           hit->setTdc((l->hit()->reccdc()->tdc + t0bes)/0.09375); //jialk
00416            hit->setTdc(l->hit()->reccdc()->timechannel);
00417            hit->setFltLen(tof * 30);//jialk 
00418           // std::cout<<"tdc :"<< l->hit()->reccdc()->tdc <<"  "<<"t0bes : "<< t0bes <<std::endl;
00419            if(cal){hit->setDoca(l->distancenew());} 
00420        else{hit->setDoca(l->distance());}
00421            hit->setZhit(l->positionOnTrack().z());
00422            
00424            hit->setEntra(entrangle);
00425            hit->setDriftT(l->getDriftTime());
00426            
00427 
00428            Identifier hitIdf = MdcID:: wire_id(l->wire()->layerId(),l->wire()->localId());
00429            hit->setMdcId(hitIdf);          
00430            hitcol->push_back(hit);
00431 
00432            SmartRef<RecMdcHit> refhit(hit);
00433            hitrefvec.push_back(refhit);           
00434            chisq += l->pull();
00435            if(l->wire()->stereo()) ++nStereos;
00436            if(firstLyr > l->wire()->layerId()) firstLyr = l->wire()->layerId();
00437            if(lastLyr < l->wire()->layerId()) lastLyr = l->wire()->layerId();
00438     }
00439 
00440     
00441     HepSymMatrix m_ea = t->helix().Ea();
00442     double errorMat[15];
00443     int k = 0;
00444     for (int ie = 0 ; ie < 5 ; ie ++){
00445       for (int je = ie ; je < 5 ; je ++) {
00446         errorMat[k] = m_ea[ie][je];
00447         k++;
00448       }
00449     }
00450 
00451     trk->setTrackId(trackindex);
00452     trk->setHelix(m_a);
00453     trk->setPxy(t->pt());
00454     trk->setPx(t->pt() * (-sin(t->helix().phi0())));
00455     trk->setPy(t->pt() * cos(t->helix().phi0()));
00456     trk->setPz(t->pz());
00457     trk->setP(t->ptot());
00458 
00459         //jialk
00460         double theta = acos((t->pz())/t->ptot());
00461         trk->setTheta(theta);
00462 
00463     double poca_dr = t->helix().dr();
00464     double poca_phi0 = t->helix().phi0();
00465     trk->setPhi(poca_phi0);
00466     HepPoint3D poca(pos.x()+poca_dr*cos(poca_phi0),
00467         pos.y()+poca_dr*sin(poca_phi0),
00468         pos.z()+t->helix().dz());
00469     trk->setPoca(poca);
00470     trk->setX(poca.x());
00471     trk->setY(poca.y());
00472     trk->setZ(poca.z());
00473     trk->setR(sqrt(poca.x()*poca.x() + poca.y()*poca.y()));
00474     trk->setPivot(pos);
00475     trk->setVX0(pos.x());
00476     trk->setVY0(pos.y());
00477     trk->setVZ0(pos.z());
00478     
00479     trk->setError(m_ea);
00480 //    trk->setError(errorMat);   //...............2
00481 
00482 //    double poca_dr = t->helix().dr();
00483 //    double poca_phi0 = t->helix().phi0();
00484 //    HepPoint3D poca(poca_dr*cos(poca_phi0),poca_dr*sin(poca_phi0),t->helix().dz()); 
00485 //    trk->setVX0(pos.x()+poca_dr*cos(poca_phi0));
00486 //    trk->setVY0(pos.y()+poca_dr*sin(poca_phi0));
00487 //    trk->setVZ0(pos.z()+t->helix().dz());
00488 //    cout<<"pivot:("<<pos.x()<<","<<pos.y()<<","<<pos.z()<<")"<<endl;
00489 //    cout<<"poca:("<<trk->getVX0()<<","<<trk->getVY0()<<","<<trk->getVZ0()<<")"<<endl;
00490 
00491     trk->setChi2(chisq);
00492     trk->setNdof(nHits-5);
00493     if (t->quality() & TrackQuality2D) trk->setNdof(nHits-3);
00494 
00495     TMLink * last = OuterMost(hits);
00496     t->approach(*last);
00497     trk->setFiTerm(last->dPhi());
00498 
00499     trk->setNhits(nHits);
00500     trk->setNster(nStereos);
00501     trk->setStat(statfinder);//yzhang add stat: ConformalFinder=2, CurlFiner=3
00502     //xuqn 2013-02-26
00503     //trk->setCharge(int(t->charge())*(-1));
00504     if(!brunge) trk->setCharge(int(t->charge())*(-1));
00505     else trk->setCharge(t->charge());
00506     trk->setVecHits(hitrefvec); 
00507     trk->setFirstLayer(firstLyr);
00508     trk->setLastLayer(lastLyr);
00509     //cout<<"first: "<<firstLyr<<"  last: "<<lastLyr<<endl;
00510     trkcol->push_back(trk);
00511   } 
00512   
00513 // StatusCode trksc;
00514 // trksc = eventSvc->registerObject("/Event/Recon/RecMdcTrackCol", trkcol);
00515 // if( trksc.isFailure() ) {
00516 //   log << MSG::FATAL << "Could not register MdcTrack" << endreq;
00517 //   return trksc; 
00518 // }
00519 //
00520 // StatusCode hitsc = eventSvc->registerObject("/Event/Recon/RecMdcHitCol", hitcol);
00521 // if ( hitsc.isFailure() ) {
00522 //   log << MSG::FATAL << "Could not register MdcRecHit" << endreq;
00523 //   return hitsc;
00524 // }
00525 //log << MSG::INFO << "MdcTrackCol registered successfully!" <<endreq;
00526    
00528 #ifdef TRKRECO_DEBUG
00529   SmartDataPtr<RecMdcTrackCol> newtrkCol(eventSvc,"/Event/Recon/RecMdcTrackCol");
00530   if (!newtrkCol) { 
00531      log << MSG::FATAL << "Could not find RecMdcTrackCol" << endreq;
00532      return( StatusCode::FAILURE);
00533   }
00534   log << MSG::INFO << "Begin to check RecMdcTrackCol"<<endreq; 
00535    RecMdcTrackCol::iterator iter_trk = newtrkCol->begin();
00536    for( ; iter_trk != newtrkCol->end(); iter_trk++){
00537      const RecMdcTrack* tk = *iter_trk;
00538      std::cout<< "//==== "<<name()<<" print RecMdcTrack No."<<tk->trackId()<<" :"<< std::endl;
00539      cout <<" dr "<<tk->helix(0)
00540        <<" phi0 "<<tk->helix(1)
00541        <<" cpa "<<tk->helix(2)
00542        <<" z0 "<<tk->helix(3)
00543        <<" tanl "<<tk->helix(4)
00544        <<endl;
00545      bool printTrackDetail = true;
00546      if(printTrackDetail){
00547        std::cout<<" q "<<tk->charge() 
00548          <<" theta "<<tk->theta()
00549          <<" phi "<<tk->phi()
00550          <<" x0 "<<tk->x()
00551          <<" y0 "<<tk->y()
00552          <<" z0 "<<tk->z()
00553          <<" r0 "<<tk->r()
00554          <<endl;
00555        std::cout <<" p "<<tk->p()
00556          <<" pt "<<tk->pxy()
00557          <<" pxyz("<<tk->px()
00558          <<","<<tk->py()
00559          <<","<<tk->pz()
00560          <<")"<<endl;
00561        std::cout<<" tkStat "<<tk->stat()
00562          <<" chi2 "<<tk->chi2()
00563          <<" ndof "<<tk->ndof()
00564          <<" nhit "<<tk->getNhits()
00565          <<" nst "<<tk->nster()
00566          <<endl;
00567        bool printErrMat = true;
00568        if(printErrMat){
00569          std::cout<< "errmat   " << std::endl;
00570          for (int i=0; i<15; i++){ std::cout<< " "<<tk->err(i); }
00571          std::cout<< "   " << std::endl;
00572        }
00573      }
00574 
00575      bool printHit = true;
00576      if(printHit){
00577        int haveDigi[43][288];
00578        bool printMcTk = true;
00579        if(printMcTk) {
00580          for(int ii=0;ii<43;ii++){
00581            for(int jj=0;jj<43;jj++){
00582              haveDigi[ii][jj]=-9999;
00583            }
00584          }
00585          MdcDigiVec mdcDigiVec = m_rawDataProviderSvc->getMdcDigiVec();
00586          MdcDigiCol::iterator iter = mdcDigiVec.begin();
00587          std::cout<<name()<<"//==== "<<name()<<" print MdcDigiVec, nDigi="<<mdcDigiVec.size()<<" :"<<std::endl;
00588          for (int iDigi=0;iter!= mdcDigiVec.end(); iter++,iDigi++ ) {
00589            long l = MdcID::layer((*iter)->identify());
00590            long w = MdcID::wire((*iter)->identify());
00591            haveDigi[l][w]=(*iter)->getTrackIndex();
00592          }
00593        }
00594 
00595        int nhits = tk->getVecHits().size();
00596        std::cout<<"nHits=" <<nhits<< std::endl;
00597        cout<<"No. ";
00598        if(printMcTk) cout<<"mcTkId ";
00599        cout<<"(layer,wire,lr) stat z"<<endl;
00600        for(int ii=0; ii <nhits ; ii++){
00601          Identifier id(tk->getVecHits()[ii]->getMdcId());
00602          int layer = MdcID::layer(id);
00603          int wire = MdcID::wire(id);
00604          cout<<ii<<":";
00605          if(printMcTk) { cout<<haveDigi[layer][wire]; }
00606          cout<<" ("<<layer<<","<<wire
00607            <<","<<tk->getVecHits()[ii]->getFlagLR()
00608            <<")  "<<tk->getVecHits()[ii]->getStat()
00609            <<"  "<<tk->getVecHits()[ii]->getZhit()<< " "<<std::endl;
00610        }//end of hit list
00611        std::cout << "  "<< std::endl;
00612      }
00613        /*
00614           std::cout << "retrieved MDC track:"
00615           << "Track Id: " << (*iter_trk)->getTrkId()
00616           << "   Pivot: " << (*iter_trk)->getVX0() << " "
00617           << (*iter_trk)->getVY0() << " " << (*iter_trk)->getVZ0() 
00618           << endl
00619           << "Phi0: " << (*iter_trk)->getFi0() << "   Error of Phi0 "
00620           << (*iter_trk)->getError()(2,2) << endreq
00621           << "kappa: " << (*iter_trk)->getCpa() << "   Error of kappa "
00622           << (*iter_trk)->getError()(3,3) << endreq
00623           << "Tanl: " << (*iter_trk)->getTanl() << "   Error of Tanl "
00624           << (*iter_trk)->getError()(5,5) << endreq      
00625           << "Chisq of fit: "<< (*iter_trk)->getChisq()
00626           << "   Phi terminal: "<< (*iter_trk)->getFiTerm()
00627           << endl
00628           << "Number of hits: "<< (*iter_trk)->getNhits()
00629           << "   Number of stereo hits " << (*iter_trk)->getNster()
00630           << endreq;
00631 
00632           log << MSG::DEBUG << "hitList of this  track:" << endreq;
00633           std::vector<MdcRecHit*> gothits = (*iter_trk)->getVecHits();
00634           std::vector<MdcRecHit*>::iterator it_gothit = gothits.begin();
00635           for( ; it_gothit != gothits.end(); it_gothit++){
00636           log << MSG::DEBUG << "hits Id: "<<(*it_gothit)->getId()
00637           << "   hits DDL&DDR " <<(*it_gothit)->getDriftDistLeft()
00638           << "   hits MDC IDentifier " <<(*it_gothit)->getMdcId()
00639           << endreq      
00640           << "   hits TDC " <<(*it_gothit)->getTdc()  
00641           << "   hits ADC " <<(*it_gothit)->getAdc() << endreq;
00642           } 
00643           */
00644    }
00645 #endif
00646    return StatusCode::SUCCESS;
00647 }
00648 
00649 
00650 
00651 void
00652 TTrackManager::saveTables(void) {
00653 #ifdef TRKRECO_DEBUG
00654   std::cout << "TTrackManager::saveTables ... # 3D tracks=" << _tracks.length()
00655     << ", # 2D tracks=" << _tracks2D.length()
00656     << ", all tracks=" << _tracksAll.length() << std::endl;
00657 #endif
00658 
00659   //...For 3D tracks...
00660   AList<TTrack> badTracks;
00661   unsigned n = _tracks.length();
00662   unsigned * id = (unsigned *) malloc(n * sizeof(unsigned));
00663   //    bzero((char *) id, n * sizeof(unsigned));
00664   memset((char *) id, 0, n * sizeof(unsigned));
00665   for (unsigned i = 0; i < n; i++) {
00666     TTrack & t = * _tracks[i];
00667     if (! t.nLinks()) {
00668       badTracks.append((TTrack &) t);
00669       continue;
00670     }
00671 
00672     //...Copy track parameters...
00673     MdcRec_trk * r = 0;
00674     MdcRec_trk_add * a = 0;
00675     int err = copyTrack(t, & r, & a);
00676     if (err) {
00677       badTracks.append(t);
00678       continue;
00679     }
00680     _tracksFinal.append(t);
00681 
00682     //...Type and quality...
00683     id[i] = r->id;
00684     r->stat = t.state();
00685     a->kind = t.type();
00686     a->decision = t.finder();
00687     a->stat = t.fitting();
00688     //          if (a->m_kind == TrackTypeCosmic) {
00689     //              a->m_quality = TrackQualityCosmic;
00690     //          }
00691     //          if (t.daughter() && (_tracks.index(t.daughter()) >= 0))
00692     //              a->m_daughter = _tracks.index(t.daughter()) + 1;
00693 
00694   }
00695 
00696   //...Daughter treatment...
00697   for (unsigned i = 0; i < n; i++) {
00698 
00699 #ifdef TRKRECO_DEBUG_DETAIL
00700     std::cout << "id[" << i << "]=" << id[i] << std::endl;
00701 #endif
00702     if (! (id[i])) continue;
00703     if (! (_tracks[i]->daughter())) continue;
00704 
00705     int dId = _tracks.index(_tracks[i]->daughter());
00706 
00707 #ifdef TRKRECO_DEBUG_DETAIL
00708     std::cout << "    dId=" << dId;
00709     if (dId >= 0) std::cout << ", id[dId]=" << id[dId];
00710     std::cout << std::endl;
00711 #endif
00712 
00713     if (dId >= 0) {
00714       if (id[dId]) {
00715         //              reccdc_trk_add * a = (reccdc_trk_add *)
00716         //                  BsGetEnt(RECMDC_TRK_ADD, id[i], BBS_No_Index);
00717         //              a->m_daughter = id[dId];
00718         MdcRec_trk_add* a = &(*MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
00719       }
00720     }
00721   }
00722   free(id);
00723 
00724   //...Remove bad tracks...
00725   _tracks.remove(badTracks);
00726   badTracks.removeAll();
00727 
00728   //...For 2D tracks...
00729   n = _tracks2D.length();
00730   for (unsigned i = 0; i < n; i++) {
00731     TTrack & t = * _tracks2D[i];
00732 
00733     //...Copy track parameters...
00734     MdcRec_trk * r = 0;
00735     MdcRec_trk_add * a = 0;
00736     int err = copyTrack(t, & r, & a);
00737     if (err) {
00738 #ifdef TRKRECO_DEBUG
00739       std::cout << "TTrackManager::saveTables !!! bad 2D tracks found"
00740         << " : err=" << err << std::endl
00741         << TrackDump(t) << std::endl;
00742 #endif
00743       badTracks.append(t);
00744       continue;
00745     }
00746     _tracksFinal.append(t);
00747 
00748     //...Reset helix parameter...
00749     //          r->m_helix[3] = 0.;
00750     //          r->m_helix[4] = 0.;
00751     //          r->m_nhits -= r->m_nster;
00752     //          r->m_nster = 0;
00753 
00754     //...Table filling...
00755     r->stat = t.state();
00756     a->kind = t.type();
00757     a->decision = t.finder();
00758     // a->m_quality = t.quality();
00759     a->quality = TrackQuality2D;
00760     a->stat = t.fitting();
00761 
00762 #ifdef TRKRECO_DEBUG
00763     if ((r->ndf == 0) && (r->chiSq > 0.)) {
00764       std::cout << "TTrackManager::saveTables !!! chisq>0 with ndf=0."
00765         << std::endl
00766         << "    Here is a track dump"
00767         << "    " << TrackDump(t) << std::endl;
00768       t.dump("detail");
00769     }
00770     if ((r->ndf > 0) && (r->chiSq == 0.)) {
00771       std::cout << "TTrackManager::saveTables !!! chisq=0 with ndf>0."
00772         << std::endl
00773         << "    Here is a track dump"
00774         << "    " << TrackDump(t) << std::endl;
00775       t.dump("detail");
00776     }
00777 
00778     if (r->ndf == 0)
00779       std::cout << "TTrackManager::saveTables ... ndf = 0" << std::endl
00780         << "    " << TrackDump(t) << std::endl;
00781     if (r->chiSq == 0.)
00782       std::cout << "TTrackManager::saveTables ... chisq = 0" << std::endl
00783         << "    " << TrackDump(t) << std::endl;
00784 #endif
00785   }
00786   _tracks2D.remove(badTracks);
00787 
00788   //...Statistics...
00789   ++_s->_nEvents;
00790   _s->_nTracks += _tracks.length();
00791   _s->_nTracksAll += _tracksAll.length();
00792   _s->_nTracks2D += _tracks2D.length();
00793   _s->_nTracksFinal += _tracksFinal.length();
00794 }
00795 
00796 void
00797 TTrackManager::saveMCTables(void) const {
00798   unsigned n = _tracksFinal.length();
00799   for (unsigned i = 0; i < n; i++) {
00800     const TTrack & t = * _tracksFinal[i];
00801 
00802     //  struct reccdc_trk * r;
00803     //  r = (struct reccdc_trk *) BsGetEnt(RECMDC_TRK, i + 1, BBS_No_Index);
00804     MdcRec_trk* r = &(*MdcRecTrkCol::getMdcRecTrkCol())[i];
00805 
00806     //...Set type...
00807 
00808     //...Hit loop...
00809     const AList<TMLink> & hits = t.finalHits();
00810     unsigned nHits = hits.length();
00811     for (unsigned j = 0; j < nHits;  j++) {
00812       TMLink * l = hits[j];
00813       MdcRec_wirhit * h = l->hit()->reccdc();
00814       MdcDat_mcwirhit * m = l->hit()->mc()->datcdc();
00815       m->trk = r;
00816       //            struct reccdc_mctrk2hep * c;
00817       //            c = (struct reccdc_mctrk2hep *) BsNewEnt(RECMDC_MCTRK2HEP);
00818       MdcRec_mctrk2hep* c =  new MdcRec_mctrk2hep;
00819       MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol()->push_back(*c);
00820       c->wir = h;
00821       c->trk = r;
00822       c->hep = l->hit()->mc()->hep()->gen();
00823     }
00824 
00825     const TTrackMC * const mc = t.mc();
00826     //  struct reccdc_mctrk * m;
00827     //  m = (struct reccdc_mctrk *) BsNewEnt(RECMDC_MCTRK);
00828     // //       MdcRec_mctrk* m = &(*MdcRecMctrkCol::getMdcRecMctrkCol())[0];
00829     MdcRec_mctrk* m = new MdcRec_mctrk;
00830     MdcRecMctrkCol::getMdcRecMctrkCol()->push_back(*m);
00831     m->wirFrac = mc->wireFraction();
00832     m->wirFracHep = mc->wireFractionHEP();
00833     m->charge = mc->charge();
00834     m->ptFrac = mc->ptFraction();
00835     m->pzFrac = mc->pzFraction();
00836     m->quality = mc->quality();
00837     if (mc->hep()) m->hep = mc->hep()->gen();
00838     else           m->hep = 0;
00839   }
00840 }
00841 
00842 void
00843 TTrackManager::movePivot(void) {
00844   unsigned n = _tracksAll.length();
00845   //  unsigned n = _tracks.length();
00846   for (unsigned i = 0; i < n; i++) {
00847     if (_tracksAll[i]->nLinks()) _tracksAll[i]->movePivot();
00848     //  if (_tracks[i]->nLinks()) _tracks[i]->movePivot();
00849   }
00850   nameTracks();
00851 }
00852 
00853 void
00854 TTrackManager::clear(void) {
00855   HepAListDeleteAll(_tracksAll);
00856   _tracks.removeAll();
00857   _tracks2D.removeAll();
00858   _tracksFinal.removeAll();
00859   HepAListDeleteAll(_associateHits);
00860   static bool first = true;
00861   if (first) {
00862     first = false;
00863     int size;
00864     //  _s = (struct summary *)
00865     //      BASF_Sharedmem->get_pointer(BASF_Sharedmem->get_id(),
00866     //                                  "TrkMgrSum",
00867     //                                  & size);
00868   }
00869 }
00870 
00871 void
00872 TTrackManager::finish(void) {
00873   refit();
00874   movePivot();
00875   if (_debugLevel > 1) {
00876     std::cout << name() << " ... finishing" << std::endl;
00877     //  unsigned n = _tracksAll.length();
00878     unsigned n = _tracks.length();
00879     for (unsigned i = 0; i < n; i++) {
00880       //            TTrack & t = * _tracksAll[i];
00881       TTrack & t = * _tracks[i];
00882       std::cout << "    " << t.name() << std::endl;
00883       t.dump("hits mc track flag sort", "    ");
00884     }
00885   }
00886 }
00887 
00888 void
00889 TTrackManager::append(AList<TTrack> & list) {
00890   _tracksAll.append(list);
00891   _tracks.append(selectGoodTracks(list));
00892   list.removeAll();
00893 }
00894 
00895 void
00896 TTrackManager::append2D(AList<TTrack> & list) {
00897   _tracksAll.append(list);
00898   _tracks2D.append(selectGoodTracks(list, true));
00899   _tracks2D.sort(SortByPt);
00900   list.removeAll();
00901 }
00902 
00903 void
00904 TTrackManager::refit(void) {
00905 #ifdef TRKRECO_DEBUG_DETAIL
00906   std::cout << name() << " ... refitting" << std::endl;
00907 #endif
00908   unsigned n = _tracks.length();
00909   AList<TTrack> bads;
00910   for (unsigned i = 0; i < n; i++) {
00911     TTrack & t = * _tracks[i];
00912     int err;
00913     err = _fitter.fit(t);
00914     if (err < 0) {
00915       bads.append(t);
00916 #ifdef TRKRECO_DEBUG_DETAIL
00917       std::cout << "    " << t.name();
00918       std::cout << " rejected because of fitting failure" << std::endl;
00919 #endif
00920       continue;
00921     }
00922     t.refine(30. * 10.);
00923     err = _fitter.fit(t);
00924     if (err < 0) {
00925       bads.append(t);
00926 #ifdef TRKRECO_DEBUG_DETAIL
00927       std::cout << "    " << t.name();
00928       std::cout << " rejected because of fitting failure" << std::endl;
00929 #endif
00930       continue;
00931     }
00932     t.refine(30. * 1.);
00933     err = _fitter.fit(t);
00934     if (err < 0) {
00935       bads.append(t);
00936 #ifdef TRKRECO_DEBUG_DETAIL
00937       std::cout << "    " << t.name();
00938       std::cout << " rejected because of fitting failure" << std::endl;
00939 #endif
00940       continue;
00941     }
00942   }
00943   _tracks.remove(bads);
00944 }
00945 
00946 void
00947 TTrackManager::mask(void) const {
00948 #ifdef TRKRECO_DEBUG_DETAIL
00949   std::cout << name() << " ... masking" << std::endl;
00950 #endif
00951 
00952   unsigned n = _tracks.length();
00953   for (unsigned i = 0; i < n; i++) {
00954     TTrack & t = * _tracks[i];
00955 
00956     //...Skip if no core...
00957     //   This should not be happend...
00958     if (! t.cores().length()) continue;
00959 
00960     //...Counts # of hits per layer...
00961     unsigned nHits[43];
00962     NHits(t.cores(), nHits);
00963 
00964     //...Check each layer...
00965     bool needMask = false;
00966     for (unsigned j = 0; j < 43; j++) {
00967       if (nHits[j] > 1) {
00968         AList<TMLink> linksInLayer = SameLayer(t.links(), j);
00969         if (Width(linksInLayer) > 2) {
00970           needMask = true;
00971 
00972 #ifdef TRKRECO_DEBUG_DETAIL
00973           Dump(linksInLayer, "sort", "    -->");
00974 #endif      
00975           break;
00976         }
00977       }
00978     }
00979     if (! needMask) continue;
00980 
00981 #ifdef TRKRECO_DEBUG_DETAIL
00982     std::cout << "    trk" << i << "(id is tmp) needs mask" << std::endl;
00983     std::cout << "        type = " << t.type() << std::endl;
00984 #endif  
00985 
00986     //...Switch by track type...
00987     switch (t.type()) {
00988       case TrackTypeNormal:
00989         maskNormal(t);
00990         maskMultiHits(t);
00991         break;
00992       case TrackTypeCurl:
00993         maskCurl(t);
00994         maskMultiHits(t);
00995         break;
00996       default:
00997         break;
00998     }
00999 
01000     //...Refit...
01001     // refit() ???
01002     _fitter.fit(t);
01003 
01004 #ifdef TRKRECO_DEBUG_DETAIL
01005     std::cout << "    masking result : ";
01006     t.dump("detail sort", "    ");
01007 #endif
01008   }
01009 }
01010 
01011 void
01012 TTrackManager::nameTracks(void) {
01013   unsigned n = _tracks.length();
01014   for (unsigned i = 0; i < n; i++) {
01015     TTrack & t = * _tracks[i];
01016     t._name = "trk" + itostring(i) + "(" + t._name + ")";
01017   }
01018   AList<TTrack> tmp = _tracksAll;
01019   tmp.remove(_tracks);
01020   unsigned n1 = tmp.length();
01021   for (unsigned i = 0; i < n1; i++) {
01022     TTrack & t = * tmp[i];
01023     t._name = "trk" + itostring(i + n)  + "(" + t._name + ")";
01024   }
01025 }
01026 
01027 TMLink &
01028 TTrackManager::divide(const TTrack & t, AList<TMLink> * l) const {
01029   TMLink & start = * OuterMost(t.links());
01030   const HepPoint3D & center = t.helix().center();
01031   const HepVector3D a = start.positionOnTrack() - center;
01032   for (unsigned j = 0; j < t.nLinks(); j++) {
01033     if (t[j] == & start) continue;
01034     TMLink & k = * t[j];
01035     const HepVector3D b = k.positionOnTrack() - center;
01036     if (a.cross(b).z() >= 0.) l[0].append(k);
01037     else                      l[1].append(k);
01038   }
01039 
01040 #ifdef TRKRECO_DEBUG_DETAIL
01041   std::cout << "    outer link = " << start.hit()->wire()->name() << std::endl;
01042   std::cout << "        nLinks of 0 = " << l[0].length() << std::endl;
01043   std::cout << "        nLinks of 1 = " << l[1].length() << std::endl;
01044 #endif
01045 
01046   if (l[0].length() == 0 || l[1].length() == 0)
01047     return divideByIp(t, l);
01048 
01049   return start;
01050 }
01051 
01052 TMLink &
01053 TTrackManager::divideByIp(const TTrack & t, AList<TMLink> * l) const {
01054   l[0].removeAll();
01055   l[1].removeAll();
01056 
01057   const HepPoint3D & center = t.helix().center();
01058   const HepVector3D a = ORIGIN - center;
01059   for (unsigned j = 0; j < t.nLinks(); j++) {
01060     TMLink & k = * t[j];
01061     const HepVector3D b = k.positionOnTrack() - center;
01062     if (a.cross(b).z() >= 0.) l[0].append(k);
01063     else                      l[1].append(k);
01064   }    
01065 
01066   //...This is a dummy...
01067   TMLink & start = * OuterMost(t.links());
01068   return start;
01069 }
01070 
01071 void
01072 TTrackManager::removeHitsAcrossOverIp(AList<TMLink> & l) const {
01073 
01074   //...Calculate average phi...
01075   unsigned n = l.length();
01076   float phiSum = 0.;
01077   for (unsigned i = 0; i < n; i++) {
01078     const TMDCWire & w = * l[i]->hit()->wire();
01079     unsigned j = w.localId();
01080     unsigned nWire = w.layer()->nWires();
01081 
01082     float phi = (float) j / (float) nWire;
01083     phiSum += phi;
01084   }
01085   float average = phiSum / (float) n;
01086 
01087   AList<TMLink> cross;
01088   for (unsigned i = 0; i < n; i++) {
01089     const TMDCWire & w = * l[i]->hit()->wire();
01090     unsigned j = w.localId();
01091     unsigned nWire = w.layer()->nWires();
01092 
01093     float phi = (float) j / (float) nWire;
01094     float dif = fabs(phi - average);
01095     if (dif > 0.5) dif = 1. - dif;
01096 
01097     if (dif > 0.3) cross.append(l[i]);
01098   }
01099   l.remove(cross);
01100 
01101 #ifdef TRKRECO_DEBUG_DETAIL
01102   std::cout << "    Cross over IP reduction : ";
01103   for (unsigned i = 0; i < cross.length(); i++) {
01104     std::cout << cross[i]->wire()->name() << ",";
01105   }
01106   std::cout << std::endl;
01107 #endif
01108 }
01109 
01110 
01111 void
01112 TTrackManager::maskOut(TTrack & t, const AList<TMLink> & links) const {
01113   unsigned n = links.length();
01114   if (! n) return;
01115   for (unsigned i = 0; i < n; i++) {
01116     const TMDCWireHit & hit = * links[i]->hit();
01117     hit.state(hit.state() | WireHitInvalidForFit);
01118   }
01119   t._fitted = false;
01120 
01121 #ifdef TRKRECO_DEBUG_DETAIL
01122   Dump(links, "detail", "    TTrackManager::maskOut ... masking ");
01123 #endif
01124 }
01125 
01126 void
01127 TTrackManager::maskMultiHits(TTrack & t) const {
01128 #ifdef TRKRECO_DEBUG_DETAIL
01129   std::cout << "... masking multi-hits" << std::endl;
01130 #endif
01131 
01132   if (! t.cores().length()) return;
01133   AList<TMLink> cores = t.cores();
01134   unsigned n = cores.length();
01135   bool layerLimited = false;
01136   AList<TMLink> bads;
01137 
01138   cores.sort(SortByWireId);
01139   for (unsigned i = 0; i < n; i++) {
01140     if (layerLimited) {
01141       bads.append(cores[i]);
01142       continue;
01143     }
01144     AList<TMLink> linksInLayer =
01145       SameLayer(cores, cores[i]->wire()->layerId());
01146     if (linksInLayer.length() > 3) {
01147       bads.append(cores[i]);
01148       layerLimited = true;
01149     }
01150   }
01151   maskOut(t, bads);
01152 }
01153 
01154 void
01155 TTrackManager::maskNormal(TTrack & t) const {
01156 
01157   //...Divide into two tracks...
01158   AList<TMLink> l[2];
01159   TMLink & start = divideByIp(t, l);
01160 
01161 #ifdef TRKRECO_DEBUG_DETAIL
01162   std::cout << "    normal : divided by IP" << std::endl;
01163   std::cout << "    0=";
01164   for (unsigned j = 0; j < l[0].length(); j++) {
01165     std::cout << "," << l[0][j]->wire()->name();
01166   }
01167   std::cout << std::endl;
01168   std::cout << "    1=";
01169   for (unsigned j = 0; j < l[1].length(); j++) {
01170     std::cout << "," << l[1][j]->wire()->name();
01171   }
01172   std::cout << std::endl;
01173 #endif
01174 
01175   //...Which should be masked out ?...
01176   unsigned maskSide = 2;
01177 
01178   //...1. Check by # of super layers...
01179   if (NSuperLayers(l[0]) < NSuperLayers(l[1]))      maskSide = 0;
01180   else if (NSuperLayers(l[0]) > NSuperLayers(l[1])) maskSide = 1;
01181 #ifdef TRKRECO_DEBUG_DETAIL
01182   std::cout << "    NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01183   std::cout << NSuperLayers(l[1]) << std::endl;
01184 #endif
01185   if (maskSide != 2) {
01186     maskOut(t, l[maskSide]);
01187     return;
01188   }
01189 
01190   //...2. Check by the inner-most layer...
01191   unsigned i0 = InnerMost(l[0])->wire()->layerId();
01192   unsigned i1 = InnerMost(l[1])->wire()->layerId();
01193   if (i0 < i1) maskSide = 1;
01194   else if (i0 > i1) maskSide = 0;
01195 #ifdef TRKRECO_DEBUG_DETAIL
01196   std::cout << "    i0, i1 = " << i0 << ", " << i1 << std::endl;
01197 #endif
01198   if (maskSide != 2) {
01199     maskOut(t, l[maskSide]);
01200     return;
01201   }
01202 
01203   //...3. Check by # of layers...
01204   if (NLayers(l[0]) < NLayers(l[1])) maskSide = 0;
01205   else if (NLayers(l[0]) > NLayers(l[1])) maskSide = 1;
01206 #ifdef TRKRECO_DEBUG_DETAIL
01207   std::cout << "    NLayers 0, 1 = " << NLayers(l[0]) << ", ";
01208   std::cout << NLayers(l[1]) << std::endl;
01209 #endif
01210   if (maskSide != 2) {
01211     maskOut(t, l[maskSide]);
01212     return;
01213   }
01214 
01215   //...4. Check by pt...
01216   if (maskSide == 2) {
01217     TTrack * tt[2];
01218     for (unsigned j = 0; j < 2; j++) {
01219       tt[j] = new TTrack(t);
01220       tt[j]->remove(l[j]);
01221       _fitter.fit(* tt[j]);
01222     }
01223     if (tt[1]->pt() > tt[0]->pt()) maskSide = 1;
01224     else                           maskSide = 0;
01225 #ifdef TRKRECO_DEBUG_DETAIL
01226     std::cout << "    pt 0 = " << tt[1]->pt() << std::endl;
01227     std::cout << "    pt 1 = " << tt[0]->pt() << std::endl;
01228 #endif      
01229     delete tt[0];
01230     delete tt[1];
01231   }
01232   maskOut(t, l[maskSide]);
01233   return;
01234 }
01235 
01236 void
01237 TTrackManager::maskCurl(TTrack & t) const {
01238 
01239   //...Divide into two tracks...
01240   AList<TMLink> l[2];
01241   TMLink & start = divideByIp(t, l);
01242   if (l[0].length() == 0) return;
01243   if (l[1].length() == 0) return;
01244 
01245 #ifdef TRKRECO_DEBUG_DETAIL
01246   std::cout << "    curl : divided by IP" << std::endl;
01247   std::cout << "    0:";
01248   Dump(l[0], "flag sort");
01249   std::cout << "    1:";
01250   Dump(l[1], "flag sort");
01251   std::cout << std::endl;
01252 #endif
01253 
01254   //...Which should be masked out ?...
01255   unsigned maskSide = 2;
01256 
01257   //...1. Check by # of super layers...
01258   if (NSuperLayers(l[0]) < NSuperLayers(l[1]))      maskSide = 0;
01259   else if (NSuperLayers(l[0]) > NSuperLayers(l[1])) maskSide = 1;
01260 #ifdef TRKRECO_DEBUG_DETAIL
01261   std::cout << "    NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01262   std::cout << NSuperLayers(l[1]) << std::endl;
01263 #endif
01264   if (maskSide != 2) {
01265     maskOut(t, l[maskSide]);
01266     return;
01267   }
01268 
01269   //...Make two tracks...
01270   TTrack * tt[2];
01271   tt[0] = new TTrack(t);
01272   tt[1] = new TTrack(t);
01273   tt[0]->remove(l[1]);
01274   tt[1]->remove(l[0]);
01275   _fitter.fit(* tt[0]);
01276   _fitter.fit(* tt[1]);
01277   Helix h0 = Helix(tt[0]->helix());
01278   Helix h1 = Helix(tt[1]->helix());
01279 
01280   //...Check by z...
01281   h0.pivot(ORIGIN);
01282   h1.pivot(ORIGIN);
01283   if (fabs(h0.dz()) < fabs(h1.dz())) maskSide = 1;
01284   else                               maskSide = 0;
01285 
01286   delete tt[0];
01287   delete tt[1];
01288   maskOut(t, l[maskSide]);
01289   return;
01290 }
01291 
01292 StatusCode 
01293 TTrackManager::determineT0(unsigned level, unsigned nMax) {
01294 #ifdef TRKRECO_DEBUG_DETAIL
01295   if (level == 0) {
01296     std::cout << "TTrackManager::determineT0 !!! called with level = 0";
01297     std::cout << std::endl;
01298   }
01299 #endif
01300 
01301   IMessageSvc *msgSvc;
01302   Gaudi::svcLocator()->service("MessageSvc", msgSvc);
01303   MsgStream log(msgSvc, "TTrackManager");
01304 
01305   IDataProviderSvc* eventSvc = NULL;
01306   Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
01307 
01308   static bool first = true;
01309   static unsigned methode = 0;
01310   if (first) {
01311     first = false;
01312 
01313     if (level == 1) {
01314       _cFitter.fit2D(true);
01315     }
01316     else if (level == 2) {
01317       // default setting
01318     }
01319     else if (level == 3) {
01320       //            _cFitter.sag(true);  //Liuqg
01321     }
01322     else if (level == 4) {
01323       //            _cFitter.sag(true);  //Liuqg
01324       _cFitter.propagation(true);
01325     }
01326     else if (level == 5) {
01327       //            _cFitter.sag(true);  //Liuqg
01328       _cFitter.propagation(true);
01329       _cFitter.tof(true);
01330     }
01331     else if (level == 6) {
01332       methode = 1;
01333       //            _cFitter.sag(true); //Liuqg
01334       _cFitter.propagation(true);
01335       _cFitter.tof(true);
01336     }
01337     else if (level == 7) {
01338       methode = 2;
01339       //            _cFitter.sag(true);  //Liuqg
01340       _cFitter.propagation(true);
01341       _cFitter.tof(true);
01342     }
01343   }
01344 
01345   unsigned n = _tracks.length();
01346   if (! n) return StatusCode::SUCCESS;
01347 
01348   if (nMax == 0) nMax = n;
01349   if (n > nMax) n = nMax;
01350 
01351   //    float t0 = 0.;
01352   _t0 = 999.;
01353 
01354   //read t0 from TDS
01355   float t0_sta = -1;
01356   float tev = 0;
01357   SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
01358   if (aevtimeCol) {
01359     RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
01360     t0_sta =  (*iter_evt)->getStat();
01361     tev = (*iter_evt)->getTest();
01362     //  cout<<"t0_sta: "<<t0_sta<<"  tev: "<<tev<<endl;
01363   }else{
01364     log << MSG::WARNING << "Could not find RecEsTimeCol" << endreq;
01365   }
01366 
01367   if (methode == 0) _t0 = T0(n);
01368 
01369   else if (methode == 1) _t0 = T0Fit(n);
01370 
01371   else if (methode ==2) {   //revise method == 2 to BESIII environment. Liuqg
01372     if (t0_sta != 1) {    //1: tof  11:tof after reco  //no tof
01373       _t0 = T0Fit(n);
01374       //cout<<"t0: "<<_t0<<endl;
01375     }
01376   }
01377 
01378   // std::cout << "reccdc_timing=" << BsCouTab(RECMDC_TIMING) << std::endl;
01379   /*    else if (methode == 2 && BsCouTab(RECMDC_TIMING) != 0) {
01380         struct reccdc_timing * r0 = (struct reccdc_timing *)
01381         BsGetEnt(RECMDC_TIMING, BsCouTab(RECMDC_TIMING), BBS_No_Index);
01382         if (r0->m_quality == 102) {
01383         if (BsCouTab(BELLE_EVENT)) {
01384         struct belle_event * b0 = (struct belle_event *)
01385         BsGetEnt(BELLE_EVENT, 1, BBS_No_Index);
01386         if(1==b0->m_ExpMC)                    t0 = T0Fit(n);
01387         if(2==b0->m_ExpMC && r0->m_time !=0.) t0 = T0Fit(n);
01388         }
01389         }
01390         else if (r0->m_quality == 100) t0 = T0Fit(n);
01391   //      std::cout << "quality=" << r0->m_quality << std::endl;
01392   }
01393   */
01394 
01395   //...For debug...
01396   if (_debugLevel) {
01397     std::cout << "TTrackManager::determineT0 ... methode=" << methode;
01398     std::cout << ", T0 offset=" << - _t0;
01399     std::cout << ", # of tracks used=" << n << std::endl;
01400   }
01401 
01402   //...store them... Liuqg
01403   float t0_rec = 999.;
01404   int t0_recSta = 8;
01405   if(fabs(_t0 + tev) < 4) t0_rec = 0;
01406   if(fabs(_t0 + tev - 8) < 4) t0_rec = 8;
01407   if(fabs(_t0 + tev - 16) < 4) t0_rec = 16;
01408   log << MSG::INFO << "beginning to make RecEsTimeCol" <<endreq;
01409   IDataManagerSvc *dataManSvc = dynamic_cast<IDataManagerSvc*> (eventSvc);
01410   DataObject *aEvTime;
01411   eventSvc->findObject("/Event/Recon/RecEsTimeCol",aEvTime);
01412   if(aEvTime!=NULL && t0_rec<25){
01413     dataManSvc->clearSubTree("/Event/Recon/RecEsTimeCol");
01414     eventSvc->unregisterObject("/Event/Recon/RecEsTimeCol");
01415   }
01416   if(t0_rec<25){
01417 
01418     RecEsTimeCol *aEvTimeCol = new RecEsTimeCol;
01419     RecEsTime *aevtime = new RecEsTime;
01420     aevtime -> setTest(t0_rec);
01421     aevtime -> setStat(t0_recSta);
01422     aEvTimeCol->push_back(aevtime);
01423 
01424     // register event time  to TDS
01425     //check whether the t0  has been already registered
01426     StatusCode evtime = eventSvc->registerObject("/Event/Recon/RecEsTimeCol", aEvTimeCol);
01427 
01428     if(evtime!=StatusCode::SUCCESS) {
01429       log << MSG::FATAL << "Could not register Event Start Time" << endreq;
01430       return( StatusCode::FAILURE);
01431     }
01432   }
01433   return( StatusCode::SUCCESS );
01434 }
01435 
01436 float
01437 TTrackManager::T0(unsigned n) {
01438 
01439 #define X0 -10.
01440 #define X1 0.
01441 #define X2 10.
01442 #define STEP 10.
01443 
01444   //...Determine T0 for each track...
01445   float t0Sum = 0.;
01446   for (unsigned i = 0; i < n; i++) {
01447     TTrack & t = * _tracks[i];
01448     float y[3];
01449     for (unsigned j = 0; j < 3; j++) {
01450       float offset = X0 + j * STEP;
01451       _cFitter.fit(t, offset);
01452       y[j] = t.chi2();
01453     }
01454     t0Sum += minimum(y[0], y[1], y[2]);
01455   }
01456   float t0 = t0Sum / (float) n;
01457   if (isnan(t0)) t0 = 0.;
01458 
01459   //...Fit with T0 correction...
01460   n = _tracks.length();
01461   for (unsigned i = 0; i < n; i++) {
01462     TTrack & t = * _tracks[i];
01463     _cFitter.fit(t, t0);
01464   }
01465 
01466   //...Store it...
01467   //    reccdc_timing * t = (reccdc_timing *) BsNewEnt(RECMDC_TIMING);
01468   /*    MdcRec_timing* t = new MdcRec_timing;
01469         MdcRecTimingCol::getMdcRecTimingCol()->push_back(*t);
01470         t->time = - t0;
01471         t->quality = 11;
01472         */
01473   return - t0;
01474 }
01475 
01476 float
01477 TTrackManager::T0Fit(unsigned n) {
01478 
01479   float tev_err;
01480   float tev_sum0= 0.;
01481   float tev_sum = 0.;
01482   float tev_sum2= 0.;
01483   float w_sum   = 0.;
01484 
01485   //sort in order of pt
01486   //    std::cout << "length=" << _tracks.length() << std::endl;
01487   const int cn=_tracks.length();
01488   float* sort = new float[cn];
01489   float ptmax_pre=1.e10;
01490   for (unsigned i = 0; i < cn; i++) {
01491     float ptmax=-999.;
01492     int   jmax;
01493     for (unsigned j = 0; j < cn; j++) {
01494       TTrack & tj = * _tracks[j];
01495       float pt = fabs(1./tj.helix().a()[2]);
01496       if( pt < ptmax_pre && pt > ptmax) {
01497         ptmax = pt;
01498         jmax  = j;
01499       }
01500       sort[i]=jmax;
01501     }
01502     ptmax_pre=ptmax;
01503   }
01504 
01505   for (unsigned i = 0; i < n; i++) {
01506     //srtbypt   TTrack & t = * _tracks[i];
01507     TTrack & t = * _tracks[sort[i]];    
01508     //float pt = fabs(1./t.helix().a()[2]);
01509     //std::cout << "pt=" << pt << endl;
01510     float tev = 0.;
01511     _cFitter.fit(t, tev, tev_err);
01512     //  std::cout << "tev,tev_err=" <<tev<< " "<<tev_err<<endl; 
01513     float w = 1. / tev_err / tev_err;
01514     tev_sum += w * tev;
01515     w_sum += w;
01516     // tev_sum0 += tev;
01517     // tev_sum2 += tev * tev;
01518   }
01519 
01520   delete [] sort;
01521 
01522   float tev_mean = tev_sum / w_sum;
01523   // float tev_err_a = 1. / sqrt(w_sum);
01524   // float tev_err_b = (tev_sum2 - tev_sum0 * tev_sum0 / (n + 1)) / n;
01525   // tev_err_b = sqrt(tev_err_b);
01526   // std::cout << "comp,t0,mean tev,err ="<<t0<<" "<<tev_mean<<" "<<tev_err_a<<" "<<tev_err_b<<std::endl;
01527   if (isnan(tev_mean)) tev_mean = 0.;
01528 
01529   //...Store it...
01530   //    reccdc_timing * tt = (reccdc_timing *) BsNewEnt(RECMDC_TIMING);
01531   //    MdcRec_timing* tt = new MdcRec_timing;
01532   //    MdcRecTimingCol::getMdcRecTimingCol()->push_back(*tt);
01533   //    tt->time = tev_mean;
01534   //    tt->quality = 151;
01535 
01536   return - tev_mean;
01537 }
01538 
01539 float
01540 TTrackManager::minimum(float y0, float y1, float y2) const {
01541   float xMin = X1 + 0.5 * STEP * (y0 - y2) / (y0 + y2 - 2. * y1);
01542   return xMin;
01543 }
01544 
01545 // added by matsu ( 1999/05/24 )
01546 void
01547 TTrackManager::merge(void) {
01548 
01549   //...Merging...
01550   unsigned n = _tracks.length();
01551   //cout<<"tracks: "<<n<<endl;
01552   AList<TTrack> bads;
01553   unsigned * flagTrk = (unsigned *) malloc(n * sizeof(unsigned));
01554   for (unsigned i = 0; i < n; i++) flagTrk[i] = 0;
01555 
01556   //...Search a track to be merged...
01557   for (unsigned i0 = 0; i0 < n; i0++) {
01558 
01559     if (flagTrk[i0] != 0) continue;
01560     TTrack & t0 = * _tracks[i0];
01561     if (! (t0.pt() < 0.13)) continue;
01562 
01563     unsigned Noverlap(0), Nall(0);
01564     float OverlapRatioMax(-999.);
01565     unsigned MaxID(0);
01566 
01567     for (unsigned i1= 0 ; i1 < n; i1++) {
01568 
01569       if (i0 == i1 || flagTrk[i1] != 0) continue;
01570       TTrack & t1 = * _tracks[i1];
01571       if (! (t1.pt() < 0.13)) continue;
01572       Nall = t1.links().length();
01573       if (! Nall) continue;
01574 
01575       Noverlap = 0;
01576       for (unsigned j = 0; j < Nall; j++) {
01577         TMLink & l =  * t1.links()[j];
01578         const TMDCWireHit & whit =  * l.hit();
01579         double load(3.);//jialk original is 2
01580         if (whit.state() & WireHitStereo) load = 4.;//jialk original is 3
01581 
01582         double x = t0.helix().center().x() - l.positionOnTrack().x();
01583         double y = t0.helix().center().y() - l.positionOnTrack().y();
01584         double r = sqrt(x * x + y * y);
01585         double R = fabs(t0.helix().radius());
01586 
01587         if ((R - load) < r && r < (R + load)) Noverlap++;
01588       }
01589 
01590       if (! Noverlap) continue;
01591       float tmpRatio = float(Noverlap) / float(Nall);
01592 
01593       if (tmpRatio > OverlapRatioMax) {
01594         OverlapRatioMax = tmpRatio;
01595         MaxID = i1;
01596       }
01597     } 
01598     //jialk caution original is 0.8
01599     if (OverlapRatioMax < 0.7) continue;
01600 
01601     //...Mask should be done...
01602     unsigned MaskID[2] = {MaxID , i0};
01603     AList<TMLink> l[2];
01604 
01605     for( unsigned j0=0;j0<2;j0++){
01606       for( unsigned j1=0;j1< _tracks[MaskID[j0]]->nLinks();j1++){
01607         TMLink &k = * _tracks[MaskID[j0]]->links()[j1]; 
01608         l[j0].append( k );
01609       }
01610     }
01611     // _tracks[i0]->links().append( _tracks[MaxID]->links() );
01612     // _tracks[MaxID]->links().append( _tracks[i0]->links ());
01613     _tracks[i0]->append(_tracks[MaxID]->links());
01614     _tracks[MaxID]->append(_tracks[i0]->links());
01615 
01616 #ifdef TRKRECO_DEBUG_DETAIL
01617     std::cout << "    mask & merge " << std::endl;
01618     std::cout << "    0:";
01619     Dump(l[0], "flag sort");
01620     std::cout << "    1:";
01621     Dump(l[1], "flag sort");
01622     std::cout << std::endl;
01623 #endif
01624 
01625     //...Which should be masked out ?...
01626     unsigned maskSide = 2;
01627 
01628 #if 0
01629     //...0. Check by # of super layers... ( not applied now )
01630     unsigned super0 = NSuperLayers(l[0]);
01631     unsigned super1 = NSuperLayers(l[1]);
01632 
01633     if( super0 < super1 ) maskSide = 0;
01634     else if ( super0 > super1 ) maskSide = 1;
01635 
01636 #ifdef TRKRECO_DEBUG_DETAIL
01637     std::cout << "    NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01638     std::cout << NSuperLayers(l[1]) << std::endl;
01639 #endif
01640 
01641     if (maskSide == 2) {
01642 #endif
01643 
01644       //...1. Check by the inner-most layer...
01645       unsigned inner0 = InnerMost(l[0])->wire()->layerId();
01646       unsigned inner1 = InnerMost(l[1])->wire()->layerId();
01647       if (inner0 < inner1 ) maskSide = 1;
01648       else if (inner0 > inner1) maskSide = 0;
01649 
01650       if( maskSide == 2 ){
01651 
01652         //...2. Check by dz 
01653 
01654         //...Make two tracks...
01655         TTrack * tt[2];
01656         tt[0] = new TTrack( *(_tracks[MaskID[0]]));
01657         tt[1] = new TTrack( *(_tracks[MaskID[1]]));
01658         _fitter.fit(* tt[0]);
01659         _fitter.fit(* tt[1]);
01660         Helix h0 = Helix(tt[0]->helix());
01661         Helix h1 = Helix(tt[1]->helix());
01662 
01663         //...Check dz...
01664         h0.pivot(ORIGIN);
01665         h1.pivot(ORIGIN);
01666         if (fabs(h0.dz()) < fabs(h1.dz())) maskSide = 1;
01667         else                               maskSide = 0;
01668 
01669         delete tt[0];
01670         delete tt[1];
01671       }
01672 #if 0
01673     }
01674 #endif
01675     bads.append(_tracks[MaskID[maskSide]]);
01676     flagTrk[MaskID[maskSide]] = 1;
01677   }
01678 
01679   //cout<<"bads: "<<bads.length()<<endl;
01680   _tracks.remove(bads);
01681 
01682   //*****  Masking *****
01683   n = _tracks.length();
01684 
01685   for( unsigned i=0;i<n;i++){
01686     TTrack & t = * _tracks[i];
01687     for( unsigned j=0;j<t.links().length();j++){
01688       TMLink & l =  * t.links()[j];
01689       const TMDCWireHit & whit =  * l.hit();
01690 
01691       if( !(whit.state() & WireHitFittingValid) ) continue;
01692 
01693       // within half circle or not?
01694       double q = t.helix().center().x() * l.positionOnTrack().y() - 
01695         t.helix().center().y() * l.positionOnTrack().x();
01696       double qq =  q *t.charge();
01697 
01698       if( qq > 0 ) whit.state(whit.state() & ~WireHitInvalidForFit);
01699       else         whit.state(whit.state() | WireHitInvalidForFit);
01700     }
01701   }
01702 
01703   free(flagTrk);
01704 }
01705 // end of addition
01706 
01707 int
01708 TTrackManager::copyTrack(TTrack & t,
01709     MdcRec_trk ** pr,
01710     MdcRec_trk_add ** pra) const {
01711 
01712   static const unsigned GoodHitMask = (WireHitTimeValid |
01713       WireHitChargeValid |
01714       WireHitFindingValid |
01715       WireHitFittingValid);
01716   int err = 0;
01717 
01718   //...Hit loop...
01719 #ifdef TRKRECO_DEBUG_DETAIL
01720   std::cout << "    checking hits ... " << t.name() 
01721     << " quality = " << t.quality();
01722   std::cout << " : " << t.cores().length() << ", " << t.ndf() << " : ";
01723   unsigned nnn = 0;
01724 #endif
01725   unsigned j = 0;
01726   unsigned nClst = 0;
01727   unsigned nStereos = 0;
01728   unsigned nOccupied = 0;
01729   AList<TMLink> hits;
01730   AList<TMLink> badHits;
01731   unsigned n = t.links().length();
01732   for (unsigned i = 0; i < n; i++) {
01733     TMLink * l = t.links()[i];
01734     MdcRec_wirhit * h = l->hit()->reccdc();
01735 
01736 #ifdef TRKRECO_DEBUG_DETAIL
01737     if (h->trk) std::cout << l->wire()->name() << "(n/a),";
01738 #endif
01739 
01740     if (h->trk) {
01741       ++nOccupied;
01742       if (! (h->stat & WireHitInvalidForFit))
01743         continue;
01744     }
01745     if ((l->hit()->state() & GoodHitMask) == GoodHitMask) {
01746       if (l->hit()->state() & WireHitInvalidForFit) {
01747         if (! (h->stat & WireHitInvalidForFit))
01748           badHits.append(l);
01749       }
01750       else {
01751         hits.append(l);
01752         if (l->wire()->stereo()) ++nStereos;
01753       }
01754     }
01755   }
01756   t.finalHits(hits);
01757 #ifdef TRKRECO_DEBUG_DETAIL
01758   std::cout << std::endl;
01759 #endif
01760 
01761   //...Check # of hits...
01762   if (t.quality() & TrackQuality2D) {
01763     if (hits.length() < 3) err = 3;
01764     if (nOccupied > 2) err = 4;
01765   }
01766   else {
01767     if (hits.length() < 5) err = 1;
01768     if (nStereos < 2) err = 2;
01769   }
01770   if (err) return err;
01771 
01772   //...Create new tables...
01773   //    * pr = (reccdc_trk *) BsNewEnt(RECMDC_TRK);
01774   //    * pra = (reccdc_trk_add *) BsNewEnt(RECMDC_TRK_ADD);
01775   //    reccdc_trk * r = * pr;
01776   //    reccdc_trk_add * ra = * pra;
01777   * pr = new MdcRec_trk;
01778   * pra = new MdcRec_trk_add;
01779   MdcRec_trk* r = * pr;
01780   MdcRec_trk_add* ra = * pra;
01781 
01782   //...Copy hit information...
01783   // const AList<TMLink> & cores = t.cores();
01784   // const AList<TMLink> & links = t.links();
01785   // unsigned allHits = cores.length();
01786   // unsigned stereoHits = NStereoHits(cores);
01787   // r.m_chiSq = t.chi2();
01788   // r.m_confl = t.confidenceLevel();
01789   // r.m_ndf = t.ndf();
01790   // r.m_nhits = allHits;
01791   // r.m_nster = stereoHits;
01792   float chisq = 0.;
01793   unsigned nHits = hits.length();
01794   for (unsigned i = 0; i < nHits; i++) {
01795     TMLink * l = hits[i];
01796     MdcRec_wirhit * h = hits[i]->hit()->reccdc();
01797     h->trk = r;
01798     h->pChiSq = l->pull();
01799     h->lr = l->leftRight();
01800     //zsl       if (l->usecathode() == 4) ++nClst;
01801     chisq += h->pChiSq;
01802   }
01803   r->chiSq = chisq;
01804   r->nhits = nHits;
01805   r->nster = nStereos;
01806   r->ndf = nHits - 5;
01807   if (t.quality() & TrackQuality2D)
01808     r->ndf = nHits - 3;
01809 
01810   //...Bad hits...
01811   n = badHits.length();
01812   for (unsigned i = 0; i < n; i++) {
01813     MdcRec_wirhit * h = badHits[i]->hit()->reccdc();
01814     h->trk = r;
01815     h->stat |= WireHitInvalidForFit;
01816   }
01817 
01818   //...Cathode...
01819   r->nclus = nClst;
01820 
01821   //...Helix parameter...
01822   const Vector & a = t.helix().a();
01823   const SymMatrix & ea = t.helix().Ea();
01824   const HepPoint3D & x = t.helix().pivot();
01825   r->helix[0] = a[0];
01826   r->helix[1] = a[1];
01827   r->helix[2] = a[2];
01828   r->helix[3] = a[3];
01829   r->helix[4] = a[4];
01830 
01831   r->pivot[0] = x.x();
01832   r->pivot[1] = x.y();
01833   r->pivot[2] = x.z();
01834 
01835   r->error[0] = ea[0][0];
01836   r->error[1] = ea[1][0];
01837   r->error[2] = ea[1][1];
01838   r->error[3] = ea[2][0];
01839   r->error[4] = ea[2][1];
01840   r->error[5] = ea[2][2];
01841   r->error[6] = ea[3][0];
01842   r->error[7] = ea[3][1];
01843   r->error[8] = ea[3][2];
01844   r->error[9] = ea[3][3];
01845   r->error[10] = ea[4][0];
01846   r->error[11] = ea[4][1];
01847   r->error[12] = ea[4][2];
01848   r->error[13] = ea[4][3];
01849   r->error[14] = ea[4][4];
01850 
01851   //...Get outer most hit(=termination point)...
01852   TMLink * last = OuterMost(hits);
01853 
01854   //...Calculate phi of the termination point...
01855   t.approach(* last);
01856   r->fiTerm = last->dPhi();
01857 
01858   return err;
01859 }
01860 
01861 void
01862 TTrackManager::sortTracksByQuality(void) {
01863   unsigned n = _tracks.length();
01864   if (n < 2) return;
01865 
01866   for (unsigned i = 0; i < n - 1; i++) {
01867     TTrack & t0 = * _tracks[i];
01868     float bestRChisq = HUGE_VAL;
01869     if (t0.ndf() > 0) bestRChisq = t0.chi2() / t0.ndf();
01870     for (unsigned j = i + 1; j < n; j++) {
01871       TTrack & t1 = * _tracks[j];
01872       float rChisq = HUGE_VAL;
01873       if (t1.ndf() > 0) rChisq = t1.chi2() / t1.ndf();
01874       if (rChisq < bestRChisq) {
01875         bestRChisq = rChisq;
01876         _tracks.swap(i, j);
01877       }
01878     }
01879   }
01880 }
01881 
01882 void
01883 TTrackManager::sortTracksByPt(void) {
01884 #ifdef TRKRECO_DEBUG_DETAIL
01885   std::cout << "trkmgr::sortTracksByPt : # of tracks="
01886     << _tracks.length() << std::endl;
01887 #endif
01888 
01889   unsigned n = _tracks.length();
01890   if (n < 2) return;
01891 
01892   for (unsigned i = 0; i < n - 1; i++) {
01893     TTrack & t0 = * _tracks[i];
01894     float bestPt = t0.pt();
01895     for (unsigned j = i + 1; j < n; j++) {
01896       TTrack & t1 = * _tracks[j];
01897       float pt = t1.pt();
01898 #ifdef TRKRECO_DEBUG_DETAIL
01899       std::cout << "i,j=" << i << "," << j
01900         << " : pt i,j=" << bestPt << "," << pt << std::endl;
01901 #endif
01902       if (pt > bestPt) {
01903         bestPt = pt;
01904         _tracks.swap(i, j);
01905       }
01906     }
01907   }
01908 }
01909 
01910 void
01911 TTrackManager::treatCurler(MdcTrk & trk1,
01912     MdcRec_trk_add & cdc1,
01913     unsigned flag) const {
01914   //...Originally coded by j.tanaka...
01915 
01916   //...Check inputs...
01917   if (trk1.zero[2] == 0) return;
01918   if (cdc1.daughter == 0) return;
01919 
01920   //...The other side...
01921   //    reccdc_trk_add & cdc2 = * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
01922   //                                                      cdc1.m_daughter,
01923   //                                                      BBS_No_Index);
01924   //    MdcRec_trk_add & cdc2 = (*MdcRecTrkAddCol::getMdcRecTrkAddCol())[cdc1.daughter.id];
01925   MdcRec_trk_add & cdc2 = * cdc1.daughter->add;
01926 
01927   if (cdc2.daughter == 0) return;
01928   if (cdc2.rectrk == 0) return;
01929   //    rectrk & trk2 = * (rectrk *) BsGetEnt(RECTRK, cdc2.m_rectrk, BBS_No_Index);
01930   MdcTrk & trk2 = * cdc2.rectrk;
01931 
01932   if (trk2.zero[2] == 0) return;
01933 
01934   //...Obtain RECTRK_LOCALZ...
01935   //    rectrk_localz & z1 = * (rectrk_localz *) BsGetEnt(RECTRK_LOCALZ,
01936   //                                                  trk1.m_zero[2],
01937   //                                                  BBS_No_Index);
01938   //    rectrk_localz & z2 = * (rectrk_localz *) BsGetEnt(RECTRK_LOCALZ,
01939   //                                                  trk2.m_zero[2],
01940   //                                                  BBS_No_Index);
01941   MdcTrk_localz & z1 = * trk1.zero[2];
01942   MdcTrk_localz & z2 = * trk2.zero[2];
01943 
01944   //...Pointer to mother and daughter...
01945   MdcRec_trk_add * mother = & cdc1;
01946   MdcRec_trk_add * daughter = & cdc2;
01947 
01948   //      //...By dr...
01949   //      if (flag == 1) {
01950   //    float dr1 = fabs(z1.m_helix[0]);
01951   //    float dr2 = fabs(z2.m_helix[0]);
01952   //    if (dr1 > dr2) {
01953   //        mother = & cdc2;
01954   //        daughter = & cdc1;
01955   //    }
01956   //      }
01957 
01958   //      //...By dz...
01959   //      else {
01960   //    float dz1 = fabs(z1.m_helix[3]);
01961   //    float dz2 = fabs(z2.m_helix[3]);
01962   //    if (dz1 > dz2) {
01963   //        mother = & cdc2;
01964   //        daughter = & cdc1;
01965   //    }
01966   //      }
01967 
01968   //...By dz + dr...
01969   if(flag == 3){
01970     float dz1 = fabs(z1.helix[3]);
01971     float dz2 = fabs(z2.helix[3]);
01972     if (fabs(dz1 - dz2) < 2.) flag = 1;
01973     else                      flag = 2;
01974   }
01975 
01976   //...By dr...
01977   if(flag == 1){
01978     float dr1 = fabs(z1.helix[0]);
01979     float dr2 = fabs(z2.helix[0]);
01980     if (dr1 > dr2) {
01981       mother = & cdc2;
01982       daughter = & cdc1;
01983     }
01984   }
01985 
01986   //...By dz...
01987   else if(flag == 2){
01988     float dz1 = fabs(z1.helix[3]);
01989     float dz2 = fabs(z2.helix[3]);
01990     if (dz1 > dz2) {
01991       mother = & cdc2;
01992       daughter = & cdc1;
01993     }
01994   }
01995 
01996   //...Update information...
01997   mother->quality &= (~ TrackQualityOutsideCurler);
01998   mother->likelihood[0] = 1.;
01999   mother->decision |= TrackTrackManager;
02000   //zsl
02001   mother->daughter = daughter->body;
02002   mother->mother = 0;
02003   //zsl end;
02004   daughter->quality |= TrackQualityOutsideCurler;
02005   daughter->likelihood[0] = 0.;
02006   daughter->mother = mother->body;
02007   daughter->daughter = 0;
02008   daughter->decision |= TrackTrackManager;
02009 }
02010 
02011 void
02012 TTrackManager::sortBanksByPt(void) const {
02013 #ifdef TRKRECO_DEBUG_DETAIL
02014   std::cout << "trkmgr::sortBanksByPt : # of tracks="
02015     //   << BsCouTab(RECMDC_TRK_ADD) << std::endl;
02016     << MdcRecTrkAddCol::getMdcRecTrkAddCol()->size() << std::endl;
02017 #endif
02018 
02019   //    unsigned n = BsCouTab(RECMDC_TRK_ADD);
02020   unsigned n = MdcRecTrkAddCol::getMdcRecTrkAddCol()->size();
02021   if (n < 2) return;
02022 
02023   //...Sort RECMDC...
02024   unsigned * id = (unsigned *) malloc(n * sizeof(unsigned));
02025   for (unsigned i = 0; i < n; i++) id[i] = i;
02026   for (unsigned i = 0; i < n - 1; i++) {
02027     //  reccdc_trk & cdc0 =
02028     //      * (reccdc_trk *) BsGetEnt(RECMDC_TRK,
02029     //                                i + 1,
02030     //                                BBS_No_Index);
02031     //  reccdc_trk_add & add0 =
02032     //      * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02033     //                                    i + 1,
02034     //                                    BBS_No_Index);
02035     //  reccdc_mctrk & mc0 =
02036     //      * (reccdc_mctrk *) BsGetEnt(RECMDC_MCTRK,
02037     //                                  i + 1,
02038     //                                  BBS_No_Index);
02039     MdcRec_trk & cdc0 = (* MdcRecTrkCol::getMdcRecTrkCol())[i]; 
02040     MdcRec_trk_add & add0 = (* MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];  
02041     MdcRec_mctrk & mc0 = (* MdcRecMctrkCol::getMdcRecMctrkCol())[i];  
02042 
02043     float bestPt = 1. / fabs(cdc0.helix[2]);
02044     unsigned bestQuality = add0.quality;
02045     for (unsigned j = i + 1; j < n; j++) {
02046       //            reccdc_trk & cdc1 =
02047       //                * (reccdc_trk *) BsGetEnt(RECMDC_TRK,
02048       //                                          j + 1,
02049       //                                          BBS_No_Index);
02050       //            reccdc_trk_add & add1 =
02051       //                * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02052       //                                              j + 1,
02053       //                                              BBS_No_Index);
02054       //            reccdc_mctrk & mc1 =
02055       //                * (reccdc_mctrk *) BsGetEnt(RECMDC_MCTRK,
02056       //                                            j + 1,
02057       //                                            BBS_No_Index);
02058       MdcRec_trk & cdc1 = (* MdcRecTrkCol::getMdcRecTrkCol())[j];
02059       MdcRec_trk_add & add1 = (* MdcRecTrkAddCol::getMdcRecTrkAddCol())[j];
02060       MdcRec_mctrk & mc1 = (* MdcRecMctrkCol::getMdcRecMctrkCol())[j];
02061 
02062       float pt = 1. / fabs(cdc1.helix[2]);
02063 #ifdef TRKRECO_DEBUG_DETAIL
02064       std::cout << "i,j=" << i << "," << j
02065         << " : quality i,j=" << bestQuality << ","
02066         << add1.quality << std::endl;
02067 #endif
02068       unsigned quality = add1.quality;
02069       if (quality > bestQuality) continue;
02070       else if (quality < bestQuality) {
02071         bestQuality = quality;
02072         bestPt = pt;
02073         swapReccdc(cdc0, add0, mc0, cdc1, add1, mc1);
02074         unsigned tmp = id[i];
02075         id[i] = id[j];
02076         id[j] = tmp;
02077 #ifdef TRKRECO_DEBUG_DETAIL
02078         std::cout << "swapped" << std::endl;
02079 #endif
02080         continue;
02081       }
02082 #ifdef TRKRECO_DEBUG_DETAIL
02083       std::cout << "i,j=" << i << "," << j
02084         << " : pt i,j=" << bestPt << "," << pt << std::endl;
02085 #endif
02086       if (pt > bestPt) {
02087 #ifdef TRKRECO_DEBUG_DETAIL
02088         std::cout << "swapping ... " << & cdc0 << "," << & add0 << ","
02089           << & mc0 << " <-> " << & cdc1 << "," << & add1 << ","
02090           << & mc1 << std::endl;
02091 #endif
02092         bestQuality = quality;
02093         bestPt = pt;
02094         swapReccdc(cdc0, add0, mc0, cdc1, add1, mc1);
02095         unsigned tmp = id[i];
02096         id[i] = id[j];
02097         id[j] = tmp;
02098 #ifdef TRKRECO_DEBUG_DETAIL
02099         std::cout << "swapped" << std::endl;
02100 #endif
02101       }
02102     }
02103   }
02104 #ifdef TRKRECO_DEBUG_DETAIL
02105   std::cout << "trkmgr::sortBanksByPt : first phase finished" << std::endl;
02106 #endif
02107 
02108   tagReccdc(id, n);
02109   free(id);
02110 
02111 #ifdef TRKRECO_DEBUG_DETAIL
02112   std::cout << "trkmgr::sortBanksByPt : second phase finished" << std::endl;
02113 #endif
02114 
02115 #if 0
02116   //...Sort RECTRK...
02117   n = BsCouTab(RECTRK);
02118   id = (unsigned *) malloc(n * sizeof(unsigned));
02119   for (unsigned i = 0; i < n; i++) id[i] = i;
02120   if (n > 1) {
02121     unsigned i = 0;
02122     while (i < n - 1) {
02123       rectrk & t = * (rectrk *) BsGetEnt(RECTRK, i + 1, BBS_No_Index);
02124       if (t.m_prekal == (i + 1)) {
02125         ++i;
02126         continue;
02127       }
02128 
02129       rectrk & s = * (rectrk *) BsGetEnt(RECTRK,
02130           t.m_prekal,
02131           BBS_No_Index);
02132 
02133       swapRectrk(t, s);
02134       unsigned tmp = id[i];
02135       id[i] = id[s.m_ID - 1];
02136       id[s.m_ID - 1] = tmp;
02137 
02138       //std::cout << "swap " << i + 1 << " and " << s.m_ID << std::endl;
02139 
02140       reccdc_trk_add & a =
02141         * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02142             t.m_prekal,
02143             BBS_No_Index);
02144       reccdc_trk_add & b =
02145         * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02146             s.m_prekal,
02147             BBS_No_Index);
02148       a.m_rectrk = t.m_ID;
02149       b.m_rectrk = s.m_ID;
02150     }
02151   }
02152 #else
02153   /*
02154   // jtanaka 000925 -->
02155   n = BsCouTab(RECTRK);
02156   id = (unsigned *) malloc(n * sizeof(unsigned));
02157   for (unsigned i = 0; i < n; i++) id[i] = i;
02158   int foundId = 0;
02159   while(foundId != n){
02160   rectrk & t = * (rectrk *) BsGetEnt(RECTRK, foundId + 1, BBS_No_Index);
02161   int minPrekal = t.m_prekal;
02162   int exchangeId = foundId;
02163   for(int i=foundId+1;i<n;++i){
02164   rectrk & s = * (rectrk *) BsGetEnt(RECTRK, i + 1, BBS_No_Index);
02165   if(s.m_prekal < minPrekal){
02166   minPrekal = s.m_prekal;
02167   exchangeId = i;
02168   }
02169   }
02170   if(exchangeId != foundId){
02171   rectrk & s = * (rectrk *) BsGetEnt(RECTRK,
02172   exchangeId + 1,
02173   BBS_No_Index);
02174 
02175   swapRectrk(t, s);
02176   unsigned tmp = id[t.m_ID - 1];
02177   id[t.m_ID - 1] = id[s.m_ID - 1];
02178   id[s.m_ID - 1] = tmp;
02179 
02180   reccdc_trk_add & a =
02181    * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02182    t.m_prekal,
02183    BBS_No_Index);
02184    reccdc_trk_add & b =
02185    * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02186    s.m_prekal,
02187    BBS_No_Index);
02188    a.m_rectrk = t.m_ID;
02189    b.m_rectrk = s.m_ID;
02190    }
02191    ++foundId;
02192    }
02193   // <-- jtanaka 000925
02194   */
02195 #endif
02196 
02197   tagRectrk(id, n);
02198   free(id);
02199 }
02200 
02201 void
02202 TTrackManager::swapReccdc(MdcRec_trk & cdc0,
02203     MdcRec_trk_add & add0,
02204     MdcRec_mctrk & mc0,
02205     MdcRec_trk & cdc1,
02206     MdcRec_trk_add & add1,
02207     MdcRec_mctrk & mc1) const {
02208 #define RECMDC_ACTUAL_SIZE 124
02209 #define RECMDCADD_ACTUAL_SIZE 40
02210 #define RECMDCMC_ACTUAL_SIZE 28
02211 
02212   static bool first = true;
02213   static void * swapRegion;
02214   if (first) {
02215     first = false;
02216     swapRegion = malloc(RECMDC_ACTUAL_SIZE);
02217   }
02218 
02219   void * s0 = & cdc0.helix[0];
02220   void * s1 = & cdc1.helix[0];
02221   memcpy(swapRegion, s0, RECMDC_ACTUAL_SIZE);
02222   memcpy(s0, s1, RECMDC_ACTUAL_SIZE);
02223   memcpy(s1, swapRegion, RECMDC_ACTUAL_SIZE);
02224 
02225   s0 = & add0.quality;
02226   s1 = & add1.quality;
02227   memcpy(swapRegion, s0, RECMDCADD_ACTUAL_SIZE);
02228   memcpy(s0, s1, RECMDCADD_ACTUAL_SIZE);
02229   memcpy(s1, swapRegion, RECMDCADD_ACTUAL_SIZE);
02230 
02231   if ((& mc0) && (& mc1)) {
02232     s0 = & mc0.hep;
02233     s1 = & mc1.hep;
02234     memcpy(swapRegion, s0, RECMDCMC_ACTUAL_SIZE);
02235     memcpy(s0, s1, RECMDCMC_ACTUAL_SIZE);
02236     memcpy(s1, swapRegion, RECMDCMC_ACTUAL_SIZE);
02237   }
02238 }
02239 
02240 void
02241 TTrackManager::swapRectrk(MdcTrk & rec0,
02242     MdcTrk & rec1) const {
02243 #define RECTRK_ACTUAL_SIZE 84
02244 
02245   static bool first = true;
02246   static void * swapRegion;
02247   if (first) {
02248     first = false;
02249     swapRegion = malloc(RECTRK_ACTUAL_SIZE);
02250   }
02251 
02252   void * s0 = & rec0.glob[0];
02253   void * s1 = & rec1.glob[0];
02254   memcpy(swapRegion, s0, RECTRK_ACTUAL_SIZE);
02255   memcpy(s0, s1, RECTRK_ACTUAL_SIZE);
02256   memcpy(s1, swapRegion, RECTRK_ACTUAL_SIZE);
02257 }
02258 
02259 void
02260 TTrackManager::tagReccdc(unsigned * id0, unsigned nTrk) const {
02261   /*
02262      unsigned * id = (unsigned *) malloc(nTrk * sizeof(unsigned));
02263      for (unsigned i = 0; i < nTrk; i++)
02264      id[id0[i]] = i;
02265 
02266 #ifdef TRKRECO_DEBUG_DETAIL
02267 for (unsigned i = 0; i < nTrk; i++)
02268 std::cout << "id0 " << i << " ... " << id0[i] << std::endl;
02269 for (unsigned i = 0; i < nTrk; i++)
02270 std::cout << "id  " << i << " ... " << id[i] << std::endl;
02271 #endif
02272   //    unsigned n = BsCouTab(RECMDC_TRK_ADD);
02273   //    unsigned n = MdcRecTrkAddCol::getMdcRecTrkAddCol()->size();
02274 
02275   //    for (unsigned i = 0; i < n; i++) {
02276   //    reccdc_trk_add & w = * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02277   //                                                       i + 1,
02278   //                                                       BBS_No_Index);
02279   //    MdcRec_trk_add & w = (* MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
02280   //    if (w.mother) w.mother = id[w.mother - 1] + 1;
02281   //    if (w.daughter) w.daughter = id[w.daughter - 1] + 1;
02282   //    }
02283 
02284 #ifdef TRKRECO_DEBUG_DETAIL
02285 std::cout << "TTrackManager::tagReccdc ... RECMDC_TRK_ADD done" << std::endl;
02286 #endif
02287 
02288 n = BsCouTab(RECMDC_WIRHIT);
02289 for (unsigned i = 0; i < n; i++) {
02290 reccdc_wirhit & w = * (reccdc_wirhit *) BsGetEnt(RECMDC_WIRHIT,
02291 i + 1,
02292 BBS_No_Index);
02293 if (w.m_trk == 0) continue;
02294 w.m_trk = id[w.m_trk - 1] + 1;
02295 }
02296 
02297 #ifdef TRKRECO_DEBUG_DETAIL
02298 std::cout << "TTrackManager::tagReccdc ... RECMDC_WIRHIT done" << std::endl;
02299 #endif
02300 
02301 n = BsCouTab(DATMDC_MCWIRHIT);
02302 for (unsigned i = 0; i < n; i++) {
02303 datcdc_mcwirhit & m =
02304    * (datcdc_mcwirhit *) BsGetEnt(DATMDC_MCWIRHIT,i + 1,BBS_No_Index);
02305    if (m.m_trk == 0) continue;
02306    m.m_trk = id[m.m_trk - 1] + 1;
02307    }
02308 
02309 #ifdef TRKRECO_DEBUG_DETAIL
02310 std::cout << "TTrackManager::tagReccdc ... DATMDC_MCWIRHIT done" << std::endl;
02311 #endif
02312 
02313 n = BsCouTab(RECTRK);
02314 for (unsigned i = 0; i < n; i++) {
02315 rectrk & r = * (rectrk *) BsGetEnt(RECTRK, i + 1, BBS_No_Index);
02316 if (r.m_prekal == 0) continue;
02317 r.m_prekal = id[r.m_prekal - 1] + 1;
02318 }
02319 
02320 #ifdef TRKRECO_DEBUG_DETAIL
02321 std::cout << "TTrackManager::tagReccdc ... RECTRK done" << std::endl;
02322 #endif
02323 
02324 // jtanaka
02325 n = BsCouTab(RECMDC_SVD_TRK);
02326 for (unsigned i = 0; i < n; i++) {
02327 reccdc_svd_trk & r = * (reccdc_svd_trk *) BsGetEnt(RECMDC_SVD_TRK, i + 1, BBS_No_Index);
02328 if (r.m_cdc_trk == 0) continue;
02329 r.m_cdc_trk = id[r.m_cdc_trk - 1] + 1;
02330 }
02331 
02332 #ifdef TRKRECO_DEBUG_DETAIL
02333 std::cout << "TTrackManager::tagReccdc ... RECMDC_SVD_TRK done" << std::endl;
02334 #endif
02335 
02336 free(id);
02337 */
02338 }
02339 
02340 void
02341 TTrackManager::setCurlerFlags(void) {
02342   unsigned n = _tracks.length();
02343   if (n < 2) return;
02344 
02345   for (unsigned i = 0; i < n - 1; i++) {
02346     TTrack & t0 = * _tracks[i];
02347     if (t0.type() != TrackTypeCurl) continue;
02348     float c0 = t0.charge();
02349 
02350     for (unsigned j = i + 1; j < n; j++) {
02351       TTrack & t1 = * _tracks[j];
02352       float c1 = t1.charge();
02353       if (c0 * c1 > 0.) continue;
02354       if (t1.type() != TrackTypeCurl) continue;
02355 
02356       bool toBeMerged = false;
02357       unsigned n0 = t0.testByApproach(t1.cores(), _sigmaCurlerMergeTest);
02358       if (n0 > _nCurlerMergeTest) toBeMerged = true;
02359       if (! toBeMerged) {
02360         unsigned n1 = t1.testByApproach(t0.cores(),
02361             _sigmaCurlerMergeTest);
02362         if (n1 > _nCurlerMergeTest) toBeMerged = true;
02363       }
02364 
02365       if (toBeMerged) {
02366         //              ++_s->_nToBeMerged;
02367         if ((t0.daughter()) || (t1.daughter()))
02368           ++_s->_nToBeMergedMoreThanTwo;
02369         t0.daughter(& t1);
02370         t1.daughter(& t0);
02371 
02372       }
02373     }
02374   }
02375 }
02376 
02377 void
02378 TTrackManager::salvageAssociateHits(const AList<TMDCWireHit> & hits,
02379     float maxSigma2) {
02380   //#define TRKRECO_DEBUG
02381   //#define TRKRECO_DEBUG_DETAIL
02382 
02383 #ifdef TRKRECO_DEBUG
02384   std::cout << "... trkmgr::salvaging associate hits" << std::endl;
02385   std::cout << "    # of given hits=" << hits.length() << std::endl;
02386 #endif
02387 
02388   //...Check arguments...
02389   unsigned nTracks = _tracks.length();
02390   if (nTracks == 0) return;
02391   unsigned nHits = hits.length();
02392   if (nHits == 0) return;
02393 
02394   static const TPoint2D o(0., 0.);
02395 
02396   //...Hit loop...
02397   for (unsigned i = 0; i < nHits; i++) {
02398     TMDCWireHit & h = * hits[i];
02399 
02400     //...Already used?...
02401     if (h.state() & WireHitUsed) continue;
02402 #ifdef TRKRECO_DEBUG_DETAIL
02403     std::cout << "    checking " << h.wire()->name();
02404 #endif
02405 
02406     //...Track loop...
02407     AList<TMLink> toBeDeleted;
02408     TMLink * best = NULL;
02409     TTrack * bestTrack = NULL;
02410     for (unsigned j = 0; j < nTracks; j++) {
02411       TTrack & t = * _tracks[j];
02412 
02413       //...Pre-selection...
02414       TPoint2D c = t.center();
02415       TPoint2D co = - c;
02416       TPoint2D x = h.wire()->xyPosition();
02417 
02418 #ifdef TRKRECO_DEBUG_DETAIL
02419       std::cout << " : c= " << co.cross(x - c) * t.charge();
02420       std::cout << ", d=" << fabs((x - c).mag() - fabs(t.radius()));
02421 #endif
02422 
02423       if (co.cross(x - c) * t.charge() > 0.)
02424         continue;
02425       if (fabs((x - c).mag() - fabs(t.radius())) > 5.)
02426         continue;
02427 
02428       //...Try to append this hit...
02429       TMLink & link = * new TMLink(0, & h);
02430       int err = t.approach(link);
02431       if (err < 0) {
02432 #ifdef TRKRECO_DEBUG_DETAIL
02433         std::cout << " : " << t.name() << " approach failure";
02434 #endif
02435         toBeDeleted.append(link);
02436         continue;
02437       }
02438 
02439       //...Calculate sigma...
02440       float distance = link.distance();
02441       float diff = fabs(distance - link.hit()->drift());
02442       float sigma = diff / link.hit()->dDrift();
02443       link.pull(sigma * sigma);
02444 
02445 #ifdef TRKRECO_DEBUG_DETAIL
02446       std::cout << " : " << t.name() << " pull = " << link.pull();
02447 #endif
02448       if (link.pull() > maxSigma2) {
02449         toBeDeleted.append(link);
02450         continue;
02451       }
02452 
02453       if (best) {
02454         if (best->pull() > link.pull()) {
02455           toBeDeleted.append(best);
02456           best = & link;
02457           bestTrack = & t;
02458         }
02459         else {
02460           toBeDeleted.append(link);
02461         }
02462       }
02463       else {
02464         best = & link;
02465         bestTrack = & t;
02466       }
02467     }
02468 
02469     if (best) {
02470       bestTrack->append(* best);
02471       best->hit()->state(best->hit()->state() | WireHitInvalidForFit);
02472       _associateHits.append(best);
02473 #ifdef TRKRECO_DEBUG
02474       std::cout << "    " << best->hit()->wire()->name();
02475       std::cout << " -> " << bestTrack->name() << std::endl;
02476 #endif
02477     }
02478     HepAListDeleteAll(toBeDeleted);
02479 
02480 #ifdef TRKRECO_DEBUG_DETAIL
02481     std::cout << std::endl;
02482 #endif
02483   }
02484 }
02485 
02486 void
02487 TTrackManager::maskBadHits(const AList<TTrack> & tracks, float maxSigma2) {
02488 #ifdef TRKRECO_DEBUG
02489   std::cout << "... trkmgr::maskBadHits" << std::endl;
02490 #endif          
02491 
02492   unsigned n = tracks.length();
02493   for (unsigned i = 0; i < n; i++) {
02494     TTrack & t = * tracks[i];
02495     bool toBeUpdated = false;
02496     const AList<TMLink> links = t.links();
02497     unsigned nHits = links.length();
02498     for (unsigned j = 0; j < nHits; j++) {
02499       if (links[j]->pull() > maxSigma2) {
02500         links[j]->hit()->state(links[j]->hit()->state() |
02501             WireHitInvalidForFit);
02502         toBeUpdated = true;
02503 #ifdef TRKRECO_DEBUG
02504         std::cout << "    " << t.name() << " : ";
02505         std::cout << links[j]->wire()->name() << "(pull=";
02506         std::cout << links[j]->pull() << ") is masked" << std::endl;
02507 #endif          
02508       }
02509     }
02510     if (toBeUpdated) t.update();
02511   }
02512 }
02513 
02514 void
02515 TTrackManager::clearTables(void) const {
02516   //    BsDelEnt(RECMDC_TRK, BBS_ID_ALL);
02517   //    BsDelEnt(RECMDC_TRK_ADD, BBS_ID_ALL);
02518   //    BsDelEnt(RECMDC_MCTRK, BBS_ID_ALL);
02519   //    BsDelEnt(RECMDC_MCTRK2HEP, BBS_ID_ALL);
02520   unsigned n = MdcRecTrkCol::getMdcRecTrkCol()->size();
02521   for (unsigned i = 0; i < n; i++) delete &(*MdcRecTrkCol::getMdcRecTrkCol())[i];
02522 
02523   n = MdcRecTrkAddCol::getMdcRecTrkAddCol()->size();
02524   for (unsigned i = 0; i < n; i++) delete &(*MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
02525 
02526   n = MdcRecMctrkCol::getMdcRecMctrkCol()->size();
02527   for (unsigned i = 0; i < n; i++) delete &(*MdcRecMctrkCol::getMdcRecMctrkCol())[i];
02528 
02529   n = MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol()->size();
02530   for (unsigned i = 0; i < n; i++) delete &(*MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol())[i];
02531 
02532 
02533   //...Clear track association...
02534   //    unsigned n = BsCouTab(RECMDC_WIRHIT);
02535   n = MdcRecWirhitCol::getMdcRecWirhitCol()->size();
02536   for (unsigned i = 0; i < n; i++) {
02537     //  reccdc_wirhit & h = * (reccdc_wirhit *)
02538     //      BsGetEnt(RECMDC_WIRHIT, i + 1, BBS_No_Index);
02539     //  h.m_trk = 0;
02540     MdcRec_wirhit& h = (*MdcRecWirhitCol::getMdcRecWirhitCol())[i];
02541     h.trk = 0;
02542   }
02543   //    n = BsCouTab(DATMDC_MCWIRHIT);
02544   n = MdcDatMcwirhitCol::getMdcDatMcwirhitCol()->size();
02545   for (unsigned i = 0; i < n; i++) {
02546     //  datcdc_mcwirhit & h = * (datcdc_mcwirhit *)
02547     //      BsGetEnt(DATMDC_MCWIRHIT, i + 1, BBS_No_Index);
02548     //  h.m_trk = 0;
02549     MdcDat_mcwirhit& h = (*MdcDatMcwirhitCol::getMdcDatMcwirhitCol())[i];
02550     h.trk = 0;
02551   }
02552 }
02553 
02554 AList<TTrack>
02555 TTrackManager::selectGoodTracks(const AList<TTrack> & list,
02556     bool track2D) const {
02557   AList<TTrack> goodTracks;
02558   unsigned n = list.length();
02559   for (unsigned i = 0; i < n; i++) {
02560     const TTrack & t = * list[i];
02561     if (! goodTrack(t, track2D)) continue;
02562 
02563     //...Remove super momentum...
02564     if (_maxMomentum > 0.) {
02565       if (t.ptot() > _maxMomentum) {
02566         //              ++_s->_nSuperMoms;
02567         continue;
02568       }
02569     }
02570 
02571     goodTracks.append((TTrack &) t);
02572   }
02573 
02574   if (_debugLevel) {
02575     if (list.length() != goodTracks.length()) {
02576       std::cout << "TTrackManager::selectGoodTracks ... bad tracks found"
02577         << std::endl
02578         << "                                    # of bad tracks = "
02579         << list.length() - goodTracks.length()
02580         << " : 2D flag = " << track2D << std::endl;
02581       AList<TTrack> tmp;
02582       tmp.append(list);
02583       tmp.remove(goodTracks);
02584       std::cout << "    Track dump" << std::endl;
02585       for (unsigned i = 0; i < tmp.length(); i++) {
02586         std::cout << "    " << TrackDump(* tmp[i]) << std::endl;
02587       }
02588     }
02589   }
02590 
02591   return goodTracks;
02592 }
02593 
02594 bool
02595 TTrackManager::checkNumberOfHits(const TTrack & t, bool track2D) {
02596   const AList<TMLink> & cores = t.cores();
02597 
02598   if (track2D) {
02599     unsigned axialHits = NAxialHits(cores);
02600     if (axialHits < 3) return false;
02601   }
02602   else {
02603     unsigned allHits = cores.length();
02604     if (allHits < 5) return false;
02605     unsigned stereoHits = NStereoHits(cores);
02606     if (stereoHits < 2) return false;
02607     unsigned axialHits = allHits - stereoHits;
02608     if (axialHits < 3) return false;
02609   }
02610   return true;
02611 }
02612 
02613 void
02614 TTrackManager::determineIP(void) {
02615   static const HepVector3D InitialVertex(0., 0., 0.);
02616 
02617   //...Track selection...
02618   //    unsigned n = BsCouTab(RECTRK);
02619   unsigned n = MdcTrkCol::getMdcTrkCol()->size();
02620   AList<MdcTrk_localz> zList;
02621   for (unsigned i = 0; i < n; i++) {
02622     //  const rectrk & t = * (rectrk *) BsGetEnt(RECTRK, i + 1, BBS_No_Index);
02623     const MdcTrk & t = (* MdcTrkCol::getMdcTrkCol())[i];
02624 
02625     if (t.prekal == 0) continue;
02626     //  const reccdc_trk_add & c = * (reccdc_trk_add *)
02627     //      BsGetEnt(RECMDC_TRK_ADD, t.m_prekal, BBS_No_Index);
02628     const MdcRec_trk_add & c = * t.prekal->add;
02629 
02630     //...Only good tracks...
02631     if (c.quality) continue;
02632 
02633     //...Require SVD hits...
02634     //  const rectrk_global & g = * (rectrk_global *) BsGetEnt(RECTRK_GLOBAL,
02635     //                                                         t.m_glob[2],
02636     //                                                         BBS_No_Index);
02637     const MdcTrk_global & g = * t.glob[2];
02638 
02639     if (! & g) continue;
02640     if (g.nhits[3] < 2) continue;
02641     if (g.nhits[4] < 2) continue;
02642 
02643     //...OK...
02644     //  const rectrk_localz & z = * (rectrk_localz *) BsGetEnt(RECTRK_LOCALZ,
02645     //                                                         t.m_zero[2],
02646     //                                                         BBS_No_Index);
02647     MdcTrk_localz & z = * t.zero[2];
02648 
02649     if (! & z) continue;
02650     //  zList.append((rectrk_localz &) z);
02651     zList.append(z);
02652   }
02653   unsigned nZ = zList.length();
02654   if (nZ < 2) return;
02655 
02656   //...Fitting...
02657   //      kvertexfitter kvf;
02658   //      kvf.initialVertex(initialVertex);
02659   //      for (unsigned i = 0; i < nZ; i++) {
02660   //    kvf.addTrack();
02661   //      }
02662 
02663 }
02664 
02665 void
02666 TTrackManager::tagRectrk(unsigned * id0, unsigned nTrk) const {
02667   /*
02668      unsigned * id = (unsigned *) malloc(nTrk * sizeof(unsigned));
02669      for (unsigned i = 0; i < nTrk; i++)
02670      id[id0[i]] = i;
02671 
02672   //      for (unsigned i = 0; i < nTrk; i++)
02673   //            std::cout << "id0 " << i << " ... " << id0[i] << std::endl;
02674   //      for (unsigned i = 0; i < nTrk; i++)
02675   //            std::cout << "id  " << i << " ... " << id[i] << std::endl;
02676   //      BsShwDat(RECTRK_TOF);
02677 
02678   unsigned n = BsCouTab(RECTRK_TOF);
02679   for (unsigned i = 0; i < n; i++) {
02680   rectrk_tof & t = * (rectrk_tof *) BsGetEnt(RECTRK_TOF,
02681   i + 1,
02682   BBS_No_Index);
02683   if (t.m_rectrk) t.m_rectrk = id[t.m_rectrk - 1] + 1;
02684   }
02685 
02686   //      BsShwDat(RECTRK_TOF);
02687 
02688   // jtanaka
02689   n = BsCouTab(RECSVD_HIT);
02690   for (unsigned i = 0; i < n; i++) {
02691   recsvd_hit & t = * (recsvd_hit *) BsGetEnt(RECSVD_HIT,
02692   i + 1,
02693   BBS_No_Index);
02694   if (t.m_trk) t.m_trk = id[t.m_trk - 1] + 1;
02695   }
02696 
02697   free(id);
02698   */
02699 }
02700 
02701 /*
02702 // jtanaka 000925 -->
02703 #define TRKRECO_REPLACE_TABLE 1
02704 #if !(TRKRECO_REPLACE_TABLE)
02705 void
02706 copyRecMDC_trk_Table(const Reccdc_trk & org,
02707 Reccdc_trk & copied)
02708 {
02709 copied.helix(0, org.helix(0));
02710 copied.helix(1, org.helix(1));
02711 copied.helix(2, org.helix(2));
02712 copied.helix(3, org.helix(3));
02713 copied.helix(4, org.helix(4));
02714 copied.pivot(0, org.pivot(0));
02715 copied.pivot(1, org.pivot(1));
02716 copied.pivot(2, org.pivot(2));
02717 copied.error(0, org.error(0));
02718 copied.error(1, org.error(1));
02719 copied.error(2, org.error(2));
02720 copied.error(3, org.error(3));
02721 copied.error(4, org.error(4));
02722 copied.error(5, org.error(5));
02723 copied.error(6, org.error(6));
02724 copied.error(7, org.error(7));
02725 copied.error(8, org.error(8));
02726 copied.error(9, org.error(9));
02727 copied.error(10, org.error(10));
02728 copied.error(11, org.error(11));
02729 copied.error(12, org.error(12));
02730 copied.error(13, org.error(13));
02731 copied.error(14, org.error(14));
02732 copied.chiSq(org.chiSq());
02733 copied.ndf(org.ndf());
02734 copied.fiTerm(org.fiTerm());
02735 copied.nhits(org.nhits());
02736 copied.nster(org.nster());
02737 copied.nclus(org.nclus());
02738 copied.stat(org.stat());
02739 copied.mass(org.mass());
02740 }
02741 
02742 void
02743 copyRecMDC_trk_add_Table(const Reccdc_trk_add & org,
02744 Reccdc_trk_add & copied)
02745 {
02746 copied.quality(org.quality());
02747 copied.kind(org.kind());
02748 copied.mother(org.mother());
02749 copied.daughter(org.daughter());
02750 copied.decision(org.decision());
02751 copied.likelihood(0, org.likelihood(0));
02752 copied.likelihood(1, org.likelihood(1));
02753 copied.likelihood(2, org.likelihood(2));
02754 copied.stat(org.stat());
02755 copied.rectrk(org.rectrk());
02756 }
02757 
02758 void
02759 copyRecMDC_MCtrk_Table(const Reccdc_mctrk & org,
02760 Reccdc_mctrk & copied)
02761 {
02762 copied.hep(org.hep());
02763 copied.wirFrac(org.wirFrac());
02764 copied.wirFracHep(org.wirFracHep());
02765 copied.charge(org.charge());
02766 copied.ptFrac(org.ptFrac());
02767 copied.pzFrac(org.pzFrac());
02768 copied.quality(org.quality());
02769 }
02770 
02771 void
02772 copyRecMDC_MCtrk2hep_Table(const Reccdc_mctrk2hep & org,
02773     Reccdc_mctrk2hep & copied)
02774 {  
02775   copied.wir(org.wir());
02776   copied.clust(org.clust());
02777   copied.trk(org.trk());
02778   copied.hep(org.hep());
02779 }
02780 #endif
02781 
02782 void
02783 TTrackManager::addSvd(const int mcFlag) const {
02784   TSvdAssociator svdA(-20000.,20000.);
02785   svdA.fillClusters();
02786 
02787   //BsShwDat(RECTRK);
02788   //BsShwDat(RECMDC_TRK);
02789   //BsShwDat(RECMDC_MCTRK);
02790 
02791   Reccdc_trk_Manager& trkMgr =
02792     Reccdc_trk_Manager::get_manager();
02793   Reccdc_trk_add_Manager& trkMgr2 =
02794     Reccdc_trk_add_Manager::get_manager();
02795   Reccdc_svd_trk_Manager& svdMgr =
02796     Reccdc_svd_trk_Manager::get_manager();
02797 
02798 #if !(TRKRECO_REPLACE_TABLE)
02799   Reccdc_wirhit_Manager& wirMgr =
02800     Reccdc_wirhit_Manager::get_manager();
02801   Reccdc_mctrk_Manager& mcMgr =
02802     Reccdc_mctrk_Manager::get_manager();
02803   Reccdc_mctrk2hep_Manager& mcMgr2 =
02804     Reccdc_mctrk2hep_Manager::get_manager();
02805   Datcdc_mcwirhit_Manager& mcMgr3 =
02806     Datcdc_mcwirhit_Manager::get_manager();
02807 #endif
02808 
02809   int nSize = trkMgr.count();  
02810   for(int i=0;i<nSize;++i){
02811     //std::cout << "trk " << i << ": " << trkMgr[i].helix(0) << std::endl;
02812 #if 1
02813     // Reconstruction Info --> SVD Recon.
02814     if(trkMgr2[i].decision() != TrackPMCurlFinder && 
02815         (trkMgr2[i].quality() & TrackQuality2D) != TrackQuality2D && 
02816         trkMgr[i].helix(2) != 0. && fabs(1./trkMgr[i].helix(2))<0.2){
02817       HepVector a(5);
02818       a[0] = trkMgr[i].helix(0);
02819       a[1] = trkMgr[i].helix(1);
02820       a[2] = trkMgr[i].helix(2);
02821       a[3] = trkMgr[i].helix(3);
02822       a[4] = trkMgr[i].helix(4);
02823       HepPoint3D p(trkMgr[i].pivot(0),
02824           trkMgr[i].pivot(1),
02825           trkMgr[i].pivot(2));
02826       Helix th(p,a);
02827       th.pivot(HepPoint3D(0.,0.,0.)); // pivot = (0,0,0)
02828       AList<TSvdHit> cand;
02829       double tz,tt;
02830       if(svdA.recTrk(th,tz,tt,0.5,50.0,cand,0.5)){
02831         //std::cout << "SVD in " << i << std::endl;
02832 #if TRKRECO_REPLACE_TABLE
02833         Reccdc_svd_trk & newSvd  = svdMgr.add();
02834 #else
02835         Reccdc_trk     & newTrk  = trkMgr.add();
02836         Reccdc_trk_add & newTrk2 = trkMgr2.add();
02837         Reccdc_svd_trk & newSvd  = svdMgr.add();
02838         // copy all information
02839         copyRecMDC_trk_Table(trkMgr[i],newTrk);
02840         copyRecMDC_trk_add_Table(trkMgr2[i],newTrk2);
02841 #endif
02842         if(mcFlag){
02843 #if TRKRECO_REPLACE_TABLE
02844           ;
02845 #else
02846           Reccdc_mctrk & newMcTrk = mcMgr.add();
02847           copyRecMDC_MCtrk_Table(mcMgr[i],mcMgr[mcMgr.count()-1]);
02848           int nMCt2h = mcMgr2.count();
02849           for(int j=0;j<nMCt2h;++j){
02850             if(mcMgr2[j].trk() &&
02851                 mcMgr2[j].trk().get_ID() == trkMgr[i].get_ID()){
02852               Reccdc_mctrk2hep & newMcTrk2Hep = mcMgr2.add();
02853               copyRecMDC_MCtrk2hep_Table(mcMgr2[j],newMcTrk2Hep);
02854               newMcTrk2Hep.trk(newTrk);
02855             }
02856           }
02857           int nMCwire = mcMgr3.count();
02858           for(int j=0;j<nMCwire;++j){
02859             if(mcMgr3[j].trk().get_ID() == trkMgr[i].get_ID()){
02860               mcMgr3[j].trk(newTrk);
02861             }
02862           }
02863 #endif
02864         }
02865         HepVector ta = th.a(); // pivot = (0,0,0)
02866         ta[3] = tz;
02867         ta[4] = tt;
02868         th.a(ta);
02869         th.pivot(p); // pivot, (0,0,0) --> p
02870 #if TRKRECO_REPLACE_TABLE
02871         trkMgr[i].helix(3, th.a()[3]);
02872         trkMgr[i].helix(4, th.a()[4]);
02873 #else
02874         newTrk.helix(3, th.a()[3]);
02875         newTrk.helix(4, th.a()[4]);
02876 #endif
02877 
02878         newSvd.Helix(0, ta[0]);
02879         newSvd.Helix(1, ta[1]);
02880         newSvd.Helix(2, ta[2]);
02881         newSvd.Helix(3, ta[3]);
02882         newSvd.Helix(4, ta[4]);
02883 #if TRKRECO_REPLACE_TABLE
02884         newSvd.cdc_trk(trkMgr2[i]);
02885 #else
02886         newSvd.cdc_trk(newTrk2);
02887 #endif
02888         newSvd.Status(0); // 0 is normal.
02889         int indexSvd  = 0;
02890         for(int j=0;j<cand.length();++j){
02891           if(indexSvd >= 16)break;
02892           if((cand[j])->rphi() && (cand[j])->z()){
02893             newSvd.svd_cluster(indexSvd, *(cand[j]->rphi()));
02894             ++indexSvd;
02895             newSvd.svd_cluster(indexSvd, *(cand[j]->z()));
02896             ++indexSvd;
02897           }else{
02898             std::cerr << "[TTrackManager of TrkReco] Why ? no associated SVDhit ?" << std::endl;
02899           }
02900         }
02901 #if TRKRECO_REPLACE_TABLE
02902         trkMgr2[i].quality(0); // set to 0 --> GOOD!
02903         trkMgr2[i].decision((trkMgr2[i].decision() | TrackSVDAssociator));
02904 #else
02905         newTrk2.quality(1); // temporary
02906         newTrk2.decision((newTrk2.decision() | TrackSVDAssociator));
02907 #endif
02908 #if !(TRKRECO_REPLACE_TABLE)
02909         // MDC Wire information
02910         for(int j=0;j<wirMgr.count();++j){
02911           if(wirMgr[j].trk() &&
02912               wirMgr[j].trk().get_ID() == trkMgr[i].get_ID()){
02913             wirMgr[j].trk(newTrk);
02914           }
02915         }
02916 #endif
02917       }else if(fabs(th.a()[3]) > 30.){
02918         if(svdA.recTrk(th,tz,tt,0.5,-1.0,cand,0.5)){
02919           //std::cout << "SVD in " << i << std::endl;
02920 #if TRKRECO_REPLACE_TABLE
02921           Reccdc_svd_trk & newSvd  = svdMgr.add();
02922 #else
02923           Reccdc_trk     & newTrk  = trkMgr.add();
02924           Reccdc_trk_add & newTrk2 = trkMgr2.add();
02925           Reccdc_svd_trk & newSvd  = svdMgr.add();
02926           // copy all information
02927           copyRecMDC_trk_Table(trkMgr[i],newTrk);
02928           copyRecMDC_trk_add_Table(trkMgr2[i],newTrk2);
02929 #endif
02930           if(mcFlag){
02931 #if TRKRECO_REPLACE_TABLE
02932             ;
02933 #else
02934             Reccdc_mctrk & newMcTrk = mcMgr.add();
02935             copyRecMDC_MCtrk_Table(mcMgr[i],mcMgr[mcMgr.count()-1]);
02936             int nMCt2h = mcMgr2.count();
02937             for(int j=0;j<nMCt2h;++j){
02938               if(mcMgr2[j].trk() &&
02939                   mcMgr2[j].trk().get_ID() == trkMgr[i].get_ID()){
02940                 Reccdc_mctrk2hep & newMcTrk2Hep = mcMgr2.add();
02941                 copyRecMDC_MCtrk2hep_Table(mcMgr2[j],newMcTrk2Hep);
02942                 newMcTrk2Hep.trk(newTrk);
02943               }
02944             }
02945             int nMCwire = mcMgr3.count();
02946             for(int j=0;j<nMCwire;++j){
02947               if(mcMgr3[j].trk().get_ID() == trkMgr[i].get_ID()){
02948                 mcMgr3[j].trk(newTrk);
02949               }
02950             }
02951 #endif
02952           }
02953           HepVector ta = th.a(); // pivot = (0,0,0)
02954           ta[3] = tz;
02955           ta[4] = tt;
02956           th.a(ta);
02957           th.pivot(p); // pivot, (0,0,0) --> p
02958 #if TRKRECO_REPLACE_TABLE
02959           trkMgr[i].helix(3, th.a()[3]);
02960           trkMgr[i].helix(4, th.a()[4]);
02961 #else
02962           newTrk.helix(3, th.a()[3]);
02963           newTrk.helix(4, th.a()[4]);
02964 #endif
02965 
02966           newSvd.Helix(0, ta[0]);
02967           newSvd.Helix(1, ta[1]);
02968           newSvd.Helix(2, ta[2]);
02969           newSvd.Helix(3, ta[3]);
02970           newSvd.Helix(4, ta[4]);
02971 #if TRKRECO_REPLACE_TABLE
02972           newSvd.cdc_trk(trkMgr2[i]);
02973 #else
02974           newSvd.cdc_trk(newTrk2);
02975 #endif
02976           newSvd.Status(0); // 0 is normal.
02977           int indexSvd  = 0;
02978           for(int j=0;j<cand.length();++j){
02979             if(indexSvd >= 16)break;
02980             if((cand[j])->rphi() && (cand[j])->z()){
02981               newSvd.svd_cluster(indexSvd, *(cand[j]->rphi()));
02982               ++indexSvd;
02983               newSvd.svd_cluster(indexSvd, *(cand[j]->z()));
02984               ++indexSvd;
02985             }else{
02986               std::cerr << "[TTrackManager of TrkReco] Why ? no associated SVDhit ?" << std::endl;
02987             }
02988           }
02989 #if TRKRECO_REPLACE_TABLE
02990           trkMgr2[i].quality(0); // set to 0 --> GOOD!
02991           trkMgr2[i].decision((trkMgr2[i].decision() | TrackSVDAssociator));
02992 #else
02993           newTrk2.quality(1); // temporary
02994           newTrk2.decision((newTrk2.decision() | TrackSVDAssociator));
02995 #endif
02996 #if !(TRKRECO_REPLACE_TABLE)
02997           // MDC Wire information
02998           for(int j=0;j<wirMgr.count();++j){
02999             if(wirMgr[j].trk() &&
03000                 wirMgr[j].trk().get_ID() == trkMgr[i].get_ID()){
03001               wirMgr[j].trk(newTrk);
03002             }
03003           }
03004 #endif    
03005         }
03006       }
03007     }
03008   }
03009 #endif
03010 }
03011 // <-- jtanaka 000925
03012 */
03013 
03014 bool
03015 TTrackManager::goodTrack(const TTrack & t, bool track2D) {
03016 
03017   //...Check number of hits...
03018   if (! checkNumberOfHits(t, track2D)) return false;
03019 
03020   //...Check helix parameter...
03021   if (HelixHasNan(t.helix())) return false;
03022 
03023   return true;
03024 }

Generated on Tue Nov 29 23:14:17 2016 for BOSS_7.0.2 by  doxygen 1.4.7