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
/** \class HLTMuonL1Filter
 *
 * See header file for documentation
 *
 *  \author J. Alcaraz
 *
 */

#include "HLTMuonL1Filter.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 "DataFormats/L1GlobalMuonTrigger/interface/L1MuGMTCand.h"
#include "FWCore/Utilities/interface/EDMException.h"

#include "TMath.h"

#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/InputTag.h"

#include <vector>

//
// constructors and destructor
//
HLTMuonL1Filter::HLTMuonL1Filter(const edm::ParameterSet& iConfig)
    : HLTFilter(iConfig),
      l1MuTriggerScalesRcdToken_(esConsumes()),
      candTag_(iConfig.getParameter<edm::InputTag>("CandTag")),
      candToken_(consumes<l1extra::L1MuonParticleCollection>(candTag_)),
      previousCandTag_(iConfig.getParameter<edm::InputTag>("PreviousCandTag")),
      previousCandToken_(consumes<trigger::TriggerFilterObjectWithRefs>(previousCandTag_)),
      maxEta_(iConfig.getParameter<double>("MaxEta")),
      minPt_(iConfig.getParameter<double>("MinPt")),
      minN_(iConfig.getParameter<int>("MinN")),
      excludeSingleSegmentCSC_(iConfig.getParameter<bool>("ExcludeSingleSegmentCSC")),
      csctfTag_(iConfig.getParameter<edm::InputTag>("CSCTFtag")),
      csctfToken_(excludeSingleSegmentCSC_ ? consumes<L1CSCTrackCollection>(csctfTag_)
                                           : edm::EDGetTokenT<L1CSCTrackCollection>{}) {
  using namespace std;

  //set the quality bit mask
  qualityBitMask_ = 0;
  vector<int> selectQualities = iConfig.getParameter<vector<int> >("SelectQualities");
  for (int selectQualitie : selectQualities) {
    if (selectQualitie > 7) {
      throw edm::Exception(edm::errors::Configuration) << "QualityBits must be smaller than 8!";
    }
    qualityBitMask_ |= 1 << selectQualitie;
  }

  // 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 << "    ExcludeSingleSegmentCSC = " << excludeSingleSegmentCSC_ << endl;
    ss << "    CSCTFtag = " << csctfTag_.encode() << endl;
    ss << "    saveTags= " << saveTags();
    LogDebug("HLTMuonL1Filter") << ss.str();
  }
}

HLTMuonL1Filter::~HLTMuonL1Filter() = default;

void HLTMuonL1Filter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  makeHLTFilterDescription(desc);
  desc.add<edm::InputTag>("CandTag", edm::InputTag("hltL1extraParticles"));
  //  desc.add<edm::InputTag>("PreviousCandTag",edm::InputTag("hltL1sL1DoubleMuOpen"));
  desc.add<edm::InputTag>("PreviousCandTag", edm::InputTag(""));
  desc.add<double>("MaxEta", 2.5);
  desc.add<double>("MinPt", 0.0);
  desc.add<int>("MinN", 1);
  desc.add<bool>("ExcludeSingleSegmentCSC", false);
  //  desc.add<edm::InputTag>("CSCTFtag",edm::InputTag("unused"));
  desc.add<edm::InputTag>("CSCTFtag", edm::InputTag("csctfDigis"));
  {
    std::vector<int> temp1;
    temp1.reserve(0);
    desc.add<std::vector<int> >("SelectQualities", temp1);
  }
  descriptions.add("hltMuonL1Filter", desc);
}

//
// member functions
//

// ------------ method called to produce the data  ------------
bool HLTMuonL1Filter::hltFilter(edm::Event& iEvent,
                                const edm::EventSetup& iSetup,
                                trigger::TriggerFilterObjectWithRefs& filterproduct) const {
  using namespace std;
  using namespace edm;
  using namespace trigger;
  using namespace l1extra;

  // 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<L1MuonParticleCollection> allMuons;
  iEvent.getByToken(candToken_, allMuons);

  /// handle for CSCTFtracks
  const L1CSCTrackCollection* csctfTracks = nullptr;
  const L1MuTriggerScales* l1MuTriggerScales = nullptr;

  // get hold of CSCTF raw tracks
  if (excludeSingleSegmentCSC_) {
    edm::Handle<L1CSCTrackCollection> csctfTracksHandle;
    iEvent.getByToken(csctfToken_, csctfTracksHandle);
    csctfTracks = csctfTracksHandle.product();

    // read scales for every event (fast, no need to cache this)
    l1MuTriggerScales = &iSetup.getData(l1MuTriggerScalesRcdToken_);
  }

  // get hold of muons that fired the previous level
  Handle<TriggerFilterObjectWithRefs> previousLevelCands;
  iEvent.getByToken(previousCandToken_, previousLevelCands);
  vector<L1MuonParticleRef> prevMuons;
  previousLevelCands->getObjects(TriggerL1Mu, prevMuons);

  // look at all muon candidates, check cuts and add to filter object
  int n = 0;
  for (size_t i = 0; i < allMuons->size(); i++) {
    L1MuonParticleRef muon(allMuons, i);

    //check if triggered by the previous level
    if (find(prevMuons.begin(), prevMuons.end(), muon) == prevMuons.end())
      continue;

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

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

    //check quality cut
    if (qualityBitMask_) {
      int quality = muon->gmtMuonCand().empty() ? 0 : (1 << muon->gmtMuonCand().quality());
      if ((quality & qualityBitMask_) == 0)
        continue;
    }

    // reject single-segment CSC objects if necessary
    if (excludeSingleSegmentCSC_ and isSingleSegmentCSC(muon, *csctfTracks, *l1MuTriggerScales))
      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()) {
    ostringstream ss;
    ss.precision(2);
    ss << "L1mu#" << '\t' << "q*pt" << '\t' << '\t' << "eta" << '\t' << "phi" << '\t' << "quality" << '\t' << "isPrev"
       << '\t' << "isFired" << '\t' << "isSingleCSC" << endl;
    ss << "--------------------------------------------------------------------------" << endl;

    vector<L1MuonParticleRef> firedMuons;
    filterproduct.getObjects(TriggerL1Mu, firedMuons);
    for (size_t i = 0; i < allMuons->size(); i++) {
      L1MuonParticleRef mu(allMuons, i);
      int quality = mu->gmtMuonCand().empty() ? 0 : mu->gmtMuonCand().quality();
      bool isPrev = find(prevMuons.begin(), prevMuons.end(), mu) != prevMuons.end();
      bool isFired = find(firedMuons.begin(), firedMuons.end(), mu) != firedMuons.end();
      bool isSingleCSC = excludeSingleSegmentCSC_ and isSingleSegmentCSC(mu, *csctfTracks, *l1MuTriggerScales);
      ss << i << '\t' << scientific << mu->charge() * mu->pt() << '\t' << fixed << mu->eta() << '\t' << mu->phi()
         << '\t' << quality << '\t' << isPrev << '\t' << isFired << '\t' << isSingleCSC << endl;
    }
    ss << "--------------------------------------------------------------------------" << endl;
    LogDebug("HLTMuonL1Filter") << ss.str() << "Decision of filter is " << accept
                                << ", number of muons passing = " << filterproduct.l1muonSize();
  }

  return accept;
}

bool HLTMuonL1Filter::isSingleSegmentCSC(l1extra::L1MuonParticleRef const& muon,
                                         L1CSCTrackCollection const& csctfTracks,
                                         L1MuTriggerScales const& l1MuTriggerScales) const {
  // is the muon matching a csctf track?
  //bool matched   = false;     // unused
  // which csctf track mode?
  // -999: no matching
  //  1: bad phi road. Not good extrapolation, but still triggering
  // 11: singles
  // 15: halo
  // 2->10 and 12->14: coincidence trigger with good extrapolation
  int csctfMode = -999;

  // loop over the CSCTF tracks
  for (auto trk = csctfTracks.begin(); trk < csctfTracks.end(); trk++) {
    int trEndcap = (trk->first.endcap() == 2 ? trk->first.endcap() - 3 : trk->first.endcap());
    int trSector = 6 * (trk->first.endcap() - 1) + trk->first.sector();

    //... in radians
    // Type 2 is CSC
    float trEtaScale = l1MuTriggerScales.getRegionalEtaScale(2)->getCenter(trk->first.eta_packed());
    float trPhiScale = l1MuTriggerScales.getPhiScale()->getLowEdge(trk->first.localPhi());

    double trEta = trEtaScale * trEndcap;
    // there is no L1ExtraParticle below -2.375
    if (trEta < -2.4)
      trEta = -2.375;

    // CSCTF has 6 sectors
    // sector 1 starts at 15 degrees
    // trPhiScale is defined inside a sector
    float trPhi02PI = fmod(trPhiScale + ((trSector - 1) * TMath::Pi() / 3) + (TMath::Pi() / 12), 2 * TMath::Pi());

    // L1 information are given from [-Pi,Pi]
    double trPhi = (trPhi02PI < TMath::Pi() ? trPhi02PI : trPhi02PI - 2 * TMath::Pi());
    /*
    std::cout << "\ntrEndcap="               << trEndcap                << std::endl;
    std::cout << "trSector="                 << trSector                << std::endl;
    std::cout << "trk->first.eta_packed()="  << trk->first.eta_packed() << std::endl;
    std::cout << "trk->first.localPhi()="    << trk->first.localPhi()   << std::endl;
    std::cout << "trEtaScale=" << trEtaScale << std::endl;
    std::cout << "trPhiScale=" << trPhiScale << std::endl;
    std::cout << "trEta="      << trEta      << std::endl;
    std::cout << "trPhi="      << trPhi      << std::endl;
    */
    if (fabs(trEta - muon->eta()) < 0.03 && fabs(trPhi - muon->phi()) < 0.001) {
      //matched = true;
      ptadd thePtAddress(trk->first.ptLUTAddress());
      csctfMode = thePtAddress.track_mode;
      //std::cout << "is matched -> trMode=" << csctfMode << std::endl;
    }
  }

  /*
  std::cout << " ===================================== " << std::endl;
  std::cout << " is matched? " << matched                << std::endl;
  std::cout << " is singles? " << (csctfMode==11 ? 1 :0) << std::endl;
  std::cout << " ===================================== " << std::endl;
  */

  // singles are mode 11 "CSCTF tracks"
  return csctfMode == 11;
}

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