Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CommonMaterialMapping.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CommonMaterialMapping.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-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 
30 
31 #include <memory>
32 
33 #include <boost/program_options.hpp>
34 
35 namespace po = boost::program_options;
36 
37 int runMaterialMapping(int argc, char* argv[],
39  // Setup and parse options
48  ActsExamples::OutputFormat::Root |
49  ActsExamples::OutputFormat::Json |
50  ActsExamples::OutputFormat::Cbor);
51 
52  // Add specific options for this geometry
53  detector.addOptions(desc);
54  auto vm = ActsExamples::Options::parse(desc, argc, argv);
55  if (vm.empty()) {
56  return EXIT_FAILURE;
57  }
58 
59  ActsExamples::Sequencer sequencer(
61 
62  // Get the log level
64 
65  // The geometry, material and decoration
66  auto geometry = ActsExamples::Geometry::build(vm, detector);
67  auto tGeometry = geometry.first;
68  auto contextDecorators = geometry.second;
69  for (const auto& cdr : contextDecorators) {
70  sequencer.addContextDecorator(cdr);
71  }
72 
73  ActsExamples::WhiteBoard contextBoard(
74  Acts::getDefaultLogger("contextBoard", logLevel));
75 
76  // The geometry context
77  ActsExamples::AlgorithmContext context(0, 0, contextBoard);
78 
80  for (auto& cdr : contextDecorators) {
81  if (cdr->decorate(context) != ActsExamples::ProcessCode::SUCCESS) {
82  throw std::runtime_error("Failed to decorate event context");
83  }
84  }
85 
89 
90  // Straight line stepper
91  using SlStepper = Acts::StraightLineStepper;
93 
94  auto mapSurface = vm["mat-mapping-surfaces"].template as<bool>();
95  auto mapVolume = vm["mat-mapping-volumes"].template as<bool>();
96  auto volumeStep = vm["mat-mapping-volume-stepsize"].template as<float>();
97  if (!mapSurface && !mapVolume) {
98  return EXIT_FAILURE;
99  }
100  // ---------------------------------------------------------------------------------
101  // Input directory & input file handling
102  std::string intputDir = vm["input-dir"].template as<std::string>();
103  auto intputFiles = vm["input-files"].template as<std::vector<std::string>>();
105  vm["mat-mapping-read-surfaces"].template as<bool>();
106  if (vm["input-root"].template as<bool>()) {
107  // Read the material step information from a ROOT TTree
108  ActsExamples::RootMaterialTrackReader::Config matTrackReaderRootConfig;
109  matTrackReaderRootConfig.collection =
111  matTrackReaderRootConfig.fileList = intputFiles;
112  matTrackReaderRootConfig.readCachedSurfaceInformation =
114  auto matTrackReaderRoot =
115  std::make_shared<ActsExamples::RootMaterialTrackReader>(
116  matTrackReaderRootConfig, logLevel);
117  sequencer.addReader(matTrackReaderRoot);
118  }
119 
121  ActsExamples::MaterialMapping::Config mmAlgConfig{geoContext, mfContext};
122  mmAlgConfig.collection = ActsExamples::Simulation::kMaterialTracks;
123  if (mapSurface) {
124  // Get a Navigator
125  Acts::Navigator navigator({tGeometry, true, true, true});
126  // Make stepper and propagator
127  SlStepper stepper;
128  Propagator propagator(stepper, std::move(navigator));
131  auto smm = std::make_shared<Acts::SurfaceMaterialMapper>(
132  smmConfig, std::move(propagator),
133  Acts::getDefaultLogger("SurfaceMaterialMapper", logLevel));
134  mmAlgConfig.materialSurfaceMapper = smm;
135  }
136  if (mapVolume) {
137  // Get a Navigator
139  // Make stepper and propagator
140  SlStepper stepper;
141  Propagator propagator(stepper, std::move(navigator));
144  vmmConfig.mappingStep = volumeStep;
145  auto vmm = std::make_shared<Acts::VolumeMaterialMapper>(
146  vmmConfig, std::move(propagator),
147  Acts::getDefaultLogger("VolumeMaterialMapper", logLevel));
148  mmAlgConfig.materialVolumeMapper = vmm;
149  }
150  mmAlgConfig.trackingGeometry = tGeometry;
151 
152  // Get the file name from the options
153  std::string materialFileName = vm["mat-output-file"].as<std::string>();
154 
155  if (not materialFileName.empty() and vm["output-root"].template as<bool>()) {
156  // The writer of the indexed material
158  rmwConfig.filePath = materialFileName + ".root";
159  // Fulfill the IMaterialWriter interface
160 
161  auto rmw =
162  std::make_shared<ActsExamples::RootMaterialWriter>(rmwConfig, logLevel);
163  mmAlgConfig.materialWriters.push_back(rmw);
164 
165  if (mapSurface) {
166  // Write the propagation steps as ROOT TTree
167  ActsExamples::RootMaterialTrackWriter::Config matTrackWriterRootConfig;
168  matTrackWriterRootConfig.filePath = materialFileName + "_tracks.root";
169  matTrackWriterRootConfig.collection =
170  mmAlgConfig.mappingMaterialCollection;
171  matTrackWriterRootConfig.storeSurface = true;
172  matTrackWriterRootConfig.storeVolume = true;
173  auto matTrackWriterRoot =
174  std::make_shared<ActsExamples::RootMaterialTrackWriter>(
175  matTrackWriterRootConfig, logLevel);
176  sequencer.addWriter(matTrackWriterRoot);
177  }
178  }
179 
180  if (!materialFileName.empty() and (vm["output-json"].template as<bool>() or
181  vm["output-cbor"].template as<bool>())) {
183  std::string fileName = vm["mat-output-file"].template as<std::string>();
184  // the material writer
186  jmConverterCfg.processSensitives =
187  vm["mat-output-sensitives"].template as<bool>();
188  jmConverterCfg.processApproaches =
189  vm["mat-output-approaches"].template as<bool>();
190  jmConverterCfg.processRepresenting =
191  vm["mat-output-representing"].template as<bool>();
192  jmConverterCfg.processBoundaries =
193  vm["mat-output-boundaries"].template as<bool>();
194  jmConverterCfg.processVolumes =
195  vm["mat-output-volumes"].template as<bool>();
196  jmConverterCfg.context = geoContext;
197  // The writer
199  jmWriterCfg.converterCfg = std::move(jmConverterCfg);
200  jmWriterCfg.fileName = materialFileName;
201 
202  ActsExamples::JsonFormat format = ActsExamples::JsonFormat::NoOutput;
203  if (vm["output-json"].template as<bool>()) {
204  format = format | ActsExamples::JsonFormat::Json;
205  }
206  if (vm["output-cbor"].template as<bool>()) {
207  format = format | ActsExamples::JsonFormat::Cbor;
208  }
209  jmWriterCfg.writeFormat = format;
210 
211  auto jmw = std::make_shared<ActsExamples::JsonMaterialWriter>(
212  std::move(jmWriterCfg), Acts::Logging::INFO);
213 
214  mmAlgConfig.materialWriters.push_back(jmw);
215  }
216 
217  // Create the material mapping
218  auto mmAlg = std::make_shared<ActsExamples::MaterialMapping>(mmAlgConfig);
219 
220  // Append the Algorithm
221  sequencer.addAlgorithm(mmAlg);
222 
223  // Initiate the run
224  sequencer.run();
225  // Return success code
226  return 0;
227 }