Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CommonSimulation.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CommonSimulation.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 
10 
21 
22 #include <boost/program_options.hpp>
23 
24 namespace ActsExamples {
25 namespace Simulation {
26 
27 // input handling
30 
31  auto opt = desc.add_options();
32  // do not use the common input options: they have the wrong doc text and add a
33  // lot of unused options.
34  opt("input-dir", value<std::string>(),
35  "Read particle input from CSV files in the given directory. If not "
36  "given, particles are generated with the particle gun");
39 }
40 
43  ActsExamples::Sequencer& sequencer,
44  std::shared_ptr<const ActsExamples::RandomNumbers> randomNumbers) {
45  auto logLevel = Options::readLogLevel(vars);
46 
47  if (not vars["input-dir"].empty()) {
48  // read particle input from csv file
49  CsvParticleReader::Config readParticles;
50  readParticles.inputDir = vars["input-dir"].as<std::string>();
51  readParticles.inputStem = "particles";
52  readParticles.outputParticles = kParticlesInput;
53  sequencer.addReader(
54  std::make_shared<CsvParticleReader>(readParticles, logLevel));
55 
56  } else {
57  // generate particle input from a particle gun
59  gen.outputParticles = kParticlesInput;
60  gen.randomNumbers = std::move(randomNumbers);
61  sequencer.addReader(std::make_shared<EventGenerator>(gen, logLevel));
62  }
63 
64  // add additional particle selection
66  select.inputParticles = kParticlesInput;
67  select.outputParticles = kParticlesSelection;
68  sequencer.addAlgorithm(
69  std::make_shared<ActsExamples::ParticleSelector>(select, logLevel));
70 }
71 
72 // output handling
73 
74 // output options are just the common output options
76  ActsExamples::Sequencer& sequencer) {
77  auto logLevel = Options::readLogLevel(vars);
78  auto outputDir =
79  ensureWritableDirectory(vars["output-dir"].template as<std::string>());
80 
81  // Write simulation information as CSV files
82  if (vars["output-csv"].template as<bool>()) {
83  // NOTE: the collection names are an internal implementation detail but the
84  // output file stems are a public user interface. the former can be
85  // changed at-will, but the later should always be the same.
86 
87  // write simulated particle initial states
88  CsvParticleWriter::Config writeInitial;
89  writeInitial.inputParticles = kParticlesInitial;
90  writeInitial.outputDir = outputDir;
91  writeInitial.outputStem = "particles_initial";
92  sequencer.addWriter(
93  std::make_shared<CsvParticleWriter>(writeInitial, logLevel));
94 
95  // write simulated particle final states
96  CsvParticleWriter::Config writeFinal;
97  writeFinal.inputParticles = kParticlesFinal;
98  writeFinal.outputDir = outputDir;
99  writeFinal.outputStem = "particles_final";
100  sequencer.addWriter(
101  std::make_shared<CsvParticleWriter>(writeFinal, logLevel));
102 
103  // write simulated hits
104  CsvSimHitWriter::Config writeSimHits;
105  writeSimHits.inputSimHits = kSimHits;
106  writeSimHits.outputDir = outputDir;
107  writeSimHits.outputStem = "hits";
108  sequencer.addWriter(
109  std::make_shared<CsvSimHitWriter>(writeSimHits, logLevel));
110  }
111 
112  // Write simulation information as ROOT files
113  if (vars["output-root"].template as<bool>()) {
114  // NOTE: the collection names are an internal implementation detail but the
115  // output file names are a public user interface. the former can be
116  // changed at-will, but the later should always be the same.
117 
118  // write simulated particle initial states
119  RootParticleWriter::Config writeInitial;
120  writeInitial.inputParticles = kParticlesInitial;
121  writeInitial.filePath = joinPaths(outputDir, "particles_initial.root");
122  sequencer.addWriter(
123  std::make_shared<RootParticleWriter>(writeInitial, logLevel));
124 
125  // write simulated particle final states
126  RootParticleWriter::Config writeFinal;
127  writeFinal.inputParticles = kParticlesFinal;
128  writeFinal.filePath = joinPaths(outputDir, "particles_final.root");
129  sequencer.addWriter(
130  std::make_shared<RootParticleWriter>(writeFinal, logLevel));
131 
132  // write simulated hits
133  RootSimHitWriter::Config writeHits;
134  writeHits.inputSimHits = kSimHits;
135  writeHits.filePath = joinPaths(outputDir, "hits.root");
136  sequencer.addWriter(
137  std::make_shared<RootSimHitWriter>(writeHits, logLevel));
138  }
139 }
140 
141 } // namespace Simulation
142 } // namespace ActsExamples