Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DigitizationExample.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DigitizationExample.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020-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 
29 
30 #include <fstream>
31 #include <memory>
32 
33 #include <boost/program_options.hpp>
34 
35 #include "DigitizationInput.hpp"
36 
37 using namespace Acts::UnitLiterals;
38 using namespace ActsExamples;
39 
41  int argc, char* argv[],
42  const std::shared_ptr<ActsExamples::IBaseDetector>& detector) {
43  // Setup and parse options
44  auto desc = Options::makeDefaultOptions();
49  Options::addOutputOptions(desc, OutputFormat::Csv | OutputFormat::Root);
54 
55  auto opt = desc.add_options();
56  opt("digi-read-write-test", boost::program_options::bool_switch(),
57  "Test reading and writing roundtrip.");
58 
59  // Add specific options for this geometry
60  detector->addOptions(desc);
61  auto vm = Options::parse(desc, argc, argv);
62  if (vm.empty()) {
63  return EXIT_FAILURE;
64  }
66 
67  // Now read the standard options
69  auto outputDir = ensureWritableDirectory(vm["output-dir"].as<std::string>());
70 
71  // The geometry, material and decoration
72  auto geometry = Geometry::build(vm, *detector);
73  auto tGeometry = geometry.first;
74  auto contextDecorators = geometry.second;
75  auto randomNumbers =
76  std::make_shared<RandomNumbers>(Options::readRandomNumbersConfig(vm));
77 
78  // Add the decorator to the sequencer
79  for (const auto& cdr : contextDecorators) {
80  sequencer.addContextDecorator(cdr);
81  }
82 
83  // Setup the magnetic field
86 
87  // Read the sim hits
88  auto simHitReaderCfg = setupSimHitReading(vm, sequencer);
89  // Read the particles
90  auto particleReaderCfg = setupParticleReading(vm, sequencer);
91 
92  auto digiCfg = DigitizationConfig(
93  vm["digi-merge"].as<bool>(), vm["digi-merge-nsigma"].as<double>(),
94  vm["digi-merge-common-corner"].as<bool>(),
95  readDigiConfigFromJson(vm["digi-config-file"].as<std::string>()));
96  digiCfg.inputSimHits = simHitReaderCfg.outputSimHits;
97  digiCfg.trackingGeometry = tGeometry;
98  digiCfg.randomNumbers = randomNumbers;
99 
100  if (not vm["dump-digi-config"].as<std::string>().empty()) {
101  writeDigiConfigToJson(digiCfg.digitizationConfigs,
102  vm["dump-digi-config"].as<std::string>());
103  }
104 
105  std::vector<
106  std::pair<Acts::GeometryIdentifier, std::vector<Acts::BoundIndices>>>
107  bIndexInput;
108 
109  if (vm["digi-read-write-test"].as<bool>()) {
110  // Read measurements from CSV files
111  auto measReaderCfg = Options::readCsvMeasurementReaderConfig(vm);
112  measReaderCfg.inputDir = vm["input-dir"].as<std::string>();
113  measReaderCfg.outputMeasurements = digiCfg.outputMeasurements;
114  measReaderCfg.outputSourceLinks = digiCfg.outputSourceLinks;
115  measReaderCfg.outputClusters = digiCfg.outputClusters;
116  measReaderCfg.outputMeasurementSimHitsMap =
117  digiCfg.outputMeasurementSimHitsMap;
118  sequencer.addReader(
119  std::make_shared<CsvMeasurementReader>(measReaderCfg, logLevel));
120  } else {
121  sequencer.addAlgorithm(
122  std::make_shared<DigitizationAlgorithm>(digiCfg, logLevel));
123  }
124 
125  // Write digitization output as ROOT files
126  if (vm["output-root"].template as<bool>()) {
127  RootMeasurementWriter::Config measWriterRoot;
128  measWriterRoot.inputMeasurements = digiCfg.outputMeasurements;
129  measWriterRoot.inputClusters = digiCfg.outputClusters;
130  measWriterRoot.inputSimHits = simHitReaderCfg.outputSimHits;
131  measWriterRoot.inputMeasurementSimHitsMap =
132  digiCfg.outputMeasurementSimHitsMap;
133  measWriterRoot.filePath =
134  joinPaths(outputDir, std::string(digiCfg.outputMeasurements) + ".root");
135  measWriterRoot.boundIndices =
137  digiCfg.getBoundIndices());
138  measWriterRoot.trackingGeometry = tGeometry;
139  sequencer.addWriter(
140  std::make_shared<RootMeasurementWriter>(measWriterRoot, logLevel));
141  }
142 
143  // Write digitization out as CSV files
144  if (vm["output-csv"].template as<bool>()) {
145  CsvMeasurementWriter::Config measWriterCsv =
147  measWriterCsv.inputMeasurements = digiCfg.outputMeasurements;
148  measWriterCsv.inputClusters = digiCfg.outputClusters;
149  measWriterCsv.inputMeasurementSimHitsMap =
150  digiCfg.outputMeasurementSimHitsMap;
151  sequencer.addWriter(
152  std::make_shared<CsvMeasurementWriter>(measWriterCsv, logLevel));
153  }
154 
155  return sequencer.run();
156 }