Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-25 23:29:57

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