Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-05 03:15:10

0001 #include "CommonTools/UtilAlgos/interface/ObjectCountFilter.h"
0002 #include "CommonTools/UtilAlgos/interface/SingleElementCollectionSelector.h"
0003 #include "CommonTools/UtilAlgos/interface/SingleObjectSelector.h"
0004 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0005 #include "DataFormats/Common/interface/RefVector.h"
0006 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0007 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0008 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0009 #include "DataFormats/PatCandidates/interface/Electron.h"
0010 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0011 #include "DataFormats/PatCandidates/interface/IsolatedTrack.h"
0012 #include "DataFormats/PatCandidates/interface/Jet.h"
0013 #include "DataFormats/PatCandidates/interface/MET.h"
0014 #include "DataFormats/PatCandidates/interface/Muon.h"
0015 #include "DataFormats/PatCandidates/interface/PFParticle.h"
0016 #include "DataFormats/PatCandidates/interface/Photon.h"
0017 #include "DataFormats/PatCandidates/interface/Tau.h"
0018 #include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
0019 #include "FWCore/Framework/interface/stream/EDFilter.h"
0020 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0021 
0022 #include <vector>
0023 
0024 namespace pat {
0025 
0026   class PATJetSelector : public edm::stream::EDFilter<> {
0027   public:
0028     PATJetSelector(edm::ParameterSet const& params)
0029         : srcToken_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("src"))),
0030           cut_(params.getParameter<std::string>("cut")),
0031           cutLoose_(params.getParameter<std::string>("cutLoose")),
0032           filter_(params.exists("filter") ? params.getParameter<bool>("filter") : false),
0033           nLoose_(params.getParameter<unsigned>("nLoose")),
0034           selector_(cut_),
0035           selectorLoose_(cutLoose_) {
0036       produces<std::vector<pat::Jet>>();
0037       produces<reco::GenJetCollection>("genJets");
0038       produces<std::vector<CaloTower>>("caloTowers");
0039       produces<reco::PFCandidateCollection>("pfCandidates");
0040       produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
0041     }
0042 
0043     ~PATJetSelector() override {}
0044 
0045     virtual void beginJob() {}
0046     virtual void endJob() {}
0047 
0048     bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
0049       auto patJets = std::make_unique<std::vector<Jet>>();
0050 
0051       auto genJetsOut = std::make_unique<reco::GenJetCollection>();
0052       auto caloTowersOut = std::make_unique<std::vector<CaloTower>>();
0053       auto pfCandidatesOut = std::make_unique<reco::PFCandidateCollection>();
0054       auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
0055 
0056       edm::RefProd<reco::GenJetCollection> h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection>("genJets");
0057       edm::RefProd<std::vector<CaloTower>> h_caloTowersOut =
0058           iEvent.getRefBeforePut<std::vector<CaloTower>>("caloTowers");
0059       edm::RefProd<reco::PFCandidateCollection> h_pfCandidatesOut =
0060           iEvent.getRefBeforePut<reco::PFCandidateCollection>("pfCandidates");
0061       edm::RefProd<edm::OwnVector<reco::BaseTagInfo>> h_tagInfosOut =
0062           iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
0063 
0064       edm::Handle<edm::View<pat::Jet>> h_jets;
0065       iEvent.getByToken(srcToken_, h_jets);
0066 
0067       unsigned nl = 0;  // number of loose jets
0068       // First loop over the products and make the secondary output collections
0069       for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
0070            ijet != iend;
0071            ++ijet) {
0072         bool selectedLoose = false;
0073         if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
0074           selectedLoose = true;
0075           ++nl;
0076         }
0077 
0078         if (selector_(*ijet) || selectedLoose) {
0079           // Copy over the calo towers
0080           for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
0081                                                      itowerEnd = ijet->caloTowersFwdPtr().end(),
0082                                                      itower = itowerBegin;
0083                itower != itowerEnd;
0084                ++itower) {
0085             // Add to global calo tower list
0086             caloTowersOut->push_back(**itower);
0087           }
0088 
0089           // Copy over the pf candidates
0090           for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
0091                                                              icandEnd = ijet->pfCandidatesFwdPtr().end(),
0092                                                              icand = icandBegin;
0093                icand != icandEnd;
0094                ++icand) {
0095             // Add to global pf candidate list
0096             pfCandidatesOut->push_back(**icand);
0097           }
0098 
0099           // Copy the tag infos
0100           for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
0101                                                        iinfoEnd = ijet->tagInfosFwdPtr().end(),
0102                                                        iinfo = iinfoBegin;
0103                iinfo != iinfoEnd;
0104                ++iinfo) {
0105             // Add to global calo tower list
0106             tagInfosOut->push_back(**iinfo);
0107           }
0108 
0109           // Copy the gen jet
0110           if (ijet->genJet() != nullptr) {
0111             genJetsOut->push_back(*(ijet->genJet()));
0112           }
0113         }
0114       }
0115 
0116       // Output the secondary collections.
0117       edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put(std::move(genJetsOut), "genJets");
0118       edm::OrphanHandle<std::vector<CaloTower>> oh_caloTowersOut = iEvent.put(std::move(caloTowersOut), "caloTowers");
0119       edm::OrphanHandle<reco::PFCandidateCollection> oh_pfCandidatesOut =
0120           iEvent.put(std::move(pfCandidatesOut), "pfCandidates");
0121       edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo>> oh_tagInfosOut =
0122           iEvent.put(std::move(tagInfosOut), "tagInfos");
0123 
0124       unsigned int caloTowerIndex = 0;
0125       unsigned int pfCandidateIndex = 0;
0126       unsigned int tagInfoIndex = 0;
0127       unsigned int genJetIndex = 0;
0128       // Now set the Ptrs with the orphan handles.
0129       nl = 0;  // Reset number of loose jets
0130       for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
0131            ijet != iend;
0132            ++ijet) {
0133         bool selectedLoose = false;
0134         if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
0135           selectedLoose = true;
0136           ++nl;
0137         }
0138 
0139         if (selector_(*ijet) || selectedLoose) {
0140           // Add the jets that pass to the output collection
0141           patJets->push_back(*ijet);
0142 
0143           // Copy over the calo towers
0144           for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
0145                                                      itowerEnd = ijet->caloTowersFwdPtr().end(),
0146                                                      itower = itowerBegin;
0147                itower != itowerEnd;
0148                ++itower) {
0149             // Update the "forward" bit of the FwdPtr to point at the new tower collection.
0150 
0151             //  ptr to "this" tower in the global list
0152             edm::Ptr<CaloTower> outPtr(oh_caloTowersOut, caloTowerIndex);
0153             patJets->back().updateFwdCaloTowerFwdPtr(itower - itowerBegin,  // index of "this" tower in the jet
0154                                                      outPtr);
0155             ++caloTowerIndex;
0156           }
0157 
0158           // Copy over the pf candidates
0159           for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
0160                                                              icandEnd = ijet->pfCandidatesFwdPtr().end(),
0161                                                              icand = icandBegin;
0162                icand != icandEnd;
0163                ++icand) {
0164             // Update the "forward" bit of the FwdPtr to point at the new tower collection.
0165 
0166             // ptr to "this" cand in the global list
0167             edm::Ptr<reco::PFCandidate> outPtr(oh_pfCandidatesOut, pfCandidateIndex);
0168             patJets->back().updateFwdPFCandidateFwdPtr(icand - icandBegin,  // index of "this" tower in the jet
0169                                                        outPtr);
0170             ++pfCandidateIndex;
0171           }
0172 
0173           // Copy the tag infos
0174           for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
0175                                                        iinfoEnd = ijet->tagInfosFwdPtr().end(),
0176                                                        iinfo = iinfoBegin;
0177                iinfo != iinfoEnd;
0178                ++iinfo) {
0179             // Update the "forward" bit of the FwdPtr to point at the new tower collection.
0180 
0181             // ptr to "this" info in the global list
0182             edm::Ptr<reco::BaseTagInfo> outPtr(oh_tagInfosOut, tagInfoIndex);
0183             patJets->back().updateFwdTagInfoFwdPtr(iinfo - iinfoBegin,  // index of "this" tower in the jet
0184                                                    outPtr);
0185             ++tagInfoIndex;
0186           }
0187 
0188           // Copy the gen jet
0189           if (ijet->genJet() != nullptr) {
0190             patJets->back().updateFwdGenJetFwdRef(
0191                 edm::Ref<reco::GenJetCollection>(oh_genJetsOut, genJetIndex)  // ref to "this" genjet in the global list
0192             );
0193             ++genJetIndex;
0194           }
0195         }
0196       }
0197 
0198       // put genEvt  in Event
0199       bool pass = !patJets->empty();
0200       iEvent.put(std::move(patJets));
0201 
0202       if (filter_)
0203         return pass;
0204       else
0205         return true;
0206     }
0207 
0208     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0209       edm::ParameterSetDescription iDesc;
0210       iDesc.setComment("Energy Correlation Functions adder");
0211       iDesc.add<edm::InputTag>("src", edm::InputTag("no default"))->setComment("input collection");
0212       iDesc.add<std::string>("cut", "")->setComment("Jet selection.");
0213       iDesc.add<std::string>("cutLoose", "")->setComment("Loose jet selection. Will keep nLoose loose jets.");
0214       iDesc.add<bool>("filter", false)->setComment("Filter selection?");
0215       iDesc.add<unsigned>("nLoose", 0)->setComment("Keep nLoose loose jets that satisfy cutLoose");
0216       descriptions.add("PATJetSelector", iDesc);
0217     }
0218 
0219   protected:
0220     const edm::EDGetTokenT<edm::View<pat::Jet>> srcToken_;
0221     const std::string cut_;
0222     const std::string cutLoose_;  // Cut to define loose jets.
0223     const bool filter_;
0224     const unsigned nLoose_;  // If desired, keep nLoose loose jets.
0225     const StringCutObjectSelector<Jet> selector_;
0226     const StringCutObjectSelector<Jet> selectorLoose_;  // Selector for loose jets.
0227   };
0228 
0229 }  // namespace pat
0230 
0231 namespace pat {
0232 
0233   typedef SingleObjectSelector<std::vector<Electron>, StringCutObjectSelector<Electron>> PATElectronSelector;
0234   typedef SingleObjectSelector<std::vector<Muon>, StringCutObjectSelector<Muon>> PATMuonSelector;
0235   typedef SingleObjectSelector<std::vector<Tau>, StringCutObjectSelector<Tau>> PATTauSelector;
0236   typedef SingleObjectSelector<std::vector<Photon>, StringCutObjectSelector<Photon>> PATPhotonSelector;
0237   /* typedef SingleObjectSelector< */
0238   /*             std::vector<Jet>, */
0239   /*             StringCutObjectSelector<Jet> */
0240   /*         > PATJetSelector; */
0241   typedef SingleObjectSelector<std::vector<MET>, StringCutObjectSelector<MET>> PATMETSelector;
0242   typedef SingleObjectSelector<std::vector<PFParticle>, StringCutObjectSelector<PFParticle>> PATPFParticleSelector;
0243   typedef SingleObjectSelector<
0244       std::vector<CompositeCandidate>,
0245       StringCutObjectSelector<CompositeCandidate, true>  // true => lazy parsing => get all methods of daughters
0246       >
0247       PATCompositeCandidateSelector;
0248   typedef SingleObjectSelector<std::vector<TriggerObjectStandAlone>, StringCutObjectSelector<TriggerObjectStandAlone>>
0249       PATTriggerObjectStandAloneSelector;
0250   typedef SingleObjectSelector<std::vector<GenericParticle>, StringCutObjectSelector<GenericParticle>>
0251       PATGenericParticleSelector;
0252 
0253   typedef SingleObjectSelector<
0254       std::vector<PackedCandidate>,
0255       StringCutObjectSelector<PackedCandidate, true>  // true => lazy parsing => get all methods of daughters
0256       >
0257       PATPackedCandidateSelector;
0258 
0259   typedef SingleObjectSelector<std::vector<edm::Ptr<PackedCandidate>>,
0260                                StringCutObjectSelector<edm::Ptr<PackedCandidate>, true>,
0261                                std::vector<edm::Ptr<PackedCandidate>>>
0262       PATPackedCandidatePtrSelector;
0263 
0264   typedef SingleObjectSelector<std::vector<edm::Ptr<PackedGenParticle>>,
0265                                StringCutObjectSelector<edm::Ptr<PackedGenParticle>, true>,
0266                                std::vector<edm::Ptr<PackedGenParticle>>>
0267       PATPackedGenParticlePtrSelector;
0268 
0269   typedef SingleObjectSelector<std::vector<Electron>,
0270                                StringCutObjectSelector<Electron>,
0271                                edm::RefVector<std::vector<Electron>>>
0272       PATElectronRefSelector;
0273   typedef SingleObjectSelector<std::vector<Muon>, StringCutObjectSelector<Muon>, edm::RefVector<std::vector<Muon>>>
0274       PATMuonRefSelector;
0275   typedef SingleObjectSelector<std::vector<Tau>, StringCutObjectSelector<Tau>, edm::RefVector<std::vector<Tau>>>
0276       PATTauRefSelector;
0277   typedef SingleObjectSelector<std::vector<Photon>, StringCutObjectSelector<Photon>, edm::RefVector<std::vector<Photon>>>
0278       PATPhotonRefSelector;
0279   typedef SingleObjectSelector<std::vector<Jet>, StringCutObjectSelector<Jet>, edm::RefVector<std::vector<Jet>>>
0280       PATJetRefSelector;
0281   typedef SingleObjectSelector<std::vector<MET>, StringCutObjectSelector<MET>, edm::RefVector<std::vector<MET>>>
0282       PATMETRefSelector;
0283   typedef SingleObjectSelector<std::vector<PFParticle>,
0284                                StringCutObjectSelector<PFParticle>,
0285                                edm::RefVector<std::vector<PFParticle>>>
0286       PATPFParticleRefSelector;
0287   typedef SingleObjectSelector<std::vector<GenericParticle>,
0288                                StringCutObjectSelector<GenericParticle>,
0289                                edm::RefVector<std::vector<GenericParticle>>>
0290       PATGenericParticleRefSelector;
0291   typedef SingleObjectSelector<
0292       std::vector<CompositeCandidate>,
0293       StringCutObjectSelector<CompositeCandidate, true>,  // true => lazy parsing => get all methods of daughters
0294       edm::RefVector<std::vector<CompositeCandidate>>>
0295       PATCompositeCandidateRefSelector;
0296 
0297   typedef SingleObjectSelector<pat::IsolatedTrackCollection, StringCutObjectSelector<pat::IsolatedTrack>>
0298       IsoTrackSelector;
0299 
0300   typedef ObjectCountFilter<pat::MuonCollection, StringCutObjectSelector<pat::Muon>>::type MuonRefPatCount;
0301 
0302 }  // namespace pat
0303 
0304 using namespace pat;
0305 
0306 #include "FWCore/Framework/interface/MakerMacros.h"
0307 DEFINE_FWK_MODULE(PATElectronSelector);
0308 DEFINE_FWK_MODULE(PATMuonSelector);
0309 DEFINE_FWK_MODULE(PATTauSelector);
0310 DEFINE_FWK_MODULE(PATPhotonSelector);
0311 DEFINE_FWK_MODULE(PATJetSelector);
0312 DEFINE_FWK_MODULE(PATMETSelector);
0313 DEFINE_FWK_MODULE(PATPFParticleSelector);
0314 DEFINE_FWK_MODULE(PATCompositeCandidateSelector);
0315 DEFINE_FWK_MODULE(PATTriggerObjectStandAloneSelector);
0316 DEFINE_FWK_MODULE(PATGenericParticleSelector);
0317 DEFINE_FWK_MODULE(PATPackedCandidateSelector);
0318 DEFINE_FWK_MODULE(PATPackedCandidatePtrSelector);
0319 DEFINE_FWK_MODULE(PATPackedGenParticlePtrSelector);
0320 
0321 DEFINE_FWK_MODULE(PATElectronRefSelector);
0322 DEFINE_FWK_MODULE(PATMuonRefSelector);
0323 DEFINE_FWK_MODULE(PATTauRefSelector);
0324 DEFINE_FWK_MODULE(PATPhotonRefSelector);
0325 DEFINE_FWK_MODULE(PATJetRefSelector);
0326 DEFINE_FWK_MODULE(PATMETRefSelector);
0327 DEFINE_FWK_MODULE(PATPFParticleRefSelector);
0328 DEFINE_FWK_MODULE(PATGenericParticleRefSelector);
0329 DEFINE_FWK_MODULE(PATCompositeCandidateRefSelector);
0330 
0331 DEFINE_FWK_MODULE(IsoTrackSelector);
0332 DEFINE_FWK_MODULE(MuonRefPatCount);