Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:13:24

0001 #include "FWCore/Utilities/interface/EDGetToken.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0004 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/EventSetup.h"
0007 #include "FWCore/Framework/interface/stream/EDProducer.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0010 #include "DataFormats/Common/interface/ValueMap.h"
0011 #include "DataFormats/MuonReco/interface/Muon.h"
0012 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0013 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0014 #include "DataFormats/VertexReco/interface/Vertex.h"
0015 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0016 
0017 class HLTDQMMuonSelector : public edm::stream::EDProducer<> {
0018 public:
0019   explicit HLTDQMMuonSelector(const edm::ParameterSet& config);
0020   void produce(edm::Event&, edm::EventSetup const&) override;
0021   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0022 
0023 private:
0024   enum class MuonSelectionType { Tight, Medium, Loose, Soft, HighPt, None };
0025 
0026   static MuonSelectionType convertToEnum(const std::string& val);
0027   bool passMuonSel(const reco::Muon& muon, const reco::Vertex& vertex) const;
0028 
0029   edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0030   edm::EDGetTokenT<reco::VertexCollection> vtxToken_;
0031   StringCutObjectSelector<reco::Muon, true> selection_;
0032 
0033   MuonSelectionType muonSelType_;
0034 };
0035 
0036 HLTDQMMuonSelector::HLTDQMMuonSelector(const edm::ParameterSet& config)
0037     : muonToken_(consumes<reco::MuonCollection>(config.getParameter<edm::InputTag>("objs"))),
0038       vtxToken_(consumes<reco::VertexCollection>(config.getParameter<edm::InputTag>("vertices"))),
0039       selection_(config.getParameter<std::string>("selection")),
0040       muonSelType_(convertToEnum(config.getParameter<std::string>("muonSelectionType"))) {
0041   produces<edm::ValueMap<bool> >();
0042 }
0043 
0044 void HLTDQMMuonSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0045   edm::ParameterSetDescription desc;
0046   desc.add<edm::InputTag>("objs", edm::InputTag("muons"));
0047   desc.add<edm::InputTag>("vertices", edm::InputTag("offlinePrimaryVertices"));
0048   desc.add<std::string>("selection", "et > 5");
0049   desc.add<std::string>("muonSelectionType", "tight");
0050   descriptions.add("hltDQMMuonSelector", desc);
0051 }
0052 
0053 void HLTDQMMuonSelector::produce(edm::Event& event, const edm::EventSetup& setup) {
0054   edm::Handle<reco::MuonCollection> muonHandle;
0055   event.getByToken(muonToken_, muonHandle);
0056 
0057   edm::Handle<reco::VertexCollection> vtxHandle;
0058   event.getByToken(vtxToken_, vtxHandle);
0059 
0060   if (!muonHandle.isValid())
0061     return;
0062 
0063   std::vector<bool> selResults;
0064   for (auto& muon : *muonHandle) {
0065     if (vtxHandle.isValid() && !vtxHandle->empty()) {
0066       selResults.push_back(passMuonSel(muon, vtxHandle->front()) && selection_(muon));
0067     } else {
0068       selResults.push_back(false);
0069     }
0070   }
0071   auto valMap = std::make_unique<edm::ValueMap<bool> >();
0072   edm::ValueMap<bool>::Filler filler(*valMap);
0073   filler.insert(muonHandle, selResults.begin(), selResults.end());
0074   filler.fill();
0075   event.put(std::move(valMap));
0076 }
0077 
0078 HLTDQMMuonSelector::MuonSelectionType HLTDQMMuonSelector::convertToEnum(const std::string& val) {
0079   const std::vector<std::pair<std::string, MuonSelectionType> > strsToEnums = {{"tight", MuonSelectionType::Tight},
0080                                                                                {"medium", MuonSelectionType::Medium},
0081                                                                                {"loose", MuonSelectionType::Loose},
0082                                                                                {"soft", MuonSelectionType::Soft},
0083                                                                                {"highpt", MuonSelectionType::HighPt},
0084                                                                                {"none", MuonSelectionType::None}};
0085   for (const auto& strEnumPair : strsToEnums) {
0086     if (val == strEnumPair.first)
0087       return strEnumPair.second;
0088   }
0089   std::ostringstream validEnums;
0090   for (const auto& strEnumPair : strsToEnums)
0091     validEnums << strEnumPair.first << " ";
0092   throw cms::Exception("InvalidConfig") << "invalid muonSelectionType " << val << ", allowed values are "
0093                                         << validEnums.str();
0094 }
0095 
0096 bool HLTDQMMuonSelector::passMuonSel(const reco::Muon& muon, const reco::Vertex& vertex) const {
0097   switch (muonSelType_) {
0098     case MuonSelectionType::Tight:
0099       return muon::isTightMuon(muon, vertex);
0100     case MuonSelectionType::Medium:
0101       return muon::isMediumMuon(muon);
0102     case MuonSelectionType::Loose:
0103       return muon::isLooseMuon(muon);
0104     case MuonSelectionType::Soft:
0105       return muon::isSoftMuon(muon, vertex);
0106     case MuonSelectionType::HighPt:
0107       return muon::isHighPtMuon(muon, vertex);
0108     case MuonSelectionType::None:
0109       return true;
0110     default:
0111       edm::LogError("HLTDQMMuonSelector") << " inconsistent code, an option has been added to MuonSelectionType "
0112                                              "without updating HLTDQMMuonSelector::passMuonSel";
0113       return false;
0114   }
0115 }
0116 
0117 #include "FWCore/Framework/interface/MakerMacros.h"
0118 DEFINE_FWK_MODULE(HLTDQMMuonSelector);