CMS 3D CMS Logo

TrackCandidateProducer.cc
Go to the documentation of this file.
1 // system
2 #include <memory>
3 #include <vector>
4 #include <map>
5 
6 // framework
14 
15 // data format
24 
25 // geometry / magnetic field / propagation
35 
36 // fastsim
41 
43 public:
44  explicit TrackCandidateProducer(const edm::ParameterSet& conf);
45 
46  void produce(edm::Event&, const edm::EventSetup&) override;
47 
48 private:
49  // tokens & labels
59 
60  // other data
62  bool splitHits;
65 };
66 
68  : propagatorLabel(conf.getParameter<std::string>("propagator")),
69  magneticFieldESToken_(esConsumes()),
70  trackerGeometryESToken_(esConsumes()),
71  trackerTopologyESToken_(esConsumes()),
72  propagatorESToken_(esConsumes(edm::ESInputTag("", propagatorLabel))),
73  hitSplitter() {
74  // produces
75  produces<TrackCandidateCollection>();
76 
77  // consumes
78  if (conf.exists("hitMasks")) {
79  hitMasksToken = consumes<std::vector<bool> >(conf.getParameter<edm::InputTag>("hitMasks"));
80  }
81  seedToken = consumes<edm::View<TrajectorySeed> >(conf.getParameter<edm::InputTag>("src"));
83  consumes<FastTrackerRecHitCombinationCollection>(conf.getParameter<edm::InputTag>("recHitCombinations"));
84  simTrackToken = consumes<edm::SimTrackContainer>(conf.getParameter<edm::InputTag>("simTracks"));
85 
86  // other parameters
87  maxSeedMatchEstimator = conf.getUntrackedParameter<double>("maxSeedMatchEstimator", 0);
88  rejectOverlaps = conf.getParameter<bool>("OverlapCleaning");
89  splitHits = conf.getParameter<bool>("SplitHits");
90 }
91 
93  // get records
95  auto const& trackerGeometry = es.getData(trackerGeometryESToken_);
96  auto const& trackerTopology = es.getData(trackerTopologyESToken_);
97  auto const& propagator = es.getData(propagatorESToken_);
98 
99  // get products
101  e.getByToken(seedToken, seeds);
102 
105 
107  e.getByToken(simTrackToken, simTracks);
108 
111  e.getByToken(hitMasksToken, hitMasks);
112  }
113 
114  // output collection
115  std::unique_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
116 
117  // loop over the seeds
118  for (unsigned seedIndex = 0; seedIndex < seeds->size(); ++seedIndex) {
119  const TrajectorySeed seed = (*seeds)[seedIndex];
120  std::vector<int32_t> recHitCombinationIndices;
121 
122  // match hitless seeds to simTracks and find corresponding recHitCombination
123  if (seed.nHits() == 0) {
124  recHitCombinationIndices = SeedMatcher::matchRecHitCombinations(
126  }
127  // for normal seeds, retrieve the corresponding recHitCombination from the seed hits
128  else {
130  recHitCombinationIndices.push_back(icomb);
131  }
132 
133  // loop over the matched recHitCombinations
134  for (auto icomb : recHitCombinationIndices) {
135  if (icomb < 0 || unsigned(icomb) >= recHitCombinations->size()) {
136  throw cms::Exception("TrackCandidateProducer")
137  << " found seed with recHitCombination out or range: " << icomb << std::endl;
138  }
139  const FastTrackerRecHitCombination& recHitCombination = (*recHitCombinations)[icomb];
140 
141  // container for select hits
142  std::vector<const FastTrackerRecHit*> selectedRecHits;
143 
144  // add the seed hits
145  for (auto const& recHit : seed.recHits()) {
146  selectedRecHits.push_back(static_cast<const FastTrackerRecHit*>(&recHit));
147  }
148 
149  // prepare to skip seed hits
150  const FastTrackerRecHit* lastHitToSkip = nullptr;
151  if (!selectedRecHits.empty()) {
152  lastHitToSkip = selectedRecHits.back();
153  }
154 
155  // inOut or outIn tracking ?
156  bool hitsAlongMomentum = (seed.direction() == alongMomentum);
157 
158  // add hits from combination to hit selection
159  for (unsigned hitIndex = hitsAlongMomentum ? 0 : recHitCombination.size() - 1;
160  hitIndex < recHitCombination.size();
161  hitsAlongMomentum ? ++hitIndex : --hitIndex) {
162  const FastTrackerRecHit* selectedRecHit = recHitCombination[hitIndex].get();
163 
164  // skip seed hits
165  if (lastHitToSkip) {
166  if (lastHitToSkip->sameId(selectedRecHit)) {
167  lastHitToSkip = nullptr;
168  }
169  continue;
170  }
171 
172  // apply hit masking
173  if (hitMasks.isValid() && fastTrackingUtilities::hitIsMasked(selectedRecHit, *hitMasks)) {
174  continue;
175  }
176 
177  // if overlap rejection is not switched on, accept all hits
178  // always accept the first hit
179  // also accept a hit if it is not on the layer of the previous hit
180  if (!rejectOverlaps || selectedRecHits.empty() ||
181  (TrackingLayer::createFromDetId(selectedRecHits.back()->geographicalId(), trackerTopology) !=
182  TrackingLayer::createFromDetId(selectedRecHit->geographicalId(), trackerTopology))) {
183  selectedRecHits.push_back(selectedRecHit);
184  }
185  // else:
186  // overlap rejection is switched on
187  // the hit is on the same layer as the previous hit
188  // accept the one with smallest error
189  else if (fastTrackingUtilities::hitLocalError(selectedRecHit) <
190  fastTrackingUtilities::hitLocalError(selectedRecHits.back())) {
191  selectedRecHits.back() = selectedRecHit;
192  }
193  }
194 
195  // split hits / store copies for the track candidate
196  edm::OwnVector<TrackingRecHit> hitsForTrackCandidate;
197  for (unsigned index = 0; index < selectedRecHits.size(); ++index) {
198  if (splitHits) {
199  // add split hits to splitSelectedRecHits
200  hitSplitter.split(*selectedRecHits[index], hitsForTrackCandidate, hitsAlongMomentum);
201  } else {
202  hitsForTrackCandidate.push_back(selectedRecHits[index]->clone());
203  }
204  }
205 
206  // set the recHitCombinationIndex
207  fastTrackingUtilities::setRecHitCombinationIndex(hitsForTrackCandidate, icomb);
208 
209  // create track candidate state
210  // 1. get seed state (defined on the surface of the most outer hit)
211  DetId seedDetId(seed.startingState().detId());
212  const GeomDet* gdet = trackerGeometry.idToDet(seedDetId);
213  TrajectoryStateOnSurface seedTSOS =
214  trajectoryStateTransform::transientState(seed.startingState(), &(gdet->surface()), &magneticField);
215  // 2. backPropagate the seedState to the surfuce of the most inner hit
216  const GeomDet* initialLayer = trackerGeometry.idToDet(hitsForTrackCandidate.front().geographicalId());
217  const TrajectoryStateOnSurface initialTSOS = propagator.propagate(seedTSOS, initialLayer->surface());
218  // 3. check validity and transform
219  if (!initialTSOS.isValid())
220  continue;
222  initialTSOS, hitsForTrackCandidate.front().geographicalId().rawId());
223 
224  // add track candidate to output collection
225  output->push_back(
226  TrackCandidate(hitsForTrackCandidate, seed, PTSOD, edm::RefToBase<TrajectorySeed>(seeds, seedIndex)));
227  }
228  }
229 
230  // Save the track candidates
231  e.put(std::move(output));
232 }
233 
void setRecHitCombinationIndex(edm::OwnVector< T > &recHits, int32_t icomb)
static std::vector< int > matchRecHitCombinations(const TrajectorySeed &seed, const FastTrackerRecHitCombinationCollection &recHitCombinationCollection, const std::vector< SimTrack > &simTrackCollection, double maxMatchEstimator, const Propagator &propagator, const MagneticField &magneticField, const TrackerGeometry &trackerGeometry)
Definition: SeedMatcher.cc:14
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryESToken_
void produce(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::View< TrajectorySeed > > seedToken
edm::EDGetTokenT< edm::SimTrackContainer > simTrackToken
TrackCandidateProducer(const edm::ParameterSet &conf)
bool exists(std::string const &parameterName) const
checks if a parameter exists
static TrackingLayer createFromDetId(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackingLayer.cc:9
std::vector< TrackCandidate > TrackCandidateCollection
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
bool sameId(const FastTrackerRecHit *other, size_t i=0, size_t j=0) const
FastTrackerRecHitSplitter hitSplitter
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magneticFieldESToken_
T getUntrackedParameter(std::string const &, T const &) const
void push_back(D *&d)
Definition: OwnVector.h:326
edm::EDGetTokenT< FastTrackerRecHitCombinationCollection > recHitCombinationsToken
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
edm::EDGetTokenT< std::vector< bool > > hitMasksToken
bool hitIsMasked(const FastTrackerRecHit *hit, const std::vector< bool > &hitMasks)
int32_t getRecHitCombinationIndex(const T &object)
const edm::ESGetToken< Propagator, TrackingComponentsRecord > propagatorESToken_
Definition: DetId.h:17
std::vector< FastTrackerRecHitRef > FastTrackerRecHitCombination
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
DetId geographicalId() const
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double hitLocalError(const TrackingRecHit *hit)
HLT enums.
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyESToken_
reference front()
Definition: OwnVector.h:459
void split(const FastTrackerRecHit &hitIn, edm::OwnVector< TrackingRecHit > &hitsOut, bool alongMomentum) const
def move(src, dest)
Definition: eostools.py:511