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