McEventCollectionCnv_p2.cxx 9.14 KB
Newer Older
1
2
3
4
5
6
///////////////////////// -*- C++ -*- /////////////////////////////

/*
  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/

7
// McEventCollectionCnv_p2.cxx
8
9
// Implementation file for class McEventCollectionCnv_p2
// Author: S.Binet<binet@cern.ch>
10
///////////////////////////////////////////////////////////////////
11
12
13
14
15
16
17
18
19


// STL includes
#include <utility>

// GeneratorObjectsTPCnv includes
#include "GeneratorObjectsTPCnv/McEventCollectionCnv_p2.h"
#include "HepMcDataPool.h"

20
#if 0
21
namespace {
22
23
24
25
26
27
28
29
30
31
32
33
34
  // helper method to compute the number of particles and vertices in a
  // whole McEventCollection
  std::pair<unsigned int,unsigned int>
  nbrParticlesAndVertices( const McEventCollection* mcEvents ) {
    unsigned int nParts = 0;
    unsigned int nVerts = 0;
    const McEventCollection::const_iterator itrEnd = mcEvents->end();
    for ( McEventCollection::const_iterator itr = mcEvents->begin();
          itr != itrEnd;
          ++itr ) {
      nParts += (*itr)->particles_size();
      nVerts += (*itr)->vertices_size();
    }
35

36
37
    return std::make_pair( nParts, nVerts );
  }
38
}
39
#endif
40

41
42
43
///////////////////////////////////////////////////////////////////
// Public methods:
///////////////////////////////////////////////////////////////////
44
45
46
47

// Constructors
////////////////
McEventCollectionCnv_p2::McEventCollectionCnv_p2() :
48
  Base_t( )
49
50
51
{}

McEventCollectionCnv_p2::McEventCollectionCnv_p2( const McEventCollectionCnv_p2& rhs ) :
52
  Base_t( rhs )
53
54
{}

55
McEventCollectionCnv_p2&
56
57
58
59
McEventCollectionCnv_p2::operator=( const McEventCollectionCnv_p2& rhs )
{
  if ( this != &rhs ) {
    Base_t::operator=( rhs );
60
  }
61
62
63
64
65
66
67
68
69
70
  return *this;
}

// Destructor
///////////////

McEventCollectionCnv_p2::~McEventCollectionCnv_p2()
{
}

71
72
///////////////////////////////////////////////////////////////////
// Const methods:
73
74
75
///////////////////////////////////////////////////////////////////

void McEventCollectionCnv_p2::persToTrans( const McEventCollection_p2* persObj,
76
77
                                           McEventCollection* transObj,
                                           MsgStream& msg )
78
79
{
  msg << MSG::DEBUG << "Loading McEventCollection from persistent state..."
80
      << endmsg;
81
82
83

  // elements are managed by DataPool
  transObj->clear(SG::VIEW_ELEMENTS);
84
85
86
87
  HepMC::DataPool datapools;
  const unsigned int nVertices = persObj->m_genVertices.size();
  if ( datapools.vtx.capacity() - datapools.vtx.allocated() < nVertices ) {
    datapools.vtx.reserve( datapools.vtx.allocated() + nVertices );
88
  }
89
90
91
  const unsigned int nParts = persObj->m_genParticles.size();
  if ( datapools.part.capacity() - datapools.part.allocated() < nParts ) {
    datapools.part.reserve( datapools.part.allocated() + nParts );
92
93
  }
  const unsigned int nEvts = persObj->m_genEvents.size();
94
95
  if ( datapools.evt.capacity() - datapools.evt.allocated() < nEvts ) {
    datapools.evt.reserve( datapools.evt.allocated() + nEvts );
96
  }
97
98
  DataPool<HepMC::GenEvent>& poolOfEvents = datapools.evt;

99
100
101
  transObj->reserve( nEvts );
  const std::vector<GenEvent_p2>::const_iterator itrEnd = persObj->m_genEvents.end();
  for ( std::vector<GenEvent_p2>::const_iterator itr = persObj->m_genEvents.begin();
102
103
        itr != itrEnd;
        ++itr ) {
104
    const GenEvent_p2& persEvt = *itr;
105
    HepMC::GenEvent * genEvt        = poolOfEvents.nextElementPtr();
106
107
108
109
110
111
112
113
114
115
116
117
118
    genEvt->m_signal_process_id     = persEvt.m_signalProcessId;
    genEvt->m_event_number          = persEvt.m_eventNbr;
    genEvt->m_event_scale           = persEvt.m_eventScale;
    genEvt->m_alphaQCD              = persEvt.m_alphaQCD;
    genEvt->m_alphaQED              = persEvt.m_alphaQED;
    genEvt->m_signal_process_vertex = 0;
    genEvt->m_weights               = persEvt.m_weights;
    genEvt->m_random_states         = persEvt.m_randomStates;
    genEvt->m_vertex_barcodes.clear();
    genEvt->m_particle_barcodes.clear();
    genEvt->m_pdf_info = 0;         //> not available at that time...

    transObj->push_back( genEvt );
119
120

    // create a temporary map associating the barcode of an end-vtx to its
121
122
123
124
    // particle.
    // As not all particles are stable (d'oh!) we take 50% of the number of
    // particles as an initial size of the hash-map (to prevent re-hash)
    ParticlesMap_t partToEndVtx( (persEvt.m_particlesEnd-
125
126
                                  persEvt.m_particlesBegin)/2 );

127
128
129
    // create the vertices
    const unsigned int endVtx = persEvt.m_verticesEnd;
    for ( unsigned int iVtx= persEvt.m_verticesBegin; iVtx != endVtx; ++iVtx ) {
130
131
132
133
      genEvt->add_vertex( createGenVertex( *persObj,
                                           persObj->m_genVertices[iVtx],
                                           partToEndVtx,
                                           &datapools ) );
134
    } //> end loop over vertices
135

136
137
138
139
140
141
142
143
144
    // set the signal process vertex
    const int sigProcVtx = persEvt.m_signalProcessVtx;
    if ( sigProcVtx != 0 ) {
      genEvt->set_signal_process_vertex( genEvt->barcode_to_vertex( sigProcVtx ) );
    }

    // connect particles to their end vertices
    const ParticlesMap_t::iterator endItr = partToEndVtx.end();
    for ( ParticlesMap_t::iterator p = partToEndVtx.begin();
145
146
          p != endItr;
          ++p ) {
147
148
      HepMC::GenVertex* decayVtx = genEvt->barcode_to_vertex( p->second );
      if ( decayVtx ) {
149
        decayVtx->add_particle_in( p->first );
150
      } else {
151
152
        msg << MSG::ERROR
            << "GenParticle points to null end vertex !!"
153
            << endmsg;
154
155
156
157
158
      }
    }
  } //> end loop over m_genEvents

  msg << MSG::DEBUG << "Loaded McEventCollection from persistent state [OK]"
159
      << endmsg;
160
161
162
163

  return;
}

164
165
166
void McEventCollectionCnv_p2::transToPers( const McEventCollection*,
                                           McEventCollection_p2*,
                                           MsgStream& msg )
167
168
{
  msg << MSG::DEBUG << "Creating persistent state of McEventCollection..."
169
      << endmsg;
170
171
172

  msg << MSG::ERROR
      << "This transient-to-persistent converter method has been RETIRED !!"
173
      << endmsg
174
      << "You are not supposed to end-up here ! Go away !"
175
      << endmsg;
176
177
178
179
180

  throw std::runtime_error( "Retired McEventCollectionCnv_p2::transToPers() !!" );
  return;
}

181
182
183
///////////////////////////////////////////////////////////////////
// Non-const methods:
///////////////////////////////////////////////////////////////////
184

185
186
187
///////////////////////////////////////////////////////////////////
// Protected methods:
///////////////////////////////////////////////////////////////////
188

189
HepMC::GenVertex*
190
McEventCollectionCnv_p2::createGenVertex( const McEventCollection_p2& persEvt,
191
192
                                          const GenVertex_p2& persVtx,
                                          ParticlesMap_t& partToEndVtx, HepMC::DataPool* datapools ) const
193
{
194
195
  DataPool<HepMC::GenVertex>& poolOfVertices = datapools->vtx;
  HepMC::GenVertex * vtx = poolOfVertices.nextElementPtr();
196
197
198
199
200
201
202
203
204
  vtx->m_position.setX( persVtx.m_x );
  vtx->m_position.setY( persVtx.m_y );
  vtx->m_position.setZ( persVtx.m_z );
  vtx->m_position.setT( persVtx.m_t );
  vtx->m_particles_in.clear();
  vtx->m_particles_out.clear();
  vtx->m_id      = persVtx.m_id;
  vtx->m_weights.m_weights.reserve( persVtx.m_weights.size() );
  vtx->m_weights.m_weights.assign ( persVtx.m_weights.begin(),
205
                                    persVtx.m_weights.end() );
206
207
  vtx->m_event   = 0;
  vtx->m_barcode = persVtx.m_barcode;
208

209
210
211
  // handle the in-going (orphans) particles
  const unsigned int nPartsIn = persVtx.m_particlesIn.size();
  for ( unsigned int i = 0; i != nPartsIn; ++i ) {
212
213
214
    createGenParticle( persEvt.m_genParticles[persVtx.m_particlesIn[i]],
                       partToEndVtx,
                       datapools );
215
  }
216

217
218
219
  // now handle the out-going particles
  const unsigned int nPartsOut = persVtx.m_particlesOut.size();
  for ( unsigned int i = 0; i != nPartsOut; ++i ) {
220
221
222
    vtx->add_particle_out( createGenParticle( persEvt.m_genParticles[persVtx.m_particlesOut[i]],
                                              partToEndVtx,
                                              datapools ) );
223
224
225
226
227
  }

  return vtx;
}

228
HepMC::GenParticle*
229
McEventCollectionCnv_p2::createGenParticle( const GenParticle_p2& persPart,
230
                                            ParticlesMap_t& partToEndVtx, HepMC::DataPool* datapools ) const
231
{
232
233
  DataPool<HepMC::GenParticle>& poolOfParticles = datapools->part;
  HepMC::GenParticle* p    = poolOfParticles.nextElementPtr();
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  p->m_momentum.setPx( persPart.m_px  );
  p->m_momentum.setPy( persPart.m_py  );
  p->m_momentum.setPz( persPart.m_pz  );
  p->m_momentum.setE ( persPart.m_ene );
  p->m_pdg_id              = persPart.m_pdgId;
  p->m_status              = persPart.m_status;
  p->m_polarization.m_theta= static_cast<double>(persPart.m_thetaPolarization);
  p->m_polarization.m_phi  = static_cast<double>(persPart.m_phiPolarization  );
  p->m_production_vertex   = 0;
  p->m_end_vertex          = 0;
  p->m_barcode             = persPart.m_barcode;

  // fillin' the flow
  const unsigned int nFlow = persPart.m_flow.size();
  for ( unsigned int iFlow= 0; iFlow != nFlow; ++iFlow ) {
249
250
    p->m_flow.set_icode( persPart.m_flow[iFlow].first,
                         persPart.m_flow[iFlow].second );
251
252
253
254
255
256
257
258
  }

  if ( persPart.m_endVtx != 0 ) {
    partToEndVtx[p] = persPart.m_endVtx;
  }

  return p;
}