CMS 3D CMS Logo

PATGenJetSlimmer.cc
Go to the documentation of this file.
1 //
2 // $Id: PATGenJetSlimmer.cc,v 1.1 2011/03/24 18:45:45 mwlebour Exp $
3 //
4 
12 #include <vector>
22 
23 namespace pat {
24 
26  public:
27  explicit PATGenJetSlimmer(const edm::ParameterSet& iConfig);
28  ~PATGenJetSlimmer() override {}
29 
30  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
31 
32  private:
35 
38  const unsigned nLoose_;
39 
41  const bool clearDaughters_;
43  const bool dropSpecific_;
44  };
45 
46 } // namespace pat
47 
49  : src_(consumes<edm::View<reco::GenJet> >(iConfig.getParameter<edm::InputTag>("src"))),
50  gp2pgp_(consumes<edm::Association<std::vector<pat::PackedGenParticle> > >(
51  iConfig.getParameter<edm::InputTag>("packedGenParticles"))),
52  cut_(iConfig.getParameter<std::string>("cut")),
53  cutLoose_(iConfig.getParameter<std::string>("cutLoose")),
54  nLoose_(iConfig.getParameter<unsigned>("nLoose")),
55  clearDaughters_(iConfig.getParameter<bool>("clearDaughters")),
56  dropSpecific_(iConfig.getParameter<bool>("dropSpecific")) {
57  produces<std::vector<reco::GenJet> >();
58  produces<edm::Association<std::vector<reco::GenJet> > >("slimmedGenJetAssociation");
59 }
60 
62  using namespace edm;
63  using namespace std;
64 
66  iEvent.getByToken(src_, src);
67 
68  auto out = std::make_unique<vector<reco::GenJet> >();
69  out->reserve(src->size());
70 
72  iEvent.getByToken(gp2pgp_, gp2pgp);
73 
74  auto genJetSlimmedGenJetAssociation = make_unique<edm::Association<std::vector<reco::GenJet> > >();
75 
76  auto mapping = std::make_unique<std::vector<int> >();
77  mapping->reserve(src->size());
78 
79  unsigned nm = 0; // number of mapped jets
80  unsigned nl = 0; // number of loose jets
81  for (View<reco::GenJet>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
82  bool selectedLoose = false;
83  if (nLoose_ > 0 && nl < nLoose_ && cutLoose_(*it)) {
84  selectedLoose = true;
85  ++nl;
86  }
87 
88  bool pass = cut_(*it) || selectedLoose;
89  if (!pass) {
90  mapping->push_back(-1);
91  continue;
92  }
93 
94  out->push_back(*it);
95  reco::GenJet& jet = out->back();
96 
97  mapping->push_back(nm++);
98 
99  if (clearDaughters_) {
100  jet.clearDaughters();
101  } else // rekey
102  {
103  //copy old
104  reco::CompositePtrCandidate::daughters old = jet.daughterPtrVector();
105  jet.clearDaughters();
106  std::map<unsigned int, reco::CandidatePtr> ptrs;
107  for (unsigned int i = 0; i < old.size(); i++) {
108  // if(! ((*gp2pgp)[old[i]]).isNonnull()) {
109  // 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;
110  // }
111  ptrs[((*gp2pgp)[old[i]]).key()] = refToPtr((*gp2pgp)[old[i]]);
112  }
113  for (std::map<unsigned int, reco::CandidatePtr>::iterator itp = ptrs.begin(); itp != ptrs.end();
114  itp++) //iterate on sorted items
115  {
116  jet.addDaughter(itp->second);
117  }
118  }
119  if (dropSpecific_) {
120  jet.setSpecific(reco::GenJet::Specific());
121  }
122  }
123 
125 
126  auto asso = std::make_unique<edm::Association<std::vector<reco::GenJet> > >(orphanHandle);
127  edm::Association<std::vector<reco::GenJet> >::Filler slimmedAssoFiller(*asso);
128  slimmedAssoFiller.insert(src, mapping->begin(), mapping->end());
129  slimmedAssoFiller.fill();
130 
131  iEvent.put(std::move(asso), "slimmedGenJetAssociation");
132 }
133 
135 using namespace pat;
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
const StringCutObjectSelector< reco::GenJet > cutLoose_
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:224
const bool clearDaughters_
reset daughters to an empty vector
Matcher of reconstructed objects to L1 Muons.
const StringCutObjectSelector< reco::GenJet > cut_
const edm::EDGetTokenT< edm::Association< std::vector< pat::PackedGenParticle > > > gp2pgp_
const bool dropSpecific_
drop the specific
Jets made from MC generator particles.
Definition: GenJet.h:23
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< CandidatePtr > daughters
collection of references to daughters
const edm::EDGetTokenT< edm::View< reco::GenJet > > src_
fixed size matrix
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
def move(src, dest)
Definition: eostools.py:511
PATGenJetSlimmer(const edm::ParameterSet &iConfig)