CMS 3D CMS Logo

TrackMerger.cc
Go to the documentation of this file.
1 #include "TrackMerger.h"
4 
5 
8 
11 
12 #define TRACK_SORT 1 // just use all hits from inner track, then append from outer outside it
13 #define DET_SORT 0 // sort hits using global position of detector
14 #define HIT_SORT 0 // sort hits using global position of hit
15 
16 
18 
19 
20  // #define VI_DEBUG
21 
22 #ifdef VI_DEBUG
23 #define DPRINT(x) std::cout << x << ": "
24 #define PRINT std::cout
25 #else
26 #define DPRINT(x) LogTrace(x)
27 #define PRINT LogTrace("")
28 #endif
29 
30 
32  useInnermostState_(iConfig.getParameter<bool>("useInnermostState")),
33  theBuilderName(iConfig.getParameter<std::string>("ttrhBuilderName"))
34 {
35 }
36 
38 {
39 }
40 
41 void TrackMerger::init(const edm::EventSetup &iSetup)
42 {
46  iSetup.get<TrackerTopologyRcd>().get(theTrkTopo);
47 }
48 
50 {
51  DPRINT("TrackMerger") << std::abs(inner.eta()) << " merging " << inner.algo() << '/' << outer.algo() << ' ' << inner.eta() << '/' << outer.eta()<< std::endl;
52 
53  std::vector<const TrackingRecHit *> hits;
54  hits.reserve(inner.recHitsSize() + outer.recHitsSize());
55  DPRINT("TrackMerger") << "Inner track hits: " << std::endl;
56  for (auto it = inner.recHitsBegin(), ed = inner.recHitsEnd(); it != ed; ++it) {
57  hits.push_back(&**it);
58  if (debug_) {
59  DetId id(hits.back()->geographicalId());
60  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << hits.back()->isValid() << " detid: " << id() << std::endl;
61  }
62  }
63  DPRINT("TrackMerger") << "Outer track hits: " << std::endl;
64 
65  if(duplicateType == DuplicateTrackType::Disjoint) {
66 #if TRACK_SORT
67  DetId lastId(hits.back()->geographicalId());
68  int lastSubdet = lastId.subdetId();
69  unsigned int lastLayer = theTrkTopo->layer(lastId);
70  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
71  const TrackingRecHit *hit = &**it;
72  DetId id(hit->geographicalId());
73  int thisSubdet = id.subdetId();
74  if (thisSubdet > lastSubdet || (thisSubdet == lastSubdet && theTrkTopo->layer(id) > lastLayer)) {
75  hits.push_back(hit);
76  PRINT << " adding subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
77  } else {
78  PRINT << " skipping subdet " << thisSubdet << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
79  }
80  }
81 #else
82  addSecondTrackHits(hits, outer);
83 #endif
84  }
85  else if(duplicateType == DuplicateTrackType::Overlapping) {
86  addSecondTrackHits(hits, outer);
87  }
88 
89  math::XYZVector p = (inner.innerMomentum() + outer.outerMomentum());
90  GlobalVector v(p.x(), p.y(), p.z());
91  if (!useInnermostState_) v *= -1;
92 
94  unsigned int nhits = hits.size();
95  ownHits.reserve(nhits);
96 
97  if(duplicateType == DuplicateTrackType::Disjoint) {
98 #if TRACK_SORT
99  if (!useInnermostState_) std::reverse(hits.begin(), hits.end());
100  for(auto hit : hits) 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) ownHits.push_back(*hit);
105 #else
106  sortByHitPosition(v, hits, ownHits);
107 #endif
108  }
109  else if(duplicateType == DuplicateTrackType::Overlapping) {
110  sortByHitPosition(v, hits, ownHits);
111  }
112 
113  PTrajectoryStateOnDet state;
115  if (useInnermostState_) {
116  pdir = alongMomentum;
117  if ((inner.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
118  // use inner state
120  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(inner.innerDetId()) );
121  } else {
122  // use outer state
124  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(inner.outerDetId()) );
125  }
126  } else {
127  pdir = oppositeToMomentum;
128  if ((outer.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
129  // use outer state
131  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(outer.outerDetId()) );
132  } else {
133  // use inner state
135  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(outer.innerDetId()) );
136  }
137 
138  }
140  TrackCandidate ret(ownHits, seed, state, (useInnermostState_ ? inner : outer).seedRef());
141  ret.setStopReason((uint8_t)(useInnermostState_ ? inner : outer).stopReason());
142  return ret;
143 }
144 
145 void TrackMerger::addSecondTrackHits(std::vector<const TrackingRecHit *>& hits,
146  const reco::Track& outer) const {
147  size_t nHitsFirstTrack = hits.size();
148  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
149  const TrackingRecHit *hit = &**it;
150  DetId id(hit->geographicalId());
151  const auto lay = theTrkTopo->layer(id);
152  bool shared = false;
153  bool valid = hit->isValid();
154  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << valid << " detid: " << id() << std::endl;
155  size_t iHit = 0;
156  for ( auto hit2 : hits) {
157  ++iHit; if (iHit > nHitsFirstTrack) break;
158  DetId id2 = hit2->geographicalId();
159  if (id.subdetId() != id2.subdetId()) continue;
160  if (theTrkTopo->layer(id2) != lay) continue;
161  if (hit->sharesInput(hit2, TrackingRecHit::all)) {
162  PRINT << " discared as duplicate of other hit" << id() << std::endl;
163  shared = true; break;
164  }
165  if (hit2->isValid() && !valid) {
166  PRINT << " replacing old invalid hit on detid " << id2() << std::endl;
167  hit = hit2; shared = true; break;
168  }
169  PRINT << " discared as additional hit on layer that already contains hit with detid " << id() << std::endl;
170  shared = true; break;
171  }
172  if (shared) continue;
173  hits.push_back(hit);
174  }
175 }
176 
178  const std::vector<const TrackingRecHit *>& hits,
179  TrackCandidate::RecHitContainer& ownHits) const {
180  // NEW sort, more accurate
181  unsigned int nhits = hits.size();
182  std::vector<TransientTrackingRecHit::RecHitPointer> ttrh(nhits);
183  for (unsigned int i = 0; i < nhits; ++i) ttrh[i] = theBuilder->build(hits[i]);
184  std::sort(ttrh.begin(), ttrh.end(), GlobalMomentumSort(v));
185  for(auto hit : ttrh) ownHits.push_back(*hit);
186 }
187 
189 {
190  const GeomDet *det1 = geom_->idToDet(hit1->geographicalId());
191  const GeomDet *det2 = geom_->idToDet(hit2->geographicalId());
192  GlobalPoint p1 = det1->toGlobal(LocalPoint(0,0,0));
193  GlobalPoint p2 = det2->toGlobal(LocalPoint(0,0,0));
194  return (p2 - p1).dot(dir_) > 0;
195 }
196 
198 {
199  GlobalPoint p1 = hit1->isValid() ? hit1->globalPosition() : hit1->det()->position();
200  GlobalPoint p2 = hit2->isValid() ? hit2->globalPosition() : hit2->det()->position();
201  return (p2 - p1).dot(dir_) > 0;
202 }
edm::ESHandle< TrackerTopology > theTrkTopo
Definition: TrackMerger.h:29
#define DPRINT(x)
Definition: TrackMerger.cc:26
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:32
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer, DuplicateTrackType duplicateType) const
Definition: TrackMerger.cc:49
static const char dir_[]
std::string theBuilderName
Definition: TrackMerger.h:27
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:124
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:54
void sortByHitPosition(const GlobalVector &v, const std::vector< const TrackingRecHit * > &hits, TrackCandidate::RecHitContainer &ownHits) const
Definition: TrackMerger.cc:177
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
virtual bool sharesInput(const TrackingRecHit *other, SharedInputType what) const
PropagationDirection
const Vector & momentum() const
track momentum vector
Definition: TrackBase.h:708
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:67
void addSecondTrackHits(std::vector< const TrackingRecHit * > &hits, const reco::Track &outer) const
Definition: TrackMerger.cc:145
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:41
bool operator()(const TransientTrackingRecHit::RecHitPointer &hit1, const TransientTrackingRecHit::RecHitPointer &hit2) const
Definition: TrackMerger.cc:197
DuplicateTrackType
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:57
TrackAlgorithm algo() const
Definition: TrackBase.h:530
void push_back(D *&d)
Definition: OwnVector.h:290
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:188
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:684
#define PRINT
Definition: TrackMerger.cc:27
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:96
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:109
void setStopReason(uint8_t value)
double p2[4]
Definition: TauolaWrapper.h:90
bool useInnermostState_
Definition: TrackMerger.h:25
std::shared_ptr< TrackingRecHit const > RecHitPointer
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
Definition: DetId.h:18
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:72
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
bool isValid() const
edm::ESHandle< TransientTrackingRecHitBuilder > theBuilder
Definition: TrackMerger.h:28
unsigned int layer(const DetId &id) const
double p1[4]
Definition: TauolaWrapper.h:89
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:62
T get() const
Definition: EventSetup.h:71
DetId geographicalId() const
TrackMerger(const edm::ParameterSet &iConfig)
Definition: TrackMerger.cc:31
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:101
edm::ESHandle< MagneticField > theMagField
Definition: TrackMerger.h:24
void reserve(size_t)
Definition: OwnVector.h:284
edm::ESHandle< TrackerGeometry > theGeometry
Definition: TrackMerger.h:23
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:114