Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:10

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::PFCandidate> > muons_;
0112     edm::EDGetTokenT<edm::View<reco::PFCandidate> > elecs_;
0113     edm::EDGetTokenT<edm::View<reco::Vertex> > pvs_;
0114     /// trigger table
0115     edm::EDGetTokenT<edm::TriggerResults> triggerTable_;
0116     /// trigger paths for monitoring, expected
0117     /// to be of form signalPath:MonitorPath
0118     std::vector<std::string> triggerPaths_;
0119 
0120     /// electronId label
0121     edm::EDGetTokenT<edm::ValueMap<float> > electronId_;
0122 
0123     /// electronId pattern we expect the following pattern:
0124     ///  0: fails
0125     ///  1: passes electron ID only
0126     ///  2: passes electron Isolation only
0127     ///  3: passes electron ID and Isolation only
0128     ///  4: passes conversion rejection
0129     ///  5: passes conversion rejection and ID
0130     ///  6: passes conversion rejection and Isolation
0131     ///  7: passes the whole selection
0132     /// As described on
0133     /// https://twiki.cern.ch/twiki/bin/view/CMS/SimpleCutBasedEleID
0134     // int eidPattern_;
0135     // the cut for the MVA Id
0136     double eidCutValue_;
0137     // electron ISO things
0138 
0139     edm::InputTag rhoTag;
0140 
0141     /// extra selection on electrons
0142 
0143     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate> > elecSelect_;
0144 
0145     /// extra selection on primary vertices; meant to investigate the pile-up
0146     /// effect
0147     std::unique_ptr<StringCutObjectSelector<reco::Vertex> > pvSelect_;
0148 
0149     /// extra isolation criterion on muon
0150     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate> > muonIso_;
0151 
0152     /// extra selection on muons
0153     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate> > muonSelect_;
0154 
0155     /// jetCorrector
0156     edm::EDGetTokenT<reco::JetCorrector> jetCorrector_;
0157 
0158     /// jetID as an extra selection type
0159     edm::EDGetTokenT<reco::JetIDValueMap> jetIDLabel_;
0160 
0161     std::unique_ptr<StringCutObjectSelector<reco::JetID> > jetIDSelect_;
0162     /// extra selection on jets
0163     std::string jetSelect_;
0164     std::unique_ptr<StringCutObjectSelector<reco::PFJet> > jetlooseSelection_;
0165     std::unique_ptr<StringCutObjectSelector<reco::PFJet> > jetSelection_;
0166     /// include btag information or not
0167     /// to be determined from the cfg
0168     bool includeBTag_;
0169     /// btag discriminator labels
0170     edm::EDGetTokenT<reco::JetTagCollection> btagEff_, btagPur_, btagVtx_, btagCSV_;
0171     /// btag working points
0172     double btagEffWP_, btagPurWP_, btagVtxWP_, btagCSVWP_;
0173     /// mass window upper and lower edge
0174     double lowerEdge_, upperEdge_;
0175 
0176     /// number of logged interesting events
0177     int logged_;
0178 
0179     /// histogram container
0180     std::map<std::string, MonitorElement*> hists_;
0181     edm::EDConsumerBase tmpConsumerBase;
0182 
0183     std::string directory_;
0184   };
0185 
0186   inline void MonitorEnsemble::triggerBinLabels(std::string channel, const std::vector<std::string> labels) {
0187     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0188       hists_[channel + "Mon_"]->setBinLabel(idx + 1, "[" + monitorPath(labels[idx]) + "]", 1);
0189       hists_[channel + "Eff_"]->setBinLabel(
0190           idx + 1, "[" + selectionPath(labels[idx]) + "]|[" + monitorPath(labels[idx]) + "]", 1);
0191     }
0192   }
0193 
0194   inline void MonitorEnsemble::fill(const edm::Event& event,
0195                                     const edm::TriggerResults& triggerTable,
0196                                     std::string channel,
0197                                     const std::vector<std::string> labels) const {
0198     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0199       if (accept(event, triggerTable, monitorPath(labels[idx]))) {
0200         fill(channel + "Mon_", idx + 0.5);
0201         // take care to fill triggerMon_ before evts is being called
0202         int evts = hists_.find(channel + "Mon_")->second->getBinContent(idx + 1);
0203         double value = hists_.find(channel + "Eff_")->second->getBinContent(idx + 1);
0204         fill(
0205             channel + "Eff_", idx + 0.5, 1. / evts * (accept(event, triggerTable, selectionPath(labels[idx])) - value));
0206       }
0207     }
0208   }
0209 }  // namespace TopSingleLepton
0210 
0211 #include <utility>
0212 
0213 #include "DQM/Physics/interface/TopDQMHelpers.h"
0214 #include "FWCore/Framework/interface/Frameworkfwd.h"
0215 #include "FWCore/ServiceRegistry/interface/Service.h"
0216 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0217 
0218 #include "FWCore/Common/interface/TriggerNames.h"
0219 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0220 #include "DataFormats/VertexReco/interface/Vertex.h"
0221 #include "DataFormats/Common/interface/TriggerResults.h"
0222 
0223 /**
0224    \class   TopSingleLeptonDQM TopSingleLeptonDQM.h
0225    "DQM/Physics/plugins/TopSingleLeptonDQM.h"
0226 
0227    \brief   Module to apply a monitored selection of top like events in the
0228    semi-leptonic channel
0229 
0230    Plugin to apply a monitored selection of top like events with some minimal
0231    flexibility
0232    in the number and definition of the selection steps. To achieve this
0233    flexibility it
0234    employes the SelectionStep class. The MonitorEnsemble class is used to
0235    provide a well
0236    defined set of histograms to be monitored after each selection step. The
0237    SelectionStep
0238    class provides a flexible and intuitive selection via the StringCutParser.
0239    SelectionStep
0240    and MonitorEnsemble classes are interleaved. The monitoring starts after a
0241    preselection
0242    step (which is not monitored in the context of this module) with an instance
0243    of the
0244    MonitorEnsemble class. The following objects are supported for selection:
0245 
0246     - jets  : of type reco::Jet (jets), reco::CaloJet (jets/calo) or reco::PFJet
0247    (jets/pflow)
0248     - elecs : of type reco::PFCandidate
0249     - muons : of type reco::PFCandidate
0250     - met   : of type reco::MET
0251 
0252    These types have to be present as prefix of the selection step paramter
0253    _label_ separated
0254    from the rest of the label by a ':' (e.g. in the form "jets:step0"). The
0255    class expects
0256    selection labels of this type. They will be disentangled by the private
0257    helper functions
0258    _objectType_ and _seletionStep_ as declared below.
0259 */
0260 
0261 /// define MonitorEnsembple to be used
0262 // using TopSingleLepton::MonitorEnsemble;
0263 
0264 class TopSingleLeptonDQM : public DQMOneEDAnalyzer<> {
0265 public:
0266   /// default constructor
0267   TopSingleLeptonDQM(const edm::ParameterSet& cfg);
0268   /// default destructor
0269   ~TopSingleLeptonDQM() override{};
0270 
0271   /// do this during the event loop
0272   void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0273 
0274 protected:
0275   //Book histograms
0276   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0277 
0278 private:
0279   /// deduce object type from ParameterSet label, the label
0280   /// is expected to be of type 'objectType:selectionStep'
0281   std::string objectType(const std::string& label) { return label.substr(0, label.find(':')); };
0282   /// deduce selection step from ParameterSet label, the
0283   /// label is expected to be of type 'objectType:selectionStep'
0284   std::string selectionStep(const std::string& label) { return label.substr(label.find(':') + 1); };
0285 
0286 private:
0287   /// trigger table
0288   edm::EDGetTokenT<edm::TriggerResults> triggerTable__;
0289   /// trigger paths
0290   std::vector<std::string> triggerPaths_;
0291   /// string cut selector
0292   std::unique_ptr<StringCutObjectSelector<reco::Vertex> > vertexSelect_;
0293 
0294   /// beamspot
0295   edm::InputTag beamspot_;
0296   edm::EDGetTokenT<reco::BeamSpot> beamspot__;
0297   /// string cut selector
0298   std::unique_ptr<StringCutObjectSelector<reco::BeamSpot> > beamspotSelect_;
0299 
0300   /// needed to guarantee the selection order as defined by the order of
0301   /// ParameterSets in the _selection_ vector as defined in the config
0302   std::vector<std::string> selectionOrder_;
0303   /// this is the heart component of the plugin; std::string keeps a label
0304   /// the selection step for later identification, edm::ParameterSet keeps
0305   /// the configuration of the selection for the SelectionStep class,
0306   /// MonitoringEnsemble keeps an instance of the MonitorEnsemble class to
0307   /// be filled _after_ each selection step
0308   std::map<std::string, std::pair<edm::ParameterSet, std::unique_ptr<TopSingleLepton::MonitorEnsemble> > > selection_;
0309   std::unique_ptr<SelectionStep<reco::PFCandidate> > MuonStep;
0310   std::unique_ptr<SelectionStep<reco::PFCandidate> > ElectronStep;
0311   std::unique_ptr<SelectionStep<reco::Vertex> > PvStep;
0312   std::unique_ptr<SelectionStep<reco::MET> > METStep;
0313   std::vector<std::unique_ptr<SelectionStep<reco::Jet> > > JetSteps;
0314   std::vector<std::unique_ptr<SelectionStep<reco::CaloJet> > > CaloJetSteps;
0315   std::vector<std::unique_ptr<SelectionStep<reco::PFJet> > > PFJetSteps;
0316 
0317   std::vector<edm::ParameterSet> sel_;
0318   edm::ParameterSet setup_;
0319 };
0320 
0321 #endif
0322 
0323 /* Local Variables: */
0324 /* show-trailing-whitespace: t */
0325 /* truncate-lines: t */
0326 /* End: */