Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:10

0001 // -*- C++ -*-
0002 //
0003 // Package:    SimMuon/CSCDigitizer
0004 // Class:      CSCChamberMasker
0005 //
0006 /**\class CSCChamberMasker CSCChamberMasker.cc
0007  SimMuon/CSCDigitizer/plugins/CSCChamberMasker.cc
0008 
0009  Description: Class to mask CSC digis on a chamber by chamber basis
0010 
0011 */
0012 //
0013 // Original Author:  Nick J. Amin
0014 //         Created:  Mon, 27 Feb 2017 15:12:51 GMT
0015 //
0016 //
0017 
0018 // system include files
0019 #include <algorithm>
0020 #include <iostream>
0021 #include <map>
0022 #include <memory>
0023 #include <string>
0024 
0025 // user include files
0026 #include "FWCore/Framework/interface/ConsumesCollector.h"
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/stream/EDProducer.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 
0031 #include "DataFormats/Common/interface/Handle.h"
0032 #include "FWCore/Framework/interface/Event.h"
0033 #include "FWCore/Framework/interface/Run.h"
0034 
0035 #include "FWCore/Framework/interface/ESHandle.h"
0036 #include "FWCore/Utilities/interface/ESGetToken.h"
0037 #include "FWCore/Framework/interface/EventSetup.h"
0038 
0039 #include "FWCore/Framework/interface/MakerMacros.h"
0040 
0041 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
0042 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0043 
0044 #include "Geometry/CSCGeometry/interface/CSCChamber.h"
0045 #include "Geometry/CSCGeometry/interface/CSCLayer.h"
0046 
0047 #include "DataFormats/CSCDigi/interface/CSCALCTDigi.h"
0048 #include "DataFormats/CSCDigi/interface/CSCALCTDigiCollection.h"
0049 #include "DataFormats/CSCDigi/interface/CSCCLCTDigi.h"
0050 #include "DataFormats/CSCDigi/interface/CSCCLCTDigiCollection.h"
0051 #include "DataFormats/CSCDigi/interface/CSCStripDigi.h"
0052 #include "DataFormats/CSCDigi/interface/CSCStripDigiCollection.h"
0053 #include "DataFormats/CSCDigi/interface/CSCWireDigi.h"
0054 #include "DataFormats/CSCDigi/interface/CSCWireDigiCollection.h"
0055 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0056 #include "DataFormats/MuonDetId/interface/CSCIndexer.h"
0057 
0058 #include "CondFormats/DataRecord/interface/MuonSystemAgingRcd.h"
0059 #include "CondFormats/RecoMuonObjects/interface/MuonSystemAging.h"
0060 
0061 #include "CLHEP/Random/RandomEngine.h"
0062 #include "FWCore/ServiceRegistry/interface/Service.h"
0063 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0064 
0065 //
0066 // class declaration
0067 //
0068 
0069 class CSCChamberMasker : public edm::stream::EDProducer<> {
0070 public:
0071   explicit CSCChamberMasker(const edm::ParameterSet &);
0072   ~CSCChamberMasker() override;
0073 
0074   static void fillDescriptions(edm::ConfigurationDescriptions &);
0075 
0076 private:
0077   void produce(edm::Event &, const edm::EventSetup &) override;
0078 
0079   void beginRun(edm::Run const &, edm::EventSetup const &) override;
0080 
0081   template <typename T, typename C = MuonDigiCollection<CSCDetId, T>>
0082   void ageDigis(edm::Event &event,
0083                 edm::EDGetTokenT<C> &digiToken,
0084                 CLHEP::HepRandomEngine &randGen,
0085                 std::unique_ptr<C> &filteredDigis);
0086 
0087   template <typename T, typename C = MuonDigiCollection<CSCDetId, T>>
0088   void copyDigis(edm::Event &event, edm::EDGetTokenT<C> &digiToken, std::unique_ptr<C> &filteredDigis);
0089 
0090   // ----------member data ---------------------------
0091 
0092   edm::EDGetTokenT<CSCStripDigiCollection> m_stripDigiToken;
0093   edm::EDGetTokenT<CSCWireDigiCollection> m_wireDigiToken;
0094   edm::EDGetTokenT<CSCCLCTDigiCollection> m_clctDigiToken;
0095   edm::EDGetTokenT<CSCALCTDigiCollection> m_alctDigiToken;
0096   edm::ESGetToken<CSCGeometry, MuonGeometryRecord> m_cscGeomToken;
0097   edm::ESGetToken<MuonSystemAging, MuonSystemAgingRcd> m_agingObjToken;
0098   std::map<CSCDetId, std::pair<unsigned int, float>> m_CSCEffs;
0099 };
0100 
0101 //
0102 // constants, enums and typedefs
0103 //
0104 
0105 //
0106 // static data member definitions
0107 //
0108 
0109 //
0110 // constructors and destructor
0111 //
0112 CSCChamberMasker::CSCChamberMasker(const edm::ParameterSet &iConfig)
0113     : m_stripDigiToken(consumes<CSCStripDigiCollection>(iConfig.getParameter<edm::InputTag>("stripDigiTag"))),
0114       m_wireDigiToken(consumes<CSCWireDigiCollection>(iConfig.getParameter<edm::InputTag>("wireDigiTag"))),
0115       m_clctDigiToken(consumes<CSCCLCTDigiCollection>(iConfig.getParameter<edm::InputTag>("clctDigiTag"))),
0116       m_alctDigiToken(consumes<CSCALCTDigiCollection>(iConfig.getParameter<edm::InputTag>("alctDigiTag"))),
0117       m_cscGeomToken(esConsumes<CSCGeometry, MuonGeometryRecord>()),
0118       m_agingObjToken(esConsumes<MuonSystemAging, MuonSystemAgingRcd>()) {
0119   produces<CSCStripDigiCollection>("MuonCSCStripDigi");
0120   produces<CSCWireDigiCollection>("MuonCSCWireDigi");
0121   produces<CSCCLCTDigiCollection>("MuonCSCCLCTDigi");
0122   produces<CSCALCTDigiCollection>("MuonCSCALCTDigi");
0123 }
0124 
0125 CSCChamberMasker::~CSCChamberMasker() {}
0126 
0127 //
0128 // member functions
0129 //
0130 
0131 // ------------ method called to produce the data  ------------
0132 void CSCChamberMasker::produce(edm::Event &event, const edm::EventSetup &conditions) {
0133   edm::Service<edm::RandomNumberGenerator> randGenService;
0134   CLHEP::HepRandomEngine &randGen = randGenService->getEngine(event.streamID());
0135 
0136   std::unique_ptr<CSCStripDigiCollection> filteredStripDigis(new CSCStripDigiCollection());
0137   std::unique_ptr<CSCWireDigiCollection> filteredWireDigis(new CSCWireDigiCollection());
0138   std::unique_ptr<CSCCLCTDigiCollection> filteredCLCTDigis(new CSCCLCTDigiCollection());
0139   std::unique_ptr<CSCALCTDigiCollection> filteredALCTDigis(new CSCALCTDigiCollection());
0140 
0141   // Handle wire and strip digis
0142   ageDigis<CSCStripDigi>(event, m_stripDigiToken, randGen, filteredStripDigis);
0143   ageDigis<CSCWireDigi>(event, m_wireDigiToken, randGen, filteredWireDigis);
0144 
0145   // Don't touch CLCT or ALCT digis
0146   copyDigis<CSCCLCTDigi>(event, m_clctDigiToken, filteredCLCTDigis);
0147   copyDigis<CSCALCTDigi>(event, m_alctDigiToken, filteredALCTDigis);
0148 
0149   event.put(std::move(filteredStripDigis), "MuonCSCStripDigi");
0150   event.put(std::move(filteredWireDigis), "MuonCSCWireDigi");
0151   event.put(std::move(filteredCLCTDigis), "MuonCSCCLCTDigi");
0152   event.put(std::move(filteredALCTDigis), "MuonCSCALCTDigi");
0153 }
0154 
0155 // ------------ method called to copy digis into aged collection  ------------
0156 template <typename T, typename C>
0157 void CSCChamberMasker::copyDigis(edm::Event &event, edm::EDGetTokenT<C> &digiToken, std::unique_ptr<C> &filteredDigis) {
0158   if (!digiToken.isUninitialized()) {
0159     edm::Handle<C> digis;
0160     event.getByToken(digiToken, digis);
0161     for (const auto &j : (*digis)) {
0162       auto digiItr = j.second.first;
0163       auto last = j.second.second;
0164 
0165       CSCDetId const cscDetId = j.first;
0166 
0167       for (; digiItr != last; ++digiItr) {
0168         filteredDigis->insertDigi(cscDetId, *digiItr);
0169       }
0170     }
0171   }
0172 }
0173 
0174 // ------------ method aging digis------------
0175 template <typename T, typename C>
0176 void CSCChamberMasker::ageDigis(edm::Event &event,
0177                                 edm::EDGetTokenT<C> &digiToken,
0178                                 CLHEP::HepRandomEngine &randGen,
0179                                 std::unique_ptr<C> &filteredDigis) {
0180   if (!digiToken.isUninitialized()) {
0181     edm::Handle<C> digis;
0182     event.getByToken(digiToken, digis);
0183 
0184     for (const auto &j : (*digis)) {
0185       auto digiItr = j.second.first;
0186       auto last = j.second.second;
0187 
0188       CSCDetId const cscDetId = j.first;
0189 
0190       // Since lookups are chamber-centric, make new DetId with layer=0
0191       CSCDetId chId = CSCDetId(cscDetId.endcap(), cscDetId.station(), cscDetId.ring(), cscDetId.chamber(), 0);
0192 
0193       for (; digiItr != last; ++digiItr) {
0194         auto chEffIt = m_CSCEffs.find(chId);
0195 
0196         if (chEffIt != m_CSCEffs.end()) {
0197           std::pair<unsigned int, float> typeEff = chEffIt->second;
0198           int type = typeEff.first % 10;   // second digit gives type of inefficiency
0199           int layer = typeEff.first / 10;  // first digit gives layer (0 = chamber level)
0200 
0201           bool doRandomize = false;
0202           if (((std::is_same<T, CSCStripDigi>::value && type == EFF_WIRES) ||
0203                (std::is_same<T, CSCWireDigi>::value && type == EFF_STRIPS) || type == EFF_CHAMBER) &&
0204               (layer == 0 || cscDetId.layer() == layer))
0205             doRandomize = true;
0206 
0207           if (!doRandomize || (randGen.flat() <= typeEff.second)) {
0208             filteredDigis->insertDigi(cscDetId, *digiItr);
0209           }
0210         }
0211       }
0212     }
0213   }
0214 }
0215 
0216 // ------------ method called when starting to processes a run  ------------
0217 void CSCChamberMasker::beginRun(edm::Run const &run, edm::EventSetup const &iSetup) {
0218   m_CSCEffs.clear();
0219 
0220   edm::ESHandle<CSCGeometry> cscGeom = iSetup.getHandle(m_cscGeomToken);
0221 
0222   edm::ESHandle<MuonSystemAging> agingObj = iSetup.getHandle(m_agingObjToken);
0223 
0224   const auto chambers = cscGeom->chambers();
0225 
0226   for (const auto *ch : chambers) {
0227     CSCDetId chId = ch->id();
0228     unsigned int rawId = chId.rawIdMaker(chId.endcap(), chId.station(), chId.ring(), chId.chamber(), 0);
0229     float eff = 1.;
0230     int type = 0;
0231     for (auto &agingPair : agingObj->m_CSCChambEffs) {
0232       if (agingPair.first != rawId)
0233         continue;
0234 
0235       type = agingPair.second.first;
0236       eff = agingPair.second.second;
0237       m_CSCEffs[chId] = std::make_pair(type, eff);
0238       break;
0239     }
0240   }
0241 }
0242 
0243 // ------------ method fills 'descriptions' with the allowed parameters for the
0244 // module  ------------
0245 void CSCChamberMasker::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0246   edm::ParameterSetDescription desc;
0247   desc.add<edm::InputTag>("stripDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCStripDigi"));
0248   desc.add<edm::InputTag>("wireDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCWireDigi"));
0249   desc.add<edm::InputTag>("comparatorDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCComparatorDigi"));
0250   desc.add<edm::InputTag>("rpcDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCRPCDigi"));
0251   desc.add<edm::InputTag>("alctDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCALCTDigi"));
0252   desc.add<edm::InputTag>("clctDigiTag", edm::InputTag("simMuonCSCDigis:MuonCSCCLCTDigi"));
0253   descriptions.add("cscChamberMasker", desc);
0254 }
0255 
0256 // define this as a plug-in
0257 DEFINE_FWK_MODULE(CSCChamberMasker);