ATLAS Offline Software
Loading...
Searching...
No Matches
McEventCollectionCnv_p3.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// McEventCollectionCnv_p3.cxx
8// Implementation file for class McEventCollectionCnv_p3
9// Author: S.Binet<binet@cern.ch>
11
12
13// STL includes
14#include <utility>
15#include <cmath>
16
17// GeneratorObjectsTPCnv includes
19#include "HepMcDataPool.h"
20
22// Constructors
24
25
29
31
32= default;
33
36{
37 if ( this != &rhs ) {
38 Base_t::operator=( rhs );
39 }
40 return *this;
41}
42
44// Destructor
46
48= default;
49
50
52 McEventCollection* transObj,
53 MsgStream& msg )
54{
55 msg << MSG::DEBUG << "Loading McEventCollection from persistent state..."
56 << endmsg;
57
58 // elements are managed by DataPool
59 transObj->clear(SG::VIEW_ELEMENTS);
60 HepMC::DataPool datapools;
61 const unsigned int nVertices = persObj->m_genVertices.size();
62 datapools.vtx.prepareToAdd(nVertices);
63 const unsigned int nParts = persObj->m_genParticles.size();
64 datapools.part.prepareToAdd(nParts);
65 const unsigned int nEvts = persObj->m_genEvents.size();
66 datapools.evt.prepareToAdd(nEvts);
67
68 transObj->reserve( nEvts );
69 for ( std::vector<GenEvent_p3>::const_iterator
70 itr = persObj->m_genEvents.begin(),
71 itrEnd = persObj->m_genEvents.end();
72 itr != itrEnd;
73 ++itr ) {
74 const GenEvent_p3& persEvt = *itr;
75
76 HepMC::GenEvent * genEvt = datapools.getGenEvent();
77#ifdef HEPMC3
78 genEvt->add_attribute (HepMCStr::barcodes, std::make_shared<HepMC::GenEventBarcodes>());
79 genEvt->add_attribute(HepMCStr::signal_process_id,std::make_shared<HepMC3::IntAttribute>(persEvt.m_signalProcessId));
80 genEvt->set_event_number(persEvt.m_eventNbr);
81 genEvt->add_attribute(HepMCStr::event_scale,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_eventScale));
82 genEvt->add_attribute(HepMCStr::alphaQCD,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQCD));
83 genEvt->add_attribute(HepMCStr::alphaQED,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQED));
84 genEvt->weights()= persEvt.m_weights;
85 genEvt->add_attribute(HepMCStr::random_states,std::make_shared<HepMC3::VectorLongIntAttribute>(persEvt.m_randomStates));
86 transObj->push_back( genEvt );
87
88 ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd- persEvt.m_particlesBegin)/2 );
89
90 // create the vertices
91 const unsigned int endVtx = persEvt.m_verticesEnd;
92 for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
93 createGenVertex( *persObj, persObj->m_genVertices[iVtx],partToEndVtx, datapools, genEvt );
94 }
95#else
96 genEvt->m_signal_process_id = persEvt.m_signalProcessId;
97 genEvt->m_event_number = persEvt.m_eventNbr;
98 genEvt->m_event_scale = persEvt.m_eventScale;
99 genEvt->m_alphaQCD = persEvt.m_alphaQCD;
100 genEvt->m_alphaQED = persEvt.m_alphaQED;
101 genEvt->m_signal_process_vertex = 0;
102 genEvt->m_weights = persEvt.m_weights;
103 genEvt->m_random_states = persEvt.m_randomStates;
104 genEvt->m_vertex_barcodes.clear();
105 genEvt->m_particle_barcodes.clear();
106 genEvt->m_pdf_info = 0; //> not available at that time...
107
108 transObj->push_back( genEvt );
109
110 // create a temporary map associating the barcode of an end-vtx to its
111 // particle.
112 // As not all particles are stable (d'oh!) we take 50% of the number of
113 // particles as an initial size of the hash-map (to prevent re-hash)
114 ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd-
115 persEvt.m_particlesBegin)/2 );
116
117 // create the vertices
118 const unsigned int endVtx = persEvt.m_verticesEnd;
119 for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
120 genEvt->add_vertex( createGenVertex( *persObj,
121 persObj->m_genVertices[iVtx],
122 partToEndVtx,
123 datapools ) );
124 } //> end loop over vertices
125#endif
126
127 // set the signal process vertex
128 const int sigProcVtx = persEvt.m_signalProcessVtx;
129 if ( sigProcVtx != 0 ) {
130 auto Vtx=HepMC::barcode_to_vertex(genEvt, sigProcVtx );
132 }
133
134 // connect particles to their end vertices
135 for (auto & p : partToEndVtx) {
136 auto decayVtx=HepMC::barcode_to_vertex(genEvt, p.second );
137 if ( decayVtx ) {
138 decayVtx->add_particle_in( p.first );
139 } else {
140 msg << MSG::ERROR
141 << "GenParticle points to null end vertex !!"
142 << endmsg;
143 }
144 }
145 } //> end loop over m_genEvents
146
147 msg << MSG::DEBUG << "Loaded McEventCollection from persistent state [OK]"
148 << endmsg;
149}
150
152 McEventCollection_p3* /*persObj*/,
153 MsgStream& msg )
154{
155 msg << MSG::ERROR
156 << "This transient-to-persistent converter method has been RETIRED !!"
157 << endmsg
158 << "You are not supposed to end-up here ! Go away !"
159 << endmsg;
160
161 throw std::runtime_error( "Retired McEventCollectionCnv_p3::transToPers() !!" );
162}
163
164
167 const GenVertex_p3& persVtx,
168 ParticlesMap_t& partToEndVtx,
169 HepMC::DataPool& datapools, HepMC::GenEvent* parent )
170{
171 HepMC::GenVertexPtr vtx = datapools.getGenVertex();
172 if (parent) parent->add_vertex(vtx);
173
174#ifdef HEPMC3
175 vtx->set_position( HepMC::FourVector(persVtx.m_x,persVtx.m_y,persVtx.m_z,persVtx.m_t) );
176 vtx->set_status(persVtx.m_id);
177 // cast from std::vector<float> to std::vector<double>
178 std::vector<double> weights( persVtx.m_weights.begin(), persVtx.m_weights.end() );
179 vtx->add_attribute(HepMCStr::weights,std::make_shared<HepMC3::VectorDoubleAttribute>(weights));
181 // handle the in-going (orphans) particles
182 //Is this needed for HEPMC3?
183 const unsigned int nPartsIn = persVtx.m_particlesIn.size();
184 for ( unsigned int i = 0; i != nPartsIn; ++i ) {
185 createGenParticle( persEvt.m_genParticles[persVtx.m_particlesIn[i]], partToEndVtx, datapools, vtx, false );
186 }
187 // now handle the out-going particles
188 const unsigned int nPartsOut = persVtx.m_particlesOut.size();
189 for ( unsigned int i = 0; i != nPartsOut; ++i ) {
190 createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]], partToEndVtx, datapools, vtx );
191 }
192#else
193 vtx->m_position.setX( persVtx.m_x );
194 vtx->m_position.setY( persVtx.m_y );
195 vtx->m_position.setZ( persVtx.m_z );
196 vtx->m_position.setT( persVtx.m_t );
197 vtx->m_particles_in.clear();
198 vtx->m_particles_out.clear();
199 vtx->m_id = persVtx.m_id;
200 vtx->m_weights.m_weights.reserve( persVtx.m_weights.size() );
201 vtx->m_weights.m_weights.assign ( persVtx.m_weights.begin(),
202 persVtx.m_weights.end() );
203 vtx->m_event = 0;
204 vtx->m_barcode = persVtx.m_barcode;
205
206 // handle the in-going (orphans) particles
207 const unsigned int nPartsIn = persVtx.m_particlesIn.size();
208 for ( unsigned int i = 0; i != nPartsIn; ++i ) {
210 partToEndVtx,
211 datapools );
212 }
213
214 // now handle the out-going particles
215 const unsigned int nPartsOut = persVtx.m_particlesOut.size();
216 for ( unsigned int i = 0; i != nPartsOut; ++i ) {
217 vtx->add_particle_out( createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]],
218 partToEndVtx,
219 datapools ) );
220 }
221#endif
222
223 return vtx;
224}
225
228 ParticlesMap_t& partToEndVtx,
229 HepMC::DataPool& datapools, const HepMC::GenVertexPtr& parent, bool add_to_output )
230{
232 if (parent) add_to_output?parent->add_particle_out(p):parent->add_particle_in(p);
233
234#ifdef HEPMC3
235 p->set_pdg_id(persPart.m_pdgId);
236 p->set_status(persPart.m_status);
237 // Note: do the E calculation in extended (long double) precision.
238 // That happens implicitly on x86 with optimization on; saying it
239 // explicitly ensures that we get the same results with and without
240 // optimization. (If this is a performance issue for platforms
241 // other than x86, one could change to double for those platforms.)
242 double temp_e=0.0;
243 if ( 0 == persPart.m_recoMethod ) {
244 temp_e = std::sqrt( (long double)(persPart.m_px)*persPart.m_px +
245 (long double)(persPart.m_py)*persPart.m_py +
246 (long double)(persPart.m_pz)*persPart.m_pz +
247 (long double)(persPart.m_m) *persPart.m_m );
248 } else {
249 const int signM2 = ( persPart.m_m >= 0. ? 1 : -1 );
250 const double persPart_ene =
251 std::sqrt( std::abs((long double)(persPart.m_px)*persPart.m_px +
252 (long double)(persPart.m_py)*persPart.m_py +
253 (long double)(persPart.m_pz)*persPart.m_pz +
254 signM2* (long double)(persPart.m_m)* persPart.m_m));
255 const int signEne = ( persPart.m_recoMethod == 1 ? 1 : -1 );
256 temp_e=signEne * persPart_ene;
257 }
258 p->set_momentum(HepMC::FourVector(persPart.m_px,persPart.m_py,persPart.m_pz,temp_e));
259 // setup flow
260 // fillin' the flow
261 std::vector<int> flows;
262 const unsigned int nFlow = persPart.m_flow.size();
263 for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
264 flows.push_back(persPart.m_flow[iFlow].second );
265 }
266 //We construct it here as vector w/o gaps.
267 p->add_attribute(HepMCStr::flows, std::make_shared<HepMC3::VectorIntAttribute>(flows));
269#else
270 p->m_pdg_id = persPart.m_pdgId;
271 p->m_status = persPart.m_status;
272 p->m_polarization.m_theta= static_cast<double>(persPart.m_thetaPolarization);
273 p->m_polarization.m_phi = static_cast<double>(persPart.m_phiPolarization );
274 p->m_production_vertex = 0;
275 p->m_end_vertex = 0;
276 p->m_barcode = persPart.m_barcode;
277
278 // Note: do the E calculation in extended (long double) precision.
279 // That happens implicitly on x86 with optimization on; saying it
280 // explicitly ensures that we get the same results with and without
281 // optimization. (If this is a performance issue for platforms
282 // other than x86, one could change to double for those platforms.)
283 if ( 0 == persPart.m_recoMethod ) {
284 p->m_momentum.setPx( persPart.m_px );
285 p->m_momentum.setPy( persPart.m_py );
286 p->m_momentum.setPz( persPart.m_pz );
287 double temp_e = std::sqrt( (long double)(persPart.m_px)*persPart.m_px +
288 (long double)(persPart.m_py)*persPart.m_py +
289 (long double)(persPart.m_pz)*persPart.m_pz +
290 (long double)(persPart.m_m) *persPart.m_m );
291 p->m_momentum.setE( temp_e );
292 } else {
293 const int signM2 = ( persPart.m_m >= 0. ? 1 : -1 );
294 const double persPart_ene =
295 std::sqrt( std::abs((long double)(persPart.m_px)*persPart.m_px +
296 (long double)(persPart.m_py)*persPart.m_py +
297 (long double)(persPart.m_pz)*persPart.m_pz +
298 signM2* (long double)(persPart.m_m)* persPart.m_m));
299 const int signEne = ( persPart.m_recoMethod == 1 ? 1 : -1 );
300 p->m_momentum.set( persPart.m_px,
301 persPart.m_py,
302 persPart.m_pz,
303 signEne * persPart_ene );
304 }
305
306 // setup flow
307 const unsigned int nFlow = persPart.m_flow.size();
308 p->m_flow.clear();
309 for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
310 p->m_flow.set_icode( persPart.m_flow[iFlow].first,
311 persPart.m_flow[iFlow].second );
312 }
313#endif
314
315 if ( persPart.m_endVtx != 0 ) {
316 partToEndVtx[p] = persPart.m_endVtx;
317 }
318
319 return p;
320}
#define endmsg
void prepareToAdd(unsigned int size)
Prepare to add cached elements.
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
void clear()
Erase all the elements in the collection.
std::vector< long int > m_randomStates
Container of random numbers for the generator states.
Definition GenEvent_p3.h:97
double m_eventScale
Energy scale.
Definition GenEvent_p3.h:74
int m_signalProcessVtx
Barcode of the GenVertex holding the signal process.
Definition GenEvent_p3.h:88
unsigned int m_verticesEnd
End position in the vector of vertices composing this event.
double m_alphaQED
value of the QED coupling.
Definition GenEvent_p3.h:82
unsigned int m_particlesEnd
End position in the vector of particles composing this event.
int m_signalProcessId
Id of the processus being generated.
Definition GenEvent_p3.h:66
std::vector< double > m_weights
Weights for this event.
Definition GenEvent_p3.h:93
unsigned int m_particlesBegin
Begin position in the vector of particles composing this event.
double m_alphaQCD
value of the QCD coupling.
Definition GenEvent_p3.h:78
int m_eventNbr
Event number.
Definition GenEvent_p3.h:70
unsigned int m_verticesBegin
Begin position in the vector of vertices composing this event.
float m_pz
z-component of the 4-momentum of this particle
int m_status
Status of this particle, as defined for HEPEVT.
float m_phiPolarization
phi polarization
short m_recoMethod
switch to know which method to chose to better recover the original HepLorentzVector.
int m_barcode
barcode of this particles (uniquely identifying this particle within a given GenEvent)
float m_m
m-component of the 4-momentum of this particle
float m_px
x-component of the 4-momentum of this particle
std::vector< std::pair< int, int > > m_flow
Flow for this particle.
float m_py
y-component of the 4-momentum of this particle
int m_endVtx
Barcode of the decay vertex of this particle.
int m_pdgId
identity of this particle, according to the Particle Data Group notation
float m_thetaPolarization
polarization
std::vector< float > m_weights
Weights for this vertex.
std::vector< int > m_particlesOut
collection of barcodes of out-going particles connected to this vertex
int m_barcode
barcode of this vertex (uniquely identifying a vertex within an event)
float m_z
z-coordinate of the vertex
std::vector< int > m_particlesIn
collection of barcodes of in-going particles connected to this vertex
float m_x
x-coordinate of the vertex
float m_y
y-coordinate of the vertex
int m_id
Id of this vertex.
float m_t
t-coordinate of the vertex
virtual void transToPers(const McEventCollection *transObj, McEventCollection_p3 *persObj, MsgStream &log)
Method creating the persistent representation McEventCollection_p3 from its transient representation ...
McEventCollectionCnv_p3 & operator=(const McEventCollectionCnv_p3 &rhs)
Assignement operator.
static HepMC::GenVertexPtr createGenVertex(const McEventCollection_p3 &persEvts, const GenVertex_p3 &vtx, ParticlesMap_t &bcToPart, HepMC::DataPool &datapools, HepMC::GenEvent *parent=nullptr)
Create a transient GenVertex from a persistent one (version 1) It returns the new GenVertex.
McEventCollectionCnv_p3()
Default constructor:
static HepMC::GenParticlePtr createGenParticle(const GenParticle_p3 &p, ParticlesMap_t &partToEndVtx, HepMC::DataPool &datapools, const HepMC::GenVertexPtr &parent=nullptr, bool add_to_output=true)
Create a transient GenParticle from a persistent one (vers.1) It returns the new GenParticle.
virtual void persToTrans(const McEventCollection_p3 *persObj, McEventCollection *transObj, MsgStream &log)
Method creating the transient representation of McEventCollection from its persistent representation ...
std::unordered_map< HepMC::GenParticlePtr, int > ParticlesMap_t
T_AthenaPoolTPCnvBase< McEventCollection, McEventCollection_p3 > Base_t
virtual ~McEventCollectionCnv_p3()
Destructor.
std::vector< GenParticle_p3 > m_genParticles
The vector of persistent representation of GenParticles.
std::vector< GenEvent_p3 > m_genEvents
The vector of persistent representation of GenEvents.
std::vector< GenVertex_p3 > m_genVertices
The vector of persistent representation of GenVertices.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
const std::string event_scale
const std::string flows
const std::string weights
const std::string signal_process_id
const std::string random_states
const std::string alphaQED
const std::string alphaQCD
const std::string barcodes
void set_signal_process_vertex(GenEvent *e, T v)
Definition GenEvent.h:669
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition GenEvent.h:646
HepMC::GenVertex * GenVertexPtr
Definition GenVertex.h:38
bool suggest_barcode(T &p, int i)
Definition GenEvent.h:689
GenParticle * GenParticlePtr
Definition GenParticle.h:37
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
A namespace for all vertexing packages and related stuff.
HepMC::GenParticlePtr getGenParticle()
GenPartPool_t part
an arena of HepMC::GenParticle for efficient object instantiation
HepMC::GenEvent * getGenEvent()
HepMC::GenVertexPtr getGenVertex()
GenVtxPool_t vtx
an arena of HepMC::GenVertex for efficient object instantiation
GenEvtPool_t evt
an arena of HepMC::GenEvent for efficient object instantiation
MsgStream & msg
Definition testRead.cxx:32