CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTHitAssociator.cc
Go to the documentation of this file.
11 #include <string>
12 #include <sstream>
13 
14 using namespace std;
15 
16 // Constructor
18  edm::ConsumesCollector && iC) :
19  DTsimhitsTag(conf.getParameter<edm::InputTag>("DTsimhitsTag")),
20  DTsimhitsXFTag(conf.getParameter<edm::InputTag>("DTsimhitsXFTag")),
21  DTdigiTag(conf.getParameter<edm::InputTag>("DTdigiTag")),
22  DTdigisimlinkTag(conf.getParameter<edm::InputTag>("DTdigisimlinkTag")),
23  DTrechitTag(conf.getParameter<edm::InputTag>("DTrechitTag")),
24 
25  // nice printout of DT hits
26  dumpDT(conf.getParameter<bool>("dumpDT")),
27  // CrossingFrame used or not ?
28  crossingframe(conf.getParameter<bool>("crossingframe")),
29  // Event contain the DTDigiSimLink collection ?
30  links_exist(conf.getParameter<bool>("links_exist")),
31  // associatorByWire links to a RecHit all the "valid" SimHits on the same DT wire
32  associatorByWire(conf.getParameter<bool>("associatorByWire")),
33 
34  printRtS(true)
35 {
36 
37  if ( crossingframe) {
39  }
40  else if (!DTsimhitsTag.label().empty()) {
41  iC.consumes<edm::PSimHitContainer>(DTsimhitsTag);
42  }
43  iC.consumes<DTDigiCollection>(DTdigiTag);
45 
46  if ( dumpDT && printRtS ) {
47  iC.consumes<DTRecHitCollection>(DTrechitTag);
48  }
49 
50 }
51 
52 
54  // input collection labels
55  DTsimhitsTag(conf.getParameter<edm::InputTag>("DTsimhitsTag")),
56  DTsimhitsXFTag(conf.getParameter<edm::InputTag>("DTsimhitsXFTag")),
57  DTdigiTag(conf.getParameter<edm::InputTag>("DTdigiTag")),
58  DTdigisimlinkTag(conf.getParameter<edm::InputTag>("DTdigisimlinkTag")),
59  DTrechitTag(conf.getParameter<edm::InputTag>("DTrechitTag")),
60 
61  // nice printout of DT hits
62  dumpDT(conf.getParameter<bool>("dumpDT")),
63  // CrossingFrame used or not ?
64  crossingframe(conf.getParameter<bool>("crossingframe")),
65  // Event contain the DTDigiSimLink collection ?
66  links_exist(conf.getParameter<bool>("links_exist")),
67  // associatorByWire links to a RecHit all the "valid" SimHits on the same DT wire
68  associatorByWire(conf.getParameter<bool>("associatorByWire")),
69 
70  printRtS(true)
71 
72 {
73  initEvent(iEvent,iSetup);
74 }
75 
77 
78  LogTrace("DTHitAssociator") <<"DTHitAssociator constructor: dumpDT = "<<dumpDT
79  <<", crossingframe = "<<crossingframe<<", links_exist = "<<links_exist
80  <<", associatorByWire = "<<associatorByWire;
81 
82  if (!links_exist && !associatorByWire) {
83  edm::LogWarning("DTHitAssociator")<<"*** WARNING in DTHitAssociator::DTHitAssociator: associatorByWire reset to TRUE !"
84  <<" \t (missing DTDigiSimLinkCollection ?)";
85  associatorByWire = true;
86  }
87 
88  // need DT Geometry to discard hits for which the drift time parametrisation is not applicable
90  iSetup.get<MuonGeometryRecord>().get(muonGeom);
91 
92  // Get the DT SimHits from the event and map PSimHit by DTWireId
93  mapOfSimHit.clear();
94  bool takeHit(true);
95 
96  if (crossingframe) {
98  LogTrace("DTHitAssociator") <<"getting CrossingFrame<PSimHit> collection - "<<DTsimhitsXFTag;
99  iEvent.getByLabel(DTsimhitsXFTag,xFrame);
100  auto_ptr<MixCollection<PSimHit> >
101  DTsimhits( new MixCollection<PSimHit>(xFrame.product()) );
102  LogTrace("DTHitAssociator") <<"... size = "<<DTsimhits->size();
104  for (isimhit=DTsimhits->begin(); isimhit!= DTsimhits->end(); isimhit++) {
105  DTWireId wireid((*isimhit).detUnitId());
106  takeHit = SimHitOK(muonGeom, *isimhit);
107  mapOfSimHit[wireid].push_back(make_pair(*isimhit,takeHit));
108  }
109  }
110  else if (!DTsimhitsTag.label().empty()) {
112  LogTrace("DTHitAssociator") <<"getting PSimHit collection - "<<DTsimhitsTag;
113  iEvent.getByLabel(DTsimhitsTag,DTsimhits);
114  LogTrace("DTHitAssociator") <<"... size = "<<DTsimhits->size();
115  edm::PSimHitContainer::const_iterator isimhit;
116  for (isimhit=DTsimhits->begin(); isimhit!= DTsimhits->end(); isimhit++) {
117  DTWireId wireid((*isimhit).detUnitId());
118  takeHit = SimHitOK(muonGeom, *isimhit);
119  mapOfSimHit[wireid].push_back(make_pair(*isimhit,takeHit));
120  }
121  }
122 
123  // Get the DT Digi collection from the event
124  mapOfDigi.clear();
126  LogTrace("DTHitAssociator") <<"getting DTDigi collection - "<<DTdigiTag;
127  iEvent.getByLabel(DTdigiTag,digis);
128 
129  if (digis.isValid()) {
130  // Map DTDigi by DTWireId
131  for (DTDigiCollection::DigiRangeIterator detUnit=digis->begin(); detUnit !=digis->end(); ++detUnit) {
132  const DTLayerId& layerid = (*detUnit).first;
133  const DTDigiCollection::Range& range = (*detUnit).second;
134 
136  for (digi = range.first; digi != range.second; ++digi){
137  DTWireId wireid(layerid,(*digi).wire());
138  mapOfDigi[wireid].push_back(*digi);
139  }
140  }
141  } else {
142  LogTrace("DTHitAssociator") <<"... NO DTDigi collection found !";
143  }
144 
145  mapOfLinks.clear();
146  if (links_exist) {
147  // Get the DT DigiSimLink collection from the event and map DTDigiSimLink by DTWireId
149  LogTrace("DTHitAssociator") <<"getting DTDigiSimLink collection - "<<DTdigisimlinkTag;
150  iEvent.getByLabel(DTdigisimlinkTag,digisimlinks);
151 
152  for (DTDigiSimLinkCollection::DigiRangeIterator detUnit=digisimlinks->begin();
153  detUnit !=digisimlinks->end();
154  ++detUnit) {
155  const DTLayerId& layerid = (*detUnit).first;
156  const DTDigiSimLinkCollection::Range& range = (*detUnit).second;
157 
159  for (link=range.first; link!=range.second; ++link){
160  DTWireId wireid(layerid,(*link).wire());
161  mapOfLinks[wireid].push_back(*link);
162  }
163  }
164  }
165 
166  if(dumpDT && printRtS) {
167 
168  // Get the DT rechits from the event
170  LogTrace("DTHitAssociator") <<"getting DTRecHit1DPair collection - "<<DTrechitTag;
171  iEvent.getByLabel(DTrechitTag,DTrechits);
172  LogTrace("DTHitAssociator") <<"... size = "<<DTrechits->size();
173 
174  // map DTRecHit1DPair by DTWireId
175  mapOfRecHit.clear();
177  for(rechit=DTrechits->begin(); rechit!=DTrechits->end(); ++rechit) {
178  DTWireId wireid = (*rechit).wireId();
179  mapOfRecHit[wireid].push_back(*rechit);
180  }
181 
182  if (mapOfSimHit.end() != mapOfSimHit.begin()) {
183  edm::LogVerbatim("DTHitAssociator")<<"\n *** Dump DT PSimHit's ***";
184 
185  int jwire = 0;
186  int ihit = 0;
187 
188  for(SimHitMap::const_iterator mapIT=mapOfSimHit.begin();
189  mapIT!=mapOfSimHit.end();
190  ++mapIT , jwire++) {
191 
192  DTWireId wireid = (*mapIT).first;
193  for (vector<PSimHit_withFlag>::const_iterator hitIT = mapOfSimHit[wireid].begin();
194  hitIT != mapOfSimHit[wireid].end();
195  hitIT++ , ihit++) {
196  PSimHit hit = hitIT->first;
197  edm::LogVerbatim("DTHitAssociator")
198  <<"PSimHit "<<ihit <<", wire "<<wireid //<<", detID = "<<hit.detUnitId()
199  <<", SimTrack Id:"<<hit.trackId()<<"/Evt:(" <<hit.eventId().event()<<","<<hit.eventId().bunchCrossing()<<") "
200  <<", pdg = "<<hit.particleType()<<", procs = "<<hit.processType();
201  }
202  }
203  } else {
204  edm::LogVerbatim("DTHitAssociator")<<"\n *** There are NO DT PSimHit's ***";
205  }
206 
207  if(mapOfDigi.end() != mapOfDigi.begin()) {
208 
209  int jwire = 0;
210  int ihit = 0;
211 
212  for(DigiMap::const_iterator mapIT=mapOfDigi.begin(); mapIT!=mapOfDigi.end(); ++mapIT , jwire++) {
213  edm::LogVerbatim("DTHitAssociator")<<"\n *** Dump DT digis ***";
214 
215  DTWireId wireid = (*mapIT).first;
216  for (vector<DTDigi>::const_iterator hitIT = mapOfDigi[wireid].begin();
217  hitIT != mapOfDigi[wireid].end();
218  hitIT++ , ihit++) {
219  edm::LogVerbatim("DTHitAssociator")
220  <<"DTDigi "<<ihit<<", wire "<<wireid<<", number = "<<hitIT->number()<<", TDC counts = "<<hitIT->countsTDC();
221  }
222  }
223  } else {
224  LogTrace("DTHitAssociator")<<"\n *** There are NO DTDigi's ***";
225  }
226 
227  if (mapOfRecHit.end() != mapOfRecHit.begin())
228  edm::LogVerbatim("DTHitAssociator")<<"\n *** Analyze DTRecHitCollection by DTWireId ***";
229 
230  int iwire = 0;
231  for(RecHitMap::const_iterator mapIT=mapOfRecHit.begin();
232  mapIT!=mapOfRecHit.end();
233  ++mapIT , iwire++) {
234 
235  DTWireId wireid = (*mapIT).first;
236  edm::LogVerbatim("DTHitAssociator")<<"\n===================================================================";
237  edm::LogVerbatim("DTHitAssociator")<<"wire index = "<<iwire<<" *** DTWireId = "<<" ("<<wireid<<")";
238 
239  if(mapOfSimHit.find(wireid) != mapOfSimHit.end()) {
240  edm::LogVerbatim("DTHitAssociator")<<"\n"<<mapOfSimHit[wireid].size()<<" SimHits (PSimHit):";
241 
242  for (vector<PSimHit_withFlag>::const_iterator hitIT = mapOfSimHit[wireid].begin();
243  hitIT != mapOfSimHit[wireid].end();
244  ++hitIT) {
245  stringstream tId;
246  tId << (hitIT->first).trackId();
247  string simhitlog = "\t SimTrack Id = "+tId.str();
248  if (hitIT->second) simhitlog = simhitlog + "\t -VALID HIT-";
249  else simhitlog = simhitlog + "\t -not valid hit-";
250  edm::LogVerbatim("DTHitAssociator")<<simhitlog;
251  }
252  }
253 
254  if(mapOfLinks.find(wireid) != mapOfLinks.end()) {
255  edm::LogVerbatim("DTHitAssociator")<<"\n"<<mapOfLinks[wireid].size()<<" Links (DTDigiSimLink):";
256 
257  for (vector<DTDigiSimLink>::const_iterator hitIT = mapOfLinks[wireid].begin();
258  hitIT != mapOfLinks[wireid].end();
259  ++hitIT) {
260  edm::LogVerbatim("DTHitAssociator")
261  <<"\t digi number = "<<hitIT->number()<<", time = "<<hitIT->time()<<", SimTrackId = "<<hitIT->SimTrackId();
262  }
263  }
264 
265  if(mapOfDigi.find(wireid) != mapOfDigi.end()) {
266  edm::LogVerbatim("DTHitAssociator")<<"\n"<<mapOfDigi[wireid].size()<<" Digis (DTDigi):";
267  for (vector<DTDigi>::const_iterator hitIT = mapOfDigi[wireid].begin();
268  hitIT != mapOfDigi[wireid].end();
269  ++hitIT) {
270  edm::LogVerbatim("DTHitAssociator")<<"\t digi number = "<<hitIT->number()<<", time = "<<hitIT->time();
271  }
272  }
273 
274  edm::LogVerbatim("DTHitAssociator")<<"\n"<<(*mapIT).second.size()<<" RecHits (DTRecHit1DPair):";
275  for(vector<DTRecHit1DPair>::const_iterator vIT =(*mapIT).second.begin();
276  vIT !=(*mapIT).second.end();
277  ++vIT) {
278  edm::LogVerbatim("DTHitAssociator")<<"\t digi time = "<<vIT->digiTime();
279  }
280  }
281  }
282 }
283 // end of constructor
284 
285 std::vector<DTHitAssociator::SimHitIdpr> DTHitAssociator::associateHitId(const TrackingRecHit & hit) {
286 
287  std::vector<SimHitIdpr> simtrackids;
288  const TrackingRecHit * hitp = &hit;
289  const DTRecHit1D * dtrechit = dynamic_cast<const DTRecHit1D *>(hitp);
290 
291  if (dtrechit) {
292  simtrackids = associateDTHitId(dtrechit);
293  } else {
294  edm::LogWarning("DTHitAssociator")<<"*** WARNING in DTHitAssociator::associateHitId, null dynamic_cast !";
295  }
296  return simtrackids;
297 }
298 
299 std::vector<DTHitAssociator::SimHitIdpr> DTHitAssociator::associateDTHitId(const DTRecHit1D * dtrechit) {
300 
301  std::vector<SimHitIdpr> matched;
302 
303  DTWireId wireid = dtrechit->wireId();
304 
305  if(associatorByWire) {
306  // matching based on DTWireId : take only "valid" SimHits on that wire
307 
308  if(mapOfSimHit.find(wireid) != mapOfSimHit.end()) {
309  for (vector<PSimHit_withFlag>::const_iterator hitIT = mapOfSimHit[wireid].begin();
310  hitIT != mapOfSimHit[wireid].end();
311  ++hitIT) {
312 
313  bool valid_hit = hitIT->second;
314  PSimHit this_hit = hitIT->first;
315 
316  if (valid_hit) {
317  SimHitIdpr currentId(this_hit.trackId(), this_hit.eventId());
318  matched.push_back(currentId);
319  }
320  }
321  }
322  }
323 
324  else {
325  // matching based on DTDigiSimLink
326 
327  float theTime = dtrechit->digiTime();
328  int theNumber(-1);
329 
330  if (mapOfLinks.find(wireid) != mapOfLinks.end()) {
331  // DTDigiSimLink::time() is set equal to DTDigi::time() only for the DTDigiSimLink corresponding to the digitizied PSimHit
332  // other DTDigiSimLinks associated to the same DTDigi are identified by having the same DTDigiSimLink::number()
333 
334  // first find the associated digi Number
335  for (vector<DTDigiSimLink>::const_iterator linkIT = mapOfLinks[wireid].begin();
336  linkIT != mapOfLinks[wireid].end();
337  ++linkIT ) {
338  float digitime = linkIT->time();
339  if (fabs(digitime-theTime)<0.1) {
340  theNumber = linkIT->number();
341  }
342  }
343 
344  // then get all the DTDigiSimLinks with that digi Number (corresponding to valid SimHits
345  // within a time window of the order of the time resolution, specified in the DTDigitizer)
346  for (vector<DTDigiSimLink>::const_iterator linkIT = mapOfLinks[wireid].begin();
347  linkIT != mapOfLinks[wireid].end();
348  ++linkIT ) {
349 
350  int digiNr = linkIT->number();
351  if (digiNr == theNumber) {
352  SimHitIdpr currentId(linkIT->SimTrackId(), linkIT->eventId());
353  matched.push_back(currentId);
354  }
355  }
356 
357  } else {
358  edm::LogError("DTHitAssociator")<<"*** ERROR in DTHitAssociator::associateDTHitId - DTRecHit1D: "
359  <<*dtrechit<<" has no associated DTDigiSimLink !"<<endl;
360  return matched;
361  }
362  }
363 
364  return matched;
365 }
366 
367 std::vector<PSimHit> DTHitAssociator::associateHit(const TrackingRecHit & hit) {
368 
369  std::vector<PSimHit> simhits;
370  std::vector<SimHitIdpr> simtrackids;
371 
372  const TrackingRecHit * hitp = &hit;
373  const DTRecHit1D * dtrechit = dynamic_cast<const DTRecHit1D *>(hitp);
374 
375  if (dtrechit) {
376  DTWireId wireid = dtrechit->wireId();
377 
378  if (associatorByWire) {
379  // matching based on DTWireId : take only "valid" SimHits on that wire
380 
381  if(mapOfSimHit.find(wireid) != mapOfSimHit.end()) {
382  for (vector<PSimHit_withFlag>::const_iterator hitIT = mapOfSimHit[wireid].begin();
383  hitIT != mapOfSimHit[wireid].end();
384  ++hitIT) {
385 
386  bool valid_hit = hitIT->second;
387  if (valid_hit) simhits.push_back(hitIT->first);
388  }
389  }
390  }
391  else {
392  // matching based on DTDigiSimLink
393 
394  simtrackids = associateDTHitId(dtrechit);
395 
396  for (vector<SimHitIdpr>::const_iterator idIT = simtrackids.begin(); idIT != simtrackids.end(); ++idIT) {
397  uint32_t trId = idIT->first;
398  EncodedEventId evId = idIT->second;
399 
400  if(mapOfSimHit.find(wireid) != mapOfSimHit.end()) {
401  for (vector<PSimHit_withFlag>::const_iterator hitIT = mapOfSimHit[wireid].begin();
402  hitIT != mapOfSimHit[wireid].end();
403  ++hitIT) {
404 
405  if (hitIT->first.trackId() == trId &&
406  hitIT->first.eventId() == evId)
407  simhits.push_back(hitIT->first);
408  }
409  }
410  }
411  }
412 
413  } else {
414  edm::LogWarning("DTHitAssociator")<<"*** WARNING in DTHitAssociator::associateHit, null dynamic_cast !";
415  }
416  return simhits;
417 }
418 
420  const PSimHit & simhit) {
421  bool result = true;
422 
423  DTWireId wireid(simhit.detUnitId());
424  const DTLayer* dtlayer = muongeom->layer(wireid.layerId());
425  LocalPoint entryP = simhit.entryPoint();
426  LocalPoint exitP = simhit.exitPoint();
427  const DTTopology &topo = dtlayer->specificTopology();
428  float xwire = topo.wirePosition(wireid.wire());
429  float xEntry = entryP.x() - xwire;
430  float xExit = exitP.x() - xwire;
431  DTTopology::Side entrySide = topo.onWhichBorder(xEntry,entryP.y(),entryP.z());
432  DTTopology::Side exitSide = topo.onWhichBorder(xExit,exitP.y(),exitP.z());
433 
434  bool noParametrisation =
435  (( entrySide == DTTopology::none || exitSide == DTTopology::none ) ||
436  (entrySide == exitSide) ||
437  ((entrySide == DTTopology::xMin && exitSide == DTTopology::xMax) ||
438  (entrySide == DTTopology::xMax && exitSide == DTTopology::xMin)) );
439 
440  // discard hits where parametrization can not be used
441  if (noParametrisation)
442  {
443  result = false;
444  return result;
445  }
446 
447  float x;
448  LocalPoint hitPos = simhit.localPosition();
449 
450  if(fabs(hitPos.z()) < 0.002) {
451  // hit center within 20 um from z=0, no need to extrapolate.
452  x = hitPos.x() - xwire;
453  } else {
454  x = xEntry - (entryP.z()*(xExit-xEntry))/(exitP.z()-entryP.z());
455  }
456 
457  // discard hits where x is out of range of the parametrization (|x|>2.1 cm)
458  x *= 10.; //cm -> mm
459  if (fabs(x) > 21.)
460  result = false;
461 
462  return result;
463 }
float wirePosition(int wireNumber) const
Returns the x position in the layer of a given wire number.
Definition: DTTopology.cc:86
std::pair< uint32_t, EncodedEventId > SimHitIdpr
int event() const
get the contents of the subdetector field (should be protected?)
Side onWhichBorder(float x, float y, float z) const
Definition: DTTopology.cc:109
std::vector< PSimHit > associateHit(const TrackingRecHit &hit)
T y() const
Definition: PV3DBase.h:63
RecHitMap mapOfRecHit
edm::InputTag DTdigiTag
edm::InputTag DTsimhitsTag
int iEvent
Definition: GenABIO.cc:230
Local3DPoint exitPoint() const
Exit point in the local Det frame.
Definition: PSimHit.h:38
edm::InputTag DTrechitTag
SimHitMap mapOfSimHit
Local3DPoint localPosition() const
Definition: PSimHit.h:44
int bunchCrossing() const
get the detector field from this detid
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
float digiTime() const
Return the time (ns) of the digi used to build the rechit.
Definition: DTRecHit1D.h:113
EncodedEventId eventId() const
Definition: PSimHit.h:105
Side
Sides of the cell.
Definition: DTTopology.h:88
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
#define LogTrace(id)
tuple conf
Definition: dbtoconf.py:185
T const * product() const
Definition: Handle.h:81
edm::InputTag DTdigisimlinkTag
const T & get() const
Definition: EventSetup.h:55
std::vector< DTDigi >::const_iterator const_iterator
unsigned short processType() const
Definition: PSimHit.h:118
std::string const & label() const
Definition: InputTag.h:42
std::vector< SimHitIdpr > associateDTHitId(const DTRecHit1D *dtrechit)
#define begin
Definition: vmac.h:30
int particleType() const
Definition: PSimHit.h:85
void initEvent(const edm::Event &, const edm::EventSetup &)
DTHitAssociator(const edm::Event &, const edm::EventSetup &, const edm::ParameterSet &, bool printRtS)
unsigned int trackId() const
Definition: PSimHit.h:102
std::pair< const_iterator, const_iterator > Range
std::vector< PSimHit > PSimHitContainer
Definition: DDAxes.h:10
edm::InputTag DTsimhitsXFTag
bool SimHitOK(const edm::ESHandle< DTGeometry > &, const PSimHit &)
T x() const
Definition: PV3DBase.h:62
Local3DPoint entryPoint() const
Entry point in the local Det frame.
Definition: PSimHit.h:35
unsigned int detUnitId() const
Definition: PSimHit.h:93
std::vector< SimHitIdpr > associateHitId(const TrackingRecHit &hit)
DTWireId wireId() const
Return the wireId.
Definition: DTRecHit1D.h:107