00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Calibration/Tools/interface/DetIdAssociator.h"
00023
00024
00025
00026 std::vector<GlobalPoint> HDetIdAssociator::getTrajectory( const FreeTrajectoryState& ftsStart,
00027 const std::vector<GlobalPoint>& surfaces)
00028 {
00029 check_setup();
00030 std::vector<GlobalPoint> trajectory;
00031 TrajectoryStateOnSurface tSOSDest;
00032 FreeTrajectoryState ftsCurrent = ftsStart;
00033
00034 for(std::vector<GlobalPoint>::const_iterator surface_iter = surfaces.begin();
00035 surface_iter != surfaces.end(); surface_iter++) {
00036
00037 Cylinder *cylinder = new Cylinder(Surface::PositionType(0,0,0),
00038 Surface::RotationType(),
00039 double (surface_iter->perp()) );
00040 Plane *forwardEndcap = new Plane(Surface::PositionType(0,0,surface_iter->z()),
00041 Surface::RotationType());
00042 Plane *backwardEndcap = new Plane(Surface::PositionType(0,0,-surface_iter->z()),
00043 Surface::RotationType());
00044
00045
00046 LogTrace("StartingPoint")<< "Propagate from "<< "\n"
00047 << "\tx: " << ftsStart.position().x()<< "\n"
00048 << "\ty: " << ftsStart.position().y()<< "\n"
00049 << "\tz: " << ftsStart.position().z()<< "\n"
00050 << "\tmomentum eta: " << ftsStart.momentum().eta()<< "\n"
00051 << "\tmomentum phi: " << ftsStart.momentum().phi()<< "\n"
00052 << "\tmomentum: " << ftsStart.momentum().mag()<< "\n";
00053
00054 float tanTheta = ftsCurrent.momentum().perp()/ftsCurrent.momentum().z();
00055 float corner = surface_iter->perp()/surface_iter->z();
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 int ibar = 0;
00070 if (fabs(tanTheta) > corner)
00071 {
00072 tSOSDest = ivProp_->propagate(ftsCurrent, *cylinder);
00073
00074 }
00075 else if(tanTheta > 0.)
00076 {tSOSDest = ivProp_->propagate(ftsCurrent, *forwardEndcap); ibar=1; }
00077 else
00078 {tSOSDest = ivProp_->propagate(ftsCurrent, *backwardEndcap); ibar=-1; }
00079
00080
00081
00082 if(! tSOSDest.isValid() )
00083 {
00084
00085 if(ibar == 0){
00086 if (tanTheta < 0 ) tSOSDest = ivProp_->propagate( ftsCurrent,*forwardEndcap);
00087 if (tanTheta >= 0 ) tSOSDest = ivProp_->propagate( ftsCurrent,*backwardEndcap);
00088 }
00089 else
00090 {
00091 tSOSDest = ivProp_->propagate(ftsCurrent, *cylinder);
00092 }
00093 } else
00094 {
00095
00096 if(abs(ibar) > 0)
00097 {
00098 if(tSOSDest.globalPosition().perp() > surface_iter->perp())
00099 {
00100 tSOSDest = ivProp_->propagate(ftsCurrent, *cylinder);
00101 }
00102 }
00103 else
00104 {
00105 if (tanTheta < 0 ) tSOSDest = ivProp_->propagate( ftsCurrent,*forwardEndcap);
00106 if (tanTheta >= 0 ) tSOSDest = ivProp_->propagate( ftsCurrent,*backwardEndcap);
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 if (! tSOSDest.isValid()) return trajectory;
00124
00125
00126 LogTrace("SuccessfullPropagation") << "Great, I reached something." << "\n"
00127 << "\tx: " << tSOSDest.freeState()->position().x() << "\n"
00128 << "\ty: " << tSOSDest.freeState()->position().y() << "\n"
00129 << "\tz: " << tSOSDest.freeState()->position().z() << "\n"
00130 << "\teta: " << tSOSDest.freeState()->position().eta() << "\n"
00131 << "\tphi: " << tSOSDest.freeState()->position().phi() << "\n";
00132
00133
00134
00135 GlobalPoint point = tSOSDest.freeState()->position();
00136 point = tSOSDest.freeState()->position();
00137 ftsCurrent = *tSOSDest.freeState();
00138 trajectory.push_back(point);
00139 }
00140 return trajectory;
00141 }
00142
00143
00144 std::set<DetId> HDetIdAssociator::getDetIdsCloseToAPoint(const GlobalPoint& direction,
00145 const int idR)
00146 {
00147 std::set<DetId> set;
00148 check_setup();
00149 int nDets=0;
00150 if (! theMap_) buildMap();
00151 LogTrace("MatchPoint") << "point (eta,phi): " << direction.eta() << "," << direction.phi() << "\n";
00152 int ieta = iEta(direction);
00153 int iphi = iPhi(direction);
00154
00155 LogTrace("MatchPoint") << "(ieta,iphi): " << ieta << "," << iphi << "\n";
00156
00157 if (ieta>=0 && ieta<nEta_ && iphi>=0 && iphi<nPhi_){
00158 set = (*theMap_)[ieta][iphi];
00159 nDets++;
00160 if (idR>0){
00161 LogTrace("MatchPoint") << "Add neighbors (ieta,iphi): " << ieta << "," << iphi << "\n";
00162
00163 int maxIEta = ieta+idR;
00164 int minIEta = ieta-idR;
00165 if(maxIEta>=nEta_) maxIEta = nEta_-1;
00166 if(minIEta<0) minIEta = 0;
00167 int maxIPhi = iphi+idR;
00168 int minIPhi = iphi-idR;
00169 if(minIPhi<0) {
00170 minIPhi+=nPhi_;
00171 maxIPhi+=nPhi_;
00172 }
00173 LogTrace("MatchPoint") << "\tieta (min,max): " << minIEta << "," << maxIEta<< "\n";
00174 LogTrace("MatchPoint") << "\tiphi (min,max): " << minIPhi << "," << maxIPhi<< "\n";
00175 for (int i=minIEta;i<=maxIEta;i++)
00176 for (int j=minIPhi;j<=maxIPhi;j++) {
00177 if( i==ieta && j==iphi) continue;
00178 set.insert((*theMap_)[i][j%nPhi_].begin(),(*theMap_)[i][j%nPhi_].end());
00179 nDets++;
00180 }
00181 }
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 return set;
00195 }
00196
00197
00198 int HDetIdAssociator::iEta (const GlobalPoint& point)
00199 {
00200
00201 int iEta1 = int(point.eta()/etaBinSize_ + nEta_/2);
00202 if (point.eta()>1.827 && point.eta()<=1.830) return iEta1-1;
00203 else if (point.eta()>1.914 && point.eta()<=1.930) return iEta1-1;
00204 else if (point.eta()>2.001 && point.eta()<=2.043) return iEta1-1;
00205 else if (point.eta()>2.088 && point.eta()<=2.172) return iEta1-1;
00206 else if (point.eta()>2.175 && point.eta()<=2.262) return iEta1-1;
00207 else if (point.eta()>2.262 && point.eta()<=2.332) return iEta1-2;
00208 else if (point.eta()>2.332 && point.eta()<=2.349) return iEta1-1;
00209 else if (point.eta()>2.349 && point.eta()<=2.436) return iEta1-2;
00210 else if (point.eta()>2.436 && point.eta()<=2.500) return iEta1-3;
00211 else if (point.eta()>2.500 && point.eta()<=2.523) return iEta1-2;
00212 else if (point.eta()>2.523 && point.eta()<=2.610) return iEta1-3;
00213 else if (point.eta()>2.610 && point.eta()<=2.650) return iEta1-4;
00214 else if (point.eta()>2.650 && point.eta()<=2.697) return iEta1-3;
00215 else if (point.eta()>2.697 && point.eta()<=2.784) return iEta1-4;
00216 else if (point.eta()>2.784 && point.eta()<=2.868) return iEta1-5;
00217 else if (point.eta()>2.868 && point.eta()<=2.871) return iEta1-4;
00218 else if (point.eta()>2.871 && point.eta()<=2.958) return iEta1-5;
00219 else if (point.eta()>2.958) return iEta1-6;
00220 else if (point.eta()<-1.827 && point.eta()>=-1.830) return iEta1+1;
00221 else if (point.eta()<-1.914 && point.eta()>=-1.930) return iEta1+1;
00222 else if (point.eta()<-2.001 && point.eta()>=-2.043) return iEta1+1;
00223 else if (point.eta()<-2.088 && point.eta()>=-2.172) return iEta1+1;
00224 else if (point.eta()<-2.175 && point.eta()>=-2.262) return iEta1+1;
00225 else if (point.eta()<-2.262 && point.eta()>=-2.332) return iEta1+2;
00226 else if (point.eta()<-2.332 && point.eta()>=-2.349) return iEta1+1;
00227 else if (point.eta()<-2.349 && point.eta()>=-2.436) return iEta1+2;
00228 else if (point.eta()<-2.436 && point.eta()>=-2.500) return iEta1+3;
00229 else if (point.eta()<-2.500 && point.eta()>=-2.523) return iEta1+2;
00230 else if (point.eta()<-2.523 && point.eta()>=-2.610) return iEta1+3;
00231 else if (point.eta()<-2.610 && point.eta()>=-2.650) return iEta1+4;
00232 else if (point.eta()<-2.650 && point.eta()>=-2.697) return iEta1+3;
00233 else if (point.eta()<-2.697 && point.eta()>=-2.784) return iEta1+4;
00234 else if (point.eta()<-2.784 && point.eta()>=-2.868) return iEta1+5;
00235 else if (point.eta()<-2.868 && point.eta()>=-2.871) return iEta1+4;
00236 else if (point.eta()<-2.871 && point.eta()>=-2.958) return iEta1+5;
00237 else if (point.eta()<-2.349) return iEta1+6;
00238 else return iEta1;
00239 }
00240
00241
00242 int HDetIdAssociator::iPhi (const GlobalPoint& point)
00243 {
00244 double pi=4*atan(1.);
00245 int iPhi1 = int((double(point.phi())+pi)/(2*pi)*nPhi_);
00246 return iPhi1;
00247 }
00248
00249
00250 void HDetIdAssociator::buildMap()
00251 {
00252
00253 check_setup();
00254 LogTrace("HDetIdAssociator")<<"building map" << "\n";
00255 if(theMap_) delete theMap_;
00256 theMap_ = new std::vector<std::vector<std::set<DetId> > >(nEta_,std::vector<std::set<DetId> >(nPhi_));
00257 int numberOfDetIdsOutsideEtaRange = 0;
00258 int numberOfDetIdsActive = 0;
00259 std::set<DetId> validIds = getASetOfValidDetIds();
00260 for (std::set<DetId>::const_iterator id_itr = validIds.begin(); id_itr!=validIds.end(); id_itr++) {
00261
00262 GlobalPoint point = getPosition(*id_itr);
00263
00264 if(point.eta()==0)continue;
00265
00266 int ieta = iEta(point);
00267 int iphi = iPhi(point);
00268 int etaMax(-1);
00269 int etaMin(nEta_);
00270 int phiMax(-1);
00271 int phiMin(nPhi_);
00272 if ( iphi >= nPhi_ ) iphi = iphi % nPhi_;
00273 assert (iphi>=0);
00274 if ( etaMin > ieta) etaMin = ieta;
00275 if ( etaMax < ieta) etaMax = ieta;
00276 if ( phiMin > iphi) phiMin = iphi;
00277 if ( phiMax < iphi) phiMax = iphi;
00278
00279 if ((ieta>54||ieta<15) && iphi%2==0) phiMax++;
00280 if ((ieta>54||ieta<15) && iphi%2==1) phiMin--;
00281
00282 if (etaMax<0||phiMax<0||etaMin>=nEta_||phiMin>=nPhi_) {
00283 LogTrace("HDetIdAssociator")<<"Out of range: DetId:" << id_itr->rawId() <<
00284 "\n\teta (min,max): " << etaMin << "," << etaMax <<
00285 "\n\tphi (min,max): " << phiMin << "," << phiMax <<
00286 "\nTower id: " << id_itr->rawId() << "\n";
00287 numberOfDetIdsOutsideEtaRange++;
00288 continue;
00289 }
00290
00291 if (phiMax-phiMin > phiMin+nPhi_-phiMax){
00292 phiMin += nPhi_;
00293 std::swap(phiMin,phiMax);
00294 }
00295 for(int ieta = etaMin; ieta <= etaMax; ieta++)
00296 for(int iphi = phiMin; iphi <= phiMax; iphi++)
00297 (*theMap_)[ieta][iphi%nPhi_].insert(*id_itr);
00298 numberOfDetIdsActive++;
00299 }
00300 LogTrace("HDetIdAssociator") << "Number of elements outside the allowed range ( |eta|>"<<
00301 nEta_/2*etaBinSize_ << "): " << numberOfDetIdsOutsideEtaRange << "\n";
00302 LogTrace("HDetIdAssociator") << "Number of active DetId's mapped: " <<
00303 numberOfDetIdsActive << "\n";
00304 }
00305
00306
00307 std::set<DetId> HDetIdAssociator::getDetIdsInACone(const std::set<DetId>& inset,
00308 const std::vector<GlobalPoint>& trajectory,
00309 const double dR)
00310 {
00311
00312 check_setup();
00313 std::set<DetId> outset;
00314
00315 if(dR>=0) {
00316 for(std::set<DetId>::const_iterator id_iter = inset.begin(); id_iter != inset.end(); id_iter++)
00317 for(std::vector<GlobalPoint>::const_iterator point_iter = trajectory.begin(); point_iter != trajectory.end(); point_iter++)
00318 if (nearElement(*point_iter,*id_iter,dR)) outset.insert(*id_iter);
00319 }
00320 else {
00321 if (inset.size()!=1) return outset;
00322 std::set<DetId>::const_iterator id_inp = inset.begin();
00323 int ieta;
00324 int iphi;
00325 GlobalPoint point = getPosition(*id_inp);
00326 ieta = iEta(point);
00327 iphi = iPhi(point);
00328 for (int i=ieta-1;i<=ieta+1;i++) {
00329 for (int j=iphi-1;j<=iphi+1;j++) {
00330
00331 if( i<0 || i>=nEta_) continue;
00332 int j2fill = j%nPhi_;
00333 if(j2fill<0) j2fill+=nPhi_;
00334 if((*theMap_)[i][j2fill].size()==0)continue;
00335 outset.insert((*theMap_)[i][j2fill].begin(),(*theMap_)[i][j2fill].end());
00336 }
00337 }
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 return outset;
00349 }
00350
00351
00352 std::set<DetId> HDetIdAssociator::getCrossedDetIds(const std::set<DetId>& inset,
00353 const std::vector<GlobalPoint>& trajectory)
00354 {
00355 check_setup();
00356 std::set<DetId> outset;
00357 for(std::set<DetId>::const_iterator id_iter = inset.begin(); id_iter != inset.end(); id_iter++)
00358 for(std::vector<GlobalPoint>::const_iterator point_iter = trajectory.begin(); point_iter != trajectory.end(); point_iter++)
00359 if (insideElement(*point_iter, *id_iter)) outset.insert(*id_iter);
00360 return outset;
00361 }
00362
00363
00364 std::set<DetId> HDetIdAssociator::getMaxEDetId(const std::set<DetId>& inset,
00365 edm::Handle<CaloTowerCollection> caloTowers)
00366 {
00367
00368 check_setup();
00369 std::set<DetId> outset;
00370 std::set<DetId>::const_iterator id_max = inset.begin();
00371 double Ehadmax=0;
00372
00373 for(std::set<DetId>::const_iterator id_iter = inset.begin(); id_iter != inset.end(); id_iter++) {
00374 DetId id(*id_iter);
00375
00376
00377
00378 CaloTowerCollection::const_iterator tower = (*caloTowers).find(id);
00379 if(tower != (*caloTowers).end() && tower->hadEnergy()>Ehadmax) {
00380 id_max = id_iter;
00381 Ehadmax = tower->hadEnergy();
00382 }
00383 }
00384
00385 if (Ehadmax > 0) outset.insert(*id_max);
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 return outset;
00396 }
00397
00398
00399 std::set<DetId> HDetIdAssociator::getMaxEDetId(const std::set<DetId>& inset,
00400 edm::Handle<HBHERecHitCollection> recHits)
00401 {
00402
00403 check_setup();
00404 std::set<DetId> outset;
00405 std::set<DetId>::const_iterator id_max = inset.begin();
00406 double Ehadmax=0;
00407
00408 for(std::set<DetId>::const_iterator id_iter = inset.begin(); id_iter != inset.end(); id_iter++) {
00409 DetId id(*id_iter);
00410
00411
00412
00413 HBHERecHitCollection::const_iterator hit = (*recHits).find(id);
00414 if(hit != (*recHits).end() && hit->energy()>Ehadmax) {
00415 id_max = id_iter;
00416 Ehadmax = hit->energy();
00417 }
00418 }
00419
00420 if (Ehadmax > 0) outset.insert(*id_max);
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 return outset;
00431 }