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