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) {
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_)
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");
165 mayConsume<CrossingFrame<HepMCProduct>>(
InputTag(
cfg.getParameter<
std::string>(
"mix"),
"generatorSmeared"));
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;
203 totalSize =
mc->particles_size();
207 const size_t size = totalSize;
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;
241 for (
size_t ipar =
offset; ipar <
offset + num_particles; ++ipar) {
246 cand.resetDaughters(ref.
id());
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;
284 auto origin = (*
mc->vertices_begin())->
position();
286 *t0Ptr = origin.t() *
mmToNs;
295 cand.resetDaughters(ref.
id());
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 {
323 cand.setThreeCharge(id2Charge.chargeTimesThree(
pdgId));
327 cand.setCollisionId(0);
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;
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.
static const double mmToNs
MCTruthHelper< reco::GenParticle > mcTruthHelperGenParts_
HepPDT::ParticleDataTable ParticleDataTable
#define DEFINE_FWK_MODULE(type)
const T & getObject(unsigned int ip) const
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > particleTableToken_
T const * product() const
std::vector< edm::EDGetTokenT< edm::HepMCProduct > > vectorSrcTokens_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > mixToken_
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
edm::EDGetTokenT< edm::HepMCProduct > srcToken_
source collection name
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
U second(std::pair< T, U > const &p)
Abs< T >::type abs(const T &t)
Run const & getRun() const
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
const HepMC::GenEvent * GetEvent() const
RefProd< PROD > getRefBeforePut()
ProductID id() const
Accessor for product ID.
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
static constexpr int PDGCacheMax
static const double mmToCm
edm::Ref< edm::HepMCProduct, HepMC::GenParticle > GenParticleRef
~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
bool fillDaughters(reco::GenParticleCollection &cand, const HepMC::GenParticle *part, reco::GenParticleRefProd const &ref, size_t index, std::unordered_map< int, size_t > &barcodes) const
static int position[264][3]
void fillGenStatusFlags(const P &p, reco::GenStatusFlags &statusFlags) const
math::XYZPoint Point
point in the space
bool saveBarCodes_
save bar-codes
bool convertParticle(reco::GenParticle &cand, const HepMC::GenParticle *part, const IDto3Charge &id2Charge) const