14 #include "G4PrimaryParticle.hh"
15 #include "G4PrimaryVertex.hh"
16 #include "G4GenericMessenger.hh"
17 #include "G4SystemOfUnits.hh"
22 #include <HepMC3/Version.h>
23 #if HEPMC3_VERSION_CODE < 3002004
24 #include <HepMC_3_2_4_ReaderFactory.h>
26 #include <HepMC3/ReaderFactory.h>
29 #include <HepMC3/GenEvent.h>
30 #include <HepMC3/GenVertex.h>
31 #include <HepMC3/GenParticle.h>
34 #include "G4AutoLock.hh"
35 namespace { G4Mutex myHepMC3Mutex = G4MUTEX_INITIALIZER; }
41 G4AutoLock mlock(&myHepMC3Mutex);
49 messenger =
new G4GenericMessenger(
this,
"/eAST/generator/HepMC3/",
50 "HepMC3 interface commands");
52 auto& fileCmd =
messenger->DeclareMethod(
"openFile",
54 fileCmd.SetParameterName(
"fileName",
false);
55 fileCmd.SetToBeBroadcasted(
false);
57 auto& vtxCmd =
messenger->DeclareMethodWithUnit(
"vertex",
"mm",
59 "Set vertex position");
60 vtxCmd.SetParameterName(
"position",
false);
61 vtxCmd.SetToBeBroadcasted(
false);
63 auto& timeCmd =
messenger->DeclareMethodWithUnit(
"time0",
"s",
65 "Set t0 (start time at the primary vertex)");
66 timeCmd.SetParameterName(
"t0",
false);
67 timeCmd.SetToBeBroadcasted(
false);
69 auto& verCmd =
messenger->DeclareMethod(
"verbose",
71 verCmd.SetParameterName(
"verboseLevel",
true);
72 verCmd.SetDefaultValue(
"0");
73 verCmd.SetToBeBroadcasted(
false);
80 G4AutoLock mlock(&myHepMC3Mutex);
89 G4cout <<
"eASTHepMC3Interface - opening " <<
fileName << G4endl;
98 G4Exception(
"eASTHepMC3Interface::GeneratePrimaryVertex",
"Event0201",
99 FatalException,
"eASTHepMC3Interface:: cannot open input.");
104 G4cout <<
"eASTHepMC3Interface - " <<
fileName <<
" is open." << G4endl;
112 G4AutoLock mlock(&myHepMC3Mutex);
115 HepMC3::GenEvent hepevt(HepMC3::Units::GEV,HepMC3::Units::MM);
119 G4Exception(
"eASTHepMC3Interface::GeneratePrimaryVertex",
"Event0201",
120 FatalException,
"eASTHepMC3Interface:: cannot open input.");
128 auto pos = hepevt.event_pos();
129 auto* g4vtx =
new G4PrimaryVertex(
pos.x()*
mm,
pos.y()*
mm,
pos.z()*
mm, 0);
141 bool safetycheck=
true;
144 for(
auto hep_p : hepevt.particles()) {
146 auto status = hep_p->status();
147 if ( status != 1 && status != 2 )
continue;
150 auto id = hep_p->id();
152 G4PrimaryParticle* g4prim;
154 g4prim = finditer->second;
159 g4vtx->SetPrimary(g4prim);
162 if ( hep_p->status() == 1 ){
164 if ( hep_p->children().size() !=0 ){
166 G4Exception(
"eASTHepMC3Interface::GeneratePrimaryVertex",
"HepmcStatus",
167 FatalException,
"eASTHepMC3Interface:: status 1 but children.");
173 if ( hep_p->status() == 2 ){
174 if ( hep_p->children().size() ==0 ){
176 G4Exception(
"eASTHepMC3Interface::GeneratePrimaryVertex",
"HepmcStatus",
177 FatalException,
"eASTHepMC3Interface:: status 2 but no children.");
180 for (
auto hep_d : hep_p->children()) {
181 auto id_d = hep_d->id();
185 G4cout <<
" PROBLEM: This primary with id = " << hep_d->id()
186 <<
", mother id = " << hep_p->id()
187 <<
", pid = " << hep_d->pid()
188 <<
", status = " << hep_d->status()
189 <<
" and 4-momentum = " << hep_d->momentum().px() <<
" " << hep_d->momentum().py() <<
" , " << hep_d->momentum().pz() <<
" , " << hep_d->momentum().e()
191 G4cout <<
" PROBLEM: already there with: " << G4endl;
192 finditer_d->second->Print();
194 G4Exception(
"eASTHepMC3Interface::GeneratePrimaryVertex",
"Daughter",
195 FatalException,
"eASTHepMC3Interface:: found daughter out of order.");
197 if ( hep_p->status() == 2 || hep_p->status() == 1 ){
198 auto g4daughter =
MakeParticle ( hep_d, safetycheck, used );
199 g4prim->SetDaughter(g4daughter);
211 g4event->AddPrimaryVertex(g4vtx);
215 const bool safetycheck, std::set<int>& used){
218 auto id = hep_p->id();
219 if ( used.count (
id ) ){
220 G4Exception(
"eASTHepMC3Interface::MakeParticle",
"Particle",
221 FatalException,
"eASTHepMC3Interface:: Double-counting particles.");
226 auto p = hep_p->momentum();
227 auto* g4prim =
new G4PrimaryParticle(hep_p->pid(),
p.x()*
GeV,
p.y()*
GeV,
p.z()*
GeV);
228 g4prim->SetPolarization(0, 0, 0);
231 G4cout <<
" Made primary with pid = " << hep_p->pid()
232 <<
", status = " << hep_p->status()
233 <<
" and 4-momentum = " <<
p.px() <<
" " <<
p.py() <<
" , " <<
p.pz() <<
" , " <<
p.e()