Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-28 03:54:27

0001 #ifndef TOPSINGLELEPTONDQM
0002 #define TOPSINGLELEPTONDQM
0003 
0004 #include "DQMServices/Core/interface/DQMOneEDAnalyzer.h"
0005 #include <string>
0006 #include <vector>
0007 
0008 #include "DataFormats/JetReco/interface/Jet.h"
0009 #include "DQM/Physics/interface/TopDQMHelpers.h"
0010 #include "DataFormats/Common/interface/ValueMap.h"
0011 #include "DataFormats/METReco/interface/CaloMET.h"
0012 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0013 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0014 #include "DataFormats/VertexReco/interface/Vertex.h"
0015 #include "FWCore/Framework/interface/ConsumesCollector.h"
0016 #include "FWCore/Framework/interface/EDConsumerBase.h"
0017 #include "FWCore/Utilities/interface/EDGetToken.h"
0018 #include "JetMETCorrections/JetCorrector/interface/JetCorrector.h"
0019 
0020 /**
0021    \class   MonitorEnsemble TopDQMHelpers.h
0022    "DQM/Physics/interface/TopDQMHelpers.h"
0023 
0024    \brief   Helper class to define histograms for monitoring of
0025    muon/electron/jet/met quantities.
0026 
0027    Helper class to contain histograms for the monitoring of
0028    muon/electron/jet/met quantities.
0029    This class can be instantiated several times after several event selection
0030    steps. It can
0031    be used to fill histograms in three different granularity levels according to
0032    STANDARD
0033    (<10 histograms), VERBOSE(<20 histograms), DEBUG(<30 histgorams). Note that
0034    for the sake
0035    of simplicity and to force the analyst to keep the number of histograms to be
0036    monitored
0037    small the MonitorEnsemble class contains the histograms for all objects at
0038    once. It should
0039    not contain much more than 10 histograms though in the STANDARD
0040    configuration, as these
0041    histograms will be monitored at each SelectionStep. Monitoring of histograms
0042    after selec-
0043    tion steps within the same object collection needs to be implemented within
0044    the Monitor-
0045    Ensemble. It will not be covered by the SelectionStep class.
0046 */
0047 
0048 namespace TopSingleLepton {
0049   using dqm::legacy::DQMStore;
0050   using dqm::legacy::MonitorElement;
0051 
0052   class MonitorEnsemble {
0053   public:
0054     /// different verbosity levels
0055     enum Level { STANDARD, VERBOSE, DEBUG };
0056 
0057   public:
0058     /// default contructor
0059     MonitorEnsemble(const char* label, const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC);
0060     /// default destructor
0061     ~MonitorEnsemble() {}
0062 
0063     /// book histograms in subdirectory _directory_
0064     void book(DQMStore::IBooker& ibooker);
0065     /// fill monitor histograms with electronId and jetCorrections
0066     void fill(const edm::Event& event, const edm::EventSetup& setup);
0067 
0068   private:
0069     /// deduce monitorPath from label, the label is expected
0070     /// to be of type 'selectionPath:monitorPath'
0071     std::string monitorPath(const std::string& label) const { return label.substr(label.find(':') + 1); };
0072     /// deduce selectionPath from label, the label is
0073     /// expected to be of type 'selectionPath:monitorPath'
0074     std::string selectionPath(const std::string& label) const { return label.substr(0, label.find(':')); };
0075 
0076     /// set configurable labels for trigger monitoring histograms
0077     void triggerBinLabels(std::string channel, const std::vector<std::string> labels);
0078     /// fill trigger monitoring histograms
0079     void fill(const edm::Event& event,
0080               const edm::TriggerResults& triggerTable,
0081               std::string channel,
0082               const std::vector<std::string> labels) const;
0083 
0084     /// check if histogram was booked
0085     bool booked(const std::string histName) const { return hists_.find(histName) != hists_.end(); };
0086     /// fill histogram if it had been booked before
0087     void fill(const std::string histName, double value) const {
0088       if (booked(histName))
0089         hists_.find(histName)->second->Fill(value);
0090     };
0091     /// fill histogram if it had been booked before (2-dim version)
0092     void fill(const std::string histName, double xValue, double yValue) const {
0093       if (booked(histName))
0094         hists_.find(histName)->second->Fill(xValue, yValue);
0095     };
0096     /// fill histogram if it had been booked before (2-dim version)
0097     void fill(const std::string histName, double xValue, double yValue, double zValue) const {
0098       if (booked(histName))
0099         hists_.find(histName)->second->Fill(xValue, yValue, zValue);
0100     };
0101 
0102   private:
0103     /// verbosity level for booking
0104     Level verbosity_;
0105     /// instance label
0106     std::string label_;
0107     /// considers a vector of METs
0108     std::vector<edm::EDGetTokenT<edm::View<reco::MET>>> mets_;
0109     /// input sources for monitoring
0110     edm::EDGetTokenT<edm::View<reco::Jet>> jets_;
0111     edm::EDGetTokenT<edm::View<reco::Muon>> muons_;
0112     edm::EDGetTokenT<edm::View<reco::GsfElectron>> elecs_;
0113     edm::EDGetTokenT<edm::View<reco::Vertex>> pvs_;
0114     edm::EDGetTokenT<std::vector<reco::PFJet>> jetToken_;
0115     /// trigger table
0116     edm::EDGetTokenT<edm::TriggerResults> triggerTable_;
0117     /// trigger paths for monitoring, expected
0118     /// to be of form signalPath:MonitorPath
0119     std::vector<std::string> triggerPaths_;
0120 
0121     /// electronId label
0122     edm::EDGetTokenT<edm::ValueMap<float>> electronId_;
0123 
0124     /// electronId pattern we expect the following pattern:
0125     ///  0: fails
0126     ///  1: passes electron ID only
0127     ///  2: passes electron Isolation only
0128     ///  3: passes electron ID and Isolation only
0129     ///  4: passes conversion rejection
0130     ///  5: passes conversion rejection and ID
0131     ///  6: passes conversion rejection and Isolation
0132     ///  7: passes the whole selection
0133     /// As described on
0134     /// https://twiki.cern.ch/twiki/bin/view/CMS/SimpleCutBasedEleID
0135     // int eidPattern_;
0136     // the cut for the MVA Id
0137     double eidCutValue_;
0138     // electron ISO things
0139 
0140     edm::InputTag rhoTag;
0141 
0142     /// extra selection on electrons
0143 
0144     std::unique_ptr<StringCutObjectSelector<reco::GsfElectron>> elecSelect_;
0145 
0146     /// extra selection on primary vertices; meant to investigate the pile-up
0147     /// effect
0148     std::unique_ptr<StringCutObjectSelector<reco::Vertex>> pvSelect_;
0149 
0150     /// extra isolation criterion on muon
0151     std::unique_ptr<StringCutObjectSelector<reco::Muon>> muonIso_;
0152 
0153     /// extra selection on muons
0154     std::unique_ptr<StringCutObjectSelector<reco::Muon>> muonSelect_;
0155 
0156     /// jetCorrector
0157     edm::EDGetTokenT<reco::JetCorrector> jetCorrector_;
0158 
0159     /// jetID as an extra selection type
0160     edm::EDGetTokenT<reco::JetIDValueMap> jetIDLabel_;
0161 
0162     std::unique_ptr<StringCutObjectSelector<reco::JetID>> jetIDSelect_;
0163     /// extra selection on jets
0164     std::string jetSelect_;
0165     std::unique_ptr<StringCutObjectSelector<reco::PFJet>> jetlooseSelection_;
0166     std::unique_ptr<StringCutObjectSelector<reco::PFJet>> jetSelection_;
0167     /// include btag information or not
0168     /// to be determined from the cfg
0169     bool includeBTag_;
0170     /// btag discriminator labels
0171     edm::EDGetTokenT<reco::JetTagCollection> btagEff_, btagPur_, btagVtx_, btagCSV_;
0172     /// btag working points
0173     double btagEffWP_, btagPurWP_, btagVtxWP_, btagCSVWP_;
0174     /// mass window upper and lower edge
0175     double lowerEdge_, upperEdge_;
0176 
0177     /// number of logged interesting events
0178     int logged_;
0179 
0180     /// histogram container
0181     std::map<std::string, MonitorElement*> hists_;
0182     edm::EDConsumerBase tmpConsumerBase;
0183 
0184     std::string directory_;
0185   };
0186 
0187   inline void MonitorEnsemble::triggerBinLabels(std::string channel, const std::vector<std::string> labels) {
0188     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0189       hists_[channel + "Mon_"]->setBinLabel(idx + 1, "[" + monitorPath(labels[idx]) + "]", 1);
0190       hists_[channel + "Eff_"]->setBinLabel(
0191           idx + 1, "[" + selectionPath(labels[idx]) + "]|[" + monitorPath(labels[idx]) + "]", 1);
0192     }
0193   }
0194 
0195   inline void MonitorEnsemble::fill(const edm::Event& event,
0196                                     const edm::TriggerResults& triggerTable,
0197                                     std::string channel,
0198                                     const std::vector<std::string> labels) const {
0199     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0200       if (accept(event, triggerTable, monitorPath(labels[idx]))) {
0201         fill(channel + "Mon_", idx + 0.5);
0202         // take care to fill triggerMon_ before evts is being called
0203         int evts = hists_.find(channel + "Mon_")->second->getBinContent(idx + 1);
0204         double value = hists_.find(channel + "Eff_")->second->getBinContent(idx + 1);
0205         fill(
0206             channel + "Eff_", idx + 0.5, 1. / evts * (accept(event, triggerTable, selectionPath(labels[idx])) - value));
0207       }
0208     }
0209   }
0210 }  // namespace TopSingleLepton
0211 
0212 #include <utility>
0213 
0214 #include "DQM/Physics/interface/TopDQMHelpers.h"
0215 #include "FWCore/Framework/interface/Frameworkfwd.h"
0216 #include "FWCore/ServiceRegistry/interface/Service.h"
0217 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0218 
0219 #include "FWCore/Common/interface/TriggerNames.h"
0220 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0221 #include "DataFormats/VertexReco/interface/Vertex.h"
0222 #include "DataFormats/Common/interface/TriggerResults.h"
0223 
0224 /**
0225    \class   TopSingleLeptonDQM TopSingleLeptonDQM.h
0226    "DQM/Physics/plugins/TopSingleLeptonDQM.h"
0227 
0228    \brief   Module to apply a monitored selection of top like events in the
0229    semi-leptonic channel
0230 
0231    Plugin to apply a monitored selection of top like events with some minimal
0232    flexibility
0233    in the number and definition of the selection steps. To achieve this
0234    flexibility it
0235    employes the SelectionStep class. The MonitorEnsemble class is used to
0236    provide a well
0237    defined set of histograms to be monitored after each selection step. The
0238    SelectionStep
0239    class provides a flexible and intuitive selection via the StringCutParser.
0240    SelectionStep
0241    and MonitorEnsemble classes are interleaved. The monitoring starts after a
0242    preselection
0243    step (which is not monitored in the context of this module) with an instance
0244    of the
0245    MonitorEnsemble class. The following objects are supported for selection:
0246 
0247     - jets  : of type reco::Jet (jets), reco::CaloJet (jets/calo) or reco::PFJet
0248    (jets/pflow)
0249     - elecs : of type reco::GsfElectron
0250     - muons : of type reco::Muon
0251     - met   : of type reco::MET
0252 
0253    These types have to be present as prefix of the selection step paramter
0254    _label_ separated
0255    from the rest of the label by a ':' (e.g. in the form "jets:step0"). The
0256    class expects
0257    selection labels of this type. They will be disentangled by the private
0258    helper functions
0259    _objectType_ and _seletionStep_ as declared below.
0260 */
0261 
0262 /// define MonitorEnsembple to be used
0263 // using TopSingleLepton::MonitorEnsemble;
0264 
0265 class TopSingleLeptonDQM : public DQMOneEDAnalyzer<> {
0266 public:
0267   /// default constructor
0268   TopSingleLeptonDQM(const edm::ParameterSet& cfg);
0269   /// default destructor
0270   ~TopSingleLeptonDQM() override {}
0271 
0272   /// do this during the event loop
0273   void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0274 
0275 protected:
0276   //Book histograms
0277   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0278 
0279 private:
0280   /// deduce object type from ParameterSet label, the label
0281   /// is expected to be of type 'objectType:selectionStep'
0282   std::string objectType(const std::string& label) { return label.substr(0, label.find(':')); };
0283   /// deduce selection step from ParameterSet label, the
0284   /// label is expected to be of type 'objectType:selectionStep'
0285   std::string selectionStep(const std::string& label) { return label.substr(label.find(':') + 1); };
0286 
0287 private:
0288   /// trigger table
0289   edm::EDGetTokenT<edm::TriggerResults> triggerTable__;
0290   /// trigger paths
0291   std::vector<std::string> triggerPaths_;
0292   /// string cut selector
0293   std::unique_ptr<StringCutObjectSelector<reco::Vertex>> vertexSelect_;
0294 
0295   /// beamspot
0296   edm::InputTag beamspot_;
0297   edm::EDGetTokenT<reco::BeamSpot> beamspot__;
0298   /// string cut selector
0299   std::unique_ptr<StringCutObjectSelector<reco::BeamSpot>> beamspotSelect_;
0300 
0301   /// needed to guarantee the selection order as defined by the order of
0302   /// ParameterSets in the _selection_ vector as defined in the config
0303   std::vector<std::string> selectionOrder_;
0304   /// this is the heart component of the plugin; std::string keeps a label
0305   /// the selection step for later identification, edm::ParameterSet keeps
0306   /// the configuration of the selection for the SelectionStep class,
0307   /// MonitoringEnsemble keeps an instance of the MonitorEnsemble class to
0308   /// be filled _after_ each selection step
0309   std::map<std::string, std::pair<edm::ParameterSet, std::unique_ptr<TopSingleLepton::MonitorEnsemble>>> selection_;
0310   std::unique_ptr<SelectionStep<reco::Muon>> MuonStep;
0311   std::unique_ptr<SelectionStep<reco::GsfElectron>> ElectronStep;
0312   std::unique_ptr<SelectionStep<reco::Vertex>> PvStep;
0313   std::unique_ptr<SelectionStep<reco::MET>> METStep;
0314   std::vector<std::unique_ptr<SelectionStep<reco::Jet>>> JetSteps;
0315   std::vector<std::unique_ptr<SelectionStep<reco::CaloJet>>> CaloJetSteps;
0316   std::vector<std::unique_ptr<SelectionStep<reco::PFJet>>> PFJetSteps;
0317 
0318   std::vector<edm::ParameterSet> sel_;
0319   edm::ParameterSet setup_;
0320 };
0321 
0322 #endif
0323 
0324 /* Local Variables: */
0325 /* show-trailing-whitespace: t */
0326 /* truncate-lines: t */
0327 /* End: */