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  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 }
Vector3DBase
Definition: Vector3DBase.h:8
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:543
TrackerGeometry::idToDet
const TrackerGeomDet * idToDet(DetId) const override
Definition: TrackerGeometry.cc:193
TrajectoryStateOnSurface.h
TrackMerger::trackerTopoToken_
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopoToken_
Definition: TrackMerger.h:39
PRINT
#define PRINT
Definition: TrackMerger.cc:19
TrackMerger::GlobalMomentumSort::operator()
bool operator()(const TransientTrackingRecHit::RecHitPointer &hit1, const TransientTrackingRecHit::RecHitPointer &hit2) const
Definition: TrackMerger.cc:209
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
GeomDet
Definition: GeomDet.h:27
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
ESInputTag
TrackMerger::theMagField
edm::ESHandle< MagneticField > theMagField
Definition: TrackMerger.h:30
edm
HLT enums.
Definition: AlignableModifier.h:19
TrackerTopology
Definition: TrackerTopology.h:16
trajectoryStateTransform::persistentState
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
Definition: TrajectoryStateTransform.cc:14
sistrip::dir_
static const char dir_[]
Definition: ConstantsForDqm.h:17
TransientRecHitRecord
Definition: TransientRecHitRecord.h:14
oppositeToMomentum
Definition: PropagationDirection.h:4
TrackMerger::addSecondTrackHits
void addSecondTrackHits(std::vector< const TrackingRecHit * > &hits, const reco::Track &outer) const
Definition: TrackMerger.cc:147
TrackMerger::builderToken_
edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > builderToken_
Definition: TrackMerger.h:38
TrackerTopology::layer
unsigned int layer(const DetId &id) const
Definition: TrackerTopology.cc:47
align::LocalPoint
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
TrackingRecHit::RecHitPointer
std::shared_ptr< TrackingRecHit const > RecHitPointer
Definition: TrackingRecHit.h:24
TrackMerger::MomentumSort::geom_
const TrackerGeometry * geom_
Definition: TrackMerger.h:62
findQualityFiles.v
v
Definition: findQualityFiles.py:179
TrackMerger::MomentumSort::dir_
GlobalVector dir_
Definition: TrackMerger.h:61
TransientTrackingRecHitBuilder::build
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
TrackingRecHit::geographicalId
DetId geographicalId() const
Definition: TrackingRecHit.h:120
groupFilesInBlocks.reverse
reverse
Definition: groupFilesInBlocks.py:131
trajectoryStateTransform::outerStateOnSurface
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
Definition: TrajectoryStateTransform.cc:118
fileCollector.seed
seed
Definition: fileCollector.py:127
IdealMagneticFieldRecord
Definition: IdealMagneticFieldRecord.h:11
DetId
Definition: DetId.h:17
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
TrackMerger::debug_
bool debug_
Definition: TrackMerger.h:32
TrackMerger.h
SurfaceOrientation::inner
Definition: Surface.h:19
TrackerDigiGeometryRecord
Definition: TrackerDigiGeometryRecord.h:16
reco::Track
Definition: Track.h:27
p2
double p2[4]
Definition: TauolaWrapper.h:90
TrackMerger::useInnermostState_
bool useInnermostState_
Definition: TrackMerger.h:31
Point3DBase< float, GlobalTag >
nhits
Definition: HIMultiTrackSelector.h:42
TrackMerger::init
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:34
DetId::subdetId
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum)
Definition: DetId.h:48
edm::OwnVector::reserve
void reserve(size_t)
Definition: OwnVector.h:320
TrackMerger::magFieldToken_
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magFieldToken_
Definition: TrackMerger.h:37
GeomDet::toGlobal
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
edm::ParameterSet
Definition: ParameterSet.h:47
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
TrackingRecHit::all
Definition: TrackingRecHit.h:59
TrackCandidate
Definition: TrackCandidate.h:24
DPRINT
#define DPRINT(x)
Definition: TrackMerger.cc:18
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
TrackMerger::sortByHitPosition
void sortByHitPosition(const GlobalVector &v, const std::vector< const TrackingRecHit * > &hits, TrackCandidate::RecHitContainer &ownHits) const
Definition: TrackMerger.cc:188
math::XYZVector
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
DuplicateTrackType
DuplicateTrackType
Definition: DuplicateTrackType.h:4
edm::EventSetup::getHandle
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:155
p1
double p1[4]
Definition: TauolaWrapper.h:89
edm::EventSetup
Definition: EventSetup.h:58
cc
TrackMerger::geometryToken_
edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > geometryToken_
Definition: TrackMerger.h:36
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TrackingRecHit
Definition: TrackingRecHit.h:21
std
Definition: JetResolutionObject.h:76
RunInfoPI::state
state
Definition: RunInfoPayloadInspectoHelper.h:16
TrackMerger::MomentumSort
Definition: TrackMerger.h:55
TrackMerger::merge
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer, DuplicateTrackType duplicateType) const
Definition: TrackMerger.cc:41
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
DuplicateTrackType::Overlapping
TrackMerger::theBuilder
edm::ESHandle< TransientTrackingRecHitBuilder > theBuilder
Definition: TrackMerger.h:34
PropagationDirection
PropagationDirection
Definition: PropagationDirection.h:4
TrackMerger::theGeometry
edm::ESHandle< TrackerGeometry > theGeometry
Definition: TrackMerger.h:29
TrajectorySeed
Definition: TrajectorySeed.h:18
TrackMerger::MomentumSort::operator()
bool operator()(const TrackingRecHit *hit1, const TrackingRecHit *hit2) const
Definition: TrackMerger.cc:201
TrajectoryStateTransform.h
edm::OwnVector::push_back
void push_back(D *&d)
Definition: OwnVector.h:326
TrackMerger::TrackMerger
TrackMerger(const edm::ParameterSet &iConfig, edm::ConsumesCollector)
Definition: TrackMerger.cc:22
DuplicateTrackType::Disjoint
TrackMerger::theTrkTopo
edm::ESHandle< TrackerTopology > theTrkTopo
Definition: TrackMerger.h:35
TrackMerger::GlobalMomentumSort
Definition: TrackMerger.h:46
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
RunInfoPI::valid
Definition: RunInfoPayloadInspectoHelper.h:16
TrackerTopologyRcd
Definition: TrackerTopologyRcd.h:10
PTrajectoryStateOnDet
Definition: PTrajectoryStateOnDet.h:10
globals_cff.id2
id2
Definition: globals_cff.py:34
SurfaceOrientation::outer
Definition: Surface.h:19
trajectoryStateTransform::innerStateOnSurface
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
Definition: TrajectoryStateTransform.cc:110
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
MagneticField
Definition: MagneticField.h:19
TransientTrackingRecHitBuilder
Definition: TransientTrackingRecHitBuilder.h:6
TrackMerger::~TrackMerger
~TrackMerger()
Definition: TrackMerger.cc:32
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
alongMomentum
Definition: PropagationDirection.h:4
hit
Definition: SiStripHitEffFromCalibTree.cc:88
edm::OwnVector< TrackingRecHit >
TrackerGeometry
Definition: TrackerGeometry.h:14