Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:49

0001 //
0002 // $Id: PATGenJetSlimmer.cc,v 1.1 2011/03/24 18:45:45 mwlebour Exp $
0003 //
0004 
0005 /**
0006   \class    pat::PATGenJetSlimmer PATGenJetSlimmer.h "PhysicsTools/PatAlgos/interface/PATGenJetSlimmer.h"
0007   \brief    Matcher of reconstructed objects to L1 Muons 
0008             
0009   \author   Giovanni Petrucciani
0010 */
0011 
0012 #include <vector>
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0018 #include "DataFormats/Common/interface/Association.h"
0019 #include "DataFormats/Common/interface/RefToPtr.h"
0020 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0021 #include "DataFormats/JetReco/interface/GenJet.h"
0022 
0023 namespace pat {
0024 
0025   class PATGenJetSlimmer : public edm::stream::EDProducer<> {
0026   public:
0027     explicit PATGenJetSlimmer(const edm::ParameterSet& iConfig);
0028     ~PATGenJetSlimmer() override {}
0029 
0030     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0031 
0032   private:
0033     const edm::EDGetTokenT<edm::View<reco::GenJet> > src_;
0034     const edm::EDGetTokenT<edm::Association<std::vector<pat::PackedGenParticle> > > gp2pgp_;
0035 
0036     const StringCutObjectSelector<reco::GenJet> cut_;
0037     const StringCutObjectSelector<reco::GenJet> cutLoose_;
0038     const unsigned nLoose_;
0039 
0040     /// reset daughters to an empty vector
0041     const bool clearDaughters_;
0042     /// drop the specific
0043     const bool dropSpecific_;
0044   };
0045 
0046 }  // namespace pat
0047 
0048 pat::PATGenJetSlimmer::PATGenJetSlimmer(const edm::ParameterSet& iConfig)
0049     : src_(consumes<edm::View<reco::GenJet> >(iConfig.getParameter<edm::InputTag>("src"))),
0050       gp2pgp_(consumes<edm::Association<std::vector<pat::PackedGenParticle> > >(
0051           iConfig.getParameter<edm::InputTag>("packedGenParticles"))),
0052       cut_(iConfig.getParameter<std::string>("cut")),
0053       cutLoose_(iConfig.getParameter<std::string>("cutLoose")),
0054       nLoose_(iConfig.getParameter<unsigned>("nLoose")),
0055       clearDaughters_(iConfig.getParameter<bool>("clearDaughters")),
0056       dropSpecific_(iConfig.getParameter<bool>("dropSpecific")) {
0057   produces<std::vector<reco::GenJet> >();
0058   produces<edm::Association<std::vector<reco::GenJet> > >("slimmedGenJetAssociation");
0059 }
0060 
0061 void pat::PATGenJetSlimmer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0062   using namespace edm;
0063   using namespace std;
0064 
0065   Handle<View<reco::GenJet> > src;
0066   iEvent.getByToken(src_, src);
0067 
0068   auto out = std::make_unique<vector<reco::GenJet> >();
0069   out->reserve(src->size());
0070 
0071   Handle<edm::Association<std::vector<pat::PackedGenParticle> > > gp2pgp;
0072   iEvent.getByToken(gp2pgp_, gp2pgp);
0073 
0074   auto genJetSlimmedGenJetAssociation = make_unique<edm::Association<std::vector<reco::GenJet> > >();
0075 
0076   auto mapping = std::make_unique<std::vector<int> >();
0077   mapping->reserve(src->size());
0078 
0079   unsigned nm = 0;  // number of mapped jets
0080   unsigned nl = 0;  // number of loose jets
0081   for (View<reco::GenJet>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0082     bool selectedLoose = false;
0083     if (nLoose_ > 0 && nl < nLoose_ && cutLoose_(*it)) {
0084       selectedLoose = true;
0085       ++nl;
0086     }
0087 
0088     bool pass = cut_(*it) || selectedLoose;
0089     if (!pass) {
0090       mapping->push_back(-1);
0091       continue;
0092     }
0093 
0094     out->push_back(*it);
0095     reco::GenJet& jet = out->back();
0096 
0097     mapping->push_back(nm++);
0098 
0099     if (clearDaughters_) {
0100       jet.clearDaughters();
0101     } else  // rekey
0102     {
0103       //copy old
0104       reco::CompositePtrCandidate::daughters old = jet.daughterPtrVector();
0105       jet.clearDaughters();
0106       std::map<unsigned int, reco::CandidatePtr> ptrs;
0107       for (unsigned int i = 0; i < old.size(); i++) {
0108         //  if(! ((*gp2pgp)[old[i]]).isNonnull())   {
0109         //      std::cout << "Missing ref for key"  <<  old[i].key() << " pdgid " << old[i]->pdgId() << " st "<<   old[i]->status() <<  " pt " << old[i]->pt() << " eta " << old[i]->eta() << std::endl;
0110         //  }
0111         ptrs[((*gp2pgp)[old[i]]).key()] = refToPtr((*gp2pgp)[old[i]]);
0112       }
0113       for (std::map<unsigned int, reco::CandidatePtr>::iterator itp = ptrs.begin(); itp != ptrs.end();
0114            itp++)  //iterate on sorted items
0115       {
0116         jet.addDaughter(itp->second);
0117       }
0118     }
0119     if (dropSpecific_) {
0120       jet.setSpecific(reco::GenJet::Specific());
0121     }
0122   }
0123 
0124   edm::OrphanHandle<std::vector<reco::GenJet> > orphanHandle = iEvent.put(std::move(out));
0125 
0126   auto asso = std::make_unique<edm::Association<std::vector<reco::GenJet> > >(orphanHandle);
0127   edm::Association<std::vector<reco::GenJet> >::Filler slimmedAssoFiller(*asso);
0128   slimmedAssoFiller.insert(src, mapping->begin(), mapping->end());
0129   slimmedAssoFiller.fill();
0130 
0131   iEvent.put(std::move(asso), "slimmedGenJetAssociation");
0132 }
0133 
0134 #include "FWCore/Framework/interface/MakerMacros.h"
0135 using namespace pat;
0136 DEFINE_FWK_MODULE(PATGenJetSlimmer);