|
| 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