Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-14 23:39:17

0001 // -*- C++ -*-
0002 //#define EDM_ML_DEBUG
0003 
0004 // system include files
0005 #include <atomic>
0006 #include <memory>
0007 #include <string>
0008 #include <cmath>
0009 #include <iostream>
0010 #include <sstream>
0011 #include <fstream>
0012 #include <vector>
0013 #include <boost/regex.hpp>
0014 
0015 // user include files
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/Framework/interface/stream/EDProducer.h"
0018 #include "FWCore/Framework/interface/Event.h"
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/Framework/interface/Run.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025 
0026 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0027 #include "DataFormats/Common/interface/Handle.h"
0028 #include "DataFormats/Common/interface/Ref.h"
0029 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0030 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
0031 #include "DataFormats/MuonReco/interface/Muon.h"
0032 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0033 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0034 #include "DataFormats/VertexReco/interface/Vertex.h"
0035 
0036 //#define EDM_ML_DEBUG
0037 //
0038 // class declaration
0039 //
0040 
0041 namespace AlCaHBHEMuons {
0042   struct Counters {
0043     Counters() : nAll_(0), nGood_(0) {}
0044     mutable std::atomic<unsigned int> nAll_, nGood_;
0045   };
0046 }  // namespace AlCaHBHEMuons
0047 
0048 class AlCaHBHEMuonProducer : public edm::stream::EDProducer<edm::GlobalCache<AlCaHBHEMuons::Counters> > {
0049 public:
0050   explicit AlCaHBHEMuonProducer(edm::ParameterSet const&, const AlCaHBHEMuons::Counters* count);
0051   ~AlCaHBHEMuonProducer() override;
0052 
0053   static std::unique_ptr<AlCaHBHEMuons::Counters> initializeGlobalCache(edm::ParameterSet const&) {
0054     return std::make_unique<AlCaHBHEMuons::Counters>();
0055   }
0056 
0057   void produce(edm::Event&, const edm::EventSetup&) override;
0058   void endStream() override;
0059   static void globalEndJob(const AlCaHBHEMuons::Counters* counters);
0060   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0061 
0062 private:
0063   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0064   void endRun(edm::Run const&, edm::EventSetup const&) override;
0065   bool select(const reco::MuonCollection&);
0066 
0067   // ----------member data ---------------------------
0068   unsigned int nRun_, nAll_, nGood_;
0069   const edm::InputTag labelBS_, labelVtx_;
0070   const edm::InputTag labelEB_, labelEE_, labelHBHE_, labelMuon_;
0071   const double pMuonMin_;
0072 
0073   edm::EDGetTokenT<reco::BeamSpot> tok_BS_;
0074   edm::EDGetTokenT<reco::VertexCollection> tok_Vtx_;
0075   edm::EDGetTokenT<EcalRecHitCollection> tok_EB_;
0076   edm::EDGetTokenT<EcalRecHitCollection> tok_EE_;
0077   edm::EDGetTokenT<HBHERecHitCollection> tok_HBHE_;
0078   edm::EDGetTokenT<reco::MuonCollection> tok_Muon_;
0079 };
0080 
0081 AlCaHBHEMuonProducer::AlCaHBHEMuonProducer(edm::ParameterSet const& iConfig, const AlCaHBHEMuons::Counters* count)
0082     : nRun_(0),
0083       nAll_(0),
0084       nGood_(0),
0085       labelBS_(iConfig.getParameter<edm::InputTag>("BeamSpotLabel")),
0086       labelVtx_(iConfig.getParameter<edm::InputTag>("VertexLabel")),
0087       labelEB_(iConfig.getParameter<edm::InputTag>("EBRecHitLabel")),
0088       labelEE_(iConfig.getParameter<edm::InputTag>("EERecHitLabel")),
0089       labelHBHE_(iConfig.getParameter<edm::InputTag>("HBHERecHitLabel")),
0090       labelMuon_(iConfig.getParameter<edm::InputTag>("MuonLabel")),
0091       pMuonMin_(iConfig.getParameter<double>("MinimumMuonP")) {
0092   // define tokens for access
0093   tok_Vtx_ = consumes<reco::VertexCollection>(labelVtx_);
0094   tok_BS_ = consumes<reco::BeamSpot>(labelBS_);
0095   tok_EB_ = consumes<EcalRecHitCollection>(labelEB_);
0096   tok_EE_ = consumes<EcalRecHitCollection>(labelEE_);
0097   tok_HBHE_ = consumes<HBHERecHitCollection>(labelHBHE_);
0098   tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
0099 
0100   edm::LogVerbatim("HcalHBHEMuon") << "Parameters read from config file \n"
0101                                    << "\t minP of muon " << pMuonMin_ << "\t input labels " << labelBS_ << " "
0102                                    << labelVtx_ << " " << labelEB_ << " " << labelEE_ << " " << labelHBHE_ << " "
0103                                    << labelMuon_;
0104 
0105   //saves the following collections
0106   produces<reco::BeamSpot>(labelBS_.label());
0107   produces<reco::VertexCollection>(labelVtx_.label());
0108   produces<EcalRecHitCollection>(labelEB_.instance());
0109   produces<EcalRecHitCollection>(labelEE_.instance());
0110   produces<HBHERecHitCollection>(labelHBHE_.label());
0111   produces<reco::MuonCollection>(labelMuon_.label());
0112 }
0113 
0114 AlCaHBHEMuonProducer::~AlCaHBHEMuonProducer() {}
0115 
0116 void AlCaHBHEMuonProducer::produce(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0117   ++nAll_;
0118   bool valid(true);
0119 #ifdef EDM_ML_DEBUG
0120   edm::LogVerbatim("HcalHBHEMuon") << "AlCaHBHEMuonProducer::Run " << iEvent.id().run() << " Event "
0121                                    << iEvent.id().event() << " Luminosity " << iEvent.luminosityBlock() << " Bunch "
0122                                    << iEvent.bunchCrossing();
0123 #endif
0124 
0125   //Step1: Get all the relevant containers
0126   auto bmspot = iEvent.getHandle(tok_BS_);
0127   if (!bmspot.isValid()) {
0128     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelBS_;
0129     valid = false;
0130   }
0131 
0132   auto vt = iEvent.getHandle(tok_Vtx_);
0133   if (!vt.isValid()) {
0134     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelVtx_;
0135     valid = false;
0136   }
0137 
0138   auto barrelRecHitsHandle = iEvent.getHandle(tok_EB_);
0139   if (!barrelRecHitsHandle.isValid()) {
0140     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEB_;
0141     valid = false;
0142   }
0143 
0144   auto endcapRecHitsHandle = iEvent.getHandle(tok_EE_);
0145   if (!endcapRecHitsHandle.isValid()) {
0146     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelEE_;
0147     valid = false;
0148   }
0149 
0150   auto hbhe = iEvent.getHandle(tok_HBHE_);
0151   if (!hbhe.isValid()) {
0152     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelHBHE_;
0153     valid = false;
0154   }
0155 
0156   auto muonhandle = iEvent.getHandle(tok_Muon_);
0157   if (!muonhandle.isValid()) {
0158     edm::LogWarning("HcalHBHEMuon") << "AlCaHBHEMuonProducer: Error! can't get product " << labelMuon_;
0159     valid = false;
0160   }
0161 
0162 #ifdef EDM_ML_DEBUG
0163   edm::LogVerbatim("HcalHBHEMuon") << "AlCaHBHEMuonProducer::obtained the collections with validity flag " << valid;
0164 #endif
0165 
0166   //For accepted events
0167   auto outputBeamSpot = std::make_unique<reco::BeamSpot>();
0168   auto outputVColl = std::make_unique<reco::VertexCollection>();
0169   auto outputEBColl = std::make_unique<EBRecHitCollection>();
0170   auto outputEEColl = std::make_unique<EERecHitCollection>();
0171   auto outputHBHEColl = std::make_unique<HBHERecHitCollection>();
0172   auto outputMColl = std::make_unique<reco::MuonCollection>();
0173 
0174   if (valid) {
0175     const reco::BeamSpot beam = *(bmspot.product());
0176     outputBeamSpot = std::make_unique<reco::BeamSpot>(
0177         beam.position(), beam.sigmaZ(), beam.dxdz(), beam.dydz(), beam.BeamWidthX(), beam.covariance(), beam.type());
0178     const reco::VertexCollection vtx = *(vt.product());
0179     const EcalRecHitCollection ebcoll = *(barrelRecHitsHandle.product());
0180     const EcalRecHitCollection eecoll = *(endcapRecHitsHandle.product());
0181     const HBHERecHitCollection hbhecoll = *(hbhe.product());
0182     const reco::MuonCollection muons = *(muonhandle.product());
0183 
0184     bool accept = select(muons);
0185 
0186     if (accept) {
0187       ++nGood_;
0188 
0189       for (reco::VertexCollection::const_iterator vtr = vtx.begin(); vtr != vtx.end(); ++vtr)
0190         outputVColl->push_back(*vtr);
0191 
0192       for (edm::SortedCollection<EcalRecHit>::const_iterator ehit = ebcoll.begin(); ehit != ebcoll.end(); ++ehit)
0193         outputEBColl->push_back(*ehit);
0194 
0195       for (edm::SortedCollection<EcalRecHit>::const_iterator ehit = eecoll.begin(); ehit != eecoll.end(); ++ehit)
0196         outputEEColl->push_back(*ehit);
0197 
0198       for (std::vector<HBHERecHit>::const_iterator hhit = hbhecoll.begin(); hhit != hbhecoll.end(); ++hhit)
0199         outputHBHEColl->push_back(*hhit);
0200 
0201       for (reco::MuonCollection::const_iterator muon = muons.begin(); muon != muons.end(); ++muon)
0202         outputMColl->push_back(*muon);
0203     }
0204   }
0205 
0206   iEvent.put(std::move(outputBeamSpot), labelBS_.label());
0207   iEvent.put(std::move(outputVColl), labelVtx_.label());
0208   iEvent.put(std::move(outputEBColl), labelEB_.instance());
0209   iEvent.put(std::move(outputEEColl), labelEE_.instance());
0210   iEvent.put(std::move(outputHBHEColl), labelHBHE_.label());
0211   iEvent.put(std::move(outputMColl), labelMuon_.label());
0212 }
0213 
0214 void AlCaHBHEMuonProducer::endStream() {
0215   globalCache()->nAll_ += nAll_;
0216   globalCache()->nGood_ += nGood_;
0217 }
0218 
0219 void AlCaHBHEMuonProducer::globalEndJob(const AlCaHBHEMuons::Counters* count) {
0220   edm::LogVerbatim("HcalHBHEMuon") << "Finds " << count->nGood_ << " good tracks in " << count->nAll_ << " events";
0221 }
0222 
0223 void AlCaHBHEMuonProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0224   //The following says we do not know what parameters are allowed so do no validation
0225   // Please change this to state exactly what you do use, even if it is no parameters
0226   edm::ParameterSetDescription desc;
0227   desc.add<edm::InputTag>("BeamSpotLabel", edm::InputTag("offlineBeamSpot"));
0228   desc.add<edm::InputTag>("VertexLabel", edm::InputTag("offlinePrimaryVertices"));
0229   desc.add<edm::InputTag>("EBRecHitLabel", edm::InputTag("ecalRecHit", "EcalRecHitsEB"));
0230   desc.add<edm::InputTag>("EERecHitLabel", edm::InputTag("ecalRecHit", "EcalRecHitsEE"));
0231   desc.add<edm::InputTag>("HBHERecHitLabel", edm::InputTag("hbhereco"));
0232   desc.add<edm::InputTag>("MuonLabel", edm::InputTag("muons"));
0233   desc.add<double>("MinimumMuonP", 10.0);
0234   descriptions.add("alcaHBHEMuonProducer", desc);
0235 }
0236 
0237 void AlCaHBHEMuonProducer::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0238   edm::LogVerbatim("HcalHBHEMuon") << "Run[" << nRun_ << "] " << iRun.run();
0239 }
0240 
0241 void AlCaHBHEMuonProducer::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0242   ++nRun_;
0243   edm::LogVerbatim("HcalHBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run();
0244 }
0245 
0246 bool AlCaHBHEMuonProducer::select(const reco::MuonCollection& muons) {
0247   bool ok(false);
0248   for (unsigned int k = 0; k < muons.size(); ++k) {
0249     if (muons[k].p() > pMuonMin_) {
0250       ok = true;
0251       break;
0252     }
0253   }
0254   return ok;
0255 }
0256 
0257 #include "FWCore/Framework/interface/MakerMacros.h"
0258 
0259 DEFINE_FWK_MODULE(AlCaHBHEMuonProducer);