ATLAS Offline Software
HepMcDataPool.h
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // HepMcDataPool.h
8 // Header file for a set of utilities for DataPool w/ HepMC classes
9 // Author: S.Binet<binet@cern.ch>
11 #ifndef GENERATOROBJECTSATHENAPOOL_HEPMCDATAPOOL_H
12 #define GENERATOROBJECTSATHENAPOOL_HEPMCDATAPOOL_H
13 
14 // HepMC / CLHEP includes
15 #ifndef HEPMC3
16 # ifdef __clang__
17 # pragma clang diagnostic push
18 # pragma clang diagnostic ignored "-Wkeyword-macro"
19 # endif
20 # define private public
21 # define protected public
22 #endif
23 #include "AtlasHepMC/GenEvent.h"
24 #include "AtlasHepMC/GenVertex.h"
25 #include "AtlasHepMC/GenParticle.h"
26 #ifndef HEPMC3
27 # undef private
28 # undef protected
29 # ifdef __clang__
30 # pragma clang diagnostic pop
31 # endif
32 #endif
33 
34 #include "AthAllocators/DataPool.h"
35 
36 #ifndef HEPMC3
37 
38 // specialization of the destruction functions for our various DataPools
39 // these specializations are needed because we have to work-around the
40 // 'shared' ownership of particles and vertices by both GenEvent and the
41 // various DataPool<Xyz>.
42 namespace SG {
43 
44  template<>
45  inline void
46  ArenaAllocatorBase::destroy_fcn<HepMC::GenParticle>(ArenaAllocatorBase::pointer p)
47  {
48  HepMC::GenParticle* part = reinterpret_cast<HepMC::GenParticle*>(p);
49  part->m_production_vertex = 0;
50  part->m_end_vertex = 0;
51  part->~GenParticle();
52  }
53 
54  template<>
55  inline void
56  ArenaAllocatorBase::destroy_fcn<HepMC::GenVertex>(ArenaAllocatorBase::pointer p)
57  {
58  HepMC::GenVertex* vtx = reinterpret_cast<HepMC::GenVertex*>(p);
59  vtx->m_event = 0;
60  vtx->m_particles_in.clear();
61  vtx->m_particles_out.clear();
62  vtx->~GenVertex();
63  }
64 
65  template<>
66  inline void
67  ArenaAllocatorBase::destroy_fcn<HepMC::GenEvent>(ArenaAllocatorBase::pointer p)
68  {
69  HepMC::GenEvent* evt = reinterpret_cast<HepMC::GenEvent*>(p);
70  evt->m_particle_barcodes.clear();
71  evt->m_vertex_barcodes.clear();
72  delete evt->m_pdf_info; evt->m_pdf_info = 0;
73  evt->~GenEvent();
74  }
75 } // end namespace SG
76 
77 #endif
78 
79 namespace HepMC {
80 
81  struct DataPool {
82 
83 #ifdef HEPMC3
84  // Helpers for allocating HepMC objects from a DataPool.
85  // But because HepMC3 keeps shared_ptr's to its objects, we need
86  // to be careful here.
87  //
88  // First, the memory we get from the pool is actually owned by the pool,
89  // so we don't want the shared_ptr's to actually delete anything.
90  // We accomplish this by creating the shared_ptr's for particles and
91  // vertices with null deleters. (This isn't an issue for the GenEvent
92  // objects, since we don't manage them what shared_ptr, but we do need
93  // to be careful not to put them in an owning DataVector.)
94  //
95  // Second, before we create a shared_ptr with a pointer we've just
96  // gotten from the DataPool, we need to be sure that there aren't any
97  // other shared_ptr's to the same object --- otherwise, the behavior
98  // is undefined. (We hide the worst consequences of this by the fact
99  // that we have no-op deleters, but it can still result in the weak
100  // references in GenParticle mysteriously expiring. See ATR-26790.)
101  // So we need to clear the objects before that. We could in principle
102  // do that in the get* functions, but it's nicer to set up clear hooks
103  // in the DataPool so that that happens when objects are returned
104  // to the pool. (And that way, we don't maintain allocated memory
105  // from free objects in the pool.)
106 
107  static void clearEvent (HepMC::GenEvent* evt)
108  {
109  evt->clear();
110  }
112  HepMC::GenEvent* getGenEvent()
113  {
114  return evt.nextElementPtr();
115  }
116 
117  static void clearVertex (HepMC::GenVertex* vtx)
118  {
119  *vtx = HepMC::GenVertex();
120  }
123  {
124  return HepMC::GenVertexPtr (vtx.nextElementPtr(), [](HepMC::GenVertex*){});
125  }
126 
127 
128  static void clearParticle (HepMC::GenParticle* part)
129  {
131  }
134  {
135  return HepMC::GenParticlePtr (part.nextElementPtr(), [](HepMC::GenParticle*){});
136  }
137 #else
138  typedef ::DataPool<HepMC::GenEvent> GenEvtPool_t;
141 
142  typedef ::DataPool<HepMC::GenVertex> GenVtxPool_t;
145 
146  typedef ::DataPool<HepMC::GenParticle> GenPartPool_t;
149 
150  HepMC::GenEvent* getGenEvent()
151  {
152  return evt.nextElementPtr();
153  }
154 
156  {
157  return vtx.nextElementPtr();
158  }
159 
161  {
162  return part.nextElementPtr();
163  }
164 #endif
165 
166  };
167 
168 } // end namespace HepMC
169 
170 #endif // GENERATOROBJECTSATHENAPOOL_HEPMCDATAPOOL_H
HepMC::GenVertexPtr
HepMC::GenVertex * GenVertexPtr
Definition: GenVertex.h:59
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
GenEvent.h
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
HepMC::DataPool
Definition: HepMcDataPool.h:81
GenVertex.h
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
HepMC::DataPool::GenEvtPool_t
::DataPool< HepMC::GenEvent > GenEvtPool_t
Definition: HepMcDataPool.h:138
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
GenParticle.h
HepMC::DataPool::getGenParticle
HepMC::GenParticlePtr getGenParticle()
Definition: HepMcDataPool.h:160
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
HepMC::DataPool::evt
GenEvtPool_t evt
an arena of HepMC::GenEvent for efficient object instantiation
Definition: HepMcDataPool.h:140
HepMC::DataPool::vtx
GenVtxPool_t vtx
an arena of HepMC::GenVertex for efficient object instantiation
Definition: HepMcDataPool.h:144
HepMC::DataPool::GenVtxPool_t
::DataPool< HepMC::GenVertex > GenVtxPool_t
Definition: HepMcDataPool.h:142
DataPool.h
HepMC::DataPool::GenPartPool_t
::DataPool< HepMC::GenParticle > GenPartPool_t
Definition: HepMcDataPool.h:146
HepMC
Definition: Barcode.h:14
HepMC::DataPool::getGenEvent
HepMC::GenEvent * getGenEvent()
Definition: HepMcDataPool.h:150
SG::ArenaAllocatorBase::pointer
char * pointer
Type for pointers to elements.
Definition: ArenaAllocatorBase.h:137
HepMC::DataPool::getGenVertex
HepMC::GenVertexPtr getGenVertex()
Definition: HepMcDataPool.h:155
GenParticle
@ GenParticle
Definition: TruthClasses.h:30
HepMC::DataPool::part
GenPartPool_t part
an arena of HepMC::GenParticle for efficient object instantiation
Definition: HepMcDataPool.h:148