DiDispStaMuonMonitor

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
#include <string>
#include <vector>

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DQMServices/Core/interface/DQMStore.h"
#include "DQMServices/Core/interface/DQMEDAnalyzer.h"
#include "DQMOffline/Trigger/plugins/TriggerDQMBase.h"
#include "CommonTools/TriggerUtils/interface/GenericTriggerEventFlag.h"
#include "CommonTools/Utils/interface/StringCutObjectSelector.h"

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"

class DiDispStaMuonMonitor : public DQMEDAnalyzer, public TriggerDQMBase {
public:
  typedef dqm::reco::MonitorElement MonitorElement;
  typedef dqm::reco::DQMStore DQMStore;

  DiDispStaMuonMonitor(const edm::ParameterSet&);
  ~DiDispStaMuonMonitor() throw() override;
  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

protected:
  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
  void analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;

private:
  const std::string folderName_;

  const bool requireValidHLTPaths_;
  bool hltPathsAreValid_;

  edm::EDGetTokenT<reco::TrackCollection> muonToken_;

  std::vector<double> muonPt_variable_binning_;
  MEbinning muonPt_binning_;
  MEbinning muonEta_binning_;
  MEbinning muonPhi_binning_;
  MEbinning muonDxy_binning_;
  MEbinning ls_binning_;

  ObjME muonPtME_;
  ObjME muonPtNoDxyCutME_;
  ObjME muonPtME_variableBinning_;
  ObjME muonPtVsLS_;
  ObjME muonEtaME_;
  ObjME muonPhiME_;
  ObjME muonDxyME_;
  ObjME subMuonPtME_;
  ObjME subMuonPtME_variableBinning_;
  ObjME subMuonEtaME_;
  ObjME subMuonPhiME_;
  ObjME subMuonDxyME_;

  std::unique_ptr<GenericTriggerEventFlag> num_genTriggerEventFlag_;
  std::unique_ptr<GenericTriggerEventFlag> den_genTriggerEventFlag_;

  StringCutObjectSelector<reco::Track, true> muonSelectionGeneral_;
  StringCutObjectSelector<reco::Track, true> muonSelectionPt_;
  StringCutObjectSelector<reco::Track, true> muonSelectionDxy_;

  unsigned int nmuons_;
};

DiDispStaMuonMonitor::DiDispStaMuonMonitor(const edm::ParameterSet& iConfig)
    : folderName_(iConfig.getParameter<std::string>("FolderName")),
      requireValidHLTPaths_(iConfig.getParameter<bool>("requireValidHLTPaths")),
      hltPathsAreValid_(false),
      muonToken_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
      muonPt_variable_binning_(
          iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<std::vector<double>>("muonPtBinning")),
      muonPt_binning_(getHistoPSet(
          iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonPtPSet"))),
      muonEta_binning_(getHistoPSet(
          iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonEtaPSet"))),
      muonPhi_binning_(getHistoPSet(
          iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonPhiPSet"))),
      muonDxy_binning_(getHistoPSet(
          iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("muonDxyPSet"))),
      ls_binning_(
          getHistoPSet(iConfig.getParameter<edm::ParameterSet>("histoPSet").getParameter<edm::ParameterSet>("lsPSet"))),
      num_genTriggerEventFlag_(new GenericTriggerEventFlag(
          iConfig.getParameter<edm::ParameterSet>("numGenericTriggerEventPSet"), consumesCollector(), *this)),
      den_genTriggerEventFlag_(new GenericTriggerEventFlag(
          iConfig.getParameter<edm::ParameterSet>("denGenericTriggerEventPSet"), consumesCollector(), *this)),
      muonSelectionGeneral_(
          iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("general")),
      muonSelectionPt_(iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("pt")),
      muonSelectionDxy_(iConfig.getParameter<edm::ParameterSet>("muonSelection").getParameter<std::string>("dxy")),
      nmuons_(iConfig.getParameter<unsigned int>("nmuons")) {}

DiDispStaMuonMonitor::~DiDispStaMuonMonitor() throw() {
  if (num_genTriggerEventFlag_) {
    num_genTriggerEventFlag_.reset();
  }
  if (den_genTriggerEventFlag_) {
    den_genTriggerEventFlag_.reset();
  }
}

void DiDispStaMuonMonitor::bookHistograms(DQMStore::IBooker& ibooker,
                                          edm::Run const& iRun,
                                          edm::EventSetup const& iSetup) {
  // Initialize the GenericTriggerEventFlag
  if (num_genTriggerEventFlag_ && num_genTriggerEventFlag_->on()) {
    num_genTriggerEventFlag_->initRun(iRun, iSetup);
  }
  if (den_genTriggerEventFlag_ && den_genTriggerEventFlag_->on()) {
    den_genTriggerEventFlag_->initRun(iRun, iSetup);
  }

  // check if every HLT path specified in numerator and denominator has a valid match in the HLT Menu
  hltPathsAreValid_ = (num_genTriggerEventFlag_ && den_genTriggerEventFlag_ && num_genTriggerEventFlag_->on() &&
                       den_genTriggerEventFlag_->on() && num_genTriggerEventFlag_->allHLTPathsAreValid() &&
                       den_genTriggerEventFlag_->allHLTPathsAreValid());

  // if valid HLT paths are required,
  // create DQM outputs only if all paths are valid
  if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
    return;
  }

  std::string histname, histtitle;

  std::string currentFolder = folderName_;
  ibooker.setCurrentFolder(currentFolder);

  histname = "muonPt";
  histtitle = "muonPt";

  bookME(ibooker, muonPtME_, histname, histtitle, muonPt_binning_.nbins, muonPt_binning_.xmin, muonPt_binning_.xmax);
  setMETitle(muonPtME_, "DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");

  histname = "muonPtNoDxyCut";
  histtitle = "muonPtNoDxyCut";
  bookME(ibooker,
         muonPtNoDxyCutME_,
         histname,
         histtitle,
         muonPt_binning_.nbins,
         muonPt_binning_.xmin,
         muonPt_binning_.xmax);
  setMETitle(muonPtNoDxyCutME_, "DisplacedStandAlone Muon p_{T} [GeV] without Dxy cut", "Events / [GeV]");

  histname = "muonPt_variable";
  histtitle = "muonPt";
  bookME(ibooker, muonPtME_variableBinning_, histname, histtitle, muonPt_variable_binning_);
  setMETitle(muonPtME_variableBinning_, "DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");

  histname = "muonPtVsLS";
  histtitle = "muonPt vs LS";
  bookME(ibooker,
         muonPtVsLS_,
         histname,
         histtitle,
         ls_binning_.nbins,
         ls_binning_.xmin,
         ls_binning_.xmax,
         muonPt_binning_.xmin,
         muonPt_binning_.xmax);
  setMETitle(muonPtVsLS_, "LS", "DisplacedStandAlone Muon p_{T} [GeV]");

  histname = "muonEta";
  histtitle = "muonEta";
  bookME(
      ibooker, muonEtaME_, histname, histtitle, muonEta_binning_.nbins, muonEta_binning_.xmin, muonEta_binning_.xmax);
  setMETitle(muonEtaME_, "DisplacedStandAlone Muon #eta", "Events");

  histname = "muonPhi";
  histtitle = "muonPhi";
  bookME(
      ibooker, muonPhiME_, histname, histtitle, muonPhi_binning_.nbins, muonPhi_binning_.xmin, muonPhi_binning_.xmax);
  setMETitle(muonPhiME_, "DisplacedStandAlone Muon #phi", "Events");

  histname = "muonDxy";
  histtitle = "muonDxy";
  bookME(
      ibooker, muonDxyME_, histname, histtitle, muonDxy_binning_.nbins, muonDxy_binning_.xmin, muonDxy_binning_.xmax);
  setMETitle(muonDxyME_, "DisplacedStandAlone Muon #dxy", "Events");

  if (nmuons_ > 1) {
    histname = "subMuonPt";
    histtitle = "subMuonPt";
    bookME(
        ibooker, subMuonPtME_, histname, histtitle, muonPt_binning_.nbins, muonPt_binning_.xmin, muonPt_binning_.xmax);
    setMETitle(subMuonPtME_, "Subleading DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");

    histname = "subMuonPt_variable";
    histtitle = "subMuonPt";
    bookME(ibooker, subMuonPtME_variableBinning_, histname, histtitle, muonPt_variable_binning_);
    setMETitle(subMuonPtME_variableBinning_, "Subleading DisplacedStandAlone Muon p_{T} [GeV]", "Events / [GeV]");

    histname = "subMuonEta";
    histtitle = "subMuonEta";
    bookME(ibooker,
           subMuonEtaME_,
           histname,
           histtitle,
           muonEta_binning_.nbins,
           muonEta_binning_.xmin,
           muonEta_binning_.xmax);
    setMETitle(subMuonEtaME_, "Subleading DisplacedStandAlone Muon #eta", "Events");

    histname = "subMuonPhi";
    histtitle = "subMuonPhi";
    bookME(ibooker,
           subMuonPhiME_,
           histname,
           histtitle,
           muonPhi_binning_.nbins,
           muonPhi_binning_.xmin,
           muonPhi_binning_.xmax);
    setMETitle(subMuonPhiME_, "Subleading DisplacedStandAlone Muon #phi", "Events");

    histname = "subMuonDxy";
    histtitle = "subMuonDxy";
    bookME(ibooker,
           subMuonDxyME_,
           histname,
           histtitle,
           muonDxy_binning_.nbins,
           muonDxy_binning_.xmin,
           muonDxy_binning_.xmax);
    setMETitle(subMuonDxyME_, "Subleading DisplacedStandAlone Muon #dxy", "Events");
  }
}

void DiDispStaMuonMonitor::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
  // if valid HLT paths are required,
  // analyze event only if all paths are valid
  if (requireValidHLTPaths_ and (not hltPathsAreValid_)) {
    return;
  }

  // Filter out events if Trigger Filtering is requested
  if (den_genTriggerEventFlag_->on() && !den_genTriggerEventFlag_->accept(iEvent, iSetup))
    return;

  const int ls = iEvent.id().luminosityBlock();

  edm::Handle<reco::TrackCollection> DSAHandle;
  iEvent.getByToken(muonToken_, DSAHandle);
  if ((unsigned int)(DSAHandle->size()) < nmuons_)
    return;
  std::vector<edm::Ptr<reco::Track>> dsaMuonPtrs_{};  // = DSAHandle->ptrs();
  for (size_t i(0); i != DSAHandle->size(); ++i) {
    dsaMuonPtrs_.emplace_back(DSAHandle, i);
  }
  std::vector<edm::Ptr<reco::Track>> muons{}, muonsCutOnPt{}, muonsCutOnDxy{}, muonsCutOnPtAndDxy{};

  // general selection
  auto selectGeneral_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionGeneral_(*m); });
  std::copy_if(dsaMuonPtrs_.begin(), dsaMuonPtrs_.end(), back_inserter(muons), selectGeneral_);
  if ((unsigned int)(muons.size()) < nmuons_)
    return;

  // sort by pt
  auto ptSorter_ = [](edm::Ptr<reco::Track> const& lhs, edm::Ptr<reco::Track> const& rhs) -> bool {
    return lhs->pt() > rhs->pt();
  };
  std::sort(muons.begin(), muons.end(), ptSorter_);

  // cut on pt
  auto selectOnPt_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionPt_(*m); });
  std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnPt), selectOnPt_);
  // cut on dxy
  auto selectOnDxy_([this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionDxy_(*m); });
  std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnDxy), selectOnDxy_);
  // cut on pt and dxy
  auto selectOnPtAndDxy_(
      [this](edm::Ptr<reco::Track> const& m) -> bool { return muonSelectionPt_(*m) && muonSelectionDxy_(*m); });
  std::copy_if(muons.begin(), muons.end(), back_inserter(muonsCutOnPtAndDxy), selectOnPtAndDxy_);

  std::sort(muonsCutOnPt.begin(), muonsCutOnPt.end(), ptSorter_);
  std::sort(muonsCutOnDxy.begin(), muonsCutOnDxy.end(), ptSorter_);
  std::sort(muonsCutOnPtAndDxy.begin(), muonsCutOnPtAndDxy.end(), ptSorter_);

  // --------------------------------
  // filling histograms (denominator)
  // --------------------------------
  if (muonsCutOnDxy.size() >= nmuons_) {
    // pt has cut on dxy
    muonPtME_.denominator->Fill(muonsCutOnDxy[0]->pt());
    muonPtNoDxyCutME_.denominator->Fill(muons[0]->pt());
    muonPtME_variableBinning_.denominator->Fill(muonsCutOnDxy[0]->pt());
    muonPtVsLS_.denominator->Fill(ls, muonsCutOnDxy[0]->pt());
    if (nmuons_ > 1) {
      subMuonPtME_.denominator->Fill(muonsCutOnDxy[1]->pt());
      subMuonPtME_variableBinning_.denominator->Fill(muonsCutOnDxy[1]->pt());
    }
  }
  if (muonsCutOnPtAndDxy.size() >= nmuons_) {
    // eta, phi have cut on pt and dxy
    muonEtaME_.denominator->Fill(muonsCutOnPtAndDxy[0]->eta());
    muonPhiME_.denominator->Fill(muonsCutOnPtAndDxy[0]->phi());
    if (nmuons_ > 1) {
      subMuonEtaME_.denominator->Fill(muonsCutOnPtAndDxy[1]->eta());
      subMuonPhiME_.denominator->Fill(muonsCutOnPtAndDxy[1]->phi());
    }
  }
  if (muonsCutOnPt.size() >= nmuons_) {
    // dxy has cut on pt
    muonDxyME_.denominator->Fill(muonsCutOnPt[0]->dxy());
    if (nmuons_ > 1) {
      subMuonDxyME_.denominator->Fill(muonsCutOnPt[1]->dxy());
    }
  }

  // --------------------------------
  // filling histograms (numerator)
  // --------------------------------
  if (num_genTriggerEventFlag_->on() && !num_genTriggerEventFlag_->accept(iEvent, iSetup))
    return;

  if (muonsCutOnDxy.size() >= nmuons_) {
    // pt has cut on dxy
    muonPtME_.numerator->Fill(muonsCutOnDxy[0]->pt());
    muonPtNoDxyCutME_.numerator->Fill(muons[0]->pt());
    muonPtME_variableBinning_.numerator->Fill(muonsCutOnDxy[0]->pt());
    muonPtVsLS_.numerator->Fill(ls, muonsCutOnDxy[0]->pt());
    if (nmuons_ > 1) {
      subMuonPtME_.numerator->Fill(muonsCutOnDxy[1]->pt());
      subMuonPtME_variableBinning_.numerator->Fill(muonsCutOnDxy[1]->pt());
    }
  }
  if (muonsCutOnPtAndDxy.size() >= nmuons_) {
    // eta, phi have cut on pt and dxy
    muonEtaME_.numerator->Fill(muonsCutOnPtAndDxy[0]->eta());
    muonPhiME_.numerator->Fill(muonsCutOnPtAndDxy[0]->phi());
    if (nmuons_ > 1) {
      subMuonEtaME_.numerator->Fill(muonsCutOnPtAndDxy[1]->eta());
      subMuonPhiME_.numerator->Fill(muonsCutOnPtAndDxy[1]->phi());
    }
  }
  if (muonsCutOnPt.size() >= nmuons_) {
    // dxy has cut on pt
    muonDxyME_.numerator->Fill(muonsCutOnPt[0]->dxy());
    if (nmuons_ > 1) {
      subMuonDxyME_.numerator->Fill(muonsCutOnPt[1]->dxy());
    }
  }
}

void DiDispStaMuonMonitor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<std::string>("FolderName", "HLT/EXO/DiDispStaMuon");
  desc.add<bool>("requireValidHLTPaths", true);

  desc.add<edm::InputTag>("muons", edm::InputTag("displacedStandAloneMuons"));
  desc.add<unsigned int>("nmuons", 2);

  edm::ParameterSetDescription muonSelection;
  muonSelection.add<std::string>("general", "pt > 0");
  muonSelection.add<std::string>("pt", "");
  muonSelection.add<std::string>("dxy", "pt > 0");
  desc.add<edm::ParameterSetDescription>("muonSelection", muonSelection);

  edm::ParameterSetDescription genericTriggerEventPSet;
  GenericTriggerEventFlag::fillPSetDescription(genericTriggerEventPSet);
  desc.add<edm::ParameterSetDescription>("numGenericTriggerEventPSet", genericTriggerEventPSet);
  desc.add<edm::ParameterSetDescription>("denGenericTriggerEventPSet", genericTriggerEventPSet);

  edm::ParameterSetDescription histoPSet;
  edm::ParameterSetDescription muonPtPSet;
  edm::ParameterSetDescription muonEtaPSet;
  edm::ParameterSetDescription muonPhiPSet;
  edm::ParameterSetDescription muonDxyPSet;
  edm::ParameterSetDescription lsPSet;
  fillHistoPSetDescription(muonPtPSet);
  fillHistoPSetDescription(muonEtaPSet);
  fillHistoPSetDescription(muonPhiPSet);
  fillHistoPSetDescription(muonDxyPSet);
  fillHistoPSetDescription(lsPSet);
  histoPSet.add<edm::ParameterSetDescription>("muonPtPSet", muonPtPSet);
  histoPSet.add<edm::ParameterSetDescription>("muonEtaPSet", muonEtaPSet);
  histoPSet.add<edm::ParameterSetDescription>("muonPhiPSet", muonPhiPSet);
  histoPSet.add<edm::ParameterSetDescription>("muonDxyPSet", muonDxyPSet);
  histoPSet.add<edm::ParameterSetDescription>("lsPSet", lsPSet);
  std::vector<double> bins = {0.,   20.,  40.,  60.,  80.,  90.,  100., 110., 120., 130., 140., 150., 160.,
                              170., 180., 190., 200., 220., 240., 260., 280., 300., 350., 400., 450., 1000.};
  histoPSet.add<std::vector<double>>("muonPtBinning", bins);

  desc.add<edm::ParameterSetDescription>("histoPSet", histoPSet);

  descriptions.add("DiDispStaMuonMonitoring", desc);
}

DEFINE_FWK_MODULE(DiDispStaMuonMonitor);