22 #include <unordered_map>
38 int chargeTimesThree(
int)
const;
41 std::vector<int> chargeP_, chargeM_;
42 std::unordered_map<int, int> chargeMap_;
43 bool abortOnUnknownPDGCode_;
48 for (
auto const&
p : iTable) {
51 int q3 =
id.threeCharge();
52 if (apdgId < PDGCacheMax && pdgId > 0) {
53 chargeP_[apdgId] = q3;
54 chargeM_[apdgId] = -q3;
56 chargeP_[apdgId] = -q3;
57 chargeM_[apdgId] = q3;
59 chargeMap_.emplace(
pdgId, q3);
60 chargeMap_.emplace(-
pdgId, -q3);
65 int IDto3Charge::chargeTimesThree(
int id)
const {
67 return id > 0 ? chargeP_[
id] : chargeM_[-
id];
68 auto f = chargeMap_.find(
id);
69 if (
f == chargeMap_.end()) {
70 if (abortOnUnknownPDGCode_)
97 std::unordered_map<int, size_t>& barcodes)
const;
99 std::vector<const HepMC::GenParticle*>&
particles,
100 std::vector<int>& barCodeVector,
102 std::unordered_map<int, size_t>& barcodes)
const;
141 using namespace reco;
143 using namespace HepMC;
146 static const double mmToNs = 1.0 / 299792458
e-6;
149 : abortOnUnknownPDGCode_(
cfg.getUntrackedParameter<
bool>(
"abortOnUnknownPDGCode",
true)),
150 saveBarCodes_(
cfg.getUntrackedParameter<
bool>(
"saveBarCodes",
false)),
151 doSubEvent_(
cfg.getUntrackedParameter<
bool>(
"doSubEvent",
false)),
152 useCF_(
cfg.getUntrackedParameter<
bool>(
"useCrossingFrame",
false)) {
153 particleTableToken_ = esConsumes<HepPDT::ParticleDataTable, edm::DefaultRecord, edm::Transition::BeginRun>();
154 produces<GenParticleCollection>();
155 produces<math::XYZPointF>(
"xyz0");
156 produces<float>(
"t0");
159 produces<vector<int>>().setBranchAlias(
alias +
"BarCodes");
164 mayConsume<CrossingFrame<HepMCProduct>>(
InputTag(
cfg.getParameter<
std::string>(
"mix"),
"generatorSmeared"));
177 std::unordered_map<int, size_t> barcodes;
179 size_t totalSize = 0;
188 npiles = cfhepmcprod->
size();
189 LogDebug(
"GenParticleProducer") <<
"npiles : " << npiles << endl;
190 for (
unsigned int icf = 0; icf < npiles; ++icf) {
195 LogDebug(
"GenParticleProducer") <<
"totalSize : " << totalSize << endl;
202 totalSize =
mc->particles_size();
206 const size_t size = totalSize;
208 auto candsPtr = std::make_unique<GenParticleCollection>(
size);
209 auto barCodeVector = std::make_unique<vector<int>>(
size);
210 std::unique_ptr<math::XYZPointF> xyz0Ptr(
new math::XYZPointF(0., 0., 0.));
211 std::unique_ptr<float> t0Ptr(
new float(0.
f));
215 size_t suboffset = 0;
217 IDto3Charge
const& id2Charge = *runCache(evt.
getRun().
index());
220 for (
size_t ipile = 0; ipile < npiles; ++ipile) {
221 LogDebug(
"GenParticleProducer") <<
"mixed object ipile : " << ipile << endl;
228 const HepMC::HeavyIon*
hi =
mc->heavy_ion();
229 if (
hi &&
hi->Ncoll_hard() > 1)
231 size_t num_particles =
mc->particles_size();
232 LogDebug(
"GenParticleProducer") <<
"num_particles : " << num_particles << endl;
234 auto origin = (*
mc->vertices_begin())->
position();
236 *t0Ptr = origin.t() *
mmToNs;
240 for (
size_t ipar =
offset; ipar <
offset + num_particles; ++ipar) {
245 cand.resetDaughters(ref.
id());
250 const GenVertex* productionVertex =
part->production_vertex();
252 if (productionVertex !=
nullptr) {
253 sub_id = productionVertex->id();
259 const GenVertex* endVertex =
part->end_vertex();
260 if (endVertex !=
nullptr)
261 sub_id = endVertex->id();
264 <<
"SubEvent not determined. Particle has no production and no end vertex!" << endl;
268 int new_id = sub_id + suboffset;
270 cands[
d].setCollisionId(new_id);
271 LogDebug(
"VertexId") <<
"SubEvent offset 3 : " << suboffset;
275 nsub =
hi->Ncoll_hard() + 1;
283 auto origin = (*
mc->vertices_begin())->
position();
285 *t0Ptr = origin.t() *
mmToNs;
294 cand.resetDaughters(ref.
id());
298 for (
size_t d = 0;
d <
cands.size(); ++
d) {
300 const GenVertex* productionVertex =
part->production_vertex();
302 if (productionVertex !=
nullptr)
304 cands[
d].setCollisionId(0);
319 IDto3Charge
const& id2Charge)
const {
322 cand.setThreeCharge(id2Charge.chargeTimesThree(
pdgId));
326 cand.setCollisionId(0);
327 const GenVertex*
v =
part->production_vertex();
329 ThreeVector
vtx =
v->point3d();
343 std::unordered_map<int, size_t>& barcodes)
const {
344 const GenVertex* productionVertex =
part->production_vertex();
345 size_t numberOfMothers = productionVertex->particles_in_size();
346 if (numberOfMothers > 0) {
347 GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
348 for (; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
350 size_t m = barcodes.find(mother->barcode())->
second;
360 vector<const HepMC::GenParticle*>&
particles,
361 vector<int>& barCodeVector,
363 std::unordered_map<int, size_t>& barcodes)
const {
365 HepMC::GenEvent::particle_const_iterator begin =
mc->particles_begin(),
end =
mc->particles_end();
366 for (HepMC::GenEvent::particle_const_iterator
p = begin;
p !=
end; ++
p) {
368 size_t barCode_this_event = particle->barcode();
369 size_t barCode = barCode_this_event +
offset;
370 if (barcodes.find(barCode) != barcodes.end())
371 throw cms::Exception(
"WrongReference") <<
"barcodes are duplicated! " << endl;
373 barCodeVector[
idx] = barCode;
374 barcodes.insert(make_pair(barCode_this_event,
idx++));