CMS 3D CMS Logo

TrackMerger.cc
Go to the documentation of this file.
1 #include "TrackMerger.h"
2 
5 
6 #define TRACK_SORT 1 // just use all hits from inner track, then append from outer outside it
7 #define DET_SORT 0 // sort hits using global position of detector
8 #define HIT_SORT 0 // sort hits using global position of hit
9 
11 
12 // #define VI_DEBUG
13 
14 #ifdef VI_DEBUG
15 #define DPRINT(x) std::cout << x << ": "
16 #define PRINT std::cout
17 #else
18 #define DPRINT(x) LogTrace(x)
19 #define PRINT LogTrace("")
20 #endif
21 
23  : useInnermostState_(iConfig.getParameter<bool>("useInnermostState")),
24  debug_(false),
25  theBuilderName(iConfig.getParameter<std::string>("ttrhBuilderName")),
28  builderToken_(
30  trackerTopoToken_(cc.esConsumes<TrackerTopology, TrackerTopologyRcd>()) {}
31 
33 
34 void TrackMerger::init(const edm::EventSetup &iSetup) {
39 }
40 
42  const reco::Track &outer,
43  DuplicateTrackType duplicateType) const {
44  DPRINT("TrackMerger") << std::abs(inner.eta()) << " merging " << inner.algo() << '/' << outer.algo() << ' '
45  << inner.eta() << '/' << outer.eta() << std::endl;
46 
47  std::vector<const TrackingRecHit *> hits;
48  hits.reserve(inner.recHitsSize() + outer.recHitsSize());
49  DPRINT("TrackMerger") << "Inner track hits: " << std::endl;
50  for (auto it = inner.recHitsBegin(), ed = inner.recHitsEnd(); it != ed; ++it) {
51  hits.push_back(&**it);
52  if (debug_) {
53  [[clang::suppress]]
54  DetId id(hits.back()->geographicalId());
55  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid "
56  << hits.back()->isValid() << " detid: " << id() << std::endl;
57  }
58  }
59  DPRINT("TrackMerger") << "Outer track hits: " << std::endl;
60 
61  if (duplicateType == DuplicateTrackType::Disjoint) {
62 #if TRACK_SORT
63  DetId lastId(hits.back()->geographicalId());
64  int lastSubdet = lastId.subdetId();
65  unsigned int lastLayer = theTrkTopo->layer(lastId);
66  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
67  const TrackingRecHit *hit = &**it;
68  DetId id(hit->geographicalId());
69  int thisSubdet = id.subdetId();
70  if (thisSubdet > lastSubdet || (thisSubdet == lastSubdet && theTrkTopo->layer(id) > lastLayer)) {
71  hits.push_back(hit);
72  PRINT << " adding subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid "
73  << hit->isValid() << " detid: " << id() << std::endl;
74  } else {
75  PRINT << " skipping subdet " << thisSubdet << " layer " << theTrkTopo->layer(id) << " valid "
76  << hit->isValid() << " detid: " << id() << std::endl;
77  }
78  }
79 #else
81 #endif
82  } else if (duplicateType == DuplicateTrackType::Overlapping) {
84  }
85 
86  math::XYZVector p = (inner.innerMomentum() + outer.outerMomentum());
87  GlobalVector v(p.x(), p.y(), p.z());
88  if (!useInnermostState_)
89  v *= -1;
90 
92  unsigned int nhits = hits.size();
93  ownHits.reserve(nhits);
94 
95  if (duplicateType == DuplicateTrackType::Disjoint) {
96 #if TRACK_SORT
97  if (!useInnermostState_)
98  std::reverse(hits.begin(), hits.end());
99  for (auto hit : hits)
100  ownHits.push_back(*hit);
101 #elif DET_SORT
102  // OLD METHOD, sometimes fails
103  std::sort(hits.begin(), hits.end(), MomentumSort(v, &*theGeometry));
104  for (auto hit : hits)
105  ownHits.push_back(*hit);
106 #else
107  sortByHitPosition(v, hits, ownHits);
108 #endif
109  } else if (duplicateType == DuplicateTrackType::Overlapping) {
110  sortByHitPosition(v, hits, ownHits);
111  }
112 
115  if (useInnermostState_) {
116  pdir = alongMomentum;
117  if ((inner.outerPosition() - inner.innerPosition()).Dot(inner.momentum()) >= 0) {
118  // use inner state
119  TrajectoryStateOnSurface originalTsosIn(
121  state = trajectoryStateTransform::persistentState(originalTsosIn, DetId(inner.innerDetId()));
122  } else {
123  // use outer state
124  TrajectoryStateOnSurface originalTsosOut(
126  state = trajectoryStateTransform::persistentState(originalTsosOut, DetId(inner.outerDetId()));
127  }
128  } else {
129  pdir = oppositeToMomentum;
130  if ((outer.outerPosition() - inner.innerPosition()).Dot(inner.momentum()) >= 0) {
131  // use outer state
132  TrajectoryStateOnSurface originalTsosOut(
134  state = trajectoryStateTransform::persistentState(originalTsosOut, DetId(outer.outerDetId()));
135  } else {
136  // use inner state
137  TrajectoryStateOnSurface originalTsosIn(
139  state = trajectoryStateTransform::persistentState(originalTsosIn, DetId(outer.innerDetId()));
140  }
141  }
143  TrackCandidate ret(ownHits, seed, state, (useInnermostState_ ? inner : outer).seedRef());
144  ret.setStopReason((uint8_t)(useInnermostState_ ? inner : outer).stopReason());
145  return ret;
146 }
147 
148 void TrackMerger::addSecondTrackHits(std::vector<const TrackingRecHit *> &hits, const reco::Track &outer) const {
149  size_t nHitsFirstTrack = hits.size();
150  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
151  const TrackingRecHit *hit = &**it;
152  DetId id(hit->geographicalId());
153  const auto lay = theTrkTopo->layer(id);
154  bool shared = false;
155  bool valid = hit->isValid();
156  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << valid
157  << " detid: " << id() << std::endl;
158  size_t iHit = 0;
159  for (auto hit2 : hits) {
160  ++iHit;
161  if (iHit > nHitsFirstTrack)
162  break;
163  DetId id2 = hit2->geographicalId();
164  if (id.subdetId() != id2.subdetId())
165  continue;
166  if (theTrkTopo->layer(id2) != lay)
167  continue;
168  if (hit->sharesInput(hit2, TrackingRecHit::all)) {
169  PRINT << " discared as duplicate of other hit" << id() << std::endl;
170  shared = true;
171  break;
172  }
173  if (hit2->isValid() && !valid) {
174  PRINT << " replacing old invalid hit on detid " << id2() << std::endl;
175  hit = hit2;
176  shared = true;
177  break;
178  }
179  PRINT << " discared as additional hit on layer that already contains hit with detid " << id() << std::endl;
180  shared = true;
181  break;
182  }
183  if (shared)
184  continue;
185  hits.push_back(hit);
186  }
187 }
188 
190  const std::vector<const TrackingRecHit *> &hits,
191  TrackCandidate::RecHitContainer &ownHits) const {
192  // NEW sort, more accurate
193  unsigned int nhits = hits.size();
194  std::vector<TransientTrackingRecHit::RecHitPointer> ttrh(nhits);
195  for (unsigned int i = 0; i < nhits; ++i)
196  ttrh[i] = theBuilder->build(hits[i]);
197  std::sort(ttrh.begin(), ttrh.end(), GlobalMomentumSort(v));
198  for (const auto &hit : ttrh)
199  ownHits.push_back(*hit);
200 }
201 
203  const GeomDet *det1 = geom_->idToDet(hit1->geographicalId());
204  const GeomDet *det2 = geom_->idToDet(hit2->geographicalId());
205  GlobalPoint p1 = det1->toGlobal(LocalPoint(0, 0, 0));
206  GlobalPoint p2 = det2->toGlobal(LocalPoint(0, 0, 0));
207  return (p2 - p1).dot(dir_) > 0;
208 }
209 
211  const TransientTrackingRecHit::RecHitPointer &hit2) const {
212  GlobalPoint p1 = hit1->isValid() ? hit1->globalPosition() : hit1->det()->position();
213  GlobalPoint p2 = hit2->isValid() ? hit2->globalPosition() : hit2->det()->position();
214  return (p2 - p1).dot(dir_) > 0;
215 }
edm::ESHandle< TrackerTopology > theTrkTopo
Definition: TrackMerger.h:35
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
#define DPRINT(x)
Definition: TrackMerger.cc:18
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
const TrackerGeometry * geom_
Definition: TrackMerger.h:62
static const char dir_[]
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
ret
prodAgent to be discontinued
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:202
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > builderToken_
Definition: TrackMerger.h:38
PropagationDirection
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:34
unsigned int layer(const DetId &id) const
DuplicateTrackType
void addSecondTrackHits(std::vector< const TrackingRecHit *> &hits, const reco::Track &outer) const
Definition: TrackMerger.cc:148
void push_back(D *&d)
Definition: OwnVector.h:326
void sortByHitPosition(const GlobalVector &v, const std::vector< const TrackingRecHit *> &hits, TrackCandidate::RecHitContainer &ownHits) const
Definition: TrackMerger.cc:189
#define PRINT
Definition: TrackMerger.cc:19
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
bool operator()(const TransientTrackingRecHit::RecHitPointer &hit1, const TransientTrackingRecHit::RecHitPointer &hit2) const
Definition: TrackMerger.cc:210
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
const TrackerGeomDet * idToDet(DetId) const override
bool useInnermostState_
Definition: TrackMerger.h:31
std::shared_ptr< TrackingRecHit const > RecHitPointer
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer, DuplicateTrackType duplicateType) const
Definition: TrackMerger.cc:41
Definition: DetId.h:17
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magFieldToken_
Definition: TrackMerger.h:37
DetId geographicalId() const
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
edm::ESHandle< TransientTrackingRecHitBuilder > theBuilder
Definition: TrackMerger.h:34
HLT enums.
TrackMerger(const edm::ParameterSet &iConfig, edm::ConsumesCollector)
Definition: TrackMerger.cc:22
edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > geometryToken_
Definition: TrackMerger.h:36
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopoToken_
Definition: TrackMerger.h:39
edm::ESHandle< MagneticField > theMagField
Definition: TrackMerger.h:30
void reserve(size_t)
Definition: OwnVector.h:320
edm::ESHandle< TrackerGeometry > theGeometry
Definition: TrackMerger.h:29
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)