CMS 3D CMS Logo

FastTrackerRecHitMatcher.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // framework stuff
11 
12 // fast tracker rechits
17 
18 // geometry stuff
25 
26 // sim stuff
29 
31 public:
33  ~FastTrackerRecHitMatcher() override { ; }
34  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
35 
36 private:
37  void produce(edm::Event&, const edm::EventSetup&) override;
38 
39  // ---------- typedefs -----------------------------
40  typedef std::pair<LocalPoint, LocalPoint> StripPosition;
41 
42  // ----------internal functions ---------------------------
43 
44  // create projected hit
45  std::unique_ptr<FastTrackerRecHit> projectOnly(const FastSingleTrackerRecHit* originalRH,
46  const GeomDet* monoDet,
47  const GluedGeomDet* gluedDet,
48  LocalVector& ldir) const;
49 
50  // create matched hit
51  std::unique_ptr<FastTrackerRecHit> match(const FastSingleTrackerRecHit* monoRH,
52  const FastSingleTrackerRecHit* stereoRH,
53  const GluedGeomDet* gluedDet,
54  LocalVector& trackdirection,
55  bool stereLayerFirst) const;
56 
57  StripPosition project(const GeomDetUnit* det,
58  const GluedGeomDet* glueddet,
59  const StripPosition& strip,
60  const LocalVector& trackdirection) const;
61 
63  if (!recHit->isSingle()) {
64  throw cms::Exception("FastTrackerRecHitMatcher")
65  << "all rechits in simHit2RecHitMap must be instances of FastSingleTrackerRecHit. recHit's rtti: "
66  << recHit->rtti() << std::endl;
67  }
68  return dynamic_cast<const FastSingleTrackerRecHit*>(recHit);
69  }
70 
71  // ----------member data ---------------------------
74 };
75 
77 
78 {
79  simHitsToken = consumes<edm::PSimHitContainer>(iConfig.getParameter<edm::InputTag>("simHits"));
81  consumes<FastTrackerRecHitRefCollection>(iConfig.getParameter<edm::InputTag>("simHit2RecHitMap"));
82 
83  produces<FastTrackerRecHitCollection>();
84  produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
85 }
86 
88  // services
90  iSetup.get<TrackerDigiGeometryRecord>().get(geometry);
91 
92  // input
94  iEvent.getByToken(simHitsToken, simHits);
95 
97  iEvent.getByToken(simHit2RecHitMapToken, simHit2RecHitMap);
98 
99  // output
100  auto output_recHits = std::make_unique<FastTrackerRecHitCollection>();
101  auto output_simHit2RecHitMap =
102  std::make_unique<FastTrackerRecHitRefCollection>(simHit2RecHitMap->size(), FastTrackerRecHitRef());
103  edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd =
105 
106  bool skipNext = false;
107  for (unsigned simHitCounter = 0; simHitCounter < simHits->size(); ++simHitCounter) {
108  // skip hit in case it was matched to previous one
109  if (skipNext) {
110  skipNext = false;
111  continue;
112  }
113  skipNext = false;
114 
115  // get simHit and associated recHit
116  const PSimHit& simHit = (*simHits)[simHitCounter];
117  const FastTrackerRecHitRef& recHitRef = (*simHit2RecHitMap)[simHitCounter];
118 
119  // skip simHits w/o associated recHit
120  if (recHitRef.isNull())
121  continue;
122 
123  // cast
124  const FastSingleTrackerRecHit* recHit = _cast2Single(recHitRef.get());
125 
126  // get subdetector id
127  DetId detid = recHit->geographicalId();
128  unsigned int subdet = detid.subdetId();
129 
130  // treat pixel hits
131  if (subdet <= 2) {
132  (*output_simHit2RecHitMap)[simHitCounter] = recHitRef;
133  }
134 
135  // treat strip hits
136  else {
137  StripSubdetector stripSubDetId(detid);
138 
139  // treat regular regular strip hits
140  if (!stripSubDetId.glued()) {
141  (*output_simHit2RecHitMap)[simHitCounter] = recHitRef;
142  }
143 
144  // treat strip hits on glued layers
145  else {
146  // Obtain direction of simtrack at simhit in local coordinates of glued module
147  // - direction of simtrack at simhit, in coordindates of the single module
148  LocalVector localSimTrackDir = simHit.localDirection();
149  // - transform to global coordinates
150  GlobalVector globalSimTrackDir = recHit->det()->surface().toGlobal(localSimTrackDir);
151  // - transform to local coordinates of glued module
152  const GluedGeomDet* gluedDet = (const GluedGeomDet*)geometry->idToDet(DetId(stripSubDetId.glued()));
153  LocalVector gluedLocalSimTrackDir = gluedDet->surface().toLocal(globalSimTrackDir);
154 
155  // check whether next hit is partner
156  const FastSingleTrackerRecHit* partnerRecHit = nullptr;
157  // - there must be a next hit
158  if (simHitCounter + 1 < simHits->size()) {
159  const FastTrackerRecHitRef& nextRecHitRef = (*simHit2RecHitMap)[simHitCounter + 1];
160  const PSimHit& nextSimHit = (*simHits)[simHitCounter + 1];
161  // - partner hit must not be null
162  // - simHit and partner simHit must belong to same simTrack
163  // - partner hit must be on the module glued to the module of the hit
164  if ((!nextRecHitRef.isNull()) && simHit.trackId() == nextSimHit.trackId() &&
165  StripSubdetector(nextRecHitRef->geographicalId()).partnerDetId() == detid.rawId()) {
166  partnerRecHit = _cast2Single(nextRecHitRef.get());
167  skipNext = true;
168  }
169  }
170 
171  std::unique_ptr<FastTrackerRecHit> newRecHit(nullptr);
172 
173  // if partner found: create a matched hit
174  if (partnerRecHit) {
175  newRecHit = match(stripSubDetId.stereo() ? partnerRecHit : recHit,
176  stripSubDetId.stereo() ? recHit : partnerRecHit,
177  gluedDet,
178  gluedLocalSimTrackDir,
179  stripSubDetId.stereo());
180  }
181  // else: create projected hit
182  else {
183  newRecHit = projectOnly(recHit, geometry->idToDet(detid), gluedDet, gluedLocalSimTrackDir);
184  }
185  output_recHits->push_back(std::move(newRecHit));
186  (*output_simHit2RecHitMap)[simHitCounter] =
187  FastTrackerRecHitRef(output_recHits_refProd, output_recHits->size() - 1);
188  }
189  }
190  }
191 
192  iEvent.put(std::move(output_recHits));
193  iEvent.put(std::move(output_simHit2RecHitMap), "simHit2RecHitMap");
194 }
195 
196 std::unique_ptr<FastTrackerRecHit> FastTrackerRecHitMatcher::match(const FastSingleTrackerRecHit* monoRH,
197  const FastSingleTrackerRecHit* stereoRH,
198  const GluedGeomDet* gluedDet,
199  LocalVector& trackdirection,
200  bool stereoHitFirst) const {
201  // stripdet = mono
202  // partnerstripdet = stereo
203  const GeomDetUnit* stripdet = gluedDet->monoDet();
204  const GeomDetUnit* partnerstripdet = gluedDet->stereoDet();
205  const StripTopology& topol = (const StripTopology&)stripdet->topology();
206 
208 
209  // position of the initial and final point of the strip (RPHI cluster) in local strip coordinates
210  MeasurementPoint RPHIpoint = topol.measurementPosition(monoRH->localPosition());
211  MeasurementPoint RPHIpointini = MeasurementPoint(RPHIpoint.x(), -0.5);
212  MeasurementPoint RPHIpointend = MeasurementPoint(RPHIpoint.x(), 0.5);
213 
214  // position of the initial and final point of the strip in local coordinates (mono det)
215  StripPosition stripmono = StripPosition(topol.localPosition(RPHIpointini), topol.localPosition(RPHIpointend));
216 
217  if (trackdirection.mag2() <
218  FLT_MIN) { // in case of no track hypothesis assume a track from the origin through the center of the strip
219  LocalPoint lcenterofstrip = monoRH->localPosition();
220  GlobalPoint gcenterofstrip = (stripdet->surface()).toGlobal(lcenterofstrip);
221  GlobalVector gtrackdirection = gcenterofstrip - GlobalPoint(0, 0, 0);
222  trackdirection = (gluedDet->surface()).toLocal(gtrackdirection);
223  }
224 
225  //project mono hit on glued det
226  StripPosition projectedstripmono = project(stripdet, gluedDet, stripmono, trackdirection);
227  const StripTopology& partnertopol = (const StripTopology&)partnerstripdet->topology();
228 
229  //error calculation (the part that depends on mono RH only)
230  LocalVector RPHIpositiononGluedendvector = projectedstripmono.second - projectedstripmono.first;
231  double c1 = sin(RPHIpositiononGluedendvector.phi());
232  double s1 = -cos(RPHIpositiononGluedendvector.phi());
233  MeasurementError errormonoRH = topol.measurementError(monoRH->localPosition(), monoRH->localPositionError());
234  double pitch = topol.localPitch(monoRH->localPosition());
235  double sigmap12 = errormonoRH.uu() * pitch * pitch;
236 
237  // position of the initial and final point of the strip (STEREO cluster)
238  MeasurementPoint STEREOpoint = partnertopol.measurementPosition(stereoRH->localPosition());
239 
240  MeasurementPoint STEREOpointini = MeasurementPoint(STEREOpoint.x(), -0.5);
241  MeasurementPoint STEREOpointend = MeasurementPoint(STEREOpoint.x(), 0.5);
242 
243  // position of the initial and final point of the strip in local coordinates (stereo det)
244  StripPosition stripstereo(partnertopol.localPosition(STEREOpointini), partnertopol.localPosition(STEREOpointend));
245 
246  //project stereo hit on glued det
247  StripPosition projectedstripstereo = project(partnerstripdet, gluedDet, stripstereo, trackdirection);
248 
249  //perform the matching
250  //(x2-x1)(y-y1)=(y2-y1)(x-x1)
252  AlgebraicVector2 c, solution;
253  m(0, 0) = -(projectedstripmono.second.y() - projectedstripmono.first.y());
254  m(0, 1) = (projectedstripmono.second.x() - projectedstripmono.first.x());
255  m(1, 0) = -(projectedstripstereo.second.y() - projectedstripstereo.first.y());
256  m(1, 1) = (projectedstripstereo.second.x() - projectedstripstereo.first.x());
257  c(0) = m(0, 1) * projectedstripmono.first.y() + m(0, 0) * projectedstripmono.first.x();
258  c(1) = m(1, 1) * projectedstripstereo.first.y() + m(1, 0) * projectedstripstereo.first.x();
259  m.Invert();
260  solution = m * c;
261  position = LocalPoint(solution(0), solution(1));
262 
263  //
264  // temporary fix by tommaso
265  //
266 
267  LocalError tempError(100, 0, 100);
268 
269  // calculate the error
270  LocalVector stereopositiononGluedendvector = projectedstripstereo.second - projectedstripstereo.first;
271  double c2 = sin(stereopositiononGluedendvector.phi());
272  double s2 = -cos(stereopositiononGluedendvector.phi());
273  MeasurementError errorstereoRH =
274  partnertopol.measurementError(stereoRH->localPosition(), stereoRH->localPositionError());
275  pitch = partnertopol.localPitch(stereoRH->localPosition());
276  double sigmap22 = errorstereoRH.uu() * pitch * pitch;
277  double diff = (c1 * s2 - c2 * s1);
278  double invdet2 = 1 / (diff * diff);
279  float xx = invdet2 * (sigmap12 * s2 * s2 + sigmap22 * s1 * s1);
280  float xy = -invdet2 * (sigmap12 * c2 * s2 + sigmap22 * c1 * s1);
281  float yy = invdet2 * (sigmap12 * c2 * c2 + sigmap22 * c1 * c1);
282  LocalError error = LocalError(xx, xy, yy);
283 
284  //Added by DAO to make sure y positions are zero.
285  DetId det(monoRH->geographicalId());
286  if (det.subdetId() > 2) {
287  return std::make_unique<FastMatchedTrackerRecHit>(position, error, *gluedDet, *monoRH, *stereoRH, stereoHitFirst);
288 
289  }
290 
291  else {
292  throw cms::Exception("FastTrackerRecHitMatcher") << "Matched Pixel!?";
293  }
294 }
295 
297  const GluedGeomDet* glueddet,
298  const StripPosition& strip,
299  const LocalVector& trackdirection) const {
300  GlobalPoint globalpointini = (det->surface()).toGlobal(strip.first);
301  GlobalPoint globalpointend = (det->surface()).toGlobal(strip.second);
302 
303  // position of the initial and final point of the strip in glued local coordinates
304  LocalPoint positiononGluedini = (glueddet->surface()).toLocal(globalpointini);
305  LocalPoint positiononGluedend = (glueddet->surface()).toLocal(globalpointend);
306 
307  //correct the position with the track direction
308 
309  float scale = -positiononGluedini.z() / trackdirection.z();
310 
311  LocalPoint projpositiononGluedini = positiononGluedini + scale * trackdirection;
312  LocalPoint projpositiononGluedend = positiononGluedend + scale * trackdirection;
313 
314  return StripPosition(projpositiononGluedini, projpositiononGluedend);
315 }
316 
317 std::unique_ptr<FastTrackerRecHit> FastTrackerRecHitMatcher::projectOnly(const FastSingleTrackerRecHit* originalRH,
318  const GeomDet* monoDet,
319  const GluedGeomDet* gluedDet,
320  LocalVector& ldir) const {
321  LocalPoint position(originalRH->localPosition().x(), 0., 0.);
322  const BoundPlane& gluedPlane = gluedDet->surface();
323  const BoundPlane& hitPlane = monoDet->surface();
324 
325  double delta = gluedPlane.localZ(hitPlane.position());
326 
327  LocalPoint lhitPos = gluedPlane.toLocal(monoDet->surface().toGlobal(position));
328  LocalPoint projectedHitPos = lhitPos - ldir * delta / ldir.z();
329 
330  LocalVector hitXAxis = gluedPlane.toLocal(hitPlane.toGlobal(LocalVector(1, 0, 0)));
331  LocalError hitErr = originalRH->localPositionError();
332 
333  if (gluedPlane.normalVector().dot(hitPlane.normalVector()) < 0) {
334  // the two planes are inverted, and the correlation element must change sign
335  hitErr = LocalError(hitErr.xx(), -hitErr.xy(), hitErr.yy());
336  }
337  LocalError rotatedError = hitErr.rotate(hitXAxis.x(), hitXAxis.y());
338 
339  const GeomDetUnit* gluedMonoDet = gluedDet->monoDet();
340  const GeomDetUnit* gluedStereoDet = gluedDet->stereoDet();
341  int isMono = 0;
342  int isStereo = 0;
343 
344  if (monoDet->geographicalId() == gluedMonoDet->geographicalId())
345  isMono = 1;
346  if (monoDet->geographicalId() == gluedStereoDet->geographicalId())
347  isStereo = 1;
348  //Added by DAO to make sure y positions are zero and correct Mono or stereo Det is filled.
349 
350  if ((isMono && isStereo) || (!isMono && !isStereo))
351  throw cms::Exception("FastTrackerRecHitMatcher") << "Something wrong with DetIds.";
352  return std::make_unique<FastProjectedTrackerRecHit>(projectedHitPos, rotatedError, *gluedDet, *originalRH);
353 }
354 
355 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
357  //The following says we do not know what parameters are allowed so do no validation
358  // Please change this to state exactly what you do use, even if it is no parameters
360  desc.setUnknown();
361  descriptions.addDefault(desc);
362 }
363 
364 //define this as a plug-in
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:81
T getParameter(std::string const &) const
float xx() const
Definition: LocalError.h:22
T mag2() const
Definition: PV3DBase.h:63
std::pair< LocalPoint, LocalPoint > StripPosition
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
LocalError localPositionError() const final
const GeomDetUnit * monoDet() const
Definition: GluedGeomDet.h:19
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
virtual const Topology & topology() const
Definition: GeomDet.cc:67
std::unique_ptr< FastTrackerRecHit > projectOnly(const FastSingleTrackerRecHit *originalRH, const GeomDet *monoDet, const GluedGeomDet *gluedDet, LocalVector &ldir) const
void produce(edm::Event &, const edm::EventSetup &) override
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
T y() const
Definition: PV3DBase.h:60
std::unique_ptr< FastTrackerRecHit > match(const FastSingleTrackerRecHit *monoRH, const FastSingleTrackerRecHit *stereoRH, const GluedGeomDet *gluedDet, LocalVector &trackdirection, bool stereLayerFirst) const
LocalVector toLocal(const reco::Track::Vector &v, const Surface &s)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
edm::Ref< FastTrackerRecHitCollection > FastTrackerRecHitRef
FastTrackerRecHitMatcher(const edm::ParameterSet &)
float xy() const
Definition: LocalError.h:23
Measurement2DPoint MeasurementPoint
Measurement points are two-dimensional by default.
int iEvent
Definition: GenABIO.cc:224
unsigned int glued() const
glued
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
ROOT::Math::SVector< double, 2 > AlgebraicVector2
edm::EDGetTokenT< FastTrackerRecHitRefCollection > simHit2RecHitMapToken
float yy() const
Definition: LocalError.h:24
virtual LocalPoint localPosition(float strip) const =0
const GeomDet * det() const
LocalPoint toLocal(const GlobalPoint &gp) const
T z() const
Definition: PV3DBase.h:61
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
virtual MeasurementPoint measurementPosition(const LocalPoint &) const =0
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
edm::EDGetTokenT< edm::PSimHitContainer > simHitsToken
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
StripPosition project(const GeomDetUnit *det, const GluedGeomDet *glueddet, const StripPosition &strip, const LocalVector &trackdirection) const
bool isNull() const
Checks for null.
Definition: Ref.h:235
RefProd< PROD > getRefBeforePut()
Definition: Event.h:156
LocalVector localDirection() const
Obsolete. Same as momentumAtEntry().unit(), for backward compatibility.
Definition: PSimHit.h:58
Definition: DetId.h:17
unsigned int stereo() const
stereo
const FastSingleTrackerRecHit * _cast2Single(const FastTrackerRecHit *recHit) const
virtual float localPitch(const LocalPoint &) const =0
ESHandle< TrackerGeometry > geometry
static int position[264][3]
Definition: ReadPGInfo.cc:289
T get() const
Definition: EventSetup.h:73
ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepStd< double, 2, 2 > > AlgebraicMatrix22
const TrackerGeomDet * idToDet(DetId) const override
unsigned int trackId() const
Definition: PSimHit.h:106
trackerHitRTTI::RTTI rtti() const
LocalPoint localPosition() const final
DetId geographicalId() const
virtual MeasurementError measurementError(const LocalPoint &, const LocalError &) const =0
LocalError rotate(float x, float y) const
Return a new LocalError, rotated by an angle defined by the direction (x,y)
Definition: LocalError.h:37
T x() const
Definition: PV2DBase.h:43
T x() const
Definition: PV3DBase.h:59
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
def move(src, dest)
Definition: eostools.py:511
const GeomDetUnit * stereoDet() const
Definition: GluedGeomDet.h:20
bool isSingle() const