CMS 3D CMS Logo

NearbyPixelClustersProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Calibration/TkAlCaRecoProducers
4 // Class: NearbyPixelClustersProducer
5 //
14 //
15 // Original Author: Marco Musich
16 // Created: Mon, 29 Mar 2021 12:29:30 GMT
17 //
18 //
19 
20 // system include files
21 #include <memory>
22 #include <map>
23 
24 // user include files
56 
57 using trajCrossings_t = std::map<uint32_t, std::vector<LocalPoint>>;
58 
59 //
60 // class declaration
61 //
62 
64 public:
66  ~NearbyPixelClustersProducer() override = default;
67 
68  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
69 
70 private:
71  void produce(edm::Event&, const edm::EventSetup&) override;
73  const edm::Handle<TrajTrackAssociationCollection>& trajTrackCollectionHandle);
74 
75  const std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> findAllNearbyClusters(
77  const uint32_t rawId,
78  const std::vector<LocalPoint>& vLocalPos);
79 
80  const std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> findAllNearbyClusters(
81  const SiPixelClusterCollectionNew& clusterSet, const uint32_t rawId, const std::vector<LocalPoint>& vLocalPos);
82 
84  bool detidIsOnPixel(const DetId& detid);
85 
86  // ----------member data ---------------------------
87 
88  // switches
89  const bool throwBadComponents_;
90  const bool dumpWholeDetId_;
91 
92  // esTokens
96 
97  // edToken
100 
101  // putToken
103 
104  // event setup
107 };
108 
109 //
110 // constructors and destructor
111 //
113  : throwBadComponents_(iConfig.getParameter<bool>("throwBadComponents")),
114  dumpWholeDetId_(iConfig.getParameter<bool>("dumpWholeDetIds")),
115  geomEsToken_(esConsumes()),
116  pixelCPEEsToken_(esConsumes(edm::ESInputTag("", "PixelCPEGeneric"))),
117  badModuleToken_(esConsumes()),
118  clustersToken_(consumes<SiPixelClusterCollectionNew>(iConfig.getParameter<edm::InputTag>("clusterCollection"))),
119  trajTrackCollectionToken_(
120  consumes<TrajTrackAssociationCollection>(iConfig.getParameter<edm::InputTag>("trajectoryInput"))),
121  clusterPutToken_(produces<SiPixelClusterCollectionNew>()) {
122  if (dumpWholeDetId_) {
123  edm::LogPrint("NearbyPixelClustersProducer") << "WARNING: selected to dump the whole DetId's worth of clusters.\n "
124  "This will have consequences on the event size!"
125  << std::endl;
126  }
127 }
128 
129 //
130 // member functions
131 //
132 
133 // ------------ method called to produce the data ------------
135  using namespace edm;
136 
137  auto outputClusters = std::make_unique<SiPixelClusterCollectionNew>();
138 
139  // get the Tracker geometry from event setup
141 
142  // get the Pixel CPE from event setup
144 
145  const auto& SiPixelBadModule_ = &iSetup.getData(badModuleToken_);
146 
147  // get cluster collection
148  const auto& clusterCollectionHandle = iEvent.getHandle(clustersToken_);
149  const SiPixelClusterCollectionNew& clusterCollection = *clusterCollectionHandle;
150 
151  // get Traj-Track Collection
152  const auto& trajTrackCollectionHandle = iEvent.getHandle(trajTrackCollectionToken_);
153  if (!trajTrackCollectionHandle.isValid())
154  return;
155 
156  // find all trajectory crossings in the event
157  const auto& allCrossings = this->findAllTrajectoriesCrossings(trajTrackCollectionHandle);
158 
159  LogDebug("NearbyPixelClustersProducer") << allCrossings.size() << std::endl;
160 
161  // now find all nearby clusters
162  for (const auto& [id, vLocalPos] : allCrossings) {
163  // prepare the filler
165 
166  // retrieve the clusters of the right detId
167  const auto& clustersOnDet = clusterCollection.find(DetId(id));
168 
169  if (clustersOnDet == clusterCollection.end())
170  continue;
171 
172  // if the cluster DetSet is not valid, move on
173  if (!(*clustersOnDet).isValid())
174  continue;
175 
176  // if the module is bad continue
177  if (throwBadComponents_ && SiPixelBadModule_->IsModuleBad(id))
178  continue;
179 
180  // find all the clusters to put into the event
181  const auto& clustersToPut = this->findAllNearbyClusters(clustersOnDet, id, vLocalPos);
182 
183  // find all the clusters to put into the event (different interface)
184  //const auto& clustersToPut = this->findAllNearbyClusters(clusterCollection, id, vLocalPos);
185 
186  for (const auto& cluster : clustersToPut) {
187  spc.push_back(*cluster);
188  }
189 
190  if (spc.empty())
191  spc.abort();
192 
193  } // loop on trajectory crossings
194 
196 }
197 
198 /*--------------------------------------------------------------------*/
200  const edm::Handle<TrajTrackAssociationCollection>& trajTrackCollectionHandle)
201 /*--------------------------------------------------------------------*/
202 {
203  trajCrossings_t crossings;
204 
205  std::vector<uint32_t> treatedIds;
206 
207  for (const auto& pair : *trajTrackCollectionHandle) {
208  const edm::Ref<std::vector<Trajectory>> traj = pair.key;
209 
210  for (const TrajectoryMeasurement& measurement : traj->measurements()) {
211  //Check if the measurement infos can be read
212  if (!measurement.updatedState().isValid())
213  continue;
214 
215  const TransientTrackingRecHit::ConstRecHitPointer& recHit = measurement.recHit();
216 
217  // Only looking for pixel hits
218  DetId recHitDetid = recHit->geographicalId();
219  const auto& rawId = recHitDetid.rawId();
220 
221  if (!this->detidIsOnPixel(recHitDetid))
222  continue;
223 
224  // Skipping hits with undeterminable positions
225  TrajectoryStateOnSurface trajStateOnSurface = this->getTrajectoryStateOnSurface(measurement);
226 
227  if (!(trajStateOnSurface.isValid()))
228  continue;
229 
230  // Position measurements
231  // Looking for valid and missing hits
232  LocalPoint localPosition = trajStateOnSurface.localPosition();
233 
234  if (std::find(treatedIds.begin(), treatedIds.end(), rawId) != treatedIds.end()) {
235  crossings.at(rawId).push_back(localPosition);
236  } else {
237  crossings.insert(std::pair<uint32_t, std::vector<LocalPoint>>(rawId, {localPosition}));
238  treatedIds.push_back(rawId);
239  }
240  } // loop on measurements in trajectory
241  } // loop on trajectories
242 
243  return crossings;
244 }
245 
246 /*--------------------------------------------------------------------*/
247 const std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> NearbyPixelClustersProducer::findAllNearbyClusters(
249  const uint32_t rawId,
250  const std::vector<LocalPoint>& vLocalPos)
251 /*--------------------------------------------------------------------*/
252 {
253  std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> outputClusters;
254 
255  static constexpr unsigned int k_maxClustersInDet = 1024;
256 
257  // something funny is going on here ...
258  if ((*clusterSet).size() > k_maxClustersInDet) {
259  edm::LogWarning("NearbyPixelClustersProducer")
260  << __func__ << "() number of clusters in det " << rawId /*(*clusterSet).id()*/ << " is " << (*clusterSet).size()
261  << ", which is larger than maximum (1024).\n Something funny with the data is going on!" << std::endl;
262  return outputClusters;
263  }
264 
265  const PixelGeomDetUnit* pixdet = (const PixelGeomDetUnit*)trackerGeometry_->idToDetUnit(rawId);
266  edmNew::DetSet<SiPixelCluster>::const_iterator itCluster = clusterSet->begin();
267 
268  // just copy straight the whole set of clusters in the detid
269  if (dumpWholeDetId_) {
270  for (; itCluster != clusterSet->end(); ++itCluster) {
271  outputClusters.push_back(itCluster);
272  }
273  return outputClusters;
274  }
275 
276  int count = 0;
277  for (const auto& localPos : vLocalPos) {
278  count++;
279  //trajectory crossing local coordinates
280  const auto& traj_lx = localPos.x();
281  const auto& traj_ly = localPos.y();
282 
283  float minD = 10000.;
285 
286  //std::cout<< rawId << " count: " << count << " n. clusters: " << (*clusterSet).size() << std::endl;
287  LogDebug("NearbyPixelClustersProducer")
288  << __func__ << rawId << " count: " << count << " n. clusters: " << (*clusterSet).size() << std::endl;
289 
290  for (; itCluster != clusterSet->end(); ++itCluster) {
291  LocalPoint lp(itCluster->x(), itCluster->y(), 0.);
292  const auto& params = pixelCPE_->getParameters(*itCluster, *pixdet);
293  lp = std::get<0>(params);
294 
295  float D = sqrt((lp.x() - traj_lx) * (lp.x() - traj_lx) + (lp.y() - traj_ly) * (lp.y() - traj_ly));
296  if (D < minD) {
297  closest = itCluster;
298  minD = D;
299  }
300  } // loop on cluster sets
301 
302  if (closest) {
303  outputClusters.push_back(closest);
304  }
305  } // loop on the trajectory crossings
306 
307  return outputClusters;
308 }
309 
310 // overloaded method: use the whole DetSet
311 /*--------------------------------------------------------------------*/
312 const std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> NearbyPixelClustersProducer::findAllNearbyClusters(
314  const uint32_t rawId,
315  const std::vector<LocalPoint>& vLocalPos)
316 /*--------------------------------------------------------------------*/
317 {
318  std::vector<edmNew::DetSet<SiPixelCluster>::const_iterator> outputClusters;
319 
320  int count = 0;
321  for (const auto& localPos : vLocalPos) {
322  count++;
323 
324  //trajectory crossing local coordinates
325  const auto& traj_lx = localPos.x();
326  const auto& traj_ly = localPos.y();
327 
328  float minD = 10000.;
329 
331  for (; itClusterSet != clusterCollection.end(); itClusterSet++) {
332  DetId detId(itClusterSet->id());
333  if (detId.rawId() != rawId)
334  continue;
335 
336  unsigned int subDetId = detId.subdetId();
338  edm::LogError("NearByPixelClusterProducer")
339  << "ERROR: not a pixel cluster!!!" << std::endl; // should not happen
340  continue;
341  }
342 
343  edmNew::DetSet<SiPixelCluster>::const_iterator itCluster = itClusterSet->begin();
344 
345  // just copy straight the whole set of clusters in the detid
346  if (dumpWholeDetId_ && count == 1) {
347  for (; itCluster != itClusterSet->end(); ++itCluster) {
348  outputClusters.push_back(itCluster);
349  }
350  return outputClusters;
351  }
352 
353  //std::cout<< rawId << " count: " << count << " n. clusters: " << (*clusterSet).size() << std::endl;
354  LogDebug("NearbyPixelClustersProducer")
355  << __func__ << rawId << " count: " << count << " n. clusters: " << (*itClusterSet).size() << std::endl;
356 
357  const PixelGeomDetUnit* pixdet = (const PixelGeomDetUnit*)trackerGeometry_->idToDetUnit(rawId);
358 
360 
361  for (; itCluster != itClusterSet->end(); ++itCluster) {
362  LocalPoint lp(itCluster->x(), itCluster->y(), 0.);
363  const auto& params = pixelCPE_->getParameters(*itCluster, *pixdet);
364  lp = std::get<0>(params);
365 
366  float D = sqrt((lp.x() - traj_lx) * (lp.x() - traj_lx) + (lp.y() - traj_ly) * (lp.y() - traj_ly));
367  if (D < minD) {
368  closest = itCluster;
369  minD = D;
370  }
371  } // loop on cluster sets
372 
373  if (closest) {
374  outputClusters.push_back(closest);
375  }
376  } // loop on all clusters
377  } // loop on the trajectory crossings
378 
379  return outputClusters;
380 }
381 
382 /*--------------------------------------------------------------------*/
384 /*--------------------------------------------------------------------*/
385 {
386  if (detid.det() != DetId::Tracker)
387  return false;
389  return true;
391  return true;
392  return false;
393 }
394 
395 /*--------------------------------------------------------------------*/
397  const TrajectoryMeasurement& measurement)
398 /*--------------------------------------------------------------------*/
399 {
400  const static TrajectoryStateCombiner trajStateCombiner;
401 
402  const auto& forwardPredictedState = measurement.forwardPredictedState();
403  const auto& backwardPredictedState = measurement.backwardPredictedState();
404 
405  if (forwardPredictedState.isValid() && backwardPredictedState.isValid())
406  return trajStateCombiner(forwardPredictedState, backwardPredictedState);
407 
408  else if (backwardPredictedState.isValid())
409  return backwardPredictedState;
410 
411  else if (forwardPredictedState.isValid())
412  return forwardPredictedState;
413 
414  edm::LogError("NearbyPixelClustersProducer") << "Error saving traj. measurement data."
415  << " Trajectory state on surface cannot be determined." << std::endl;
416 
417  return TrajectoryStateOnSurface();
418 }
419 
420 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
423  desc.setComment(
424  "Produces the collection of SiPixelClusters closest, hit by hit, to the trajectory measurements of a given "
425  "track");
426  desc.add<bool>("throwBadComponents", false)
427  ->setComment(
428  "do not consider modules flagged as bad components. Careful, it changes the efficiency denominator!");
429  desc.add<bool>("dumpWholeDetIds", false)
430  ->setComment("put in the event all the pixel cluster on the impacted module, by default only the closest");
431  ;
432  desc.add<edm::InputTag>("clusterCollection", edm::InputTag("siPixelClusters"));
433  desc.add<edm::InputTag>("trajectoryInput", edm::InputTag("myRefitter"));
434  descriptions.addWithDefaultLabel(desc);
435 }
436 
437 //define this as a plug-in
const std::vector< edmNew::DetSet< SiPixelCluster >::const_iterator > findAllNearbyClusters(const SiPixelClusterCollectionNew::const_iterator &clusterSet, const uint32_t rawId, const std::vector< LocalPoint > &vLocalPos)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void push_back(data_type const &d)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const TrackerGeomDet * idToDetUnit(DetId) const override
Return the pointer to the GeomDetUnit corresponding to a given DetId.
edm::EDPutTokenT< SiPixelClusterCollectionNew > clusterPutToken_
std::map< uint32_t, std::vector< LocalPoint > > trajCrossings_t
const PixelClusterParameterEstimator * pixelCPE_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrajectoryStateOnSurface const & forwardPredictedState() const
Access to forward predicted state (from fitter or builder)
constexpr unsigned int subDetId[21]
edm::EDGetTokenT< TrajTrackAssociationCollection > trajTrackCollectionToken_
const trajCrossings_t findAllTrajectoriesCrossings(const edm::Handle< TrajTrackAssociationCollection > &trajTrackCollectionHandle)
data_type const * const_iterator
Definition: DetSetNew.h:31
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
key_type key() const
Accessor for product key.
Definition: Ref.h:250
virtual ReturnType getParameters(const SiPixelCluster &cl, const GeomDetUnit &det) const =0
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int iEvent
Definition: GenABIO.cc:224
std::shared_ptr< TrackingRecHit const > ConstRecHitPointer
T sqrt(T t)
Definition: SSEVec.h:19
TrajectoryStateOnSurface const & backwardPredictedState() const
Access to backward predicted state (from smoother)
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
bool getData(T &iHolder) const
Definition: EventSetup.h:122
const edm::ESGetToken< PixelClusterParameterEstimator, TkPixelCPERecord > pixelCPEEsToken_
NearbyPixelClustersProducer(const edm::ParameterSet &)
Log< level::Warning, true > LogPrint
TrajectoryStateOnSurface getTrajectoryStateOnSurface(const TrajectoryMeasurement &measurement)
const edm::ESGetToken< SiPixelQuality, SiPixelQualityFromDbRcd > badModuleToken_
~NearbyPixelClustersProducer() override=default
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:141
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void produce(edm::Event &, const edm::EventSetup &) override
HLT enums.
iterator end()
Definition: DetSetNew.h:56
Log< level::Warning, false > LogWarning
edm::EDGetTokenT< SiPixelClusterCollectionNew > clustersToken_
def move(src, dest)
Definition: eostools.py:511
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
#define LogDebug(id)
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > geomEsToken_