Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geant4Common.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Geant4Common.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 // This file is part of the Acts project.
10 //
11 // Copyright (C) 2021 CERN for the benefit of the Acts project
12 //
13 // This Source Code Form is subject to the terms of the Mozilla Public
14 // License, v. 2.0. If a copy of the MPL was not distributed with this
15 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 
18 
39 
40 #include <memory>
41 #include <string>
42 
43 #include <FTFP_BERT.hh>
44 #include <G4RunManager.hh>
45 #include <G4VUserDetectorConstruction.hh>
46 #include <boost/program_options.hpp>
47 
48 namespace ActsExamples {
49 
52  ActsExamples::Sequencer& sequencer,
53  std::shared_ptr<DetectorConstructionFactory> detectorConstructionFactory) {
54  auto g4loglevel =
55  Acts::Logging::Level(vars["g4-loglevel"].as<unsigned int>());
56  size_t seed = vars["g4-seed"].as<size_t>();
57 
59 
60  g4Cfg.detectorConstructionFactory = std::move(detectorConstructionFactory);
61  g4Cfg.randomNumbers = std::make_shared<ActsExamples::RandomNumbers>(
63  g4Cfg.inputParticles = Simulation::kParticlesInitial;
64  g4Cfg.outputMaterialTracks = Simulation::kMaterialTracks;
65  g4Cfg.excludeMaterials = {"Air", "Vacuum"};
66 
67  sequencer.addAlgorithm(
68  std::make_shared<Geant4MaterialRecording>(g4Cfg, g4loglevel));
69 }
70 
73  ActsExamples::Sequencer& sequencer,
74  std::shared_ptr<DetectorConstructionFactory> detectorConstructionFactory,
75  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
76  std::shared_ptr<const Acts::MagneticFieldProvider> magneticField) {
77  auto g4loglevel =
78  Acts::Logging::Level(vars["g4-loglevel"].as<unsigned int>());
79  size_t seed = vars["g4-seed"].as<size_t>();
80 
82 
83  g4Cfg.detectorConstructionFactory = std::move(detectorConstructionFactory);
84  g4Cfg.randomNumbers = std::make_shared<ActsExamples::RandomNumbers>(
86  g4Cfg.inputParticles = Simulation::kParticlesSelection;
87  g4Cfg.outputSimHits = Simulation::kSimHits;
88  g4Cfg.outputParticlesInitial = Simulation::kParticlesInitial;
89  g4Cfg.outputParticlesFinal = Simulation::kParticlesFinal;
90  g4Cfg.trackingGeometry = std::move(trackingGeometry);
91  g4Cfg.magneticField = std::move(magneticField);
92 
93  sequencer.addAlgorithm(std::make_shared<Geant4Simulation>(g4Cfg, g4loglevel));
94 }
95 
98  std::shared_ptr<DetectorConstructionFactory> detectorConstructionFactory) {
99  // Set up the sequencer
100  Sequencer sequencer(Options::readSequencerConfig(vars));
101 
102  // Output level and log level
103  auto logLevel = Options::readLogLevel(vars);
104 
106  ensureWritableDirectory(vars["output-dir"].as<std::string>());
107 
108  // Basic services
109  auto rnd =
110  std::make_shared<RandomNumbers>(Options::readRandomNumbersConfig(vars));
111 
112  // Event generation w/ particle gun
114  evgen.outputParticles = Simulation::kParticlesInitial;
115  evgen.randomNumbers = rnd;
116  sequencer.addReader(std::make_shared<EventGenerator>(evgen, logLevel));
117 
118  // Set up the Geant4 Simulation
119  setupMaterialRecording(vars, sequencer,
120  std::move(detectorConstructionFactory));
121 
122  // setup the output writing
123  if (vars["output-root"].as<bool>()) {
124  // Write the propagation steps as ROOT TTree
125  RootMaterialTrackWriter::Config materialTrackWriter;
126  materialTrackWriter.prePostStep = true;
127  materialTrackWriter.recalculateTotals = true;
128  materialTrackWriter.collection = Simulation::kMaterialTracks;
129  materialTrackWriter.filePath = joinPaths(
130  outputDir,
131  "geant4_" + std::string(Simulation::kMaterialTracks) + ".root");
132  sequencer.addWriter(std::make_shared<RootMaterialTrackWriter>(
133  materialTrackWriter, logLevel));
134  }
135 
136  auto result = sequencer.run();
137  return result;
138 }
139 
142  std::shared_ptr<DetectorConstructionFactory> detectorConstructionFactory,
143  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry) {
144  // Basic services
145  auto randomNumbers =
146  std::make_shared<RandomNumbers>(Options::readRandomNumbersConfig(vars));
147 
148  // Set up the sequencer
149  Sequencer sequencer(Options::readSequencerConfig(vars));
150 
151  // Set up the magnetic field
153 
154  // Setup algorithm chain: Input / Simulation / Output
155  Simulation::setupInput(vars, sequencer, randomNumbers);
156  setupGeant4Simulation(vars, sequencer, std::move(detectorConstructionFactory),
157  std::move(trackingGeometry), magneticField);
158  Simulation::setupOutput(vars, sequencer);
159 
160  auto result = sequencer.run();
161  return result;
162 }
163 
164 } // namespace ActsExamples