21 #include <unordered_map> 31 bool abortOnUnknownPDGCode);
33 int chargeTimesThree(
int)
const;
36 std::vector<int> chargeP_, chargeM_;
37 std::unordered_map<int, int> chargeMap_;
38 bool abortOnUnknownPDGCode_;
43 bool iAbortOnUnknownPDGCode):
45 abortOnUnknownPDGCode_(iAbortOnUnknownPDGCode) {
46 for(
auto const&
p: iTable ) {
49 int q3 =
id.threeCharge();
50 if ( apdgId < PDGCacheMax && pdgId > 0 ) {
51 chargeP_[ apdgId ] = q3;
52 chargeM_[ apdgId ] = -q3;
54 chargeP_[ apdgId ] = -q3;
55 chargeM_[ apdgId ] = q3;
57 chargeMap_.emplace( pdgId, q3);
58 chargeMap_.emplace( -pdgId, -q3);
63 int IDto3Charge::chargeTimesThree(
int id )
const {
65 return id > 0 ? chargeP_[
id ] : chargeM_[ -
id ];
66 auto f = chargeMap_.find(
id );
67 if (
f == chargeMap_.end() ) {
68 if ( abortOnUnknownPDGCode_ )
70 <<
"invalid PDG id: " <<
id;
93 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;
131 using namespace reco;
133 using namespace HepMC;
136 static const double mmToNs = 1.0/299792458
e-6;
139 abortOnUnknownPDGCode_( cfg.getUntrackedParameter<bool>(
"abortOnUnknownPDGCode",
true ) ),
140 saveBarCodes_( cfg.getUntrackedParameter<bool>(
"saveBarCodes",
false ) ),
141 doSubEvent_(cfg.getUntrackedParameter<bool>(
"doSubEvent",
false )),
142 useCF_(cfg.getUntrackedParameter<bool>(
"useCrossingFrame",
false ))
144 produces<GenParticleCollection>();
145 produces<math::XYZPointF>(
"xyz0");
146 produces<float>(
"t0");
149 produces<vector<int> >().setBranchAlias( alias +
"BarCodes" );
167 std::unordered_map<int, size_t> barcodes;
169 size_t totalSize = 0;
170 const GenEvent *
mc = 0;
178 npiles = cfhepmcprod->
size();
179 LogDebug(
"GenParticleProducer")<<
"npiles : "<<npiles<<endl;
180 for(
unsigned int icf = 0; icf < npiles; ++icf){
184 LogDebug(
"GenParticleProducer")<<
"totalSize : "<<totalSize<<endl;
191 <<
"HepMC has null pointer to GenEvent" << endl;
192 totalSize = mc->particles_size();
196 const size_t size = totalSize;
197 vector<const HepMC::GenParticle *>
particles( size );
198 auto candsPtr = std::make_unique<GenParticleCollection>(
size);
199 auto barCodeVector = std::make_unique<vector<int>>(
size);
200 std::unique_ptr<math::XYZPointF> xyz0Ptr(
new math::XYZPointF(0.,0.,0.));
201 std::unique_ptr<float> t0Ptr(
new float(0.
f));
205 size_t suboffset = 0;
207 IDto3Charge
const& id2Charge = *runCache(evt.
getRun().
index());
210 for(
size_t ipile = 0; ipile < npiles; ++ipile){
211 LogDebug(
"GenParticleProducer")<<
"mixed object ipile : "<<ipile<<endl;
217 const HepMC::HeavyIon *
hi = mc->heavy_ion();
218 if(hi && hi->Ncoll_hard() > 1) isHI =
true;
219 size_t num_particles = mc->particles_size();
220 LogDebug(
"GenParticleProducer")<<
"num_particles : "<<num_particles<<endl;
222 auto origin = (*mc->vertices_begin())->
position();
224 *t0Ptr = origin.t() *
mmToNs;
226 fillIndices(mc, particles, *barCodeVector, offset,barcodes);
228 for(
size_t ipar = offset; ipar < offset + num_particles; ++ ipar ) {
236 for(
size_t d = offset;
d < offset + num_particles; ++
d ) {
238 const GenVertex * productionVertex = part->production_vertex();
240 if ( productionVertex != 0 ) {
241 sub_id = productionVertex->id();
242 if(!isHI) sub_id = 0;
246 const GenVertex * endVertex = part->end_vertex();
247 if(endVertex != 0) sub_id = endVertex->id();
248 else throw cms::Exception(
"SubEventID" )<<
"SubEvent not determined. Particle has no production and no end vertex!"<<endl;
250 if(sub_id < 0) sub_id = 0;
251 int new_id = sub_id + suboffset;
253 cands[
d].setCollisionId(new_id);
254 LogDebug(
"VertexId")<<
"SubEvent offset 3 : "<<suboffset;
258 nsub = hi->Ncoll_hard()+1;
263 offset += num_particles;
266 auto origin = (*mc->vertices_begin())->
position();
268 *t0Ptr = origin.t() *
mmToNs;
269 fillIndices(mc, particles, *barCodeVector, 0,barcodes);
272 for(
size_t i = 0;
i < particles.size(); ++
i ) {
281 for(
size_t d = 0;
d < cands.size(); ++
d ) {
283 const GenVertex * productionVertex = part->production_vertex();
285 if ( productionVertex != 0 )
fillDaughters(cands,part,ref,
d,barcodes);
286 cands[
d].setCollisionId(0);
292 if(cfhepmcprod)
delete cfhepmcprod;
299 int pdgId = part->pdg_id();
305 const GenVertex *
v = part->production_vertex();
307 ThreeVector
vtx = v->point3d();
319 const GenVertex * productionVertex = part->production_vertex();
320 size_t numberOfMothers = productionVertex->particles_in_size();
321 if ( numberOfMothers > 0 ) {
322 GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
323 for( ; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
325 size_t m = barcodes.find( mother->barcode() )->
second;
336 HepMC::GenEvent::particle_const_iterator
begin = mc->particles_begin(),
end = mc->particles_end();
337 for( HepMC::GenEvent::particle_const_iterator
p = begin;
p !=
end; ++
p ) {
339 size_t barCode_this_event = particle->barcode();
340 size_t barCode = barCode_this_event +
offset;
341 if( barcodes.find(barCode) != barcodes.end() )
343 <<
"barcodes are duplicated! " << endl;
344 particles[
idx] = particle;
345 barCodeVector[
idx] = barCode;
346 barcodes.insert( make_pair(barCode_this_event, idx ++) );
GenParticleProducer(const edm::ParameterSet &)
constructor
virtual void produce(edm::StreamID, edm::Event &e, const edm::EventSetup &) const override
process one event
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
bool abortOnUnknownPDGCode_
unknown code treatment flag
MCTruthHelper< HepMC::GenParticle > mcTruthHelper_
virtual void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
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
#define DEFINE_FWK_MODULE(type)
virtual void setPdgId(int pdgId) final
std::vector< edm::EDGetTokenT< edm::HepMCProduct > > vectorSrcTokens_
~GenParticleProducer()
destructor
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 getData(T &iHolder) const
void fillGenStatusFlags(const P &p, reco::GenStatusFlags &statusFlags) const
U second(std::pair< T, U > const &p)
void resetDaughters(const edm::ProductID &id)
set daughters product ID
edm::Ref< edm::HepMCProduct, HepMC::GenParticle > GenParticleRef
Abs< T >::type abs(const T &t)
virtual void setVertex(const Point &vertex)
set vertex
RefProd< PROD > getRefBeforePut()
const GenStatusFlags & statusFlags() const
ProductID id() const
Accessor for product ID.
virtual void setStatus(int status) final
set status word
const T & getObject(unsigned int ip) const
const HepMC::GenEvent * GetEvent() const
T const * product() const
static const double mmToCm
virtual std::shared_ptr< IDto3Charge > globalBeginRun(const edm::Run &, const edm::EventSetup &) const override
virtual void setP4(const LorentzVector &p4) final
set 4-momentum
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
virtual void setThreeCharge(Charge qx3) final
set electric charge