File indexing completed on 2021-09-16 03:24:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <memory>
0017
0018
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/Framework/interface/ESHandle.h"
0021
0022
0023 #include "PhysicsTools/SelectorUtils/interface/JetIDSelectionFunctor.h"
0024 #include "PhysicsTools/SelectorUtils/interface/strbitset.h"
0025
0026 #include "FWCore/Framework/interface/Frameworkfwd.h"
0027 #include "FWCore/Framework/interface/stream/EDFilter.h"
0028
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033
0034 #include "FWCore/Utilities/interface/Exception.h"
0035
0036 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
0037 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0038 #include "DataFormats/DetId/interface/DetId.h"
0039 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0040 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0041
0042 #include "CondFormats/EcalObjects/interface/EcalChannelStatus.h"
0043 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
0044
0045 #include "DataFormats/METReco/interface/METCollection.h"
0046 #include "DataFormats/METReco/interface/CaloMET.h"
0047 #include "DataFormats/METReco/interface/CaloMETCollection.h"
0048
0049 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0050 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0051 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0052
0053 #include "Geometry/Records/interface/CaloTopologyRecord.h"
0054 #include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
0055 #include "Geometry/CaloTopology/interface/CaloTopology.h"
0056
0057
0058 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0059 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
0060 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0061 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0062
0063 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
0064 #include "Geometry/CaloTopology/interface/CaloTowerConstituentsMap.h"
0065 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
0066 #include "DataFormats/CaloTowers/interface/CaloTowerDetId.h"
0067
0068 #include "DataFormats/METReco/interface/PFMETCollection.h"
0069 #include "DataFormats/METReco/interface/PFMET.h"
0070
0071 #include "RecoJets/JetProducers/interface/JetIDHelper.h"
0072
0073 #include "DataFormats/JetReco/interface/GenJet.h"
0074 #include "DataFormats/JetReco/interface/GenJetCollection.h"
0075 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0076 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0077 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
0078
0079 #include "FWCore/Common/interface/TriggerNames.h"
0080 #include "FWCore/Framework/interface/TriggerNamesService.h"
0081 #include "DataFormats/Common/interface/TriggerResults.h"
0082
0083 #include "DataFormats/Math/interface/deltaR.h"
0084
0085 #include "DataFormats/Provenance/interface/RunLumiEventNumber.h"
0086
0087 class EcalDeadCellDeltaRFilter : public edm::stream::EDFilter<> {
0088 public:
0089 explicit EcalDeadCellDeltaRFilter(const edm::ParameterSet &);
0090 ~EcalDeadCellDeltaRFilter() override = default;
0091
0092 private:
0093 bool filter(edm::Event &, const edm::EventSetup &) override;
0094 void beginRun(const edm::Run &, const edm::EventSetup &) override;
0095 void endRun(const edm::Run &, const edm::EventSetup &) override;
0096 virtual void envSet(const edm::EventSetup &);
0097
0098
0099 edm::EDGetTokenT<edm::View<reco::Jet> > jetToken_;
0100 edm::Handle<edm::View<reco::Jet> > jets;
0101
0102
0103 const std::vector<double> jetSelCuts_;
0104
0105 edm::EDGetTokenT<edm::View<reco::MET> > metToken_;
0106 edm::Handle<edm::View<reco::MET> > met;
0107
0108 const bool debug_, printSkimInfo_;
0109
0110 bool isPrintedOnce;
0111
0112 void loadEventInfo(const edm::Event &iEvent, const edm::EventSetup &iSetup);
0113 void loadJets(const edm::Event &iEvent, const edm::EventSetup &iSetup);
0114 void loadMET(const edm::Event &iEvent, const edm::EventSetup &iSetup);
0115
0116 edm::RunNumber_t run;
0117 edm::EventNumber_t event;
0118 edm::LuminosityBlockNumber_t ls;
0119 bool isdata;
0120
0121 double calomet, calometPhi, tcmet, tcmetPhi, pfmet, pfmetPhi;
0122
0123
0124 edm::ESHandle<EcalChannelStatus> ecalStatus;
0125 edm::ESHandle<CaloGeometry> geometry;
0126 const EcalTrigTowerConstituentsMap *ttMap_;
0127 const edm::ESGetToken<EcalChannelStatus, EcalChannelStatusRcd> ecalChannelStatusToken_;
0128 const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> caloGeometryToken_;
0129 const edm::ESGetToken<EcalTrigTowerConstituentsMap, IdealGeometryRecord> ecalTrigTowerConstituentsMapToken_;
0130
0131 const int maskedEcalChannelStatusThreshold_;
0132 const int chnStatusToBeEvaluated_;
0133
0134
0135
0136 std::map<DetId, std::vector<double> > EcalAllDeadChannelsValMap;
0137
0138
0139 std::map<DetId, std::vector<int> > EcalAllDeadChannelsBitMap;
0140
0141
0142 std::map<DetId, EcalTrigTowerDetId> EcalAllDeadChannelsTTMap;
0143
0144 int getChannelStatusMaps();
0145
0146 int evtProcessedCnt, totTPFilteredCnt;
0147 double wtdEvtProcessed, wtdTPFiltered;
0148
0149 const bool isProd_;
0150 const int verbose_;
0151
0152 const bool doCracks_;
0153
0154 const std::vector<double> cracksHBHEdef_, cracksHEHFdef_;
0155
0156
0157 const std::vector<double> EcalDeadCellDeltaRFilterInput_;
0158
0159 int dPhiToMETfunc(const std::vector<reco::Jet> &jetTVec,
0160 const double &dPhiCutVal,
0161 std::vector<reco::Jet> &closeToMETjetsVec);
0162 int dRtoMaskedChnsEvtFilterFunc(const std::vector<reco::Jet> &jetTVec, const int &chnStatus, const double &dRCutVal);
0163
0164 int etaToBoundary(const std::vector<reco::Jet> &jetTVec);
0165
0166 int isCloseToBadEcalChannel(const reco::Jet &jet,
0167 const double &deltaRCut,
0168 const int &chnStatus,
0169 std::map<double, DetId> &deltaRdetIdMap);
0170
0171 const bool taggingMode_;
0172 };
0173
0174 void EcalDeadCellDeltaRFilter::loadMET(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
0175 iEvent.getByToken(metToken_, met);
0176 }
0177
0178 void EcalDeadCellDeltaRFilter::loadEventInfo(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
0179 run = iEvent.id().run();
0180 event = iEvent.id().event();
0181 ls = iEvent.luminosityBlock();
0182 isdata = iEvent.isRealData();
0183
0184 if (!isPrintedOnce) {
0185 if (debug_) {
0186 if (isdata)
0187 std::cout << "\nInput dataset is DATA" << std::endl << std::endl;
0188 else
0189 std::cout << "\nInput dataset is MC" << std::endl << std::endl;
0190 }
0191 isPrintedOnce = true;
0192 }
0193 }
0194
0195 void EcalDeadCellDeltaRFilter::loadJets(const edm::Event &iEvent, const edm::EventSetup &iSetup) {
0196 iEvent.getByToken(jetToken_, jets);
0197 }
0198
0199
0200
0201
0202
0203
0204
0205
0206 EcalDeadCellDeltaRFilter::EcalDeadCellDeltaRFilter(const edm::ParameterSet &iConfig)
0207 : jetToken_(consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>("jetInputTag"))),
0208 jetSelCuts_(iConfig.getParameter<std::vector<double> >("jetSelCuts")),
0209 metToken_(consumes<edm::View<reco::MET> >(iConfig.getParameter<edm::InputTag>("metInputTag"))),
0210 debug_(iConfig.getUntrackedParameter<bool>("debug", false)),
0211 printSkimInfo_(iConfig.getUntrackedParameter<bool>("printSkimInfo", false)),
0212 ecalChannelStatusToken_(esConsumes<edm::Transition::BeginRun>()),
0213 caloGeometryToken_(esConsumes<edm::Transition::BeginRun>()),
0214 ecalTrigTowerConstituentsMapToken_(esConsumes<edm::Transition::BeginRun>()),
0215 maskedEcalChannelStatusThreshold_(iConfig.getParameter<int>("maskedEcalChannelStatusThreshold")),
0216 chnStatusToBeEvaluated_(iConfig.getParameter<int>("chnStatusToBeEvaluated")),
0217 isProd_(iConfig.getUntrackedParameter<bool>("isProd")),
0218 verbose_(iConfig.getParameter<int>("verbose")),
0219 doCracks_(iConfig.getUntrackedParameter<bool>("doCracks")),
0220 cracksHBHEdef_(iConfig.getParameter<std::vector<double> >("cracksHBHEdef")),
0221 cracksHEHFdef_(iConfig.getParameter<std::vector<double> >("cracksHEHFdef")),
0222 EcalDeadCellDeltaRFilterInput_(iConfig.getParameter<std::vector<double> >("EcalDeadCellDeltaRFilterInput")),
0223 taggingMode_(iConfig.getParameter<bool>("taggingMode")) {
0224 produces<int>("deadCellStatus");
0225 produces<int>("boundaryStatus");
0226 produces<bool>();
0227 }
0228
0229 void EcalDeadCellDeltaRFilter::envSet(const edm::EventSetup &iSetup) {
0230 if (debug_)
0231 std::cout << "***envSet***" << std::endl;
0232
0233 ttMap_ = &iSetup.getData(ecalTrigTowerConstituentsMapToken_);
0234
0235 ecalStatus = iSetup.getHandle(ecalChannelStatusToken_);
0236 geometry = iSetup.getHandle(caloGeometryToken_);
0237
0238 if (!ecalStatus.isValid())
0239 throw cms::Exception("ESDataError") << "Failed to get ECAL channel status!";
0240 if (!geometry.isValid())
0241 throw cms::Exception("ESDataError") << "Failed to get the geometry!";
0242 }
0243
0244
0245 bool EcalDeadCellDeltaRFilter::filter(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0246 loadEventInfo(iEvent, iSetup);
0247 loadJets(iEvent, iSetup);
0248 loadMET(iEvent, iSetup);
0249
0250
0251
0252 bool pass = true;
0253
0254 using namespace edm;
0255
0256 std::vector<reco::Jet> seledJets;
0257
0258 for (edm::View<reco::Jet>::const_iterator ij = jets->begin(); ij != jets->end(); ij++) {
0259 if (ij->pt() > jetSelCuts_[0] && std::abs(ij->eta()) < jetSelCuts_[1]) {
0260 seledJets.push_back(reco::Jet(*ij));
0261 }
0262 }
0263
0264 if (seledJets.empty())
0265 return pass;
0266
0267 double dPhiToMET = EcalDeadCellDeltaRFilterInput_[0], dRtoDeadCell = EcalDeadCellDeltaRFilterInput_[1];
0268
0269 std::vector<reco::Jet> closeToMETjetsVec;
0270
0271 int dPhiToMETstatus = dPhiToMETfunc(seledJets, dPhiToMET, closeToMETjetsVec);
0272
0273
0274 int deadCellStatus = dRtoMaskedChnsEvtFilterFunc(closeToMETjetsVec, chnStatusToBeEvaluated_, dRtoDeadCell);
0275
0276 int boundaryStatus = etaToBoundary(closeToMETjetsVec);
0277
0278 if (debug_) {
0279 printf("\nrun : %8u event : %12llu ls : %8u dPhiToMETstatus : %d deadCellStatus : %d boundaryStatus : %d\n",
0280 run,
0281 event,
0282 ls,
0283 dPhiToMETstatus,
0284 deadCellStatus,
0285 boundaryStatus);
0286 printf("met : %6.2f metphi : % 6.3f dPhiToMET : %5.3f dRtoDeadCell : %5.3f\n",
0287 (*met)[0].pt(),
0288 (*met)[0].phi(),
0289 dPhiToMET,
0290 dRtoDeadCell);
0291 }
0292
0293 iEvent.put(std::make_unique<int>(deadCellStatus), "deadCellStatus");
0294 iEvent.put(std::make_unique<int>(boundaryStatus), "boundaryStatus");
0295
0296 if (deadCellStatus || (doCracks_ && boundaryStatus))
0297 pass = false;
0298
0299 iEvent.put(std::make_unique<bool>(pass));
0300
0301 return taggingMode_ || pass;
0302 }
0303
0304
0305 void EcalDeadCellDeltaRFilter::beginRun(const edm::Run &run, const edm::EventSetup &iSetup) {
0306 if (debug_)
0307 std::cout << "beginRun" << std::endl;
0308
0309
0310 envSet(iSetup);
0311 getChannelStatusMaps();
0312 if (debug_)
0313 std::cout << "EcalAllDeadChannelsValMap.size() : " << EcalAllDeadChannelsValMap.size()
0314 << " EcalAllDeadChannelsBitMap.size() : " << EcalAllDeadChannelsBitMap.size() << std::endl;
0315 return;
0316 }
0317
0318
0319 void EcalDeadCellDeltaRFilter::endRun(const edm::Run &run, const edm::EventSetup &iSetup) {
0320 if (debug_)
0321 std::cout << "endRun" << std::endl;
0322 return;
0323 }
0324
0325 int EcalDeadCellDeltaRFilter::etaToBoundary(const std::vector<reco::Jet> &jetTVec) {
0326 int isClose = 0;
0327
0328 int cntOrder10 = 0;
0329 for (unsigned int ij = 0; ij < jetTVec.size(); ij++) {
0330 double recoJetEta = jetTVec[ij].eta();
0331
0332 if (std::abs(recoJetEta) > cracksHBHEdef_[0] && std::abs(recoJetEta) < cracksHBHEdef_[1])
0333 isClose += (cntOrder10 * 10 + 1);
0334 if (std::abs(recoJetEta) > cracksHEHFdef_[0] && std::abs(recoJetEta) < cracksHEHFdef_[1])
0335 isClose += (cntOrder10 * 10 + 2);
0336
0337 if (isClose / pow(10, cntOrder10) >= 3)
0338 cntOrder10 = isClose / 10 + 1;
0339 }
0340
0341 return isClose;
0342 }
0343
0344
0345 int EcalDeadCellDeltaRFilter::dPhiToMETfunc(const std::vector<reco::Jet> &jetTVec,
0346 const double &dPhiCutVal,
0347 std::vector<reco::Jet> &closeToMETjetsVec) {
0348 closeToMETjetsVec.clear();
0349
0350 double minDphi = 999.0;
0351 int minIdx = -1;
0352 for (unsigned int ii = 0; ii < jetTVec.size(); ii++) {
0353 const reco::Jet &jet = jetTVec[ii];
0354
0355 double deltaPhi = std::abs(reco::deltaPhi(jet.phi(), (*met)[0].phi()));
0356 if (deltaPhi > dPhiCutVal)
0357 continue;
0358
0359 closeToMETjetsVec.push_back(jetTVec[ii]);
0360
0361 if (deltaPhi < minDphi) {
0362 minDphi = deltaPhi;
0363 minIdx = ii;
0364 }
0365 }
0366
0367 if (minIdx == -1) {
0368 }
0369
0370
0371
0372 return (int)closeToMETjetsVec.size();
0373 }
0374
0375 int EcalDeadCellDeltaRFilter::dRtoMaskedChnsEvtFilterFunc(const std::vector<reco::Jet> &jetTVec,
0376 const int &chnStatus,
0377 const double &dRCutVal) {
0378 int isClose = 0;
0379
0380 for (unsigned int ii = 0; ii < jetTVec.size(); ii++) {
0381 const reco::Jet &jet = jetTVec[ii];
0382
0383 std::map<double, DetId> dummy;
0384 int isPerJetClose = isCloseToBadEcalChannel(jet, dRCutVal, chnStatus, dummy);
0385
0386 if (isPerJetClose) {
0387 isClose++;
0388 }
0389 }
0390
0391 return isClose;
0392 }
0393
0394 int EcalDeadCellDeltaRFilter::isCloseToBadEcalChannel(const reco::Jet &jet,
0395 const double &deltaRCut,
0396 const int &chnStatus,
0397 std::map<double, DetId> &deltaRdetIdMap) {
0398 double jetEta = jet.eta(), jetPhi = jet.phi();
0399
0400 deltaRdetIdMap.clear();
0401
0402 double min_dist = 999;
0403 DetId min_detId;
0404
0405 std::map<DetId, std::vector<int> >::iterator bitItor;
0406 for (bitItor = EcalAllDeadChannelsBitMap.begin(); bitItor != EcalAllDeadChannelsBitMap.end(); bitItor++) {
0407 DetId maskedDetId = bitItor->first;
0408
0409 int status = bitItor->second.back();
0410
0411 if (chnStatus > 0 && status != chnStatus)
0412 continue;
0413 if (chnStatus < 0 && status < abs(chnStatus))
0414 continue;
0415
0416 std::map<DetId, std::vector<double> >::iterator valItor = EcalAllDeadChannelsValMap.find(maskedDetId);
0417 if (valItor == EcalAllDeadChannelsValMap.end()) {
0418 std::cout << "Error cannot find maskedDetId in EcalAllDeadChannelsValMap ?!" << std::endl;
0419 continue;
0420 }
0421
0422 double eta = (valItor->second)[0], phi = (valItor->second)[1];
0423
0424 double dist = reco::deltaR(eta, phi, jetEta, jetPhi);
0425
0426 if (min_dist > dist) {
0427 min_dist = dist;
0428 min_detId = maskedDetId;
0429 }
0430 }
0431
0432 if (min_dist > deltaRCut && deltaRCut > 0)
0433 return 0;
0434
0435 deltaRdetIdMap.insert(std::make_pair(min_dist, min_detId));
0436
0437 return 1;
0438 }
0439
0440 int EcalDeadCellDeltaRFilter::getChannelStatusMaps() {
0441 EcalAllDeadChannelsValMap.clear();
0442 EcalAllDeadChannelsBitMap.clear();
0443
0444
0445 for (int ieta = -85; ieta <= 85; ieta++) {
0446 for (int iphi = 0; iphi <= 360; iphi++) {
0447 if (!EBDetId::validDetId(ieta, iphi))
0448 continue;
0449
0450 const EBDetId detid = EBDetId(ieta, iphi, EBDetId::ETAPHIMODE);
0451 EcalChannelStatus::const_iterator chit = ecalStatus->find(detid);
0452
0453 int status = (chit != ecalStatus->end()) ? chit->getStatusCode() & 0x1F : -1;
0454
0455 const CaloSubdetectorGeometry *subGeom = geometry->getSubdetectorGeometry(detid);
0456 auto cellGeom = subGeom->getGeometry(detid);
0457 double eta = cellGeom->getPosition().eta();
0458 double phi = cellGeom->getPosition().phi();
0459 double theta = cellGeom->getPosition().theta();
0460
0461 if (status >= maskedEcalChannelStatusThreshold_) {
0462 std::vector<double> valVec;
0463 std::vector<int> bitVec;
0464 valVec.push_back(eta);
0465 valVec.push_back(phi);
0466 valVec.push_back(theta);
0467 bitVec.push_back(1);
0468 bitVec.push_back(ieta);
0469 bitVec.push_back(iphi);
0470 bitVec.push_back(status);
0471 EcalAllDeadChannelsValMap.insert(std::make_pair(detid, valVec));
0472 EcalAllDeadChannelsBitMap.insert(std::make_pair(detid, bitVec));
0473 }
0474 }
0475 }
0476
0477
0478 for (int ix = 0; ix <= 100; ix++) {
0479 for (int iy = 0; iy <= 100; iy++) {
0480 for (int iz = -1; iz <= 1; iz++) {
0481 if (iz == 0)
0482 continue;
0483 if (!EEDetId::validDetId(ix, iy, iz))
0484 continue;
0485
0486 const EEDetId detid = EEDetId(ix, iy, iz, EEDetId::XYMODE);
0487 EcalChannelStatus::const_iterator chit = ecalStatus->find(detid);
0488 int status = (chit != ecalStatus->end()) ? chit->getStatusCode() & 0x1F : -1;
0489
0490 const CaloSubdetectorGeometry *subGeom = geometry->getSubdetectorGeometry(detid);
0491 auto cellGeom = subGeom->getGeometry(detid);
0492 double eta = cellGeom->getPosition().eta();
0493 double phi = cellGeom->getPosition().phi();
0494 double theta = cellGeom->getPosition().theta();
0495
0496 if (status >= maskedEcalChannelStatusThreshold_) {
0497 std::vector<double> valVec;
0498 std::vector<int> bitVec;
0499 valVec.push_back(eta);
0500 valVec.push_back(phi);
0501 valVec.push_back(theta);
0502 bitVec.push_back(2);
0503 bitVec.push_back(ix);
0504 bitVec.push_back(iy);
0505 bitVec.push_back(iz);
0506 bitVec.push_back(status);
0507 EcalAllDeadChannelsValMap.insert(std::make_pair(detid, valVec));
0508 EcalAllDeadChannelsBitMap.insert(std::make_pair(detid, bitVec));
0509 }
0510 }
0511 }
0512 }
0513
0514 EcalAllDeadChannelsTTMap.clear();
0515 std::map<DetId, std::vector<int> >::iterator bitItor;
0516 for (bitItor = EcalAllDeadChannelsBitMap.begin(); bitItor != EcalAllDeadChannelsBitMap.end(); bitItor++) {
0517 const DetId id = bitItor->first;
0518 EcalTrigTowerDetId ttDetId = ttMap_->towerOf(id);
0519 EcalAllDeadChannelsTTMap.insert(std::make_pair(id, ttDetId));
0520 }
0521
0522 return 1;
0523 }
0524
0525
0526 DEFINE_FWK_MODULE(EcalDeadCellDeltaRFilter);