CMS 3D CMS Logo

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