File indexing completed on 2024-04-06 12:26:33
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 "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0008 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0009 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
0010 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
0011 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
0012 #include "DataFormats/TrackerRecHit2D/interface/ProjectedSiStripRecHit2D.h"
0013 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
0014 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
0015 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
0016 #include "DataFormats/Common/interface/Handle.h"
0017 #include "DataFormats/Common/interface/DetSetVector.h"
0018 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0019 #include "DataFormats/Common/interface/ContainerMask.h"
0020 #include "DataFormats/Provenance/interface/ProductID.h"
0021
0022 #include "DataFormats/TrackReco/interface/Track.h"
0023 #include "DataFormats/TrackerRecHit2D/interface/ClusterRemovalInfo.h"
0024
0025 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0026 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0027 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.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
0036
0037
0038 class HLTTrackClusterRemoverNew final : public edm::stream::EDProducer<> {
0039 public:
0040 HLTTrackClusterRemoverNew(const edm::ParameterSet &iConfig);
0041 ~HLTTrackClusterRemoverNew() override;
0042 void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
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 usesCharge_(iConfig.exists("maxCharge")),
0051 usesSize_(iConfig.exists("maxSize")),
0052 cutOnPixelCharge_(iConfig.exists("minGoodPixelCharge")),
0053 cutOnStripCharge_(iConfig.exists("minGoodStripCharge")),
0054 maxChi2_(iConfig.getParameter<double>("maxChi2")),
0055 maxCharge_(usesCharge_ ? iConfig.getParameter<double>("maxCharge") : 0),
0056 minGoodPixelCharge_(cutOnPixelCharge_ ? iConfig.getParameter<double>("minGoodPixelCharge") : 0),
0057 minGoodStripCharge_(cutOnStripCharge_ ? iConfig.getParameter<double>("minGoodStripCharge") : 0),
0058 maxSize_(usesSize_ ? iConfig.getParameter<uint32_t>("maxSize") : 0) {}
0059 bool isSet_, usesCharge_, usesSize_, cutOnPixelCharge_, cutOnStripCharge_;
0060 float maxChi2_, maxCharge_, minGoodPixelCharge_, minGoodStripCharge_;
0061 size_t maxSize_;
0062 };
0063 static const unsigned int NumberOfParamBlocks = 6;
0064
0065 bool doTracks_;
0066 bool doStrip_, doPixel_;
0067 bool mergeOld_;
0068
0069 typedef edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > PixelMaskContainer;
0070 typedef edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > StripMaskContainer;
0071 edm::EDGetTokenT<edmNew::DetSetVector<SiPixelCluster> > pixelClusters_;
0072 edm::EDGetTokenT<edmNew::DetSetVector<SiStripCluster> > stripClusters_;
0073 edm::EDGetTokenT<PixelMaskContainer> oldPxlMaskToken_;
0074 edm::EDGetTokenT<StripMaskContainer> oldStrMaskToken_;
0075 edm::EDGetTokenT<std::vector<Trajectory> > trajectories_;
0076
0077 ParamBlock pblocks_[NumberOfParamBlocks];
0078 void readPSet(const edm::ParameterSet &iConfig,
0079 const std::string &name,
0080 int id1 = -1,
0081 int id2 = -1,
0082 int id3 = -1,
0083 int id4 = -1,
0084 int id5 = -1,
0085 int id6 = -1);
0086
0087 std::vector<uint8_t> pixels, strips;
0088 edm::ProductID pixelSourceProdID, stripSourceProdID;
0089
0090 inline void process(const TrackingRecHit *hit, float chi2, const TrackerGeometry *tg);
0091 inline void process(const OmniClusterRef &cluRef, uint32_t subdet);
0092
0093 template <typename T>
0094 std::unique_ptr<edmNew::DetSetVector<T> > cleanup(const edmNew::DetSetVector<T> &oldClusters,
0095 const std::vector<uint8_t> &isGood,
0096 reco::ClusterRemovalInfo::Indices &refs,
0097 const reco::ClusterRemovalInfo::Indices *oldRefs);
0098
0099
0100 void mergeOld(reco::ClusterRemovalInfo::Indices &refs, const reco::ClusterRemovalInfo::Indices &oldRefs);
0101
0102 bool makeProducts_;
0103 bool doStripChargeCheck_, doPixelChargeCheck_;
0104 std::vector<bool> collectedRegStrips_;
0105 std::vector<bool> collectedPixels_;
0106 };
0107
0108 using namespace std;
0109 using namespace edm;
0110 using namespace reco;
0111
0112 void HLTTrackClusterRemoverNew::readPSet(
0113 const edm::ParameterSet &iConfig, const std::string &name, int id1, int id2, int id3, int id4, int id5, int id6) {
0114 if (iConfig.exists(name)) {
0115 ParamBlock pblock(iConfig.getParameter<ParameterSet>(name));
0116 if (id1 == -1) {
0117 fill(pblocks_, pblocks_ + NumberOfParamBlocks, pblock);
0118 } else {
0119 pblocks_[id1] = pblock;
0120 if (id2 != -1)
0121 pblocks_[id2] = pblock;
0122 if (id3 != -1)
0123 pblocks_[id3] = pblock;
0124 if (id4 != -1)
0125 pblocks_[id4] = pblock;
0126 if (id5 != -1)
0127 pblocks_[id5] = pblock;
0128 if (id6 != -1)
0129 pblocks_[id6] = pblock;
0130 }
0131 }
0132 }
0133
0134 HLTTrackClusterRemoverNew::HLTTrackClusterRemoverNew(const ParameterSet &iConfig)
0135 : tTrackerGeom_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>()),
0136 doTracks_(iConfig.exists("trajectories")),
0137 doStrip_(iConfig.existsAs<bool>("doStrip") ? iConfig.getParameter<bool>("doStrip") : true),
0138 doPixel_(iConfig.existsAs<bool>("doPixel") ? iConfig.getParameter<bool>("doPixel") : true),
0139 mergeOld_(false),
0140 makeProducts_(true),
0141 doStripChargeCheck_(
0142 iConfig.existsAs<bool>("doStripChargeCheck") ? iConfig.getParameter<bool>("doStripChargeCheck") : false),
0143 doPixelChargeCheck_(
0144 iConfig.existsAs<bool>("doPixelChargeCheck") ? iConfig.getParameter<bool>("doPixelChargeCheck") : false)
0145
0146 {
0147 if (iConfig.exists("oldClusterRemovalInfo")) {
0148 oldPxlMaskToken_ = consumes<PixelMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0149 oldStrMaskToken_ = consumes<StripMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0150 if (not(iConfig.getParameter<InputTag>("oldClusterRemovalInfo") == edm::InputTag()))
0151 mergeOld_ = true;
0152 }
0153
0154 if ((doPixelChargeCheck_ && !doPixel_) || (doStripChargeCheck_ && !doStrip_))
0155 throw cms::Exception("Configuration Error")
0156 << "HLTTrackClusterRemoverNew: Charge check asked without cluster collection ";
0157 if (doPixelChargeCheck_)
0158 throw cms::Exception("Configuration Error")
0159 << "HLTTrackClusterRemoverNew: Pixel cluster charge check not yet implemented";
0160
0161 fill(pblocks_, pblocks_ + NumberOfParamBlocks, ParamBlock());
0162 readPSet(iConfig, "Common", -1);
0163 if (doPixel_) {
0164 readPSet(iConfig, "Pixel", 0, 1);
0165 readPSet(iConfig, "PXB", 0);
0166 readPSet(iConfig, "PXE", 1);
0167 }
0168 if (doStrip_) {
0169 readPSet(iConfig, "Strip", 2, 3, 4, 5);
0170 readPSet(iConfig, "StripInner", 2, 3);
0171 readPSet(iConfig, "StripOuter", 4, 5);
0172 readPSet(iConfig, "TIB", 2);
0173 readPSet(iConfig, "TID", 3);
0174 readPSet(iConfig, "TOB", 4);
0175 readPSet(iConfig, "TEC", 5);
0176 }
0177
0178 bool usingCharge = false;
0179 for (size_t i = 0; i < NumberOfParamBlocks; ++i) {
0180 if (!pblocks_[i].isSet_)
0181 throw cms::Exception("Configuration Error")
0182 << "HLTTrackClusterRemoverNew: Missing configuration for detector with subDetID = " << (i + 1);
0183 if (pblocks_[i].usesCharge_ && !usingCharge) {
0184 throw cms::Exception("Configuration Error")
0185 << "HLTTrackClusterRemoverNew: Configuration for subDetID = " << (i + 1)
0186 << " uses cluster charge, which is not enabled.";
0187 }
0188 }
0189
0190
0191 if (doTracks_)
0192 trajectories_ = consumes<vector<Trajectory> >(iConfig.getParameter<InputTag>("trajectories"));
0193 if (doPixel_)
0194 pixelClusters_ = consumes<edmNew::DetSetVector<SiPixelCluster> >(iConfig.getParameter<InputTag>("pixelClusters"));
0195 if (doStrip_)
0196 stripClusters_ = consumes<edmNew::DetSetVector<SiStripCluster> >(iConfig.getParameter<InputTag>("stripClusters"));
0197 if (mergeOld_) {
0198 oldPxlMaskToken_ = consumes<PixelMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0199 oldStrMaskToken_ = consumes<StripMaskContainer>(iConfig.getParameter<InputTag>("oldClusterRemovalInfo"));
0200 }
0201
0202
0203
0204
0205 produces<edm::ContainerMask<edmNew::DetSetVector<SiPixelCluster> > >();
0206 produces<edm::ContainerMask<edmNew::DetSetVector<SiStripCluster> > >();
0207 }
0208
0209 HLTTrackClusterRemoverNew::~HLTTrackClusterRemoverNew() {}
0210
0211 void HLTTrackClusterRemoverNew::mergeOld(ClusterRemovalInfo::Indices &refs,
0212 const ClusterRemovalInfo::Indices &oldRefs) {
0213 for (size_t i = 0, n = refs.size(); i < n; ++i) {
0214 refs[i] = oldRefs[refs[i]];
0215 }
0216 }
0217
0218 template <typename T>
0219 std::unique_ptr<edmNew::DetSetVector<T> > HLTTrackClusterRemoverNew::cleanup(
0220 const edmNew::DetSetVector<T> &oldClusters,
0221 const std::vector<uint8_t> &isGood,
0222 reco::ClusterRemovalInfo::Indices &refs,
0223 const reco::ClusterRemovalInfo::Indices *oldRefs) {
0224 typedef typename edmNew::DetSetVector<T> DSV;
0225 typedef typename edmNew::DetSetVector<T>::FastFiller DSF;
0226 typedef typename edmNew::DetSet<T> DS;
0227 auto output = std::make_unique<DSV>();
0228 output->reserve(oldClusters.size(), oldClusters.dataSize());
0229
0230
0231 const T *firstOffset = &oldClusters.data().front();
0232 for (typename DSV::const_iterator itdet = oldClusters.begin(), enddet = oldClusters.end(); itdet != enddet; ++itdet) {
0233 DS oldDS = *itdet;
0234
0235 if (oldDS.empty())
0236 continue;
0237
0238 uint32_t id = oldDS.detId();
0239 DSF outds(*output, id);
0240
0241 for (typename DS::const_iterator it = oldDS.begin(), ed = oldDS.end(); it != ed; ++it) {
0242 uint32_t index = ((&*it) - firstOffset);
0243 if (isGood[index]) {
0244 outds.push_back(*it);
0245 refs.push_back(index);
0246
0247 }
0248 }
0249 if (outds.empty())
0250 outds.abort();
0251 }
0252
0253 if (oldRefs != nullptr)
0254 mergeOld(refs, *oldRefs);
0255 return output;
0256 }
0257
0258 void HLTTrackClusterRemoverNew::process(OmniClusterRef const &clusterReg, uint32_t subdet) {
0259 if (clusterReg.id() != stripSourceProdID)
0260 throw cms::Exception("Inconsistent Data")
0261 << "HLTTrackClusterRemoverNew: strip cluster ref from Product ID = " << clusterReg.id()
0262 << " does not match with source cluster collection (ID = " << stripSourceProdID << ")\n.";
0263
0264 if (collectedRegStrips_.size() <= clusterReg.key()) {
0265 edm::LogError("BadCollectionSize") << collectedRegStrips_.size() << " is smaller than " << clusterReg.key();
0266
0267 assert(collectedRegStrips_.size() > clusterReg.key());
0268 }
0269 collectedRegStrips_[clusterReg.key()] = true;
0270 }
0271
0272 void HLTTrackClusterRemoverNew::process(const TrackingRecHit *hit, float chi2, const TrackerGeometry *tg) {
0273 DetId detid = hit->geographicalId();
0274 uint32_t subdet = detid.subdetId();
0275
0276 assert((subdet > 0) && (subdet <= NumberOfParamBlocks));
0277
0278
0279 if (chi2 > pblocks_[subdet - 1].maxChi2_)
0280 return;
0281
0282 if (GeomDetEnumerators::isTrackerPixel(tg->geomDetSubDetector(subdet))) {
0283
0284 if (!doPixel_)
0285 return;
0286
0287 const SiPixelRecHit *pixelHit = static_cast<const SiPixelRecHit *>(hit);
0288
0289 SiPixelRecHit::ClusterRef cluster = pixelHit->cluster();
0290 if (cluster.id() != pixelSourceProdID)
0291 throw cms::Exception("Inconsistent Data")
0292 << "HLTTrackClusterRemoverNew: pixel cluster ref from Product ID = " << cluster.id()
0293 << " does not match with source cluster collection (ID = " << pixelSourceProdID << ")\n.";
0294
0295 assert(cluster.id() == pixelSourceProdID);
0296
0297
0298
0299 if (pblocks_[subdet - 1].usesSize_ && (cluster->pixels().size() > pblocks_[subdet - 1].maxSize_))
0300 return;
0301
0302
0303
0304 assert(collectedPixels_.size() > cluster.key());
0305 collectedPixels_[cluster.key()] = true;
0306 } else {
0307 if (!doStrip_)
0308 return;
0309 const type_info &hitType = typeid(*hit);
0310 if (hitType == typeid(SiStripRecHit2D)) {
0311 const SiStripRecHit2D *stripHit = static_cast<const SiStripRecHit2D *>(hit);
0312
0313 process(stripHit->omniClusterRef(), subdet);
0314
0315
0316
0317 } else if (hitType == typeid(SiStripRecHit1D)) {
0318 const SiStripRecHit1D *hit1D = static_cast<const SiStripRecHit1D *>(hit);
0319 process(hit1D->omniClusterRef(), subdet);
0320
0321
0322
0323 } else if (hitType == typeid(SiStripMatchedRecHit2D)) {
0324 const SiStripMatchedRecHit2D *matchHit = static_cast<const SiStripMatchedRecHit2D *>(hit);
0325
0326 process(matchHit->monoClusterRef(), subdet);
0327
0328
0329
0330
0331 process(matchHit->stereoClusterRef(), subdet);
0332
0333
0334
0335
0336 } else if (hitType == typeid(ProjectedSiStripRecHit2D)) {
0337 const ProjectedSiStripRecHit2D *projHit = static_cast<const ProjectedSiStripRecHit2D *>(hit);
0338
0339 process(projHit->originalHit().omniClusterRef(), subdet);
0340
0341
0342
0343 } else
0344 throw cms::Exception("NOT IMPLEMENTED")
0345 << "Don't know how to handle " << hitType.name() << " on detid " << detid.rawId() << "\n";
0346 }
0347 }
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359 void HLTTrackClusterRemoverNew::produce(Event &iEvent, const EventSetup &iSetup) {
0360 ProductID pixelOldProdID, stripOldProdID;
0361
0362 const auto &tgh = &iSetup.getData(tTrackerGeom_);
0363
0364 edm::Handle<edmNew::DetSetVector<SiPixelCluster> > pixelClusters;
0365 if (doPixel_) {
0366 iEvent.getByToken(pixelClusters_, pixelClusters);
0367 pixelSourceProdID = pixelClusters.id();
0368 }
0369
0370 edm::Handle<edmNew::DetSetVector<SiStripCluster> > stripClusters;
0371 if (doStrip_) {
0372 iEvent.getByToken(stripClusters_, stripClusters);
0373 stripSourceProdID = stripClusters.id();
0374 }
0375
0376
0377 edm::Handle<vector<Trajectory> > trajectories;
0378 iEvent.getByToken(trajectories_, trajectories);
0379
0380 if (mergeOld_) {
0381 edm::Handle<PixelMaskContainer> oldPxlMask;
0382 edm::Handle<StripMaskContainer> oldStrMask;
0383 iEvent.getByToken(oldPxlMaskToken_, oldPxlMask);
0384 iEvent.getByToken(oldStrMaskToken_, oldStrMask);
0385 LogDebug("TrackClusterRemover") << "to merge in, " << oldStrMask->size() << " strp and " << oldPxlMask->size()
0386 << " pxl";
0387 oldStrMask->copyMaskTo(collectedRegStrips_);
0388 oldPxlMask->copyMaskTo(collectedPixels_);
0389 collectedRegStrips_.resize(stripClusters->dataSize(), false);
0390 } else {
0391 collectedRegStrips_.resize(stripClusters->dataSize(), false);
0392 collectedPixels_.resize(pixelClusters->dataSize(), false);
0393 }
0394
0395
0396
0397
0398 for (std::vector<Trajectory>::const_iterator it = trajectories->begin(), ed = trajectories->end(); it != ed; ++it) {
0399 const Trajectory &tj = *it;
0400 const std::vector<TrajectoryMeasurement> &tms = tj.measurements();
0401
0402 std::vector<TrajectoryMeasurement>::const_iterator itm, endtm;
0403 for (itm = tms.begin(), endtm = tms.end(); itm != endtm; ++itm) {
0404 const TrackingRecHit *hit = itm->recHit()->hit();
0405 if (!hit->isValid())
0406 continue;
0407
0408 process(hit, itm->estimate(), tgh);
0409 }
0410 }
0411
0412
0413
0414
0415
0416 if (doStripChargeCheck_) {
0417
0418
0419 auto const &clusters = stripClusters->data();
0420 for (auto const &item : stripClusters->ids()) {
0421 if (!item.isValid())
0422 continue;
0423
0424 DetId detid = item.id;
0425 uint32_t subdet = detid.subdetId();
0426 if (!pblocks_[subdet - 1].cutOnStripCharge_)
0427 continue;
0428
0429
0430
0431 for (int i = item.offset; i < item.offset + int(item.size); ++i) {
0432 int clusCharge = 0;
0433 for (auto cAmp : clusters[i].amplitudes())
0434 clusCharge += cAmp;
0435
0436
0437 if (clusCharge < pblocks_[subdet - 1].minGoodStripCharge_)
0438 collectedRegStrips_[i] = true;
0439 }
0440 }
0441 }
0442
0443
0444
0445 LogDebug("TrackClusterRemover") << "total strip to skip: "
0446 << std::count(collectedRegStrips_.begin(), collectedRegStrips_.end(), true);
0447
0448 iEvent.put(std::make_unique<StripMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiStripCluster> >(stripClusters),
0449 collectedRegStrips_));
0450
0451 LogDebug("TrackClusterRemover") << "total pxl to skip: "
0452 << std::count(collectedPixels_.begin(), collectedPixels_.end(), true);
0453 iEvent.put(std::make_unique<PixelMaskContainer>(edm::RefProd<edmNew::DetSetVector<SiPixelCluster> >(pixelClusters),
0454 collectedPixels_));
0455
0456 collectedRegStrips_.clear();
0457 collectedPixels_.clear();
0458 }
0459
0460 #include "FWCore/PluginManager/interface/ModuleDef.h"
0461 #include "FWCore/Framework/interface/MakerMacros.h"
0462 DEFINE_FWK_MODULE(HLTTrackClusterRemoverNew);