Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-15 01:25:30

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