Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:32:59

0001 #ifndef TOPDILEPTONOFFLINEDQM
0002 #define TOPDILEPTONOFFLINEDQM
0003 
0004 #include "DQMServices/Core/interface/DQMOneEDAnalyzer.h"
0005 
0006 #include <string>
0007 #include <vector>
0008 
0009 #include "DataFormats/Math/interface/deltaR.h"
0010 #include "DataFormats/JetReco/interface/Jet.h"
0011 #include "DQM/Physics/interface/TopDQMHelpers.h"
0012 #include "DataFormats/Common/interface/ValueMap.h"
0013 #include "DataFormats/METReco/interface/CaloMET.h"
0014 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0015 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
0016 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0017 #include "FWCore/Utilities/interface/EDGetToken.h"
0018 #include "FWCore/Framework/interface/EDConsumerBase.h"
0019 #include "FWCore/Framework/interface/ConsumesCollector.h"
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 TopDiLeptonOffline {
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     /// make clear which LorentzVector to use
0057     /// for jet, electrons and muon buffering
0058     typedef reco::LeafCandidate::LorentzVector LorentzVector;
0059     /// different decay channels
0060     enum DecayChannel { NONE, DIMUON, DIELEC, ELECMU };
0061 
0062   public:
0063     /// default contructor
0064     /// MonitorEnsemble(const char* label, const edm::ParameterSet& cfg);
0065     MonitorEnsemble(const char* label, const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC);
0066     /// default destructor
0067     ~MonitorEnsemble(){};
0068 
0069     /// book histograms in subdirectory _directory_
0070     void book(DQMStore::IBooker& ibooker);
0071     /// fill monitor histograms with electronId and jetCorrections
0072     void fill(const edm::Event& event, const edm::EventSetup& setup);
0073 
0074   private:
0075     /// deduce monitorPath from label, the label is expected
0076     /// to be of type 'selectionPath:monitorPath'
0077     std::string monitorPath(const std::string& label) const { return label.substr(label.find(':') + 1); };
0078     /// deduce selectionPath from label, the label is
0079     /// expected to be of type 'selectionPath:monitorPath'
0080     std::string selectionPath(const std::string& label) const { return label.substr(0, label.find(':')); };
0081     /// determine dileptonic decay channel
0082     DecayChannel decayChannel(const std::vector<const reco::PFCandidate*>& muons,
0083                               const std::vector<const reco::PFCandidate*>& elecs) const;
0084 
0085     /// set labels for event logging histograms
0086     void loggerBinLabels(std::string hist);
0087     /// set configurable labels for trigger monitoring histograms
0088     void triggerBinLabels(std::string channel, const std::vector<std::string> labels);
0089     /// fill trigger monitoring histograms
0090     void fill(const edm::Event& event,
0091               const edm::TriggerResults& triggerTable,
0092               std::string channel,
0093               const std::vector<std::string> labels) const;
0094 
0095     /// check if histogram was booked
0096     bool booked(const std::string histName) const { return hists_.find(histName) != hists_.end(); };
0097     /// fill histogram if it had been booked before
0098     void fill(const std::string histName, double value) const {
0099       if (booked(histName))
0100         hists_.find(histName)->second->Fill(value);
0101     };
0102     /// fill histogram if it had been booked before (2-dim version)
0103     void fill(const std::string histName, double xValue, double yValue) const {
0104       if (booked(histName))
0105         hists_.find(histName)->second->Fill(xValue, yValue);
0106     };
0107     /// fill histogram if it had been booked before (2-dim version)
0108     void fill(const std::string histName, double xValue, double yValue, double zValue) const {
0109       if (booked(histName))
0110         hists_.find(histName)->second->Fill(xValue, yValue, zValue);
0111     };
0112 
0113   private:
0114     /// verbosity level for booking
0115     Level verbosity_;
0116     /// instance label
0117     std::string label_;
0118     /// input sources for monitoring
0119     edm::EDGetTokenT<edm::View<reco::Jet> > jets_;
0120     edm::EDGetTokenT<edm::View<reco::PFCandidate> > muons_;
0121     edm::EDGetTokenT<edm::View<reco::PFCandidate> > elecs_;
0122 
0123     /// considers a vector of METs
0124     std::vector<edm::EDGetTokenT<edm::View<reco::MET> > > mets_;
0125 
0126     /// trigger table
0127     edm::EDGetTokenT<edm::TriggerResults> triggerTable_;
0128     /// trigger paths for monitoring, expected
0129     /// to be of form signalPath:MonitorPath
0130     std::vector<std::string> elecMuPaths_;
0131     /// trigger paths for di muon channel
0132     std::vector<std::string> diMuonPaths_;
0133 
0134     /// electronId label
0135     edm::EDGetTokenT<edm::ValueMap<float> > electronId_;
0136     /// electronId pattern we expect the following pattern:
0137     ///  0: fails
0138     ///  1: passes electron ID only
0139     ///  2: passes electron Isolation only
0140     ///  3: passes electron ID and Isolation only
0141     ///  4: passes conversion rejection
0142     ///  5: passes conversion rejection and ID
0143     ///  6: passes conversion rejection and Isolation
0144     ///  7: passes the whole selection
0145     /// As described on
0146     /// https://twiki.cern.ch/twiki/bin/view/CMS/SimpleCutBasedEleID
0147     // int eidPattern_;
0148     // the cut for the MVA Id
0149     double eidCutValue_;
0150     /// extra isolation criterion on electron
0151     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate> > elecIso_;
0152     /// extra selection on electrons
0153     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate> > elecSelect_;
0154 
0155     /// extra isolation criterion on muon
0156     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate, true> > muonIso_;
0157 
0158     /// extra selection on muons
0159     std::unique_ptr<StringCutObjectSelector<reco::PFCandidate, true> > muonSelect_;
0160 
0161     /// jetCorrector
0162     edm::ESGetToken<JetCorrector, JetCorrectionsRecord> jetCorrector_;
0163     /// jetID as an extra selection type
0164     edm::EDGetTokenT<reco::JetIDValueMap> jetIDLabel_;
0165     /// extra jetID selection on calo jets
0166     std::unique_ptr<StringCutObjectSelector<reco::JetID> > jetIDSelect_;
0167     /// extra selection on jets (here given as std::string as it depends
0168     /// on the the jet type, which selections are valid and which not)
0169     std::string jetSelect_;
0170     /// mass window upper and lower edge
0171     double lowerEdge_, upperEdge_;
0172 
0173     /// number of logged interesting events
0174     int elecMuLogged_, diMuonLogged_, diElecLogged_;
0175     /// storage manager
0176     /// histogram container
0177     std::map<std::string, MonitorElement*> hists_;
0178 
0179     std::string directory_;
0180   };
0181 
0182   inline void MonitorEnsemble::loggerBinLabels(std::string hist) {
0183     // set axes titles for selected events
0184     hists_[hist]->getTH1()->SetOption("TEXT");
0185     hists_[hist]->setBinLabel(1, "Run", 1);
0186     hists_[hist]->setBinLabel(2, "Block", 1);
0187     hists_[hist]->setBinLabel(3, "Event", 1);
0188     hists_[hist]->setBinLabel(6, "pt_{L2L3}(jet1)", 1);
0189     hists_[hist]->setBinLabel(7, "pt_{L2L3}(jet2)", 1);
0190     hists_[hist]->setBinLabel(8, "MET_{Calo}", 1);
0191     hists_[hist]->setAxisTitle("logged evts", 2);
0192 
0193     if (hist == "diMuonLogger_") {
0194       hists_[hist]->setBinLabel(4, "pt(muon)", 1);
0195       hists_[hist]->setBinLabel(5, "pt(muon)", 1);
0196     }
0197     if (hist == "diElecLogger_") {
0198       hists_[hist]->setBinLabel(4, "pt(elec)", 1);
0199       hists_[hist]->setBinLabel(5, "pt(elec)", 1);
0200     }
0201     if (hist == "elecMuLogger_") {
0202       hists_[hist]->setBinLabel(4, "pt(elec)", 1);
0203       hists_[hist]->setBinLabel(5, "pt(muon)", 1);
0204     }
0205   }
0206 
0207   inline void MonitorEnsemble::triggerBinLabels(std::string channel, const std::vector<std::string> labels) {
0208     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0209       hists_[channel + "Mon_"]->setBinLabel(idx + 1, "[" + monitorPath(labels[idx]) + "]", 1);
0210       hists_[channel + "Eff_"]->setBinLabel(
0211           idx + 1, "[" + selectionPath(labels[idx]) + "]|[" + monitorPath(labels[idx]) + "]", 1);
0212     }
0213   }
0214 
0215   inline void MonitorEnsemble::fill(const edm::Event& event,
0216                                     const edm::TriggerResults& triggerTable,
0217                                     std::string channel,
0218                                     const std::vector<std::string> labels) const {
0219     for (unsigned int idx = 0; idx < labels.size(); ++idx) {
0220       if (accept(event, triggerTable, monitorPath(labels[idx]))) {
0221         fill(channel + "Mon_", idx + 0.5);
0222         // take care to fill triggerMon_ before evts is being called
0223         int evts = hists_.find(channel + "Mon_")->second->getBinContent(idx + 1);
0224         double value = hists_.find(channel + "Eff_")->second->getBinContent(idx + 1);
0225         fill(
0226             channel + "Eff_", idx + 0.5, 1. / evts * (accept(event, triggerTable, selectionPath(labels[idx])) - value));
0227       }
0228     }
0229   }
0230 
0231   inline MonitorEnsemble::DecayChannel MonitorEnsemble::decayChannel(
0232       const std::vector<const reco::PFCandidate*>& muons, const std::vector<const reco::PFCandidate*>& elecs) const {
0233     DecayChannel type = NONE;
0234     if (muons.size() > 1) {
0235       type = DIMUON;
0236     } else if (elecs.size() > 1) {
0237       type = DIELEC;
0238     } else if (!elecs.empty() && !muons.empty()) {
0239       type = ELECMU;
0240     }
0241     return type;
0242   }
0243 }  // namespace TopDiLeptonOffline
0244 
0245 #include <utility>
0246 
0247 #include "FWCore/Framework/interface/Frameworkfwd.h"
0248 #include "FWCore/ServiceRegistry/interface/Service.h"
0249 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0250 
0251 #include "FWCore/Common/interface/TriggerNames.h"
0252 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0253 #include "DataFormats/VertexReco/interface/Vertex.h"
0254 #include "DataFormats/Common/interface/TriggerResults.h"
0255 
0256 /**
0257    \class   TopDiLeptonOfflineDQM TopDiLeptonOfflineDQM.h
0258    "DQM/Physics/plugins/TopDiLeptonOfflineDQM.h"
0259 
0260    \brief   Module to apply a monitored selection of top like events in the
0261    di-leptonic channel
0262 
0263    Plugin to apply a monitored selection of top like events with some minimal
0264    flexibility
0265    in the number and definition of the selection steps. To achieve this
0266    flexibility it
0267    employes the SelectionStep class. The MonitorEnsemble class is used to
0268    provide a well
0269    defined set of histograms to be monitored after each selection step. The
0270    SelectionStep
0271    class provides a flexible and intuitive selection via the StringCutParser.
0272    SelectionStep
0273    and MonitorEnsemble classes are interleaved. The monitoring starts after a
0274    preselection
0275    step (which is not monitored in the context of this module) with an instance
0276    of the
0277    MonitorEnsemble class. The following objects are supported for selection:
0278 
0279     - jets  : of type reco::Jet
0280     - elecs : of type reco::PFCandidate
0281     - muons : of type reco::PFCandidate
0282     - met   : of type reco::MET
0283 
0284    These types have to be present as prefix of the selection step paramter
0285    _label_ separated
0286    from the rest of the label by a ':' (e.g. in the form "jets:step0"). The
0287    class expects
0288    selection labels of this type. They will be disentangled by the private
0289    helper functions
0290    _objectType_ and _seletionStep_ as declared below.
0291 */
0292 
0293 /// define MonitorEnsembple to be used
0294 // using TopDiLeptonOffline::MonitorEnsemble;
0295 
0296 class TopDiLeptonOfflineDQM : public DQMOneEDAnalyzer<> {
0297 public:
0298   /// default constructor
0299   TopDiLeptonOfflineDQM(const edm::ParameterSet& cfg);
0300   /// default destructor
0301   ~TopDiLeptonOfflineDQM() override {}
0302 
0303   /// do this during the event loop
0304   void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0305 
0306 protected:
0307   //Book histograms
0308   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0309 
0310 private:
0311   /// deduce object type from ParameterSet label, the label
0312   /// is expected to be of type 'objectType:selectionStep'
0313   std::string objectType(const std::string& label) { return label.substr(0, label.find(':')); };
0314   /// deduce selection step from ParameterSet label, the
0315   /// label is expected to be of type 'objectType:selectionStep'
0316   std::string selectionStep(const std::string& label) { return label.substr(label.find(':') + 1); };
0317 
0318 private:
0319   /// trigger table
0320   edm::EDGetTokenT<edm::TriggerResults> triggerTable_;
0321   /// trigger paths
0322   std::vector<std::string> triggerPaths_;
0323   /// primary vertex
0324   edm::EDGetTokenT<std::vector<reco::Vertex> > vertex_;
0325   /// string cut selector
0326   std::unique_ptr<StringCutObjectSelector<reco::Vertex> > vertexSelect_;
0327   /// beamspot
0328   edm::EDGetTokenT<reco::BeamSpot> beamspot_;
0329   /// string cut selector
0330   std::unique_ptr<StringCutObjectSelector<reco::BeamSpot> > beamspotSelect_;
0331 
0332   /// needed to guarantee the selection order as defined by the order of
0333   /// ParameterSets in the _selection_ vector as defined in the config
0334   std::vector<std::string> selectionOrder_;
0335   /// this is the heart component of the plugin; std::string keeps a label
0336   /// the selection step for later identification, edm::ParameterSet keeps
0337   /// the configuration of the selection for the SelectionStep class,
0338   /// MonitoringEnsemble keeps an instance of the MonitorEnsemble class to
0339   /// be filled _after_ each selection step
0340   std::map<std::string, std::pair<edm::ParameterSet, std::unique_ptr<TopDiLeptonOffline::MonitorEnsemble> > > selection_;
0341   std::unique_ptr<SelectionStep<reco::PFCandidate> > MuonStep;
0342   std::unique_ptr<SelectionStep<reco::PFCandidate> > ElectronStep;
0343   std::unique_ptr<SelectionStep<reco::Vertex> > PvStep;
0344   std::unique_ptr<SelectionStep<reco::MET> > METStep;
0345   std::vector<std::unique_ptr<SelectionStep<reco::Jet> > > JetSteps;
0346   std::vector<std::unique_ptr<SelectionStep<reco::CaloJet> > > CaloJetSteps;
0347   std::vector<std::unique_ptr<SelectionStep<reco::PFJet> > > PFJetSteps;
0348 
0349   std::vector<edm::ParameterSet> sel_;
0350   edm::ParameterSet setup_;
0351 };
0352 
0353 #endif