Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:20:35

0001 /** \class HLTMuonIsoFilter
0002  *
0003  * See header file for documentation
0004  *
0005  *  \author J. Alcaraz
0006  *
0007  */
0008 
0009 #include "HLTMuonIsoFilter.h"
0010 
0011 #include "DataFormats/Common/interface/Handle.h"
0012 #include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h"
0013 #include "DataFormats/HLTReco/interface/TriggerRefsCollections.h"
0014 
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include "DataFormats/TrackReco/interface/Track.h"
0018 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
0019 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
0020 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
0021 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidateFwd.h"
0022 
0023 #include "RecoMuon/MuonIsolation/interface/MuonIsolatorFactory.h"
0024 
0025 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0026 
0027 #include "FWCore/Framework/interface/ConsumesCollector.h"
0028 
0029 #include <iostream>
0030 //
0031 // constructors and destructor
0032 //
0033 HLTMuonIsoFilter::HLTMuonIsoFilter(const edm::ParameterSet& iConfig)
0034     : HLTFilter(iConfig),
0035       candTag_(iConfig.getParameter<edm::InputTag>("CandTag")),
0036       candToken_(consumes<reco::RecoChargedCandidateCollection>(candTag_)),
0037       previousCandTag_(iConfig.getParameter<edm::InputTag>("PreviousCandTag")),
0038       previousCandToken_(consumes<trigger::TriggerFilterObjectWithRefs>(previousCandTag_)),
0039       depTag_(iConfig.getParameter<std::vector<edm::InputTag> >("DepTag")),
0040       depToken_(),
0041       min_N_(iConfig.getParameter<int>("MinN")) {
0042   depToken_.reserve(depTag_.size());
0043   for (auto const& t : depTag_) {
0044     depToken_.push_back(consumes<edm::ValueMap<reco::IsoDeposit> >(t));
0045   }
0046   decMapToken_ = consumes<edm::ValueMap<bool> >(depTag_.front());
0047 
0048   LogDebug("HLTMuonIsoFilter").log([this](auto& iLog) {
0049     iLog << " candTag : " << candTag_.encode() << "\n";
0050     int i = 0;
0051     for (auto const& t : depTag_) {
0052       iLog << " IsoTag[" << i++ << "] : " << t.encode() << " \n";
0053     }
0054     iLog << "  MinN : " << min_N_;
0055   });
0056 
0057   edm::ParameterSet isolatorPSet = iConfig.getParameter<edm::ParameterSet>("IsolatorPSet");
0058   if (not isolatorPSet.empty()) {
0059     std::string type = isolatorPSet.getParameter<std::string>("ComponentName");
0060     theDepositIsolator = std::unique_ptr<muonisolation::MuIsoBaseIsolator>(
0061         MuonIsolatorFactory::get()->create(type, isolatorPSet, consumesCollector()));
0062   }
0063 
0064   if (theDepositIsolator)
0065     produces<edm::ValueMap<bool> >();
0066 }
0067 
0068 HLTMuonIsoFilter::~HLTMuonIsoFilter() = default;
0069 
0070 //
0071 // member functions
0072 //
0073 void HLTMuonIsoFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0074   edm::ParameterSetDescription desc;
0075   makeHLTFilterDescription(desc);
0076   desc.add<edm::InputTag>("CandTag", edm::InputTag("hltL3MuonCandidates"));
0077   desc.add<edm::InputTag>("PreviousCandTag", edm::InputTag(""));
0078   desc.add<int>("MinN", 1);
0079   std::vector<edm::InputTag> depTag(1, edm::InputTag("hltL3MuonIsolations"));
0080   desc.add<std::vector<edm::InputTag> >("DepTag", depTag);
0081   edm::ParameterSetDescription isolatorPSet;
0082   desc.add<edm::ParameterSetDescription>("IsolatorPSet", isolatorPSet);
0083   descriptions.add("hltMuonIsoFilter", desc);
0084 }
0085 
0086 // ------------ method called to produce the data  ------------
0087 bool HLTMuonIsoFilter::hltFilter(edm::Event& iEvent,
0088                                  const edm::EventSetup& iSetup,
0089                                  trigger::TriggerFilterObjectWithRefs& filterproduct) const {
0090   using namespace std;
0091   using namespace edm;
0092   using namespace trigger;
0093   using namespace reco;
0094 
0095   // All HLT filters must create and fill an HLT filter object,
0096   // recording any reconstructed physics objects satisfying (or not)
0097   // this HLT filter, and place it in the Event.
0098 
0099   //the decision map
0100   std::unique_ptr<edm::ValueMap<bool> > isoMap(new edm::ValueMap<bool>());
0101 
0102   // get hold of trks
0103   Handle<RecoChargedCandidateCollection> mucands;
0104   if (saveTags())
0105     filterproduct.addCollectionTag(candTag_);
0106   iEvent.getByToken(candToken_, mucands);
0107   Handle<TriggerFilterObjectWithRefs> previousLevelCands;
0108   iEvent.getByToken(previousCandToken_, previousLevelCands);
0109   vector<RecoChargedCandidateRef> vcands;
0110   previousLevelCands->getObjects(TriggerMuon, vcands);
0111 
0112   //get hold of energy deposition
0113   unsigned int nDep = depTag_.size();
0114   std::vector<Handle<edm::ValueMap<reco::IsoDeposit> > > depMap(nDep);
0115   Handle<edm::ValueMap<bool> > decisionMap;
0116   muonisolation::MuIsoBaseIsolator::DepositContainer isoContainer(nDep);
0117 
0118   if (theDepositIsolator) {
0119     for (unsigned int i = 0; i != nDep; ++i)
0120       iEvent.getByToken(depToken_[i], depMap[i]);
0121   } else {
0122     bool success = iEvent.getByToken(decMapToken_, decisionMap);
0123     LogDebug("HLTMuonIsoFilter") << "get decisionMap " << success;
0124   }
0125 
0126   // look at all mucands,  check cuts and add to filter object
0127   int nIsolatedMu = 0;
0128   unsigned int nMu = mucands->size();
0129   std::vector<bool> isos(nMu, false);
0130   unsigned int iMu = 0;
0131   for (; iMu < nMu; iMu++) {
0132     RecoChargedCandidateRef candref(mucands, iMu);
0133     LogDebug("HLTMuonIsoFilter") << "candref isNonnull " << candref.isNonnull();
0134 
0135     //did this candidate triggered at previous stage.
0136     if (!triggerdByPreviousLevel(candref, vcands))
0137       continue;
0138 
0139     //reference to the track
0140     TrackRef tk = candref->get<TrackRef>();
0141     LogDebug("HLTMuonIsoFilter") << "tk isNonNull " << tk.isNonnull();
0142     if (theDepositIsolator) {
0143       //get the deposits
0144       for (unsigned int iDep = 0; iDep != nDep; ++iDep) {
0145         const edm::ValueMap<reco::IsoDeposit>::value_type& muonDeposit = (*(depMap[iDep]))[candref];
0146         LogDebug("HLTMuonIsoFilter") << " Muon with q*pt= " << tk->charge() * tk->pt() << " ("
0147                                      << candref->charge() * candref->pt() << ") "
0148                                      << ", eta= " << tk->eta() << " (" << candref->eta() << ") "
0149                                      << "; has deposit[" << iDep << "]: " << muonDeposit.print();
0150         isoContainer[iDep] = muonisolation::MuIsoBaseIsolator::DepositAndVetos(&muonDeposit);
0151       }
0152 
0153       //get the selection
0154       muonisolation::MuIsoBaseIsolator::Result selection = theDepositIsolator->result(isoContainer, *tk);
0155       isos[iMu] = selection.valBool;
0156 
0157     } else {
0158       //get the decision from the event
0159       isos[iMu] = (*decisionMap)[candref];
0160     }
0161     LogDebug("HLTMuonIsoFilter") << " Muon with q*pt= " << tk->charge() * tk->pt() << ", eta= " << tk->eta() << "; "
0162                                  << (isos[iMu] ? "Is an isolated muon." : "Is NOT an isolated muon.");
0163 
0164     if (!isos[iMu])
0165       continue;
0166 
0167     nIsolatedMu++;
0168     filterproduct.addObject(TriggerMuon, candref);
0169   }
0170 
0171   // filter decision
0172   const bool accept(nIsolatedMu >= min_N_);
0173 
0174   if (theDepositIsolator) {
0175     //put the decision map
0176     if (nMu != 0) {
0177       edm::ValueMap<bool>::Filler isoFiller(*isoMap);
0178       isoFiller.insert(mucands, isos.begin(), isos.end());
0179       isoFiller.fill();
0180     }
0181     iEvent.put(std::move(isoMap));
0182   }
0183 
0184   LogDebug("HLTMuonIsoFilter") << " >>>>> Result of HLTMuonIsoFilter is " << accept
0185                                << ", number of muons passing isolation cuts= " << nIsolatedMu;
0186 
0187   return accept;
0188 }
0189 
0190 bool HLTMuonIsoFilter::triggerdByPreviousLevel(const reco::RecoChargedCandidateRef& candref,
0191                                                const std::vector<reco::RecoChargedCandidateRef>& vcands) {
0192   bool ok = false;
0193   unsigned int i = 0;
0194   unsigned int i_max = vcands.size();
0195   for (; i != i_max; ++i) {
0196     if (candref == vcands[i]) {
0197       ok = true;
0198       break;
0199     }
0200   }
0201 
0202   return ok;
0203 }
0204 
0205 // declare this class as a framework plugin
0206 #include "FWCore/Framework/interface/MakerMacros.h"
0207 DEFINE_FWK_MODULE(HLTMuonIsoFilter);