Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICGenParticleProducer.cc
Go to the documentation of this file.
2 #include <string>
3 #include <vector>
4 #include <bitset>
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"
17 
18 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION == 7 && CMSSW_MINOR_VERSION >= 4)
19 #include "DataFormats/HepMCCandidate/interface/GenStatusFlags.h"
20 #endif
21 
22 ICGenParticleProducer::ICGenParticleProducer(const edm::ParameterSet& config)
23  : input_(config.getParameter<edm::InputTag>("input")),
24  branch_(config.getParameter<std::string>("branch")),
25  store_mothers_(config.getParameter<bool>("includeMothers")),
26  store_daughters_(config.getParameter<bool>("includeDaughters")),
27  store_statusFlags_(config.getParameter<bool>("includeStatusFlags")){
28  consumes<edm::View<reco::GenParticle>>(input_);
29  particles_ = new std::vector<ic::GenParticle>();
30 
31  PrintHeaderWithProduces(config, input_, branch_);
32  PrintOptional(1, store_mothers_, "includeMothers");
33  PrintOptional(1, store_daughters_, "includeDaughters");
34  PrintOptional(1, store_statusFlags_, "includeStatusFlags");
35 }
36 
38 
39 void ICGenParticleProducer::produce(edm::Event& event,
40  const edm::EventSetup& setup) {
41  edm::Handle<edm::View<reco::GenParticle> > parts_handle;
42  event.getByLabel(input_, parts_handle);
43 
44  particles_->clear();
45  particles_->resize(parts_handle->size(), ic::GenParticle());
46 
47  for (unsigned i = 0; i < parts_handle->size(); ++i) {
48  reco::GenParticle const& src = parts_handle->at(i);
49  // edm::RefToBase<reco::GenParticle> ref = parts_handle->refAt(i);
50  ic::GenParticle& dest = particles_->at(i);
51  dest.set_id(particle_hasher_(&src));
52  dest.set_pt(src.pt());
53  dest.set_eta(src.eta());
54  dest.set_phi(src.phi());
55  dest.set_energy(src.energy());
56  dest.set_charge(src.charge());
57  dest.set_index(static_cast<int>((parts_handle->refAt(i).key())));
58  dest.set_pdgid(src.pdgId());
59  dest.set_status(src.status());
60 #if CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION == 7 && CMSSW_MINOR_VERSION >= 4 )
61  if (store_statusFlags_){
62  reco::GenStatusFlags statusflags = src.statusFlags();
63  if(statusflags.flags_.size()!=15){
64  throw cms::Exception("OptionNotSupported")<<"size of status flags bitset is "<<statusflags.flags_.size()<<" which is different to what was expected\n";
65  }
66  else{
67  std::vector<bool> statusflags_;
68  for(unsigned iFlag=0;iFlag<statusflags.flags_.size();iFlag++){
69  statusflags_.push_back(statusflags.flags_[iFlag]);
70  }
71  dest.set_statusFlags(statusflags_);
72  }
73  }
74 #endif
75 
76  if (store_mothers_) {
77  std::vector<int> mothers(src.motherRefVector().size(), 0);
78  for (unsigned j = 0; j < src.motherRefVector().size(); ++j) {
79  mothers[j] = static_cast<int>(src.motherRefVector().at(j).key());
80  }
81  dest.set_mothers(mothers);
82  }
83  if (store_daughters_) {
84  std::vector<int> daughters(src.daughterRefVector().size(), 0);
85  for (unsigned j = 0; j < src.daughterRefVector().size(); ++j) {
86  daughters[j] = static_cast<int>(src.daughterRefVector().at(j).key());
87  }
88  dest.set_daughters(daughters);
89  }
90  }
91 }
92 
93 void ICGenParticleProducer::beginJob() {
94  ic::StaticTree::tree_->Branch(branch_.c_str(), &particles_);
95 #if !(CMSSW_MAJOR_VERSION > 7 || (CMSSW_MAJOR_VERSION == 7 && CMSSW_MINOR_VERSION >= 4))
96  if(store_statusFlags_){
97  throw cms::Exception("OptionNotSupported")<<"status flags not supported for CMSSW versions before 7_4_X\n";
98  }
99 #endif
100 
101 }
102 
103 void ICGenParticleProducer::endJob() {}
104 
105 // define this as a plug-in
void set_daughters(std::vector< int > const &daughters)
A vector of ic::GenParticle::index() values that identify the daughter particles. ...
Definition: GenParticle.hh:89
DEFINE_FWK_MODULE(ICGenParticleProducer)
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_charge(int const &charge)
Electric charge.
Definition: Candidate.hh:81
static TTree * tree_
Definition: StaticTree.hh:13
void set_pdgid(int const &pdgid)
PDG number to identify the particle type, see this link
Definition: GenParticle.hh:78
void set_energy(double const &energy)
Direct access to the energy.
Definition: Candidate.hh:78
See documentation here.
void set_statusFlags(std::vector< bool > const &statusFlags)
A genstatusflags object to give information about the production of the particle. ...
Definition: GenParticle.hh:94
void set_index(int const &index)
The index position of the particle in the original list.
Definition: GenParticle.hh:75
void set_status(int const &status)
The generator-dependent particle status.
Definition: GenParticle.hh:81
void PrintOptional(unsigned depth, bool value, std::string text)
Definition: Consumes.h:19
void set_pt(double const &pt)
Direct access to the .
Definition: Candidate.hh:69
ICGenParticleProducer(const edm::ParameterSet &)
void set_eta(double const &eta)
Direct access to .
Definition: Candidate.hh:72
void set_mothers(std::vector< int > const &mothers)
A vector of ic::GenParticle::index() values that identify the mother particles.
Definition: GenParticle.hh:84
void PrintHeaderWithProduces(edm::ParameterSet const &config, edm::InputTag const &in, std::string branch)
Stores the basic properties of generator-level particles as well as mother-daughter relations with ot...
Definition: GenParticle.hh:38