00001
00002
00003 #include <memory>
00004 #include <string>
00005 #include <vector>
00006
00007 #include "SimTracker/TrackerHitAssociation/interface/TrackerHitAssociator.h"
00008
00009 #include "DataFormats/Common/interface/Ref.h"
00010 #include "DataFormats/DetId/interface/DetId.h"
00011 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00012 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00013
00014 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00015
00016
00017 #include <numeric>
00018 #include <iostream>
00019
00020 using namespace std;
00021 using namespace edm;
00022
00023
00024
00025
00026 TrackerHitAssociator::TrackerHitAssociator(const edm::Event& e) :
00027 myEvent_(e),
00028 doPixel_( true ),
00029 doStrip_( true ),
00030 doTrackAssoc_( false ) {
00031 trackerContainers.clear();
00032
00033
00034
00035 trackerContainers.push_back("g4SimHitsTrackerHitsTIBLowTof");
00036 trackerContainers.push_back("g4SimHitsTrackerHitsTIBHighTof");
00037 trackerContainers.push_back("g4SimHitsTrackerHitsTIDLowTof");
00038 trackerContainers.push_back("g4SimHitsTrackerHitsTIDHighTof");
00039 trackerContainers.push_back("g4SimHitsTrackerHitsTOBLowTof");
00040 trackerContainers.push_back("g4SimHitsTrackerHitsTOBHighTof");
00041 trackerContainers.push_back("g4SimHitsTrackerHitsTECLowTof");
00042 trackerContainers.push_back("g4SimHitsTrackerHitsTECHighTof");
00043 trackerContainers.push_back("g4SimHitsTrackerHitsPixelBarrelLowTof");
00044 trackerContainers.push_back("g4SimHitsTrackerHitsPixelBarrelHighTof");
00045 trackerContainers.push_back("g4SimHitsTrackerHitsPixelEndcapLowTof");
00046 trackerContainers.push_back("g4SimHitsTrackerHitsPixelEndcapHighTof");
00047
00048
00049
00050
00051
00052
00053 for(uint32_t i = 0; i< trackerContainers.size();i++){
00054 e.getByLabel("mix",trackerContainers[i],cf_simhit);
00055 cf_simhitvec.push_back(cf_simhit.product());
00056 }
00057
00058 std::auto_ptr<MixCollection<PSimHit> > allTrackerHits(new MixCollection<PSimHit>(cf_simhitvec));
00059 TrackerHits = (*allTrackerHits);
00060
00061
00062 SimHitMap.clear();
00063
00064 MixCollection<PSimHit>::iterator isim;
00065 for (isim=allTrackerHits->begin(); isim!= allTrackerHits->end();isim++) {
00066 SimHitMap[(*isim).detUnitId()].push_back((*isim));
00067 }
00068
00069 if(doStrip_) e.getByLabel("simSiStripDigis", stripdigisimlink);
00070 if(doPixel_) e.getByLabel("simSiPixelDigis", pixeldigisimlink);
00071
00072 }
00073
00074
00075
00076
00077 TrackerHitAssociator::TrackerHitAssociator(const edm::Event& e, const edm::ParameterSet& conf) :
00078 myEvent_(e),
00079 doPixel_( conf.getParameter<bool>("associatePixel") ),
00080 doStrip_( conf.getParameter<bool>("associateStrip") ),
00081 doTrackAssoc_( conf.getParameter<bool>("associateRecoTracks") ){
00082
00083 trackerContainers.clear();
00084 trackerContainers = conf.getParameter<std::vector<std::string> >("ROUList");
00085
00086
00087 if(!doTrackAssoc_) {
00088
00089
00090
00091
00092 for(uint32_t i = 0; i< trackerContainers.size();i++){
00093 e.getByLabel("mix",trackerContainers[i],cf_simhit);
00094 cf_simhitvec.push_back(cf_simhit.product());
00095 }
00096
00097
00098
00099
00100
00101
00102 std::auto_ptr<MixCollection<PSimHit> > allTrackerHits(new MixCollection<PSimHit>(cf_simhitvec));
00103 TrackerHits = (*allTrackerHits);
00104
00105
00106 SimHitMap.clear();
00107
00108 MixCollection<PSimHit>::iterator isim;
00109 for (isim=allTrackerHits->begin(); isim!= allTrackerHits->end();isim++) {
00110 SimHitMap[(*isim).detUnitId()].push_back((*isim));
00111 }
00112
00113 }
00114
00115 if(doStrip_) e.getByLabel("simSiStripDigis", stripdigisimlink);
00116 if(doPixel_) e.getByLabel("simSiPixelDigis", pixeldigisimlink);
00117
00118 }
00119
00120 std::vector<PSimHit> TrackerHitAssociator::associateHit(const TrackingRecHit & thit)
00121 {
00122
00123
00124 if(const TransientTrackingRecHit * ttrh = dynamic_cast<const TransientTrackingRecHit *>(&thit)) {
00125 return associateHit(*ttrh->hit());
00126 }
00127
00128
00129 std::vector<PSimHit> result;
00130
00131
00132 simtrackid.clear();
00133 simhitCFPos.clear();
00134 StripHits = false;
00135
00136
00137 DetId detid= thit.geographicalId();
00138 uint32_t detID = detid.rawId();
00139
00140
00141
00142 if(detid.subdetId() == StripSubdetector::TIB ||
00143 detid.subdetId() == StripSubdetector::TOB ||
00144 detid.subdetId() == StripSubdetector::TID ||
00145 detid.subdetId() == StripSubdetector::TEC)
00146 {
00147
00148 if(const SiStripRecHit2D * rechit =
00149 dynamic_cast<const SiStripRecHit2D *>(&thit))
00150 {
00151 simtrackid = associateSimpleRecHit(rechit);
00152 }
00153
00154 if(const SiStripMatchedRecHit2D * rechit =
00155 dynamic_cast<const SiStripMatchedRecHit2D *>(&thit))
00156 {
00157 simtrackid = associateMatchedRecHit(rechit);
00158 }
00159
00160 if(const ProjectedSiStripRecHit2D * rechit =
00161 dynamic_cast<const ProjectedSiStripRecHit2D *>(&thit))
00162 {
00163 simtrackid = associateProjectedRecHit(rechit);
00164 detid = rechit->originalHit().geographicalId();
00165 detID = detid.rawId();
00166 }
00167
00168 }
00169
00170 if( (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelBarrel ||
00171 (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelEndcap)
00172 {
00173 if(const SiPixelRecHit * rechit = dynamic_cast<const SiPixelRecHit *>(&thit))
00174 {
00175 simtrackid = associatePixelRecHit(rechit);
00176 }
00177 }
00178
00179
00180 if(const SiTrackerGSRecHit2D * rechit = dynamic_cast<const SiTrackerGSRecHit2D *>(&thit))
00181 {
00182 simtrackid = associateGSRecHit(rechit);
00183 }
00184 if (const SiTrackerMultiRecHit * rechit = dynamic_cast<const SiTrackerMultiRecHit *>(&thit)){
00185 return associateMultiRecHit(rechit);
00186 }
00187
00188
00189 if(const SiTrackerGSMatchedRecHit2D * rechit = dynamic_cast<const SiTrackerGSMatchedRecHit2D *>(&thit))
00190 {
00191 simtrackid = associateGSMatchedRecHit(rechit);
00192 }
00193
00194
00195
00196
00197
00198 if(StripHits){
00199
00200
00201
00202 for(size_t i=0; i<simhitCFPos.size(); i++){
00203
00204
00205 result.push_back( TrackerHits.getObject(simhitCFPos[i]));
00206 }
00207 }else {
00208
00209
00210 vector<PSimHit> simHit;
00211 std::map<unsigned int, std::vector<PSimHit> >::const_iterator it = SimHitMap.find(detID);
00212 simHit.clear();
00213 if (it!= SimHitMap.end()){
00214 simHit = it->second;
00215 vector<PSimHit>::const_iterator simHitIter = simHit.begin();
00216 vector<PSimHit>::const_iterator simHitIterEnd = simHit.end();
00217 for (;simHitIter != simHitIterEnd; ++simHitIter) {
00218 const PSimHit ihit = *simHitIter;
00219 unsigned int simHitid = ihit.trackId();
00220 EncodedEventId simHiteid = ihit.eventId();
00221
00222 for(size_t i=0; i<simtrackid.size();i++){
00223 if(simHitid == simtrackid[i].first && simHiteid == simtrackid[i].second){
00224
00225
00226 result.push_back(ihit);
00227 }
00228 }
00229 }
00230 }else{
00232 std::map<unsigned int, std::vector<PSimHit> >::const_iterator itrphi =
00233 SimHitMap.find(detID+2);
00234 std::map<unsigned int, std::vector<PSimHit> >::const_iterator itster =
00235 SimHitMap.find(detID+1);
00236 if (itrphi!= SimHitMap.end()&&itster!=SimHitMap.end()){
00237 simHit = itrphi->second;
00238 simHit.insert(simHit.end(),(itster->second).begin(),(itster->second).end());
00239 vector<PSimHit>::const_iterator simHitIter = simHit.begin();
00240 vector<PSimHit>::const_iterator simHitIterEnd = simHit.end();
00241 for (;simHitIter != simHitIterEnd; ++simHitIter) {
00242 const PSimHit ihit = *simHitIter;
00243 unsigned int simHitid = ihit.trackId();
00244 EncodedEventId simHiteid = ihit.eventId();
00245
00246
00247
00248
00249
00250 for(size_t i=0; i<simtrackid.size();i++){
00251 if(simHitid == simtrackid[i].first && simHiteid == simtrackid[i].second){
00252
00253
00254 result.push_back(ihit);
00255 }
00256 }
00257 }
00258 }
00259 }
00260 }
00261
00262
00263 return result;
00264 }
00265
00266
00267 std::vector< SimHitIdpr > TrackerHitAssociator::associateHitId(const TrackingRecHit & thit)
00268 {
00269
00270
00271 if(const TransientTrackingRecHit * ttrh = dynamic_cast<const TransientTrackingRecHit *>(&thit)) {
00272 return associateHitId(*ttrh->hit());
00273 }
00274
00275
00276 simtrackid.clear();
00277
00278 simhitCFPos.clear();
00279
00280
00281 DetId detid= thit.geographicalId();
00282
00283
00284 if (const SiTrackerMultiRecHit * rechit = dynamic_cast<const SiTrackerMultiRecHit *>(&thit)){
00285 return associateMultiRecHitId(rechit);
00286 }
00287
00288
00289
00290 if(detid.subdetId() == StripSubdetector::TIB ||
00291 detid.subdetId() == StripSubdetector::TOB ||
00292 detid.subdetId() == StripSubdetector::TID ||
00293 detid.subdetId() == StripSubdetector::TEC)
00294 {
00295
00296 if(const SiStripRecHit2D * rechit =
00297 dynamic_cast<const SiStripRecHit2D *>(&thit))
00298 {
00299 simtrackid = associateSimpleRecHit(rechit);
00300 }
00301
00302 else if(const SiStripMatchedRecHit2D * rechit =
00303 dynamic_cast<const SiStripMatchedRecHit2D *>(&thit))
00304 {
00305 simtrackid = associateMatchedRecHit(rechit);
00306 }
00307
00308 else if(const ProjectedSiStripRecHit2D * rechit =
00309 dynamic_cast<const ProjectedSiStripRecHit2D *>(&thit))
00310 {
00311 simtrackid = associateProjectedRecHit(rechit);
00312 }
00313 }
00314
00315 else if( (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelBarrel ||
00316 (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelEndcap)
00317 {
00318 if(const SiPixelRecHit * rechit = dynamic_cast<const SiPixelRecHit *>(&thit))
00319 {
00320 simtrackid = associatePixelRecHit(rechit);
00321 }
00322 }
00323
00324 if(const SiTrackerGSRecHit2D * rechit = dynamic_cast<const SiTrackerGSRecHit2D *>(&thit))
00325 {
00326 simtrackid = associateGSRecHit(rechit);
00327 }
00328 if(const SiTrackerGSMatchedRecHit2D * rechit = dynamic_cast<const SiTrackerGSMatchedRecHit2D *>(&thit))
00329 {
00330 simtrackid = associateGSMatchedRecHit(rechit);
00331 }
00332
00333
00334 return simtrackid;
00335 }
00336
00337
00338 std::vector<SimHitIdpr> TrackerHitAssociator::associateSimpleRecHit(const SiStripRecHit2D * simplerechit)
00339 {
00340
00341 StripHits =true;
00342
00343 DetId detid= simplerechit->geographicalId();
00344 uint32_t detID = detid.rawId();
00345
00346
00347 std::vector<SimHitIdpr> cache_simtrackid;
00348 cache_simtrackid.clear();
00349
00350 std::map<SimHitIdpr, vector<float> > temp_simtrackid;
00351 temp_simtrackid.clear();
00352
00353 edm::DetSetVector<StripDigiSimLink>::const_iterator isearch = stripdigisimlink->find(detID);
00354 if(isearch != stripdigisimlink->end()) {
00355
00356 edm::DetSet<StripDigiSimLink> link_detset = (*stripdigisimlink)[detID];
00357
00358
00359 const SiStripCluster* clust = 0;
00360 if(simplerechit->cluster().isNonnull())
00361 {
00362 clust=&(*simplerechit->cluster());
00363 }else if(simplerechit->cluster_regional().isNonnull())
00364 {
00365 clust=&(*simplerechit->cluster_regional());
00366 }
00367 else
00368 {
00369 edm::LogError("TrackerHitAssociator")<<"no cluster reference attached";
00370 return simtrackid;
00371 }
00372
00373
00374
00375 int clusiz = clust->amplitudes().size();
00376 int first = clust->firstStrip();
00377 int last = first + clusiz;
00378
00379
00380
00381
00382 std::vector<SimHitIdpr> idcachev;
00383 std::vector<int> CFposcachev;
00384 for(edm::DetSet<StripDigiSimLink>::const_iterator linkiter = link_detset.data.begin(); linkiter != link_detset.data.end(); linkiter++){
00385 StripDigiSimLink link = *linkiter;
00386
00387 if( (int)(link.channel()) >= first && (int)(link.channel()) < last ){
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 SimHitIdpr currentId(link.SimTrackId(), link.eventId());
00399
00400
00401
00402
00403 if(find(idcachev.begin(),idcachev.end(),currentId ) == idcachev.end()){
00404
00405
00406
00407
00408
00409
00410 idcachev.push_back(currentId);
00411 simtrackid.push_back(currentId);
00412 }
00413
00414
00415
00416 int currentCFPos = link.CFposition()-1;
00417 if(find(CFposcachev.begin(),CFposcachev.end(),currentCFPos ) == CFposcachev.end()){
00418
00419
00420
00421
00422
00423
00424 CFposcachev.push_back(currentCFPos);
00425 simhitCFPos.push_back(currentCFPos);
00426
00427 }
00428 }
00429 }
00430 }
00431 return simtrackid;
00432
00433 }
00434
00435 std::vector<SimHitIdpr> TrackerHitAssociator::associateMatchedRecHit(const SiStripMatchedRecHit2D * matchedrechit)
00436 {
00437
00438 StripHits = true;
00439
00440
00441 vector<SimHitIdpr> matched_mono;
00442 vector<SimHitIdpr> matched_st;
00443 matched_mono.clear();
00444 matched_st.clear();
00445
00446 const SiStripRecHit2D *mono = matchedrechit->monoHit();
00447 const SiStripRecHit2D *st = matchedrechit->stereoHit();
00448
00449 matched_mono = associateSimpleRecHit(mono);
00450 matched_st = associateSimpleRecHit(st);
00451
00452
00453 if(!matched_mono.empty() && !matched_st.empty()){
00454 simtrackid.clear();
00455
00456 std::vector<SimHitIdpr> idcachev;
00457
00458 for(vector<SimHitIdpr>::iterator mhit=matched_mono.begin(); mhit != matched_mono.end(); mhit++){
00459
00460 if(find(idcachev.begin(), idcachev.end(),(*mhit)) == idcachev.end()) {
00461 idcachev.push_back(*mhit);
00462
00463 if(find(matched_st.begin(), matched_st.end(),(*mhit))!=matched_st.end()){
00464 simtrackid.push_back(*mhit);
00465
00466 }
00467 }
00468 }
00469 }
00470 return simtrackid;
00471 }
00472
00473
00474 std::vector<SimHitIdpr> TrackerHitAssociator::associateProjectedRecHit(const ProjectedSiStripRecHit2D * projectedrechit)
00475 {
00476 StripHits = true;
00477
00478
00479
00480
00481 vector<SimHitIdpr> matched_mono;
00482 matched_mono.clear();
00483
00484 const SiStripRecHit2D mono = projectedrechit->originalHit();
00485 matched_mono = associateSimpleRecHit(&mono);
00486 return matched_mono;
00487 }
00488
00489
00490 std::vector<SimHitIdpr> TrackerHitAssociator::associatePixelRecHit(const SiPixelRecHit * pixelrechit)
00491 {
00492 StripHits = false;
00493
00494
00495
00496
00497 DetId detid= pixelrechit->geographicalId();
00498 uint32_t detID = detid.rawId();
00499 edm::DetSetVector<PixelDigiSimLink>::const_iterator isearch = pixeldigisimlink->find(detID);
00500 if(isearch != pixeldigisimlink->end()) {
00501 edm::DetSet<PixelDigiSimLink> link_detset = (*pixeldigisimlink)[detID];
00502
00503 SiPixelRecHit::ClusterRef const& cluster = pixelrechit->cluster();
00504
00505
00506
00507 if(pixelrechit->cluster().isNull()){
00508 edm::LogError("TrackerHitAssociator")<<"no Pixel cluster reference attached";
00509 return simtrackid;
00510 }
00511
00512
00513
00514 int minPixelRow = (*cluster).minPixelRow();
00515 int maxPixelRow = (*cluster).maxPixelRow();
00516 int minPixelCol = (*cluster).minPixelCol();
00517 int maxPixelCol = (*cluster).maxPixelCol();
00518
00519
00520 edm::DetSet<PixelDigiSimLink>::const_iterator linkiter = link_detset.data.begin();
00521 int dsl = 0;
00522
00523 std::vector<SimHitIdpr> idcachev;
00524 for( ; linkiter != link_detset.data.end(); linkiter++) {
00525 dsl++;
00526 std::pair<int,int> pixel_coord = PixelDigi::channelToPixel(linkiter->channel());
00527
00528 if( pixel_coord.first <= maxPixelRow &&
00529 pixel_coord.first >= minPixelRow &&
00530 pixel_coord.second <= maxPixelCol &&
00531 pixel_coord.second >= minPixelCol ) {
00532
00533
00534 SimHitIdpr currentId(linkiter->SimTrackId(), linkiter->eventId());
00535
00536 if(find(idcachev.begin(),idcachev.end(),currentId) == idcachev.end()){
00537
00538
00539 simtrackid.push_back(currentId);
00540 idcachev.push_back(currentId);
00541 }
00542 }
00543 }
00544 }
00545
00546 return simtrackid;
00547 }
00548
00549 std::vector<SimHitIdpr> TrackerHitAssociator::associateGSRecHit(const SiTrackerGSRecHit2D * gsrechit)
00550 {
00551 StripHits = false;
00552
00553
00554 vector<SimHitIdpr> simtrackid;
00555 simtrackid.clear();
00556 SimHitIdpr currentId(gsrechit->simtrackId(), EncodedEventId(gsrechit->eeId()));
00557 simtrackid.push_back(currentId);
00558 return simtrackid;
00559 }
00560
00561 std::vector<PSimHit> TrackerHitAssociator::associateMultiRecHit(const SiTrackerMultiRecHit * multirechit){
00562 std::vector<const TrackingRecHit*> componenthits = multirechit->recHits();
00563 std::vector<PSimHit> assimhits;
00564 std::vector<const TrackingRecHit*>::const_iterator iter;
00565 for (iter = componenthits.begin(); iter != componenthits.end(); iter ++){
00566 std::vector<PSimHit> asstocurrent = associateHit(**iter);
00567 assimhits.insert(assimhits.end(), asstocurrent.begin(), asstocurrent.end());
00568 }
00569
00570 return assimhits;
00571 }
00572
00573 std::vector<SimHitIdpr> TrackerHitAssociator::associateMultiRecHitId(const SiTrackerMultiRecHit * multirechit){
00574
00575 std::vector<const TrackingRecHit*> componenthits = multirechit->recHits();
00576 std::vector<SimHitIdpr> assimhits;
00577 std::vector<const TrackingRecHit*>::const_iterator iter;
00578 for (iter = componenthits.begin(); iter != componenthits.end(); iter ++){
00579 std::vector<SimHitIdpr> asstocurrent = associateHitId(**iter);
00580 assimhits.insert(assimhits.end(), asstocurrent.begin(), asstocurrent.end());
00581 }
00582
00583 return assimhits;
00584 }
00585
00586 std::vector<SimHitIdpr> TrackerHitAssociator::associateGSMatchedRecHit(const SiTrackerGSMatchedRecHit2D * gsmrechit)
00587 {
00588 StripHits = false;
00589
00590
00591 vector<SimHitIdpr> simtrackid;
00592 simtrackid.clear();
00593 SimHitIdpr currentId(gsmrechit->simtrackId(), EncodedEventId(gsmrechit->eeId()));
00594 simtrackid.push_back(currentId);
00595 return simtrackid;
00596 }
00597