CMS 3D CMS Logo

SeedClusterRemover.cc
Go to the documentation of this file.
7 
23 
26 
31 
36 
37 //
38 // class decleration
39 //
40 
42  public:
43  SeedClusterRemover(const edm::ParameterSet& iConfig) ;
44  ~SeedClusterRemover() override ;
45  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override ;
46  private:
47  struct ParamBlock {
49  ParamBlock(const edm::ParameterSet& iConfig) :
50  isSet_(true),
51  usesCharge_(iConfig.exists("maxCharge")),
52  usesSize_(iConfig.exists("maxSize")),
53  maxChi2_(iConfig.getParameter<double>("maxChi2")),
54  maxCharge_(usesCharge_ ? iConfig.getParameter<double>("maxCharge") : 0),
55  maxSize_(usesSize_ ? iConfig.getParameter<uint32_t>("maxSize") : 0) { }
58  size_t maxSize_;
59  };
60  static const unsigned int NumberOfParamBlocks = 6;
61 
63  bool mergeOld_;
72 
73 // std::vector< edm::EDGetTokenT<edm::ValueMap<int> > > overrideTrkQuals_;
74 
76  void readPSet(const edm::ParameterSet& iConfig, const std::string &name,
77  int id1=-1, int id2=-1, int id3=-1, int id4=-1, int id5=-1, int id6=-1) ;
78 
79  std::vector<uint8_t> pixels, strips; // avoid unneed alloc/dealloc of this
80  edm::ProductID pixelSourceProdID, stripSourceProdID; // ProdIDs refs must point to (for consistency tests)
81 
82  inline void process(const TrackingRecHit *hit, float chi2, const TrackerGeometry* tg);
83  inline void process(const OmniClusterRef & cluRef, uint32_t subdet);
84 
85 
86  template<typename T>
87  std::unique_ptr<edmNew::DetSetVector<T> >
88  cleanup(const edmNew::DetSetVector<T> &oldClusters, const std::vector<uint8_t> &isGood,
90 
91  // Carries in full removal info about a given det from oldRefs
93 
97  std::vector<bool> collectedStrips_;
98  std::vector<bool> collectedPixels_;
99 };
100 
101 
102 using namespace std;
103 using namespace edm;
104 using namespace reco;
105 
106 void
108  int id1, int id2, int id3, int id4, int id5, int id6)
109 {
110  if (iConfig.exists(name)) {
111  ParamBlock pblock(iConfig.getParameter<ParameterSet>(name));
112  if (id1 == -1) {
114  } else {
115  pblocks_[id1] = pblock;
116  if (id2 != -1) pblocks_[id2] = pblock;
117  if (id3 != -1) pblocks_[id3] = pblock;
118  if (id4 != -1) pblocks_[id4] = pblock;
119  if (id5 != -1) pblocks_[id5] = pblock;
120  if (id6 != -1) pblocks_[id6] = pblock;
121  }
122  }
123 }
124 
126  doStrip_(iConfig.existsAs<bool>("doStrip") ? iConfig.getParameter<bool>("doStrip") : true),
127  doPixel_(iConfig.existsAs<bool>("doPixel") ? iConfig.getParameter<bool>("doPixel") : true),
128  mergeOld_(iConfig.exists("oldClusterRemovalInfo")),
130 {
131 // if (iConfig.exists("overrideTrkQuals"))
132 // overrideTrkQuals_.push_back(iConfig.getParameter<edm::InputTag>("overrideTrkQuals"));
133  if (iConfig.exists("clusterLessSolution"))
134  clusterWasteSolution_=!iConfig.getParameter<bool>("clusterLessSolution");
135  if (doPixel_ && clusterWasteSolution_) produces< edmNew::DetSetVector<SiPixelCluster> >();
136  if (doStrip_ && clusterWasteSolution_) produces< edmNew::DetSetVector<SiStripCluster> >();
137  if (clusterWasteSolution_) produces< ClusterRemovalInfo >();
138 
140  readPSet(iConfig, "Common",-1);
141  if (doPixel_) {
142  readPSet(iConfig, "Pixel" ,0,1);
143  readPSet(iConfig, "PXB" ,0);
144  readPSet(iConfig, "PXE" ,1);
145  }
146  if (doStrip_) {
147  readPSet(iConfig, "Strip" ,2,3,4,5);
148  readPSet(iConfig, "StripInner" ,2,3);
149  readPSet(iConfig, "StripOuter" ,4,5);
150  readPSet(iConfig, "TIB" ,2);
151  readPSet(iConfig, "TID" ,3);
152  readPSet(iConfig, "TOB" ,4);
153  readPSet(iConfig, "TEC" ,5);
154  }
155 
156  bool usingCharge = false;
157  for (size_t i = 0; i < NumberOfParamBlocks; ++i) {
158  if (!pblocks_[i].isSet_) throw cms::Exception("Configuration Error") << "SeedClusterRemover: Missing configuration for detector with subDetID = " << (i+1);
159  if (pblocks_[i].usesCharge_ && !usingCharge) {
160  throw cms::Exception("Configuration Error") << "SeedClusterRemover: Configuration for subDetID = " << (i+1) << " uses cluster charge, which is not enabled.";
161  }
162  }
163 
164  if (!clusterWasteSolution_){
165  produces<edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > >();
166  produces<edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > >();
167  }
169  filterTracks_=false;
170  if (iConfig.exists("TrackQuality")){
171  filterTracks_=true;
172  trackQuality_=reco::TrackBase::qualityByName(iConfig.getParameter<std::string>("TrackQuality"));
173  }
174 
175  trajectories_ = consumes<TrajectorySeedCollection>(iConfig.getParameter<InputTag>("trajectories"));
176  if (doPixel_) pixelClusters_ = consumes<edmNew::DetSetVector<SiPixelCluster> >(iConfig.getParameter<InputTag>("pixelClusters"));
177  if (doStrip_) stripClusters_ = consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<InputTag>("stripClusters"));
178  if (mergeOld_) {
179  oldRemovalInfo_ = consumes<ClusterRemovalInfo>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
180  oldPxlMaskToken_ = consumes<PixelMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
181  oldStrMaskToken_ = consumes<StripMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
182  }
183 }
184 
185 
187 {
188 }
189 
191  const ClusterRemovalInfo::Indices &oldRefs)
192 {
193  for (size_t i = 0, n = refs.size(); i < n; ++i) {
194  refs[i] = oldRefs[refs[i]];
195  }
196 }
197 
198 
199 template<typename T>
200 std::unique_ptr<edmNew::DetSetVector<T> >
201 SeedClusterRemover::cleanup(const edmNew::DetSetVector<T> &oldClusters, const std::vector<uint8_t> &isGood,
203  typedef typename edmNew::DetSetVector<T> DSV;
204  typedef typename edmNew::DetSetVector<T>::FastFiller DSF;
205  typedef typename edmNew::DetSet<T> DS;
206  auto output = std::make_unique<DSV>();
207  output->reserve(oldClusters.size(), oldClusters.dataSize());
208 
209  unsigned int countOld=0;
210  unsigned int countNew=0;
211 
212  // cluster removal loop
213  const T * firstOffset = & oldClusters.data().front();
214  for (typename DSV::const_iterator itdet = oldClusters.begin(), enddet = oldClusters.end(); itdet != enddet; ++itdet) {
215  DS oldDS = *itdet;
216 
217  if (oldDS.empty()) continue; // skip empty detsets
218 
219  uint32_t id = oldDS.detId();
220  DSF outds(*output, id);
221 
222  for (typename DS::const_iterator it = oldDS.begin(), ed = oldDS.end(); it != ed; ++it) {
223  uint32_t index = ((&*it) - firstOffset);
224  countOld++;
225  if (isGood[index]) {
226  outds.push_back(*it);
227  countNew++;
228  refs.push_back(index);
229  //std::cout << "SeedClusterRemover::cleanup " << typeid(T).name() << " reference " << index << " to " << (refs.size() - 1) << std::endl;
230  }
231 
232  }
233  if (outds.empty()) outds.abort(); // not write in an empty DSV
234  }
235 
236  if (oldRefs != nullptr) mergeOld(refs, *oldRefs);
237  return output;
238 }
239 
240 
241 void SeedClusterRemover::process(OmniClusterRef const & ocluster, uint32_t subdet) {
242  SiStripRecHit2D::ClusterRef cluster = ocluster.cluster_strip();
243  if (cluster.id() != stripSourceProdID) throw cms::Exception("Inconsistent Data") <<
244  "SeedClusterRemover: strip cluster ref from Product ID = " << cluster.id() <<
245  " does not match with source cluster collection (ID = " << stripSourceProdID << ")\n.";
246 
247  assert(cluster.id() == stripSourceProdID);
248  if (pblocks_[subdet-1].usesSize_ && (cluster->amplitudes().size() > pblocks_[subdet-1].maxSize_)) return;
249 
250  strips[cluster.key()] = false;
251  //if (!clusterWasteSolution_) collectedStrip[hit->geographicalId()].insert(cluster);
252  assert(collectedStrips_.size() > cluster.key());
253  //assert(hit->geographicalId() == cluster->geographicalId()); //This condition fails
254  if (!clusterWasteSolution_) collectedStrips_[cluster.key()]=true;
255 }
256 
257 
259  DetId detid = hit->geographicalId();
260  uint32_t subdet = detid.subdetId();
261 
262  assert ((subdet > 0) && (subdet <= NumberOfParamBlocks));
263 
264  // chi2 cut
265  if (chi2 > pblocks_[subdet-1].maxChi2_) return;
266 
268  if (!doPixel_) return;
269  // this is a pixel, and i *know* it is
270  const SiPixelRecHit *pixelHit = static_cast<const SiPixelRecHit *>(hit);
271 
272  SiPixelRecHit::ClusterRef cluster = pixelHit->cluster();
273 
274  if (cluster.id() != pixelSourceProdID) throw cms::Exception("Inconsistent Data") <<
275  "SeedClusterRemover: pixel cluster ref from Product ID = " << cluster.id() <<
276  " does not match with source cluster collection (ID = " << pixelSourceProdID << ")\n.";
277 
278  assert(cluster.id() == pixelSourceProdID);
279 //DBG// cout << "HIT NEW PIXEL DETID = " << detid.rawId() << ", Cluster [ " << cluster.key().first << " / " << cluster.key().second << " ] " << endl;
280 
281  // if requested, cut on cluster size
282  if (pblocks_[subdet-1].usesSize_ && (cluster->pixels().size() > pblocks_[subdet-1].maxSize_)) return;
283 
284  // mark as used
285  pixels[cluster.key()] = false;
286 
287  //if(!clusterWasteSolution_) collectedPixel[detid.rawId()].insert(cluster);
288  assert(collectedPixels_.size() > cluster.key());
289  //assert(detid.rawId() == cluster->geographicalId()); //This condition fails
290  if(!clusterWasteSolution_) collectedPixels_[cluster.key()]=true;
291 
292  } else { // aka Strip
293  if (!doStrip_) return;
294  const type_info &hitType = typeid(*hit);
295  if (hitType == typeid(SiStripRecHit2D)) {
296  const SiStripRecHit2D *stripHit = static_cast<const SiStripRecHit2D *>(hit);
297 //DBG// cout << "Plain RecHit 2D: " << endl;
298  process(stripHit->omniClusterRef(),subdet);}
299  else if (hitType == typeid(SiStripRecHit1D)) {
300  const SiStripRecHit1D *hit1D = static_cast<const SiStripRecHit1D *>(hit);
301  process(hit1D->omniClusterRef(),subdet);
302  } else if (hitType == typeid(SiStripMatchedRecHit2D)) {
303  const SiStripMatchedRecHit2D *matchHit = static_cast<const SiStripMatchedRecHit2D *>(hit);
304 //DBG// cout << "Matched RecHit 2D: " << endl;
305  process(matchHit->monoClusterRef(),subdet);
306  process(matchHit->stereoClusterRef(),subdet);
307  } else if (hitType == typeid(ProjectedSiStripRecHit2D)) {
308  const ProjectedSiStripRecHit2D *projHit = static_cast<const ProjectedSiStripRecHit2D *>(hit);
309 //DBG// cout << "Projected RecHit 2D: " << endl;
310  process(projHit->originalHit().omniClusterRef(),subdet);
311  } else throw cms::Exception("NOT IMPLEMENTED") << "Don't know how to handle " << hitType.name() << " on detid " << detid.rawId() << "\n";
312  }
313 }
314 
315 /* Schematic picture of n-th step Iterative removal
316  * (that os removing clusters after n-th step tracking)
317  * clusters: [ C1 ] -> [ C2 ] -> ... -> [ Cn ] -> [ Cn + 1 ]
318  * ^ ^ ^--- OUTPUT "new" ID
319  * |-- before any removal |----- Source clusters
320  * |-- OUTPUT "old" ID |----- Hits in Traj. point here
321  * | \----- Old ClusterRemovalInfo "new" ID
322  * \-- Old ClusterRemovalInfo "old" ID
323  */
324 
325 
326 void
328 {
329  ProductID pixelOldProdID, stripOldProdID;
330 
332  iSetup.get<TrackerDigiGeometryRecord>().get("",tgh); //is it correct to use "" ?
333 
335  if (doPixel_) {
336  iEvent.getByToken(pixelClusters_, pixelClusters);
337  pixelSourceProdID = pixelClusters.id();
338  }
339 //DBG// std::cout << "SeedClusterRemover: Read pixel " << pixelClusters_.encode() << " = ID " << pixelSourceProdID << std::endl;
340 
342  if (doStrip_) {
343  iEvent.getByToken(stripClusters_, stripClusters);
344  stripSourceProdID = stripClusters.id();
345  }
346 //DBG// std::cout << "SeedClusterRemover: Read strip " << stripClusters_.encode() << " = ID " << stripSourceProdID << std::endl;
347 
348  std::unique_ptr<ClusterRemovalInfo> cri;
350  if (doStrip_ && doPixel_) cri = std::make_unique<ClusterRemovalInfo>(pixelClusters, stripClusters);
351  else if (doStrip_) cri = std::make_unique<ClusterRemovalInfo>(stripClusters);
352  else if (doPixel_) cri = std::make_unique<ClusterRemovalInfo>(pixelClusters);
353  }
354 
355  Handle<ClusterRemovalInfo> oldRemovalInfo;
357  iEvent.getByToken(oldRemovalInfo_, oldRemovalInfo);
358  // Check ProductIDs
359  if ( (oldRemovalInfo->stripNewRefProd().id() == stripClusters.id()) &&
360  (oldRemovalInfo->pixelNewRefProd().id() == pixelClusters.id()) ) {
361 
362  cri->getOldClustersFrom(*oldRemovalInfo);
363 
364  pixelOldProdID = oldRemovalInfo->pixelRefProd().id();
365  stripOldProdID = oldRemovalInfo->stripRefProd().id();
366 
367  } else {
370  throw cms::Exception("Inconsistent Data") << "SeedClusterRemover: " <<
371  "Input collection product IDs are [pixel: " << pixelClusters.id() << ", strip: " << stripClusters.id() << "] \n" <<
372  "\t but the *old* ClusterRemovalInfo " << labels.productInstance << " refers as 'new product ids' to " <<
373  "[pixel: " << oldRemovalInfo->pixelNewRefProd().id() << ", strip: " << oldRemovalInfo->stripNewRefProd().id() << "]\n" <<
374  "NOTA BENE: when running SeedClusterRemover with an old ClusterRemovalInfo the hits in the trajectory MUST be already re-keyed.\n";
375  }
376  } else { // then Old == Source
377  pixelOldProdID = pixelSourceProdID;
378  stripOldProdID = stripSourceProdID;
379  }
380 
381  if (doStrip_) {
382  strips.resize(stripClusters->dataSize()); fill(strips.begin(), strips.end(), true);
383  }
384  if (doPixel_) {
385  pixels.resize(pixelClusters->dataSize()); fill(pixels.begin(), pixels.end(), true);
386  }
387  if(mergeOld_) {
390  iEvent.getByToken(oldPxlMaskToken_ ,oldPxlMask);
391  iEvent.getByToken(oldStrMaskToken_ ,oldStrMask);
392  LogDebug("SeedClusterRemover")<<"to merge in, "<<oldStrMask->size()<<" strp and "<<oldPxlMask->size()<<" pxl";
393  oldStrMask->copyMaskTo(collectedStrips_);
394  oldPxlMask->copyMaskTo(collectedPixels_);
395  assert(stripClusters->dataSize()>=collectedStrips_.size());
396  collectedStrips_.resize(stripClusters->dataSize(),false); // for ondemand
397  }else {
398  collectedStrips_.resize(stripClusters->dataSize(), false);
399  collectedPixels_.resize(pixelClusters->dataSize(), false);
400  }
401 
402 
404  iEvent.getByToken(trajectories_,seeds);
405 
406  TrajectorySeedCollection::const_iterator seed=seeds->begin();
407 
408  for (;seed!=seeds->end();++seed){
409  TrajectorySeed::range hits=seed->recHits();
411  for (;hit!=hits.second;++hit){
412  if (!hit->isValid()) continue;
413  process( &(*hit), 0. , tgh.product());
414  }
415  }
416 
417 
420  iEvent.put(cleanup(*pixelClusters, pixels, cri->pixelIndices(), mergeOld_ ? &oldRemovalInfo->pixelIndices() : nullptr));
421 //DBG// std::cout << "SeedClusterRemover: Wrote pixel " << newPixels.id() << " from " << pixelSourceProdID << std::endl;
422  cri->setNewPixelClusters(newPixels);
423  }
426  iEvent.put(cleanup(*stripClusters, strips, cri->stripIndices(), mergeOld_ ? &oldRemovalInfo->stripIndices() : nullptr));
427 //DBG// std::cout << "SeedClusterRemover: Wrote strip " << newStrips.id() << " from " << stripSourceProdID << std::endl;
428  cri->setNewStripClusters(newStrips);
429  }
430 
431 
432  if (clusterWasteSolution_) {
433  // double fraction_pxl= cri->pixelIndices().size() / (double) pixels.size();
434  // double fraction_strp= cri->stripIndices().size() / (double) strips.size();
435  // edm::LogWarning("SeedClusterRemover")<<" fraction: " << fraction_pxl <<" "<<fraction_strp;
436  iEvent.put(std::move(cri));
437  }
438 
439  pixels.clear(); strips.clear();
440 
441  if (!clusterWasteSolution_){
442  //auto_ptr<edmNew::DetSetVector<SiPixelClusterRefNew> > removedPixelClsuterRefs(new edmNew::DetSetVector<SiPixelClusterRefNew>());
443  //auto_ptr<edmNew::DetSetVector<SiStripRecHit1D::ClusterRef> > removedStripClsuterRefs(new edmNew::DetSetVector<SiStripRecHit1D::ClusterRef>());
444 
445  LogDebug("SeedClusterRemover")<<"total strip to skip: "<<std::count(collectedStrips_.begin(),collectedStrips_.end(),true);
446  iEvent.put(std::make_unique<StripMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiStripCluster> >(stripClusters),collectedStrips_));
447 
448  LogDebug("SeedClusterRemover")<<"total pxl to skip: "<<std::count(collectedPixels_.begin(),collectedPixels_.end(),true);
449  iEvent.put(std::make_unique<PixelMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiPixelCluster> >(pixelClusters),collectedPixels_));
450 
451  }
452  collectedStrips_.clear();
453  collectedPixels_.clear();
454 
455 }
456 
#define LogDebug(id)
edm::EDGetTokenT< reco::ClusterRemovalInfo > oldRemovalInfo_
T getParameter(std::string const &) const
const GeomDetEnumerators::SubDetector geomDetSubDetector(int subdet) const
const_iterator end(bool update=false) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
size_type dataSize() const
ProductID id() const
Definition: HandleBase.cc:15
static const unsigned int NumberOfParamBlocks
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
TrackQuality
track quality
Definition: TrackBase.h:151
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
OmniClusterRef const & stereoClusterRef() const
SeedClusterRemover(const edm::ParameterSet &iConfig)
bool exists(std::string const &parameterName) const
checks if a parameter exists
key_type key() const
Accessor for product key.
Definition: Ref.h:265
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void copyMaskTo(std::vector< bool > &) const
Definition: ContainerMask.h:90
ProductID id() const
Accessor for product ID.
Definition: Ref.h:259
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
int iEvent
Definition: GenABIO.cc:230
std::vector< bool > collectedStrips_
edm::ProductID pixelSourceProdID
recHitContainer::const_iterator const_iterator
void readPSet(const edm::ParameterSet &iConfig, const std::string &name, int id1=-1, int id2=-1, int id3=-1, int id4=-1, int id5=-1, int id6=-1)
ClusterStripRef cluster_strip() const
ParamBlock pblocks_[NumberOfParamBlocks]
data_type const * data(size_t cell) const
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > stripClusters_
std::pair< const_iterator, const_iterator > range
edm::ContainerMask< edmNew::DetSetVector< SiStripCluster > > StripMaskContainer
edm::ContainerMask< edmNew::DetSetVector< SiPixelCluster > > PixelMaskContainer
size_t size() const
Definition: ContainerMask.h:54
void mergeOld(reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices &oldRefs)
OmniClusterRef const & monoClusterRef() const
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
std::vector< uint32_t > Indices
edm::EDGetTokenT< TrajectorySeedCollection > trajectories_
edm::ProductID stripSourceProdID
SiStripRecHit2D originalHit() const
Definition: DetId.h:18
OmniClusterRef const & omniClusterRef() const
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:125
ParamBlock(const edm::ParameterSet &iConfig)
ClusterRef cluster() const
Definition: SiPixelRecHit.h:49
const T & get() const
Definition: EventSetup.h:55
void process(const TrackingRecHit *hit, float chi2, const TrackerGeometry *tg)
void labelsForToken(EDGetToken iToken, Labels &oLabels) const
size_type size() const
char const * productInstance
Definition: ProductLabels.h:6
fixed size matrix
HLT enums.
std::vector< uint8_t > strips
edm::EDGetTokenT< StripMaskContainer > oldStrMaskToken_
edm::EDGetTokenT< edmNew::DetSetVector< SiPixelCluster > > pixelClusters_
edm::EDGetTokenT< PixelMaskContainer > oldPxlMaskToken_
std::vector< bool > collectedPixels_
std::vector< uint8_t > pixels
DetId geographicalId() const
std::unique_ptr< edmNew::DetSetVector< T > > cleanup(const edmNew::DetSetVector< T > &oldClusters, const std::vector< uint8_t > &isGood, reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices *oldRefs)
ProductIndex id() const
Definition: ProductID.h:38
reco::TrackBase::TrackQuality trackQuality_
long double T
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510
bool isTrackerPixel(GeomDetEnumerators::SubDetector m)
const_iterator begin(bool update=false) const
Our base class.
Definition: SiPixelRecHit.h:23