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 "HepMC3AccessStrings.h"
20#include "HepMcDataPool.h"
21
22using namespace GeneratorObjectsTPCnv;
24// Constructors
26
27
31
33
34= default;
35
38{
39 if ( this != &rhs ) {
40 Base_t::operator=( rhs );
41 }
42 return *this;
43}
44
46// Destructor
48
50= default;
51
52
54 McEventCollection* transObj,
55 MsgStream& msg )
56{
57 msg << MSG::DEBUG << "Loading McEventCollection from persistent state..."
58 << endmsg;
59
60 // elements are managed by DataPool
61 transObj->clear(SG::VIEW_ELEMENTS);
62 HepMC::DataPool datapools;
63 const unsigned int nVertices = persObj->m_genVertices.size();
64 datapools.vtx.prepareToAdd(nVertices);
65 const unsigned int nParts = persObj->m_genParticles.size();
66 datapools.part.prepareToAdd(nParts);
67 const unsigned int nEvts = persObj->m_genEvents.size();
68 datapools.evt.prepareToAdd(nEvts);
69
70 transObj->reserve( nEvts );
71 for ( std::vector<GenEvent_p3>::const_iterator
72 itr = persObj->m_genEvents.begin(),
73 itrEnd = persObj->m_genEvents.end();
74 itr != itrEnd;
75 ++itr ) {
76 const GenEvent_p3& persEvt = *itr;
77
78 HepMC::GenEvent * genEvt = datapools.getGenEvent();
79#ifdef HEPMC3
80
81 genEvt->add_attribute (barcodesStr, std::make_shared<HepMC::GenEventBarcodes>());
82 genEvt->add_attribute(signalProcessIdStr,std::make_shared<HepMC3::IntAttribute>(persEvt.m_signalProcessId));
83 genEvt->set_event_number(persEvt.m_eventNbr);
84 genEvt->add_attribute(eventScaleStr,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_eventScale));
85 genEvt->add_attribute(alphaQcdStr,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQCD));
86 genEvt->add_attribute(alphaQedStr,std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQED));
87 genEvt->weights()= persEvt.m_weights;
88 genEvt->add_attribute(randomStatesStr,std::make_shared<HepMC3::VectorLongIntAttribute>(persEvt.m_randomStates));
89 transObj->push_back( genEvt );
90
91 ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd- persEvt.m_particlesBegin)/2 );
92
93 // create the vertices
94 const unsigned int endVtx = persEvt.m_verticesEnd;
95 for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
96 createGenVertex( *persObj, persObj->m_genVertices[iVtx],partToEndVtx, datapools, genEvt );
97 }
98#else
99 genEvt->m_signal_process_id = persEvt.m_signalProcessId;
100 genEvt->m_event_number = persEvt.m_eventNbr;
101 genEvt->m_event_scale = persEvt.m_eventScale;
102 genEvt->m_alphaQCD = persEvt.m_alphaQCD;
103 genEvt->m_alphaQED = persEvt.m_alphaQED;
104 genEvt->m_signal_process_vertex = 0;
105 genEvt->m_weights = persEvt.m_weights;
106 genEvt->m_random_states = persEvt.m_randomStates;
107 genEvt->m_vertex_barcodes.clear();
108 genEvt->m_particle_barcodes.clear();
109 genEvt->m_pdf_info = 0; //> not available at that time...
110
111 transObj->push_back( genEvt );
112
113 // create a temporary map associating the barcode of an end-vtx to its
114 // particle.
115 // As not all particles are stable (d'oh!) we take 50% of the number of
116 // particles as an initial size of the hash-map (to prevent re-hash)
117 ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd-
118 persEvt.m_particlesBegin)/2 );
119
120 // create the vertices
121 const unsigned int endVtx = persEvt.m_verticesEnd;
122 for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
123 genEvt->add_vertex( createGenVertex( *persObj,
124 persObj->m_genVertices[iVtx],
125 partToEndVtx,
126 datapools ) );
127 } //> end loop over vertices
128#endif
129
130 // set the signal process vertex
131 const int sigProcVtx = persEvt.m_signalProcessVtx;
132 if ( sigProcVtx != 0 ) {
133 auto Vtx=HepMC::barcode_to_vertex(genEvt, sigProcVtx );
135 }
136
137 // connect particles to their end vertices
138 for (auto & p : partToEndVtx) {
139 auto decayVtx=HepMC::barcode_to_vertex(genEvt, p.second );
140 if ( decayVtx ) {
141 decayVtx->add_particle_in( p.first );
142 } else {
143 msg << MSG::ERROR
144 << "GenParticle points to null end vertex !!"
145 << endmsg;
146 }
147 }
148 } //> end loop over m_genEvents
149
150 msg << MSG::DEBUG << "Loaded McEventCollection from persistent state [OK]"
151 << endmsg;
152}
153
155 McEventCollection_p3* /*persObj*/,
156 MsgStream& msg )
157{
158 msg << MSG::ERROR
159 << "This transient-to-persistent converter method has been RETIRED !!"
160 << endmsg
161 << "You are not supposed to end-up here ! Go away !"
162 << endmsg;
163
164 throw std::runtime_error( "Retired McEventCollectionCnv_p3::transToPers() !!" );
165}
166
167
170 const GenVertex_p3& persVtx,
171 ParticlesMap_t& partToEndVtx,
172 HepMC::DataPool& datapools, HepMC::GenEvent* parent )
173{
174 HepMC::GenVertexPtr vtx = datapools.getGenVertex();
175 if (parent) parent->add_vertex(vtx);
176
177#ifdef HEPMC3
178 vtx->set_position( HepMC::FourVector(persVtx.m_x,persVtx.m_y,persVtx.m_z,persVtx.m_t) );
179 vtx->set_status(persVtx.m_id);
180 // cast from std::vector<float> to std::vector<double>
181 std::vector<double> weights( persVtx.m_weights.begin(), persVtx.m_weights.end() );
182 vtx->add_attribute("weights",std::make_shared<HepMC3::VectorDoubleAttribute>(weights));
184 // handle the in-going (orphans) particles
185 //Is this needed for HEPMC3?
186 const unsigned int nPartsIn = persVtx.m_particlesIn.size();
187 for ( unsigned int i = 0; i != nPartsIn; ++i ) {
188 createGenParticle( persEvt.m_genParticles[persVtx.m_particlesIn[i]], partToEndVtx, datapools, vtx, false );
189 }
190 // now handle the out-going particles
191 const unsigned int nPartsOut = persVtx.m_particlesOut.size();
192 for ( unsigned int i = 0; i != nPartsOut; ++i ) {
193 createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]], partToEndVtx, datapools, vtx );
194 }
195#else
196 vtx->m_position.setX( persVtx.m_x );
197 vtx->m_position.setY( persVtx.m_y );
198 vtx->m_position.setZ( persVtx.m_z );
199 vtx->m_position.setT( persVtx.m_t );
200 vtx->m_particles_in.clear();
201 vtx->m_particles_out.clear();
202 vtx->m_id = persVtx.m_id;
203 vtx->m_weights.m_weights.reserve( persVtx.m_weights.size() );
204 vtx->m_weights.m_weights.assign ( persVtx.m_weights.begin(),
205 persVtx.m_weights.end() );
206 vtx->m_event = 0;
207 vtx->m_barcode = persVtx.m_barcode;
208
209 // handle the in-going (orphans) particles
210 const unsigned int nPartsIn = persVtx.m_particlesIn.size();
211 for ( unsigned int i = 0; i != nPartsIn; ++i ) {
213 partToEndVtx,
214 datapools );
215 }
216
217 // now handle the out-going particles
218 const unsigned int nPartsOut = persVtx.m_particlesOut.size();
219 for ( unsigned int i = 0; i != nPartsOut; ++i ) {
220 vtx->add_particle_out( createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]],
221 partToEndVtx,
222 datapools ) );
223 }
224#endif
225
226 return vtx;
227}
228
231 ParticlesMap_t& partToEndVtx,
232 HepMC::DataPool& datapools, const HepMC::GenVertexPtr& parent, bool add_to_output )
233{
235 if (parent) add_to_output?parent->add_particle_out(p):parent->add_particle_in(p);
236
237#ifdef HEPMC3
238 p->set_pdg_id(persPart.m_pdgId);
239 p->set_status(persPart.m_status);
240 // Note: do the E calculation in extended (long double) precision.
241 // That happens implicitly on x86 with optimization on; saying it
242 // explicitly ensures that we get the same results with and without
243 // optimization. (If this is a performance issue for platforms
244 // other than x86, one could change to double for those platforms.)
245 double temp_e=0.0;
246 if ( 0 == persPart.m_recoMethod ) {
247 temp_e = std::sqrt( (long double)(persPart.m_px)*persPart.m_px +
248 (long double)(persPart.m_py)*persPart.m_py +
249 (long double)(persPart.m_pz)*persPart.m_pz +
250 (long double)(persPart.m_m) *persPart.m_m );
251 } else {
252 const int signM2 = ( persPart.m_m >= 0. ? 1 : -1 );
253 const double persPart_ene =
254 std::sqrt( std::abs((long double)(persPart.m_px)*persPart.m_px +
255 (long double)(persPart.m_py)*persPart.m_py +
256 (long double)(persPart.m_pz)*persPart.m_pz +
257 signM2* (long double)(persPart.m_m)* persPart.m_m));
258 const int signEne = ( persPart.m_recoMethod == 1 ? 1 : -1 );
259 temp_e=signEne * persPart_ene;
260 }
261 p->set_momentum(HepMC::FourVector(persPart.m_px,persPart.m_py,persPart.m_pz,temp_e));
262 // setup flow
263 // fillin' the flow
264 std::vector<int> flows;
265 const unsigned int nFlow = persPart.m_flow.size();
266 for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
267 flows.push_back(persPart.m_flow[iFlow].second );
268 }
269 //We construct it here as vector w/o gaps.
270 p->add_attribute("flows", std::make_shared<HepMC3::VectorIntAttribute>(flows));
272#else
273 p->m_pdg_id = persPart.m_pdgId;
274 p->m_status = persPart.m_status;
275 p->m_polarization.m_theta= static_cast<double>(persPart.m_thetaPolarization);
276 p->m_polarization.m_phi = static_cast<double>(persPart.m_phiPolarization );
277 p->m_production_vertex = 0;
278 p->m_end_vertex = 0;
279 p->m_barcode = persPart.m_barcode;
280
281 // Note: do the E calculation in extended (long double) precision.
282 // That happens implicitly on x86 with optimization on; saying it
283 // explicitly ensures that we get the same results with and without
284 // optimization. (If this is a performance issue for platforms
285 // other than x86, one could change to double for those platforms.)
286 if ( 0 == persPart.m_recoMethod ) {
287 p->m_momentum.setPx( persPart.m_px );
288 p->m_momentum.setPy( persPart.m_py );
289 p->m_momentum.setPz( persPart.m_pz );
290 double temp_e = std::sqrt( (long double)(persPart.m_px)*persPart.m_px +
291 (long double)(persPart.m_py)*persPart.m_py +
292 (long double)(persPart.m_pz)*persPart.m_pz +
293 (long double)(persPart.m_m) *persPart.m_m );
294 p->m_momentum.setE( temp_e );
295 } else {
296 const int signM2 = ( persPart.m_m >= 0. ? 1 : -1 );
297 const double persPart_ene =
298 std::sqrt( std::abs((long double)(persPart.m_px)*persPart.m_px +
299 (long double)(persPart.m_py)*persPart.m_py +
300 (long double)(persPart.m_pz)*persPart.m_pz +
301 signM2* (long double)(persPart.m_m)* persPart.m_m));
302 const int signEne = ( persPart.m_recoMethod == 1 ? 1 : -1 );
303 p->m_momentum.set( persPart.m_px,
304 persPart.m_py,
305 persPart.m_pz,
306 signEne * persPart_ene );
307 }
308
309 // setup flow
310 const unsigned int nFlow = persPart.m_flow.size();
311 p->m_flow.clear();
312 for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
313 p->m_flow.set_icode( persPart.m_flow[iFlow].first,
314 persPart.m_flow[iFlow].second );
315 }
316#endif
317
318 if ( persPart.m_endVtx != 0 ) {
319 partToEndVtx[p] = persPart.m_endVtx;
320 }
321
322 return p;
323}
#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...
void set_signal_process_vertex(GenEvent *e, T v)
Definition GenEvent.h:670
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition GenEvent.h:647
HepMC::GenVertex * GenVertexPtr
Definition GenVertex.h:59
bool suggest_barcode(T &p, int i)
Definition GenEvent.h:690
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