Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-22 07:34:25

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/stream/EDProducer.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0009 
0010 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0011 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0012 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
0013 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
0014 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
0015 #include "DataFormats/TrackerRecHit2D/interface/ProjectedSiStripRecHit2D.h"
0016 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
0017 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
0018 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
0019 #include "DataFormats/Common/interface/Handle.h"
0020 #include "DataFormats/Common/interface/DetSetVector.h"
0021 #include "DataFormats/Common/interface/ValueMap.h"
0022 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0023 #include "DataFormats/Provenance/interface/ProductID.h"
0024 #include "DataFormats/Common/interface/ContainerMask.h"
0025 
0026 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0027 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
0028 
0029 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0030 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0031 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0032 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
0033 
0034 //
0035 // class decleration
0036 //
0037 
0038 class SeedClusterRemover : public edm::stream::EDProducer<> {
0039 public:
0040   SeedClusterRemover(const edm::ParameterSet &iConfig);
0041   void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0042   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0043 
0044 private:
0045   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> const tTrackerGeom_;
0046   struct ParamBlock {
0047     ParamBlock() : isSet_(false), usesCharge_(false) {}
0048     ParamBlock(const edm::ParameterSet &iConfig)
0049         : isSet_(true),
0050           maxChi2_(iConfig.getParameter<double>("maxChi2")),
0051           maxCharge_(iConfig.getParameter<double>("maxCharge")),
0052           maxSize_(iConfig.getParameter<uint32_t>("maxSize")),
0053           usesCharge_(maxCharge_ > 0.),
0054           usesSize_(maxSize_ > 0.) {}
0055     bool isSet_;
0056     float maxChi2_, maxCharge_;
0057     size_t maxSize_;
0058     bool usesCharge_, usesSize_;
0059 
0060     static void fillPSetDescription(edm::ParameterSetDescription &desc) {
0061       desc.addOptional<double>("maxChi2")->setComment("Maximum chi2 value for the cluster.");
0062       desc.add<double>("maxCharge", 0.)->setComment("Maximum charge for the cluster.");
0063       desc.add<uint32_t>("maxSize", 0.)->setComment("Maximum size of the cluster.");
0064     }
0065   };
0066   static const unsigned int NumberOfParamBlocks = 6;
0067 
0068   bool doStrip_, doPixel_;
0069   bool mergeOld_;
0070 
0071   typedef edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > PixelMaskContainer;
0072   typedef edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > StripMaskContainer;
0073   edm::EDGetTokenT<edmNew::DetSetVector<SiPixelCluster> > pixelClusters_;
0074   edm::EDGetTokenT<edmNew::DetSetVector<SiStripCluster> > stripClusters_;
0075   edm::EDGetTokenT<PixelMaskContainer> oldPxlMaskToken_;
0076   edm::EDGetTokenT<StripMaskContainer> oldStrMaskToken_;
0077   edm::EDGetTokenT<TrajectorySeedCollection> trajectories_;
0078 
0079   ParamBlock pblocks_[NumberOfParamBlocks];
0080   void readPSet(const edm::ParameterSet &iConfig,
0081                 const std::string &name,
0082                 int id1 = -1,
0083                 int id2 = -1,
0084                 int id3 = -1,
0085                 int id4 = -1,
0086                 int id5 = -1,
0087                 int id6 = -1);
0088 
0089   std::vector<uint8_t> pixels, strips;                  // avoid unneed alloc/dealloc of this
0090   edm::ProductID pixelSourceProdID, stripSourceProdID;  // ProdIDs refs must point to (for consistency tests)
0091 
0092   inline void process(const TrackingRecHit *hit, float chi2, const TrackerGeometry *tg);
0093   inline void process(const OmniClusterRef &cluRef, uint32_t subdet);
0094 
0095   std::vector<bool> collectedStrips_;
0096   std::vector<bool> collectedPixels_;
0097 };
0098 
0099 using namespace std;
0100 using namespace edm;
0101 
0102 void SeedClusterRemover::readPSet(
0103     const edm::ParameterSet &iConfig, const std::string &name, int id1, int id2, int id3, int id4, int id5, int id6) {
0104   if (iConfig.exists(name)) {
0105     ParamBlock pblock(iConfig.getParameter<ParameterSet>(name));
0106     if (id1 == -1) {
0107       fill(pblocks_, pblocks_ + NumberOfParamBlocks, pblock);
0108     } else {
0109       pblocks_[id1] = pblock;
0110       if (id2 != -1)
0111         pblocks_[id2] = pblock;
0112       if (id3 != -1)
0113         pblocks_[id3] = pblock;
0114       if (id4 != -1)
0115         pblocks_[id4] = pblock;
0116       if (id5 != -1)
0117         pblocks_[id5] = pblock;
0118       if (id6 != -1)
0119         pblocks_[id6] = pblock;
0120     }
0121   }
0122 }
0123 
0124 SeedClusterRemover::SeedClusterRemover(const ParameterSet &iConfig)
0125     : tTrackerGeom_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>()),
0126       doStrip_(iConfig.getParameter<bool>("doStrip")),
0127       doPixel_(iConfig.getParameter<bool>("doPixel")),
0128       mergeOld_(iConfig.exists("oldClusterRemovalInfo")) {
0129   fill(pblocks_, pblocks_ + NumberOfParamBlocks, ParamBlock());
0130   readPSet(iConfig, "Common", -1);
0131   if (doPixel_) {
0132     readPSet(iConfig, "Pixel", 0, 1);
0133     readPSet(iConfig, "PXB", 0);
0134     readPSet(iConfig, "PXE", 1);
0135   }
0136   if (doStrip_) {
0137     readPSet(iConfig, "Strip", 2, 3, 4, 5);
0138     readPSet(iConfig, "StripInner", 2, 3);
0139     readPSet(iConfig, "StripOuter", 4, 5);
0140     readPSet(iConfig, "TIB", 2);
0141     readPSet(iConfig, "TID", 3);
0142     readPSet(iConfig, "TOB", 4);
0143     readPSet(iConfig, "TEC", 5);
0144   }
0145 
0146   bool usingCharge = false;
0147   for (size_t i = 0; i < NumberOfParamBlocks; ++i) {
0148     if (!pblocks_[i].isSet_)
0149       throw cms::Exception("Configuration Error")
0150           << "SeedClusterRemover: Missing configuration for detector with subDetID = " << (i + 1);
0151     if (pblocks_[i].usesCharge_ && !usingCharge) {
0152       throw cms::Exception("Configuration Error") << "SeedClusterRemover: Configuration for subDetID = " << (i + 1)
0153                                                   << " uses cluster charge, which is not enabled.";
0154     }
0155   }
0156 
0157   trajectories_ = consumes<TrajectorySeedCollection>(iConfig.getParameter<InputTag>("trajectories"));
0158   if (doPixel_)
0159     pixelClusters_ = consumes<edmNew::DetSetVector<SiPixelCluster> >(iConfig.getParameter<InputTag>("pixelClusters"));
0160   if (doStrip_)
0161     stripClusters_ = consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<InputTag>("stripClusters"));
0162   if (mergeOld_) {
0163     oldPxlMaskToken_ = consumes<PixelMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0164     oldStrMaskToken_ = consumes<StripMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0165   }
0166 
0167   produces<edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > >();
0168   produces<edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > >();
0169 }
0170 
0171 void SeedClusterRemover::process(OmniClusterRef const &ocluster, uint32_t subdet) {
0172   SiStripRecHit2D::ClusterRef cluster = ocluster.cluster_strip();
0173   if (cluster.id() != stripSourceProdID)
0174     throw cms::Exception("Inconsistent Data")
0175         << "SeedClusterRemover: strip cluster ref from Product ID = " << cluster.id()
0176         << " does not match with source cluster collection (ID = " << stripSourceProdID << ")\n.";
0177 
0178   assert(cluster.id() == stripSourceProdID);
0179   if (pblocks_[subdet - 1].usesSize_ && (cluster->amplitudes().size() > pblocks_[subdet - 1].maxSize_))
0180     return;
0181 
0182   strips[cluster.key()] = false;
0183   assert(collectedStrips_.size() > cluster.key());
0184   collectedStrips_[cluster.key()] = true;
0185 }
0186 
0187 void SeedClusterRemover::process(const TrackingRecHit *hit, float chi2, const TrackerGeometry *tg) {
0188   DetId detid = hit->geographicalId();
0189   uint32_t subdet = detid.subdetId();
0190 
0191   assert((subdet > 0) && (subdet <= NumberOfParamBlocks));
0192 
0193   // chi2 cut
0194   if (chi2 > pblocks_[subdet - 1].maxChi2_)
0195     return;
0196 
0197   if (GeomDetEnumerators::isTrackerPixel(tg->geomDetSubDetector(subdet))) {
0198     if (!doPixel_)
0199       return;
0200     // this is a pixel, and i *know* it is
0201     const SiPixelRecHit *pixelHit = static_cast<const SiPixelRecHit *>(hit);
0202 
0203     SiPixelRecHit::ClusterRef cluster = pixelHit->cluster();
0204 
0205     if (cluster.id() != pixelSourceProdID)
0206       throw cms::Exception("Inconsistent Data")
0207           << "SeedClusterRemover: pixel cluster ref from Product ID = " << cluster.id()
0208           << " does not match with source cluster collection (ID = " << pixelSourceProdID << ")\n.";
0209 
0210     assert(cluster.id() == pixelSourceProdID);
0211     //DBG// cout << "HIT NEW PIXEL DETID = " << detid.rawId() << ", Cluster [ " << cluster.key().first << " / " <<  cluster.key().second << " ] " << endl;
0212 
0213     // if requested, cut on cluster size
0214     if (pblocks_[subdet - 1].usesSize_ && (cluster->pixels().size() > pblocks_[subdet - 1].maxSize_))
0215       return;
0216 
0217     // mark as used
0218     pixels[cluster.key()] = false;
0219 
0220     assert(collectedPixels_.size() > cluster.key());
0221     collectedPixels_[cluster.key()] = true;
0222 
0223   } else {  // aka Strip
0224     if (!doStrip_)
0225       return;
0226     const type_info &hitType = typeid(*hit);
0227     if (hitType == typeid(SiStripRecHit2D)) {
0228       const SiStripRecHit2D *stripHit = static_cast<const SiStripRecHit2D *>(hit);
0229       //DBG//     cout << "Plain RecHit 2D: " << endl;
0230       process(stripHit->omniClusterRef(), subdet);
0231     } else if (hitType == typeid(SiStripRecHit1D)) {
0232       const SiStripRecHit1D *hit1D = static_cast<const SiStripRecHit1D *>(hit);
0233       process(hit1D->omniClusterRef(), subdet);
0234     } else if (hitType == typeid(SiStripMatchedRecHit2D)) {
0235       const SiStripMatchedRecHit2D *matchHit = static_cast<const SiStripMatchedRecHit2D *>(hit);
0236       //DBG//     cout << "Matched RecHit 2D: " << endl;
0237       process(matchHit->monoClusterRef(), subdet);
0238       process(matchHit->stereoClusterRef(), subdet);
0239     } else if (hitType == typeid(ProjectedSiStripRecHit2D)) {
0240       const ProjectedSiStripRecHit2D *projHit = static_cast<const ProjectedSiStripRecHit2D *>(hit);
0241       //DBG//     cout << "Projected RecHit 2D: " << endl;
0242       process(projHit->originalHit().omniClusterRef(), subdet);
0243     } else
0244       throw cms::Exception("NOT IMPLEMENTED")
0245           << "Don't know how to handle " << hitType.name() << " on detid " << detid.rawId() << "\n";
0246   }
0247 }
0248 
0249 void SeedClusterRemover::produce(Event &iEvent, const EventSetup &iSetup) {
0250   const auto &tgh = &iSetup.getData(tTrackerGeom_);
0251 
0252   Handle<edmNew::DetSetVector<SiPixelCluster> > pixelClusters;
0253   if (doPixel_) {
0254     iEvent.getByToken(pixelClusters_, pixelClusters);
0255     pixelSourceProdID = pixelClusters.id();
0256   }
0257   //DBG// std::cout << "SeedClusterRemover: Read pixel " << pixelClusters_.encode() << " = ID " << pixelSourceProdID << std::endl;
0258 
0259   Handle<edmNew::DetSetVector<SiStripCluster> > stripClusters;
0260   if (doStrip_) {
0261     iEvent.getByToken(stripClusters_, stripClusters);
0262     stripSourceProdID = stripClusters.id();
0263   }
0264   //DBG// std::cout << "SeedClusterRemover: Read strip " << stripClusters_.encode() << " = ID " << stripSourceProdID << std::endl;
0265 
0266   if (doStrip_) {
0267     strips.resize(stripClusters->dataSize());
0268     fill(strips.begin(), strips.end(), true);
0269   }
0270   if (doPixel_) {
0271     pixels.resize(pixelClusters->dataSize());
0272     fill(pixels.begin(), pixels.end(), true);
0273   }
0274   if (mergeOld_) {
0275     edm::Handle<PixelMaskContainer> oldPxlMask;
0276     edm::Handle<StripMaskContainer> oldStrMask;
0277     iEvent.getByToken(oldPxlMaskToken_, oldPxlMask);
0278     iEvent.getByToken(oldStrMaskToken_, oldStrMask);
0279     LogDebug("SeedClusterRemover") << "to merge in, " << oldStrMask->size() << " strp and " << oldPxlMask->size()
0280                                    << " pxl";
0281     oldStrMask->copyMaskTo(collectedStrips_);
0282     oldPxlMask->copyMaskTo(collectedPixels_);
0283     assert(stripClusters->dataSize() >= collectedStrips_.size());
0284     collectedStrips_.resize(stripClusters->dataSize(), false);  // for ondemand
0285   } else {
0286     collectedStrips_.resize(stripClusters->dataSize(), false);
0287     collectedPixels_.resize(pixelClusters->dataSize(), false);
0288   }
0289 
0290   edm::Handle<TrajectorySeedCollection> seeds;
0291   iEvent.getByToken(trajectories_, seeds);
0292 
0293   for (auto const &seed : (*seeds)) {
0294     for (auto const &hit : seed.recHits()) {
0295       if (!hit.isValid())
0296         continue;
0297       process(&hit, 0., tgh);
0298     }
0299   }
0300 
0301   pixels.clear();
0302   strips.clear();
0303 
0304   LogDebug("SeedClusterRemover") << "total strip to skip: "
0305                                  << std::count(collectedStrips_.begin(), collectedStrips_.end(), true);
0306   iEvent.put(std::make_unique<StripMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiStripCluster> >(stripClusters),
0307                                                   collectedStrips_));
0308 
0309   LogDebug("SeedClusterRemover") << "total pxl to skip: "
0310                                  << std::count(collectedPixels_.begin(), collectedPixels_.end(), true);
0311   iEvent.put(std::make_unique<PixelMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiPixelCluster> >(pixelClusters),
0312                                                   collectedPixels_));
0313 
0314   collectedStrips_.clear();
0315   collectedPixels_.clear();
0316 }
0317 
0318 void SeedClusterRemover::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0319   edm::ParameterSetDescription desc;
0320 
0321   // Define the structure for optional ParameterSets like Common, Pixel, Strip, etc.
0322   edm::ParameterSetDescription paramBlockDesc;
0323   ParamBlock::fillPSetDescription(paramBlockDesc);
0324 
0325   // Add all possible ParameterSets (optional) using the above structure
0326   desc.addOptional<edm::ParameterSetDescription>("Common", paramBlockDesc);
0327   desc.addOptional<edm::ParameterSetDescription>("Pixel", paramBlockDesc);
0328   desc.addOptional<edm::ParameterSetDescription>("Strip", paramBlockDesc);
0329   desc.addOptional<edm::ParameterSetDescription>("PXB", paramBlockDesc);
0330   desc.addOptional<edm::ParameterSetDescription>("PXE", paramBlockDesc);
0331   desc.addOptional<edm::ParameterSetDescription>("StripInner", paramBlockDesc);
0332   desc.addOptional<edm::ParameterSetDescription>("StripOuter", paramBlockDesc);
0333   desc.addOptional<edm::ParameterSetDescription>("TIB", paramBlockDesc);
0334   desc.addOptional<edm::ParameterSetDescription>("TID", paramBlockDesc);
0335   desc.addOptional<edm::ParameterSetDescription>("TOB", paramBlockDesc);
0336   desc.addOptional<edm::ParameterSetDescription>("TEC", paramBlockDesc);
0337 
0338   desc.add<bool>("doStrip", true);
0339   desc.add<bool>("doPixel", true);
0340   desc.add<InputTag>("trajectories");
0341   desc.add<InputTag>("pixelClusters");
0342   desc.add<InputTag>("stripClusters");
0343   desc.add<InputTag>("oldClusterRemovalInfo");
0344   descriptions.addDefault(desc);
0345 }
0346 
0347 #include "FWCore/PluginManager/interface/ModuleDef.h"
0348 #include "FWCore/Framework/interface/MakerMacros.h"
0349 DEFINE_FWK_MODULE(SeedClusterRemover);