CMS 3D CMS Logo

SiStripRecHitMatcher.h
Go to the documentation of this file.
1 #ifndef RECOLOCALTRACKER_SISTRIPCLUSTERIZER_SISTRIPRECHITMATCH_H
2 #define RECOLOCALTRACKER_SISTRIPCLUSTERIZER_SISTRIPRECHITMATCH_H
3 
11 
13 
15 
17 
18 class GluedGeomDet;
19 
20 #include <cfloat>
21 #include <functional>
22 #include <memory>
23 #include <vector>
24 
26 public:
27  // may become a template argument
29 
31  typedef std::vector<const SiStripRecHit2D*> SimpleHitCollection;
32  typedef SimpleHitCollection::const_iterator SimpleHitIterator;
33 
34  typedef std::function<void(SiStripMatchedRecHit2D const&)> Collector;
35 
36  typedef std::pair<LocalPoint, LocalPoint> StripPosition;
37 
39  SiStripRecHitMatcher(const double theScale);
40 
41  bool preFilter() const { return preFilter_; }
42 
43  inline static float sigmaPitch(LocalPoint const& pos, LocalError const& err, const StripTopology& topol) {
45  auto pitch = topol.localPitch(pos);
46  return error.uu() * pitch * pitch;
47  }
48 
49  // optimized matching iteration (the algo is the same, just recoded)
50  template <typename MonoIterator, typename StereoIterator, typename CollectorHelper>
51  void doubleMatch(MonoIterator monoRHiter,
52  MonoIterator monoRHend,
53  StereoIterator seconditer,
54  StereoIterator seconditerend,
55  const GluedGeomDet* gluedDet,
56  LocalVector trdir,
57  CollectorHelper& collectorHelper) const;
58 
59  //match a single hit
60  std::unique_ptr<SiStripMatchedRecHit2D> match(const SiStripRecHit2D* monoRH,
61  const SiStripRecHit2D* stereoRH,
62  const GluedGeomDet* gluedDet,
63  LocalVector trackdirection,
64  bool force) const;
65 
66  // this is the one used by the RecHitConverter
67  void match(const SiStripRecHit2D* monoRH,
68  RecHitIterator begin,
70  CollectorMatched& collector,
71  const GluedGeomDet* gluedDet,
72  LocalVector trackdirection) const;
73 
74  void match(const SiStripRecHit2D* monoRH,
75  SimpleHitIterator begin,
77  CollectorMatched& collector,
78  const GluedGeomDet* gluedDet,
79  LocalVector trackdirection) const;
80 
81  // project strip coordinates on Glueddet
82 
84  const GluedGeomDet* glueddet,
86  LocalVector trackdirection) const;
87 
88  // needed by the obsolete version still in use on some architectures
89  void match(const SiStripRecHit2D* monoRH,
90  SimpleHitIterator begin,
92  std::vector<std::unique_ptr<SiStripMatchedRecHit2D>>& collector,
93  const GluedGeomDet* gluedDet,
94  LocalVector trackdirection) const;
95 
96  void match(const SiStripRecHit2D* monoRH,
97  SimpleHitIterator begin,
99  std::vector<SiStripMatchedRecHit2D*>& collector,
100  const GluedGeomDet* gluedDet,
101  LocalVector trackdirection) const;
102 
104  void match(const SiStripRecHit2D* monoRH,
105  SimpleHitIterator begin,
107  Collector& collector,
108  const GluedGeomDet* gluedDet,
109  LocalVector trackdirection) const;
110 
111  float scale_;
112  bool preFilter_ = false;
113 };
114 
116 #if defined(USE_SSEVECT) || defined(USE_EXTVECT)
117 #define DOUBLE_MATCH
118 #endif
119 
120 #ifdef DOUBLE_MATCH
121 
122 #if defined(USE_SSEVECT)
123 using mathSSE::Rot3F;
124 using mathSSE::Vec2D;
125 using mathSSE::Vec3D;
126 using mathSSE::Vec3F;
127 #endif
128 
131 
132 namespace matcherDetails {
133 
134  struct StereoInfo {
135  Vec2D c1vec;
136  const SiStripRecHit2D* secondHit;
137  float sigmap22;
138  double m10, m11;
139  };
140 
141 } // namespace matcherDetails
142 
143 template <typename MonoIterator, typename StereoIterator, typename CollectorHelper>
144 void SiStripRecHitMatcher::doubleMatch(MonoIterator monoRHiter,
145  MonoIterator monoRHend,
146  StereoIterator seconditer,
147  StereoIterator seconditerend,
148  const GluedGeomDet* gluedDet,
149  LocalVector trdir,
150  CollectorHelper& collectorHelper) const {
151  using matcherDetails::StereoInfo;
152 
153  typedef GloballyPositioned<float> ToGlobal;
154  typedef typename GloballyPositioned<float>::ToLocal ToLocal;
155 
156  // hits in both mono and stero
157  // match
158  bool notk = trdir.mag2() < float(FLT_MIN);
159  // FIXME we shall find a faster approximation for trdir: not useful to compute it each time for each strip
160 
161  // stripdet = mono
162  // partnerstripdet = stereo
163  const GeomDetUnit* stripdet = gluedDet->monoDet();
164  const GeomDetUnit* partnerstripdet = gluedDet->stereoDet();
165  const StripTopology& topol = (const StripTopology&)stripdet->topology();
166  const StripTopology& partnertopol = (const StripTopology&)partnerstripdet->topology();
167 
168  // toGlobal is fast, toLocal is slow
169  ToGlobal const& stripDetTrans = stripdet->surface();
170  ToGlobal const& partnerStripDetTrans = partnerstripdet->surface();
171  ToLocal gluedDetInvTrans(gluedDet->surface());
172 
173  std::vector<StereoInfo> cache(std::distance(seconditer, seconditerend));
174  //iterate on stereo rechits
175  // fill cache with relevant info
176  int cacheSize = 0;
177  for (; seconditer != seconditerend; ++seconditer) {
178  const SiStripRecHit2D& secondHit = CollectorHelper::stereoHit(seconditer);
179 
180  float sigmap22 = sigmaPitch(secondHit.localPosition(), secondHit.localPositionError(), partnertopol);
181  // assert (sigmap22>=0);
182 
183  auto STEREOpointX = partnertopol.measurementPosition(secondHit.localPositionFast()).x();
184  MeasurementPoint STEREOpointini(STEREOpointX, -0.5f);
185  MeasurementPoint STEREOpointend(STEREOpointX, 0.5f);
186 
187  LocalPoint locp1 = partnertopol.localPosition(STEREOpointini);
188  LocalPoint locp2 = partnertopol.localPosition(STEREOpointend);
189 
190  GlobalPoint globalpointini = partnerStripDetTrans.toGlobal(locp1);
191  GlobalPoint globalpointend = partnerStripDetTrans.toGlobal(locp2);
192 
193  // position of the initial and final point of the strip in glued local coordinates
194  LocalPoint positiononGluedini = gluedDetInvTrans.toLocal(globalpointini);
195  LocalPoint positiononGluedend = gluedDetInvTrans.toLocal(globalpointend);
196 
197  // in case of no track hypothesis assume a track from the origin through the center of the strip
198  if (notk) {
199  const LocalPoint& lcenterofstrip = secondHit.localPositionFast();
200  GlobalPoint gcenterofstrip = partnerStripDetTrans.toGlobal(lcenterofstrip);
201  GlobalVector gtrackdirection = gcenterofstrip - GlobalPoint(0, 0, 0);
202  trdir = gluedDetInvTrans.toLocal(gtrackdirection);
203  }
204 
205  Vec3F offset = trdir.basicVector().v * positiononGluedini.z() / trdir.z();
206 
207  Vec3F ret1 = positiononGluedini.basicVector().v - offset;
208  Vec3F ret2 = positiononGluedend.basicVector().v - offset;
209 
210  double m10 = -(ret2[1] - ret1[1]);
211  double m11 = ret2[0] - ret1[0];
212  double dd = m11 * ret1[1] + m10 * ret1[0];
213 
214  Vec2D c1vec{dd, dd};
215 
216  // store
217  StereoInfo info = {c1vec, &secondHit, sigmap22, m10, m11};
218  cache[cacheSize++] = info;
219  }
220 
221  for (; monoRHiter != monoRHend; ++monoRHiter) {
222  SiStripRecHit2D const& monoRH = CollectorHelper::monoHit(monoRHiter);
223 
224  // position of the initial and final point of the strip (RPHI cluster) in local strip coordinates
225  auto RPHIpointX = topol.measurementPosition(monoRH.localPositionFast()).x();
226  MeasurementPoint RPHIpointini(RPHIpointX, -0.5f);
227  MeasurementPoint RPHIpointend(RPHIpointX, 0.5f);
228 
229  // position of the initial and final point of the strip in local coordinates (mono det)
230  //StripPosition stripmono=StripPosition(topol.localPosition(RPHIpointini),topol.localPosition(RPHIpointend));
231  LocalPoint locp1o = topol.localPosition(RPHIpointini);
232  LocalPoint locp2o = topol.localPosition(RPHIpointend);
233 
234  // in case of no track hypothesis assume a track from the origin through the center of the strip
235  if (notk) {
236  const LocalPoint& lcenterofstrip = monoRH.localPositionFast();
237  GlobalPoint gcenterofstrip = stripDetTrans.toGlobal(lcenterofstrip);
238  GlobalVector gtrackdirection = gcenterofstrip - GlobalPoint(0, 0, 0);
239  trdir = gluedDetInvTrans.toLocal(gtrackdirection);
240  }
241 
242  //project mono hit on glued det
243  //StripPosition projectedstripmono=project(stripdet,gluedDet,stripmono,trackdirection);
244 
245  GlobalPoint globalpointini = stripDetTrans.toGlobal(locp1o);
246  GlobalPoint globalpointend = stripDetTrans.toGlobal(locp2o);
247 
248  // position of the initial and final point of the strip in glued local coordinates
249  LocalPoint positiononGluedini = gluedDetInvTrans.toLocal(globalpointini);
250  LocalPoint positiononGluedend = gluedDetInvTrans.toLocal(globalpointend);
251 
252  Vec3F offset = trdir.basicVector().v * positiononGluedini.z() / trdir.z();
253 
254  Vec3F projini = positiononGluedini.basicVector().v - offset;
255  Vec3F projend = positiononGluedend.basicVector().v - offset;
256 
257  // ret1o = ret1o + (trdir * (ret1o.getSimd(2) / trdirz));
258  // ret2o = ret2o + (trdir * (ret2o.getSimd(2) / trdirz));
259 
260  double m00 = -(projend[1] - projini[1]); //-(projectedstripmono.second.y()-projectedstripmono.first.y());
261  double m01 = (projend[0] - projini[0]); // (projectedstripmono.second.x()-projectedstripmono.first.x());
262  double c0 =
263  m01 * projini[1] + m00 * projini[0]; //m01*projectedstripmono.first.y() + m00*projectedstripmono.first.x();
264 
265  Vec2D c0vec{c0, c0};
266  Vec2D minv00{-m01, m00};
267 
268  //error calculation (the part that depends on mono RH only)
269  double c1 = -m00;
270  double s1 = -m01;
271  double l1 = 1. / (c1 * c1 + s1 * s1);
272 
273  float sigmap12 = sigmaPitch(monoRH.localPosition(), monoRH.localPositionError(), topol);
274  // float sigmap12 = monoRH.sigmaPitch();
275  // assert(sigmap12>=0);
276 
277  //float code
278  float fc1(c1), fs1(s1);
279  Vec3F scc1{fs1, fc1, fc1, 0.f};
280  Vec3F ssc1{fs1, fs1, fc1, 0.f};
281  const Vec3F cslsimd = scc1 * ssc1 * float(l1);
282 
283  for (int i = 0; i != cacheSize; ++i) {
284  StereoInfo const si = cache[i];
285 
286  // match
287  Vec2D minv10{si.m11, -si.m10};
288  double mult = 1. / (m00 * si.m11 - m01 * si.m10);
289  Vec2D resultmatmul = mult * (minv10 * c0vec + minv00 * si.c1vec);
290 
291  Local2DPoint position(resultmatmul[0], resultmatmul[1]);
292 
293  // LocalError tempError (100,0,100);
294  if (!((gluedDet->surface()).bounds().inside(position, 10.f * scale_)))
295  continue;
296 
297  double c2 = -si.m10;
298  double s2 = -si.m11;
299  double l2 = 1. / (c2 * c2 + s2 * s2);
300 
301  double diff = (c1 * s2 - c2 * s1);
302  double invdet2 = 1. / (diff * diff * l1 * l2);
303 
304  float fc2(c2), fs2(s2), fid2(invdet2);
305  Vec3F invdet2simd{fid2, -fid2, fid2, 0.f};
306  Vec3F ccssimd{fs2, fc2, fc2, 0.f};
307  Vec3F csssimd{fs2, fs2, fc2, 0.f};
308  Vec3F result = invdet2simd * (si.sigmap22 * cslsimd + sigmap12 * ccssimd * csssimd * float(l2));
309 
310  LocalError error(result[0], result[1], result[2]);
311 
312  if ((gluedDet->surface()).bounds().inside(position, error, scale_)) { //if it is inside the gluedet bonds
313 
314  //Change NSigmaInside in the configuration file to accept more hits
315  //...and add it to the Rechit collection
316 
317  collectorHelper.collector()(
318  SiStripMatchedRecHit2D(LocalPoint(position), error, *gluedDet, &monoRH, si.secondHit));
319  }
320 
321  } // loop on cache info
322 
323  collectorHelper.closure(monoRHiter);
324  } // loop on mono hit
325 }
326 
327 #endif //DOUBLE_MATCH
328 
329 #endif // RECOLOCALTRACKER_SISTRIPCLUSTERIZER_SISTRIPRECHITMATCH_H
static const TGPicture * info(bool iBackgroundIsBlack)
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
Vec4< float > Vec3F
Definition: SSEVec.h:488
T z() const
Definition: PV3DBase.h:61
virtual const Topology & topology() const
Definition: GeomDet.cc:67
std::unique_ptr< SiStripMatchedRecHit2D > match(const SiStripRecHit2D *monoRH, const SiStripRecHit2D *stereoRH, const GluedGeomDet *gluedDet, LocalVector trackdirection, bool force) const
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::function< void(SiStripMatchedRecHit2D const &)> Collector
std::pair< LocalPoint, LocalPoint > StripPosition
Vec4< float > Vec3F
Definition: ExtVec.h:164
static float sigmaPitch(LocalPoint const &pos, LocalError const &err, const StripTopology &topol)
T mag2() const
Definition: PV3DBase.h:63
string dd
Definition: createTree.py:154
virtual float localPitch(const LocalPoint &) const =0
virtual MeasurementError measurementError(const LocalPoint &, const LocalError &) const =0
SiStripRecHitMatcher(const edm::ParameterSet &conf)
double f[11][100]
virtual MeasurementPoint measurementPosition(const LocalPoint &) const =0
Vec4< double > Vec3D
Definition: SSEVec.h:490
const LocalPoint & localPositionFast() const
Rot3< float > Rot3F
Definition: SSERot.h:71
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:53
const GeomDetUnit * monoDet() const
Definition: GluedGeomDet.h:19
SiStripMatchedRecHit2DCollectionNew::FastFiller CollectorMatched
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
LocalError localPositionError() const override
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
std::vector< const SiStripRecHit2D * > SimpleHitCollection
SiStripRecHit2DCollectionNew::DetSet::const_iterator RecHitIterator
const GeomDetUnit * stereoDet() const
Definition: GluedGeomDet.h:20
void doubleMatch(MonoIterator monoRHiter, MonoIterator monoRHend, StereoIterator seconditer, StereoIterator seconditerend, const GluedGeomDet *gluedDet, LocalVector trdir, CollectorHelper &collectorHelper) const
SimpleHitCollection::const_iterator SimpleHitIterator
LocalPoint localPosition() const override
static int position[264][3]
Definition: ReadPGInfo.cc:289
def cache(function)
Definition: utilities.py:3
virtual LocalPoint localPosition(float strip) const =0
StripPosition project(const GeomDetUnit *det, const GluedGeomDet *glueddet, StripPosition strip, LocalVector trackdirection) const
Vec2< double > Vec2D
Definition: SSEVec.h:489