23 #include <unordered_map>
39 int chargeTimesThree(
int)
const;
42 std::vector<int> chargeP_, chargeM_;
43 std::unordered_map<int, int> chargeMap_;
44 bool abortOnUnknownPDGCode_;
49 for (
auto const&
p : iTable) {
50 const HepPDT::ParticleID&
id =
p.first;
51 int pdgId =
id.pid(), apdgId =
std::abs(pdgId);
52 int q3 =
id.threeCharge();
53 if (apdgId < PDGCacheMax && pdgId > 0) {
54 chargeP_[apdgId] = q3;
55 chargeM_[apdgId] = -q3;
57 chargeP_[apdgId] = -q3;
58 chargeM_[apdgId] = q3;
60 chargeMap_.emplace(pdgId, q3);
61 chargeMap_.emplace(-pdgId, -q3);
66 int IDto3Charge::chargeTimesThree(
int id)
const {
68 return id > 0 ? chargeP_[
id] : chargeM_[-
id];
69 auto f = chargeMap_.find(
id);
70 if (
f == chargeMap_.end()) {
71 if (abortOnUnknownPDGCode_)
74 return HepPDT::ParticleID(
id).threeCharge();
98 std::unordered_map<int, size_t>& barcodes)
const;
100 std::vector<const HepMC::GenParticle*>& particles,
101 std::vector<int>& barCodeVector,
103 std::unordered_map<int, size_t>& barcodes)
const;
142 using namespace reco;
144 using namespace HepMC;
147 static const double mmToNs = 1.0 / 299792458
e-6;
150 : abortOnUnknownPDGCode_(
false),
151 saveBarCodes_(cfg.getUntrackedParameter<bool>(
"saveBarCodes",
false)),
152 doSubEvent_(cfg.getUntrackedParameter<bool>(
"doSubEvent",
false)),
153 useCF_(cfg.getUntrackedParameter<bool>(
"useCrossingFrame",
false)) {
154 particleTableToken_ = esConsumes<HepPDT::ParticleDataTable, edm::DefaultRecord, edm::Transition::BeginRun>();
155 produces<GenParticleCollection>();
156 produces<math::XYZPointF>(
"xyz0");
157 produces<float>(
"t0");
160 produces<vector<int>>().setBranchAlias(alias +
"BarCodes");
178 std::unordered_map<int, size_t> barcodes;
180 size_t totalSize = 0;
189 npiles = cfhepmcprod->
size();
190 LogDebug(
"GenParticleProducer") <<
"npiles : " << npiles << endl;
191 for (
unsigned int icf = 0; icf < npiles; ++icf) {
196 LogDebug(
"GenParticleProducer") <<
"totalSize : " << totalSize << endl;
200 mc = mcp->GetEvent();
203 totalSize = mc->particles_size();
207 const size_t size = totalSize;
208 vector<const HepMC::GenParticle*> particles(size);
209 auto candsPtr = std::make_unique<GenParticleCollection>(
size);
210 auto barCodeVector = std::make_unique<vector<int>>(
size);
211 std::unique_ptr<math::XYZPointF> xyz0Ptr(
new math::XYZPointF(0., 0., 0.));
212 std::unique_ptr<float> t0Ptr(
new float(0.
f));
216 size_t suboffset = 0;
218 IDto3Charge
const& id2Charge = *runCache(evt.
getRun().
index());
221 for (
size_t ipile = 0; ipile < npiles; ++ipile) {
222 LogDebug(
"GenParticleProducer") <<
"mixed object ipile : " << ipile << endl;
229 const HepMC::HeavyIon* hi = mc->heavy_ion();
230 if (hi && hi->Ncoll_hard() > 1)
232 size_t num_particles = mc->particles_size();
233 LogDebug(
"GenParticleProducer") <<
"num_particles : " << num_particles << endl;
235 auto origin = (*mc->vertices_begin())->
position();
237 *t0Ptr = origin.t() *
mmToNs;
239 fillIndices(mc, particles, *barCodeVector, offset, barcodes);
241 for (
size_t ipar = offset; ipar < offset + num_particles; ++ipar) {
249 for (
size_t d = offset;
d < offset + num_particles; ++
d) {
251 const GenVertex* productionVertex = part->production_vertex();
253 if (productionVertex !=
nullptr) {
254 sub_id = productionVertex->id();
260 const GenVertex* endVertex = part->end_vertex();
261 if (endVertex !=
nullptr)
262 sub_id = endVertex->id();
265 <<
"SubEvent not determined. Particle has no production and no end vertex!" << endl;
269 int new_id = sub_id + suboffset;
271 cands[
d].setCollisionId(new_id);
272 LogDebug(
"VertexId") <<
"SubEvent offset 3 : " << suboffset;
276 nsub = hi->Ncoll_hard() + 1;
281 offset += num_particles;
284 auto origin = (*mc->vertices_begin())->
position();
286 *t0Ptr = origin.t() *
mmToNs;
287 fillIndices(mc, particles, *barCodeVector, 0, barcodes);
290 for (
size_t i = 0;
i < particles.size(); ++
i) {
299 for (
size_t d = 0;
d < cands.size(); ++
d) {
301 const GenVertex* productionVertex = part->production_vertex();
303 if (productionVertex !=
nullptr)
305 cands[
d].setCollisionId(0);
320 IDto3Charge
const& id2Charge)
const {
322 int pdgId = part->pdg_id();
328 const GenVertex*
v = part->production_vertex();
330 ThreeVector vtx = v->point3d();
344 std::unordered_map<int, size_t>& barcodes)
const {
345 const GenVertex* productionVertex = part->production_vertex();
346 size_t numberOfMothers = productionVertex->particles_in_size();
347 if (numberOfMothers > 0) {
348 GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
349 for (; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
351 size_t m = barcodes.find(mother->barcode())->
second;
361 vector<const HepMC::GenParticle*>& particles,
362 vector<int>& barCodeVector,
364 std::unordered_map<int, size_t>& barcodes)
const {
366 HepMC::GenEvent::particle_const_iterator
begin = mc->particles_begin(),
end = mc->particles_end();
367 for (HepMC::GenEvent::particle_const_iterator
p = begin;
p !=
end; ++
p) {
369 size_t barCode_this_event = particle->barcode();
370 size_t barCode = barCode_this_event +
offset;
371 if (barcodes.find(barCode) != barcodes.end())
372 throw cms::Exception(
"WrongReference") <<
"barcodes are duplicated! " << endl;
373 particles[idx] = particle;
374 barCodeVector[idx] = barCode;
375 barcodes.insert(make_pair(barCode_this_event, idx++));
GenParticleProducer(const edm::ParameterSet &)
constructor
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
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
uint16_t *__restrict__ id
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
#define DEFINE_FWK_MODULE(type)
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > particleTableToken_
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
U second(std::pair< T, U > const &p)
void resetDaughters(const edm::ProductID &id)
set daughters product ID
Abs< T >::type abs(const T &t)
RefProd< PROD > getRefBeforePut()
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
const GenStatusFlags & statusFlags() const
static constexpr int PDGCacheMax
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
T getParameter(std::string const &) const
~GenParticleProducer() override
destructor
math::XYZTLorentzVector LorentzVector
Lorentz vector.
void produce(edm::StreamID, edm::Event &e, const edm::EventSetup &) const override
process one event
bool doSubEvent_
input & output modes
std::shared_ptr< IDto3Charge > globalBeginRun(const edm::Run &, const edm::EventSetup &) const override
static int position[264][3]
ProductID id() const
Accessor for product ID.
math::XYZPoint Point
point in the space
bool saveBarCodes_
save bar-codes
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
void setStatus(int status) final
set status word
void setPdgId(int pdgId) final
void setP4(const LorentzVector &p4) final
set 4-momentum
tuple size
Write out results.