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
/** \class HLTMuonTurnOnAnalyzer
 *  Get L1/HLT turn on curves
 *
 *  \author J. Alcaraz
 */

#include "HLTrigger/Muon/test/HLTMuonTurnOnAnalyzer.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/L1Trigger/interface/L1MuonParticle.h"
#include "DataFormats/L1Trigger/interface/L1MuonParticleFwd.h"

#include "DataFormats/TrackReco/interface/Track.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
HLTMuonTurnOnAnalyzer::HLTMuonTurnOnAnalyzer(const ParameterSet& pset) {
  theGenLabel = pset.getUntrackedParameter<InputTag>("GenLabel");
  useMuonFromGenerator = pset.getUntrackedParameter<bool>("UseMuonFromGenerator");
  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));
  }
  theReferenceThreshold = pset.getUntrackedParameter<double>("ReferenceThreshold");

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

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

  theNumberOfEvents = 0.;
}

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

void HLTMuonTurnOnAnalyzer::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 Generated Pt (GeV), label=%s", theL1CollectionLabel.encode().c_str());
  hL1eff = new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax);
  hL1nor = 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 Generated Pt (GeV), label=%s", theHLTCollectionLabel.encode().c_str());
    hHLTeff.push_back(new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax));
    hHLTnor.push_back(new TH1F(chname, chtitle, theNbins, thePtMin, thePtMax));
  }
}

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

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

  // L1 operations
  hL1eff->Divide(hL1nor);
  hL1eff->Scale(100.);

  // HLT operations
  for (unsigned int i = 0; i < theHLTCollectionLabels.size(); i++) {
    hHLTeff[i]->Divide(hHLTnor[i]);
    hHLTeff[i]->Scale(100.);
  }

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

  theFile->Close();
}

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

  // Get the HepMC product
  double this_event_weight = 1.;
  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];
  theNumberOfEvents += this_event_weight;

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

  // 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++;
  }

  // Get the muon with maximum pt at generator level or reconstruction, depending on the choice
  bool refmuon_found = false;
  double ptuse = -1;

  if (useMuonFromGenerator) {
    HepMC::GenEvent::particle_const_iterator part;
    for (part = evt->particles_begin(); part != evt->particles_end(); ++part) {
      int id1 = (*part)->pdg_id();
      if (id1 != 13 && id1 != -13)
        continue;
      float pt1 = (*part)->momentum().perp();
      if (pt1 > ptuse) {
        refmuon_found = true;
        ptuse = pt1;
      }
    }
  } else {
    unsigned int i = modules_in_this_event - 1;
    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();
      if (pt > ptuse) {
        refmuon_found = true;
        ptuse = pt;
      }
    }
  }

  if (!refmuon_found) {
    LogInfo("HLTMuonTurnOnAnalyzer") << " NO reference muon found!!!";
    LogInfo("HLTMuonTurnOnAnalyzer") << " Skipping event";
    return;
  }

  // Fix L1 thresholds to obtain the efficiecy plot
  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 > theReferenceThreshold)
      nL1FoundRef++;
  }
  hL1nor->Fill(ptuse, this_event_weight);
  if (nL1FoundRef > 0)
    hL1eff->Fill(ptuse, this_event_weight);

  // HLT filling
  unsigned int last_module = modules_in_this_event - 1;
  if ((!useMuonFromGenerator) && last_module > 0)
    last_module--;
  for (unsigned int i = 0; i <= last_module; i++) {
    double ptcut = theReferenceThreshold;
    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();
      if (pt > ptcut)
        nFound++;
    }
    hHLTnor[i]->Fill(ptuse, this_event_weight);
    if (nFound > 0)
      hHLTeff[i]->Fill(ptuse, this_event_weight);
  }
}

DEFINE_FWK_MODULE(HLTMuonTurnOnAnalyzer);