CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/SimG4Core/Application/src/G4SimEvent.cc

Go to the documentation of this file.
00001 #include "SimG4Core/Application/interface/G4SimEvent.h"
00002 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
00003 
00004 #include "G4SystemOfUnits.hh"
00005 
00006 class IdSort{
00007 public:
00008   bool operator()(const SimTrack& a, const SimTrack& b) {
00009     return a.trackId() < b.trackId();
00010   }
00011 };
00012 
00013 
00014 G4SimEvent::G4SimEvent() : hepMCEvent(0),
00015                            weight_(0),
00016                            collisionPoint_(math::XYZTLorentzVectorD(0.,0.,0.,0.)),
00017                            nparam_(0),param_(0) {}
00018 
00019 G4SimEvent::~G4SimEvent() 
00020 {
00021 
00022 /*
00023    while ( !g4tracks.empty() )
00024    {
00025       delete g4tracks.back() ;
00026       g4tracks.pop_back() ;
00027    }
00028    while ( !g4vertices.empty() )
00029    {
00030       delete g4vertices.back() ;
00031       g4vertices.pop_back() ;
00032    }
00033 */
00034 
00035    // per suggestion by Chris Jones, it's faster 
00036    // that delete back() and pop_back() 
00037    //
00038    unsigned int i = 0 ;
00039    
00040    for ( i=0; i<g4tracks.size(); i++ )
00041    {
00042       delete g4tracks[i] ;
00043       g4tracks[i] = 0 ;
00044    }
00045    g4tracks.clear() ;
00046    
00047    for ( i=0; i<g4vertices.size(); i++ )
00048    {
00049       delete g4vertices[i] ;
00050       g4vertices[i] = 0 ;
00051    }
00052    g4vertices.clear();
00053 }
00054 
00055 void G4SimEvent::load(edm::SimTrackContainer & c) const
00056 {
00057     for (unsigned int i=0; i<g4tracks.size(); i++)
00058     {
00059         G4SimTrack * trk    = g4tracks[i];
00060         int ip              = trk->part();
00061         math::XYZTLorentzVectorD p( trk->momentum().x()/GeV,
00062                                     trk->momentum().y()/GeV,
00063                                     trk->momentum().z()/GeV,
00064                                     trk->energy()/GeV ) ;
00065         int iv              = trk->ivert();
00066         int ig              = trk->igenpart();
00067         int id              = trk->id();
00068         math::XYZVectorD tkpos( trk->trackerSurfacePosition().x()/cm,
00069                                 trk->trackerSurfacePosition().y()/cm,
00070                                 trk->trackerSurfacePosition().z()/cm ) ;
00071         math::XYZTLorentzVectorD tkmom( trk->trackerSurfaceMomentum().x()/GeV,
00072                                         trk->trackerSurfaceMomentum().y()/GeV,
00073                                         trk->trackerSurfaceMomentum().z()/GeV,
00074                                         trk->trackerSurfaceMomentum().e()/GeV ) ;
00075         // ip = particle ID as PDG
00076         // pp = 4-momentum
00077         // iv = corresponding G4SimVertex index
00078         // ig = corresponding GenParticle index
00079         SimTrack t = SimTrack(ip,p,iv,ig,tkpos,tkmom);
00080         t.setTrackId(id);
00081         t.setEventId(EncodedEventId(0));
00082         c.push_back(t);
00083     }
00084     std::stable_sort(c.begin(),c.end(),IdSort());
00085     
00086 }
00087 
00088 void G4SimEvent::load(edm::SimVertexContainer & c) const
00089 {
00090     for (unsigned int i=0; i<g4vertices.size(); i++)
00091     {
00092         G4SimVertex * vtx   = g4vertices[i];
00093         //
00094         // starting 1_1_0_pre3, SimVertex stores in cm !!!
00095         // 
00096         math::XYZVectorD v3( vtx->vertexPosition().x()/cm, 
00097                              vtx->vertexPosition().y()/cm,
00098                              vtx->vertexPosition().z()/cm ) ;
00099         float t             = vtx->vertexGlobalTime()/second;
00100         int iv              = vtx->parentIndex();
00101         // vv = position
00102         // t  = global time
00103         // iv = index of the parent in the SimEvent SimTrack container (-1 if no parent)
00104         SimVertex v = SimVertex(v3,t,iv,i);
00105         v.setEventId(EncodedEventId(0));
00106         c.push_back(v);
00107     }
00108 }
00109