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 {
44 public:
45 
46  explicit TrackCandidateProducer(const edm::ParameterSet& conf);
47 
48  void produce(edm::Event& e, const edm::EventSetup& es) override;
49 
50 private:
51 
52  // tokens & labels
58 
59  // other data
61  bool splitHits;
64 };
65 
67  : hitSplitter()
68 {
69  // produces
70  produces<TrackCandidateCollection>();
71 
72  // consumes
73  if (conf.exists("hitMasks")){
74  hitMasksToken = consumes<std::vector<bool> >(conf.getParameter<edm::InputTag>("hitMasks"));
75  }
76  seedToken = consumes<edm::View<TrajectorySeed> >(conf.getParameter<edm::InputTag>("src"));
77  recHitCombinationsToken = consumes<FastTrackerRecHitCombinationCollection>(conf.getParameter<edm::InputTag>("recHitCombinations"));
78  simTrackToken = consumes<edm::SimTrackContainer>(conf.getParameter<edm::InputTag>("simTracks"));
79 
80  // other parameters
81  maxSeedMatchEstimator = conf.getUntrackedParameter<double>("maxSeedMatchEstimator",0);
82  rejectOverlaps = conf.getParameter<bool>("OverlapCleaning");
83  splitHits = conf.getParameter<bool>("SplitHits");
84  propagatorLabel = conf.getParameter<std::string>("propagator");
85 }
86 
87 void
89 
90  // get records
92  es.get<IdealMagneticFieldRecord>().get(magneticField);
93 
94  edm::ESHandle<TrackerGeometry> trackerGeometry;
95  es.get<TrackerDigiGeometryRecord>().get(trackerGeometry);
96 
97  edm::ESHandle<TrackerTopology> trackerTopology;
98  es.get<TrackerTopologyRcd>().get(trackerTopology);
99 
101  es.get<TrackingComponentsRecord>().get(propagatorLabel,propagator);
102 
103  // get products
105  e.getByToken(seedToken,seeds);
106 
108  e.getByToken(recHitCombinationsToken, recHitCombinations);
109 
111  e.getByToken(simTrackToken,simTracks);
112 
115  {
116  e.getByToken(hitMasksToken,hitMasks);
117  }
118 
119  // output collection
120  std::unique_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
121 
122  // loop over the seeds
123  for (unsigned seedIndex = 0; seedIndex < seeds->size(); ++seedIndex){
124 
125  const TrajectorySeed seed = (*seeds)[seedIndex];
126  std::vector<int32_t> recHitCombinationIndices;
127 
128  // match hitless seeds to simTracks and find corresponding recHitCombination
129  if(seed.nHits()==0){
130  recHitCombinationIndices = SeedMatcher::matchRecHitCombinations(
131  seed,
132  *recHitCombinations,
133  *simTracks,
135  *propagator,
136  *magneticField,
137  *trackerGeometry);
138  }
139  // for normal seeds, retrieve the corresponding recHitCombination from the seed hits
140  else
141  {
143  recHitCombinationIndices.push_back(icomb);
144  }
145 
146  // loop over the matched recHitCombinations
147  for(auto icomb : recHitCombinationIndices)
148  {
149  if(icomb < 0 || unsigned(icomb) >= recHitCombinations->size())
150  {
151  throw cms::Exception("TrackCandidateProducer") << " found seed with recHitCombination out or range: " << icomb << std::endl;
152  }
153  const FastTrackerRecHitCombination & recHitCombination = (*recHitCombinations)[icomb];
154 
155  // container for select hits
156  std::vector<const FastTrackerRecHit *> selectedRecHits;
157 
158  // add the seed hits
159  TrajectorySeed::range seedHitRange = seed.recHits();//Hits in a seed
160  for (TrajectorySeed::const_iterator ihit = seedHitRange.first; ihit != seedHitRange.second; ++ihit)
161  {
162  selectedRecHits.push_back(static_cast<const FastTrackerRecHit*>(&*ihit));
163  }
164 
165  // prepare to skip seed hits
166  const FastTrackerRecHit * lastHitToSkip = nullptr;
167  if(!selectedRecHits.empty())
168  {
169  lastHitToSkip = selectedRecHits.back();
170  }
171 
172  // inOut or outIn tracking ?
173  bool hitsAlongMomentum = (seed.direction()== alongMomentum);
174 
175  // add hits from combination to hit selection
176  for (unsigned hitIndex = hitsAlongMomentum ? 0 : recHitCombination.size() - 1;
177  hitIndex < recHitCombination.size();
178  hitsAlongMomentum ? ++hitIndex : --hitIndex)
179  {
180 
181  const FastTrackerRecHit * selectedRecHit = recHitCombination[hitIndex].get();
182 
183  // skip seed hits
184  if(lastHitToSkip)
185  {
186  if(lastHitToSkip->sameId(selectedRecHit))
187  {
188  lastHitToSkip=nullptr;
189  }
190  continue;
191  }
192 
193  // apply hit masking
194  if(hitMasks.isValid() && fastTrackingUtilities::hitIsMasked(selectedRecHit,*hitMasks))
195  {
196  continue;
197  }
198 
199 
200  // if overlap rejection is not switched on, accept all hits
201  // always accept the first hit
202  // also accept a hit if it is not on the layer of the previous hit
203  if( ! rejectOverlaps
204  || selectedRecHits.empty()
205  || ( TrackingLayer::createFromDetId(selectedRecHits.back()->geographicalId(),*trackerTopology.product())
206  != TrackingLayer::createFromDetId(selectedRecHit->geographicalId(),*trackerTopology.product())))
207  {
208  selectedRecHits.push_back(selectedRecHit);
209  }
210  // else:
211  // overlap rejection is switched on
212  // the hit is on the same layer as the previous hit
213  // accept the one with smallest error
214  else if ( fastTrackingUtilities::hitLocalError(selectedRecHit)
215  < fastTrackingUtilities::hitLocalError(selectedRecHits.back()) )
216  {
217  selectedRecHits.back() = selectedRecHit;
218  }
219  }
220 
221  // split hits / store copies for the track candidate
222  edm::OwnVector<TrackingRecHit> hitsForTrackCandidate;
223  for ( unsigned index = 0; index<selectedRecHits.size(); ++index )
224  {
225  if(splitHits)
226  {
227  // add split hits to splitSelectedRecHits
228  hitSplitter.split(*selectedRecHits[index],hitsForTrackCandidate,hitsAlongMomentum);
229  }
230  else
231  {
232  hitsForTrackCandidate.push_back(selectedRecHits[index]->clone());
233  }
234  }
235 
236  // set the recHitCombinationIndex
237  fastTrackingUtilities::setRecHitCombinationIndex(hitsForTrackCandidate,icomb);
238 
239  // create track candidate state
240  // 1. get seed state (defined on the surface of the most outer hit)
241  DetId seedDetId(seed.startingState().detId());
242  const GeomDet* gdet = trackerGeometry->idToDet(seedDetId);
243  TrajectoryStateOnSurface seedTSOS = trajectoryStateTransform::transientState(seed.startingState(), &(gdet->surface()),magneticField.product());
244  // 2. backPropagate the seedState to the surfuce of the most inner hit
245  const GeomDet* initialLayer = trackerGeometry->idToDet(hitsForTrackCandidate.front().geographicalId());
246  const TrajectoryStateOnSurface initialTSOS = propagator->propagate(seedTSOS,initialLayer->surface()) ;
247  // 3. check validity and transform
248  if (!initialTSOS.isValid()) continue;
249  PTrajectoryStateOnDet PTSOD = trajectoryStateTransform::persistentState(initialTSOS,hitsForTrackCandidate.front().geographicalId().rawId());
250 
251  // add track candidate to output collection
252  output->push_back(TrackCandidate(hitsForTrackCandidate,seed,PTSOD,edm::RefToBase<TrajectorySeed>(seeds,seedIndex)));
253  }
254  }
255 
256  // Save the track candidates
257  e.put(std::move(output));
258 
259 }
260 
PropagationDirection direction() const
void setRecHitCombinationIndex(edm::OwnVector< T > &recHits, int32_t icomb)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
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
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
edm::EDGetTokenT< edm::View< TrajectorySeed > > seedToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
bool sameId(const FastTrackerRecHit *other, size_t i=0, size_t j=0) const
edm::EDGetTokenT< edm::SimTrackContainer > simTrackToken
TrackCandidateProducer(const edm::ParameterSet &conf)
static TrackingLayer createFromDetId(const DetId &detId, const TrackerTopology &trackerTopology)
std::vector< TrackCandidate > TrackCandidateCollection
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
bool exists(std::string const &parameterName) const
checks if a parameter exists
FastTrackerRecHitSplitter hitSplitter
void produce(edm::Event &e, const edm::EventSetup &es) override
void push_back(D *&d)
Definition: OwnVector.h:290
const SurfaceType & surface() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< FastTrackerRecHitCombinationCollection > recHitCombinationsToken
recHitContainer::const_iterator const_iterator
std::pair< const_iterator, const_iterator > range
unsigned int detId() const
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< std::vector< bool > > hitMasksToken
bool hitIsMasked(const FastTrackerRecHit *hit, const std::vector< bool > &hitMasks)
int32_t getRecHitCombinationIndex(const T &object)
Definition: DetId.h:18
std::vector< FastTrackerRecHitRef > FastTrackerRecHitCombination
PTrajectoryStateOnDet const & startingState() const
void split(const FastTrackerRecHit &hitIn, edm::OwnVector< TrackingRecHit > &hitsOut, bool alongMomentum) const
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
range recHits() const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:53
unsigned int nHits() const
double hitLocalError(const TrackingRecHit *hit)
T get() const
Definition: EventSetup.h:71
const TrackerGeomDet * idToDet(DetId) const override
bool isUninitialized() const
Definition: EDGetToken.h:70
DetId geographicalId() const
T const * product() const
Definition: ESHandle.h:86
reference front()
Definition: OwnVector.h:423
def move(src, dest)
Definition: eostools.py:511