Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICGenJetProducer.cc
Go to the documentation of this file.
2 #include <memory>
3 #include <string>
4 #include <vector>
5 #include "FWCore/Framework/interface/Event.h"
6 #include "FWCore/Framework/interface/EventSetup.h"
7 #include "FWCore/Framework/interface/MakerMacros.h"
8 #include "FWCore/ParameterSet/interface/ParameterSet.h"
9 #include "FWCore/Utilities/interface/InputTag.h"
10 #include "DataFormats/Common/interface/Handle.h"
11 #include "DataFormats/Common/interface/View.h"
12 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
18 
19 ICGenJetProducer::ICGenJetProducer(const edm::ParameterSet& config)
20  : input_(config.getParameter<edm::InputTag>("input")),
21  branch_(config.getParameter<std::string>("branch")),
22  input_particles_(config.getParameter<edm::InputTag>("inputGenParticles")),
23  request_gen_particles_(config.getParameter<bool>("requestGenParticles")),
24  is_slimmed_(config.getParameter<bool>("isSlimmed")) {
25  consumes<edm::View<reco::GenJet>>(input_);
26  consumes<reco::GenParticleCollection>(input_particles_);
27  gen_jets_ = new std::vector<ic::GenJet>();
28  if (request_gen_particles_) {
29  produces<reco::GenParticleRefVector>("requestedGenParticles");
30  }
31 
32  PrintHeaderWithProduces(config, input_, branch_);
33  PrintOptional(1, request_gen_particles_, "requestGenParticles");
34 }
35 
36 ICGenJetProducer::~ICGenJetProducer() { delete gen_jets_; }
37 
38 void ICGenJetProducer::produce(edm::Event& event,
39  const edm::EventSetup& setup) {
40  edm::Handle<edm::View<reco::GenJet> > jets_handle;
41  event.getByLabel(input_, jets_handle);
42 
43  edm::Handle<reco::GenParticleCollection> parts_handle;
44  std::map<reco::GenParticle const*, std::size_t> part_map;
45  if (request_gen_particles_) {
46  event.getByLabel(input_particles_, parts_handle);
47  for (unsigned i = 0; i < parts_handle->size(); ++i) {
48  part_map[&parts_handle->at(i)] = i;
49  }
50  }
51 
52  std::auto_ptr<reco::GenParticleRefVector> part_requests(
53  new reco::GenParticleRefVector());
54 
55  gen_jets_->clear();
56  gen_jets_->resize(jets_handle->size());
57 
58  for (unsigned i = 0; i < jets_handle->size(); ++i) {
59  reco::GenJet const& src = jets_handle->at(i);
60  ic::GenJet& dest = gen_jets_->at(i);
61  dest.set_id(gen_jet_hasher_(&src));
62  dest.set_pt(src.pt());
63  dest.set_eta(src.eta());
64  dest.set_phi(src.phi());
65  dest.set_energy(src.energy());
66  dest.set_charge(src.charge());
67  dest.set_flavour(0);
68  if(is_slimmed_) dest.set_n_constituents(src.numberOfDaughters());
69  else dest.set_n_constituents(src.getGenConstituents().size());
70  if (request_gen_particles_) {
71  std::vector<std::size_t> constituents;
72  for (unsigned i = 0; i < src.getGenConstituents().size(); ++i) {
73  reco::GenParticle const* ptr = src.getGenConstituent(i);
74  std::map<reco::GenParticle const*, std::size_t>::const_iterator it =
75  part_map.find(ptr);
76  if (it != part_map.end()) {
77  constituents.push_back(particle_hasher_(ptr));
78  part_requests->push_back(
79  reco::GenParticleRef(parts_handle, it->second));
80  }
81  }
82  dest.set_constituents(constituents);
83  }
84  }
85  if (request_gen_particles_) event.put(part_requests, "requestedGenParticles");
86 }
87 
88 void ICGenJetProducer::beginJob() {
89  ic::StaticTree::tree_->Branch(branch_.c_str(), &gen_jets_);
90 }
91 
92 void ICGenJetProducer::endJob() {}
93 
94 // define this as a plug-in
void set_n_constituents(unsigned const &n_constituents)
The number of GenParticle constituents.
Definition: GenJet.hh:42
ICGenJetProducer(const edm::ParameterSet &)
Stores the basic properties of a generator-level jet, in particular can save the IDs of the GenPartic...
Definition: GenJet.hh:15
void set_phi(double const &phi)
Direct access to .
Definition: Candidate.hh:75
void set_id(std::size_t const &id)
Unique identifier.
Definition: Candidate.hh:66
void set_constituents(std::vector< std::size_t > const &constituents)
A vector of ic::GenParticle::id() values corresponding to the constituent particles.
Definition: GenJet.hh:47
void set_charge(int const &charge)
Electric charge.
Definition: Candidate.hh:81
static TTree * tree_
Definition: StaticTree.hh:13
void set_flavour(int const &flavour)
The true flavour of the jet.
Definition: GenJet.hh:39
void set_energy(double const &energy)
Direct access to the energy.
Definition: Candidate.hh:78
void PrintOptional(unsigned depth, bool value, std::string text)
DEFINE_FWK_MODULE(ICGenJetProducer)
Definition: Consumes.h:19
See documentation here.
void set_pt(double const &pt)
Direct access to the .
Definition: Candidate.hh:69
void set_eta(double const &eta)
Direct access to .
Definition: Candidate.hh:72
void PrintHeaderWithProduces(edm::ParameterSet const &config, edm::InputTag const &in, std::string branch)