CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TkGluedMeasurementDet.cc
Go to the documentation of this file.
10 #include "RecHitPropagator.h"
12 
14 #include <iostream>
15 #include <memory>
16 
17 #include <typeinfo>
18 
19 // #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
21 
22 using namespace std;
23 
24 TkGluedMeasurementDet::TkGluedMeasurementDet( const GluedGeomDet* gdet,
25  const SiStripRecHitMatcher* matcher,
26  const StripClusterParameterEstimator* cpe) :
27  MeasurementDet(gdet),
28  theMatcher(matcher), theCPE(cpe),
29  theMonoDet(nullptr), theStereoDet(nullptr)
30 {}
31 
33  const MeasurementDet* stereoDet) {
34  theMonoDet = dynamic_cast<const TkStripMeasurementDet *>(monoDet);
35  theStereoDet = dynamic_cast<const TkStripMeasurementDet *>(stereoDet);
36 
37  if ((theMonoDet == 0) || (theStereoDet == 0)) {
38  throw MeasurementDetException("TkGluedMeasurementDet ERROR: Trying to glue a det which is not a TkStripMeasurementDet");
39  }
40 }
41 
42 TkGluedMeasurementDet::RecHitContainer
43 TkGluedMeasurementDet::recHits( const TrajectoryStateOnSurface& ts, const MeasurementTrackerEvent & data) const
44 {
45 
46  RecHitContainer result;
47  HitCollectorForRecHits collector( &fastGeomDet(), theMatcher, theCPE, result );
48  collectRecHits(ts, data, collector);
49  return result;
50 }
51 
52 struct take_address { template<typename T> const T * operator()(const T &val) const { return &val; } };
53 
54 #ifdef DOUBLE_MATCH
55 template<typename Collector>
56 void
57 TkGluedMeasurementDet::collectRecHits( const TrajectoryStateOnSurface& ts, const MeasurementTrackerEvent & data, Collector & collector) const
58 {
59  doubleMatch(ts,data,collector);
60 }
61 #else
62 template<typename Collector>
63 void
64 TkGluedMeasurementDet::collectRecHits( const TrajectoryStateOnSurface& ts, const MeasurementTrackerEvent & data, Collector & collector) const
65 {
66  //------ WARNING: here ts is used as it is on the mono/stereo surface.
67  //----- A further propagation is necessary.
68  //----- To limit the problem, the SimpleCPE should be used
69  RecHitContainer monoHits = theMonoDet->recHits( ts, data );
70  GlobalVector glbDir = (ts.isValid() ? ts.globalParameters().momentum() : position()-GlobalPoint(0,0,0));
71 
72  //edm::LogWarning("TkGluedMeasurementDet::recHits") << "Query-for-detid-" << theGeomDet->geographicalId().rawId();
73 
74  //checkProjection(ts, monoHits, stereoHits);
75 
76  if (monoHits.empty()) {
77  // make stereo TTRHs and project them
78  projectOnGluedDet( collector, theStereoDet->recHits(ts, data), glbDir);
79  } else {
80  // collect simple stereo hits
81  static std::vector<SiStripRecHit2D> simpleSteroHitsByValue;
82  simpleSteroHitsByValue.clear();
83  theStereoDet->simpleRecHits(ts, data, simpleSteroHitsByValue);
84 
85  if (simpleSteroHitsByValue.empty()) {
86  projectOnGluedDet( collector, monoHits, glbDir);
87  } else {
88 
89  LocalVector tkDir = (ts.isValid() ? ts.localDirection() : surface().toLocal( position()-GlobalPoint(0,0,0)));
91  vsStereoHits.resize(simpleSteroHitsByValue.size());
92  std::transform(simpleSteroHitsByValue.begin(), simpleSteroHitsByValue.end(), vsStereoHits.begin(), take_address());
93 
94  // convert mono hits to type expected by matcher
95  for (RecHitContainer::const_iterator monoHit = monoHits.begin();
96  monoHit != monoHits.end(); ++monoHit) {
97  const TrackingRecHit* tkhit = (**monoHit).hit();
98  const SiStripRecHit2D* verySpecificMonoHit = reinterpret_cast<const SiStripRecHit2D*>(tkhit);
99  theMatcher->match( verySpecificMonoHit, vsStereoHits.begin(), vsStereoHits.end(),
100  collector.collector(), &specificGeomDet(), tkDir);
101 
102  if (collector.hasNewMatchedHits()) {
103  collector.clearNewMatchedHitsFlag();
104  } else {
105  collector.addProjected( **monoHit, glbDir );
106  }
107  } // loop on mono hit
108  }
109  //GIO// std::cerr << "TkGluedMeasurementDet hits " << monoHits.size() << "/" << stereoHits.size() << " => " << result.size() << std::endl;
110  }
111 }
112 #endif
113 
114 #include<cstdint>
115 #include<cstdio>
116 namespace {
117  struct Stat {
118  double totCall=0;
119  double totMono=0;
120  double totStereo=0;
121  double totComb=0;
122  double totMatched=0;
123  double filtMono=0;
124  double filtStereo=0;
125  double filtComb=0;
126  double matchT=0;
127  double matchF=0;
128  double singleF=0;
129  double zeroM=0;
130  double zeroS=0;
131 
132  void match(uint64_t t) {
133  if(t!=0) ++matchT;
134  totMatched+=t;
135  }
136  void operator()(uint64_t m,uint64_t s, uint64_t fm, uint64_t fs) {
137  ++totCall;
138  totMono+=m;
139  totStereo+=s;
140  totComb += m*s;
141  filtMono+=fm;
142  filtStereo+=fs;
143  filtComb += fm*fs;
144  if(fm==0) ++zeroM;
145  if(fs==0) ++zeroS;
146  if(fm!=0&&fs!=0) ++matchF;
147  if(fm!=0||fs!=0) ++singleF;
148  }
149  ~Stat() {
150  if ( totCall>0)
151  printf("Matches:%d/%d/%d/%d/%d/%d : %f/%f/%f/%f/%f/%f/%f\n",
152  int(totCall),int(matchF),int(singleF-matchF),int(matchT),int(zeroM),int(zeroS),
153  totMono/totCall,totStereo/totCall,totComb/totCall,totMatched/matchT,
154  filtMono/totCall,filtStereo/totCall,filtComb/matchF);
155  }
156  };
157 
158  Stat stat;
159 }
160 
161 
162 bool TkGluedMeasurementDet::measurements( const TrajectoryStateOnSurface& stateOnThisDet,
163  const MeasurementEstimator& est,
164  const MeasurementTrackerEvent & data,
165  TempMeasurements & result) const {
166 
167  if unlikely((!theMonoDet->isActive(data)) && (!theStereoDet->isActive(data))) {
168  // LogDebug("TkStripMeasurementDet") << " DetID " << geomDet().geographicalId().rawId() << " (glued) fully inactive";
169  result.add (InvalidTransientRecHit::build(&fastGeomDet(), TrackingRecHit::inactive),0.F);
170  return true;
171  }
172 
173  auto oldSize = result.size();
174 
175  HitCollectorForFastMeasurements collector( &fastGeomDet(), theMatcher, theCPE, stateOnThisDet, est, result);
176  collectRecHits(stateOnThisDet, data, collector);
177 
178 
179  if (result.size()>oldSize) return true;
180 
181  //LogDebug("TkStripMeasurementDet") << "No hit found on TkGlued. Testing strips... ";
182  const BoundPlane &gluedPlane = geomDet().surface();
183  if ( // sorry for the big IF, but I want to exploit short-circuiting of logic
184  stateOnThisDet.hasError() && ( /* do this only if the state has uncertainties, otherwise it will throw
185  (states without uncertainties are passed to this code from seeding */
186  (theMonoDet->isActive(data) &&
187  (theMonoDet->hasAllGoodChannels() ||
188  testStrips(stateOnThisDet,gluedPlane,*theMonoDet)
189  )
190  ) /*Mono OK*/ ||
191  (theStereoDet->isActive(data) &&
192  (theStereoDet->hasAllGoodChannels() ||
193  testStrips(stateOnThisDet,gluedPlane,*theStereoDet)
194  )
195  ) /*Stereo OK*/
196  ) /* State has errors */
197  ) {
198  result.add(InvalidTransientRecHit::build(&fastGeomDet(),TrackingRecHit::missing), 0.F);
199  return false;
200  }
201  result.add(InvalidTransientRecHit::build(&fastGeomDet(), TrackingRecHit::inactive), 0.F);
202  return true;
203 
204 }
205 
206 TkGluedMeasurementDet::RecHitContainer
207 TkGluedMeasurementDet::projectOnGluedDet( const RecHitContainer& hits,
208  const TrajectoryStateOnSurface& ts) const
209 {
210  if (hits.empty()) return hits;
212  RecHitContainer result;
213  for ( RecHitContainer::const_iterator ihit = hits.begin(); ihit!=hits.end(); ihit++) {
214  result.push_back( proj.project( **ihit, fastGeomDet(), ts));
215  }
216  return result;
217 }
218 
219 template<typename Collector>
220 void
221 TkGluedMeasurementDet::projectOnGluedDet( Collector& collector,
222  const RecHitContainer& hits,
223  const GlobalVector & gdir ) const
224 {
225  for ( RecHitContainer::const_iterator ihit = hits.begin(); ihit!=hits.end(); ihit++) {
226  collector.addProjected( **ihit, gdir );
227  }
228 }
229 
230 void TkGluedMeasurementDet::checkProjection(const TrajectoryStateOnSurface& ts,
231  const RecHitContainer& monoHits,
232  const RecHitContainer& stereoHits) const
233 {
234  for (RecHitContainer::const_iterator i=monoHits.begin(); i != monoHits.end(); ++i) {
235  checkHitProjection( **i, ts, fastGeomDet());
236  }
237  for (RecHitContainer::const_iterator i=stereoHits.begin(); i != stereoHits.end(); ++i) {
238  checkHitProjection( **i, ts, fastGeomDet());
239  }
240 }
241 
242 void TkGluedMeasurementDet::checkHitProjection(const TransientTrackingRecHit& hit,
243  const TrajectoryStateOnSurface& ts,
244  const GeomDet& det) const
245 {
247  TransientTrackingRecHit::RecHitPointer projectedHit = proj.project( hit, det, ts);
248 
249  RecHitPropagator prop;
250  TrajectoryStateOnSurface propState = prop.propagate( hit, det.surface(), ts);
251 
252  if ((projectedHit->localPosition()-propState.localPosition()).mag() > 0.0001) {
253  cout << "PROBLEM: projected and propagated hit positions differ by "
254  << (projectedHit->localPosition()-propState.localPosition()).mag() << endl;
255  }
256 
257  LocalError le1 = projectedHit->localPositionError();
258  LocalError le2 = propState.localError().positionError();
259  double eps = 1.e-5;
260  double cutoff = 1.e-4; // if element below cutoff, use absolute instead of relative accuracy
261  double maxdiff = std::max( std::max( fabs(le1.xx() - le2.xx())/(cutoff+le1.xx()),
262  fabs(le1.xy() - le2.xy())/(cutoff+fabs(le1.xy()))),
263  fabs(le1.yy() - le2.yy())/(cutoff+le1.xx()));
264  if (maxdiff > eps) {
265  cout << "PROBLEM: projected and propagated hit errors differ by "
266  << maxdiff << endl;
267  }
268 
269 }
270 
271 bool
272 TkGluedMeasurementDet::testStrips(const TrajectoryStateOnSurface& tsos,
273  const BoundPlane &gluedPlane,
274  const TkStripMeasurementDet &mdet) const {
275  // from TrackingRecHitProjector
276  const GeomDet &det = mdet.fastGeomDet();
277  const BoundPlane &stripPlane = det.surface();
278 
279  //LocalPoint glp = tsos.localPosition();
280  LocalError err = tsos.localError().positionError();
281  /*LogDebug("TkStripMeasurementDet") <<
282  "Testing local pos glued: " << glp <<
283  " local err glued: " << tsos.localError().positionError() <<
284  " in? " << gluedPlane.bounds().inside(glp) <<
285  " in(3s)? " << gluedPlane.bounds().inside(glp, err, 3.0f);*/
286 
287  GlobalVector gdir = tsos.globalParameters().momentum();
288 
289  LocalPoint slp = stripPlane.toLocal(tsos.globalPosition());
290  LocalVector sld = stripPlane.toLocal(gdir);
291 
292  double delta = stripPlane.localZ( tsos.globalPosition());
293  LocalPoint pos = slp - sld * delta/sld.z();
294 
295 
296  // now the error
297  LocalVector hitXAxis = stripPlane.toLocal( gluedPlane.toGlobal( LocalVector(1,0,0)));
298  if (stripPlane.normalVector().dot( gluedPlane.normalVector()) < 0) {
299  // the two planes are inverted, and the correlation element must change sign
300  err = LocalError( err.xx(), -err.xy(), err.yy());
301  }
302  LocalError rotatedError = err.rotate( hitXAxis.x(), hitXAxis.y());
303 
304  /* // This is probably meaningless
305  LogDebug("TkStripMeasurementDet") <<
306  "Testing local pos on strip (SLP): " << slp <<
307  " in? :" << stripPlane.bounds().inside(slp) <<
308  " in(3s)? :" << stripPlane.bounds().inside(slp, rotatedError, 3.0f);
309  // but it helps to test bugs in the formula for POS */
310  /*LogDebug("TkStripMeasurementDet") <<
311  "Testing local pos strip: " << pos <<
312  " in? " << stripPlane.bounds().inside(pos) <<
313  " in(3s)? " << stripPlane.bounds().inside(pos, rotatedError, 3.0f);*/
314 
315  // now we need to convert to MeasurementFrame
316  const StripTopology &topo = mdet.specificGeomDet().specificTopology();
317  float utraj = topo.measurementPosition(pos).x();
318  float uerr = std::sqrt(topo.measurementError(pos,rotatedError).uu());
319  return mdet.testStrips(utraj, uerr);
320 }
321 
322 #include<boost/bind.hpp>
323 TkGluedMeasurementDet::HitCollectorForRecHits::HitCollectorForRecHits(const GeomDet * geomDet,
324  const SiStripRecHitMatcher * matcher, const StripClusterParameterEstimator* cpe,
325  RecHitContainer & target) :
326  geomDet_(geomDet), matcher_(matcher), cpe_(cpe),target_(target),
327  collector_(boost::bind(&HitCollectorForRecHits::add,boost::ref(*this),_1)),
328  hasNewHits_(false)
329 {
330 }
331 
332 void
333 TkGluedMeasurementDet::HitCollectorForRecHits::addProjected(const TransientTrackingRecHit& hit,
334  const GlobalVector & gdir)
335 {
337  target_.push_back( proj.project( hit, *geomDet_, gdir));
338 }
339 
340 TkGluedMeasurementDet::HitCollectorForFastMeasurements::HitCollectorForFastMeasurements
341 (const GeomDet * geomDet,
342  const SiStripRecHitMatcher * matcher, const StripClusterParameterEstimator* cpe,
343  const TrajectoryStateOnSurface& stateOnThisDet,
344  const MeasurementEstimator& est,
345  TempMeasurements & target) :
346  geomDet_(geomDet), matcher_(matcher), cpe_(cpe),stateOnThisDet_(stateOnThisDet), est_(est), target_(target),
347  collector_(boost::bind(&HitCollectorForFastMeasurements::add,boost::ref(*this),_1)),
348  hasNewHits_(false)
349 {
350 }
351 
352 void
353 TkGluedMeasurementDet::HitCollectorForFastMeasurements::add(SiStripMatchedRecHit2D const& hit2d)
354 {
355  static thread_local std::auto_ptr<TSiStripMatchedRecHit> lcache;
356  std::auto_ptr<TSiStripMatchedRecHit> & cache = lcache;
357  TSiStripMatchedRecHit::buildInPlace( cache, geomDet_, &hit2d, matcher_, cpe_ );
358  std::pair<bool,double> diffEst = est_.estimate( stateOnThisDet_, *cache);
359  if ( diffEst.first) {
360  cache->clonePersistentHit(); // clone and take ownership of the persistent 2D hit
361  target_.add(RecHitPointer(cache.release()),
362  diffEst.second);
363  } else {
364  cache->clearPersistentHit(); // drop ownership
365  }
366  hasNewHits_ = true; //FIXME: see also what happens moving this within testAndPush
367 }
368 
369 void
370 TkGluedMeasurementDet::HitCollectorForFastMeasurements::addProjected(const TransientTrackingRecHit& hit,
371  const GlobalVector & gdir)
372 {
373  // here we're ok with some extra casual new's and delete's
375  RecHitPointer && phit = proj.project( hit, *geomDet_, gdir );
376  std::pair<bool,double> diffEst = est_.estimate( stateOnThisDet_, *phit);
377  if ( diffEst.first) {
378  target_.add(phit, diffEst.second);
379  }
380 
381 }
382 
383 
384 
385 #ifdef DOUBLE_MATCH
386 #include "doubleMatch.icc"
387 #endif
dbl * delta
Definition: mlp_gen.cc:36
int i
Definition: DBlmapReader.cc:9
float xx() const
Definition: LocalError.h:24
TrajectoryStateOnSurface propagate(const TransientTrackingRecHit &hit, const Plane &plane, const TrajectoryStateOnSurface &ts) const
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
LocalVector localDirection() const
#define nullptr
int init
Definition: HydjetWrapper.h:62
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
LocalError positionError() const
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
RecHitPointer project(const TransientTrackingRecHit &hit, const GeomDet &det, const TrajectoryStateOnSurface &ts) const
#define unlikely(x)
Definition: Likely.h:21
float xy() const
Definition: LocalError.h:25
virtual MeasurementError measurementError(const LocalPoint &, const LocalError &) const =0
float yy() const
Definition: LocalError.h:26
const T & max(const T &a, const T &b)
T sqrt(T t)
Definition: SSEVec.h:48
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
float uu() const
virtual MeasurementPoint measurementPosition(const LocalPoint &) const =0
const LocalTrajectoryError & localError() const
const T * operator()(const T &val) const
unsigned long long uint64_t
Definition: Time.h:15
const GlobalTrajectoryParameters & globalParameters() const
std::vector< const SiStripRecHit2D * > SimpleHitCollection
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple cout
Definition: gather_cfg.py:121
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6
volatile std::atomic< bool > shutdown_flag false
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
LocalError rotate(float x, float y) const
Return a new LocalError, rotated by an angle defined by the direction (x,y)
Definition: LocalError.h:39
T x() const
Definition: PV2DBase.h:45
long double T
T x() const
Definition: PV3DBase.h:62