CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 TRACK_SORT
66  DetId lastId(hits.back()->geographicalId());
67  int lastSubdet = lastId.subdetId();
68  unsigned int lastLayer = theTrkTopo->layer(lastId);
69  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
70  const TrackingRecHit *hit = &**it;
71  DetId id(hit->geographicalId());
72  int thisSubdet = id.subdetId();
73  if (thisSubdet > lastSubdet || (thisSubdet == lastSubdet && theTrkTopo->layer(id) > lastLayer)) {
74  hits.push_back(hit);
75  PRINT << " adding subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
76  } else {
77  PRINT << " skipping subdet " << thisSubdet << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
78  }
79  }
80 #else
81  size_t nHitsFirstTrack = hits.size();
82  for (auto it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
83  const TrackingRecHit *hit = &**it;
84  DetId id(hit->geographicalId());
85  int lay = theTrkTopo->layer(id);
86  bool shared = false;
87  bool valid = hit->isValid();
88  PRINT << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << valid << " detid: " << id() << std::endl;
89  size_t iHit = 0;
90  for ( auto hit2 : hits) {
91  ++iHit; if (iHit > nHitsFirstTrack) break;
92  DetId id2 = hit2->geographicalId();
93  if (id.subdetId() != id2.subdetId()) continue;
94  if (theTrkTopo->layer(id2) != lay) continue;
95  if (hit->sharesInput(hit2, TrackingRecHit::all)) {
96  PRINT << " discared as duplicate of other hit" << id() << std::endl;
97  shared = true; break;
98  }
99  if (hit2->isValid() && !valid) {
100  PRINT << " replacing old invalid hit on detid " << id2() << std::endl;
101  hit2 = hit; shared = true; break;
102  }
103  PRINT << " discared as additional hit on layer that already contains hit with detid " << id() << std::endl;
104  shared = true; break;
105  }
106  if (shared) continue;
107  hits.push_back(hit);
108  }
109 #endif
110 
111  math::XYZVector p = (inner.innerMomentum() + outer.outerMomentum());
112  GlobalVector v(p.x(), p.y(), p.z());
113  if (!useInnermostState_) v *= -1;
114 
116  unsigned int nhits = hits.size();
117  ownHits.reserve(nhits);
118 
119 #if TRACK_SORT
120  if (!useInnermostState_) std::reverse(hits.begin(), hits.end());
121  for(auto hit : hits) ownHits.push_back(*hit);
122 #elif DET_SORT
123  // OLD METHOD, sometimes fails
124  std::sort(hits.begin(), hits.end(), MomentumSort(v, &*theGeometry));
125  for(auto hit : hits) ownHits.push_back(*hit);
126 #else
127  // NEW sort, more accurate
128  std::vector<TransientTrackingRecHit::RecHitPointer> ttrh(nhits);
129  for (unsigned int i = 0; i < nhits; ++i) ttrh[i] = theBuilder->build(hits[i]);
130  std::sort(ttrh.begin(), ttrh.end(), GlobalMomentumSort(v));
131  for(auto hit : ttrh) ownHits.push_back(*hit);
132 #endif
133 
134  PTrajectoryStateOnDet state;
136  if (useInnermostState_) {
137  pdir = alongMomentum;
138  if ((inner.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
139  // use inner state
141  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(inner.innerDetId()) );
142  } else {
143  // use outer state
145  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(inner.outerDetId()) );
146  }
147  } else {
148  pdir = oppositeToMomentum;
149  if ((outer.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
150  // use outer state
152  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(outer.outerDetId()) );
153  } else {
154  // use inner state
156  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(outer.innerDetId()) );
157  }
158 
159  }
161  TrackCandidate ret(ownHits, seed, state, (useInnermostState_ ? inner : outer).seedRef());
162  ret.setStopReason((uint8_t)(useInnermostState_ ? inner : outer).stopReason());
163  return ret;
164 }
165 
166 
168 {
169  const GeomDet *det1 = geom_->idToDet(hit1->geographicalId());
170  const GeomDet *det2 = geom_->idToDet(hit2->geographicalId());
171  GlobalPoint p1 = det1->toGlobal(LocalPoint(0,0,0));
172  GlobalPoint p2 = det2->toGlobal(LocalPoint(0,0,0));
173  return (p2 - p1).dot(dir_) > 0;
174 }
175 
177 {
178  GlobalPoint p1 = hit1->isValid() ? hit1->globalPosition() : hit1->det()->position();
179  GlobalPoint p2 = hit2->isValid() ? hit2->globalPosition() : hit2->det()->position();
180  return (p2 - p1).dot(dir_) > 0;
181 }
edm::ESHandle< TrackerTopology > theTrkTopo
Definition: TrackMerger.h:27
int i
Definition: DBlmapReader.cc:9
tuple ret
prodAgent to be discontinued
#define DPRINT(x)
Definition: TrackMerger.cc:26
const TrackerGeometry * geom_
Definition: TrackMerger.h:42
static const char dir_[]
std::string theBuilderName
Definition: TrackMerger.h:25
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:119
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:52
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:670
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:41
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer) const
Definition: TrackMerger.cc:49
bool operator()(const TransientTrackingRecHit::RecHitPointer &hit1, const TransientTrackingRecHit::RecHitPointer &hit2) const
Definition: TrackMerger.cc:176
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
TrackAlgorithm algo() const
Definition: TrackBase.h:492
void push_back(D *&d)
Definition: OwnVector.h:290
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:167
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:646
#define PRINT
Definition: TrackMerger.cc:27
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
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:104
void setStopReason(uint8_t value)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
double p2[4]
Definition: TauolaWrapper.h:90
bool useInnermostState_
Definition: TrackMerger.h:23
std::shared_ptr< TrackingRecHit const > RecHitPointer
Definition: DetId.h:18
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:70
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
const T & get() const
Definition: EventSetup.h:56
bool isValid() const
edm::ESHandle< TransientTrackingRecHitBuilder > theBuilder
Definition: TrackMerger.h:26
double p1[4]
Definition: TauolaWrapper.h:89
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
DetId geographicalId() const
bool isValid() const
Definition: ESHandle.h:47
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:99
edm::ESHandle< MagneticField > theMagField
Definition: TrackMerger.h:22
void reserve(size_t)
Definition: OwnVector.h:284
edm::ESHandle< TrackerGeometry > theGeometry
Definition: TrackMerger.h:21
virtual const TrackerGeomDet * idToDet(DetId) const
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:109