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
/* \class HLTMuonL1TFilter
 *
 * This is an HLTFilter implementing filtering on L1T Stage2 GMT objects
 * 
 * \author:  V. Rekovic
 *
 */

#include "HLTMuonL1TFilter.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h"
#include "DataFormats/HLTReco/interface/TriggerRefsCollections.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Math/interface/deltaR.h"
#include "TMath.h"

#include <vector>

HLTMuonL1TFilter::HLTMuonL1TFilter(const edm::ParameterSet& iConfig)
    : HLTFilter(iConfig),
      candTag_(iConfig.getParameter<edm::InputTag>("CandTag")),
      candToken_(consumes<l1t::MuonBxCollection>(candTag_)),
      previousCandTag_(iConfig.getParameter<edm::InputTag>("PreviousCandTag")),
      previousCandToken_(consumes<trigger::TriggerFilterObjectWithRefs>(previousCandTag_)),
      maxEta_(iConfig.getParameter<double>("MaxEta")),
      minPt_(iConfig.getParameter<double>("MinPt")),
      maxDR_(iConfig.getParameter<double>("MaxDeltaR")),
      maxDR2_(maxDR_ * maxDR_),
      minN_(iConfig.getParameter<int>("MinN")),
      centralBxOnly_(iConfig.getParameter<bool>("CentralBxOnly")) {
  //set the quality bit mask
  qualityBitMask_ = 0;
  vector<int> selectQualities = iConfig.getParameter<vector<int> >("SelectQualities");
  for (int selectQualitie : selectQualities) {
    //     if(selectQualities[i] > 7){  // FIXME: this will be updated once we have info from L1
    //       throw edm::Exception(edm::errors::Configuration) << "QualityBits must be smaller than 8!";
    //     }
    qualityBitMask_ |= 1 << selectQualitie;
  }
  //make sure cut parameter for candidate matching is strictly positive
  if (maxDR_ <= 0.) {
    throw cms::Exception("HLTMuonL1TFilterConfiguration")
        << "invalid value for parameter \"MaxDeltaR\" (must be > 0): " << maxDR_;
  }
  // dump parameters for debugging
  if (edm::isDebugEnabled()) {
    ostringstream ss;
    ss << "Constructed with parameters:" << endl;
    ss << "    CandTag = " << candTag_.encode() << endl;
    ss << "    PreviousCandTag = " << previousCandTag_.encode() << endl;
    ss << "    MaxEta = " << maxEta_ << endl;
    ss << "    MinPt = " << minPt_ << endl;
    ss << "    SelectQualities =";
    //     for(size_t i = 0; i < 8; i++){
    //       if((qualityBitMask_>>i) % 2) ss << " " << i;
    //     }
    ss << endl;
    ss << "    MinN = " << minN_ << endl;
    ss << "    saveTags= " << saveTags();
    LogDebug("HLTMuonL1TFilter") << ss.str();
  }
}

HLTMuonL1TFilter::~HLTMuonL1TFilter() = default;

void HLTMuonL1TFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  makeHLTFilterDescription(desc);
  desc.add<edm::InputTag>("CandTag", edm::InputTag("hltGmtStage2Digis"));
  desc.add<edm::InputTag>("PreviousCandTag", edm::InputTag(""));
  desc.add<double>("MaxEta", 2.5);
  desc.add<double>("MinPt", 0.0);
  desc.add<double>("MaxDeltaR", 0.3);
  desc.add<int>("MinN", 1);
  desc.add<bool>("CentralBxOnly", true);
  {
    std::vector<int> temp1;
    temp1.reserve(0);
    desc.add<std::vector<int> >("SelectQualities", temp1);
  }
  descriptions.add("hltMuonL1TFilter", desc);
}

bool HLTMuonL1TFilter::hltFilter(edm::Event& iEvent,
                                 const edm::EventSetup& iSetup,
                                 trigger::TriggerFilterObjectWithRefs& filterproduct) const {
  using namespace std;
  using namespace edm;
  using namespace trigger;
  using namespace l1t;

  // All HLT filters must create and fill an HLT filter object,
  // recording any reconstructed physics objects satisfying (or not)
  // this HLT filter, and place it in the Event.

  // get hold of all muons
  Handle<MuonBxCollection> allMuons;
  iEvent.getByToken(candToken_, allMuons);

  // get hold of TFOWRs that fired the previous level
  Handle<TriggerFilterObjectWithRefs> previousLevelTFOWR;
  iEvent.getByToken(previousCandToken_, previousLevelTFOWR);

  vector<MuonRef> prevMuons;
  previousLevelTFOWR->getObjects(TriggerL1Mu, prevMuons);

  // look at all muon candidates, check cuts and add to filter object
  int n = 0;

  for (int ibx = allMuons->getFirstBX(); ibx <= allMuons->getLastBX(); ++ibx) {
    if (centralBxOnly_ && (ibx != 0))
      continue;
    for (auto it = allMuons->begin(ibx); it != allMuons->end(ibx); it++) {
      MuonRef muon(allMuons, distance(allMuons->begin(allMuons->getFirstBX()), it));

      //check maxEta cut
      if (fabs(muon->eta()) > maxEta_)
        continue;

      //check pT cut
      if (muon->pt() < minPt_)
        continue;

      //check quality cut
      if (qualityBitMask_) {
        int quality = (it->hwQual() == 0 ? 0 : (1 << it->hwQual()));
        if ((quality & qualityBitMask_) == 0)
          continue;
      }

      // Only select muons that were selected in the previous level
      bool matchPrevL1 = false;
      int prevSize = prevMuons.size();
      for (int it2 = 0; it2 < prevSize; it2++) {
        if (deltaR2(muon->eta(), muon->phi(), prevMuons[it2]->eta(), prevMuons[it2]->phi()) < maxDR2_) {
          matchPrevL1 = true;
          break;
        }
      }
      if (!matchPrevL1)
        continue;

      //we have a good candidate
      n++;
      filterproduct.addObject(TriggerL1Mu, muon);
    }
  }

  if (saveTags())
    filterproduct.addCollectionTag(candTag_);

  // filter decision
  const bool accept(n >= minN_);

  // dump event for debugging
  if (edm::isDebugEnabled()) {
    LogTrace("HLTMuonL1TFilter") << "\nHLTMuonL1TFilter -----------------------------------------------" << endl;
    LogTrace("HLTMuonL1TFilter") << "L1mu#" << '\t' << "q" << '\t' << "pt" << '\t' << '\t' << "eta" << '\t' << "phi"
                                 << '\t' << "quality" << '\t' << "isPrev\t (|maxEta| = " << maxEta_ << ")" << endl;
    LogTrace("HLTMuonL1TFilter") << "--------------------------------------------------------------------------"
                                 << endl;

    vector<MuonRef> firedMuons;
    filterproduct.getObjects(TriggerL1Mu, firedMuons);
    for (size_t i = 0; i < firedMuons.size(); i++) {
      l1t::MuonRef mu = firedMuons[i];
      bool isPrev = find(prevMuons.begin(), prevMuons.end(), mu) != prevMuons.end();
      LogTrace("HLTMuonL1TFilter") << i << '\t' << setprecision(2) << scientific << mu->charge() << '\t' << mu->pt()
                                   << '\t' << fixed << mu->eta() << '\t' << mu->phi() << '\t' << mu->hwQual() << '\t'
                                   << isPrev << endl;
    }
    LogTrace("HLTMuonL1TFilter") << "--------------------------------------------------------------------------"
                                 << endl;
    LogTrace("HLTMuonL1TFilter") << "Decision of this filter is " << accept
                                 << ", number of muons passing = " << filterproduct.l1tmuonSize();
  }

  return accept;
}

// declare this class as a framework plugin
#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(HLTMuonL1TFilter);