31#include "GaudiKernel/MsgStream.h"
32#include "GaudiKernel/AlgFactory.h"
37#include "GaudiKernel/SmartDataPtr.h"
60 Algorithm(name, pSvcLocator)
63 declareProperty(
"debug",
m_debug =
false);
64 declareProperty(
"hist", m_hist =
false);
66 declareProperty(
"doSag", m_doSag =
false);
68 declareProperty(
"cosmicSewPar", m_cosmicSewPar);
69 declareProperty(
"cosmicSewSkip", m_cosmicSewSkip=
false);
71 declareProperty(
"countPropTime", m_countPropTime =
true);
72 declareProperty(
"lineFit", m_lineFit =
false);
86 if(
NULL == m_gm)
return StatusCode::FAILURE;
88 return StatusCode::SUCCESS;
95 MsgStream log(
msgSvc(), name());
97 log << MSG::INFO <<
"in initialize()" << endreq;
103 sc = service (
"MagneticFieldSvc",m_pIMF);
104 if(sc!=StatusCode::SUCCESS) {
105 log << MSG::ERROR <<
"Unable to open Magnetic field service"<<endreq;
107 m_bfield =
new BField(m_pIMF);
108 log << MSG::INFO <<
"field z = "<<m_bfield->
bFieldNominal()<< endreq;
114 sc = service (
"MdcCalibFunSvc", imdcCalibSvc);
116 if ( sc.isFailure() ){
117 log << MSG::FATAL <<
"Could not load MdcCalibFunSvc!" << endreq;
118 return StatusCode::FAILURE;
123 sc = service (
"RawDataProviderSvc", iRawDataProvider);
124 if ( sc.isFailure() ){
125 log << MSG::FATAL <<
"Could not load RawDataProviderSvc!" << endreq;
126 return StatusCode::FAILURE;
132 sc = service (
"MdcUtilitySvc", imdcUtilitySvc);
133 m_mdcUtilitySvc =
dynamic_cast<MdcUtilitySvc*
> (imdcUtilitySvc);
134 if ( sc.isFailure() ){
135 log << MSG::FATAL <<
"Could not load MdcUtilitySvc!" << endreq;
136 return StatusCode::FAILURE;
141 sc = service (
"MdcPrintSvc", imdcPrintSvc);
142 m_mdcPrintSvc =
dynamic_cast<MdcPrintSvc*
> (imdcPrintSvc);
143 if ( sc.isFailure() ){
144 log << MSG::FATAL <<
"Could not load MdcPrintSvc!" << endreq;
145 return StatusCode::FAILURE;
150 NTuplePtr ntCsmcSew(
ntupleSvc(),
"MdcxReco/csmc");
151 if ( ntCsmcSew ) { m_xtupleCsmcSew = ntCsmcSew;}
153 m_xtupleCsmcSew =
ntupleSvc()->book (
"MdcxReco/csmc", CLID_ColumnWiseTuple,
"MdcxReco reconsturction results");
154 if ( m_xtupleCsmcSew ) {
155 sc = m_xtupleCsmcSew->addItem (
"dD0", m_csmcD0);
156 sc = m_xtupleCsmcSew->addItem (
"dPhi0", m_csmcPhi0);
157 sc = m_xtupleCsmcSew->addItem (
"dZ0", m_csmcZ0);
158 sc = m_xtupleCsmcSew->addItem (
"dOmega", m_csmcOmega);
159 sc = m_xtupleCsmcSew->addItem (
"dPt", m_csmcPt);
160 sc = m_xtupleCsmcSew->addItem (
"dTanl", m_csmcTanl);
162 log << MSG::FATAL <<
"Could not book MdcxReco/csmc!" << endreq;
163 return StatusCode::FAILURE;
168 return StatusCode::SUCCESS;
172 MsgStream log(
msgSvc(), name());
173 log << MSG::INFO <<
"in execute()" << endreq;
176 setFilterPassed(
false);
178 SmartDataPtr<Event::EventHeader> evtHead(eventSvc(),
"/Event/EventHeader");
180 log << MSG::FATAL<<
"Could not retrieve event header" << endreq;
181 return StatusCode::FAILURE;
183 long t_runNo = evtHead->runNumber();
184 long t_evtNo = evtHead->eventNumber();
185 if (m_debug) std::cout <<
"sew "<<++i_evt<<
" run "<<t_runNo<<
" evt " << t_evtNo << std::endl;
190 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc(),
"/Event/Recon/RecEsTimeCol");
191 if (!aevtimeCol || aevtimeCol->size()==0) {
192 log << MSG::WARNING<<
"Could not find RecEsTimeCol"<< endreq;
193 return StatusCode::SUCCESS;
195 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
196 for(; iter_evt!=aevtimeCol->end(); iter_evt++){
197 m_bunchT0 = (*iter_evt)->getTest();
203 if (!recMdcTrackCol || ! recMdcHitCol)
return StatusCode::SUCCESS;
204 if (2!=recMdcTrackCol->size()){
206 return StatusCode::SUCCESS;
208 if(m_debug)std::cout<<name()<<
" nTk==2 begin sewing"<< std::endl;
212 double dParam[5]={0.,0.,0.,0.,0.};
215 RecMdcTrackCol::iterator besthel;
217 int besthelId = -999;
218 RecMdcTrackCol::iterator it = recMdcTrackCol->begin();
219 for (
int iTk=0; it != recMdcTrackCol->end(); it++,iTk++) {
222 double d0 = -tk->
helix(0);
223 double phi0 = tk->
helix(1)+ CLHEP::halfpi;
224 double omega = Bz*tk->
helix(2)/-333.567;
225 double z0 = tk->
helix(3);
226 double tanl = tk->
helix(4);
231 if(m_debug)std::cout<<__FILE__<<
" "<<__LINE__<<
"tk"<<iTk
232 <<
"("<<d0<<
","<<phi0<<
"," <<
","<<omega<<
","<<z0<<
","<<tanl<<
")"<<std::endl;
252 float bchisq = tk->
chi2();
253 int bndof = tk->
ndof();
262 if(besthelId == -999)
return StatusCode::SUCCESS;
269 std::cout<<__FILE__<<
" param diff: " <<
"\n D0 " << dParam[0]
270 <<
"\n Phi0 " << dParam[1] <<
"\n Omega " << dParam[2]
271 <<
"\n Z0 " << dParam[3] <<
"\n Tanl " << dParam[4] << std::endl;
276 m_csmcPhi0=dParam[1];
277 m_csmcOmega=dParam[2];
279 m_csmcTanl=dParam[4];
280 m_xtupleCsmcSew->write();
284 if( (fabs(dParam[0])>m_cosmicSewPar[0]) ||
285 (fabs(dParam[1])>m_cosmicSewPar[1]) ||
286 (fabs(dParam[2])>m_cosmicSewPar[2]) ||
287 (fabs(dParam[3])>m_cosmicSewPar[3]) ||
288 (fabs(dParam[4])>m_cosmicSewPar[4]) ){
289 if(m_debug)std::cout<<__FILE__<<
" 2 trk param not satisfied skip!"<<std::endl;
291 if(fabs(dParam[0])>m_cosmicSewPar[0]) std::cout<<
" cut by d0 "<<std::endl;
292 if(fabs(dParam[1])>m_cosmicSewPar[1]) std::cout<<
" cut by phi0 "<<std::endl;
293 if(fabs(dParam[2])>m_cosmicSewPar[2]) std::cout<<
" cut by omega "<<std::endl;
294 if(fabs(dParam[3])>m_cosmicSewPar[3]) std::cout<<
" cut by z0"<<std::endl;
295 if(fabs(dParam[4])>m_cosmicSewPar[4]) std::cout<<
" cut by tanl"<<std::endl;
298 return StatusCode::SUCCESS;
304 double d0 = -tk->
helix(0);
305 double phi0 = tk->
helix(1)+ CLHEP::halfpi;
306 double omega = Bz*tk->
helix(2)/-333.567;
307 double z0 = tk->
helix(3);
308 double tanl = tk->
helix(4);
311 if(m_debug)std::cout<<__FILE__<<
" best helix: No."<<tk->
trackId()<<
" Pat param=("<<d0<<
" "<<phi0
312 <<
" "<<omega<<
" "<<z0<<
" "<<tanl<<
")"<< std::endl;
318 newTrack = linefactory.
makeTrack(tt, chisum, *m_context, m_bunchT0*1.e-9);
321 newTrack = helixfactory.
makeTrack(tt, chisum, *m_context, m_bunchT0*1.e-9);
328 it = recMdcTrackCol->begin();
329 for (
int iTk=0; it != recMdcTrackCol->end(); it++,iTk++) {
346 if(m_debug)std::cout<<__FILE__<<
" sew fit failed!!!"<< std::endl;
347 HitRefVec::iterator it= skippedHits.begin();
348 for (;it!=skippedHits.end();++it){
delete it->data();}
361 std::cout<<__FILE__<<
" sew cosmic fit good "<< std::endl;
362 newTrack->
print(std::cout);
365 SmartDataPtr<MdcHitCol> m_hitCol(eventSvc(),
"/Event/Hit/MdcHitCol");
368 StatusCode sc = eventSvc()->registerObject(
"/Event/Hit/MdcHitCol",m_hitCol);
369 if(!sc.isSuccess()) {
370 std::cout<<
" Could not register MdcHitCol" <<endreq;
371 return StatusCode::FAILURE;
374 uint32_t getDigiFlag = 0;
376 const MdcDigi* m_digiMap[43][288];
377 MdcDigiVec::iterator
iter = mdcDigiVec.begin();
378 for (;
iter != mdcDigiVec.end();
iter++ ) {
383 m_digiMap[layer][wire] = aDigi;
387 HitRefVec::iterator itHitSkipped = skippedHits.begin();
388 for (;itHitSkipped!=skippedHits.end();++itHitSkipped){
398 double doca = m_mdcUtilitySvc->
docaPatPar(layer,wire,helix,err);
399 double newDoca = fabs(doca);
401 if ( flagLR == 0 ){ newDoca = -fabs(doca); }
403 if(m_debug>=3)std::cout<<
"("<<layer<<
","<<wire<<
") new doca "<<newDoca<<
" resid "<<fabs(fabs(newDoca)-fabs(h->
getDriftDistLeft()))<<std::endl;
407 MdcHit *thehit =
new MdcHit(m_digiMap[layer][wire], m_gm);
411 m_hitCol->push_back(thehit);
414 getInfo(helix,0,poca,tempDir);
415 if(m_debug>3)std::cout<<
"track poca "<<poca<<std::endl;
417 getInfo(helix,docaFltLen,pos,dir);
419 if(pos.y()>poca.y()){
420 int wireAmb = patAmbig(flagLR);
422 double tof = docaFltLen/
Constants::c + (m_bunchT0)*1.e-9;
428 double dist = thehit->
driftDist(tof, wireAmb, eAngle, dAngle, z);
429 double sigma = thehit->
sigma(dist,wireAmb,eAngle,dAngle,z);
431 if(m_debug>3&&fabs(fabs(h->
getDoca())-fabs(dist))>0.02)
433 <<
" new dd "<<dist <<
" newAmbig "<<wireAmb
436 <<
" new sigma "<<
sigma<<
" "<<std::endl;
447 store(newTrack, skippedHits);
449 setFilterPassed(
true);
455 return StatusCode::SUCCESS;
459 MsgStream log(
msgSvc(), name());
460 log << MSG::INFO <<
"in finalize()" << endreq;
461 std::cout<<name()<<
" after sewed got "<<m_nSewed<<
" cosmic tracks"<<std::endl;
462 return StatusCode::SUCCESS;
469 SmartDataPtr<MdcHitCol> m_hitCol(eventSvc(),
"/Event/Hit/MdcHitCol");
472 StatusCode sc = eventSvc()->registerObject(
"/Event/Hit/MdcHitCol",m_hitCol);
473 if(!sc.isSuccess()) {
474 std::cout<<
" Could not register MdcHitCol" <<endreq;
480 uint32_t getDigiFlag = 0;
482 if (0 == mdcDigiVec.size()){
483 std::cout <<
" No hits in MdcDigiVec" << std::endl;
486 const MdcDigi* m_digiMap[43][288];
487 MdcDigiVec::iterator
iter = mdcDigiVec.begin();
488 for (;
iter != mdcDigiVec.end();
iter++ ) {
493 m_digiMap[layer][wire] = aDigi;
498 getInfo(helix,0,poca,tempDir);
499 if(m_debug>3)std::cout<<
"track poca "<<poca<<std::endl;
501 HitRefVec::iterator itHit = recMdcHits.begin();
502 for (;itHit!=recMdcHits.end();++itHit){
511 if(pos.y()>poca.y()){
514 if(m_debug>3) std::cout<<
" Up track, change sign of fltLen "<<std::endl;
516 int newFlagLR = bes3FlagLR(newAmbig);
518 if(m_cosmicSewSkip && layer<20){
536 skippedHits.push_back(newSkippedHit);
538 MdcHit *thehit =
new MdcHit(m_digiMap[layer][wire], m_gm);
542 m_hitCol->push_back(thehit);
547 if(m_debug>3) std::cout<<name()<<
" ("<<layer<<
","<<wire<<
") fltLen "<<h->
getFltLen()<<
" newFltLen "<<newFltLen<<
" newAmbig "<<newAmbig<<
" pos.y() "<<pos.y()<<std::endl;
556 MsgStream log(
msgSvc(), name());
558 assert (aTrack !=
NULL);
561 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
562 DataObject *aTrackCol;
563 eventSvc()->findObject(
"/Event/Recon/RecMdcTrackCol",aTrackCol);
564 if(aTrackCol !=
NULL) {
565 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcTrackCol");
566 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcTrackCol");
569 DataObject *aRecHitCol;
570 eventSvc()->findObject(
"/Event/Recon/RecMdcHitCol",aRecHitCol);
571 if(aRecHitCol !=
NULL) {
572 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcHitCol");
573 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcHitCol");
580 if(!sc.isSuccess()) {
581 log << MSG::FATAL <<
" Could not register RecMdcTrack collection" <<endreq;
589 if(!sc.isSuccess()) {
590 log << MSG::FATAL <<
" Could not register RecMdcHit collection" <<endreq;
593 int trackId = trackList->size() - 1;
595 if(m_debug)std::cout<<__FILE__<<
" "<<__LINE__<<
" STORED"<< std::endl;
600 mdcTrack.
storeTrack(trackId, trackList, hitList, tkStat);
603 RecMdcTrackCol::iterator it = trackList->begin();
606 HitRefVec::iterator itHit = hl.begin();
608 for (;itHit!=hl.end();++itHit){
611 SmartRef<RecMdcHit> refHit(h);
612 hitRefVec.push_back(refHit);
615 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
"refHit stored "<< ihl<< std::endl;
617 HitRefVec::iterator itHitSkipped = skippedHits.begin();
619 for (;itHitSkipped!=skippedHits.end();++itHitSkipped){
621 (*itHitSkipped)->setTrkId(trackId);
622 hitList->push_back(*itHitSkipped);
623 SmartRef<RecMdcHit> refHitSkipped(*itHitSkipped);
624 hitRefVec.push_back(refHitSkipped);
626 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
"skippedHits stored "<< iskipped<< std::endl;
627 (*it)->setVecHits(hitRefVec);
629 (*it)->setNhits((*it)->getVecHits().size());
633 if((*it)->helix(2)<0.00001)(*it)->setNdof((*it)->getNhits()-4);
634 else (*it)->setNdof((*it)->getNhits()-5);
636 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
" stored "<< (*it)->getVecHits().size()<< std::endl;
641 MsgStream log(
msgSvc(), name());
647 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
648 DataObject *aTrackCol;
649 eventSvc()->findObject(
"/Event/Recon/RecMdcTrackCol",aTrackCol);
650 if(aTrackCol !=
NULL) {
651 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcTrackCol");
652 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcTrackCol");
654 DataObject *aRecHitCol;
655 eventSvc()->findObject(
"/Event/Recon/RecMdcHitCol",aRecHitCol);
656 if(aRecHitCol !=
NULL) {
657 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcHitCol");
658 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcHitCol");
661 if (!trackListTemp) {
664 if(!sc.isSuccess()) {
665 log << MSG::FATAL <<
" Could not register RecMdcTrack collection" <<endreq;
672 if(!sc.isSuccess()) {
673 log << MSG::FATAL <<
" Could not register RecMdcHit collection" <<endreq;
678void MdcxCosmicSewer::getInfo(HepVector helix,
double fltLen,
HepPoint3D& pos, Hep3Vector & dir){
679 if(m_lineFit)
return;
680 double d0 = helix[0];
681 double phi0 = helix[1];
682 double omega = helix[2];
683 double z0 = helix[3];
684 double tanDip = helix[4];
685 double cDip = 1./sqrt(1.+tanDip*tanDip);
686 double sDip = tanDip * cDip;
688 double ang = phi00 + cDip*fltLen*omega;
689 double cang =
cos(ang);
690 double sang =
sin(ang);
691 double sphi0 =
sin(phi00);
692 double cphi0 =
cos(phi00);
694 double xt = (sang - sphi0)/omega - d0*sphi0 + referencePoint.x();
695 double yt = -(cang - cphi0)/omega + d0*cphi0 + referencePoint.y();
696 double zt = z0 + fltLen*sDip + referencePoint.z();
701 dir.setX(cang * cDip);
702 dir.setY(sang * cDip);
706int MdcxCosmicSewer::patAmbig(
int bes3FlagLR){
708 if ( bes3FlagLR == 0 ){
710 }
else if( bes3FlagLR == 1){
712 }
else if( bes3FlagLR == 2){
718int MdcxCosmicSewer::bes3FlagLR(
int patAmbig){
720 if ( patAmbig == 1 ){
722 }
else if( patAmbig == -1){
724 }
else if( patAmbig == 0){
double sin(const BesAngle a)
double cos(const BesAngle a)
ObjectVector< MdcHit > MdcHitCol
std::vector< MdcDigi * > MdcDigiVec
float Mdcxprobab(int &ndof, float &chisq)
ObjectVector< RecMdcHit > RecMdcHitCol
ObjectVector< RecMdcTrack > RecMdcTrackCol
SmartRefVector< RecMdcHit > HitRefVec
double bFieldNominal() const
static const double twoPi
const double chi2() const
const int trackId() const
const HepVector helix() const
......
static MdcDetector * instance()
void setCalibSvc(const MdcCalibFunSvc *calibSvc)
void setCosmicFit(const bool cosmicfit)
double sigma(double, int, double, double, double) const
void setCountPropTime(const bool count)
double driftDist(double, int, double, double, double) const
static int layer(const Identifier &id)
Values of different levels (failure returns 0)
static int wire(const Identifier &id)
void printRecMdcTrackCol() const
void storeTrack(int trackId, RecMdcTrackCol *trackList, RecMdcHitCol *hitList, int tkStat)
HepVector besPar2PatPar(const HepVector &helixPar) const
double docaPatPar(int layer, int cell, const HepVector helixPat, const HepSymMatrix errMatPat, bool passCellRequired=true, bool doSag=true) const
void clearRecMdcTrackHit()
virtual ~MdcxCosmicSewer()
void MdcxHitsToHots(HepVector &helix, TrkRecoTrk *trk, HitRefVec &hits, HitRefVec &skiped)
void store(TrkRecoTrk *tk, HitRefVec &skip)
MdcDigiVec & getMdcDigiVec(uint32_t control=0)
virtual Identifier identify() const
const int getFlagLR(void) const
const double getZhit(void) const
void setMdcId(Identifier mdcid)
void setErrDriftDistRight(double erddr)
void setFltLen(double fltLen)
const int getStat(void) const
const double getChisqAdd(void) const
void setErrDriftDistLeft(double erddl)
void setDriftDistLeft(double ddl)
const double getAdc(void) const
const Identifier getMdcId(void) const
void setDoca(double doca)
const double getTdc(void) const
const double getDriftDistRight(void) const
const double getDriftT(void) const
const double getEntra(void) const
const double getDriftDistLeft(void) const
void setChisqAdd(double pChisq)
const double getFltLen(void) const
void setZhit(double zhit)
void setDriftT(double driftT)
const double getDoca(void) const
const double getErrDriftDistRight(void) const
void setDriftDistRight(double ddr)
const double getErrDriftDistLeft(void) const
void setEntra(double entra)
const HitRefVec getVecHits(void) const
const HepVector & params() const
const HepSymMatrix & covariance() const
virtual TrkExchangePar helix(double fltL) const =0
TrkHitOnTrk * appendHot(const TrkHitOnTrk *theHot)
virtual void print(std::ostream &) const
const TrkFit * fitResult() const
bool setFlipAndDrop(TrkRecoTrk &, bool allowFlips, bool allowDrops) const
TrkRecoTrk * makeTrack(const TrkExchangePar &helix, const double chi2, const TrkContext &, double trackT0) const
_EXTERN_ std::string RecMdcTrackCol
_EXTERN_ std::string RecMdcHitCol