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");
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;
204 vector<const HepMC::GenParticle*>
particles(size);
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;
235 fillIndices(mc, particles, *barCodeVector, offset, barcodes);
237 for (
size_t ipar = offset; ipar < offset + num_particles; ++ipar) {
245 for (
size_t d = offset;
d < offset + num_particles; ++
d) {
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;
277 offset += num_particles;
280 auto origin = (*mc->vertices_begin())->
position();
282 *t0Ptr = origin.t() *
mmToNs;
283 fillIndices(mc, particles, *barCodeVector, 0, barcodes);
286 for (
size_t i = 0;
i < particles.size(); ++
i) {
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 {
318 int pdgId = part->pdg_id();
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;
369 particles[
idx] = particle;
370 barCodeVector[
idx] = barCode;
371 barcodes.insert(make_pair(barCode_this_event, idx++));
GenParticleProducer(const edm::ParameterSet &)
constructor
void produce(edm::StreamID, edm::Event &e, const edm::EventSetup &) const override
process one event
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
bool abortOnUnknownPDGCode_
unknown code treatment flag
MCTruthHelper< HepMC::GenParticle > mcTruthHelper_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
void setCollisionId(int s)
static const double mmToNs
bool fillDaughters(reco::GenParticleCollection &cand, const HepMC::GenParticle *part, reco::GenParticleRefProd const &ref, size_t index, std::unordered_map< int, size_t > &barcodes) const
MCTruthHelper< reco::GenParticle > mcTruthHelperGenParts_
HepPDT::ParticleDataTable ParticleDataTable
bool getByToken(EDGetToken token, Handle< PROD > &result) const
std::vector< edm::EDGetTokenT< edm::HepMCProduct > > vectorSrcTokens_
Run const & getRun() const
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > mixToken_
bool fillIndices(const HepMC::GenEvent *mc, std::vector< const HepMC::GenParticle * > &particles, std::vector< int > &barCodeVector, int offset, std::unordered_map< int, size_t > &barcodes) const
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
edm::EDGetTokenT< edm::HepMCProduct > srcToken_
source collection name
bool convertParticle(reco::GenParticle &cand, const HepMC::GenParticle *part, const IDto3Charge &id2Charge) const
void fillGenStatusFlags(const P &p, reco::GenStatusFlags &statusFlags) const
void setVertex(const Point &vertex) override
set vertex
bool getData(T &iHolder) const
U second(std::pair< T, U > const &p)
void resetDaughters(const edm::ProductID &id)
set daughters product ID
#define DEFINE_FWK_MODULE(type)
Abs< T >::type abs(const T &t)
RefProd< PROD > getRefBeforePut()
const GenStatusFlags & statusFlags() const
ProductID id() const
Accessor for product ID.
const T & getObject(unsigned int ip) const
const HepMC::GenEvent * GetEvent() const
T const * product() const
void setThreeCharge(Charge qx3) final
set electric charge
static const double mmToCm
edm::Ref< edm::HepMCProduct, HepMC::GenParticle > GenParticleRef
~GenParticleProducer() override
destructor
std::shared_ptr< IDto3Charge > globalBeginRun(const edm::Run &, const edm::EventSetup &) const override
math::XYZTLorentzVector LorentzVector
Lorentz vector.
bool doSubEvent_
input & output modes
static int position[264][3]
math::XYZPoint Point
point in the space
bool saveBarCodes_
save bar-codes
void setStatus(int status) final
set status word
void setPdgId(int pdgId) final
void setP4(const LorentzVector &p4) final
set 4-momentum