Skip to content

Commit 50ebfc1

Browse files
committed
Add a tool that writes the Geant4 particle table
This commit provides a new tool o2-sim-g4-particle-table that writes the Geant4 particle table to a text file. - One line per particle with a PDG code: pdg, Geant4 name, ctau in mm, short-lived flag, decay-table flag. - The Geant4 setup is the same as in o2-sim-g4-determine-unknown-pdg-properties. - Particles that o2-sim adds at run time are not in the table. Usage: o2-sim-g4-particle-table --output g4_particle_table.dat https://its.cern.ch/jira/browse/O2-5903 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3566138 commit 50ebfc1

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎run/CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ o2_add_executable(g4-determine-unknown-pdg-properties
113113
SOURCES g4DetermineUnknownPdgProperties.cxx
114114
PUBLIC_LINK_LIBRARIES O2::O2TrivialMC MC::Geant4 MC::Geant4VMC O2::SimConfig)
115115

116+
# Writes the Geant4 particle table to a text file.
117+
o2_add_executable(g4-particle-table
118+
COMPONENT_NAME sim
119+
SOURCES o2sim_g4_particle_table.cxx
120+
PUBLIC_LINK_LIBRARIES O2::O2TrivialMC MC::Geant4 MC::Geant4VMC O2::SimConfig)
121+
116122
# Reads a geometry file and a field and reports where the two do not fit together.
117123
# It needs neither the simulation engines nor any detector code -- the medium flags
118124
# it reasons about are GSTMED parameters stored in the geometry file itself -- so it

‎run/o2sim_g4_particle_table.cxx‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
// Writes the Geant4 particle table (one line per particle with a PDG code) to a text file.
13+
14+
#include <cstdio>
15+
#include <iostream>
16+
#include <string>
17+
#include <fstream>
18+
19+
#include "G4ParticleTable.hh"
20+
#include "G4ParticleDefinition.hh"
21+
#include "G4Version.hh"
22+
#include "CLHEP/Units/PhysicalConstants.h"
23+
24+
#include "TGeant4.h"
25+
#include "TG4RunConfiguration.h"
26+
27+
#include "SimConfig/G4Params.h"
28+
#include "O2TrivialMC/O2TrivialMCApplication.h"
29+
30+
int main(int argc, char** argv)
31+
{
32+
if (argc != 3 || std::string(argv[1]) != "--output") {
33+
std::cerr << "Usage: " << argv[0] << " --output <file>\n";
34+
return 1;
35+
}
36+
37+
std::ofstream os{argv[2]};
38+
if (!os.is_open()) {
39+
std::cerr << "Cannot open " << argv[2] << "\n";
40+
return 1;
41+
}
42+
43+
// We use our O2 VMC setup to make sure we are in line with our physics definition.
44+
new o2::mc::O2TrivialMCApplication();
45+
auto& physicsSetup = ::o2::conf::G4Params::Instance().getPhysicsConfigString();
46+
auto runConfiguration = new TG4RunConfiguration("geomRoot", physicsSetup);
47+
auto vmc = new TGeant4("TGeant4", "The Geant4 Monte Carlo", runConfiguration);
48+
// needs to be initialised to have the particle table / physics entirely ready
49+
vmc->Init();
50+
51+
os << "# Geant4 particle table, Geant4 version " << G4VERSION_NUMBER << "\n";
52+
os << "# physics setup: " << physicsSetup << "\n";
53+
os << "# pdg g4name ctau_mm shortlived hasDecayTable\n";
54+
55+
auto table = G4ParticleTable::GetParticleTable();
56+
auto it = table->GetIterator();
57+
it->reset();
58+
int nParticles = 0;
59+
while ((*it)()) {
60+
auto particle = it->value();
61+
int pdg = particle->GetPDGEncoding();
62+
if (pdg == 0) {
63+
continue;
64+
}
65+
double lifetime = particle->GetPDGLifeTime();
66+
// lifetime is in G4 internal units, times c_light gives mm
67+
double ctau = lifetime > 0. ? lifetime * CLHEP::c_light : 0.;
68+
char buf[64];
69+
std::snprintf(buf, sizeof(buf), "%.6g", ctau);
70+
os << pdg << " " << particle->GetParticleName() << " " << buf << " "
71+
<< (particle->IsShortLived() ? 1 : 0) << " "
72+
<< (particle->GetDecayTable() ? 1 : 0) << "\n";
73+
++nParticles;
74+
}
75+
std::cout << "Wrote " << nParticles << " particles to " << argv[2] << "\n";
76+
return 0;
77+
}

0 commit comments

Comments
 (0)