CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackerHitAssociator.cc
Go to the documentation of this file.
1 // File: TrackerHitAssociator.cc
2 
3 #include <memory>
4 #include <string>
5 #include <vector>
6 
8 //--- for Geometry:
13 
15 
16 //for accumulate
17 #include <numeric>
18 #include <iostream>
19 
20 using namespace std;
21 using namespace edm;
22 
23 //
24 // Constructor for Config helper class, using default parameters
25 //
27  doPixel_(true),
28  doStrip_(true),
29  doTrackAssoc_(false),
30  assocHitbySimTrack_(false) {
31 
32  if(doStrip_) stripToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(edm::InputTag("simSiStripDigis"));
33  if(doPixel_) pixelToken_ = iC.consumes<edm::DetSetVector<PixelDigiSimLink> >(edm::InputTag("simSiPixelDigis"));
34  if(!doTrackAssoc_) {
35  std::vector<std::string> trackerContainers;
36  trackerContainers.reserve(12);
37  trackerContainers.emplace_back("g4SimHitsTrackerHitsTIBLowTof");
38  trackerContainers.emplace_back("g4SimHitsTrackerHitsTIBHighTof");
39  trackerContainers.emplace_back("g4SimHitsTrackerHitsTIDLowTof");
40  trackerContainers.emplace_back("g4SimHitsTrackerHitsTIDHighTof");
41  trackerContainers.emplace_back("g4SimHitsTrackerHitsTOBLowTof");
42  trackerContainers.emplace_back("g4SimHitsTrackerHitsTOBHighTof");
43  trackerContainers.emplace_back("g4SimHitsTrackerHitsTECLowTof");
44  trackerContainers.emplace_back("g4SimHitsTrackerHitsTECHighTof");
45  trackerContainers.emplace_back("g4SimHitsTrackerHitsPixelBarrelLowTof");
46  trackerContainers.emplace_back("g4SimHitsTrackerHitsPixelBarrelHighTof");
47  trackerContainers.emplace_back("g4SimHitsTrackerHitsPixelEndcapLowTof");
48  trackerContainers.emplace_back("g4SimHitsTrackerHitsPixelEndcapHighTof");
49  cfTokens_.reserve(trackerContainers.size());
50  simHitTokens_.reserve(trackerContainers.size());
51  for(auto const& trackerContainer : trackerContainers) {
52  cfTokens_.push_back(iC.consumes<CrossingFrame<PSimHit> >(edm::InputTag("mix", trackerContainer)));
53  simHitTokens_.push_back(iC.consumes<std::vector<PSimHit> >(edm::InputTag("g4SimHits", trackerContainer)));
54  }
55  }
56 }
57 
58 //
59 // Constructor for Config helper class, using configured parameters
60 //
62  doPixel_( conf.getParameter<bool>("associatePixel") ),
63  doStrip_( conf.getParameter<bool>("associateStrip") ),
64  doTrackAssoc_( conf.getParameter<bool>("associateRecoTracks") ),
65  assocHitbySimTrack_(conf.existsAs<bool>("associateHitbySimTrack") ? conf.getParameter<bool>("associateHitbySimTrack") : false) {
66 
67  if(doStrip_) stripToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(conf.getParameter<edm::InputTag>("stripSimLinkSrc"));
68  if(doPixel_) pixelToken_ = iC.consumes<edm::DetSetVector<PixelDigiSimLink> >(conf.getParameter<edm::InputTag>("pixelSimLinkSrc"));
69  if(!doTrackAssoc_) {
70  std::vector<std::string> trackerContainers(conf.getParameter<std::vector<std::string> >("ROUList"));
71  cfTokens_.reserve(trackerContainers.size());
72  simHitTokens_.reserve(trackerContainers.size());
73  for(auto const& trackerContainer : trackerContainers) {
74  cfTokens_.push_back(iC.consumes<CrossingFrame<PSimHit> >(edm::InputTag("mix", trackerContainer)));
75  simHitTokens_.push_back(iC.consumes<std::vector<PSimHit> >(edm::InputTag("g4SimHits", trackerContainer)));
76  }
77  }
78  }
79 
80 //
81 // Constructor supporting consumes interface
82 //
84  doPixel_(config.doPixel_),
85  doStrip_(config.doStrip_),
88  //if track association there is no need to access the input collections
89  if(!doTrackAssoc_) {
90  makeMaps(e, config);
91  }
92 
95 }
96 
98  // Step A: Get Inputs
99  // The collections are specified via ROUList in the configuration, and can
100  // be either crossing frames (e.g., mix/g4SimHitsTrackerHitsTIBLowTof)
101  // or just PSimHits (e.g., g4SimHits/TrackerHitsTIBLowTof)
102  if (assocHitbySimTrack_) {
103  for(auto const& cfToken : config.cfTokens_) {
105  //int Nhits = 0;
106  if (theEvent.getByToken(cfToken, cf_simhit)) {
107  std::unique_ptr<MixCollection<PSimHit> > thisContainerHits(new MixCollection<PSimHit>(cf_simhit.product()));
108  for (auto const& isim : *thisContainerHits) {
109  DetId theDet(isim.detUnitId());
110  SimHitMap[theDet].push_back(isim);
111  // ++Nhits;
112  }
113  // std::cout << "simHits from crossing frames; map size = " << SimHitCollMap.size() << ", Hit count = " << Nhits << std::endl;
114  }
115  }
116  for(auto const& simHitToken : config.simHitTokens_) {
118  //int Nhits = 0;
119  if(theEvent.getByToken(simHitToken, simHits)) {
120  for (auto const& isim : *simHits) {
121  DetId theDet(isim.detUnitId());
122  SimHitMap[theDet].push_back(isim);
123  //++Nhits;
124  }
125  // std::cout << "simHits from prompt collections; map size = " << SimHitCollMap.size() << ", Hit count = " << Nhits << std::endl;
126  }
127  }
128  } else {
129  const char* const highTag = "HighTof";
130  unsigned int tofBin;
132  simHitCollectionID theSimHitCollID;
133  for(auto const& cfToken : config.cfTokens_) {
135  //int Nhits = 0;
136  if (theEvent.getByToken(cfToken, cf_simhit)) {
137  std::unique_ptr<MixCollection<PSimHit> > thisContainerHits(new MixCollection<PSimHit>(cf_simhit.product()));
138  theEvent.labelsForToken(cfToken, labels);
139  if(std::strstr(labels.productInstance, highTag) != NULL) {
140  tofBin = StripDigiSimLink::HighTof;
141  } else {
142  tofBin = StripDigiSimLink::LowTof;
143  }
144  for (auto const& isim : *thisContainerHits) {
145  DetId theDet(isim.detUnitId());
146  theSimHitCollID = std::make_pair(theDet.subdetId(), tofBin);
147  SimHitCollMap[theSimHitCollID].push_back(isim);
148  //++Nhits;
149  }
150  // std::cout << "simHits from crossing frames; map size = " << SimHitCollMap.size() << ", Hit count = " << Nhits << std::endl;
151  }
152  }
153  for(auto const& simHitToken : config.simHitTokens_) {
155  //int Nhits = 0;
156  if(theEvent.getByToken(simHitToken, simHits)) {
157  theEvent.labelsForToken(simHitToken, labels);
158  if(std::strstr(labels.productInstance, highTag) != NULL) {
159  tofBin = StripDigiSimLink::HighTof;
160  } else {
161  tofBin = StripDigiSimLink::LowTof;
162  }
163  for (auto const& isim : *simHits) {
164  DetId theDet(isim.detUnitId());
165  theSimHitCollID = std::make_pair(theDet.subdetId(), tofBin);
166  SimHitCollMap[theSimHitCollID].push_back(isim);
167  //++Nhits;
168  }
169  // std::cout << "simHits from prompt collections; map size = " << SimHitCollMap.size() << ", Hit count = " << Nhits << std::endl;
170  }
171  }
172  }
173 }
174 
175 std::vector<PSimHit> TrackerHitAssociator::associateHit(const TrackingRecHit & thit) const
176 {
177 
178  if (const SiTrackerMultiRecHit * rechit = dynamic_cast<const SiTrackerMultiRecHit *>(&thit)){
179  return associateMultiRecHit(rechit);
180  }
181 
182  //vector with the matched SimHit
183  std::vector<PSimHit> result;
184 
185  if(doTrackAssoc_) return result;
186 
187  // Vectors to contain lists of matched simTracks, simHits
188  std::vector<SimHitIdpr> simtrackid;
189  std::vector<simhitAddr> simhitCFPos;
190 
191  //get the Detector type of the rechit
192  DetId detid= thit.geographicalId();
193  uint32_t detID = detid.rawId();
194 
195  // Get the vectors of simtrackIDs and simHit indices associated with this rechit
196  associateHitId(thit, simtrackid, &simhitCFPos);
197  // std::cout << "recHit subdet, detID = " << detid.subdetId() << ", " << detID << ", (bnch, evt, trk) = ";
198  // for (size_t i=0; i<simtrackid.size(); ++i)
199  // std::cout << ", (" << simtrackid[i].second.bunchCrossing() << ", "
200  // << simtrackid[i].second.event() << ", " << simtrackid[i].first << ")";
201  // std::cout << std::endl;
202 
203  // Get the vector of simHits associated with this rechit
204 
205  if (!assocHitbySimTrack_ && simhitCFPos.size() > 0) {
206  // We use the indices to the simHit collections taken
207  // from the DigiSimLinks and returned in simhitCFPos.
208  // simhitCFPos[i] contains the full address of the ith simhit:
209  // <collection, index> = <<subdet, tofBin>, index>
210 
211  //check if the recHit is a SiStripMatchedRecHit2D
212  if(dynamic_cast<const SiStripMatchedRecHit2D *>(&thit)) {
213  for(auto const& theSimHitAddr : simhitCFPos) {
214  simHitCollectionID theSimHitCollID = theSimHitAddr.first;
215  simhit_collectionMap::const_iterator it = SimHitCollMap.find(theSimHitCollID);
216  if (it!= SimHitCollMap.end()) {
217  unsigned int theSimHitIndex = theSimHitAddr.second;
218  if (theSimHitIndex < (it->second).size()) {
219  const PSimHit& theSimHit = (it->second)[theSimHitIndex];
220  // Try to remove ghosts by requiring a match to the simTrack also
221  unsigned int simHitid = theSimHit.trackId();
222  EncodedEventId simHiteid = theSimHit.eventId();
223  for(auto const& id : simtrackid) {
224  if(simHitid == id.first && simHiteid == id.second) {
225  result.push_back(theSimHit);
226  }
227  }
228  // std::cout << "by CFpos, simHit detId = " << theSimHit.detUnitId() << " address = (" << (theSimHitAddr.first).first
229  // << ", " << (theSimHitAddr.first).second << ", " << theSimHitIndex
230  // << "), process = " << theSimHit.processType() << " (" << theSimHit.eventId().bunchCrossing()
231  // << ", " << theSimHit.eventId().event() << ", " << theSimHit.trackId() << ")" << std::endl;
232  }
233  }
234  }
235  } else {
236  for(auto const& theSimHitAddr : simhitCFPos) {
237  simHitCollectionID theSimHitCollID = theSimHitAddr.first;
238  simhit_collectionMap::const_iterator it = SimHitCollMap.find(theSimHitCollID);
239  if (it!= SimHitCollMap.end()) {
240  unsigned int theSimHitIndex = theSimHitAddr.second;
241  if (theSimHitIndex < (it->second).size()) {
242  result.push_back((it->second)[theSimHitIndex]);
243  // std::cout << "by CFpos, simHit detId = " << theSimHit.detUnitId() << " address = (" << (theSimHitAddr.first).first
244  // << ", " << (theSimHitAddr.first).second << ", " << theSimHitIndex
245  // << "), process = " << theSimHit.processType() << " (" << theSimHit.eventId().bunchCrossing()
246  // << ", " << theSimHit.eventId().event() << ", " << theSimHit.trackId() << ")" << std::endl;
247  }
248  }
249  }
250  }
251  return result;
252  }
253 
254  // Get the SimHit from the trackid instead
255  std::map<unsigned int, std::vector<PSimHit> >::const_iterator it = SimHitMap.find(detID);
256  if (it!= SimHitMap.end()) {
257  vector<PSimHit>::const_iterator simHitIter = (it->second).begin();
258  vector<PSimHit>::const_iterator simHitIterEnd = (it->second).end();
259  for (;simHitIter != simHitIterEnd; ++simHitIter) {
260  const PSimHit& ihit = *simHitIter;
261  unsigned int simHitid = ihit.trackId();
262  EncodedEventId simHiteid = ihit.eventId();
263  // std::cout << "by simTk, simHit, process = " << ihit.processType() << " (" << ihit.eventId().bunchCrossing()
264  // << ", " << ihit.eventId().event() << ", " << ihit.trackId() << ")";
265  for(auto id : simtrackid) {
266  if(simHitid == id.first && simHiteid == id.second) {
267 // cout << "Associator ---> ID" << ihit.trackId() << " Simhit x= " << ihit.localPosition().x()
268 // << " y= " << ihit.localPosition().y() << " z= " << ihit.localPosition().x() << endl;
269  // std::cout << " matches";
270  result.push_back(ihit);
271  break;
272  }
273  }
274  // std::cout << std::endl;
275  }
276 
277  }else{
278 
280  std::map<unsigned int, std::vector<PSimHit> >::const_iterator itrphi =
281  SimHitMap.find(detID+2); //iterator to the simhit in the rphi module
282  std::map<unsigned int, std::vector<PSimHit> >::const_iterator itster =
283  SimHitMap.find(detID+1);//iterator to the simhit in the stereo module
284  if (itrphi!= SimHitMap.end()&&itster!=SimHitMap.end()) {
285  std::vector<PSimHit> simHitVector = itrphi->second;
286  simHitVector.insert(simHitVector.end(),(itster->second).begin(),(itster->second).end());
287  vector<PSimHit>::const_iterator simHitIter = simHitVector.begin();
288  vector<PSimHit>::const_iterator simHitIterEnd = simHitVector.end();
289  for (;simHitIter != simHitIterEnd; ++simHitIter) {
290  const PSimHit& ihit = *simHitIter;
291  unsigned int simHitid = ihit.trackId();
292  EncodedEventId simHiteid = ihit.eventId();
293  for(auto const& id : simtrackid) {
294  if(simHitid == id.first && simHiteid == id.second) {
295 // if(simHitid == simtrackid[i].first && simHiteid.bunchCrossing() == simtrackid[i].second.bunchCrossing()) {
296  // cout << "GluedDet Associator ---> ID" << ihit.trackId() << " Simhit x= " << ihit.localPosition().x()
297  // << " y= " << ihit.localPosition().y() << " z= " << ihit.localPosition().x() << endl;
298  result.push_back(ihit);
299  break;
300  }
301  }
302  }
303  }
304  }
305 
306  return result;
307 }
308 
309 std::vector< SimHitIdpr > TrackerHitAssociator::associateHitId(const TrackingRecHit & thit) const
310 {
311  std::vector< SimHitIdpr > simhitid;
312  associateHitId(thit, simhitid);
313  return simhitid;
314 }
315 
316 void TrackerHitAssociator::associateHitId(const TrackingRecHit & thit, std::vector< SimHitIdpr > & simtkid,
317  std::vector<simhitAddr>* simhitCFPos) const
318 {
319 
320  simtkid.clear();
321 
322  //get the Detector type of the rechit
323  DetId detid= thit.geographicalId();
324  if (const SiTrackerMultiRecHit * rechit = dynamic_cast<const SiTrackerMultiRecHit *>(&thit)){
325  simtkid=associateMultiRecHitId(rechit, simhitCFPos);
326  }
327 
328  //cout << "Associator ---> get Detid " << detID << endl;
329  //check we are in the strip tracker
330  if(detid.subdetId() == StripSubdetector::TIB ||
331  detid.subdetId() == StripSubdetector::TOB ||
332  detid.subdetId() == StripSubdetector::TID ||
333  detid.subdetId() == StripSubdetector::TEC)
334  {
335  //check if it is a simple SiStripRecHit2D
336  if(const SiStripRecHit2D * rechit =
337  dynamic_cast<const SiStripRecHit2D *>(&thit))
338  {
339  associateSiStripRecHit(rechit, simtkid, simhitCFPos);
340  }
341  //check if it is a SiStripRecHit1D
342  else if(const SiStripRecHit1D * rechit =
343  dynamic_cast<const SiStripRecHit1D *>(&thit))
344  {
345  associateSiStripRecHit(rechit, simtkid, simhitCFPos);
346  }
347  //check if it is a SiStripMatchedRecHit2D
348  else if(const SiStripMatchedRecHit2D * rechit =
349  dynamic_cast<const SiStripMatchedRecHit2D *>(&thit))
350  {
351  simtkid = associateMatchedRecHit(rechit, simhitCFPos);
352  }
353  //check if it is a ProjectedSiStripRecHit2D
354  else if(const ProjectedSiStripRecHit2D * rechit =
355  dynamic_cast<const ProjectedSiStripRecHit2D *>(&thit))
356  {
357  simtkid = associateProjectedRecHit(rechit, simhitCFPos);
358  }
359  else{
360  //std::cout << "associate to invalid" << std::endl;
361  //throw cms::Exception("Unknown RecHit Type") << "TrackerHitAssociator failed second casting of " << typeid(thit).name() << " type ";
362  }
363  }
364  //check we are in the pixel tracker
365  else if( (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelBarrel ||
366  (unsigned int)(detid.subdetId()) == PixelSubdetector::PixelEndcap)
367  {
368  if(const SiPixelRecHit * rechit = dynamic_cast<const SiPixelRecHit *>(&thit))
369  {
370  associatePixelRecHit(rechit, simtkid, simhitCFPos);
371  }
372  }
373  //check if these are GSRecHits (from FastSim)
374  if(trackerHitRTTI::isFast(thit))
375  {
376  simtkid = associateFastRecHit(static_cast<const FastTrackerRecHit *>(&thit));
377  }
378 }
379 
380 template<typename T>
381 inline void TrackerHitAssociator::associateSiStripRecHit(const T *simplerechit, std::vector<SimHitIdpr>& simtrackid, std::vector<simhitAddr>* simhitCFPos) const
382 {
383  const SiStripCluster* clust = &(*simplerechit->cluster());
384  associateSimpleRecHitCluster(clust, simplerechit->geographicalId(), simtrackid, simhitCFPos);
385 }
386 
387 //
388 // Method for obtaining simTracks and simHits from a cluster
389 //
391  const DetId& detid,
392  std::vector<SimHitIdpr>& simtrackid,
393  std::vector<PSimHit>& simhit) const {
394  std::vector<simhitAddr> simhitCFPos;
395  associateSimpleRecHitCluster(clust, detid, simtrackid, &simhitCFPos);
396 
397  for(auto const& theSimHitAddr : simhitCFPos ) {
398  simHitCollectionID theSimHitCollID = theSimHitAddr.first;
399  simhit_collectionMap::const_iterator it = SimHitCollMap.find(theSimHitCollID);
400  if (it!= SimHitCollMap.end()) {
401  unsigned int theSimHitIndex = theSimHitAddr.second;
402  if (theSimHitIndex < (it->second).size()) simhit.push_back((it->second)[theSimHitIndex]);
403  // const PSimHit& theSimHit = (it->second)[theSimHitIndex];
404  // std::cout << "For cluster, simHit detId = " << theSimHit.detUnitId() << " address = (" << (theSimHitAddr.first).first
405  // << ", " << (theSimHitAddr.first).second << ", " << theSimHitIndex
406  // << "), process = " << theSimHit.processType() << " (" << theSimHit.eventId().bunchCrossing()
407  // << ", " << theSimHit.eventId().event() << ", " << theSimHit.trackId() << ")" << std::endl;
408  }
409  }
410 }
411 
413  const DetId& detid,
414  std::vector<SimHitIdpr>& simtrackid,
415  std::vector<simhitAddr>* simhitCFPos) const {
416 
417  uint32_t detID = detid.rawId();
419  if(isearch != stripdigisimlink->end()) { //if it is not empty
420  edm::DetSet<StripDigiSimLink> link_detset = (*isearch);
421 
422  if(clust!=0){//the cluster is valid
423  int clusiz = clust->amplitudes().size();
424  int first = clust->firstStrip();
425  int last = first + clusiz;
426 
427 // std::cout << "CLUSTERSIZE " << clusiz << " first strip = " << first << " last strip = " << last-1 << std::endl;
428 // std::cout << " detID = " << detID << " DETSET size = " << link_detset.data.size() << std::endl;
429  //use a vector
430  std::vector<SimHitIdpr> idcachev;
431  std::vector<simhitAddr> CFposcachev;
432  int channel;
433 // for(auto linkiter : link_detset.data){
434  for(edm::DetSet<StripDigiSimLink>::const_iterator linkiter = link_detset.data.begin(), linkerEnd = link_detset.data.end(); linkiter != linkerEnd; ++linkiter){
435  channel = (int)(linkiter->channel());
436  if( channel >= first && channel < last ){
437 
438  //check this digisimlink
439 // printf("%s%4d%s%8d%s%3d%s%8.4f\n", "CHANNEL = ", linkiter->channel(), " TrackID = ", linkiter->SimTrackId(),
440 // " tofBin = ", linkiter->TofBin(), " fraction = ", linkiter->fraction());
441  /*
442  std::cout << "CHECKING CHANNEL = " << linkiter->channel() << std::endl;
443  std::cout << "TrackID = " << linkiter->SimTrackId() << std::endl;
444  std::cout << "Position = " << linkiter->CFposition() << std::endl;
445  std::cout << " fraction = " << linkiter->fraction() << std::endl;
446  */
447 
448  SimHitIdpr currentId(linkiter->SimTrackId(), linkiter->eventId());
449 
450  //create a vector with the list of SimTrack ID's of the tracks that contributed to the RecHit
451  //write the id only once in the vector
452 
453  if(find(idcachev.begin(),idcachev.end(),currentId ) == idcachev.end()){
454  /*
455  std::cout << " Adding track id = " << currentId.first
456  << " Event id = " << currentId.second.event()
457  << " Bunch Xing = " << currentId.second.bunchCrossing()
458  << std::endl;
459  */
460  idcachev.push_back(currentId);
461  simtrackid.push_back(currentId);
462  }
463 
464  if (simhitCFPos != 0) {
465  //create a vector that contains all the position (in the MixCollection) of the SimHits that contributed to the RecHit
466  //write position only once
467  unsigned int currentCFPos = linkiter->CFposition();
468  unsigned int tofBin = linkiter->TofBin();
469  simHitCollectionID theSimHitCollID = std::make_pair(detid.subdetId(), tofBin);
470  simhitAddr currentAddr = std::make_pair(theSimHitCollID, currentCFPos);
471 
472  if(find(CFposcachev.begin(), CFposcachev.end(), currentAddr ) == CFposcachev.end()) {
473 // std::cout << "CHECKING CHANNEL = " << linkiter->channel() << std::endl;
474 // std::cout << "\tTrackID = " << linkiter->SimTrackId() << "\tCFPos = " << currentCFPos <<"\ttofBin = " << tofBin << std::endl;
475 // simhit_collectionMap::const_iterator it = SimHitCollMap.find(theSimHitCollID);
476 // if (it!= SimHitCollMap.end()) {
477 // PSimHit theSimHit = it->second[currentCFPos];
478 // std::cout << "\tLocal Pos = " << theSimHit.localPosition()
479 // << "\tProcess = " << theSimHit.processType() << std::endl;
480 // }
481  CFposcachev.push_back(currentAddr);
482  simhitCFPos->push_back(currentAddr);
483  }
484  }
485  }
486  }
487  }
488  else {
489  edm::LogError("TrackerHitAssociator")<<"no cluster reference attached";
490  }
491  }
492 }
493 
494 std::vector<SimHitIdpr> TrackerHitAssociator::associateMatchedRecHit(const SiStripMatchedRecHit2D* matchedrechit, std::vector<simhitAddr>* simhitCFPos) const
495 {
496  std::vector<SimHitIdpr> matched_mono;
497  std::vector<SimHitIdpr> matched_st;
498 
499  const SiStripRecHit2D mono = matchedrechit->monoHit();
500  const SiStripRecHit2D st = matchedrechit->stereoHit();
501  //associate the two simple hits separately
502  associateSiStripRecHit(&mono, matched_mono, simhitCFPos);
503  associateSiStripRecHit(&st, matched_st, simhitCFPos);
504 
505  //save in a vector all the simtrack-id's that are common to mono and stereo hits
506  std::vector<SimHitIdpr> simtrackid;
507  if(!(matched_mono.empty() || matched_st.empty())){
508  std::vector<SimHitIdpr> idcachev;
509  for(auto const& mhit: matched_mono){
510  //save only once the ID
511  if(find(idcachev.begin(), idcachev.end(), mhit) == idcachev.end()) {
512  idcachev.push_back(mhit);
513  //save if the stereoID matched the monoID
514  if(find(matched_st.begin(), matched_st.end(), mhit) != matched_st.end()) {
515  simtrackid.push_back(mhit);
516  }
517  }
518  }
519  }
520 
521  return simtrackid;
522 }
523 
524 
525 std::vector<SimHitIdpr> TrackerHitAssociator::associateProjectedRecHit(const ProjectedSiStripRecHit2D * projectedrechit,
526  std::vector<simhitAddr>* simhitCFPos) const
527 {
528  //projectedRecHit is a "matched" rechit with only one component
529 
530  std::vector<SimHitIdpr> matched_mono;
531 
532  const SiStripRecHit2D mono = projectedrechit->originalHit();
533  associateSiStripRecHit(&mono, matched_mono, simhitCFPos);
534  return matched_mono;
535 }
536 
538  std::vector<SimHitIdpr> & simtrackid,
539  std::vector<simhitAddr>* simhitCFPos) const
540 {
541  //
542  // Pixel associator
543  //
544  DetId detid= pixelrechit->geographicalId();
545  uint32_t detID = detid.rawId();
546 
548  if(isearch != pixeldigisimlink->end()) { //if it is not empty
549  edm::DetSet<PixelDigiSimLink> link_detset = (*isearch);
550  SiPixelRecHit::ClusterRef const& cluster = pixelrechit->cluster();
551 
552  //check the reference is valid
553 
554  if(!(cluster.isNull())){//if the cluster is valid
555 
556  int minPixelRow = (*cluster).minPixelRow();
557  int maxPixelRow = (*cluster).maxPixelRow();
558  int minPixelCol = (*cluster).minPixelCol();
559  int maxPixelCol = (*cluster).maxPixelCol();
560  //std::cout << " Cluster minRow " << minPixelRow << " maxRow " << maxPixelRow << std::endl;
561  //std::cout << " Cluster minCol " << minPixelCol << " maxCol " << maxPixelCol << std::endl;
562  edm::DetSet<PixelDigiSimLink>::const_iterator linkiter = link_detset.data.begin(), linkEnd = link_detset.data.end();
563  int dsl = 0;
564  std::vector<SimHitIdpr> idcachev;
565  std::vector<simhitAddr> CFposcachev;
566  for( ; linkiter != linkEnd; ++linkiter) {
567  ++dsl;
568  std::pair<int,int> pixel_coord = PixelDigi::channelToPixel(linkiter->channel());
569  //std::cout << " " << dsl << ") Digi link: row " << pixel_coord.first << " col " << pixel_coord.second << std::endl;
570  if( pixel_coord.first <= maxPixelRow &&
571  pixel_coord.first >= minPixelRow &&
572  pixel_coord.second <= maxPixelCol &&
573  pixel_coord.second >= minPixelCol ) {
574  //std::cout << " !-> trackid " << linkiter->SimTrackId() << endl;
575  //std::cout << " fraction " << linkiter->fraction() << endl;
576  SimHitIdpr currentId(linkiter->SimTrackId(), linkiter->eventId());
577  if(find(idcachev.begin(),idcachev.end(),currentId) == idcachev.end()){
578  simtrackid.push_back(currentId);
579  idcachev.push_back(currentId);
580  }
581 
582  if (simhitCFPos != 0) {
583  //create a vector that contains all the position (in the MixCollection) of the SimHits that contributed to the RecHit
584  //write position only once
585  unsigned int currentCFPos = linkiter->CFposition();
586  unsigned int tofBin = linkiter->TofBin();
587  simHitCollectionID theSimHitCollID = std::make_pair(detid.subdetId(), tofBin);
588  simhitAddr currentAddr = std::make_pair(theSimHitCollID, currentCFPos);
589 
590  if(find(CFposcachev.begin(), CFposcachev.end(), currentAddr) == CFposcachev.end()) {
591  CFposcachev.push_back(currentAddr);
592  simhitCFPos->push_back(currentAddr);
593  }
594  }
595 
596  }
597  }
598  }
599  else{
600  edm::LogError("TrackerHitAssociator")<<"no Pixel cluster reference attached";
601 
602  }
603  }
604 }
605 
606 std::vector<PSimHit> TrackerHitAssociator::associateMultiRecHit(const SiTrackerMultiRecHit * multirechit) const{
607  std::vector<const TrackingRecHit*> componenthits = multirechit->recHits();
608  // std::vector<PSimHit> assimhits;
609  int size=multirechit->weights().size(), idmostprobable=0;
610 
611  for (int i=0; i<size; ++i){
612  if(multirechit->weight(i)>multirechit->weight(idmostprobable)) idmostprobable=i;
613  }
614 
615  return associateHit(*componenthits[idmostprobable]);
616 }
617 
618 std::vector<SimHitIdpr> TrackerHitAssociator::associateMultiRecHitId(const SiTrackerMultiRecHit * multirechit, std::vector<simhitAddr>* simhitCFPos) const{
619  std::vector<const TrackingRecHit*> componenthits = multirechit->recHits();
620  int size=multirechit->weights().size(), idmostprobable=0;
621 
622  for (int i=0; i<size; ++i){
623  if(multirechit->weight(i)>multirechit->weight(idmostprobable)) idmostprobable=i;
624  }
625 
626  std::vector< SimHitIdpr > simhitid;
627  associateHitId(*componenthits[idmostprobable], simhitid, simhitCFPos);
628  return simhitid;
629 }
630 
631  // fastsim
632 std::vector<SimHitIdpr> TrackerHitAssociator::associateFastRecHit(const FastTrackerRecHit * rechit) const
633 {
634  vector<SimHitIdpr> simtrackid;
635  simtrackid.clear();
636  for(size_t index =0, indexEnd = rechit->nSimTrackIds();index<indexEnd;++index){
637  SimHitIdpr currentId(rechit->simTrackId(index), EncodedEventId(rechit->simTrackEventId(index)));
638  simtrackid.push_back(currentId);
639  }
640  return simtrackid;
641 }
void labelsForToken(EDGetToken const &iToken, ProductLabels &oLabels) const
Definition: Event.h:227
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< SimHitIdpr > associateMultiRecHitId(const SiTrackerMultiRecHit *multirechit, std::vector< simhitAddr > *simhitCFPos=0) const
edm::Handle< edm::DetSetVector< StripDigiSimLink > > stripdigisimlink
std::vector< PSimHit > associateMultiRecHit(const SiTrackerMultiRecHit *multirechit) const
std::vector< edm::EDGetTokenT< CrossingFrame< PSimHit > > > cfTokens_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
void reserve(size_t s)
Definition: DetSetVector.h:150
std::pair< simHitCollectionID, unsigned int > simhitAddr
void associatePixelRecHit(const SiPixelRecHit *pixelrechit, std::vector< SimHitIdpr > &simhitid, std::vector< simhitAddr > *simhitCFPos=0) const
std::vector< SimHitIdpr > associateFastRecHit(const FastTrackerRecHit *rechit) const
#define NULL
Definition: scimark2.h:8
uint16_t firstStrip() const
virtual int32_t simTrackEventId(size_t i) const
edm::Handle< edm::DetSetVector< PixelDigiSimLink > > pixeldigisimlink
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
void associateSimpleRecHitCluster(const SiStripCluster *clust, const DetId &detid, std::vector< SimHitIdpr > &simtrackid, std::vector< simhitAddr > *simhitCFPos=0) const
edm::EDGetTokenT< edm::DetSetVector< PixelDigiSimLink > > pixelToken_
float weight(unsigned int i) const
tuple result
Definition: mps_fire.py:84
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
U second(std::pair< T, U > const &p)
virtual std::vector< const TrackingRecHit * > recHits() const
Access to component RecHits (if any)
void makeMaps(const edm::Event &theEvent, const Config &config)
std::vector< float > const & weights() const
virtual int32_t simTrackId(size_t i) const
EncodedEventId eventId() const
Definition: PSimHit.h:105
#define end
Definition: vmac.h:37
bool isFast(TrackingRecHit const &hit)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool isNull() const
Checks for null.
Definition: Ref.h:249
edm::EDGetTokenT< edm::DetSetVector< StripDigiSimLink > > stripToken_
std::vector< edm::EDGetTokenT< std::vector< PSimHit > > > simHitTokens_
simhit_collectionMap SimHitCollMap
SiStripRecHit2D originalHit() const
Definition: DetId.h:18
void associateCluster(const SiStripCluster *clust, const DetId &detid, std::vector< SimHitIdpr > &simtrackid, std::vector< PSimHit > &simhit) const
std::pair< uint32_t, EncodedEventId > SimHitIdpr
SiStripRecHit2D stereoHit() const
T const * product() const
Definition: Handle.h:81
ClusterRef cluster() const
Definition: SiPixelRecHit.h:49
static std::pair< int, int > channelToPixel(int ch)
Definition: PixelDigi.h:62
virtual size_t nSimTrackIds() const
tuple simHits
Definition: trackerHits.py:16
TrackerHitAssociator(const edm::Event &e, const Config &config)
SiStripRecHit2D monoHit() const
collection_type data
Definition: DetSet.h:78
std::vector< SimHitIdpr > associateHitId(const TrackingRecHit &thit) const
char const * productInstance
Definition: ProductLabels.h:6
#define begin
Definition: vmac.h:30
std::vector< SimHitIdpr > associateProjectedRecHit(const ProjectedSiStripRecHit2D *projectedrechit, std::vector< simhitAddr > *simhitCFPos=0) const
unsigned int trackId() const
Definition: PSimHit.h:102
std::vector< PSimHit > associateHit(const TrackingRecHit &thit) const
Config(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC)
DetId geographicalId() const
volatile std::atomic< bool > shutdown_flag false
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:104
long double T
std::vector< SimHitIdpr > associateMatchedRecHit(const SiStripMatchedRecHit2D *matchedrechit, std::vector< simhitAddr > *simhitCFPos=0) const
tuple size
Write out results.
void associateSiStripRecHit(const T *simplerechit, std::vector< SimHitIdpr > &simtrackid, std::vector< simhitAddr > *simhitCFPos=0) const
const std::vector< uint8_t > & amplitudes() const
std::pair< unsigned int, unsigned int > simHitCollectionID
Our base class.
Definition: SiPixelRecHit.h:23