HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
HepMCG4Interface.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 /// \file eventgenerator/HepMC/HepMCEx01/src/HepMCG4Interface.cc
27 /// \brief Implementation of the HepMCG4Interface class
28 //
29 // ====================================================================
30 //
31 // HepMCG4Interface.cc
32 // $Id$
33 //
34 // ====================================================================
35 #include "HGCal/TBStandaloneSimulator/interface/HepMCG4Interface.hh"
36 
37 #include "G4RunManager.hh"
38 #include "G4LorentzVector.hh"
39 #include "G4Event.hh"
40 #include "G4PrimaryParticle.hh"
41 #include "G4PrimaryVertex.hh"
42 #include "G4TransportationManager.hh"
43 #include "G4PhysicalConstants.hh"
44 #include "G4SystemOfUnits.hh"
45 
46 ////////////////////////////////////
47 HepMCG4Interface::HepMCG4Interface()
48  : hepmcEvent(0)
49 ////////////////////////////////////
50 {
51 }
52 
53 /////////////////////////////////////
54 HepMCG4Interface::~HepMCG4Interface()
55 /////////////////////////////////////
56 {
57  delete hepmcEvent;
58 }
59 
60 /////////////////////////////////////////////////////////
61 G4bool HepMCG4Interface::CheckVertexInsideWorld
62 (const G4ThreeVector& pos) const
63 /////////////////////////////////////////////////////////
64 {
65  G4Navigator* navigator = G4TransportationManager::GetTransportationManager()
66  -> GetNavigatorForTracking();
67 
68  G4VPhysicalVolume* world = navigator-> GetWorldVolume();
69  G4VSolid* solid = world-> GetLogicalVolume()-> GetSolid();
70  EInside qinside = solid-> Inside(pos);
71 
72  if( qinside != kInside) return false;
73  else return true;
74 }
75 
76 ////////////////////////////////////////////////////////////////
77 void HepMCG4Interface::HepMC2G4(const HepMC::GenEvent* hepmcevt,
78  G4Event* g4event)
79 ////////////////////////////////////////////////////////////////
80 {
81  for(HepMC::GenEvent::vertex_const_iterator vitr = hepmcevt->vertices_begin();
82  vitr != hepmcevt->vertices_end(); ++vitr ) { // loop for vertex ...
83 
84  // real vertex?
85  G4bool qvtx = false;
86  for (HepMC::GenVertex::particle_iterator
87  pitr = (*vitr)->particles_begin(HepMC::children);
88  pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
89 
90  if (!(*pitr)->end_vertex() && (*pitr)->status() == 1) {
91  qvtx = true;
92  break;
93  }
94  }
95  if (!qvtx) continue;
96 
97  // check world boundary
98  HepMC::FourVector pos = (*vitr)-> position();
99  G4LorentzVector xvtx(pos.x(), pos.y(), pos.z(), pos.t());
100  if (! CheckVertexInsideWorld(xvtx.vect()*mm)) continue;
101 
102  // create G4PrimaryVertex and associated G4PrimaryParticles
103  G4PrimaryVertex* g4vtx =
104  new G4PrimaryVertex(xvtx.x()*mm, xvtx.y()*mm, xvtx.z()*mm,
105  xvtx.t()*mm / c_light);
106 
107  for (HepMC::GenVertex::particle_iterator
108  vpitr = (*vitr)->particles_begin(HepMC::children);
109  vpitr != (*vitr)->particles_end(HepMC::children); ++vpitr) {
110 
111  if( (*vpitr)->status() != 1 ) continue;
112 
113  G4int pdgcode = (*vpitr)-> pdg_id();
114  pos = (*vpitr)-> momentum();
115  G4LorentzVector p(pos.px(), pos.py(), pos.pz(), pos.e());
116  G4PrimaryParticle* g4prim =
117  new G4PrimaryParticle(pdgcode, p.x()*GeV, p.y()*GeV, p.z()*GeV);
118 
119  g4vtx-> SetPrimary(g4prim);
120  }
121  g4event-> AddPrimaryVertex(g4vtx);
122  }
123 }
124 
125 
126 ///////////////////////////////////////////////////////
127 HepMC::GenEvent* HepMCG4Interface::GenerateHepMCEvent()
128 ///////////////////////////////////////////////////////
129 {
130  HepMC::GenEvent* aevent = new HepMC::GenEvent();
131  return aevent;
132 }
133 
134 //////////////////////////////////////////////////////////////
135 void HepMCG4Interface::GeneratePrimaryVertex(G4Event* anEvent)
136 //////////////////////////////////////////////////////////////
137 {
138  // delete previous event object
139  delete hepmcEvent;
140 
141  // generate next event
142  hepmcEvent = GenerateHepMCEvent();
143  if(! hepmcEvent) {
144  G4cout << "HepMCInterface: no generated particles. run terminated..."
145  << G4endl;
146  G4RunManager::GetRunManager()-> AbortRun();
147  return;
148  }
149  HepMC2G4(hepmcEvent, anEvent);
150 }
151