21 #include <unordered_map>
37 int chargeTimesThree(
int)
const;
40 std::vector<int> chargeP_, chargeM_;
41 std::unordered_map<int, int> chargeMap_;
42 bool abortOnUnknownPDGCode_;
47 for (
auto const&
p : iTable) {
50 int q3 =
id.threeCharge();
51 if (apdgId < PDGCacheMax && pdgId > 0) {
52 chargeP_[apdgId] = q3;
53 chargeM_[apdgId] = -q3;
55 chargeP_[apdgId] = -q3;
56 chargeM_[apdgId] = q3;
58 chargeMap_.emplace(
pdgId, q3);
59 chargeMap_.emplace(-
pdgId, -q3);
64 int IDto3Charge::chargeTimesThree(
int id)
const {
66 return id > 0 ? chargeP_[
id] : chargeM_[-
id];
67 auto f = chargeMap_.find(
id);
68 if (
f == chargeMap_.end()) {
69 if (abortOnUnknownPDGCode_)
96 std::unordered_map<int, size_t>& barcodes)
const;
98 std::vector<const HepMC::GenParticle*>&
particles,
99 std::vector<int>& barCodeVector,
101 std::unordered_map<int, size_t>& barcodes)
const;
138 using namespace reco;
140 using namespace HepMC;
143 static const double mmToNs = 1.0 / 299792458
e-6;
146 : abortOnUnknownPDGCode_(
cfg.getUntrackedParameter<
bool>(
"abortOnUnknownPDGCode",
true)),
147 saveBarCodes_(
cfg.getUntrackedParameter<
bool>(
"saveBarCodes",
false)),
148 doSubEvent_(
cfg.getUntrackedParameter<
bool>(
"doSubEvent",
false)),
149 useCF_(
cfg.getUntrackedParameter<
bool>(
"useCrossingFrame",
false)) {
150 produces<GenParticleCollection>();
151 produces<math::XYZPointF>(
"xyz0");
152 produces<float>(
"t0");
155 produces<vector<int>>().setBranchAlias(
alias +
"BarCodes");
160 mayConsume<CrossingFrame<HepMCProduct>>(
InputTag(
cfg.getParameter<
std::string>(
"mix"),
"generatorSmeared"));
174 std::unordered_map<int, size_t> barcodes;
176 size_t totalSize = 0;
185 npiles = cfhepmcprod->
size();
186 LogDebug(
"GenParticleProducer") <<
"npiles : " << npiles << endl;
187 for (
unsigned int icf = 0; icf < npiles; ++icf) {
192 LogDebug(
"GenParticleProducer") <<
"totalSize : " << totalSize << endl;
199 totalSize =
mc->particles_size();
203 const size_t size = totalSize;
205 auto candsPtr = std::make_unique<GenParticleCollection>(
size);
206 auto barCodeVector = std::make_unique<vector<int>>(
size);
207 std::unique_ptr<math::XYZPointF> xyz0Ptr(
new math::XYZPointF(0., 0., 0.));
208 std::unique_ptr<float> t0Ptr(
new float(0.
f));
212 size_t suboffset = 0;
214 IDto3Charge
const& id2Charge = *runCache(evt.
getRun().
index());
217 for (
size_t ipile = 0; ipile < npiles; ++ipile) {
218 LogDebug(
"GenParticleProducer") <<
"mixed object ipile : " << ipile << endl;
225 const HepMC::HeavyIon*
hi =
mc->heavy_ion();
226 if (
hi &&
hi->Ncoll_hard() > 1)
228 size_t num_particles =
mc->particles_size();
229 LogDebug(
"GenParticleProducer") <<
"num_particles : " << num_particles << endl;
231 auto origin = (*
mc->vertices_begin())->
position();
233 *t0Ptr = origin.t() *
mmToNs;
237 for (
size_t ipar =
offset; ipar <
offset + num_particles; ++ipar) {
242 cand.resetDaughters(ref.
id());
247 const GenVertex* productionVertex =
part->production_vertex();
249 if (productionVertex !=
nullptr) {
250 sub_id = productionVertex->id();
256 const GenVertex* endVertex =
part->end_vertex();
257 if (endVertex !=
nullptr)
258 sub_id = endVertex->id();
261 <<
"SubEvent not determined. Particle has no production and no end vertex!" << endl;
265 int new_id = sub_id + suboffset;
267 cands[
d].setCollisionId(new_id);
268 LogDebug(
"VertexId") <<
"SubEvent offset 3 : " << suboffset;
272 nsub =
hi->Ncoll_hard() + 1;
280 auto origin = (*
mc->vertices_begin())->
position();
282 *t0Ptr = origin.t() *
mmToNs;
291 cand.resetDaughters(ref.
id());
295 for (
size_t d = 0;
d <
cands.size(); ++
d) {
297 const GenVertex* productionVertex =
part->production_vertex();
299 if (productionVertex !=
nullptr)
301 cands[
d].setCollisionId(0);
316 IDto3Charge
const& id2Charge)
const {
319 cand.setThreeCharge(id2Charge.chargeTimesThree(
pdgId));
323 cand.setCollisionId(0);
324 const GenVertex*
v =
part->production_vertex();
326 ThreeVector
vtx =
v->point3d();
340 std::unordered_map<int, size_t>& barcodes)
const {
341 const GenVertex* productionVertex =
part->production_vertex();
342 size_t numberOfMothers = productionVertex->particles_in_size();
343 if (numberOfMothers > 0) {
344 GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
345 for (; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
347 size_t m = barcodes.find(mother->barcode())->
second;
357 vector<const HepMC::GenParticle*>&
particles,
358 vector<int>& barCodeVector,
360 std::unordered_map<int, size_t>& barcodes)
const {
362 HepMC::GenEvent::particle_const_iterator
begin =
mc->particles_begin(),
end =
mc->particles_end();
363 for (HepMC::GenEvent::particle_const_iterator
p =
begin;
p !=
end; ++
p) {
365 size_t barCode_this_event = particle->barcode();
366 size_t barCode = barCode_this_event +
offset;
367 if (barcodes.find(barCode) != barcodes.end())
368 throw cms::Exception(
"WrongReference") <<
"barcodes are duplicated! " << endl;
370 barCodeVector[
idx] = barCode;
371 barcodes.insert(make_pair(barCode_this_event,
idx++));