ATLAS Offline Software
McEventCollectionCnv_p2.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // McEventCollectionCnv_p2.cxx
8 // Implementation file for class McEventCollectionCnv_p2
9 // Author: S.Binet<binet@cern.ch>
11 
12 
13 // STL includes
14 #include <utility>
15 
16 // GeneratorObjectsTPCnv includes
18 #include "HepMcDataPool.h"
19 
20 
22 // Constructors
24 
26  Base_t( )
27 {}
28 
30 
31 = default;
32 
35 {
36  if ( this != &rhs ) {
37  Base_t::operator=( rhs );
38  }
39  return *this;
40 }
41 
43 // Destructor
45 
47 = default;
48 
49 
51  McEventCollection* transObj,
52  MsgStream& msg )
53 {
54  msg << MSG::DEBUG << "Loading McEventCollection from persistent state..."
55  << endmsg;
56 
57  // elements are managed by DataPool
58  transObj->clear(SG::VIEW_ELEMENTS);
59  HepMC::DataPool datapools;
60  const unsigned int nVertices = persObj->m_genVertices.size();
61  if ( datapools.vtx.capacity() - datapools.vtx.allocated() < nVertices ) {
62  datapools.vtx.reserve( datapools.vtx.allocated() + nVertices );
63  }
64  const unsigned int nParts = persObj->m_genParticles.size();
65  if ( datapools.part.capacity() - datapools.part.allocated() < nParts ) {
66  datapools.part.reserve( datapools.part.allocated() + nParts );
67  }
68  const unsigned int nEvts = persObj->m_genEvents.size();
69  if ( datapools.evt.capacity() - datapools.evt.allocated() < nEvts ) {
70  datapools.evt.reserve( datapools.evt.allocated() + nEvts );
71  }
72 
73  transObj->reserve( nEvts );
74  const std::vector<GenEvent_p2>::const_iterator itrEnd = persObj->m_genEvents.end();
75  for ( std::vector<GenEvent_p2>::const_iterator itr = persObj->m_genEvents.begin();
76  itr != itrEnd;
77  ++itr ) {
78  const GenEvent_p2& persEvt = *itr;
79  HepMC::GenEvent * genEvt = datapools.getGenEvent();
80 #ifdef HEPMC3
81  genEvt->add_attribute ("barcodes", std::make_shared<HepMC::GenEventBarcodes>());
82  genEvt->add_attribute("signal_process_id",std::make_shared<HepMC3::IntAttribute>(persEvt.m_signalProcessId));
83  genEvt->set_event_number(persEvt.m_eventNbr);
84  genEvt->add_attribute("event_scale",std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_eventScale));
85  genEvt->add_attribute("alphaQCD",std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQCD));
86  genEvt->add_attribute("alphaQED",std::make_shared<HepMC3::DoubleAttribute>(persEvt.m_alphaQED));
87  genEvt->weights()= persEvt.m_weights;
88  genEvt->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(persEvt.m_randomStates));
89 
90  transObj->push_back( genEvt );
91 
92  ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd-persEvt.m_particlesBegin)/2 );
93 
94  // create the vertices
95  const unsigned int endVtx = persEvt.m_verticesEnd;
96  for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
97  genEvt->add_vertex( createGenVertex( *persObj,
98  persObj->m_genVertices[iVtx],
99  partToEndVtx,
100  datapools ) );
101  } //> end loop over vertices
102 
103  // set the signal process vertex
104  const int sigProcVtx = persEvt.m_signalProcessVtx;
105  if ( sigProcVtx ) {
106  auto Vtx=HepMC::barcode_to_vertex(genEvt, sigProcVtx );
108  }
109  // connect particles to their end vertices
110  for ( const auto& p: partToEndVtx) {
111  auto decayVtx = HepMC::barcode_to_vertex(genEvt, p.second );
112  if ( decayVtx ) {
113  decayVtx->add_particle_in( p.first );
114  } else {
115  msg << MSG::ERROR<< "GenParticle points to null end vertex !!"<< endmsg;
116  }
117  }
118 #else
119  genEvt->m_signal_process_id = persEvt.m_signalProcessId;
120  genEvt->m_event_number = persEvt.m_eventNbr;
121  genEvt->m_event_scale = persEvt.m_eventScale;
122  genEvt->m_alphaQCD = persEvt.m_alphaQCD;
123  genEvt->m_alphaQED = persEvt.m_alphaQED;
124  genEvt->m_signal_process_vertex = 0;
125  genEvt->m_weights = persEvt.m_weights;
126  genEvt->m_random_states = persEvt.m_randomStates;
127  genEvt->m_vertex_barcodes.clear();
128  genEvt->m_particle_barcodes.clear();
129  genEvt->m_pdf_info = 0; //> not available at that time...
130 
131  transObj->push_back( genEvt );
132 
133  // create a temporary map associating the barcode of an end-vtx to its
134  // particle.
135  // As not all particles are stable (d'oh!) we take 50% of the number of
136  // particles as an initial size of the hash-map (to prevent re-hash)
137  ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd-
138  persEvt.m_particlesBegin)/2 );
139 
140  // create the vertices
141  const unsigned int endVtx = persEvt.m_verticesEnd;
142  for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
143  genEvt->add_vertex( createGenVertex( *persObj,
144  persObj->m_genVertices[iVtx],
145  partToEndVtx,
146  datapools ) );
147  } //> end loop over vertices
148 
149  // set the signal process vertex
150  const int sigProcVtx = persEvt.m_signalProcessVtx;
151  if ( sigProcVtx != 0 ) {
152  genEvt->set_signal_process_vertex( genEvt->barcode_to_vertex( sigProcVtx ) );
153  }
154 
155  // connect particles to their end vertices
156  const ParticlesMap_t::iterator endItr = partToEndVtx.end();
157  for ( ParticlesMap_t::iterator p = partToEndVtx.begin();
158  p != endItr;
159  ++p ) {
160  auto decayVtx = genEvt->barcode_to_vertex( p->second );
161  if ( decayVtx ) {
162  decayVtx->add_particle_in( p->first );
163  } else {
164  msg << MSG::ERROR
165  << "GenParticle points to null end vertex !!"
166  << endmsg;
167  }
168  }
169 #endif
170  } //> end loop over m_genEvents
171 
172  msg << MSG::DEBUG << "Loaded McEventCollection from persistent state [OK]"
173  << endmsg;
174 }
175 
178  MsgStream& msg )
179 {
180  msg << MSG::DEBUG << "Creating persistent state of McEventCollection..."
181  << endmsg;
182 
183  msg << MSG::ERROR
184  << "This transient-to-persistent converter method has been RETIRED !!"
185  << endmsg
186  << "You are not supposed to end-up here ! Go away !"
187  << endmsg;
188 
189  throw std::runtime_error( "Retired McEventCollectionCnv_p2::transToPers() !!" );
190 }
191 
192 
195  const GenVertex_p2& persVtx,
196  ParticlesMap_t& partToEndVtx, HepMC::DataPool& datapools, HepMC::GenEvent* parent )
197 {
198  HepMC::GenVertexPtr vtx = datapools.getGenVertex();
199  if (parent) parent->add_vertex(vtx);
200 #ifdef HEPMC3
201  vtx->set_position( HepMC::FourVector(persVtx.m_x,persVtx.m_y, persVtx.m_z, persVtx.m_t) );
202  vtx->set_status(persVtx.m_id);
203  // cast from std::vector<float> to std::vector<double>
204  std::vector<double> weights( persVtx.m_weights.begin(), persVtx.m_weights.end() );
205  vtx->add_attribute("weights",std::make_shared<HepMC3::VectorDoubleAttribute>(weights));
206  HepMC::suggest_barcode(vtx,persVtx.m_barcode);
207 
208  // handle the in-going (orphans) particles
209  const unsigned int nPartsIn = persVtx.m_particlesIn.size();
210  for ( unsigned int i = 0; i != nPartsIn; ++i ) {
211  createGenParticle( persEvt.m_genParticles[persVtx.m_particlesIn[i]], partToEndVtx, 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  createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]], partToEndVtx, datapools, vtx );
218  }
219 #else
220  vtx->m_position.setX( persVtx.m_x );
221  vtx->m_position.setY( persVtx.m_y );
222  vtx->m_position.setZ( persVtx.m_z );
223  vtx->m_position.setT( persVtx.m_t );
224  vtx->m_particles_in.clear();
225  vtx->m_particles_out.clear();
226  vtx->m_id = persVtx.m_id;
227  vtx->m_weights.m_weights.reserve( persVtx.m_weights.size() );
228  vtx->m_weights.m_weights.assign ( persVtx.m_weights.begin(),
229  persVtx.m_weights.end() );
230  vtx->m_event = 0;
231  vtx->m_barcode = persVtx.m_barcode;
232 
233  // handle the in-going (orphans) particles
234  const unsigned int nPartsIn = persVtx.m_particlesIn.size();
235  for ( unsigned int i = 0; i != nPartsIn; ++i ) {
236  createGenParticle( persEvt.m_genParticles[persVtx.m_particlesIn[i]],
237  partToEndVtx,
238  datapools );
239  }
240 
241  // now handle the out-going particles
242  const unsigned int nPartsOut = persVtx.m_particlesOut.size();
243  for ( unsigned int i = 0; i != nPartsOut; ++i ) {
244  vtx->add_particle_out( createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]],
245  partToEndVtx,
246  datapools ) );
247  }
248 #endif
249 
250  return vtx;
251 }
252 
255  ParticlesMap_t& partToEndVtx, HepMC::DataPool& datapools, const HepMC::GenVertexPtr& parent )
256 {
257  HepMC::GenParticlePtr p = datapools.getGenParticle();
258 
259  if (parent) parent->add_particle_out(p);
260 #ifdef HEPMC3
261  p->set_momentum( HepMC::FourVector(persPart.m_px,persPart.m_py,persPart.m_pz, persPart.m_ene ));
262  p->set_pdg_id( persPart.m_pdgId);
263  p->set_status( persPart.m_status);
264  p->add_attribute("phi",std::make_shared<HepMC3::DoubleAttribute>(persPart.m_phiPolarization));
265  p->add_attribute("theta",std::make_shared<HepMC3::DoubleAttribute>(persPart.m_thetaPolarization));
267 
268  // fillin' the flow
269  std::vector<int> flows;
270  const unsigned int nFlow = persPart.m_flow.size();
271  for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
272  flows.push_back(persPart.m_flow[iFlow].second );
273  }
274  //We construct it here as vector w/o gaps.
275  p->add_attribute("flows", std::make_shared<HepMC3::VectorIntAttribute>(flows));
276 
277 #else
278  p->m_momentum.setPx( persPart.m_px );
279  p->m_momentum.setPy( persPart.m_py );
280  p->m_momentum.setPz( persPart.m_pz );
281  p->m_momentum.setE ( persPart.m_ene );
282  p->m_pdg_id = persPart.m_pdgId;
283  p->m_status = persPart.m_status;
284  p->m_polarization.m_theta= static_cast<double>(persPart.m_thetaPolarization);
285  p->m_polarization.m_phi = static_cast<double>(persPart.m_phiPolarization );
286  p->m_production_vertex = 0;
287  p->m_end_vertex = 0;
288  p->m_barcode = persPart.m_barcode;
289 
290  // fillin' the flow
291  const unsigned int nFlow = persPart.m_flow.size();
292  p->m_flow.clear();
293  for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
294  p->m_flow.set_icode( persPart.m_flow[iFlow].first,
295  persPart.m_flow[iFlow].second );
296  }
297 #endif
298 
299  if ( persPart.m_endVtx != 0 ) {
300  partToEndVtx[p] = persPart.m_endVtx;
301  }
302 
303  return p;
304 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
HepMC::GenVertexPtr
HepMC::GenVertex * GenVertexPtr
Definition: GenVertex.h:59
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
HepMC::suggest_barcode
bool suggest_barcode(T &p, int i)
Definition: GenEvent.h:548
GenParticle_p2::m_endVtx
int m_endVtx
Barcode of the decay vertex of this particle.
Definition: GenParticle_p2.h:100
GenParticle_p2::m_pz
double m_pz
z-component of the 4-momentum of this particle
Definition: GenParticle_p2.h:62
GenParticle_p2
Definition: GenParticle_p2.h:22
GenEvent_p2
Definition: GenEvent_p2.h:23
GenParticle_p2::m_py
double m_py
y-component of the 4-momentum of this particle
Definition: GenParticle_p2.h:58
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
NSWL1::nVertices
int nVertices(const Polygon &p)
Definition: GeoUtils.cxx:35
GenVertex_p2::m_y
double m_y
y-coordinate of the vertex
Definition: GenVertex_p2.h:55
Vtx
GenEvent_p2::m_signalProcessVtx
int m_signalProcessVtx
Barcode of the GenVertex holding the signal process.
Definition: GenEvent_p2.h:88
HepMC::DataPool
Definition: HepMcDataPool.h:81
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
GenVertex_p2::m_barcode
int m_barcode
barcode of this vertex (uniquely identifying a vertex within an event)
Definition: GenVertex_p2.h:83
GenEvent_p2::m_weights
std::vector< double > m_weights
Weights for this event.
Definition: GenEvent_p2.h:93
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
GenParticle_p2::m_px
double m_px
x-component of the 4-momentum of this particle
Definition: GenParticle_p2.h:54
TPConverterBase
Definition: TPConverter.h:738
GenVertex_p2::m_t
double m_t
t-coordinate of the vertex
Definition: GenVertex_p2.h:63
McEventCollection_p2::m_genEvents
std::vector< GenEvent_p2 > m_genEvents
The vector of persistent representation of GenEvents.
Definition: McEventCollection_p2.h:51
HepMcDataPool.h
GenParticle_p2::m_ene
double m_ene
e-component of the 4-momentum of this particle
Definition: GenParticle_p2.h:66
GenEvent_p2::m_alphaQED
double m_alphaQED
value of the QED coupling.
Definition: GenEvent_p2.h:82
GenParticle_p2::m_pdgId
int m_pdgId
identity of this particle, according to the Particle Data Group notation
Definition: GenParticle_p2.h:70
GenParticle_p2::m_flow
std::vector< std::pair< int, int > > m_flow
Flow for this particle.
Definition: GenParticle_p2.h:78
HepMC::DataPool::getGenParticle
HepMC::GenParticlePtr getGenParticle()
Definition: HepMcDataPool.h:160
McEventCollectionCnv_p2::persToTrans
virtual void persToTrans(const McEventCollection_p2 *persObj, McEventCollection *transObj, MsgStream &log)
Method creating the transient representation of McEventCollection from its persistent representation ...
Definition: McEventCollectionCnv_p2.cxx:50
GenVertex_p2::m_x
double m_x
x-coordinate of the vertex
Definition: GenVertex_p2.h:51
GenEvent_p2::m_eventNbr
int m_eventNbr
Event number.
Definition: GenEvent_p2.h:70
GenEvent_p2::m_verticesBegin
unsigned int m_verticesBegin
Begin position in the vector of vertices composing this event.
Definition: GenEvent_p2.h:101
GenEvent_p2::m_signalProcessId
int m_signalProcessId
Id of the processus being generated.
Definition: GenEvent_p2.h:66
McEventCollectionCnv_p2::operator=
McEventCollectionCnv_p2 & operator=(const McEventCollectionCnv_p2 &rhs)
Assignement operator.
Definition: McEventCollectionCnv_p2.cxx:34
GenEvent_p2::m_particlesEnd
unsigned int m_particlesEnd
End position in the vector of particles composing this event.
Definition: GenEvent_p2.h:113
HepMC::set_signal_process_vertex
void set_signal_process_vertex(GenEvent *e, T v)
Definition: GenEvent.h:528
HepMC::DataPool::evt
GenEvtPool_t evt
an arena of HepMC::GenEvent for efficient object instantiation
Definition: HepMcDataPool.h:140
GenVertex_p2::m_z
double m_z
z-coordinate of the vertex
Definition: GenVertex_p2.h:59
lumiFormat.i
int i
Definition: lumiFormat.py:92
GenEvent_p2::m_alphaQCD
double m_alphaQCD
value of the QCD coupling.
Definition: GenEvent_p2.h:78
McEventCollectionCnv_p2::createGenParticle
static HepMC::GenParticlePtr createGenParticle(const GenParticle_p2 &p, ParticlesMap_t &partToEndVtx, HepMC::DataPool &datapools, const HepMC::GenVertexPtr &parent=nullptr)
Create a transient GenParticle from a persistent one (vers.1) It returns the new GenParticle.
Definition: McEventCollectionCnv_p2.cxx:254
GenVertex_p2
Definition: GenVertex_p2.h:23
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
McEventCollectionCnv_p2::transToPers
virtual void transToPers(const McEventCollection *transObj, McEventCollection_p2 *persObj, MsgStream &log)
Method creating the persistent representation McEventCollection_p2 from its transient representation ...
Definition: McEventCollectionCnv_p2.cxx:176
GenParticle_p2::m_thetaPolarization
float m_thetaPolarization
polarization
Definition: GenParticle_p2.h:86
test_pyathena.parent
parent
Definition: test_pyathena.py:15
GenVertex_p2::m_id
int m_id
Id of this vertex.
Definition: GenVertex_p2.h:75
HepMC::DataPool::vtx
GenVtxPool_t vtx
an arena of HepMC::GenVertex for efficient object instantiation
Definition: HepMcDataPool.h:144
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
DataVector::clear
void clear()
Erase all the elements in the collection.
McEventCollection_p2
Definition: McEventCollection_p2.h:27
McEventCollectionCnv_p2::ParticlesMap_t
std::unordered_map< HepMC::GenParticlePtr, int > ParticlesMap_t
Definition: McEventCollectionCnv_p2.h:93
McEventCollectionCnv_p2::createGenVertex
static HepMC::GenVertexPtr createGenVertex(const McEventCollection_p2 &persEvts, const GenVertex_p2 &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.
Definition: McEventCollectionCnv_p2.cxx:194
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
McEventCollectionCnv_p2::McEventCollectionCnv_p2
McEventCollectionCnv_p2()
Default constructor:
Definition: McEventCollectionCnv_p2.cxx:25
GenVertex_p2::m_particlesIn
std::vector< int > m_particlesIn
collection of barcodes of in-going particles connected to this vertex
Definition: GenVertex_p2.h:67
GenEvent_p2::m_particlesBegin
unsigned int m_particlesBegin
Begin position in the vector of particles composing this event.
Definition: GenEvent_p2.h:109
GenParticle_p2::m_status
int m_status
Status of this particle, as defined for HEPEVT.
Definition: GenParticle_p2.h:74
GenEvent_p2::m_eventScale
double m_eventScale
Energy scale.
Definition: GenEvent_p2.h:74
GenParticle_p2::m_phiPolarization
float m_phiPolarization
phi polarization
Definition: GenParticle_p2.h:90
McEventCollectionCnv_p2::~McEventCollectionCnv_p2
virtual ~McEventCollectionCnv_p2()
Destructor.
DEBUG
#define DEBUG
Definition: page_access.h:11
GenVertex_p2::m_particlesOut
std::vector< int > m_particlesOut
collection of barcodes of out-going particles connected to this vertex
Definition: GenVertex_p2.h:71
McEventCollectionCnv_p2
Definition: McEventCollectionCnv_p2.h:48
HepMC::DataPool::getGenEvent
HepMC::GenEvent * getGenEvent()
Definition: HepMcDataPool.h:150
HepMC::barcode_to_vertex
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition: GenEvent.h:505
McEventCollectionCnv_p2.h
GenEvent_p2::m_randomStates
std::vector< long int > m_randomStates
Container of random numbers for the generator states.
Definition: GenEvent_p2.h:97
McEventCollection_p2::m_genVertices
std::vector< GenVertex_p2 > m_genVertices
The vector of persistent representation of GenVertices.
Definition: McEventCollection_p2.h:55
McEventCollection_p2::m_genParticles
std::vector< GenParticle_p2 > m_genParticles
The vector of persistent representation of GenParticles.
Definition: McEventCollection_p2.h:59
HepMC::DataPool::getGenVertex
HepMC::GenVertexPtr getGenVertex()
Definition: HepMcDataPool.h:155
GenEvent_p2::m_verticesEnd
unsigned int m_verticesEnd
End position in the vector of vertices composing this event.
Definition: GenEvent_p2.h:105
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
GenVertex_p2::m_weights
std::vector< float > m_weights
Weights for this vertex.
Definition: GenVertex_p2.h:79
HepMC::DataPool::part
GenPartPool_t part
an arena of HepMC::GenParticle for efficient object instantiation
Definition: HepMcDataPool.h:148
GenParticle_p2::m_barcode
int m_barcode
barcode of this particles (uniquely identifying this particle within a given GenEvent)
Definition: GenParticle_p2.h:105