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