00001 #include "FWCore/Framework/interface/Frameworkfwd.h"
00002 #include "FWCore/Framework/interface/EDProducer.h"
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/Utilities/interface/InputTag.h"
00006
00007 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
00008 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
00009 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
00010 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
00011 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
00012 #include "DataFormats/TrackerRecHit2D/interface/ProjectedSiStripRecHit2D.h"
00013 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
00014 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
00015 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
00016 #include "DataFormats/Common/interface/Handle.h"
00017 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00018 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00019 #include "DataFormats/Common/interface/DetSetVector.h"
00020 #include "DataFormats/Common/interface/ValueMap.h"
00021 #include "DataFormats/Common/interface/DetSetVectorNew.h"
00022 #include "DataFormats/Provenance/interface/ProductID.h"
00023 #include "DataFormats/Common/interface/ContainerMask.h"
00024
00025 #include "DataFormats/TrackReco/interface/Track.h"
00026 #include "DataFormats/TrackerRecHit2D/interface/ClusterRemovalInfo.h"
00027
00028 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00029 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00030 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00031 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00032
00033
00034
00035
00036
00037
00038 class SeedClusterRemover : public edm::EDProducer {
00039 public:
00040 SeedClusterRemover(const edm::ParameterSet& iConfig) ;
00041 ~SeedClusterRemover() ;
00042 void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) ;
00043 private:
00044 struct ParamBlock {
00045 ParamBlock() : isSet_(false), usesCharge_(false) {}
00046 ParamBlock(const edm::ParameterSet& iConfig) :
00047 isSet_(true),
00048 usesCharge_(iConfig.exists("maxCharge")),
00049 usesSize_(iConfig.exists("maxSize")),
00050 maxChi2_(iConfig.getParameter<double>("maxChi2")),
00051 maxCharge_(usesCharge_ ? iConfig.getParameter<double>("maxCharge") : 0),
00052 maxSize_(usesSize_ ? iConfig.getParameter<uint32_t>("maxSize") : 0) { }
00053 bool isSet_, usesCharge_, usesSize_;
00054 float maxChi2_, maxCharge_;
00055 size_t maxSize_;
00056 };
00057 static const unsigned int NumberOfParamBlocks = 6;
00058
00059 edm::InputTag trajectories_;
00060 std::vector<edm::InputTag> overrideTrkQuals_;
00061 bool doStrip_, doPixel_;
00062 edm::InputTag stripClusters_, pixelClusters_;
00063 bool mergeOld_;
00064 edm::InputTag oldRemovalInfo_;
00065
00066 ParamBlock pblocks_[NumberOfParamBlocks];
00067 void readPSet(const edm::ParameterSet& iConfig, const std::string &name,
00068 int id1=-1, int id2=-1, int id3=-1, int id4=-1, int id5=-1, int id6=-1) ;
00069
00070 std::vector<uint8_t> pixels, strips;
00071 edm::ProductID pixelSourceProdID, stripSourceProdID;
00072
00073 inline void process(const TrackingRecHit *hit, float chi2);
00074 inline void process(const OmniClusterRef & cluRef, uint32_t subdet);
00075
00076
00077 template<typename T>
00078 std::auto_ptr<edmNew::DetSetVector<T> >
00079 cleanup(const edmNew::DetSetVector<T> &oldClusters, const std::vector<uint8_t> &isGood,
00080 reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices *oldRefs) ;
00081
00082
00083 void mergeOld(reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices &oldRefs) ;
00084
00085 bool clusterWasteSolution_;
00086 bool filterTracks_;
00087 reco::TrackBase::TrackQuality trackQuality_;
00088 std::vector<bool> collectedStrips_;
00089 std::vector<bool> collectedPixels_;
00090 };
00091
00092
00093 using namespace std;
00094 using namespace edm;
00095 using namespace reco;
00096
00097 void
00098 SeedClusterRemover::readPSet(const edm::ParameterSet& iConfig, const std::string &name,
00099 int id1, int id2, int id3, int id4, int id5, int id6)
00100 {
00101 if (iConfig.exists(name)) {
00102 ParamBlock pblock(iConfig.getParameter<ParameterSet>(name));
00103 if (id1 == -1) {
00104 fill(pblocks_, pblocks_+NumberOfParamBlocks, pblock);
00105 } else {
00106 pblocks_[id1] = pblock;
00107 if (id2 != -1) pblocks_[id2] = pblock;
00108 if (id3 != -1) pblocks_[id3] = pblock;
00109 if (id4 != -1) pblocks_[id4] = pblock;
00110 if (id5 != -1) pblocks_[id5] = pblock;
00111 if (id6 != -1) pblocks_[id6] = pblock;
00112 }
00113 }
00114 }
00115
00116 SeedClusterRemover::SeedClusterRemover(const ParameterSet& iConfig):
00117 trajectories_(iConfig.getParameter<InputTag>("trajectories")),
00118 doStrip_(iConfig.existsAs<bool>("doStrip") ? iConfig.getParameter<bool>("doStrip") : true),
00119 doPixel_(iConfig.existsAs<bool>("doPixel") ? iConfig.getParameter<bool>("doPixel") : true),
00120 stripClusters_(doStrip_ ? iConfig.getParameter<InputTag>("stripClusters") : InputTag("NONE")),
00121 pixelClusters_(doPixel_ ? iConfig.getParameter<InputTag>("pixelClusters") : InputTag("NONE")),
00122 mergeOld_(iConfig.exists("oldClusterRemovalInfo")),
00123 oldRemovalInfo_(mergeOld_ ? iConfig.getParameter<InputTag>("oldClusterRemovalInfo") : InputTag("NONE")),
00124 clusterWasteSolution_(true)
00125 {
00126 if (iConfig.exists("overrideTrkQuals"))
00127 overrideTrkQuals_.push_back(iConfig.getParameter<edm::InputTag>("overrideTrkQuals"));
00128 if (iConfig.exists("clusterLessSolution"))
00129 clusterWasteSolution_=!iConfig.getParameter<bool>("clusterLessSolution");
00130 if (doPixel_ && clusterWasteSolution_) produces< edmNew::DetSetVector<SiPixelCluster> >();
00131 if (doStrip_ && clusterWasteSolution_) produces< edmNew::DetSetVector<SiStripCluster> >();
00132 if (clusterWasteSolution_) produces< ClusterRemovalInfo >();
00133
00134 fill(pblocks_, pblocks_+NumberOfParamBlocks, ParamBlock());
00135 readPSet(iConfig, "Common",-1);
00136 if (doPixel_) {
00137 readPSet(iConfig, "Pixel" ,0,1);
00138 readPSet(iConfig, "PXB" ,0);
00139 readPSet(iConfig, "PXE" ,1);
00140 }
00141 if (doStrip_) {
00142 readPSet(iConfig, "Strip" ,2,3,4,5);
00143 readPSet(iConfig, "StripInner" ,2,3);
00144 readPSet(iConfig, "StripOuter" ,4,5);
00145 readPSet(iConfig, "TIB" ,2);
00146 readPSet(iConfig, "TID" ,3);
00147 readPSet(iConfig, "TOB" ,4);
00148 readPSet(iConfig, "TEC" ,5);
00149 }
00150
00151 bool usingCharge = false;
00152 for (size_t i = 0; i < NumberOfParamBlocks; ++i) {
00153 if (!pblocks_[i].isSet_) throw cms::Exception("Configuration Error") << "SeedClusterRemover: Missing configuration for detector with subDetID = " << (i+1);
00154 if (pblocks_[i].usesCharge_ && !usingCharge) {
00155 throw cms::Exception("Configuration Error") << "SeedClusterRemover: Configuration for subDetID = " << (i+1) << " uses cluster charge, which is not enabled.";
00156 }
00157 }
00158
00159 if (!clusterWasteSolution_){
00160 produces<edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > >();
00161 produces<edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > >();
00162 }
00163 trackQuality_=reco::TrackBase::undefQuality;
00164 filterTracks_=false;
00165 if (iConfig.exists("TrackQuality")){
00166 filterTracks_=true;
00167 trackQuality_=reco::TrackBase::qualityByName(iConfig.getParameter<std::string>("TrackQuality"));
00168 }
00169
00170 }
00171
00172
00173 SeedClusterRemover::~SeedClusterRemover()
00174 {
00175 }
00176
00177 void SeedClusterRemover::mergeOld(ClusterRemovalInfo::Indices &refs,
00178 const ClusterRemovalInfo::Indices &oldRefs)
00179 {
00180 for (size_t i = 0, n = refs.size(); i < n; ++i) {
00181 refs[i] = oldRefs[refs[i]];
00182 }
00183 }
00184
00185
00186 template<typename T>
00187 auto_ptr<edmNew::DetSetVector<T> >
00188 SeedClusterRemover::cleanup(const edmNew::DetSetVector<T> &oldClusters, const std::vector<uint8_t> &isGood,
00189 reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices *oldRefs){
00190 typedef typename edmNew::DetSetVector<T> DSV;
00191 typedef typename edmNew::DetSetVector<T>::FastFiller DSF;
00192 typedef typename edmNew::DetSet<T> DS;
00193 auto_ptr<DSV> output(new DSV());
00194 output->reserve(oldClusters.size(), oldClusters.dataSize());
00195
00196 unsigned int countOld=0;
00197 unsigned int countNew=0;
00198
00199
00200 const T * firstOffset = & oldClusters.data().front();
00201 for (typename DSV::const_iterator itdet = oldClusters.begin(), enddet = oldClusters.end(); itdet != enddet; ++itdet) {
00202 DS oldDS = *itdet;
00203
00204 if (oldDS.empty()) continue;
00205
00206 uint32_t id = oldDS.detId();
00207 DSF outds(*output, id);
00208
00209 for (typename DS::const_iterator it = oldDS.begin(), ed = oldDS.end(); it != ed; ++it) {
00210 uint32_t index = ((&*it) - firstOffset);
00211 countOld++;
00212 if (isGood[index]) {
00213 outds.push_back(*it);
00214 countNew++;
00215 refs.push_back(index);
00216
00217 }
00218
00219 }
00220 if (outds.empty()) outds.abort();
00221 }
00222
00223 if (oldRefs != 0) mergeOld(refs, *oldRefs);
00224 return output;
00225 }
00226
00227
00228 void SeedClusterRemover::process(OmniClusterRef const & ocluster, uint32_t subdet) {
00229 SiStripRecHit2D::ClusterRef cluster = ocluster.cluster_strip();
00230 if (cluster.id() != stripSourceProdID) throw cms::Exception("Inconsistent Data") <<
00231 "SeedClusterRemover: strip cluster ref from Product ID = " << cluster.id() <<
00232 " does not match with source cluster collection (ID = " << stripSourceProdID << ")\n.";
00233
00234 assert(cluster.id() == stripSourceProdID);
00235 if (pblocks_[subdet-1].usesSize_ && (cluster->amplitudes().size() > pblocks_[subdet-1].maxSize_)) return;
00236
00237 strips[cluster.key()] = false;
00238
00239 assert(collectedStrips_.size() > cluster.key());
00240
00241 if (!clusterWasteSolution_) collectedStrips_[cluster.key()]=true;
00242 }
00243
00244
00245 void SeedClusterRemover::process(const TrackingRecHit *hit, float chi2) {
00246 DetId detid = hit->geographicalId();
00247 uint32_t subdet = detid.subdetId();
00248
00249 assert ((subdet > 0) && (subdet <= NumberOfParamBlocks));
00250
00251
00252 if (chi2 > pblocks_[subdet-1].maxChi2_) return;
00253
00254 if ((subdet == PixelSubdetector::PixelBarrel) || (subdet == PixelSubdetector::PixelEndcap)) {
00255 if (!doPixel_) return;
00256
00257 const SiPixelRecHit *pixelHit = static_cast<const SiPixelRecHit *>(hit);
00258
00259 SiPixelRecHit::ClusterRef cluster = pixelHit->cluster();
00260
00261 if (cluster.id() != pixelSourceProdID) throw cms::Exception("Inconsistent Data") <<
00262 "SeedClusterRemover: pixel cluster ref from Product ID = " << cluster.id() <<
00263 " does not match with source cluster collection (ID = " << pixelSourceProdID << ")\n.";
00264
00265 assert(cluster.id() == pixelSourceProdID);
00266
00267
00268
00269 if (pblocks_[subdet-1].usesSize_ && (cluster->pixels().size() > pblocks_[subdet-1].maxSize_)) return;
00270
00271
00272 pixels[cluster.key()] = false;
00273
00274
00275 assert(collectedPixels_.size() > cluster.key());
00276
00277 if(!clusterWasteSolution_) collectedPixels_[cluster.key()]=true;
00278
00279 } else {
00280 if (!doStrip_) return;
00281 const type_info &hitType = typeid(*hit);
00282 if (hitType == typeid(SiStripRecHit2D)) {
00283 const SiStripRecHit2D *stripHit = static_cast<const SiStripRecHit2D *>(hit);
00284
00285 process(stripHit->omniClusterRef(),subdet);}
00286 else if (hitType == typeid(SiStripRecHit1D)) {
00287 const SiStripRecHit1D *hit1D = static_cast<const SiStripRecHit1D *>(hit);
00288 process(hit1D->omniClusterRef(),subdet);
00289 } else if (hitType == typeid(SiStripMatchedRecHit2D)) {
00290 const SiStripMatchedRecHit2D *matchHit = static_cast<const SiStripMatchedRecHit2D *>(hit);
00291
00292 process(matchHit->monoClusterRef(),subdet);
00293 process(matchHit->stereoClusterRef(),subdet);
00294 } else if (hitType == typeid(ProjectedSiStripRecHit2D)) {
00295 const ProjectedSiStripRecHit2D *projHit = static_cast<const ProjectedSiStripRecHit2D *>(hit);
00296
00297 process(projHit->originalHit().omniClusterRef(),subdet);
00298 } else throw cms::Exception("NOT IMPLEMENTED") << "Don't know how to handle " << hitType.name() << " on detid " << detid.rawId() << "\n";
00299 }
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 void
00314 SeedClusterRemover::produce(Event& iEvent, const EventSetup& iSetup)
00315 {
00316 ProductID pixelOldProdID, stripOldProdID;
00317
00318 Handle<edmNew::DetSetVector<SiPixelCluster> > pixelClusters;
00319 if (doPixel_) {
00320 iEvent.getByLabel(pixelClusters_, pixelClusters);
00321 pixelSourceProdID = pixelClusters.id();
00322 }
00323
00324
00325 Handle<edmNew::DetSetVector<SiStripCluster> > stripClusters;
00326 if (doStrip_) {
00327 iEvent.getByLabel(stripClusters_, stripClusters);
00328 stripSourceProdID = stripClusters.id();
00329 }
00330
00331
00332 auto_ptr<ClusterRemovalInfo> cri;
00333 if (clusterWasteSolution_){
00334 if (doStrip_ && doPixel_) cri.reset(new ClusterRemovalInfo(pixelClusters, stripClusters));
00335 else if (doStrip_) cri.reset(new ClusterRemovalInfo(stripClusters));
00336 else if (doPixel_) cri.reset(new ClusterRemovalInfo(pixelClusters));
00337 }
00338
00339 Handle<ClusterRemovalInfo> oldRemovalInfo;
00340 if (mergeOld_ && clusterWasteSolution_) {
00341 iEvent.getByLabel(oldRemovalInfo_, oldRemovalInfo);
00342
00343 if ( (oldRemovalInfo->stripNewRefProd().id() == stripClusters.id()) &&
00344 (oldRemovalInfo->pixelNewRefProd().id() == pixelClusters.id()) ) {
00345
00346 cri->getOldClustersFrom(*oldRemovalInfo);
00347
00348 pixelOldProdID = oldRemovalInfo->pixelRefProd().id();
00349 stripOldProdID = oldRemovalInfo->stripRefProd().id();
00350
00351 } else {
00352 throw cms::Exception("Inconsistent Data") << "SeedClusterRemover: " <<
00353 "Input collection product IDs are [pixel: " << pixelClusters.id() << ", strip: " << stripClusters.id() << "] \n" <<
00354 "\t but the *old* ClusterRemovalInfo " << oldRemovalInfo_.encode() << " refers as 'new product ids' to " <<
00355 "[pixel: " << oldRemovalInfo->pixelNewRefProd().id() << ", strip: " << oldRemovalInfo->stripNewRefProd().id() << "]\n" <<
00356 "NOTA BENE: when running SeedClusterRemover with an old ClusterRemovalInfo the hits in the trajectory MUST be already re-keyed.\n";
00357 }
00358 } else {
00359 pixelOldProdID = pixelSourceProdID;
00360 stripOldProdID = stripSourceProdID;
00361 }
00362
00363 if (doStrip_) {
00364 strips.resize(stripClusters->dataSize()); fill(strips.begin(), strips.end(), true);
00365 }
00366 if (doPixel_) {
00367 pixels.resize(pixelClusters->dataSize()); fill(pixels.begin(), pixels.end(), true);
00368 }
00369 typedef edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > PixelMaskContainer;
00370 typedef edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > StripMaskContainer;
00371 if(mergeOld_) {
00372 edm::Handle<PixelMaskContainer> oldPxlMask;
00373 edm::Handle<StripMaskContainer> oldStrMask;
00374 iEvent.getByLabel(oldRemovalInfo_,oldPxlMask);
00375 iEvent.getByLabel(oldRemovalInfo_,oldStrMask);
00376 LogDebug("SeedClusterRemover")<<"to merge in, "<<oldStrMask->size()<<" strp and "<<oldPxlMask->size()<<" pxl";
00377 oldStrMask->copyMaskTo(collectedStrips_);
00378 oldPxlMask->copyMaskTo(collectedPixels_);
00379 }else {
00380 collectedStrips_.resize(stripClusters->dataSize()); fill(collectedStrips_.begin(), collectedStrips_.end(), false);
00381 collectedPixels_.resize(pixelClusters->dataSize()); fill(collectedPixels_.begin(), collectedPixels_.end(), false);
00382 }
00383
00384
00385 edm::Handle<TrajectorySeedCollection> seeds;
00386 iEvent.getByLabel(trajectories_,seeds);
00387
00388 TrajectorySeedCollection::const_iterator seed=seeds->begin();
00389
00390 for (;seed!=seeds->end();++seed){
00391 TrajectorySeed::range hits=seed->recHits();
00392 TrajectorySeed::const_iterator hit=hits.first;
00393 for (;hit!=hits.second;++hit){
00394 if (!hit->isValid()) continue;
00395 process( &(*hit), 0. );
00396 }
00397 }
00398
00399
00400 if (doPixel_ && clusterWasteSolution_) {
00401 auto_ptr<edmNew::DetSetVector<SiPixelCluster> > newPixelClusters = cleanup(*pixelClusters, pixels,
00402 cri->pixelIndices(), mergeOld_ ? &oldRemovalInfo->pixelIndices() : 0);
00403 OrphanHandle<edmNew::DetSetVector<SiPixelCluster> > newPixels = iEvent.put(newPixelClusters);
00404
00405 cri->setNewPixelClusters(newPixels);
00406 }
00407 if (doStrip_ && clusterWasteSolution_) {
00408 auto_ptr<edmNew::DetSetVector<SiStripCluster> > newStripClusters = cleanup(*stripClusters, strips,
00409 cri->stripIndices(), mergeOld_ ? &oldRemovalInfo->stripIndices() : 0);
00410 OrphanHandle<edmNew::DetSetVector<SiStripCluster> > newStrips = iEvent.put(newStripClusters);
00411
00412 cri->setNewStripClusters(newStrips);
00413 }
00414
00415
00416 if (clusterWasteSolution_) {
00417
00418
00419
00420 iEvent.put(cri);
00421 }
00422
00423 pixels.clear(); strips.clear();
00424
00425 if (!clusterWasteSolution_){
00426
00427
00428
00429 std::auto_ptr<StripMaskContainer> removedStripClusterMask(
00430 new StripMaskContainer(edm::RefProd<edmNew::DetSetVector<SiStripCluster> >(stripClusters),collectedStrips_));
00431 LogDebug("SeedClusterRemover")<<"total strip to skip: "<<std::count(collectedStrips_.begin(),collectedStrips_.end(),true);
00432 iEvent.put( removedStripClusterMask );
00433
00434 std::auto_ptr<PixelMaskContainer> removedPixelClusterMask(
00435 new PixelMaskContainer(edm::RefProd<edmNew::DetSetVector<SiPixelCluster> >(pixelClusters),collectedPixels_));
00436 LogDebug("SeedClusterRemover")<<"total pxl to skip: "<<std::count(collectedPixels_.begin(),collectedPixels_.end(),true);
00437 iEvent.put( removedPixelClusterMask );
00438
00439 }
00440
00441
00442 }
00443
00444 #include "FWCore/PluginManager/interface/ModuleDef.h"
00445 #include "FWCore/Framework/interface/MakerMacros.h"
00446 DEFINE_FWK_MODULE(SeedClusterRemover);