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
/** \class HLTMuonRateAnalyzer
 *  Get L1/HLT efficiency/rate plots
 *
 *  \author J. Alcaraz
 */

#include "HLTrigger/Muon/test/HLTMuonRateAnalyzer.h"

// Collaborating Class Header
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "DataFormats/Common/interface/Handle.h"

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/L1Trigger/interface/L1MuonParticle.h"
#include "DataFormats/L1Trigger/interface/L1MuonParticleFwd.h"
#include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
#include "DataFormats/RecoCandidate/interface/RecoChargedCandidateFwd.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "TFile.h"
#include "TH1F.h"

using namespace std;
using namespace edm;
using namespace reco;
using namespace trigger;
using namespace l1extra;

/// Constructor
HLTMuonRateAnalyzer::HLTMuonRateAnalyzer(const ParameterSet& pset) {
  theGenLabel = pset.getUntrackedParameter<InputTag>("GenLabel");
  theL1CollectionLabel = pset.getUntrackedParameter<InputTag>("L1CollectionLabel");
  theHLTCollectionLabels = pset.getUntrackedParameter<std::vector<InputTag> >("HLTCollectionLabels");
  theGenToken = consumes<edm::HepMCProduct>(theGenLabel);
  theL1CollectionToken = consumes<trigger::TriggerFilterObjectWithRefs>(theL1CollectionLabel);
  for (auto& theHLTCollectionLabel : theHLTCollectionLabels) {
    theHLTCollectionTokens.push_back(consumes<trigger::TriggerFilterObjectWithRefs>(theHLTCollectionLabel));
  }
  theL1ReferenceThreshold = pset.getUntrackedParameter<double>("L1ReferenceThreshold");
  theNSigmas = pset.getUntrackedParameter<std::vector<double> >("NSigmas90");

  theNumberOfObjects = pset.getUntrackedParameter<unsigned int>("NumberOfObjects");
  theCrossSection = pset.getUntrackedParameter<double>("CrossSection");
  // Convert it already into /nb/s)
  theLuminosity = pset.getUntrackedParameter<double>("Luminosity") * 1.e-33;

  thePtMin = pset.getUntrackedParameter<double>("PtMin");
  thePtMax = pset.getUntrackedParameter<double>("PtMax");
  theNbins = pset.getUntrackedParameter<unsigned int>("Nbins");

  theRootFileName = pset.getUntrackedParameter<string>("RootFileName");

  theNumberOfEvents = 0.;
}

/// Destructor
HLTMuonRateAnalyzer::~HLTMuonRateAnalyzer() = default;

void HLTMuonRateAnalyzer::beginJob() {
  // Create the root file
  theFile = new TFile(theRootFileName.c_str(), "RECREATE");
  theFile->cd();

  char chname[256];
  char chtitle[256];
  snprintf(chname, 255, "eff_%s", theL1CollectionLabel.encode().c_str());
  snprintf(chtitle, 255, "Efficiency (%%) vs L1 Pt threshold (GeV), label=%s", theL1CollectionLabel.encode().c_str());
  hL1eff = new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax);
  snprintf(chname, 255, "rate_%s", theL1CollectionLabel.encode().c_str());
  snprintf(chtitle,
           255,
           "Rate (Hz) vs L1 Pt threshold (GeV), label=%s, L=%.2E (cm^{-2} s^{-1})",
           theL1CollectionLabel.encode().c_str(),
           theLuminosity * 1.e33);
  hL1rate = new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax);

  for (auto& theHLTCollectionLabel : theHLTCollectionLabels) {
    snprintf(chname, 255, "eff_%s", theHLTCollectionLabel.encode().c_str());
    snprintf(
        chtitle, 255, "Efficiency (%%) vs HLT Pt threshold (GeV), label=%s", theHLTCollectionLabel.encode().c_str());
    hHLTeff.push_back(new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax));
    snprintf(chname, 255, "rate_%s", theHLTCollectionLabel.encode().c_str());
    snprintf(chtitle,
             255,
             "Rate (Hz) vs HLT Pt threshold (GeV), label=%s, L=%.2E (cm^{-2} s^{-1})",
             theHLTCollectionLabel.encode().c_str(),
             theLuminosity * 1.e33);
    hHLTrate.push_back(new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax));
  }
}

void HLTMuonRateAnalyzer::endJob() {
  LogInfo("HLTMuonRateAnalyzer") << " (Weighted) number of analyzed events= " << theNumberOfEvents;
  theFile->cd();

  if (theNumberOfEvents == 0) {
    LogInfo("HLTMuonRateAnalyzer") << " No histograms will be written because number of events=0!!!";
    theFile->Close();
    return;
  }

  // L1 operations
  for (unsigned int k = 0; k <= theNbins + 1; k++) {
    double this_eff = hL1eff->GetBinContent(k) / theNumberOfEvents;
    // Hope that this will be essentially OK for weighted samples
    // It should be strictly OK in a binomial scheme when weights = 1
    double this_eff_error = hL1eff->GetBinError(k) / theNumberOfEvents * sqrt(1 - this_eff);
    hL1eff->SetBinContent(k, 100 * this_eff);
    hL1eff->SetBinError(k, 100 * this_eff_error);
    double this_rate = theLuminosity * theCrossSection * this_eff;
    double this_rate_error = theLuminosity * theCrossSection * this_eff_error;
    hL1rate->SetBinContent(k, this_rate);
    hL1rate->SetBinError(k, this_rate_error);
  }

  // HLT operations
  for (unsigned int i = 0; i < theHLTCollectionLabels.size(); i++) {
    for (unsigned int k = 0; k <= theNbins + 1; k++) {
      // Hope that this will be essentially OK for weighted samples
      // It should be strictly OK in a binomial scheme when weights = 1
      double this_eff = hHLTeff[i]->GetBinContent(k) / theNumberOfEvents;
      double this_eff_error = hHLTeff[i]->GetBinError(k) / theNumberOfEvents;
      hHLTeff[i]->SetBinContent(k, 100 * this_eff);
      hHLTeff[i]->SetBinError(k, 100 * this_eff_error);
      double this_rate = theLuminosity * theCrossSection * this_eff;
      double this_rate_error = theLuminosity * theCrossSection * this_eff_error;
      hHLTrate[i]->SetBinContent(k, this_rate);
      hHLTrate[i]->SetBinError(k, this_rate_error);
    }
  }

  // Write the histos to file
  hL1eff->Write();
  hL1rate->Write();
  for (unsigned int i = 0; i < theHLTCollectionLabels.size(); i++) {
    hHLTeff[i]->Write();
    hHLTrate[i]->Write();
  }

  theFile->Close();
}

void HLTMuonRateAnalyzer::analyze(const Event& event, const EventSetup& eventSetup) {
  theFile->cd();

  // Get the HepMC product
  double this_event_weight = 1.;
  try {
    Handle<HepMCProduct> genProduct;
    event.getByToken(theGenToken, genProduct);
    const HepMC::GenEvent* evt = genProduct->GetEvent();
    HepMC::WeightContainer weights = evt->weights();
    if (weights.size() > 0)
      this_event_weight = weights[0];
  } catch (...) {
    LogInfo("HLTMuonRateAnalyzer") << " NO HepMCProduct found!!!!!!!!!!!!!!!";
    LogInfo("HLTMuonRateAnalyzer") << " SETTING EVENT WEIGHT TO 1";
  }
  theNumberOfEvents += this_event_weight;

  // Get the L1 collection
  Handle<TriggerFilterObjectWithRefs> l1cands;
  event.getByToken(theL1CollectionToken, l1cands);
  if (l1cands.failedToGet())
    return;

  // Get the HLT collections
  std::vector<Handle<TriggerFilterObjectWithRefs> > hltcands(theHLTCollectionLabels.size());
  unsigned int modules_in_this_event = 0;
  for (unsigned int i = 0; i < theHLTCollectionLabels.size(); i++) {
    event.getByToken(theHLTCollectionTokens[i], hltcands[i]);
    if (hltcands[i].failedToGet())
      break;
    modules_in_this_event++;
  }

  // Fix L1 thresholds to obtain HLT plots
  unsigned int nL1FoundRef = 0;
  double epsilon = 0.001;
  vector<L1MuonParticleRef> l1mu;
  l1cands->getObjects(TriggerL1Mu, l1mu);
  for (auto& k : l1mu) {
    L1MuonParticleRef candref = L1MuonParticleRef(k);
    // L1 PTs are "quantized" due to LUTs.
    // Their meaning: true_pt > ptLUT more than 90% pof the times
    double ptLUT = candref->pt();
    // Add "epsilon" to avoid rounding errors when ptLUT==L1Threshold
    if (ptLUT + epsilon > theL1ReferenceThreshold)
      nL1FoundRef++;
  }

  for (unsigned int j = 0; j < theNbins; j++) {
    double ptcut = thePtMin + j * (thePtMax - thePtMin) / theNbins;

    // L1 filling
    unsigned int nFound = 0;
    for (auto& k : l1mu) {
      L1MuonParticleRef candref = L1MuonParticleRef(k);
      double pt = candref->pt();
      if (pt > ptcut)
        nFound++;
    }
    if (nFound >= theNumberOfObjects)
      hL1eff->Fill(ptcut, this_event_weight);

    // Stop here if L1 reference cuts were not satisfied
    if (nL1FoundRef < theNumberOfObjects)
      continue;

    // HLT filling
    for (unsigned int i = 0; i < modules_in_this_event; i++) {
      unsigned nFound = 0;
      vector<RecoChargedCandidateRef> vref;
      hltcands[i]->getObjects(TriggerMuon, vref);
      for (auto& k : vref) {
        RecoChargedCandidateRef candref = RecoChargedCandidateRef(k);
        TrackRef tk = candref->get<TrackRef>();
        double pt = tk->pt();
        double err0 = tk->error(0);
        double abspar0 = fabs(tk->parameter(0));
        // convert to 90% efficiency threshold
        if (abspar0 > 0)
          pt += theNSigmas[i] * err0 / abspar0 * pt;
        if (pt > ptcut)
          nFound++;
      }
      if (nFound >= theNumberOfObjects) {
        hHLTeff[i]->Fill(ptcut, this_event_weight);
      } else {
        break;
      }
    }
  }
}

DEFINE_FWK_MODULE(HLTMuonRateAnalyzer);