Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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