ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryExampleBase.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryExampleBase.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 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 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
30 
31 int processGeometry(int argc, char* argv[], FW::IBaseDetector& detector) {
32  // setup and parse options
40 
41  // Add specific options for this geometry
42  detector.addOptions(desc);
43  auto vm = FW::Options::parse(desc, argc, argv);
44  if (vm.empty()) {
45  return EXIT_FAILURE;
46  }
47 
48  // Now read the standard options
49  auto logLevel = FW::Options::readLogLevel(vm);
51 
52  // The geometry, material and decoration
53  auto geometry = FW::Geometry::build(vm, detector);
54  auto tGeometry = geometry.first;
55  auto contextDecorators = geometry.second;
56 
57  // The detectors
58  read_strings subDetectors = vm["geo-subdetectors"].as<read_strings>();
59 
60  auto surfaceLogLevel =
61  Acts::Logging::Level(vm["geo-surface-loglevel"].as<size_t>());
62  // auto layerLogLevel =
63  // Acts::Logging::Level(vm["geo-layer-loglevel"].as<size_t>());
64  auto volumeLogLevel =
65  Acts::Logging::Level(vm["geo-volume-loglevel"].as<size_t>());
66 
67  for (size_t ievt = 0; ievt < nEvents; ++ievt) {
68  // Setup the event and algorithm context
69  FW::WhiteBoard eventStore(
70  Acts::getDefaultLogger("EventStore#" + std::to_string(ievt), logLevel));
71  size_t ialg = 0;
72 
73  // The geometry context
74  FW::AlgorithmContext context(ialg, ievt, eventStore);
75 
77  for (auto& cdr : contextDecorators) {
78  if (cdr->decorate(context) != FW::ProcessCode::SUCCESS)
79  throw std::runtime_error("Failed to decorate event context");
80  }
81 
82  std::string geoContextStr = "";
83  if (contextDecorators.size() > 0) {
84  // We need indeed a context object
85  if (nEvents > 1) {
86  geoContextStr = "_geoContext" + std::to_string(ievt);
87  }
88  }
89 
90  // ---------------------------------------------------------------------------------
91  // Output directory
92  std::string outputDir = vm["output-dir"].template as<std::string>();
93 
94  // OBJ output
95  if (vm["output-obj"].as<bool>()) {
96  // The writers
97  std::vector<std::shared_ptr<FW::Obj::ObjSurfaceWriter>> subWriters;
98  std::vector<std::shared_ptr<std::ofstream>> subStreams;
99  // Loop and create the obj output writers per defined sub detector
100  for (auto sdet : subDetectors) {
101  // Sub detector stream
102  auto sdStream = std::shared_ptr<std::ofstream>(new std::ofstream);
103  std::string sdOutputName =
104  FW::joinPaths(outputDir, sdet + geoContextStr + ".obj");
105  sdStream->open(sdOutputName);
106  // Object surface writers
107  FW::Obj::ObjSurfaceWriter::Config sdObjWriterConfig =
108  FW::Options::readObjSurfaceWriterConfig(vm, sdet, surfaceLogLevel);
109  sdObjWriterConfig.outputStream = sdStream;
110  // Let's not write the layer surface when we have misalignment
111  if (contextDecorators.size() > 0) {
112  sdObjWriterConfig.outputLayerSurface = false;
113  }
114  auto sdObjWriter =
115  std::make_shared<FW::Obj::ObjSurfaceWriter>(sdObjWriterConfig);
116  // Push back
117  subWriters.push_back(sdObjWriter);
118  subStreams.push_back(sdStream);
119  }
120 
121  // Configure the tracking geometry writer
122  auto tgObjWriterConfig = FW::Options::readObjTrackingGeometryWriterConfig(
123  vm, "ObjTrackingGeometryWriter", volumeLogLevel);
124 
125  tgObjWriterConfig.surfaceWriters = subWriters;
126  auto tgObjWriter = std::make_shared<FW::Obj::ObjTrackingGeometryWriter>(
127  tgObjWriterConfig);
128 
129  // Write the tracking geometry object
130  tgObjWriter->write(context, *tGeometry);
131 
132  // Close the output streams
133  for (auto sStreams : subStreams) {
134  sStreams->close();
135  }
136  }
137 
138  // CSV output
139  if (vm["output-csv"].as<bool>()) {
140  // setup the tracking geometry writer
141  FW::CsvTrackingGeometryWriter::Config tgCsvWriterConfig;
142  tgCsvWriterConfig.trackingGeometry = tGeometry;
143  tgCsvWriterConfig.outputDir = outputDir;
144  tgCsvWriterConfig.writePerEvent = true;
145  auto tgCsvWriter = std::make_shared<FW::CsvTrackingGeometryWriter>(
146  tgCsvWriterConfig, logLevel);
147 
148  // Write the tracking geometry object
149  tgCsvWriter->write(context);
150  }
151 
152  // Get the file name from the options
153  std::string materialFileName = vm["mat-output-file"].as<std::string>();
154 
155  if (!materialFileName.empty() and vm["output-root"].template as<bool>()) {
156  // The writer of the indexed material
157  FW::RootMaterialWriter::Config rmwConfig("MaterialWriter");
158  rmwConfig.fileName = materialFileName + ".root";
159  FW::RootMaterialWriter rmwImpl(rmwConfig);
160  rmwImpl.write(*tGeometry);
161  }
162 
163  if (!materialFileName.empty() and vm["output-json"].template as<bool>()) {
165  std::string fileName = vm["mat-output-file"].template as<std::string>();
166  // the material writer
168  "JsonGeometryConverter", Acts::Logging::INFO);
169  jmConverterCfg.processSensitives =
170  vm["mat-output-sensitives"].template as<bool>();
171  jmConverterCfg.processApproaches =
172  vm["mat-output-approaches"].template as<bool>();
173  jmConverterCfg.processRepresenting =
174  vm["mat-output-representing"].template as<bool>();
175  jmConverterCfg.processBoundaries =
176  vm["mat-output-boundaries"].template as<bool>();
177  jmConverterCfg.processVolumes =
178  vm["mat-output-volumes"].template as<bool>();
179  jmConverterCfg.writeData = vm["mat-output-data"].template as<bool>();
180  jmConverterCfg.processnonmaterial =
181  vm["mat-output-allsurfaces"].template as<bool>();
182  // The writer
183  FW::Json::JsonMaterialWriter jmwImpl(std::move(jmConverterCfg),
184  materialFileName + ".json");
185 
186  jmwImpl.write(*tGeometry);
187  }
188  }
189 
190  return 0;
191 }