CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiHitGeneratorFromChi2.cc
Go to the documentation of this file.
3 
7 
12 
14 
20 
26 
28 
31 
32 #include <algorithm>
33 #include <iostream>
34 #include <vector>
35 #include <cmath>
36 #include <map>
37 #include <limits>
38 
39 using namespace std;
40 
42 
43 namespace {
44  struct LayerRZPredictions {
46  };
47 } // namespace
48 
51  useFixedPreFiltering(cfg.getParameter<bool>("useFixedPreFiltering")),
53  cfg.getParameter<double>("extraHitRZtolerance")), //extra window in ThirdHitRZPrediction range
54  extraHitRPhitolerance(cfg.getParameter<double>(
55  "extraHitRPhitolerance")), //extra window in ThirdHitPredictionFromCircle range (divide by R to get phi)
57  cfg.getParameter<double>("extraZKDBox")), //extra windown in Z when building the KDTree box (used in barrel)
59  cfg.getParameter<double>("extraRKDBox")), //extra windown in R when building the KDTree box (used in endcap)
60  extraPhiKDBox(cfg.getParameter<double>("extraPhiKDBox")), //extra windown in Phi when building the KDTree box
61  fnSigmaRZ(cfg.getParameter<double>(
62  "fnSigmaRZ")), //this multiplies the max hit error on the layer when building the KDTree box
63  chi2VsPtCut(cfg.getParameter<bool>("chi2VsPtCut")),
64  maxChi2(cfg.getParameter<double>("maxChi2")),
65  refitHits(cfg.getParameter<bool>("refitHits")),
66  filterName_(cfg.getParameter<std::string>("ClusterShapeHitFilterName")),
67  builderName_(cfg.existsAs<std::string>("TTRHBuilder") ? cfg.getParameter<std::string>("TTRHBuilder")
68  : std::string("WithTrackAngle")),
69  useSimpleMF_(false),
70  mfName_("") {
72  dphi = cfg.getParameter<double>("phiPreFiltering");
73  if (chi2VsPtCut) {
74  pt_interv = cfg.getParameter<std::vector<double> >("pt_interv");
75  chi2_cuts = cfg.getParameter<std::vector<double> >("chi2_cuts");
76  }
77 #ifdef EDM_ML_DEBUG
78  detIdsToDebug = cfg.getParameter<std::vector<int> >("detIdsToDebug");
79  //if (detIdsToDebug.size()<3) //fixme
80 #else
81  detIdsToDebug.push_back(0);
82  detIdsToDebug.push_back(0);
83  detIdsToDebug.push_back(0);
84 #endif
85  // 2014/02/11 mia:
86  // we should get rid of the boolean parameter useSimpleMF,
87  // and use only a string magneticField [instead of SimpleMagneticField]
88  // or better an edm::ESInputTag (at the moment HLT does not handle ESInputTag)
89  if (cfg.exists("SimpleMagneticField")) {
90  useSimpleMF_ = true;
91  mfName_ = cfg.getParameter<std::string>("SimpleMagneticField");
92  }
93  filter = nullptr;
94  bfield = nullptr;
95  nomField = -1.;
96 
97  if (useSimpleMF_) {
99  } else {
101  }
102  if (refitHits) {
105  }
106 }
107 
109 
112 
113  // fixed phi filtering
114  desc.add<bool>("useFixedPreFiltering", false);
115  desc.add<double>("phiPreFiltering", 0.3);
116 
117  // box properties
118  desc.add<double>("extraHitRPhitolerance", 0);
119  desc.add<double>("extraHitRZtolerance", 0);
120  desc.add<double>("extraZKDBox", 0.2);
121  desc.add<double>("extraRKDBox", 0.2);
122  desc.add<double>("extraPhiKDBox", 0.005);
123  desc.add<double>("fnSigmaRZ", 2.0);
124 
125  // refit&filter hits
126  desc.add<bool>("refitHits", true);
127  desc.add<std::string>("ClusterShapeHitFilterName", "ClusterShapeHitFilter");
128  desc.add<std::string>("TTRHBuilder", "WithTrackAngle");
129 
130  // chi2 cuts
131  desc.add<double>("maxChi2", 5.0);
132  desc.add<bool>("chi2VsPtCut", true);
133  desc.add<std::vector<double> >("pt_interv", std::vector<double>{{0.4, 0.7, 1.0, 2.0}});
134  desc.add<std::vector<double> >("chi2_cuts", std::vector<double>{{3.0, 4.0, 5.0, 5.0}});
135 
136  // debugging
137  desc.add<std::vector<int> >("detIdsToDebug", std::vector<int>{{0, 0, 0}});
138 }
139 
143  ufield.set(nomField); // more than enough (never actually used)
144 
145  if (refitHits) {
147  auto const& builderRef = es.getData(transientTrackingRecHitBuilderESToken_);
148  builder = (TkTransientTrackingRecHitBuilder const*)(&builderRef);
149  cloner = (*builder).cloner();
150  }
151 }
152 
153 namespace {
154  inline bool intersect(Range& range, const Range& second) {
155  if (range.first > second.max() || range.second < second.min())
156  return false;
157  if (range.first < second.min())
158  range.first = second.min();
159  if (range.second > second.max())
160  range.second = second.max();
161  return range.first < range.second;
162  }
163 } // namespace
164 
167  const edm::Event& ev,
168  const edm::EventSetup& es,
170  std::vector<SeedingLayerSetsHits::SeedingLayer> thirdLayers) {
171  LogDebug("MultiHitGeneratorFromChi2") << "pair: " << thePairGenerator->innerLayer(pairLayers).name() << "+"
172  << thePairGenerator->outerLayer(pairLayers).name()
173  << " 3rd lay size: " << thirdLayers.size();
174 
175  auto const& doublets = thePairGenerator->doublets(region, ev, es, pairLayers);
176  LogTrace("MultiHitGeneratorFromChi2") << "";
177  if (doublets.empty()) {
178  // LogDebug("MultiHitGeneratorFromChi2") << "empy pairs";
179  return;
180  }
181 
183  hitSets(region, result, doublets, thirdLayers, *theLayerCache, cache);
184 }
185 
188  const HitDoublets& doublets,
189  const std::vector<SeedingLayerSetsHits::SeedingLayer>& thirdLayers,
190  LayerCacheType& layerCache,
191  cacheHits& refittedHitStorage) {
192  int size = thirdLayers.size();
193  const RecHitsSortedInPhi* thirdHitMap[size];
194  vector<const DetLayer*> thirdLayerDetLayer(size, nullptr);
195  for (int il = 0; il < size; ++il) {
196  thirdHitMap[il] = &layerCache(thirdLayers[il], region);
197 
198  thirdLayerDetLayer[il] = thirdLayers[il].detLayer();
199  }
200  hitSets(region, result, doublets, thirdHitMap, thirdLayerDetLayer, size, refittedHitStorage);
201 }
202 
205  const HitDoublets& doublets,
206  const RecHitsSortedInPhi** thirdHitMap,
207  const std::vector<const DetLayer*>& thirdLayerDetLayer,
208  const int nThirdLayers) {
209  hitSets(region, result, doublets, thirdHitMap, thirdLayerDetLayer, nThirdLayers, cache);
210 }
211 
214  const HitDoublets& doublets,
215  const RecHitsSortedInPhi** thirdHitMap,
216  const std::vector<const DetLayer*>& thirdLayerDetLayer,
217  const int nThirdLayers,
218  cacheHits& refittedHitStorage) {
219 #ifdef EDM_ML_DEBUG
220  unsigned int debug_Id0 = detIdsToDebug[0];
221  unsigned int debug_Id1 = detIdsToDebug[1];
222  unsigned int debug_Id2 = detIdsToDebug[2];
223 #endif
224 
225  std::array<bool, 3> bl;
226  bl[0] = doublets.innerLayer().isBarrel;
227  bl[1] = doublets.outerLayer().isBarrel;
228 
229  //gc: these are all the layers compatible with the layer pairs (as defined in the config file)
230 
231  //gc: initialize a KDTree per each 3rd layer
232  std::vector<KDTreeNodeInfo<RecHitsSortedInPhi::HitIter, 2> > layerTree; // re-used throughout
233  std::vector<RecHitsSortedInPhi::HitIter> foundNodes; // re-used thoughout
234  foundNodes.reserve(100);
236  declareDynArray(LayerRZPredictions, nThirdLayers, mapPred);
237  float rzError[nThirdLayers]; //save maximum errors
238 
239  const float maxDelphi = region.ptMin() < 0.3f ? float(M_PI) / 4.f : float(M_PI) / 8.f; // FIXME move to config??
240  const float maxphi = M_PI + maxDelphi, minphi = -maxphi; // increase to cater for any range
241  const float safePhi = M_PI - maxDelphi; // sideband
242 
243  //gc: loop over each layer
244  for (int il = 0; il < nThirdLayers; il++) {
245  LogTrace("MultiHitGeneratorFromChi2")
246  << "considering third layer: with hits: " << thirdHitMap[il]->all().second - thirdHitMap[il]->all().first;
247  const DetLayer* layer = thirdLayerDetLayer[il];
248  LayerRZPredictions& predRZ = mapPred[il];
249  predRZ.line.initLayer(layer);
250  predRZ.line.initTolerance(extraHitRZtolerance);
251 
252  //gc: now we take all hits in the layer and fill the KDTree
253  auto const& layer3 = *thirdHitMap[il]; // Get iterators
254  layerTree.clear();
255  float minz = 999999.0f, maxz = -minz; // Initialise to extreme values in case no hits
256  float maxErr = 0.0f;
257  if (!layer3.empty()) {
258  minz = layer3.v[0];
259  maxz = minz; //In case there's only one hit on the layer
260  for (auto i = 0U; i < layer3.size(); ++i) {
261  auto hi = layer3.theHits.begin() + i;
262  auto angle = layer3.phi(i);
263  auto myz = layer3.v[i];
264 #ifdef EDM_ML_DEBUG
265  IfLogTrace(hi->hit()->rawId() == debug_Id2, "MultiHitGeneratorFromChi2")
266  << "filling KDTree with hit in id=" << debug_Id2 << " with pos: " << hi->hit()->globalPosition()
267  << " phi=" << hi->hit()->globalPosition().phi() << " z=" << hi->hit()->globalPosition().z()
268  << " r=" << hi->hit()->globalPosition().perp();
269 #endif
270  //use (phi,r) for endcaps rather than (phi,z)
271  if (myz < minz) {
272  minz = myz;
273  } else {
274  if (myz > maxz) {
275  maxz = myz;
276  }
277  }
278  auto myerr = layer3.dv[i];
279  if (myerr > maxErr) {
280  maxErr = myerr;
281  }
282  layerTree.push_back(KDTreeNodeInfo<RecHitsSortedInPhi::HitIter, 2>(hi, angle, myz)); // save it
283  // populate side-bands
284  if (angle > safePhi)
285  layerTree.push_back(
287  else if (angle < -safePhi)
288  layerTree.push_back(
290  }
291  }
292  KDTreeBox phiZ(minphi, maxphi, minz - 0.01f, maxz + 0.01f); // declare our bounds
293  //add fudge factors in case only one hit and also for floating-point inaccuracy
294  hitTree[il].build(layerTree, phiZ); // make KDtree
295  rzError[il] = maxErr; //save error
296  }
297  //gc: now we have initialized the KDTrees and we are out of the layer loop
298 
299  //gc: this sets the minPt of the triplet
300  auto curv = PixelRecoUtilities::curvature(1. / region.ptMin(), *bfield);
301 
302  LogTrace("MultiHitGeneratorFromChi2") << "doublet size=" << doublets.size() << std::endl;
303 
304  //fixme add pixels
305  auto filterHit = [&](TrackingRecHit const* hit, GlobalVector const& dir) -> bool {
306  auto hh = reinterpret_cast<BaseTrackerRecHit const*>(hit);
307  if ( //hh->geographicalId().subdetId() > 2
308  hh->geographicalId().subdetId() == SiStripDetId::TIB || hh->geographicalId().subdetId() == SiStripDetId::TID
309  //|| hh->geographicalId().subdetId()==SiStripDetId::TOB
310  //|| hh->geographicalId().subdetId()==SiStripDetId::TEC
311  ) {
312  // carefull " for matched and projected local of tsos != local for individual clusters...
313  if (hh->isMatched()) {
314  const SiStripMatchedRecHit2D* matchedHit = reinterpret_cast<const SiStripMatchedRecHit2D*>(hh);
315  if (filter->isCompatible(DetId(matchedHit->monoId()), matchedHit->monoCluster(), dir) == 0 ||
316  filter->isCompatible(DetId(matchedHit->stereoId()), matchedHit->stereoCluster(), dir) == 0)
317  return false;
318  } else if (hh->isProjected()) {
319  const ProjectedSiStripRecHit2D* precHit = reinterpret_cast<const ProjectedSiStripRecHit2D*>(hh);
320  if (filter->isCompatible(precHit->originalHit(), dir) == 0)
321  return false; //FIXME??
322  } else if (hh->isSingle()) {
323  const SiStripRecHit2D* recHit = reinterpret_cast<const SiStripRecHit2D*>(hh);
324  if (filter->isCompatible(*recHit, dir) == 0)
325  return false;
326  }
327  }
328  return true;
329  };
330 
331  //gc: now we loop over all pairs
332  for (std::size_t ip = 0; ip != doublets.size(); ip++) {
333  int foundTripletsFromPair = 0;
334  bool usePair = false;
335  cacheHitPointer bestH2;
336  float minChi2 = std::numeric_limits<float>::max();
337 
340 
341  HitOwnPtr hit0(*oriHit0);
342  HitOwnPtr hit1(*oriHit1);
343  GlobalPoint gp0 = doublets.gp(ip, HitDoublets::inner);
344  GlobalPoint gp1 = doublets.gp(ip, HitDoublets::outer);
345 
346 #ifdef EDM_ML_DEBUG
347  bool debugPair = oriHit0->rawId() == debug_Id0 && oriHit1->rawId() == debug_Id1;
348 #endif
349  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << endl
350  << endl
351  << "found new pair with ids " << oriHit0->rawId() << " "
352  << oriHit1->rawId() << " with pos: " << gp0 << " " << gp1;
353 
354  if (refitHits) {
355  TrajectoryStateOnSurface tsos0, tsos1;
356  assert(!hit0.isOwn());
357  assert(!hit1.isOwn());
358 #ifdef EDM_ML_DEBUG
359  refit2Hits(hit0, hit1, tsos0, tsos1, region, nomField, debugPair);
360 #else
361  refit2Hits(hit0, hit1, tsos0, tsos1, region, nomField, false);
362 #endif
363 
364  bool passFilterHit0 = filterHit(hit0->hit(), tsos0.globalMomentum());
365  IfLogTrace(debugPair && !passFilterHit0, "MultiHitGeneratorFromChi2") << "hit0 did not pass cluster shape filter";
366  if (!passFilterHit0)
367  continue;
368  bool passFilterHit1 = filterHit(hit1->hit(), tsos1.globalMomentum());
369  IfLogTrace(debugPair && !passFilterHit1, "MultiHitGeneratorFromChi2") << "hit1 did not pass cluster shape filter";
370  if (!passFilterHit1)
371  continue;
372  // refit hits
373  hit0.reset((SeedingHitSet::RecHitPointer)(cloner(*hit0, tsos0)));
374  hit1.reset((SeedingHitSet::RecHitPointer)(cloner(*hit1, tsos1)));
375 
376 #ifdef EDM_ML_DEBUG
377  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2")
378  << "charge=" << tsos0.charge() << std::endl
379  << "state1 pt=" << tsos0.globalMomentum().perp() << " eta=" << tsos0.globalMomentum().eta()
380  << " phi=" << tsos0.globalMomentum().phi() << std::endl
381  << "state2 pt=" << tsos1.globalMomentum().perp() << " eta=" << tsos1.globalMomentum().eta()
382  << " phi=" << tsos1.globalMomentum().phi() << std::endl
383  << "positions after refitting: " << hit0->globalPosition() << " " << hit1->globalPosition();
384 #endif
385  } else {
386  // not refit clone anyhow
387  hit0.reset((BaseTrackerRecHit*)hit0->clone());
388  hit1.reset((BaseTrackerRecHit*)hit1->clone());
389  }
390 
391  assert(hit0.isOwn());
392  assert(hit1.isOwn());
393 
394  //gc: create the RZ line for the pair
395  SimpleLineRZ line(PixelRecoPointRZ(gp0.perp(), gp0.z()), PixelRecoPointRZ(gp1.perp(), gp1.z()));
396  ThirdHitPredictionFromCircle predictionRPhi(gp0, gp1, extraHitRPhitolerance);
397 
398  auto toPos = std::signbit(gp1.z() - gp0.z());
399 
400  //gc: this is the curvature of the two hits assuming the region
401  Range pairCurvature = predictionRPhi.curvature(region.originRBound());
402  //gc: intersect not only returns a bool but may change pairCurvature to intersection with curv
403  if (!intersect(pairCurvature, Range(-curv, curv))) {
404  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2")
405  << "curvature cut: curv=" << curv << " gc=(" << pairCurvature.first << ", " << pairCurvature.second << ")";
406  continue;
407  }
408 
409  std::array<GlobalPoint, 3> gp;
410  std::array<GlobalError, 3> ge;
411  gp[0] = hit0->globalPosition();
412  ge[0] = hit0->globalPositionError();
413  gp[1] = hit1->globalPosition();
414  ge[1] = hit1->globalPositionError();
415 
416  //gc: loop over all third layers compatible with the pair
417  for (int il = 0; (il < nThirdLayers) & (!usePair); il++) {
418  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2")
419  << "cosider layer:"
420  << " for this pair. Location: " << thirdLayerDetLayer[il]->location();
421 
422  if (hitTree[il].empty()) {
423  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "empty hitTree";
424  continue; // Don't bother if no hits
425  }
426 
427  cacheHitPointer bestL2;
428  float chi2FromThisLayer = std::numeric_limits<float>::max();
429 
430  const DetLayer* layer = thirdLayerDetLayer[il];
431  // bool barrelLayer = layer->location() == GeomDetEnumerators::barrel;
432  auto const& layer3 = *thirdHitMap[il];
433  bool barrelLayer = layer3.isBarrel;
434  bl[2] = layer3.isBarrel;
435 
436  if ((!barrelLayer) & (toPos != std::signbit(layer->position().z())))
437  continue;
438 
439  LayerRZPredictions& predRZ = mapPred[il];
440  predRZ.line.initPropagator(&line);
441 
442  //gc: this takes the z at R-thick/2 and R+thick/2 according to
443  // the line from the two points and the adds the extra tolerance
444  Range rzRange = predRZ.line();
445 
446  if (rzRange.first >= rzRange.second) {
447  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "rzRange empty";
448  continue;
449  }
450  //gc: check that rzRange is compatible with detector bounds
451  // note that intersect may change rzRange to intersection with bounds
452  if (!intersect(rzRange, predRZ.line.detSize())) { // theDetSize = Range(-maxZ, maxZ);
453  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "rzRange and detector do not intersect";
454  continue;
455  }
456  Range radius = barrelLayer ? predRZ.line.detRange() : rzRange;
457 
458  //gc: define the phi range of the hits
459  Range phiRange;
460  if (useFixedPreFiltering) {
461  //gc: in this case it takes as range the phi of the outer
462  // hit +/- the phiPreFiltering value from cfg
463  float phi0 = oriHit0->globalPosition().phi();
464  phiRange = Range(phi0 - dphi, phi0 + dphi);
465  } else {
466  //gc: predictionRPhi uses the cosine rule to find the phi of the 3rd point at radius, assuming the pairCurvature range [-c,+c]
467  if (pairCurvature.first < 0. && pairCurvature.second < 0.) {
468  radius.swap();
469  } else if (pairCurvature.first >= 0. && pairCurvature.second >= 0.) {
470  ;
471  } else {
472  radius.first = radius.second;
473  }
474  auto phi12 = predictionRPhi.phi(pairCurvature.first, radius.first);
475  auto phi22 = predictionRPhi.phi(pairCurvature.second, radius.second);
476  phi12 = normalizedPhi(phi12);
477  phi22 = proxim(phi22, phi12);
478  phiRange = Range(phi12, phi22);
479  phiRange.sort();
480  }
481 
482  float prmin = phiRange.min(), prmax = phiRange.max();
483 
484  if (prmax - prmin > maxDelphi) {
485  auto prm = phiRange.mean();
486  prmin = prm - 0.5f * maxDelphi;
487  prmax = prm + 0.5f * maxDelphi;
488  }
489 
490  //gc: this is the place where hits in the compatible region are put in the foundNodes
492  foundNodes.clear(); // Now recover hits in bounding box...
493 
494  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "defining kd tree box";
495 
496  if (barrelLayer) {
497  KDTreeBox phiZ(prmin - extraPhiKDBox,
498  prmax + extraPhiKDBox,
499  float(rzRange.min() - fnSigmaRZ * rzError[il] - extraZKDBox),
500  float(rzRange.max() + fnSigmaRZ * rzError[il] + extraZKDBox));
501  hitTree[il].search(phiZ, foundNodes);
502 
503  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2")
504  << "kd tree box bounds, phi: " << prmin - extraPhiKDBox << "," << prmax + extraPhiKDBox
505  << " z: " << rzRange.min() - fnSigmaRZ * rzError[il] - extraZKDBox << ","
506  << rzRange.max() + fnSigmaRZ * rzError[il] + extraZKDBox << " rzRange: " << rzRange.min() << ","
507  << rzRange.max();
508 
509  } else {
510  KDTreeBox phiR(prmin - extraPhiKDBox,
511  prmax + extraPhiKDBox,
512  float(rzRange.min() - fnSigmaRZ * rzError[il] - extraRKDBox),
513  float(rzRange.max() + fnSigmaRZ * rzError[il] + extraRKDBox));
514  hitTree[il].search(phiR, foundNodes);
515 
516  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2")
517  << "kd tree box bounds, phi: " << prmin - extraPhiKDBox << "," << prmax + extraPhiKDBox
518  << " r: " << rzRange.min() - fnSigmaRZ * rzError[il] - extraRKDBox << ","
519  << rzRange.max() + fnSigmaRZ * rzError[il] + extraRKDBox << " rzRange: " << rzRange.min() << ","
520  << rzRange.max();
521  }
522 
523  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "kd tree box size: " << foundNodes.size();
524 
525  //gc: now we loop over the hits in the box for this layer
526  for (std::vector<RecHitsSortedInPhi::HitIter>::iterator ih = foundNodes.begin();
527  ih != foundNodes.end() && !usePair;
528  ++ih) {
529  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << endl << "triplet candidate";
530 
531  const RecHitsSortedInPhi::HitIter KDdata = *ih;
532 
533  auto oriHit2 = KDdata->hit();
534  auto kk = KDdata - layer3.theHits.begin();
535  cacheHitPointer hit2;
536  auto gp2 = layer3.gp(kk);
537  if (refitHits) { //fixme
538 
539  //fitting all 3 hits takes too much time... do it quickly only for 3rd hit
540  GlobalVector initMomentum(gp2 - gp1);
541  initMomentum /= initMomentum.perp(); //set pT=1
542 
543  //fixme add pixels
544  bool passFilterHit2 = filterHit(oriHit2->hit(), initMomentum);
545  if (!passFilterHit2)
546  continue;
548  *oriHit2->surface());
549  hit2.reset((SeedingHitSet::RecHitPointer)(cloner(*oriHit2, state)));
550 
551  } else {
552  // not refit clone anyhow
553  hit2.reset((BaseTrackerRecHit*)oriHit2->clone());
554  }
555 
556  //gc: add the chi2 cut
557  gp[2] = hit2->globalPosition();
558  ge[2] = hit2->globalPositionError();
559  RZLine rzLine(gp, ge, bl);
560  float chi2 = rzLine.chi2();
561 
562 #ifdef EDM_ML_DEBUG
563  bool debugTriplet = debugPair && hit2->rawId() == debug_Id2;
564 #endif
565  IfLogTrace(debugTriplet, "MultiHitGeneratorFromChi2")
566  << endl
567  << "triplet candidate in debug id" << std::endl
568  << "hit in id=" << hit2->rawId() << " (from KDTree) with pos: " << KDdata->hit()->globalPosition()
569  << " refitted: " << hit2->globalPosition() << " chi2: " << chi2;
570  // should fix nan
571  if ((chi2 > maxChi2) | edm::isNotFinite(chi2))
572  continue;
573 
574  if (chi2VsPtCut) {
575  FastCircle theCircle(gp[2], gp[1], gp[0]);
576  float tesla0 = 0.1f * nomField;
577  float rho = theCircle.rho();
578  float cm2GeV = 0.01f * 0.3f * tesla0;
579  float pt = cm2GeV * rho;
580  IfLogTrace(debugTriplet, "MultiHitGeneratorFromChi2") << "triplet pT=" << pt;
581  if (pt < region.ptMin())
582  continue;
583 
584  if (chi2_cuts.size() == 4) {
585  if ((pt > pt_interv[0] && pt <= pt_interv[1] && chi2 > chi2_cuts[0]) ||
586  (pt > pt_interv[1] && pt <= pt_interv[2] && chi2 > chi2_cuts[1]) ||
587  (pt > pt_interv[2] && pt <= pt_interv[3] && chi2 > chi2_cuts[2]) ||
588  (pt > pt_interv[3] && chi2 > chi2_cuts[3]))
589  continue;
590  }
591 
592  // apparently this takes too much time...
593  // if (chi2_cuts.size()>1) {
594  // int ncuts = chi2_cuts.size();
595  // if ( pt<=pt_interv[0] && chi2 > chi2_cuts[0] ) continue;
596  // bool pass = true;
597  // for (int icut=1; icut<ncuts-1; icut++){
598  // if ( pt>pt_interv[icut-1] && pt<=pt_interv[icut] && chi2 > chi2_cuts[icut] ) pass=false;
599  // }
600  // if (!pass) continue;
601  // if ( pt>pt_interv[ncuts-2] && chi2 > chi2_cuts[ncuts-1] ) continue;
602  // if (hit0->rawId()==debug_Id0 && hit1->rawId()==debug_Id1 && hit2->rawId()==debug_Id2) {
603  // LogTrace("MultiHitGeneratorFromChi2") << "triplet passed chi2 vs pt cut" << std::endl;
604  // }
605  // }
606  }
607 
608  if (theMaxElement != 0 && result.size() >= theMaxElement) {
609  result.clear();
610  edm::LogError("TooManyTriplets") << " number of triples exceed maximum. no triplets produced.";
611  return;
612  }
613  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "triplet made";
614  //result.push_back(SeedingHitSet(hit0, hit1, hit2));
615  /* no refit so keep only hit2
616  assert(tripletFromThisLayer.empty());
617  assert(hit0.isOwn()); assert(hit1.isOwn());assert(hit2.isOwn());
618  tripletFromThisLayer.emplace_back(std::move(hit0));
619  tripletFromThisLayer.emplace_back(std::move(hit1));
620  tripletFromThisLayer.emplace_back(std::move(hit2));
621  assert(hit0.isEmpty()); assert(hit1.isEmpty());assert(hit2.isEmpty());
622  */
623  bestL2 = std::move(hit2);
624  chi2FromThisLayer = chi2;
625  foundTripletsFromPair++;
626  if (foundTripletsFromPair >= 2) {
627  usePair = true;
628  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "using pair";
629  break;
630  }
631  } //loop over hits in KDTree
632 
633  if (usePair)
634  break;
635  else {
636  //if there is one triplet in more than one layer, try picking the one with best chi2
637  if (chi2FromThisLayer < minChi2) {
638  bestH2 = std::move(bestL2);
639  minChi2 = chi2FromThisLayer;
640  }
641  /*
642  else {
643  if (!bestH2 && foundTripletsFromPair>0)
644  LogTrace("MultiHitGeneratorFromChi2") << "what?? " << minChi2 << ' ' << chi2FromThisLayer;
645  }
646  */
647  }
648 
649  } //loop over layers
650 
651  if (foundTripletsFromPair == 0)
652  continue;
653 
654  //push back only (max) once per pair
655  IfLogTrace(debugPair, "MultiHitGeneratorFromChi2") << "Done seed #" << result.size();
656  if (usePair)
657  result.push_back(SeedingHitSet(oriHit0, oriHit1));
658  else {
659  assert(1 == foundTripletsFromPair);
660  assert(bestH2);
661  result.emplace_back(&*hit0, &*hit1, &*bestH2);
662  assert(hit0.isOwn());
663  assert(hit1.isOwn());
664  refittedHitStorage.emplace_back(const_cast<BaseTrackerRecHit*>(hit0.release()));
665  refittedHitStorage.emplace_back(const_cast<BaseTrackerRecHit*>(hit1.release()));
666  refittedHitStorage.emplace_back(std::move(bestH2));
667  assert(hit0.empty());
668  assert(hit1.empty());
669  assert(!bestH2);
670  }
671  // LogTrace("MultiHitGeneratorFromChi2") << (usePair ? "pair " : "triplet ") << minChi2 <<' ' << refittedHitStorage.size();
672 
673  } //loop over pairs
674  LogTrace("MultiHitGeneratorFromChi2") << "triplet size=" << result.size();
675  // std::cout << "MultiHitGeneratorFromChi2 " << "triplet size=" << result.size() << std::endl;
676 }
677 
679  HitOwnPtr& hit2,
680  TrajectoryStateOnSurface& state1,
681  TrajectoryStateOnSurface& state2,
682  const TrackingRegion& region,
683  float nomField,
684  bool isDebug) {
685  //these need to be sorted in R
686  const GlobalPoint& gp0 = region.origin();
687  GlobalPoint gp1 = hit1->globalPosition();
688  GlobalPoint gp2 = hit2->globalPosition();
689 
690  IfLogTrace(isDebug, "MultiHitGeneratorFromChi2")
691  << "positions before refitting: " << hit1->globalPosition() << " " << hit2->globalPosition();
692 
693  FastCircle theCircle(gp2, gp1, gp0);
694  GlobalPoint cc(theCircle.x0(), theCircle.y0(), 0);
695  float tesla0 = 0.1f * nomField;
696  float rho = theCircle.rho();
697  float cm2GeV = 0.01f * 0.3f * tesla0;
698  float pt = cm2GeV * rho;
699 
700  GlobalVector vec20 = gp2 - gp0;
701  //if (isDebug) { cout << "vec20.eta=" << vec20.eta() << endl; }
702 
703  GlobalVector p0(gp0.y() - cc.y(), -gp0.x() + cc.x(), 0.);
704  p0 = p0 * pt / p0.perp();
705  GlobalVector p1(gp1.y() - cc.y(), -gp1.x() + cc.x(), 0.);
706  p1 = p1 * pt / p1.perp();
707  GlobalVector p2(gp2.y() - cc.y(), -gp2.x() + cc.x(), 0.);
708  p2 = p2 * pt / p2.perp();
709 
710  //check sign according to scalar product
711  if ((p0.x() * (gp1.x() - gp0.x()) + p0.y() * (gp1.y() - gp0.y())) < 0) {
712  p0 *= -1.;
713  p1 *= -1.;
714  p2 *= -1.;
715  }
716 
717  //now set z component
718  auto zv = vec20.z() / vec20.perp();
719  p0 = GlobalVector(p0.x(), p0.y(), p0.perp() * zv);
720  p1 = GlobalVector(p1.x(), p1.y(), p1.perp() * zv);
721  p2 = GlobalVector(p2.x(), p2.y(), p2.perp() * zv);
722 
723  //get charge from vectorial product
724  TrackCharge q = 1;
725  if ((gp1 - cc).x() * p1.y() - (gp1 - cc).y() * p1.x() > 0)
726  q = -q;
727 
728  TrajectoryStateOnSurface(GlobalTrajectoryParameters(gp1, p1, q, &ufield), *hit1->surface()).swap(state1);
729  // hit1.reset((SeedingHitSet::RecHitPointer)(cloner(*hit1,state1)));
730 
731  TrajectoryStateOnSurface(GlobalTrajectoryParameters(gp2, p2, q, &ufield), *hit2->surface()).swap(state2);
732  // hit2.reset((SeedingHitSet::RecHitPointer)(cloner(*hit2,state2)));
733 }
734 
735 /*
736 void MultiHitGeneratorFromChi2::refit3Hits(HitOwnPtr & hit0,
737  HitOwnPtr & hit1,
738  HitOwnPtr & hit2,
739  TrajectoryStateOnSurface& state0,
740  TrajectoryStateOnSurface& state1,
741  TrajectoryStateOnSurface& state2,
742  float nomField, bool isDebug) {
743 
744  //these need to be sorted in R
745  GlobalPoint gp0 = hit0->globalPosition();
746  GlobalPoint gp1 = hit1->globalPosition();
747  GlobalPoint gp2 = hit2->globalPosition();
748 
749  IfLogTrace(isDebug, "MultiHitGeneratorFromChi2") << "positions before refitting: " << hit0->globalPosition() << " " << hit1->globalPosition() << " " << hit2->globalPosition();
750  }
751 
752  FastCircle theCircle(gp2,gp1,gp0);
753  GlobalPoint cc(theCircle.x0(),theCircle.y0(),0);
754  float tesla0 = 0.1*nomField;
755  float rho = theCircle.rho();
756  float cm2GeV = 0.01 * 0.3*tesla0;
757  float pt = cm2GeV * rho;
758 
759  GlobalVector vec20 = gp2-gp0;
760  //IfLogTrace(isDebug, "MultiHitGeneratorFromChi2") << "vec20.eta=" << vec20.eta();
761 
762  GlobalVector p0( gp0.y()-cc.y(), -gp0.x()+cc.x(), 0. );
763  p0 = p0*pt/p0.perp();
764  GlobalVector p1( gp1.y()-cc.y(), -gp1.x()+cc.x(), 0. );
765  p1 = p1*pt/p1.perp();
766  GlobalVector p2( gp2.y()-cc.y(), -gp2.x()+cc.x(), 0. );
767  p2 = p2*pt/p2.perp();
768 
769  //check sign according to scalar product
770  if ( (p0.x()*(gp1.x()-gp0.x())+p0.y()*(gp1.y()-gp0.y()) ) < 0 ) {
771  p0*=-1.;
772  p1*=-1.;
773  p2*=-1.;
774  }
775 
776  //now set z component
777  p0 = GlobalVector(p0.x(),p0.y(),p0.perp()/tan(vec20.theta()));
778  p1 = GlobalVector(p1.x(),p1.y(),p1.perp()/tan(vec20.theta()));
779  p2 = GlobalVector(p2.x(),p2.y(),p2.perp()/tan(vec20.theta()));
780 
781  //get charge from vectorial product
782  TrackCharge q = 1;
783  if ((gp1-cc).x()*p1.y() - (gp1-cc).y()*p1.x() > 0) q =-q;
784 
785  GlobalTrajectoryParameters kine0 = GlobalTrajectoryParameters(gp0, p0, q, &*bfield);
786  state0 = TrajectoryStateOnSurface(kine0,*hit0->surface());
787  hit0 = hit0->clone(state0);
788 
789  GlobalTrajectoryParameters kine1 = GlobalTrajectoryParameters(gp1, p1, q, &*bfield);
790  state1 = TrajectoryStateOnSurface(kine1,*hit1->surface());
791  hit1 = hit1->clone(state1);
792 
793  GlobalTrajectoryParameters kine2 = GlobalTrajectoryParameters(gp2, p2, q, &*bfield);
794  state2 = TrajectoryStateOnSurface(kine2,*hit2->surface());
795  hit2 = hit2->clone(state2);
796 
797  IfLogTrace(isDebug, "MultiHitGeneratorFromChi2") << "charge=" << q << std::endl
798  << "state0 pt=" << state0.globalMomentum().perp() << " eta=" << state0.globalMomentum().eta() << " phi=" << state0.globalMomentum().phi() << std::endl
799  << "state1 pt=" << state1.globalMomentum().perp() << " eta=" << state1.globalMomentum().eta() << " phi=" << state1.globalMomentum().phi() << std::endl
800  << "state2 pt=" << state2.globalMomentum().perp() << " eta=" << state2.globalMomentum().eta() << " phi=" << state2.globalMomentum().phi() << std::endl
801  << "positions after refitting: " << hit0->globalPosition() << " " << hit1->globalPosition() << " " << hit2->globalPosition();
802 }
803 */
float originRBound() const
bounds the particle vertex in the transverse plane
unsigned int size() const override
std::size_t size() const
unsigned int stereoId() const
tuple cfg
Definition: looper.py:296
MultiHitGeneratorFromChi2(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
bool empty() const
Definition: mayown_ptr.h:85
T perp() const
Definition: PV3DBase.h:69
std::unique_ptr< BaseTrackerRecHit > cacheHitPointer
GlobalPoint const & origin() const
const TString p2
Definition: fwPaths.cc:13
HitLayer const & innerLayer() const
int nominalValue() const
The nominal field value for this map in kGauss.
Definition: MagneticField.h:49
#define IfLogTrace(cond, cat)
double x0() const
Definition: FastCircle.h:43
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
PixelRecoRange< float > Range
static constexpr auto TID
Definition: SiStripDetId.h:38
TkTransientTrackingRecHitBuilder const * builder
bool isOwn() const
Definition: mayown_ptr.h:28
SiStripCluster const & monoCluster() const
const ClusterShapeHitFilter * filter
constexpr T normalizedPhi(T phi)
Definition: normalizedPhi.h:8
float *__restrict__ zv
void hitTriplets(const TrackingRegion &region, OrderedMultiHits &result, const HitDoublets &doublets, const RecHitsSortedInPhi **thirdHitMap, const std::vector< const DetLayer * > &thirdLayerDetLayer, const int nThirdLayers) override
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T y() const
Definition: PV3DBase.h:60
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool ev
Log< level::Error, false > LogError
assert(be >=bs)
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > transientTrackingRecHitBuilderESToken_
double rho() const
Definition: FastCircle.h:47
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
T curvature(T InversePt, const MagneticField &field)
#define LogTrace(id)
void reset()
Definition: mayown_ptr.h:72
constexpr T proxim(T b, T a)
Definition: normalizedPhi.h:14
constexpr std::array< uint8_t, layerIndexSize > layer
tuple extraHitRZtolerance
T const * release()
Definition: mayown_ptr.h:67
const uint16_t range(const Frame &aFrame)
tuple result
Definition: mps_fire.py:311
Range curvature(double transverseIP) const
__constant__ float const minz[nPairs]
bool getData(T &iHolder) const
Definition: EventSetup.h:128
U second(std::pair< T, U > const &p)
HitLayer const & outerLayer() const
constexpr float fnSigmaRZ
int TrackCharge
Definition: TrackCharge.h:4
BaseTrackerRecHit const * ConstRecHitPointer
Definition: SeedingHitSet.h:10
std::vector< HitWithPhi >::const_iterator HitIter
T z() const
Definition: PV3DBase.h:61
edm::ESGetToken< ClusterShapeHitFilter, CkfComponentsRecord > clusterShapeHitFilterESToken_
def move
Definition: eostools.py:511
void initES(const edm::EventSetup &es) override
const TString p1
Definition: fwPaths.cc:12
GlobalPoint gp(int i, layer l) const
tuple extraHitRPhitolerance
ParameterDescriptionBase * add(U const &iLabel, T const &value)
BaseTrackerRecHit const * Hit
#define M_PI
Definition: RZLine.h:12
SiStripRecHit2D originalHit() const
Definition: DetId.h:17
void hitSets(const TrackingRegion &region, OrderedMultiHits &trs, const edm::Event &ev, const edm::EventSetup &es, SeedingLayerSetsHits::SeedingLayerSet pairLayers, std::vector< SeedingLayerSetsHits::SeedingLayer > thirdLayers) override
__constant__ float const maxz[nPairs]
virtual const Surface::PositionType & position() const
Returns position of the surface.
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
float ptMin() const
minimal pt of interest
static constexpr auto TIB
Definition: SiStripDetId.h:37
T eta() const
Definition: PV3DBase.h:73
Hit const & hit(int i, layer l) const
double y0() const
Definition: FastCircle.h:45
SiStripCluster const & stereoCluster() const
bool isCompatible(const SiPixelRecHit &recHit, const LocalVector &ldir, const SiPixelClusterShapeCache &clusterShapeCache, PixelData const *pd=nullptr) const
GlobalVector globalMomentum() const
std::unique_ptr< HitPairGeneratorFromLayerPair > thePairGenerator
static void fillDescriptions(edm::ParameterSetDescription &desc)
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
void set(GlobalVector value)
unsigned int monoId() const
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magneticFieldESToken_
constexpr double twoPi()
Definition: Pi.h:32
T x() const
Definition: PV3DBase.h:59
static void fillDescriptions(edm::ParameterSetDescription &desc)
float chi2() const
Definition: RZLine.h:94
float phi(float curvature, float radius) const
tuple size
Write out results.
auto const & hh
tuple useFixedPreFiltering
Global3DVector GlobalVector
Definition: GlobalVector.h:10
#define LogDebug(id)
void refit2Hits(HitOwnPtr &hit0, HitOwnPtr &hit1, TrajectoryStateOnSurface &tsos0, TrajectoryStateOnSurface &tsos1, const TrackingRegion &region, float nomField, bool isDebug)
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11