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.
3 
7 
9 
10 #include <boost/foreach.hpp>
11 #define foreach BOOST_FOREACH
12 
13 #define TRACK_SORT 1 // just use all hits from inner track, then append from outer outside it
14 #define DET_SORT 0 // sort hits using global position of detector
15 #define HIT_SORT 0 // sort hits using global position of hit
16 
17 
19  useInnermostState_(iConfig.getParameter<bool>("useInnermostState")),
20  debug_(iConfig.getUntrackedParameter<bool>("debug",false)),
21  theBuilderName(iConfig.getParameter<std::string>("ttrhBuilderName"))
22 {
23 }
24 
26 {
27 }
28 
29 void TrackMerger::init(const edm::EventSetup &iSetup)
30 {
34  iSetup.get<IdealGeometryRecord>().get(theTrkTopo);
35 }
36 
38 {
39  std::vector<const TrackingRecHit *> hits;
40  hits.reserve(inner.recHitsSize() + outer.recHitsSize());
41  if (debug_) std::cout << "Inner track hits: " << std::endl;
42  for (trackingRecHit_iterator it = inner.recHitsBegin(), ed = inner.recHitsEnd(); it != ed; ++it) {
43  hits.push_back(&**it);
44  if (debug_) {
45  DetId id(hits.back()->geographicalId());
46  std::cout << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << hits.back()->isValid() << " detid: " << id() << std::endl;
47  }
48  }
49  if (debug_) std::cout << "Outer track hits: " << std::endl;
50 
51 #if TRACK_SORT
52  DetId lastId(hits.back()->geographicalId());
53  int lastSubdet = lastId.subdetId();
54  unsigned int lastLayer = theTrkTopo->layer(lastId);
55  for (trackingRecHit_iterator it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
56  const TrackingRecHit *hit = &**it;
57  DetId id(hit->geographicalId());
58  int thisSubdet = id.subdetId();
59  if (thisSubdet > lastSubdet || (thisSubdet == lastSubdet && theTrkTopo->layer(id) > lastLayer)) {
60  hits.push_back(hit);
61  if (debug_) std::cout << " adding subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
62  } else {
63  if (debug_) std::cout << " skipping subdet " << thisSubdet << " layer " << theTrkTopo->layer(id) << " valid " << hit->isValid() << " detid: " << id() << std::endl;
64  }
65  }
66 #else
67  size_t nHitsFirstTrack = hits.size();
68  for (trackingRecHit_iterator it = outer.recHitsBegin(), ed = outer.recHitsEnd(); it != ed; ++it) {
69  const TrackingRecHit *hit = &**it;
70  DetId id(hit->geographicalId());
71  int lay = theTrkTopo->layer(id);
72  bool shared = false;
73  bool valid = hit->isValid();
74  if (debug_) std::cout << " subdet " << id.subdetId() << " layer " << theTrkTopo->layer(id) << " valid " << valid << " detid: " << id() << std::endl;
75  size_t iHit = 0;
76  foreach(const TrackingRecHit *& hit2, hits) {
77  ++iHit; if (iHit > nHitsFirstTrack) break;
78  DetId id2 = hit2->geographicalId();
79  if (id.subdetId() != id2.subdetId()) continue;
80  if (theTrkTopo->layer(id2) != lay) continue;
81  if (hit->sharesInput(hit2, TrackingRecHit::all)) {
82  if (debug_) std::cout << " discared as duplicate of other hit" << id() << std::endl;
83  shared = true; break;
84  }
85  if (hit2->isValid() && !valid) {
86  if (debug_) std::cout << " replacing old invalid hit on detid " << id2() << std::endl;
87  hit2 = hit; shared = true; break;
88  }
89  if (debug_) std::cout << " discared as additional hit on layer that already contains hit with detid " << id() << std::endl;
90  shared = true; break;
91  }
92  if (shared) continue;
93  hits.push_back(hit);
94  }
95 #endif
96 
97  math::XYZVector p = (inner.innerMomentum() + outer.outerMomentum());
98  GlobalVector v(p.x(), p.y(), p.z());
99  if (!useInnermostState_) v *= -1;
100 
102  unsigned int nhits = hits.size();
103  ownHits.reserve(nhits);
104 
105 #if TRACK_SORT
106  if (!useInnermostState_) std::reverse(hits.begin(), hits.end());
107  foreach(const TrackingRecHit * hit, hits) {
108  ownHits.push_back(*hit);
109  }
110 #elif DET_SORT
111  // OLD METHOD, sometimes fails
112  std::sort(hits.begin(), hits.end(), MomentumSort(v, &*theGeometry));
113 
114  foreach(const TrackingRecHit * hit, hits) {
115  ownHits.push_back(*hit);
116  }
117 #else
118  // NEW sort, more accurate
119  std::vector<TransientTrackingRecHit::RecHitPointer> ttrh(nhits);
120  for (unsigned int i = 0; i < nhits; ++i) ttrh[i] = theBuilder->build(hits[i]);
121  std::sort(ttrh.begin(), ttrh.end(), GlobalMomentumSort(v));
122 
123  foreach(const TransientTrackingRecHit::RecHitPointer & hit, ttrh) {
124  ownHits.push_back(*hit->hit());
125  }
126 #endif
127 
128  PTrajectoryStateOnDet state;
130  if (useInnermostState_) {
131  pdir = alongMomentum;
132  if ((inner.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
133  // use inner state
135  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(inner.innerDetId()) );
136  } else {
137  // use outer state
139  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(inner.outerDetId()) );
140  }
141  } else {
142  pdir = oppositeToMomentum;
143  if ((outer.outerPosition()-inner.innerPosition()).Dot(inner.momentum()) >= 0) {
144  // use outer state
146  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(outer.outerDetId()) );
147  } else {
148  // use inner state
150  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(outer.innerDetId()) );
151  }
152 
153  }
155  return TrackCandidate(ownHits, seed, state, (useInnermostState_ ? inner : outer).seedRef());
156 }
157 
158 
160 {
161  const GeomDet *det1 = geom_->idToDet(hit1->geographicalId());
162  const GeomDet *det2 = geom_->idToDet(hit2->geographicalId());
163  GlobalPoint p1 = det1->toGlobal(LocalPoint(0,0,0));
164  GlobalPoint p2 = det2->toGlobal(LocalPoint(0,0,0));
165  return (p2 - p1).dot(dir_) > 0;
166 }
167 
169 {
170  GlobalPoint p1 = hit1->isValid() ? hit1->globalPosition() : hit1->det()->position();
171  GlobalPoint p2 = hit2->isValid() ? hit2->globalPosition() : hit2->det()->position();
172  return (p2 - p1).dot(dir_) > 0;
173 }
edm::ESHandle< TrackerTopology > theTrkTopo
Definition: TrackMerger.h:27
int i
Definition: DBlmapReader.cc:9
const Vector & momentum() const
track momentum vector
Definition: TrackBase.h:148
const TrackerGeometry * geom_
Definition: TrackMerger.h:42
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field)
static const char dir_[]
std::string theBuilderName
Definition: TrackMerger.h:25
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:69
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:47
size_type size() const
Definition: OwnVector.h:248
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
virtual bool sharesInput(const TrackingRecHit *other, SharedInputType what) const
PropagationDirection
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:47
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:29
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer) const
Definition: TrackMerger.cc:37
bool operator()(const TransientTrackingRecHit::RecHitPointer &hit1, const TransientTrackingRecHit::RecHitPointer &hit2) const
Definition: TrackMerger.cc:168
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:42
void push_back(D *&d)
Definition: OwnVector.h:274
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:159
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
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
virtual const GeomDet * idToDet(DetId) const
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:49
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
const T & get() const
Definition: EventSetup.h:55
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field)
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:45
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
tuple cout
Definition: gather_cfg.py:121
DetId geographicalId() const
volatile std::atomic< bool > shutdown_flag false
bool isValid() const
Definition: ESHandle.h:37
TrackMerger(const edm::ParameterSet &iConfig)
Definition: TrackMerger.cc:18
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
edm::ESHandle< MagneticField > theMagField
Definition: TrackMerger.h:22
void reserve(size_t)
Definition: OwnVector.h:268
edm::ESHandle< TrackerGeometry > theGeometry
Definition: TrackMerger.h:21
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65