Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-01-13 02:34:40

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