StudyTriggerHLT

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
// -*- C++ -*-
//
// Package:    IsolatedParticles
// Class:      StudyTriggerHLT
//
/**\class StudyTriggerHLT StudyTriggerHLT.cc Calibration/IsolatedParticles/plugins/StudyTriggerHLT.cc

 Description: Studies single particle response measurements in data/MC

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Sunanda Banerjee
//         Created:  Thu Mar  4 18:52:02 CST 2011
//
//

// system include files
#include <memory>
#include <string>

// Root objects
#include "TH1.h"
#include "TH2.h"

// user include files
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"

#include "DataFormats/Common/interface/TriggerResults.h"
#include "DataFormats/HLTReco/interface/TriggerEvent.h"
#include "DataFormats/HLTReco/interface/TriggerObject.h"
#include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Common/interface/TriggerNames.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DataFormats/Math/interface/deltaR.h"
#include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
#include "DataFormats/MuonReco/interface/Muon.h"
#include "DataFormats/MuonReco/interface/MuonFwd.h"
#include "DataFormats/MuonReco/interface/MuonSelectors.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"
#include "DataFormats/TrackReco/interface/TrackBase.h"
#include "DataFormats/TrackReco/interface/HitPattern.h"
#include "Calibration/IsolatedParticles/interface/TrackSelection.h"

class StudyTriggerHLT : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
public:
  explicit StudyTriggerHLT(const edm::ParameterSet&);
  ~StudyTriggerHLT() override {}

  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
  void analyze(edm::Event const&, edm::EventSetup const&) override;
  void beginJob() override;
  void beginRun(edm::Run const&, edm::EventSetup const&) override;
  void endRun(edm::Run const&, edm::EventSetup const&) override;
  virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
  virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}

  std::string truncate_str(const std::string&);

  // ----------member data ---------------------------
  HLTConfigProvider hltConfig_;
  edm::Service<TFileService> fs_;
  const int verbosity_;
  const std::vector<std::string> trigNames_, newNames_;
  const edm::InputTag triggerEvent_, theTriggerResultsLabel_;
  const edm::InputTag labelMuon_, labelGenTrack_;
  const std::string theTrackQuality_;
  std::vector<std::string> HLTNames_;
  bool changed_, firstEvent_;
  reco::TrackBase::TrackQuality trackQuality_;

  edm::EDGetTokenT<trigger::TriggerEvent> tok_trigEvt;
  edm::EDGetTokenT<edm::TriggerResults> tok_trigRes;
  edm::EDGetTokenT<reco::MuonCollection> tok_Muon_;
  edm::EDGetTokenT<reco::TrackCollection> tok_genTrack_;
  std::vector<bool> mediumMuon_;
  TH1I *h_nHLT, *h_HLTAccept, *h_HLTCorr;
  TH2I* h_nHLTvsRN;
  std::vector<TH1I*> h_HLTAccepts;
  TH1D *h_pt, *h_eta, *h_phi, *h_dr1, *h_dr2, *h_dr3;
  int nRun_;
};

StudyTriggerHLT::StudyTriggerHLT(const edm::ParameterSet& iConfig)
    : verbosity_(iConfig.getParameter<int>("verbosity")),
      triggerEvent_(edm::InputTag("hltTriggerSummaryAOD", "", "HLT")),
      theTriggerResultsLabel_(edm::InputTag("TriggerResults", "", "HLT")),
      labelMuon_(iConfig.getParameter<edm::InputTag>("labelMuon")),
      labelGenTrack_(iConfig.getParameter<edm::InputTag>("labelTrack")),
      theTrackQuality_(iConfig.getParameter<std::string>("trackQuality")),
      nRun_(0) {
  usesResource(TFileService::kSharedResource);
  trackQuality_ = reco::TrackBase::qualityByName(theTrackQuality_);

  // define tokens for access
  tok_trigEvt = consumes<trigger::TriggerEvent>(triggerEvent_);
  tok_trigRes = consumes<edm::TriggerResults>(theTriggerResultsLabel_);
  tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
  tok_genTrack_ = consumes<reco::TrackCollection>(labelGenTrack_);

  edm::LogVerbatim("StudyHLT") << "Verbosity " << verbosity_ << " Trigger labels " << triggerEvent_ << " and "
                               << theTriggerResultsLabel_ << " Labels used: Track " << labelGenTrack_ << " Muon "
                               << labelMuon_ << " Track Quality " << theTrackQuality_;

  firstEvent_ = true;
  changed_ = false;
}

void StudyTriggerHLT::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<int>("verbosity", 0);
  desc.add<edm::InputTag>("labelMuon", edm::InputTag("muons", "", "RECO"));
  desc.add<edm::InputTag>("labelTrack", edm::InputTag("generalTracks", "", "RECO"));
  desc.add<std::string>("trackQuality", "highPurity");
  descriptions.add("studyTriggerHLT", desc);
}

void StudyTriggerHLT::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
  int RunNo = iEvent.id().run();
  int EvtNo = iEvent.id().event();

  if (verbosity_ > 0)
    edm::LogVerbatim("StudyHLT") << "RunNo " << RunNo << " EvtNo " << EvtNo << " Lumi " << iEvent.luminosityBlock()
                                 << " Bunch " << iEvent.bunchCrossing();

  trigger::TriggerEvent triggerEvent;
  edm::Handle<trigger::TriggerEvent> triggerEventHandle;
  iEvent.getByToken(tok_trigEvt, triggerEventHandle);

  if (!triggerEventHandle.isValid()) {
    edm::LogWarning("StudyHLT") << "Error! Can't get the product " << triggerEvent_.label();
  } else {
    triggerEvent = *(triggerEventHandle.product());

    /////////////////////////////TriggerResults
    edm::Handle<edm::TriggerResults> triggerResults;
    iEvent.getByToken(tok_trigRes, triggerResults);

    if (triggerResults.isValid()) {
      h_nHLT->Fill(triggerResults->size());
      h_nHLTvsRN->Fill(RunNo, triggerResults->size());

      const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
      const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
      for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
        int ipos = -1;
        std::string newtriggerName = truncate_str(triggerNames_[iHLT]);
        for (unsigned int i = 0; i < HLTNames_.size(); ++i) {
          if (newtriggerName == HLTNames_[i]) {
            ipos = i + 1;
            break;
          }
        }
        if (ipos < 0) {
          HLTNames_.push_back(newtriggerName);
          ipos = (int)(HLTNames_.size());
          if (ipos <= h_HLTAccept->GetNbinsX())
            h_HLTAccept->GetXaxis()->SetBinLabel(ipos, newtriggerName.c_str());
        }
        if ((int)(iHLT + 1) > h_HLTAccepts[nRun_]->GetNbinsX()) {
          edm::LogVerbatim("StudyHLT") << "Wrong trigger " << RunNo << " Event " << EvtNo << " Hlt " << iHLT;
        } else {
          if (firstEvent_)
            h_HLTAccepts[nRun_]->GetXaxis()->SetBinLabel(iHLT + 1, newtriggerName.c_str());
        }
        int hlt = triggerResults->accept(iHLT);
        if (hlt) {
          h_HLTAccepts[nRun_]->Fill(iHLT + 1);
          h_HLTAccept->Fill(ipos);
        }
      }
    }
  }
  firstEvent_ = false;

  double globalMin = 1000;
  edm::Handle<reco::MuonCollection> muonEventHandle;
  iEvent.getByToken(tok_Muon_, muonEventHandle);
  edm::Handle<reco::TrackCollection> trackEventHandle;
  iEvent.getByToken(tok_genTrack_, trackEventHandle);
  edm::LogVerbatim("StudyHLT") << "Muon Handle " << muonEventHandle.isValid() << " Track Handle "
                               << trackEventHandle.isValid();
  for (reco::TrackCollection::const_iterator track1 = trackEventHandle->begin(); track1 != trackEventHandle->end();
       ++track1) {
    double localMin = 1000;
    if (muonEventHandle.isValid()) {
      for (reco::MuonCollection::const_iterator recMuon = muonEventHandle->begin(); recMuon != muonEventHandle->end();
           ++recMuon) {
        if (((recMuon->isPFMuon()) && (recMuon->isGlobalMuon() || recMuon->isTrackerMuon())) &&
            (recMuon->innerTrack()->validFraction() > 0.49)) {
          double chiGlobal = ((recMuon->globalTrack().isNonnull()) ? recMuon->globalTrack()->normalizedChi2() : 999);
          bool goodGlob =
              (recMuon->isGlobalMuon() && chiGlobal < 3 && recMuon->combinedQuality().chi2LocalPosition < 12 &&
               recMuon->combinedQuality().trkKink < 20);
          if (muon::segmentCompatibility(*recMuon) > (goodGlob ? 0.303 : 0.451)) {
            double dr = reco::deltaR(track1->eta(), track1->phi(), recMuon->eta(), recMuon->phi());
            if (dr < localMin) {
              localMin = dr;
              if (localMin < globalMin)
                globalMin = localMin;
            }
          }
        }
      }
    }
    h_pt->Fill(track1->pt());
    h_eta->Fill(track1->eta());
    h_phi->Fill(track1->phi());
    h_dr1->Fill(localMin);
    if (track1->quality(trackQuality_))
      h_dr3->Fill(localMin);
    edm::LogVerbatim("StudyHLT") << "Track pT " << track1->pt() << " eta " << track1->eta() << " phi " << track1->phi()
                                 << " minimum distance " << localMin;
  }
  edm::LogVerbatim("StudyHLT") << "GlobalMinimum  = " << globalMin;
  h_dr2->Fill(globalMin);
}

void StudyTriggerHLT::beginJob() {
  // Book histograms
  h_nHLT = fs_->make<TH1I>("h_nHLT", "size of trigger Names", 1000, 0, 1000);
  h_HLTAccept = fs_->make<TH1I>("h_HLTAccept", "HLT Accepts for all runs", 500, 0, 500);
  for (int i = 1; i <= 500; ++i)
    h_HLTAccept->GetXaxis()->SetBinLabel(i, " ");
  h_nHLTvsRN = fs_->make<TH2I>("h_nHLTvsRN", "size of trigger Names vs RunNo", 300, 319200, 319500, 100, 400, 500);
  h_pt = fs_->make<TH1D>("h_pt", "p_{t}", 50, 0, 20);
  h_pt->Sumw2();
  h_eta = fs_->make<TH1D>("h_eta", "#eta", 50, -3, 3);
  h_eta->Sumw2();
  h_phi = fs_->make<TH1D>("h_phi", "#phi", 50, -10, 10);
  h_phi->Sumw2();
  h_dr1 = fs_->make<TH1D>("dR1", "#Delta R (Track)", 1000, 0, 0.1);
  h_dr1->Sumw2();
  h_dr2 = fs_->make<TH1D>("dR2", "#Delta R (Global)", 3000, 0, 0.000003);
  h_dr2->Sumw2();
  h_dr3 = fs_->make<TH1D>("dR3", "#Delta R (Good Track)", 1000, 0, 0.1);
  h_dr3->Sumw2();
}

// ------------ method called when starting to processes a run  ------------
void StudyTriggerHLT::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
  char hname[100], htit[400];
  edm::LogVerbatim("StudyHLT") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init "
                               << hltConfig_.init(iRun, iSetup, "HLT", changed_);
  sprintf(hname, "h_HLTAccepts_%i", iRun.run());
  sprintf(htit, "HLT Accepts for Run No %i", iRun.run());
  TH1I* hnew = fs_->make<TH1I>(hname, htit, 500, 0, 500);
  for (int i = 1; i <= 500; ++i)
    hnew->GetXaxis()->SetBinLabel(i, " ");
  h_HLTAccepts.push_back(hnew);
  edm::LogVerbatim("StudyHLT") << "beginRun " << iRun.run();
  firstEvent_ = true;
  changed_ = false;
}

// ------------ method called when ending the processing of a run  ------------
void StudyTriggerHLT::endRun(edm::Run const& iRun, edm::EventSetup const&) {
  ++nRun_;
  edm::LogVerbatim("StudyHLT") << "endRun[" << nRun_ << "] " << iRun.run();
}

std::string StudyTriggerHLT::truncate_str(const std::string& str) {
  std::string truncated_str(str);
  int length = str.length();
  for (int i = 0; i < length - 2; i++) {
    if (str[i] == '_' && str[i + 1] == 'v' && isdigit(str.at(i + 2))) {
      int z = i + 1;
      truncated_str = str.substr(0, z);
    }
  }
  return (truncated_str);
}

DEFINE_FWK_MODULE(StudyTriggerHLT);