00001 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00002 #include "RecoHIMuon/HiMuTracking/interface/HICMeasurementEstimator.h"
00003 #include "TrackingTools/PatternTools/interface/MeasurementExtractor.h"
00004 #include "DataFormats/GeometrySurface/interface/BoundPlane.h"
00005 using namespace edm;
00006 using namespace std;
00007 using namespace cms;
00008
00009
00010 std::pair<bool,double>
00011 HICMeasurementEstimator::estimate(const TrajectoryStateOnSurface& tsos,
00012 const TransientTrackingRecHit& aRecHit) const {
00013 std::pair<bool,double> flag(false,0.);
00014 if(!tsos.isValid()) {
00015 #ifdef DEBUG
00016 std::cout<<" HICMeasurementEstimator::estimate::trajectory is not valid "<<std::endl;
00017 #endif
00018 return flag;
00019 }
00020
00021 switch (aRecHit.dimension()) {
00022 case 1: return estimate<1>(tsos,aRecHit);
00023 case 2: return estimate<2>(tsos,aRecHit);
00024 case 3: return estimate<3>(tsos,aRecHit);
00025 case 4: return estimate<4>(tsos,aRecHit);
00026 case 5: return estimate<5>(tsos,aRecHit);
00027 }
00028 throw cms::Exception("RecHit of invalid size (not 1,2,3,4,5)");
00029 }
00030
00031 template <unsigned int D> std::pair<bool,double>
00032 HICMeasurementEstimator::estimate(const TrajectoryStateOnSurface& tsos,
00033 const TransientTrackingRecHit& aRecHit) const {
00034 typedef typename AlgebraicROOTObject<D>::Vector Vec;
00035 typedef typename AlgebraicROOTObject<D>::SymMatrix Mat;
00036 double est = 0.;
00037
00038 if(!(aRecHit.isValid())) {
00039 #ifdef DEBUG
00040 std::cout<<" Measurement estimator::RecHit is invalid "<<std::endl;
00041 #endif
00042 return HitReturnType(false,est);
00043 }
00044
00045
00046 double dphi = fabs(tsos.freeTrajectoryState()->parameters().position().phi() - aRecHit.globalPosition().phi() - thePhiBoundMean);
00047 double dz = fabs( tsos.freeTrajectoryState()->parameters().position().z() - aRecHit.globalPosition().z() - theZBoundMean );
00048 double dr = fabs( tsos.freeTrajectoryState()->parameters().position().perp() - aRecHit.globalPosition().perp() - theZBoundMean );
00049 #ifdef DEBUG
00050 std::cout<<" Momentum "<<tsos.freeTrajectoryState()->parameters().momentum().perp()<<" "<<tsos.freeTrajectoryState()->parameters().momentum().z()<<std::endl;
00051 std::cout<<" RecHit position r "<<aRecHit.globalPosition().perp()<<" phi "<<aRecHit.globalPosition().phi()<<" "<<aRecHit.globalPosition().z()<<std::endl;
00052 std::cout<<" Predicted position "<<tsos.freeTrajectoryState()->parameters().position().perp()<<" "<<tsos.freeTrajectoryState()->parameters().position().phi()<<
00053 " "<<tsos.freeTrajectoryState()->parameters().position().z()<<std::endl;
00054 std::cout<<" HICMeasurementEstimator::phi "<<dphi<<" "<<thePhiBound<<std::endl;
00055 std::cout<<" HICMeasurementEstimator::z "<<dz<<" "<<theZBound<<std::endl;
00056 std::cout<<" HICMeasurementEstimator::z "<<dr<<" "<<theZBound<<std::endl;
00057 #endif
00058 if( dphi > thePhiBound ) {
00059 #ifdef DEBUG
00060 std::cout<<" HICMeasurementEstimator::phi::failed "<<std::endl;
00061 #endif
00062 return HitReturnType(false,est);
00063 }
00064 if( dz > theZBound ) {
00065 #ifdef DEBUG
00066 std::cout<<" HICMeasurementEstimator::z::failed "<<std::endl;
00067 #endif
00068 return HitReturnType(false,est);
00069 }
00070 if( dr > theZBound ) {
00071 #ifdef DEBUG
00072 std::cout<<" HICMeasurementEstimator::r::failed "<<std::endl;
00073 #endif
00074 return HitReturnType(false,est);
00075 }
00076
00077
00078 MeasurementExtractor me(tsos);
00079 Vec r = asSVector<D>(aRecHit.parameters()) - me.measuredParameters<D>(aRecHit);
00080 Mat R = asSMatrix<D>(aRecHit.parametersError()) + me.measuredError<D>(aRecHit);
00081
00082 R.Invert();
00083 est = ROOT::Math::Similarity(r, R);
00084
00085 if( est > theChi2Cut )
00086 {
00087 #ifdef DEBUG
00088 std::cout<<" HICMeasurementEstimator::chi2::failed "<<est<<" "<<theChi2Cut<<std::endl;
00089 #endif
00090
00091 return HitReturnType(false,est);
00092 }
00093
00094 return HitReturnType(true,est);
00095 }
00096
00097 bool HICMeasurementEstimator::estimate( const TrajectoryStateOnSurface& ts,
00098 const BoundPlane& plane) const
00099 {
00100
00101
00102 double pi = 4.*atan(1.);
00103 double twopi = 2.*pi;
00104 float theZError = plane.bounds().length() + 4.;
00105 float thePhiError = 2.*plane.bounds().width()/plane.position().perp();
00106
00107
00108
00109 #ifdef DEBUG
00110 cout<<" ======================================================================================== ";
00111 cout<<" Estimate detector:: tsos : detector : Error "<<endl;
00112 cout<<" R "<<ts.globalPosition().perp()<<" "<<plane.position().perp()<<" "<<theZError<<endl;
00113 cout<<" Phi "<<ts.globalPosition().phi()<<" "<<plane.position().phi()<<" "<<thePhiError<<endl;
00114 cout<<" Z "<<ts.globalPosition().z()<<" "<<plane.position().z()<<" "<<theZError<<endl;
00115 #endif
00116
00117 bool flag = false;
00118 if(fabs(ts.globalPosition().perp()-plane.position().perp())<theZError){
00119 if(fabs(ts.globalPosition().z()-plane.position().z())<theZError){
00120 float phi1 = ts.globalPosition().phi();
00121 float phi2 = plane.position().phi();
00122 if(phi1<0.) phi1 = twopi+phi1;
00123 if(phi2<0.) phi2 = twopi+phi2;
00124 float dfi = fabs(phi1-phi2);
00125 if(dfi>pi) dfi = twopi-dfi;
00126 if(dfi<thePhiError) flag = true;
00127 }
00128 }
00129 #ifdef DEBUG
00130 cout<<" Estimate = "<<flag<<endl;
00131 #endif
00132
00133 return flag;
00134
00135 }
00136
00137 vector<double> HICMeasurementEstimator::setCuts(Trajectory& traj, const DetLayer* b)
00138 {
00139 vector<double> theCuts;
00140 const DetLayer* a = traj.data().back().layer();
00141 const DetLayer* first = traj.data().front().layer();
00142 const DetLayer* last = traj.data().front().layer();
00143 thePhiWinMean = 0.;
00144 theZWinMean = 0.;
00145 thePhiWin = 0.;
00146 theZWin = 0.;
00147 theNewCut = 10.;
00148 theNewCutB = 5.;
00149
00150 thePhiWinMeanB = 0.002;
00151 theZWinMeanB = 0.;
00152 thePhiWinB = 0.008;
00153 theZWinB = 17.;
00154
00155 theZCutMean = 0.;
00156 thePhiCutMean = 0.;
00157 thePhiCut = 0.;
00158 theZCut = 0.;
00159
00160 theLayer = b;
00161 theLastLayer = a;
00162
00163
00164 theTrajectorySize = traj.data().size();
00165
00166
00167 if( theBarrel.size() == 0 || theForward.size() == 0 )
00168 {
00169 #ifdef DEBUG
00170 cout<<" HICMeasurementEstimator::setCuts:: no datector map "<<endl;
00171 #endif
00172 return theCuts;
00173 }
00174
00175 if( a->location() == GeomDetEnumerators::barrel )
00176 {
00177 if( first->location() == GeomDetEnumerators::barrel )
00178 {
00179 thePhiWin = (*theHICConst).phiwinbar[(*theBarrel.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00180 theZWin = (*theHICConst).zwinbar[(*theBarrel.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00181 thePhiCut = (*theHICConst).phicutbar[(*theBarrel.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00182 theZCut = (*theHICConst).zcutbar[(*theBarrel.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00183
00184
00185 }
00186 else
00187 {
00188 if(first->surface().position().z() > 0. )
00189 {
00190 thePhiWin = (*theHICConst).phiwinfbb[(*theForward.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00191 theZWin = (*theHICConst).zwinfbb[(*theForward.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00192 thePhiCut = (*theHICConst).phicutfbb[(*theForward.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00193 theZCut = (*theHICConst).zcutfbb[(*theForward.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00194
00195
00196 } else {
00197 thePhiWin = (*theHICConst).phiwinfbb[(*theBackward.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00198 theZWin = (*theHICConst).zwinfbb[(*theBackward.find(first)).second][(*theBarrel.find(a)).second][(*theBarrel.find(b)).second];
00199 thePhiCut = (*theHICConst).phicutfbb[(*theBackward.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00200 theZCut = (*theHICConst).zcutfbb[(*theBackward.find(first)).second][(*theBarrel.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00201
00202
00203 }
00204 }
00205
00206
00207 theCuts.push_back(thePhiWin); theCuts.push_back(theZWin);
00208 theCuts.push_back(thePhiCut); theCuts.push_back(theZCut);
00209
00210 return theCuts;
00211 }
00212 if( a->location() == GeomDetEnumerators::endcap && b->location() == GeomDetEnumerators::endcap)
00213 {
00214 if( a->surface().position().z() > 0. )
00215 {
00216 thePhiWin = (*theHICConst).phiwinfrw[(*theForward.find(first)).second][(*theForward.find(a)).second][(*theForward.find(b)).second];
00217 theZWin = (*theHICConst).zwinfrw[(*theForward.find(first)).second][(*theForward.find(a)).second][(*theForward.find(b)).second];
00218 thePhiCut = (*theHICConst).phicutfrw[(*theForward.find(first)).second][(*theForward.find(theLastLayer)).second][(*theForward.find(b)).second];
00219 theZCut = (*theHICConst).zcutfrw[(*theForward.find(first)).second][(*theForward.find(theLastLayer)).second][(*theForward.find(b)).second];
00220 }
00221 else
00222 {
00223 thePhiWin = (*theHICConst).phiwinfrw[(*theBackward.find(first)).second][(*theBackward.find(a)).second][(*theBackward.find(b)).second];
00224 theZWin = (*theHICConst).zwinfrw[(*theBackward.find(first)).second][(*theBackward.find(a)).second][(*theBackward.find(b)).second];
00225 thePhiCut = (*theHICConst).phicutfrw[(*theBackward.find(first)).second][(*theBackward.find(theLastLayer)).second][(*theBackward.find(b)).second];
00226 theZCut = (*theHICConst).zcutfrw[(*theBackward.find(first)).second][(*theBackward.find(theLastLayer)).second][(*theBackward.find(b)).second];
00227 }
00228
00229 if( theLowMult == 1 )
00230 {
00231 if( b->subDetector() == GeomDetEnumerators::PixelEndcap ) theNewCut = 20.;
00232 if( traj.measurements().size() == 1 ) theNewCut = 20.;
00233 theNewCutB = 30.;
00234 }
00235
00236 thePhiWinMeanB = 0.004;
00237 thePhiWinB = 0.04;
00238
00239
00240 theCuts.push_back(thePhiWin); theCuts.push_back(theZWin);
00241 theCuts.push_back(thePhiCut); theCuts.push_back(theZCut);
00242
00243 return theCuts;
00244 }
00245 if( a->location() == GeomDetEnumerators::endcap && b->location() == GeomDetEnumerators::barrel )
00246 {
00247
00248 if( a->surface().position().z() > 0. )
00249 {
00250 thePhiWin = (*theHICConst).phiwinbfrw[(*theForward.find(first)).second][(*theForward.find(a)).second][(*theBarrel.find(b)).second];
00251 theZWin = (*theHICConst).zwinbfrw[(*theForward.find(first)).second][(*theForward.find(a)).second][(*theBarrel.find(b)).second];
00252 thePhiCut = (*theHICConst).phicutbfrw[(*theForward.find(first)).second][(*theForward.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00253 theZCut = (*theHICConst).zcutbfrw[(*theForward.find(first)).second][(*theForward.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00254 }
00255 else
00256 {
00257 thePhiWin = (*theHICConst).phiwinbfrw[(*theBackward.find(first)).second][(*theBackward.find(a)).second][(*theBarrel.find(b)).second];
00258 theZWin = (*theHICConst).zwinbfrw[(*theBackward.find(first)).second][(*theBackward.find(a)).second][(*theBarrel.find(b)).second];
00259 thePhiCut = (*theHICConst).phicutbfrw[(*theBackward.find(first)).second][(*theBackward.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00260 theZCut = (*theHICConst).zcutbfrw[(*theBackward.find(first)).second][(*theBackward.find(theLastLayer)).second][(*theBarrel.find(b)).second];
00261 }
00262
00263 if( b->subDetector() == GeomDetEnumerators::PixelBarrel) theNewCut = 20.;
00264
00265 thePhiWinMeanB = 0.004;
00266 thePhiWinB = 0.016;
00267
00268 theCuts.push_back(thePhiWin); theCuts.push_back(theZWin);
00269 theCuts.push_back(thePhiCut); theCuts.push_back(theZCut);
00270
00271 return theCuts;
00272 }
00273
00274 return theCuts;
00275 }
00276
00277 void HICMeasurementEstimator::chooseCuts(int& i)
00278 {
00279
00280 theChi2Cut = theNewCut;
00281
00282
00283 if( i == 1 )
00284 {
00285 thePhiBound = thePhiWin;
00286 theZBound = theZWin;
00287 thePhiBoundMean = thePhiWinMean;
00288 theZBoundMean = theZWinMean;
00289
00290 }
00291 if( i == 2 )
00292 {
00293 thePhiBound = thePhiWinB;
00294 theZBound = theZWinB;
00295 thePhiBoundMean = thePhiWinMean;
00296 theZBoundMean = theZWinMean;
00297
00298 }
00299 if( i == 3 )
00300 {
00301 thePhiBound = thePhiCut;
00302 theZBound = theZCut;
00303 thePhiBoundMean = thePhiCutMean;
00304 theZBoundMean = theZCutMean;
00305
00306 }
00307
00308 theCutType = i;
00309 }
00310
00311 int HICMeasurementEstimator::getDetectorCode(const DetLayer* a)
00312 {
00313 int layer = 0;
00314 if( a->location() == GeomDetEnumerators::barrel )
00315 {
00316 layer = (*theBarrel.find(a)).second;
00317 }
00318 if( a->location() == GeomDetEnumerators::endcap )
00319 {
00320 if( a->surface().position().z() > 0. ) { layer = 100+(*theForward.find(a)).second;}
00321 if( a->surface().position().z() < 0. ) { layer = -100-(*theBackward.find(a)).second;}
00322 }
00323 return layer;
00324 }
00325
00326 void HICMeasurementEstimator::setHICDetMap()
00327 {
00328
00329 #ifdef DEBUG
00330 std::cout<<" Set Detector Map... "<<std::endl;
00331 #endif
00332
00333 int ila=0;
00334 for ( std::vector<BarrelDetLayer*>::const_iterator ilayer = bl.begin(); ilayer != bl.end(); ilayer++)
00335 {
00336 theBarrel[(*ilayer)]=ila;
00337 ila++;
00338 }
00339
00340
00341
00342
00343 int ilf1 = 0;
00344 int ilf2 = 0;
00345 for ( vector<ForwardDetLayer*>::const_iterator ilayer = fpos.begin();
00346 ilayer != fpos.end(); ilayer++)
00347 {
00348 theForward[(*ilayer)] = ilf1;
00349 ilf1++;
00350 }
00351 for ( vector<ForwardDetLayer*>::const_iterator ilayer = fneg.begin();
00352 ilayer != fneg.end(); ilayer++)
00353 {
00354 #ifdef DEBUG
00355 cout<<" HICDetectorMap::negative layers "<<(**ilayer).position().z()<<" "<<ilf2<<endl;
00356 #endif
00357 theBackward[(*ilayer)] = ilf2;
00358 ilf2++;
00359 }
00360
00361 }