CMS 3D CMS Logo

GenPUProtonProducer.cc
Go to the documentation of this file.
1 /* \class GenPUProtonProducer
2  *
3  * Modification of GenParticleProducer.
4  * Saves final state protons from HepMC events in Crossing Frame, in the generator-particle format.
5  *
6  * Note: Use the option USER_CXXFLAGS=-DEDM_ML_DEBUG with SCRAM in order to enable debug messages.
7  *
8  * March 9, 2017 : Initial version.
9  * March 14, 2017 : Updated debug messages.
10  * July 27, 2017 : Removed extra loop initially inherited from GenParticleProducer.
11  * August 17, 2017 : Replaced std::auto_ptr with std::unique_ptr.
12  * September 6, 2017 : Updated module to edm::global::EDProducer with ConvertParticle as RunCache following GenParticleProducer.
13  *
14  */
15 
21 
22 #include <vector>
23 #include <string>
24 #include <unordered_map>
25 
26 namespace {
27 
28  class ConvertParticle {
29  public:
30  static constexpr int PDGCacheMax = 32768;
31  static constexpr double mmToCm = 0.1;
32 
33  ConvertParticle()
34  : abortOnUnknownPDGCode_(true), initialized_(false), chargeP_(PDGCacheMax, 0), chargeM_(PDGCacheMax, 0){};
35 
36  ConvertParticle(bool abortOnUnknownPDGCode)
37  : abortOnUnknownPDGCode_(abortOnUnknownPDGCode),
38  initialized_(false),
39  chargeP_(PDGCacheMax, 0),
40  chargeM_(PDGCacheMax, 0){};
41 
42  ~ConvertParticle(){};
43 
44  bool initialized() const { return initialized_; }
45 
46  void init(HepPDT::ParticleDataTable const& pdt) {
47  if (!initialized_) {
48  for (HepPDT::ParticleDataTable::const_iterator p = pdt.begin(); p != pdt.end(); ++p) {
49  HepPDT::ParticleID const& id = p->first;
50  int pdgId = id.pid(), apdgId = std::abs(pdgId);
51  int q3 = id.threeCharge();
52  if (apdgId < PDGCacheMax && pdgId > 0) {
53  chargeP_[apdgId] = q3;
54  chargeM_[apdgId] = -q3;
55  } else if (apdgId < PDGCacheMax) {
56  chargeP_[apdgId] = -q3;
57  chargeM_[apdgId] = q3;
58  } else {
59  chargeMap_.emplace(pdgId, q3);
60  chargeMap_.emplace(-pdgId, -q3);
61  }
62  }
63  initialized_ = true;
64  }
65  }
66 
67  bool operator()(reco::GenParticle& cand, HepMC::GenParticle const* part) const {
69  int pdgId = part->pdg_id();
70  cand.setThreeCharge(chargeTimesThree(pdgId));
71  cand.setPdgId(pdgId);
72  cand.setStatus(part->status());
73  cand.setP4(p4);
74  cand.setCollisionId(0);
75  HepMC::GenVertex const* v = part->production_vertex();
76  if (v != nullptr) {
77  HepMC::ThreeVector vtx = v->point3d();
79  cand.setVertex(vertex);
80  } else {
81  cand.setVertex(reco::Candidate::Point(0, 0, 0));
82  }
83  return true;
84  }
85 
86  private:
87  bool abortOnUnknownPDGCode_;
88  bool initialized_;
89  std::vector<int> chargeP_, chargeM_;
90  std::unordered_map<int, int> chargeMap_;
91 
92  int chargeTimesThree(int id) const {
93  if (std::abs(id) < PDGCacheMax)
94  return id > 0 ? chargeP_[id] : chargeM_[-id];
95 
96  auto f = chargeMap_.find(id);
97  if (f == chargeMap_.end()) {
98  if (abortOnUnknownPDGCode_)
99  throw edm::Exception(edm::errors::LogicError) << "invalid PDG id: " << id << std::endl;
100  else
101  return HepPDT::ParticleID(id).threeCharge();
102  }
103  return f->second;
104  }
105  };
106 
107  class SelectProton {
108  public:
109  bool operator()(HepMC::GenParticle const* part, double minPz) const {
110  bool selection = ((!part->end_vertex() && part->status() == 1) && (part->pdg_id() == 2212) &&
111  (TMath::Abs(part->momentum().pz()) >= minPz));
112  return selection;
113  }
114  };
115 
116 } // Anonymous namespace
117 
123 
129 
130 namespace edm {
131  class ParameterSet;
132 }
133 
134 class GenPUProtonProducer : public edm::global::EDProducer<edm::RunCache<ConvertParticle> > {
135 public:
137  ~GenPUProtonProducer() override;
138 
139  void produce(edm::StreamID, edm::Event& e, const edm::EventSetup&) const override;
140  std::shared_ptr<ConvertParticle> globalBeginRun(const edm::Run&, const edm::EventSetup&) const override;
141  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override{};
142 
143 private:
147  std::vector<int> bunchList_;
148  double minPz_;
149  SelectProton select_;
150 };
151 
159 
162 
163 #include <algorithm>
164 
165 using namespace edm;
166 using namespace reco;
167 using namespace std;
168 using namespace HepMC;
169 
171  : abortOnUnknownPDGCode_(cfg.getUntrackedParameter<bool>("abortOnUnknownPDGCode", true)),
172  bunchList_(cfg.getParameter<vector<int> >("bunchCrossingList")),
173  minPz_(cfg.getParameter<double>("minPz")) {
174  produces<GenParticleCollection>();
175  mixToken_ =
176  consumes<CrossingFrame<HepMCProduct> >(InputTag(cfg.getParameter<std::string>("mix"), "generatorSmeared"));
177  pdtToken_ = esConsumes<HepPDT::ParticleDataTable, edm::DefaultRecord, edm::Transition::BeginRun>();
178 }
179 
181 
182 std::shared_ptr<ConvertParticle> GenPUProtonProducer::globalBeginRun(const Run&, const EventSetup& es) const {
184  auto convert_ptr = std::make_shared<ConvertParticle>(abortOnUnknownPDGCode_);
185  if (!convert_ptr->initialized())
186  convert_ptr->init(*pdt);
187 
188  return convert_ptr;
189 }
190 
192  size_t totalSize = 0;
193  size_t npiles = 1;
194 
196  evt.getByToken(mixToken_, cf);
197  std::unique_ptr<MixCollection<HepMCProduct> > cfhepmcprod(new MixCollection<HepMCProduct>(cf.product()));
198  npiles = cfhepmcprod->size();
199 
200  LogDebug("GenPUProtonProducer") << " Number of pile-up events : " << npiles << endl;
201 
202  for (size_t icf = 0; icf < npiles; ++icf) {
203  LogDebug("GenPUProtonProducer") << "CF " << icf
204  << " size : " << cfhepmcprod->getObject(icf).GetEvent()->particles_size() << endl;
205  totalSize += cfhepmcprod->getObject(icf).GetEvent()->particles_size();
206  }
207  LogDebug("GenPUProtonProducer") << "Total size : " << totalSize << endl;
208 
209  // Initialise containers
210  auto candsPtr = std::make_unique<GenParticleCollection>();
211  GenParticleCollection& cands = *candsPtr;
212 
213  // Loop over pile-up events
214  ConvertParticle const& convertParticle_ = *runCache(evt.getRun().index());
215 
217  unsigned int total_number_of_protons = 0;
218  size_t idx_mix = 0;
219  // Fill collection
220  for (mixHepMC_itr = cfhepmcprod->begin(); mixHepMC_itr != cfhepmcprod->end(); ++mixHepMC_itr, ++idx_mix) {
221  int bunch = mixHepMC_itr.bunch();
222 
223  if (find(bunchList_.begin(), bunchList_.end(), bunch) != bunchList_.end()) {
224  auto event = (*mixHepMC_itr).GetEvent();
225 
226  size_t num_particles = event->particles_size();
227 
228  // Fill output collection
229  unsigned int number_of_protons = 0;
230  for (auto p = event->particles_begin(); p != event->particles_end(); ++p) {
231  HepMC::GenParticle const* part = *p;
232  if (select_(part, minPz_)) {
234  convertParticle_(cand, part);
235  ++number_of_protons;
236  cands.push_back(cand);
237  }
238  }
239  LogDebug("GenPUProtonProducer") << "Idx : " << idx_mix << " Bunch : " << bunch
240  << " Number of particles : " << num_particles
241  << " Number of protons : " << number_of_protons << endl;
242 
243  total_number_of_protons += number_of_protons;
244  }
245  }
246  LogDebug("GenPUProtonProducer") << "Total number of protons : " << total_number_of_protons << endl;
247  LogDebug("GenPUProtonProducer") << "Output collection size : " << cands.size() << endl;
248 
249  evt.put(std::move(candsPtr));
250 }
251 
253 
edm::StreamID
Definition: StreamID.h:30
Handle.h
init
int init
Definition: HydjetWrapper.h:64
electrons_cff.bool
bool
Definition: electrons_cff.py:366
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
edm::Handle::product
T const * product() const
Definition: Handle.h:70
ESHandle.h
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
GenPUProtonProducer::select_
SelectProton select_
Definition: GenPUProtonProducer.cc:149
edm::errors::LogicError
Definition: EDMException.h:37
reco::GenParticle
Definition: GenParticle.h:21
edm::Run
Definition: Run.h:45
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm::Run::index
RunIndex index() const
Definition: Run.cc:26
edm
HLT enums.
Definition: AlignableModifier.h:19
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CrossingFrame.h
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
reco::GenParticleCollection
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
Definition: GenParticleFwd.h:13
GenPUProtonProducer::globalEndRun
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
Definition: GenPUProtonProducer.cc:141
GenPUProtonProducer::abortOnUnknownPDGCode_
bool abortOnUnknownPDGCode_
Definition: GenPUProtonProducer.cc:146
GenPUProtonProducer::mixToken_
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > mixToken_
Definition: GenPUProtonProducer.cc:141
GenPUProtonProducer::produce
void produce(edm::StreamID, edm::Event &e, const edm::EventSetup &) const override
Definition: GenPUProtonProducer.cc:191
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
GenPUProtonProducer
Definition: GenPUProtonProducer.cc:134
findQualityFiles.v
v
Definition: findQualityFiles.py:179
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::Handle
Definition: AssociativeIterator.h:50
genPUProtons_cfi.minPz
minPz
Definition: genPUProtons_cfi.py:6
MixCollection::MixItr
Definition: MixCollection.h:61
ESGetToken.h
GenParticle.h
CandidateFwd.h
EDMException.h
MakerMacros.h
MixCollection::MixItr::bunch
int bunch() const
Definition: MixCollection.h:90
part
part
Definition: HCALResponse.h:20
GeneratorMix_cff.abortOnUnknownPDGCode
abortOnUnknownPDGCode
Definition: GeneratorMix_cff.py:10
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Abs
T Abs(T a)
Definition: MathUtil.h:49
MixCollection.h
MixCollection
Definition: MixCollection.h:10
GenPUProtonProducer::pdtToken_
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > pdtToken_
Definition: GenPUProtonProducer.cc:145
GenPUProtonProducer::~GenPUProtonProducer
~GenPUProtonProducer() override
Definition: GenPUProtonProducer.cc:180
GenParticleFwd.h
corrVsCorr.selection
selection
main part
Definition: corrVsCorr.py:100
Run.h
edm::ESHandle< HepPDT::ParticleDataTable >
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:535
edm::global::EDProducer
Definition: EDProducer.h:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
funct::true
true
Definition: Factorize.h:173
HLT_FULL_cff.cands
cands
Definition: HLT_FULL_cff.py:15144
bphysicsOniaDQM_cfi.vertex
vertex
Definition: bphysicsOniaDQM_cfi.py:7
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
ParticleDataTable.h
cand
Definition: decayParser.h:32
createfilelist.int
int
Definition: createfilelist.py:10
p4
double p4[4]
Definition: TauolaWrapper.h:92
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::EventSetup::getHandle
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:155
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
GenPUProtonProducer::GenPUProtonProducer
GenPUProtonProducer(const edm::ParameterSet &)
Definition: GenPUProtonProducer.cc:170
EgammaValidation_cff.pdgId
pdgId
Definition: EgammaValidation_cff.py:118
edm::EventSetup
Definition: EventSetup.h:58
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord >
InputTag.h
PDGCacheMax
static constexpr int PDGCacheMax
Definition: GenParticleProducer.cc:33
looper.cfg
cfg
Definition: looper.py:297
GenPUProtonProducer::bunchList_
std::vector< int > bunchList_
Definition: GenPUProtonProducer.cc:147
GenParticle.GenParticle
GenParticle
Definition: GenParticle.py:18
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
mmToCm
static const double mmToCm
Definition: GenParticleProducer.cc:146
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
HepMC
Definition: GenParticle.h:15
Exception
Definition: hltDiff.cc:245
EventSetup.h
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
HepMCProduct.h
EDProducer.h
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
reco::Candidate::Point
math::XYZPoint Point
point in the space
Definition: Candidate.h:40
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
LHEGenericFilter_cfi.ParticleID
ParticleID
Definition: LHEGenericFilter_cfi.py:6
ParticleDataTable
HepPDT::ParticleDataTable ParticleDataTable
Definition: ParticleDataTable.h:8
GenPUProtonProducer::globalBeginRun
std::shared_ptr< ConvertParticle > globalBeginRun(const edm::Run &, const edm::EventSetup &) const override
Definition: GenPUProtonProducer.cc:182
edm::Event::getRun
Run const & getRun() const
Definition: Event.cc:112
GenPUProtonProducer::minPz_
double minPz_
Definition: GenPUProtonProducer.cc:148
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37