1#include "MdcUtilitySvc/MdcUtilitySvc.h"
2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/SmartDataPtr.h"
4#include "GaudiKernel/Bootstrap.h"
5#include "GaudiKernel/ISvcLocator.h"
6#include "EventModel/EventHeader.h"
7#include "MdcGeom/Constants.h"
8#include "MdcGeom/BesAngle.h"
9#include "TrkBase/HelixTraj.h"
10#include "TrkBase/TrkPoca.h"
11#include "MdcGeom/MdcLayer.h"
12#include "GaudiKernel/IDataProviderSvc.h"
14#ifndef ENABLE_BACKWARDS_COMPATIBILITY
23 Service (name, svcloc) {
24 declareProperty(
"debug", m_debug = 0);
31 MsgStream log(messageService(), name());
32 log << MSG::INFO <<
"MdcUtilitySvc::initialize()" << endreq;
34 StatusCode sc = Service::initialize();
35 if( sc.isFailure() ) {
36 log << MSG::ERROR <<
"Service::initialize() failure" << endreq;
41 sc = service(
"MagneticFieldSvc", m_pIMF);
43 log << MSG::FATAL <<
" ERROR Unable to open Magnetic field service "<< endreq;
44 return StatusCode::FAILURE;
48 sc = service(
"MdcGeomSvc", m_mdcGeomSvc);
50 log << MSG::FATAL <<
" Could not load MdcGeomSvc! "<< endreq;
51 return StatusCode::FAILURE;
54 return StatusCode::SUCCESS;
58 MsgStream log(messageService(), name());
59 log << MSG::INFO <<
"MdcUtilitySvc::finalize()" << endreq;
61 return StatusCode::SUCCESS;
66 if( IID_IMdcUtilitySvc.versionMatch(riid) ){
69 return Service::queryInterface(riid, ppvInterface);
73 return StatusCode::SUCCESS;
79 HepVector helixBesParam(5,0);
80 for(
int i=0; i<5; ++i) helixBesParam[i] = helixBes[i];
91 for(
unsigned iLayer=0; iLayer<43; iLayer++){
94 double rMidLayer = m_mdcGeomSvc->
Layer(iLayer)->
Radius();
95 double flightLength = rMidLayer;
99 double c = CLHEP::c_light * 100.;
100 double alpha = 1/(c * Bz());
101 double kappa = helix[2];
102 double rc = (-1.)*
alpha/kappa;
104 double tanl = helix[4];
105 double phi0 = helix[1];
106 double phi = flightLength/rc + phi0;
107 double z = pivot.z() + dz - (
alpha/kappa) * tanl * phi;
109 double layerHalfLength = m_mdcGeomSvc->
Layer(iLayer)->
Length()/2.;
113 if (fabs(z) < fabs(layerHalfLength)) ++nLayer;
121 HepVector helix(5,0);
122 double d0 = -helixPar[0];
123 double phi0 = helixPar[1]+ CLHEP::halfpi;
124 double omega = Bz()*helixPar[2]/-333.567;
125 double z0 = helixPar[3];
126 double tanl = helixPar[4];
142 HepSymMatrix mS(err.num_row(),0);
146 mS[2][2]=Bz()/-333.567;
149 HepSymMatrix mVy= err.similarity(mS);
165 double rInner,rOuter;
167 double rCize1 =0.1* m_mdcGeomSvc->
Layer(layer)->
RCSiz1();
168 double rCize2 =0.1* m_mdcGeomSvc->
Layer(layer)->
RCSiz2();
169 double rLay =0.1* m_mdcGeomSvc->
Layer(layer)->
Radius();
172 rInner = rLay - rCize1 ;
173 rOuter = rLay + rCize2 ;
178 if (innerOrOuter) r = rInner;
181 double d0 = helixPat[0];
182 double phi0 = helixPat[1];
183 double omega = helixPat[2];
184 double z0 = helixPat[3];
185 double tanl = helixPat[4];
190 double xc = piv.x() + ( d0 + rc) *
cos(phi0);
191 double yc = piv.y() + ( d0 + rc) *
sin(phi0);
193 rc = sqrt(xc*xc + yc*yc);
198 double dphi = acos((a*a-b*b-c*c)/(-2.*b*c));
199 double fltlen = dphi * rc;
200 double phi = phi0 * omega * fltlen;
201 double x = piv.x()+d0*
sin(phi) - (rc+d0)*
sin(phi0);
202 double y = piv.y()+d0*
cos(phi) + (rc+d0)*
cos(phi0);
203 double z = piv.z()+ z0 + fltlen*tanl;
206 std::cout<<
" abc "<<a<<
" "<<b<<
" "<<c<<
" omega "<<omega<<
" r "<<r<<
" rc "<<rc<<
" dphi "<<dphi<<
" piv "<<piv.z()
207 <<
" z0 "<<z0<<
" fltlen "<<fltlen<<
" tanl "<<tanl<<std::endl;
208 std::cout<<
" pointOnHelixPatPar in Hel "<<helixPat<<std::endl;
209 cout<<
"HepPoint3D(x, y, z) = "<<
HepPoint3D(
x, y, z)<<endl;
216double MdcUtilitySvc::doca(
int layer,
int cell,
const HepVector helixBes,
const HepSymMatrix errMatBes,
bool passCellRequired,
bool doSag)
const{
220 return docaPatPar(layer, cell, helixPat, errMatPat, passCellRequired, doSag);
229 return docaPatPar(layer, cell, eastP, westP, helixPat, errMatPat, passCellRequired, doSag);
234double MdcUtilitySvc::doca(
int layer,
int cell,
const MdcSWire* sWire,
const HepVector helixBes,
const HepSymMatrix errMatBes,
bool passCellRequired)
const{
238 return docaPatPar(layer, cell, sWire, helixPat, errMatPat, passCellRequired);
243double MdcUtilitySvc::docaPatPar(
int layer,
int cell,
const HepVector helixPat,
const HepSymMatrix errMat,
bool passCellRequired,
bool doSag)
const{
246 int id = geowir->
Id();
248 if(doSag) sag = geowir->
Sag()/10.;
253 double doca =
docaPatPar(layer, cell, sWire, helixPat, errMat, passCellRequired);
264 int id = geowir->
Id();
266 if(doSag) sag = geowir->
Sag()/10.;
269 double doca =
docaPatPar(layer, cell, sWire, helixPat, errMat, passCellRequired);
279 if(m_debug) std::cout<<
" helixPat "<<helixPat<<std::endl;
280 if(m_debug) std::cout<<
" layer "<<layer<<
" cell "<<cell<<std::endl;
289 std::cout<<
" cellId in "<<cellId_in<<
" out "<<cellId_out <<std::endl;
290 cout <<
"cell = " << cell <<
", cellId_in = " << cellId_in <<
", cellId_out = " << cellId_out << endl;
292 if (passCellRequired &&(cell < cellId_in && cell > cellId_out))
return -999.;
299 int innerOrOuter = 1;
301 double fltTrack = 0.1 * m_mdcGeomSvc -> Layer(layer)->Radius();
303 std::cout<<
" cell_IO "<<cell_IO<<std::endl;
304 std::cout<<
" fltTrack "<<fltTrack<<std::endl;
313 std::cout<<
" sag "<<m_wireTraj->
sag()<<std::endl;
314 std::cout<<
" east -------- "<< start_In->x()<<
","<<start_In->y()<<
","<<start_In->z()<<std::endl;
321 double zWire = cell_IO.z();
324 if(m_debug) std::cout<<
" zWire "<<zWire<<
" zEndDC "<<sWire->
zEndDC()<<std::endl;
327 double fltWire = sqrt( (pos_in.x()-start_In->x())*(pos_in.x()-start_In->x()) +
328 (pos_in.y()-start_In->y())*(pos_in.y()-start_In->y()) +
329 (pos_in.z()-start_In->z())*(pos_in.z()-start_In->z()) );
330 trkPoca =
new TrkPoca(*trajHelix, fltTrack, *trajWire, fltWire);
333 double hitLen = trkPoca->
flt2();
334 double startLen = trajWire->
lowRange() - 5.;
335 double endLen = trajWire->
hiRange() + 5.;
336 if(hitLen < startLen || hitLen > endLen) {
337 if(m_debug) std::cout<<
"WARNING MdcUtilitySvc::docaPatPar() isBeyondEndflange! hitLen="<<hitLen <<
" startLen="<<startLen<<
" endLen "<<endLen<<std::endl;
342 if(m_debug) std::cout<<
" doca "<<
doca<<std::endl;
388 int nCell = m_mdcGeomSvc->
Layer(layer)->
NCell();
390 double dPhiz = (m_mdcGeomSvc->
Wire(layer,0)->
Forward().phi() - m_mdcGeomSvc->
Wire(layer,0)->
Backward().phi())*0.5;
391 double rEnd = m_mdcGeomSvc->
Wire(layer,0)->
Backward().rho()/10.;
392 double rMid = rEnd *
cos(dPhiz);
395 double fltLenIn = rMid;
396 double phiIn = helixPat[1] + helixPat[2]*fltLenIn;
398 double phiEPOffset = m_mdcGeomSvc->
Wire(layer,0)->
Backward().phi();
400 if(m_debug) std::cout<<
"cellTrackPassed nCell "<<nCell<<
" layer "<<layer<<
" fltLenIn "<<fltLenIn<<
" phiEPOffset "<<phiEPOffset<<std::endl;
402 int wlow = (int)floor(nCell * tmp.
rad() / twopi );
403 int wbig = (int)ceil(nCell * tmp.
rad() / twopi );
404 if (tmp == 0 ){ wlow = -1; wbig = 1; }
406 if ((wlow%nCell)< 0) wlow += nCell;
409 if ((wbig%nCell)< 0) wbig += nCell;
412 bool passedOneCell = (cellId_in == cellId_out);
414 return passedOneCell;
426 int charge,type,nCell;
427 double dr0,phi0,kappa,dz0,tanl;
428 double ALPHA_loc,rho,phi,cosphi0,sinphi0,x_hc,y_hc,z_hc;
429 double dphi0,IO_phi,C_alpha,xx,yy;
430 double inlow,inup,outlow,outup,phi_in,phi_out,phi_bin;
431 double rCize1,rCize2,rLay,length,phioffset,slant,shift;
432 double m_crio[2], phi_io[2], stphi[2],phioff[2],dphi[2];
440 ALPHA_loc = 1000/(2.99792458*Bz());
441 charge = ( kappa >=0 ) ? 1 : -1 ;
442 rho = ALPHA_loc/kappa ;
444 phi = fmod(phi0 + 4*
pi , 2*
pi);
446 sinphi0 = (1.0 - cosphi0 ) * (1.0 + cosphi0 );
447 sinphi0 = sqrt(( sinphi0 > 0.) ? sinphi0 : 0.);
448 if( phi >
pi ) sinphi0 = -sinphi0 ;
450 x_hc = 0. + ( dr0 + rho ) * cosphi0;
451 y_hc = 0. + ( dr0 + rho ) * sinphi0;
458 double m_c_perp(hcenter.perp());
459 Hep3Vector m_c_unit((
HepPoint3D)hcenter.unit());
462 dphi0 = fmod(IO.phi()+4*
pi, 2*
pi) - phi;
463 IO_phi = fmod(IO.phi()+4*
pi, 2*
pi);
465 if(dphi0 >
pi) dphi0 -= 2*
pi;
466 else if(dphi0 < -
pi) dphi0 += 2*
pi;
468 phi_io[0] = -(1+charge)*
pi/2 - charge*dphi0;
469 phi_io[1] = phi_io[0]+1.5*
pi;
472 bool outFlag =
false;
486 m_crio[0] = rLay - rCize1 ;
487 m_crio[1] = rLay + rCize2 ;
491 Hep3Vector iocand[2];
492 Hep3Vector cell_IO[2];
494 for(
int ii =0; ii<2; ii++){
496 double cos_alpha = (m_c_perp*m_c_perp + m_crio[ii]*m_crio[ii] - rho*rho)
497 / ( 2 * m_c_perp * m_crio[ii] );
498 if(fabs(cos_alpha)>1&&(ii==0)){
502 if(fabs(cos_alpha)>1&&(ii==1)){
503 cos_alpha = (m_c_perp*m_c_perp + m_crio[0]*m_crio[0] - rho*rho)
504 / ( 2 * m_c_perp * m_crio[0] );
505 C_alpha = 2*
pi - acos( cos_alpha);
507 C_alpha = acos( cos_alpha );
510 iocand[ii] = m_c_unit;
511 iocand[ii].rotateZ( charge*sign*C_alpha );
512 iocand[ii] *= m_crio[ii];
514 xx = iocand[ii].x() - x_hc ;
515 yy = iocand[ii].y() - y_hc ;
517 dphi[ii] = atan2(yy,xx) - phi0 - 0.5*
pi*(1-charge);
518 dphi[ii] = fmod( dphi[ii] + 8.0*
pi,2*
pi);
521 if( dphi[ii] < phi_io[0] ) {
523 }
else if( dphi[ii] > phi_io[1] ){
527 cell_IO[ii] =
Hel(piv, dr0, phi, ALPHA_loc, kappa,dz0, dphi[ii], tanl);
530 if( (cell_IO[ii].
x()==0 ) && (cell_IO[ii].y()==0) && (cell_IO[ii].z()==0))
continue;
534 cellId_in = cellId_out = -1 ;
535 phi_in = cell_IO[0].phi();
536 phi_in = fmod ( phi_in + 4 *
pi, 2 *
pi );
537 phi_out = cell_IO[1].phi();
538 phi_out = fmod ( phi_out + 4 *
pi, 2 *
pi );
539 phi_bin = 2.0 *
pi / nCell ;
542 stphi[0] = shift * phi_bin * (0.5 - cell_IO[0].z()/length);
543 stphi[1] = shift * phi_bin * (0.5 - cell_IO[1].z()/length);
546 phioff[0] = phioffset + stphi[0];
547 phioff[1] = phioffset + stphi[1];
549 for(
int kk = 0; kk<nCell ; kk++){
551 inlow = phioff[0] + phi_bin*kk - phi_bin*0.5;
552 inlow = fmod( inlow + 4.0 *
pi , 2.0 *
pi);
553 inup = phioff[0] + phi_bin*kk + phi_bin*0.5;
554 inup = fmod( inup + 4.0 *
pi , 2.0 *
pi);
555 outlow = phioff[1] + phi_bin*kk - phi_bin*0.5;
556 outlow = fmod( outlow + 4.0 *
pi ,2.0 *
pi);
557 outup = phioff[1] + phi_bin*kk + phi_bin*0.5;
558 outup = fmod( outup + 4.0 *
pi , 2.0 *
pi);
560 if((phi_in>=inlow && phi_in<=inup)) cellId_in = kk;
561 if((phi_out>=outlow&&phi_out<outup)) cellId_out = kk;
563 if((phi_in>=inlow&&phi_in<2.0*
pi)||(phi_in>=0.0&&phi_in<inup)) cellId_in = kk;
566 if((phi_out>=outlow&&phi_out<=2.0*
pi)||(phi_out>=0.0&&phi_out<outup)) cellId_out = kk;
570 return (cellId_in==cellId_out);
574 double x = piv.x() + dr*
cos(phi0) + (Alpha_L/kappa) * (
cos(phi0) -
cos(phi0+dphi));
575 double y = piv.y() + dr*
sin(phi0) + (Alpha_L/kappa) * (
sin(phi0) -
sin(phi0+dphi));
576 double z = piv.z() + dz - (Alpha_L/kappa) * dphi * tanl;
578 if((
x>-1000. &&
x<1000.) || (y >-1000. && y <1000. ) ||(z>-1000. && z<1000.)){
588 HepVector helix(5,0);
589 double d0 = -helixPar[0];
590 double phi0 = helixPar[1]+ CLHEP::halfpi;
591 double omega = Bz()*helixPar[2]/-333.567;
592 double z0 = helixPar[3];
593 double tanl = helixPar[4];
608 HepSymMatrix mS(err.num_row(),0);
611 mS[2][2]=Bz()/-333.567;
614 HepSymMatrix mVy= err.similarity(mS);
621 p4.setPx(-
sin(helix[1]) / fabs(helix[2]));
622 p4.setPy(
cos(helix[1]) / fabs(helix[2]));
623 p4.setPz(helix[4] / fabs(helix[2]));
624 double p3 = p4.mag();
626 p4.setE(sqrt(p3 * p3 +
mass *
mass));
634 double zboost = 0.0075;
636 HepLorentzVector psip(0.011 * ecm, 0, zboost, ecm);
638 Hep3Vector boostv = psip.boostVector();
644 double p_cms = p4.rho();
653 double fi0 = trk->
helix(1);
654 double cpa = trk->
helix(2);
655 double tanl = trk->
helix(4);
658 if(cpa != 0) pxy = 1/fabs(cpa);
660 double px = pxy * (-
sin(fi0));
661 double py = pxy *
cos(fi0);
662 double pz = pxy * tanl;
672 double srtopi=2.0/sqrt(2.0*
M_PI);
676 if(ndof<=0) {
return prob;}
677 if(chisq<0.0) {
return prob;}
680 if(chisq>upl) {
return prob;}
681 double sum=
exp(-0.5*chisq);
686 if(m==1){
return sum;}
687 for(
int i=2; i<=m;i++){
688 term=0.5*term*chisq/(i-1);
696 double srty=sqrt(chisq);
697 double temp=srty/M_SQRT2;
699 if(ndof==1) {
return prob;}
700 if(ndof==3) {
return (srtopi*srty*sum+prob);}
702 for(
int i=1; i<=m; i++){
703 term=term*chisq/(2*i+1);
706 return (srtopi*srty*sum+prob);
712 double srty=sqrt(chisq)-sqrt(ndof-0.5);
713 if(srty<12.0) {prob=0.5*erfc(srty);};
EvtComplex exp(const EvtComplex &c)
double abs(const EvtComplex &c)
HepGeom::Point3D< double > HepPoint3D
double sin(const BesAngle a)
double cos(const BesAngle a)
HepGeom::Point3D< double > HepPoint3D
static const double epsilon
const HepVector helix() const
......
virtual const MdcGeoLayer *const Layer(unsigned id)=0
virtual const MdcGeoWire *const Wire(unsigned id)=0
double Radius(void) const
double RCSiz2(void) const
double Length(void) const
MdcGeoSuper * Sup(void) const
double RCSiz1(void) const
double Offset(void) const
const double Sag(void) const
HepPoint3D Forward(void) const
HepPoint3D Backward(void) const
double yWireDC(double z) const
double xWireDC(double z) const
double zEndDC(void) const
const MdcSagTraj * getTraj(void) const
const HepPoint3D * getEastPoint(void) const
MdcUtilitySvc(const std::string &name, ISvcLocator *svcloc)
HepVector besPar2PatPar(const HepVector &helixPar) const
bool cellTrackPassedByPhi(const HepVector helix, int layer, int &cellId_in, int &cellId_out) const
virtual StatusCode initialize()
HepSymMatrix patErr2BesErr(const HepSymMatrix &err) const
HepPoint3D Hel(HepPoint3D piv, double dr, double phi0, double Alpha_L, double kappa, double dz, double dphi, double tanl) const
HepSymMatrix besErr2PatErr(const HepSymMatrix &err) const
bool cellTrackPassedByPhiPatPar(const HepVector helix, int layer, int &cellId_in, int &cellId_out) const
HepVector patPar2BesPar(const HepVector &helixPar) const
StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
virtual StatusCode finalize()
double probab(const int &ndof, const double &chisq) const
Hep3Vector momentum(const RecMdcTrack *trk) const
double doca(int layer, int cell, const HepVector helix, const HepSymMatrix errMat, bool passCellRequired=true, bool doSag=true) const
int nLayerTrackPassed(const HepVector helix) const
bool cellTrackPassed(const HepVector helix, int layer, int &cellId_in, int &cellId_out) const
double p_cms(HepVector helix, int runNo, double mass) const
double docaPatPar(int layer, int cell, const HepVector helixPat, const HepSymMatrix errMatPat, bool passCellRequired=true, bool doSag=true) const
HepPoint3D pointOnHelixPatPar(const HepVector helixPat, int lay, int innerOrOuter) const
HepPoint3D pointOnHelix(const HepVector helixPar, int lay, int innerOrOuter) const
bool cellTrackPassedPatPar(const HepVector helix, int layer, int &cellId_in, int &cellId_out) const