26#include "MdcxReco/MdcxCosmicSewer.h"
31#include "GaudiKernel/MsgStream.h"
32#include "GaudiKernel/AlgFactory.h"
33#include "EventModel/EventHeader.h"
34#include "EvTimeEvent/RecEsTime.h"
35#include "Identifier/MdcID.h"
36#include "Identifier/Identifier.h"
37#include "GaudiKernel/SmartDataPtr.h"
38#include "MdcCalibFunSvc/IMdcCalibFunSvc.h"
39#include "RawDataProviderSvc/IRawDataProviderSvc.h"
40#include "RawDataProviderSvc/MdcRawDataProvider.h"
41#include "MdcRawEvent/MdcDigi.h"
42#include "TrkBase/TrkFit.h"
43#include "TrkFitter/TrkHelixMaker.h"
44#include "TrkFitter/TrkLineMaker.h"
45#include "MdcxReco/Mdcxprobab.h"
46#include "MdcData/MdcHit.h"
47#include "MdcData/MdcRecoHitOnTrack.h"
48#include "MdcTrkRecon/MdcTrack.h"
49#include "TrkBase/TrkHitList.h"
50#include "MdcPrintSvc/IMdcPrintSvc.h"
51#include "TrkBase/TrkExchangePar.h"
52#include "MdcGeom/EntranceAngle.h"
59 Algorithm(name, pSvcLocator)
62 declareProperty(
"debug", m_debug =
false);
63 declareProperty(
"hist", m_hist =
false);
65 declareProperty(
"doSag", m_doSag =
false);
67 declareProperty(
"cosmicSewPar", m_cosmicSewPar);
68 declareProperty(
"cosmicSewSkip", m_cosmicSewSkip=
false);
70 declareProperty(
"countPropTime", m_countPropTime =
true);
71 declareProperty(
"lineFit", m_lineFit =
false);
85 if(NULL == m_gm)
return StatusCode::FAILURE;
87 return StatusCode::SUCCESS;
94 MsgStream log(
msgSvc(), name());
96 log << MSG::INFO <<
"in initialize()" << endreq;
102 sc = service (
"MagneticFieldSvc",m_pIMF);
103 if(sc!=StatusCode::SUCCESS) {
104 log << MSG::ERROR <<
"Unable to open Magnetic field service"<<endreq;
106 m_bfield =
new BField(m_pIMF);
107 log << MSG::INFO <<
"field z = "<<m_bfield->
bFieldNominal()<< endreq;
113 sc = service (
"MdcCalibFunSvc", imdcCalibSvc);
115 if ( sc.isFailure() ){
116 log << MSG::FATAL <<
"Could not load MdcCalibFunSvc!" << endreq;
117 return StatusCode::FAILURE;
122 sc = service (
"RawDataProviderSvc", iRawDataProvider);
123 if ( sc.isFailure() ){
124 log << MSG::FATAL <<
"Could not load RawDataProviderSvc!" << endreq;
125 return StatusCode::FAILURE;
131 sc = service (
"MdcUtilitySvc", imdcUtilitySvc);
132 m_mdcUtilitySvc =
dynamic_cast<MdcUtilitySvc*
> (imdcUtilitySvc);
133 if ( sc.isFailure() ){
134 log << MSG::FATAL <<
"Could not load MdcUtilitySvc!" << endreq;
135 return StatusCode::FAILURE;
140 sc = service (
"MdcPrintSvc", imdcPrintSvc);
141 m_mdcPrintSvc =
dynamic_cast<MdcPrintSvc*
> (imdcPrintSvc);
142 if ( sc.isFailure() ){
143 log << MSG::FATAL <<
"Could not load MdcPrintSvc!" << endreq;
144 return StatusCode::FAILURE;
149 NTuplePtr ntCsmcSew(
ntupleSvc(),
"MdcxReco/csmc");
150 if ( ntCsmcSew ) { m_xtupleCsmcSew = ntCsmcSew;}
152 m_xtupleCsmcSew =
ntupleSvc()->book (
"MdcxReco/csmc", CLID_ColumnWiseTuple,
"MdcxReco reconsturction results");
153 if ( m_xtupleCsmcSew ) {
154 sc = m_xtupleCsmcSew->addItem (
"dD0", m_csmcD0);
155 sc = m_xtupleCsmcSew->addItem (
"dPhi0", m_csmcPhi0);
156 sc = m_xtupleCsmcSew->addItem (
"dZ0", m_csmcZ0);
157 sc = m_xtupleCsmcSew->addItem (
"dOmega", m_csmcOmega);
158 sc = m_xtupleCsmcSew->addItem (
"dPt", m_csmcPt);
159 sc = m_xtupleCsmcSew->addItem (
"dTanl", m_csmcTanl);
161 log << MSG::FATAL <<
"Could not book MdcxReco/csmc!" << endreq;
162 return StatusCode::FAILURE;
167 return StatusCode::SUCCESS;
171 MsgStream log(
msgSvc(), name());
172 log << MSG::INFO <<
"in execute()" << endreq;
175 setFilterPassed(
false);
177 SmartDataPtr<Event::EventHeader> evtHead(eventSvc(),
"/Event/EventHeader");
179 log << MSG::FATAL<<
"Could not retrieve event header" << endreq;
180 return StatusCode::FAILURE;
182 long t_runNo = evtHead->runNumber();
183 long t_evtNo = evtHead->eventNumber();
184 if (m_debug) std::cout <<
"sew "<<++i_evt<<
" run "<<t_runNo<<
" evt " << t_evtNo << std::endl;
189 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc(),
"/Event/Recon/RecEsTimeCol");
190 if (!aevtimeCol || aevtimeCol->size()==0) {
191 log << MSG::WARNING<<
"Could not find RecEsTimeCol"<< endreq;
192 return StatusCode::SUCCESS;
194 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
195 for(; iter_evt!=aevtimeCol->end(); iter_evt++){
196 m_bunchT0 = (*iter_evt)->getTest();
202 if (!recMdcTrackCol || ! recMdcHitCol)
return StatusCode::SUCCESS;
203 if (2!=recMdcTrackCol->size()){
205 return StatusCode::SUCCESS;
207 if(m_debug)std::cout<<name()<<
" nTk==2 begin sewing"<< std::endl;
211 double dParam[5]={0.,0.,0.,0.,0.};
214 RecMdcTrackCol::iterator besthel;
216 int besthelId = -999;
217 RecMdcTrackCol::iterator it = recMdcTrackCol->begin();
218 for (
int iTk=0; it != recMdcTrackCol->end(); it++,iTk++) {
221 double d0 = -tk->
helix(0);
222 double phi0 = tk->
helix(1)+ CLHEP::halfpi;
223 double omega = Bz*tk->
helix(2)/-333.567;
224 double z0 = tk->
helix(3);
225 double tanl = tk->
helix(4);
230 if(m_debug)std::cout<<__FILE__<<
" "<<__LINE__<<
"tk"<<iTk
231 <<
"("<<d0<<
","<<phi0<<
"," <<
","<<omega<<
","<<z0<<
","<<tanl<<
")"<<std::endl;
251 float bchisq = tk->
chi2();
252 int bndof = tk->
ndof();
261 if(besthelId == -999)
return StatusCode::SUCCESS;
268 std::cout<<__FILE__<<
" param diff: " <<
"\n D0 " << dParam[0]
269 <<
"\n Phi0 " << dParam[1] <<
"\n Omega " << dParam[2]
270 <<
"\n Z0 " << dParam[3] <<
"\n Tanl " << dParam[4] << std::endl;
275 m_csmcPhi0=dParam[1];
276 m_csmcOmega=dParam[2];
278 m_csmcTanl=dParam[4];
279 m_xtupleCsmcSew->write();
283 if( (fabs(dParam[0])>m_cosmicSewPar[0]) ||
284 (fabs(dParam[1])>m_cosmicSewPar[1]) ||
285 (fabs(dParam[2])>m_cosmicSewPar[2]) ||
286 (fabs(dParam[3])>m_cosmicSewPar[3]) ||
287 (fabs(dParam[4])>m_cosmicSewPar[4]) ){
288 if(m_debug)std::cout<<__FILE__<<
" 2 trk param not satisfied skip!"<<std::endl;
290 if(fabs(dParam[0])>m_cosmicSewPar[0]) std::cout<<
" cut by d0 "<<std::endl;
291 if(fabs(dParam[1])>m_cosmicSewPar[1]) std::cout<<
" cut by phi0 "<<std::endl;
292 if(fabs(dParam[2])>m_cosmicSewPar[2]) std::cout<<
" cut by omega "<<std::endl;
293 if(fabs(dParam[3])>m_cosmicSewPar[3]) std::cout<<
" cut by z0"<<std::endl;
294 if(fabs(dParam[4])>m_cosmicSewPar[4]) std::cout<<
" cut by tanl"<<std::endl;
297 return StatusCode::SUCCESS;
303 double d0 = -tk->
helix(0);
304 double phi0 = tk->
helix(1)+ CLHEP::halfpi;
305 double omega = Bz*tk->
helix(2)/-333.567;
306 double z0 = tk->
helix(3);
307 double tanl = tk->
helix(4);
310 if(m_debug)std::cout<<__FILE__<<
" best helix: No."<<tk->
trackId()<<
" Pat param=("<<d0<<
" "<<phi0
311 <<
" "<<omega<<
" "<<z0<<
" "<<tanl<<
")"<< std::endl;
317 newTrack = linefactory.
makeTrack(tt, chisum, *m_context, m_bunchT0*1.e-9);
320 newTrack = helixfactory.
makeTrack(tt, chisum, *m_context, m_bunchT0*1.e-9);
327 it = recMdcTrackCol->begin();
328 for (
int iTk=0; it != recMdcTrackCol->end(); it++,iTk++) {
345 if(m_debug)std::cout<<__FILE__<<
" sew fit failed!!!"<< std::endl;
346 HitRefVec::iterator it= skippedHits.begin();
347 for (;it!=skippedHits.end();++it){
delete it->data();}
360 std::cout<<__FILE__<<
" sew cosmic fit good "<< std::endl;
361 newTrack->
print(std::cout);
364 SmartDataPtr<MdcHitCol> m_hitCol(eventSvc(),
"/Event/Hit/MdcHitCol");
367 StatusCode sc = eventSvc()->registerObject(
"/Event/Hit/MdcHitCol",m_hitCol);
368 if(!sc.isSuccess()) {
369 std::cout<<
" Could not register MdcHitCol" <<endreq;
370 return StatusCode::FAILURE;
373 uint32_t getDigiFlag = 0;
375 const MdcDigi* m_digiMap[43][288];
376 MdcDigiVec::iterator
iter = mdcDigiVec.begin();
377 for (;
iter != mdcDigiVec.end();
iter++ ) {
382 m_digiMap[layer][wire] = aDigi;
386 HitRefVec::iterator itHitSkipped = skippedHits.begin();
387 for (;itHitSkipped!=skippedHits.end();++itHitSkipped){
397 double doca = m_mdcUtilitySvc->
docaPatPar(layer,wire,helix,err);
398 double newDoca = fabs(doca);
400 if ( flagLR == 0 ){ newDoca = -fabs(doca); }
402 if(m_debug>=3)std::cout<<
"("<<layer<<
","<<wire<<
") new doca "<<newDoca<<
" resid "<<fabs(fabs(newDoca)-fabs(h->
getDriftDistLeft()))<<std::endl;
406 MdcHit *thehit =
new MdcHit(m_digiMap[layer][wire], m_gm);
410 m_hitCol->push_back(thehit);
413 getInfo(helix,0,poca,tempDir);
414 if(m_debug>3)std::cout<<
"track poca "<<poca<<std::endl;
416 getInfo(helix,docaFltLen,pos,dir);
418 if(pos.y()>poca.y()){
419 int wireAmb = patAmbig(flagLR);
421 double tof = docaFltLen/
Constants::c + (m_bunchT0)*1.e-9;
427 double dist = thehit->
driftDist(tof, wireAmb, eAngle, dAngle, z);
428 double sigma = thehit->
sigma(dist,wireAmb,eAngle,dAngle,z);
430 if(m_debug>3&&fabs(fabs(h->
getDoca())-fabs(dist))>0.02)
432 <<
" new dd "<<dist <<
" newAmbig "<<wireAmb
435 <<
" new sigma "<<sigma<<
" "<<std::endl;
446 store(newTrack, skippedHits);
448 setFilterPassed(
true);
454 return StatusCode::SUCCESS;
458 MsgStream log(
msgSvc(), name());
459 log << MSG::INFO <<
"in finalize()" << endreq;
460 std::cout<<name()<<
" after sewed got "<<m_nSewed<<
" cosmic tracks"<<std::endl;
461 return StatusCode::SUCCESS;
468 SmartDataPtr<MdcHitCol> m_hitCol(eventSvc(),
"/Event/Hit/MdcHitCol");
471 StatusCode sc = eventSvc()->registerObject(
"/Event/Hit/MdcHitCol",m_hitCol);
472 if(!sc.isSuccess()) {
473 std::cout<<
" Could not register MdcHitCol" <<endreq;
479 uint32_t getDigiFlag = 0;
481 if (0 == mdcDigiVec.size()){
482 std::cout <<
" No hits in MdcDigiVec" << std::endl;
485 const MdcDigi* m_digiMap[43][288];
486 MdcDigiVec::iterator
iter = mdcDigiVec.begin();
487 for (;
iter != mdcDigiVec.end();
iter++ ) {
492 m_digiMap[layer][wire] = aDigi;
497 getInfo(helix,0,poca,tempDir);
498 if(m_debug>3)std::cout<<
"track poca "<<poca<<std::endl;
500 HitRefVec::iterator itHit = recMdcHits.begin();
501 for (;itHit!=recMdcHits.end();++itHit){
510 if(pos.y()>poca.y()){
513 if(m_debug>3) std::cout<<
" Up track, change sign of fltLen "<<std::endl;
515 int newFlagLR = bes3FlagLR(newAmbig);
517 if(m_cosmicSewSkip && layer<20){
535 skippedHits.push_back(newSkippedHit);
537 MdcHit *thehit =
new MdcHit(m_digiMap[layer][wire], m_gm);
541 m_hitCol->push_back(thehit);
546 if(m_debug>3) std::cout<<name()<<
" ("<<layer<<
","<<wire<<
") fltLen "<<h->
getFltLen()<<
" newFltLen "<<newFltLen<<
" newAmbig "<<newAmbig<<
" pos.y() "<<pos.y()<<std::endl;
555 MsgStream log(
msgSvc(), name());
557 assert (aTrack != NULL);
560 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
561 DataObject *aTrackCol;
562 eventSvc()->findObject(
"/Event/Recon/RecMdcTrackCol",aTrackCol);
563 if(aTrackCol != NULL) {
564 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcTrackCol");
565 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcTrackCol");
568 DataObject *aRecHitCol;
569 eventSvc()->findObject(
"/Event/Recon/RecMdcHitCol",aRecHitCol);
570 if(aRecHitCol != NULL) {
571 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcHitCol");
572 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcHitCol");
579 if(!sc.isSuccess()) {
580 log << MSG::FATAL <<
" Could not register RecMdcTrack collection" <<endreq;
588 if(!sc.isSuccess()) {
589 log << MSG::FATAL <<
" Could not register RecMdcHit collection" <<endreq;
592 int trackId = trackList->size() - 1;
594 if(m_debug)std::cout<<__FILE__<<
" "<<__LINE__<<
" STORED"<< std::endl;
599 mdcTrack.
storeTrack(trackId, trackList, hitList, tkStat);
602 RecMdcTrackCol::iterator it = trackList->begin();
605 HitRefVec::iterator itHit = hl.begin();
607 for (;itHit!=hl.end();++itHit){
610 SmartRef<RecMdcHit> refHit(h);
611 hitRefVec.push_back(refHit);
614 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
"refHit stored "<< ihl<< std::endl;
616 HitRefVec::iterator itHitSkipped = skippedHits.begin();
618 for (;itHitSkipped!=skippedHits.end();++itHitSkipped){
620 (*itHitSkipped)->setTrkId(trackId);
621 hitList->push_back(*itHitSkipped);
622 SmartRef<RecMdcHit> refHitSkipped(*itHitSkipped);
623 hitRefVec.push_back(refHitSkipped);
625 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
"skippedHits stored "<< iskipped<< std::endl;
626 (*it)->setVecHits(hitRefVec);
628 (*it)->setNhits((*it)->getVecHits().size());
632 if((*it)->helix(2)<0.00001)(*it)->setNdof((*it)->getNhits()-4);
633 else (*it)->setNdof((*it)->getNhits()-5);
635 if(m_debug) std::cout<<__FILE__<<
" "<<__LINE__<<
" stored "<< (*it)->getVecHits().size()<< std::endl;
640 MsgStream log(
msgSvc(), name());
646 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
647 DataObject *aTrackCol;
648 eventSvc()->findObject(
"/Event/Recon/RecMdcTrackCol",aTrackCol);
649 if(aTrackCol != NULL) {
650 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcTrackCol");
651 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcTrackCol");
653 DataObject *aRecHitCol;
654 eventSvc()->findObject(
"/Event/Recon/RecMdcHitCol",aRecHitCol);
655 if(aRecHitCol != NULL) {
656 dataManSvc->clearSubTree(
"/Event/Recon/RecMdcHitCol");
657 eventSvc()->unregisterObject(
"/Event/Recon/RecMdcHitCol");
660 if (!trackListTemp) {
663 if(!sc.isSuccess()) {
664 log << MSG::FATAL <<
" Could not register RecMdcTrack collection" <<endreq;
671 if(!sc.isSuccess()) {
672 log << MSG::FATAL <<
" Could not register RecMdcHit collection" <<endreq;
677void MdcxCosmicSewer::getInfo(HepVector helix,
double fltLen,
HepPoint3D& pos, Hep3Vector & dir){
678 if(m_lineFit)
return;
679 double d0 = helix[0];
680 double phi0 = helix[1];
681 double omega = helix[2];
682 double z0 = helix[3];
683 double tanDip = helix[4];
684 double cDip = 1./sqrt(1.+tanDip*tanDip);
685 double sDip = tanDip * cDip;
687 double ang = phi00 + cDip*fltLen*omega;
688 double cang =
cos(ang);
689 double sang =
sin(ang);
690 double sphi0 =
sin(phi00);
691 double cphi0 =
cos(phi00);
693 double xt = (sang - sphi0)/omega - d0*sphi0 + referencePoint.x();
694 double yt = -(cang - cphi0)/omega + d0*cphi0 + referencePoint.y();
695 double zt = z0 + fltLen*sDip + referencePoint.z();
700 dir.setX(cang * cDip);
701 dir.setY(sang * cDip);
705int MdcxCosmicSewer::patAmbig(
int bes3FlagLR){
707 if ( bes3FlagLR == 0 ){
709 }
else if( bes3FlagLR == 1){
711 }
else if( bes3FlagLR == 2){
717int MdcxCosmicSewer::bes3FlagLR(
int patAmbig){
719 if ( patAmbig == 1 ){
721 }
else if( patAmbig == -1){
723 }
else if( patAmbig == 0){
std::vector< MdcDigi * > MdcDigiVec
double abs(const EvtComplex &c)
ObjectVector< MdcHit > MdcHitCol
double sin(const BesAngle a)
double cos(const BesAngle a)
ObjectVector< RecMdcHit > RecMdcHitCol
ObjectVector< RecMdcTrack > RecMdcTrackCol
SmartRefVector< RecMdcHit > HitRefVec
float Mdcxprobab(int &ndof, float &chisq)
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()
MdcxCosmicSewer(const std::string &name, ISvcLocator *pSvcLocator)
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