00001 #include "RecoEgamma/EgammaPhotonAlgos/interface/TangentApproachInRPhi.h"
00002 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00003 #include "MagneticField/Engine/interface/MagneticField.h"
00004 #include "FWCore/Utilities/interface/Exception.h"
00005
00006 using namespace std;
00007
00008 bool TangentApproachInRPhi::calculate(const TrajectoryStateOnSurface & sta,
00009 const TrajectoryStateOnSurface & stb)
00010 {
00011 TrackCharge chargeA = sta.charge(); TrackCharge chargeB = stb.charge();
00012 GlobalVector momentumA = sta.globalMomentum();
00013 GlobalVector momentumB = stb.globalMomentum();
00014 GlobalPoint positionA = sta.globalPosition();
00015 GlobalPoint positionB = stb.globalPosition();
00016 paramA = sta.globalParameters();
00017 paramB = stb.globalParameters();
00018
00019 return calculate(chargeA, momentumA, positionA, chargeB, momentumB, positionB,
00020 sta.freeState()->parameters().magneticField());
00021 }
00022
00023
00024 bool TangentApproachInRPhi::calculate(const FreeTrajectoryState & sta,
00025 const FreeTrajectoryState & stb)
00026 {
00027 TrackCharge chargeA = sta.charge(); TrackCharge chargeB = stb.charge();
00028 GlobalVector momentumA = sta.momentum();
00029 GlobalVector momentumB = stb.momentum();
00030 GlobalPoint positionA = sta.position();
00031 GlobalPoint positionB = stb.position();
00032 paramA = sta.parameters();
00033 paramB = stb.parameters();
00034
00035 return calculate(chargeA, momentumA, positionA, chargeB, momentumB, positionB,
00036 sta.parameters().magneticField());
00037 }
00038
00039 pair<GlobalPoint, GlobalPoint> TangentApproachInRPhi::points() const
00040 {
00041 if (!status_)
00042 throw cms::Exception("TrackingTools/PatternTools","TangentApproachInRPhi::could not compute track crossing. Check status before calling this method!");
00043 return pair<GlobalPoint, GlobalPoint> (posA, posB);
00044 }
00045
00046
00047 GlobalPoint
00048 TangentApproachInRPhi::crossingPoint() const
00049 {
00050 if (!status_)
00051 throw cms::Exception("TrackingTools/PatternTools","TangentApproachInRPhi::could not compute track crossing. Check status before calling this method!");
00052 return GlobalPoint((posA.x() + posB.x())/2.,
00053 (posA.y() + posB.y())/2.,
00054 (posA.z() + posB.z())/2.);
00055 }
00056
00057
00058 float TangentApproachInRPhi::distance() const
00059 {
00060 if (!status_)
00061 throw cms::Exception("TrackingTools/PatternTools","TangentApproachInRPhi::could not compute track crossing. Check status before calling this method!");
00062 return (posB - posA).mag();
00063 }
00064
00065 float TangentApproachInRPhi::perpdist() const
00066 {
00067 if (!status_)
00068 throw cms::Exception("TrackingTools/PatternTools","TangentApproachInRPhi::could not compute track crossing. Check status before calling this method!");
00069
00070 float perpdist = (posB - posA).perp();
00071
00072 if (intersection_) {
00073 perpdist = -perpdist;
00074 }
00075
00076 return perpdist;
00077
00078 }
00079
00080
00081 bool TangentApproachInRPhi::calculate(const TrackCharge & chargeA,
00082 const GlobalVector & momentumA,
00083 const GlobalPoint & positionA,
00084 const TrackCharge & chargeB,
00085 const GlobalVector & momentumB,
00086 const GlobalPoint & positionB,
00087 const MagneticField& magField)
00088 {
00089
00090
00091 double xca, yca, ra;
00092 circleParameters(chargeA, momentumA, positionA, xca, yca, ra, magField);
00093 double xcb, ycb, rb;
00094 circleParameters(chargeB, momentumB, positionB, xcb, ycb, rb, magField);
00095
00096
00097 double xg1, yg1, xg2, yg2;
00098 int flag = transverseCoord(xca, yca, ra, xcb, ycb, rb, xg1, yg1, xg2, yg2);
00099 if (flag == 0) {
00100 status_ = false;
00101 return false;
00102 }
00103
00104 double xga, yga, zga, xgb, ygb, zgb;
00105
00106 if (flag == 1) {
00107 intersection_ = true;
00108 }
00109 else {
00110 intersection_ = false;
00111 }
00112
00113
00114 xga = xg1; yga = yg1;
00115 zga = zCoord(momentumA, positionA, ra, xca, yca, xga, yga);
00116 xgb = xg2; ygb = yg2;
00117 zgb = zCoord(momentumB, positionB, rb, xcb, ycb, xgb, ygb);
00118
00119
00120 posA = GlobalPoint(xga, yga, zga);
00121 posB = GlobalPoint(xgb, ygb, zgb);
00122 status_ = true;
00123 return true;
00124 }
00125
00126 pair <GlobalTrajectoryParameters, GlobalTrajectoryParameters>
00127 TangentApproachInRPhi::trajectoryParameters () const
00128 {
00129 if (!status_)
00130 throw cms::Exception("TrackingTools/PatternTools","TangentApproachInRPhi::could not compute track crossing. Check status before calling this method!");
00131 pair <GlobalTrajectoryParameters, GlobalTrajectoryParameters>
00132 ret ( trajectoryParameters ( posA, paramA ),
00133 trajectoryParameters ( posB, paramB ) );
00134 return ret;
00135 }
00136
00137 GlobalTrajectoryParameters TangentApproachInRPhi::trajectoryParameters (
00138 const GlobalPoint & newpt, const GlobalTrajectoryParameters & oldgtp ) const
00139 {
00140
00141 double xc, yc, r;
00142 circleParameters( oldgtp.charge(), oldgtp.momentum(),
00143 oldgtp.position(), xc, yc, r, oldgtp.magneticField() );
00144
00145
00146 double dx1 = oldgtp.position().x() - xc;
00147 double dy1 = oldgtp.position().y() - yc;
00148 double dx2 = newpt.x() - xc;
00149 double dy2 = newpt.y() - yc;
00150
00151
00152 double cosphi = ( dx1 * dx2 + dy1 * dy2 ) /
00153 ( sqrt ( dx1 * dx1 + dy1 * dy1 ) * sqrt ( dx2 * dx2 + dy2 * dy2 ));
00154 double sinphi = - oldgtp.charge() * sqrt ( 1 - cosphi * cosphi );
00155
00156
00157 double px = cosphi * oldgtp.momentum().x() - sinphi * oldgtp.momentum().y();
00158 double py = sinphi * oldgtp.momentum().x() + cosphi * oldgtp.momentum().y();
00159
00160 GlobalVector vta ( px, py, oldgtp.momentum().z() );
00161 GlobalTrajectoryParameters gta( newpt , vta , oldgtp.charge(), &(oldgtp.magneticField()) );
00162 return gta;
00163 }
00164
00165 void
00166 TangentApproachInRPhi::circleParameters(const TrackCharge& charge,
00167 const GlobalVector& momentum,
00168 const GlobalPoint& position,
00169 double& xc, double& yc, double& r,
00170 const MagneticField& magField)
00171 const
00172 {
00173
00174
00178
00179 double bz = magField.inTesla(position).z() * 2.99792458e-3;
00180
00181
00182 double signed_r = charge*momentum.transverse() / bz;
00183 r = abs(signed_r);
00187
00188 double phi = momentum.phi();
00189 xc = signed_r*sin(phi) + position.x();
00190 yc = -signed_r*cos(phi) + position.y();
00191
00192 }
00193
00194
00195 int
00196 TangentApproachInRPhi::transverseCoord(double cxa, double cya, double ra,
00197 double cxb, double cyb, double rb,
00198 double & xg1, double & yg1,
00199 double & xg2, double & yg2) const
00200 {
00201 int flag = 0;
00202 double x1, y1, x2, y2;
00203
00204
00205
00206
00207 double d_ab = sqrt((cxb - cxa)*(cxb - cxa) + (cyb - cya)*(cyb - cya));
00208 if (d_ab == 0) {
00209 return 0;
00210 }
00211
00212 double u = (cxb - cxa) / d_ab;
00213 double v = (cyb - cya) / d_ab;
00214
00215
00216 if (d_ab <= ra + rb && d_ab >= abs(rb - ra)) {
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 flag = 1;
00232
00233
00234
00235 x1 = ra; y1 = 0; x2 = d_ab - rb; y2 = 0;
00236
00237
00238 }
00239 else if (d_ab > ra + rb) {
00240
00241
00242 flag = 2;
00243
00244
00245
00246 x1 = ra; y1 = 0; x2 = d_ab - rb; y2 = 0;
00247 }
00248 else if (d_ab < abs(rb - ra)) {
00249
00250
00251 flag = 2;
00252
00253
00254
00255 double sign = 1.;
00256 if (ra <= rb) sign = -1.;
00257 x1 = sign*ra; y1 = 0; x2 = d_ab + sign*rb; y2 = 0;
00258 }
00259 else {
00260 return 0;
00261 }
00262
00263
00264 xg1 = u*x1 - v*y1 + cxa; yg1 = v*x1 + u*y1 + cya;
00265 xg2 = u*x2 - v*y2 + cxa; yg2 = v*x2 + u*y2 + cya;
00266
00267 return flag;
00268 }
00269
00270
00271 double
00272 TangentApproachInRPhi::zCoord(const GlobalVector& mom,
00273 const GlobalPoint& pos,
00274 double r, double xc, double yc,
00275 double xg, double yg) const
00276 {
00277
00278
00279 double x = pos.x(); double y = pos.y(); double z = pos.z();
00280
00281 double px = mom.x(); double py = mom.y(); double pz = mom.z();
00282
00283
00284
00285
00286 double phi = 0.;
00287 double sinHalfPhi = sqrt((x-xg)*(x-xg) + (y-yg)*(y-yg))/(2*r);
00288 if (sinHalfPhi < 0.383) {
00289 phi = 2*asin(sinHalfPhi);
00290 }
00291 else {
00292 double cosPhi = ((x-xc)*(xg-xc) + (y-yc)*(yg-yc))/(r*r);
00293 if (std::abs(cosPhi) > 1) cosPhi = (cosPhi > 0 ? 1 : -1);
00294 phi = abs(acos(cosPhi));
00295 }
00296
00297 double signPhi = ((x - xc)*(yg - yc) - (xg - xc)*(y - yc) > 0) ? 1. : -1.;
00298
00299
00300
00301 double signOmega = ((x - xc)*py - (y - yc)*px > 0) ? 1. : -1.;
00302
00303
00304
00305
00306 double dz = signPhi*signOmega*(pz/mom.transverse())*phi*r;
00307
00308 return z + dz;
00309 }