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")),
27  magFieldToken_(cc.esConsumes<MagneticField, IdealMagneticFieldRecord>()),
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  DetId id(hits.back()->geographicalId());
54  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid "
55  << hits.back()->isValid() << " detid: " << id() << std::endl;
56  }
57  }
58  DPRINT("TrackMerger") << "Outer track hits: " << std::endl;
59 
60  if (duplicateType == DuplicateTrackType::Disjoint) {
61 #if TRACK_SORT
62  DetId lastId(hits.back()->geographicalId());
63  int lastSubdet = lastId.subdetId();
64  unsigned int lastLayer = theTrkTopo->layer(lastId);
65  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
66  const TrackingRecHit *hit = &**it;
67  DetId id(hit->geographicalId());
68  int thisSubdet = id.subdetId();
69  if (thisSubdet > lastSubdet || (thisSubdet == lastSubdet && theTrkTopo->layer(id) > lastLayer)) {
70  hits.push_back(hit);
71  PRINT << " adding subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid "
72  << hit->isValid() << " detid: " << id() << std::endl;
73  } else {
74  PRINT << " skipping subdet " << thisSubdet << " layer " << theTrkTopo->layer(id) << " valid "
75  << hit->isValid() << " detid: " << id() << std::endl;
76  }
77  }
78 #else
80 #endif
81  } else if (duplicateType == DuplicateTrackType::Overlapping) {
83  }
84 
85  math::XYZVector p = (inner.innerMomentum() + outer.outerMomentum());
86  GlobalVector v(p.x(), p.y(), p.z());
87  if (!useInnermostState_)
88  v *= -1;
89 
91  unsigned int nhits = hits.size();
92  ownHits.reserve(nhits);
93 
94  if (duplicateType == DuplicateTrackType::Disjoint) {
95 #if TRACK_SORT
96  if (!useInnermostState_)
97  std::reverse(hits.begin(), hits.end());
98  for (auto hit : hits)
99  ownHits.push_back(*hit);
100 #elif DET_SORT
101  // OLD METHOD, sometimes fails
102  std::sort(hits.begin(), hits.end(), MomentumSort(v, &*theGeometry));
103  for (auto hit : hits)
104  ownHits.push_back(*hit);
105 #else
106  sortByHitPosition(v, hits, ownHits);
107 #endif
108  } else if (duplicateType == DuplicateTrackType::Overlapping) {
109  sortByHitPosition(v, hits, ownHits);
110  }
111 
114  if (useInnermostState_) {
115  pdir = alongMomentum;
116  if ((inner.outerPosition() - inner.innerPosition()).Dot(inner.momentum()) >= 0) {
117  // use inner state
118  TrajectoryStateOnSurface originalTsosIn(
120  state = trajectoryStateTransform::persistentState(originalTsosIn, DetId(inner.innerDetId()));
121  } else {
122  // use outer state
123  TrajectoryStateOnSurface originalTsosOut(
125  state = trajectoryStateTransform::persistentState(originalTsosOut, DetId(inner.outerDetId()));
126  }
127  } else {
128  pdir = oppositeToMomentum;
129  if ((outer.outerPosition() - inner.innerPosition()).Dot(inner.momentum()) >= 0) {
130  // use outer state
131  TrajectoryStateOnSurface originalTsosOut(
133  state = trajectoryStateTransform::persistentState(originalTsosOut, DetId(outer.outerDetId()));
134  } else {
135  // use inner state
136  TrajectoryStateOnSurface originalTsosIn(
138  state = trajectoryStateTransform::persistentState(originalTsosIn, DetId(outer.innerDetId()));
139  }
140  }
142  TrackCandidate ret(ownHits, seed, state, (useInnermostState_ ? inner : outer).seedRef());
143  ret.setStopReason((uint8_t)(useInnermostState_ ? inner : outer).stopReason());
144  return ret;
145 }
146 
147 void TrackMerger::addSecondTrackHits(std::vector<const TrackingRecHit *> &hits, const reco::Track &outer) const {
148  size_t nHitsFirstTrack = hits.size();
149  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
150  const TrackingRecHit *hit = &**it;
151  DetId id(hit->geographicalId());
152  const auto lay = theTrkTopo->layer(id);
153  bool shared = false;
154  bool valid = hit->isValid();
155  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << valid
156  << " detid: " << id() << std::endl;
157  size_t iHit = 0;
158  for (auto hit2 : hits) {
159  ++iHit;
160  if (iHit > nHitsFirstTrack)
161  break;
162  DetId id2 = hit2->geographicalId();
163  if (id.subdetId() != id2.subdetId())
164  continue;
165  if (theTrkTopo->layer(id2) != lay)
166  continue;
167  if (hit->sharesInput(hit2, TrackingRecHit::all)) {
168  PRINT << " discared as duplicate of other hit" << id() << std::endl;
169  shared = true;
170  break;
171  }
172  if (hit2->isValid() && !valid) {
173  PRINT << " replacing old invalid hit on detid " << id2() << std::endl;
174  hit = hit2;
175  shared = true;
176  break;
177  }
178  PRINT << " discared as additional hit on layer that already contains hit with detid " << id() << std::endl;
179  shared = true;
180  break;
181  }
182  if (shared)
183  continue;
184  hits.push_back(hit);
185  }
186 }
187 
189  const std::vector<const TrackingRecHit *> &hits,
190  TrackCandidate::RecHitContainer &ownHits) const {
191  // NEW sort, more accurate
192  unsigned int nhits = hits.size();
193  std::vector<TransientTrackingRecHit::RecHitPointer> ttrh(nhits);
194  for (unsigned int i = 0; i < nhits; ++i)
195  ttrh[i] = theBuilder->build(hits[i]);
196  std::sort(ttrh.begin(), ttrh.end(), GlobalMomentumSort(v));
197  for (const auto &hit : ttrh)
198  ownHits.push_back(*hit);
199 }
200 
202  const GeomDet *det1 = geom_->idToDet(hit1->geographicalId());
203  const GeomDet *det2 = geom_->idToDet(hit2->geographicalId());
204  GlobalPoint p1 = det1->toGlobal(LocalPoint(0, 0, 0));
205  GlobalPoint p2 = det2->toGlobal(LocalPoint(0, 0, 0));
206  return (p2 - p1).dot(dir_) > 0;
207 }
208 
210  const TransientTrackingRecHit::RecHitPointer &hit2) const {
211  GlobalPoint p1 = hit1->isValid() ? hit1->globalPosition() : hit1->det()->position();
212  GlobalPoint p2 = hit2->isValid() ? hit2->globalPosition() : hit2->det()->position();
213  return (p2 - p1).dot(dir_) > 0;
214 }
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
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:201
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:147
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:188
#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:209
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:151
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)