CMS 3D CMS Logo

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