diff --git a/Algorithms/MaterialMapping/include/ACTFW/MaterialMapping/GeantinoRecording.hpp b/Algorithms/MaterialMapping/include/ACTFW/MaterialMapping/GeantinoRecording.hpp index 503fe34bec49e05290e6974e4fddf331cf392240..63e03d6258107cfba3fbf4993acbbaf35fb454fc 100644 --- a/Algorithms/MaterialMapping/include/ACTFW/MaterialMapping/GeantinoRecording.hpp +++ b/Algorithms/MaterialMapping/include/ACTFW/MaterialMapping/GeantinoRecording.hpp @@ -49,9 +49,8 @@ public: /// @class Config struct Config { - /// The writer writing out the MaterialTrack entities - std::shared_ptr<FW::IWriterT<Acts::RecordedMaterialTrack>> - materialTrackWriter = nullptr; + std::string geantMaterialCollection = "geant-material-tracks"; + /// The service possibly providing the Geant4 geometry (optional) /// @note If this is not set, the geometry should be given by gdml file std::shared_ptr<FW::IGeant4Service> geant4Service = nullptr; @@ -60,6 +59,7 @@ public: std::string gdmlFile; /// The number of tracks per event size_t tracksPerEvent = 0; + /// random number seed 1 int seed1 = 12345; /// random number seed 2 diff --git a/Algorithms/MaterialMapping/src/GeantinoRecording.cpp b/Algorithms/MaterialMapping/src/GeantinoRecording.cpp index e01793cda1e45a60b1690d04a8ef77a3eaf5c7db..1872b2e6f69c0dcaded1a7c47e5d073cc0d3a29f 100644 --- a/Algorithms/MaterialMapping/src/GeantinoRecording.cpp +++ b/Algorithms/MaterialMapping/src/GeantinoRecording.cpp @@ -9,6 +9,7 @@ #include "ACTFW/MaterialMapping/GeantinoRecording.hpp" #include <iostream> #include <stdexcept> +#include "ACTFW/Framework/WhiteBoard.hpp" #include "ACTFW/Plugins/Geant4/MMDetectorConstruction.hpp" #include "ACTFW/Plugins/Geant4/MMEventAction.hpp" #include "ACTFW/Plugins/Geant4/MMPrimaryGeneratorAction.hpp" @@ -23,11 +24,6 @@ FW::GeantinoRecording::GeantinoRecording( , m_cfg(cnf) , m_runManager(std::make_unique<G4RunManager>()) { - /// Make sure that a writer was provided in the configuration - if (!m_cfg.materialTrackWriter) { - throw std::invalid_argument("Missing material track writer"); - } - /// Check if the geometry should be accessed over the geant4 service if (m_cfg.geant4Service) { m_runManager->SetUserInitialization(m_cfg.geant4Service->geant4Geometry()); @@ -63,12 +59,16 @@ FW::GeantinoRecording::execute(const FW::AlgorithmContext& context) const // Begin with the simulation m_runManager->BeamOn(m_cfg.tracksPerEvent); // Retrieve the track material tracks from Geant4 - auto mtrecords = FW::Geant4::MMEventAction::Instance()->MaterialTracks(); - ACTS_INFO("Received " << mtrecords.size() + auto recordedMaterial + = FW::Geant4::MMEventAction::Instance()->MaterialTracks(); + ACTS_INFO("Received " << recordedMaterial.size() << " MaterialTracks. Writing them now onto file..."); - // write to the file - for (auto& record : mtrecords) { - m_cfg.materialTrackWriter->write(context, record); + + // Write the recorded material to the event store + if (context.eventStore.add(m_cfg.geantMaterialCollection, + std::move(recordedMaterial)) + == FW::ProcessCode::ABORT) { + return FW::ProcessCode::ABORT; } return FW::ProcessCode::SUCCESS; diff --git a/Core/include/ACTFW/Random/RandomNumbersSvc.hpp b/Core/include/ACTFW/Random/RandomNumbersSvc.hpp index d199914dba9bc75ae04d6933c1679680ea907da2..2550327f7f07f6228485df5eb8511e8385db6ce9 100644 --- a/Core/include/ACTFW/Random/RandomNumbersSvc.hpp +++ b/Core/include/ACTFW/Random/RandomNumbersSvc.hpp @@ -74,7 +74,7 @@ public: RandomEngine spawnGenerator(const AlgorithmContext& context) const; - const unsigned int + unsigned int generateSeed(const AlgorithmContext& context) const; /// Ask for the seed diff --git a/Core/src/Random/RandomNumbersSvc.cpp b/Core/src/Random/RandomNumbersSvc.cpp index d1b812d469808d39009661459c8da499e88a6ce3..d528eb3612509bbb5b82b764b8d9240755199bdc 100644 --- a/Core/src/Random/RandomNumbersSvc.cpp +++ b/Core/src/Random/RandomNumbersSvc.cpp @@ -35,7 +35,7 @@ FW::RandomNumbersSvc::spawnGenerator(const AlgorithmContext& context) const return RandomEngine(generateSeed(context)); } -const unsigned int +unsigned int FW::RandomNumbersSvc::generateSeed(const AlgorithmContext& context) const { // use Cantor pairing function to generate a unique generator id from diff --git a/Detectors/GeometryInterfaces/include/ACTFW/GeometryInterfaces/ITrackingGeometryService.hpp b/Detectors/GeometryInterfaces/include/ACTFW/GeometryInterfaces/ITrackingGeometryService.hpp index ab6735c66a4e892a18135628209f9cf47a1ce9a3..73e4223a93e60ac5433dd74dbb132119f265b414 100644 --- a/Detectors/GeometryInterfaces/include/ACTFW/GeometryInterfaces/ITrackingGeometryService.hpp +++ b/Detectors/GeometryInterfaces/include/ACTFW/GeometryInterfaces/ITrackingGeometryService.hpp @@ -10,7 +10,7 @@ // ITrackingGeometryService.hpp /////////////////////////////////////////////////////////////////// -#pragma +#pragma once #include <memory> #include "ACTFW/Framework/IService.hpp" diff --git a/Examples/MaterialMapping/CMakeLists.txt b/Examples/MaterialMapping/CMakeLists.txt index c5ef9e6858a0e9398b71fec076265f688b5def50..6455f8e3488bdfbdabd2ef763f8ba8147f2d146c 100644 --- a/Examples/MaterialMapping/CMakeLists.txt +++ b/Examples/MaterialMapping/CMakeLists.txt @@ -19,7 +19,7 @@ target_link_libraries(ACTFWGenericMaterialMappingExample PRIVATE ${Boost_LIBRARI install(TARGETS ACTFWGenericMaterialMappingExample RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -if (USE_GEANT4 AND USE+DD4HEP) +if (USE_GEANT4 AND USE_DD4HEP) include(${Geant4_USE_FILE}) add_executable(ACTFWGeantinoRecordingExample src/GeantinoRecordingExample.cpp) diff --git a/Examples/MaterialMapping/src/GeantinoRecordingExample.cpp b/Examples/MaterialMapping/src/GeantinoRecordingExample.cpp index 34e4571730d95d29264a0cc425c7a9906e1f239a..fc8abdbf5a54157dd4a3bc4b9ace4b96ce2e08e1 100644 --- a/Examples/MaterialMapping/src/GeantinoRecordingExample.cpp +++ b/Examples/MaterialMapping/src/GeantinoRecordingExample.cpp @@ -9,14 +9,16 @@ #include <boost/program_options.hpp> #include "ACTFW/Common/CommonOptions.hpp" #include "ACTFW/DD4hepDetector/DD4hepDetectorOptions.hpp" +#include "ACTFW/DD4hepDetector/DD4hepGeometryService.hpp" #include "ACTFW/Framework/Sequencer.hpp" #include "ACTFW/MaterialMapping/GeantinoRecording.hpp" -#include "ACTFW/Plugins/DD4hep/GeometryService.hpp" #include "ACTFW/Plugins/DD4hepG4/DD4hepToG4Svc.hpp" #include "ACTFW/Plugins/Root/RootMaterialTrackWriter.hpp" #include "ACTFW/Random/RandomNumbersSvc.hpp" +#include "ACTFW/Utilities/Paths.hpp" #include "ACTFW/Writers/IWriterT.hpp" #include "Acts/Detector/TrackingGeometry.hpp" +#include "Acts/Utilities/GeometryContext.hpp" namespace po = boost::program_options; @@ -25,8 +27,8 @@ main(int argc, char* argv[]) { // Declare the supported program options. po::options_description desc("Allowed options"); - // add the standard options - FW::Options::addStandardOptions<po::options_description>(desc, 100, 2); + // Add the standard/common options + FW::Options::addCommonOptions<po::options_description>(desc); // add the detector options FW::Options::addDD4hepOptions<po::options_description>(desc); po::variables_map vm; @@ -38,25 +40,25 @@ main(int argc, char* argv[]) std::cout << desc << std::endl; return 1; } - // now read the standard options - auto standardOptions - = FW::Options::readStandardOptions<po::variables_map>(vm); - // @todo update - make program options in separate MR - auto nEvents = standardOptions.first; + // Now read the standard options + auto nEvents = FW::Options::readNumberOfEvents<po::variables_map>(vm); + size_t nTracks = 100; int randomSeed1 = 536235167; int randomSeed2 = 729237523; + Acts::GeometryContext geoContext; + // DETECTOR: // -------------------------------------------------------------------------------- // DD4Hep detector definition // read the detector config & dd4hep detector auto dd4HepDetectorConfig = FW::Options::readDD4hepConfig<po::variables_map>(vm); - auto geometrySvc - = std::make_shared<FW::DD4hep::GeometryService>(dd4HepDetectorConfig); + auto geometrySvc = std::make_shared<FW::DD4hep::DD4hepGeometryService>( + dd4HepDetectorConfig); std::shared_ptr<const Acts::TrackingGeometry> tGeometry - = geometrySvc->trackingGeometry(); + = geometrySvc->trackingGeometry(geoContext); // DD4Hep to Geant4 conversion // @@ -69,20 +71,15 @@ main(int argc, char* argv[]) // Geant4 JOB: // -------------------------------------------------------------------------------- // set up the writer for - FW::Root::RootMaterialTrackWriter::Config g4WriterConfig( - "MaterialTrackWriter", Acts::Logging::INFO); - g4WriterConfig.fileName = "GeantMaterialTracks.root"; - g4WriterConfig.treeName = "GeantMaterialTracks"; - auto g4TrackRecWriter - = std::make_shared<FW::Root::RootMaterialTrackWriter>(g4WriterConfig); + + // --------------------------------------------------------------------------------- // set up the algorithm writing out the material map FW::GeantinoRecording::Config g4rConfig; - g4rConfig.materialTrackWriter = g4TrackRecWriter; - g4rConfig.geant4Service = dd4hepToG4Svc; - g4rConfig.tracksPerEvent = nTracks; - g4rConfig.seed1 = randomSeed1; - g4rConfig.seed2 = randomSeed2; + g4rConfig.geant4Service = dd4hepToG4Svc; + g4rConfig.tracksPerEvent = nTracks; + g4rConfig.seed1 = randomSeed1; + g4rConfig.seed2 = randomSeed2; // create the geant4 algorithm auto g4rAlgorithm = std::make_shared<FW::GeantinoRecording>(g4rConfig, Acts::Logging::INFO); @@ -93,8 +90,26 @@ main(int argc, char* argv[]) FW::Sequencer::Config g4SeqConfig; // now create the sequencer FW::Sequencer g4Sequencer(g4SeqConfig); - // the writer is a service as it needs initialize, finalize - g4Sequencer.addService(g4TrackRecWriter); - g4Sequencer.addAlgorithm(g4rAlgorithm); - return g4Sequencer.run(nEvents); + + // Output directory + std::string outputDir = vm["output-dir"].template as<std::string>(); + auto matCollection = vm["prop-material-collection"].as<std::string>(); + + if (vm["output-root"].template as<bool>()) { + // Write the propagation steps as ROOT TTree + FW::Root::RootMaterialTrackWriter::Config matTrackWriterRootConfig; + matTrackWriterRootConfig.collection = matCollection; + matTrackWriterRootConfig.filePath + = FW::joinPaths(outputDir, matCollection + ".root"); + auto matTrackWriterRoot + = std::make_shared<FW::Root::RootMaterialTrackWriter>( + matTrackWriterRootConfig); + if (g4Sequencer.addWriters({matTrackWriterRoot}) + != FW::ProcessCode::SUCCESS) + return -1; + } + + // Append the algorithm and run + g4Sequencer.appendEventAlgorithms({g4rAlgorithm}); + g4Sequencer.run(nEvents); } diff --git a/Plugins/Json/include/ACTFW/Plugins/Json/JsonGeometryConverter.hpp b/Plugins/Json/include/ACTFW/Plugins/Json/JsonGeometryConverter.hpp index 06a6453c19f607cb8c520c8062140f747129ce35..72a947926e328bcd2173ef0e9e197bad259c829e 100644 --- a/Plugins/Json/include/ACTFW/Plugins/Json/JsonGeometryConverter.hpp +++ b/Plugins/Json/include/ACTFW/Plugins/Json/JsonGeometryConverter.hpp @@ -65,6 +65,9 @@ namespace Json { // the geometry id Acts::GeometryID volumeID; + /// the namne + std::string volumeName; + std::map<geo_id_value, LayerRep> layers; SurfaceMaterialRep boundaries; const Acts::IVolumeMaterial* material = nullptr; @@ -102,6 +105,8 @@ namespace Json { std::string detkey = "detector"; /// The volume identification string std::string volkey = "volumes"; + /// The name identification + std::string namekey = "name"; /// The boundary surface string std::string boukey = "boundaries"; /// The layer identification string diff --git a/Plugins/Json/src/JsonGeometryConverter.cpp b/Plugins/Json/src/JsonGeometryConverter.cpp index 0dedb15657f6aa81fbb594d126ee33010da6f763..4972cfcb716099b1bda9c1667bc0b1abae05a048 100644 --- a/Plugins/Json/src/JsonGeometryConverter.cpp +++ b/Plugins/Json/src/JsonGeometryConverter.cpp @@ -208,6 +208,7 @@ FW::Json::JsonGeometryConverter::detectorRepToJson(const DetectorRep& detRep) for (auto & [ key, value ] : detRep.volumes) { json volj; ACTS_VERBOSE("a2j: -> Writing Volume: " << key); + volj[m_cfg.namekey] = value.volumeName; // write the layers if (not value.layers.empty()) { ACTS_VERBOSE("a2j: ---> Found " << value.layers.size() << " layer(s) "); @@ -350,7 +351,9 @@ FW::Json::JsonGeometryConverter::convertToRep( // Write if it's good if (volRep) { Acts::GeometryID volumeID = tVolume.geoID(); - geo_id_value vid = volumeID.value(Acts::GeometryID::volume_mask); + volRep.volumeName = tVolume.volumeName(); + volRep.volumeID = volumeID; + geo_id_value vid = volumeID.value(Acts::GeometryID::volume_mask); detRep.volumes.insert({vid, std::move(volRep)}); } return; @@ -445,7 +448,7 @@ FW::Json::JsonGeometryConverter::surfaceMaterialToJson( if (bsMaterial != nullptr) { // type is binned smj[m_cfg.typekey] = "binned"; - bUtility = &(psMaterial->binUtility()); + bUtility = &(bsMaterial->binUtility()); // convert the data // get the material matrix if (m_cfg.writeData) { @@ -458,6 +461,7 @@ FW::Json::JsonGeometryConverter::surfaceMaterialToJson( for (auto& mp : mpVector) { mvec.push_back(convertMaterialProperties(mp)); } + mmat.push_back(std::move(mvec)); } smj[m_cfg.datakey] = mmat; } diff --git a/Plugins/Json/src/JsonMaterialWriter.cpp b/Plugins/Json/src/JsonMaterialWriter.cpp index 3bbb7a523dd81d0cc5b9bef89f1f12f4dad7e49d..df259b28985ef8b59f8ff4adf71cbea63fca854c 100644 --- a/Plugins/Json/src/JsonMaterialWriter.cpp +++ b/Plugins/Json/src/JsonMaterialWriter.cpp @@ -38,7 +38,7 @@ FW::Json::JsonMaterialWriter::write( auto jout = jmConverter.materialMapsToJson(detMaterial); // write prettified JSON to another file - std::string jsonOutputName = m_cfg.fileName + ".json"; + std::string jsonOutputName = m_cfg.fileName; std::ofstream ofj(jsonOutputName); ofj << std::setw(4) << jout << std::endl; diff --git a/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialReader.hpp b/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialReader.hpp index 1e1b709265ad2b350b67fb68802811630f0c55c5..9a168519e866b466623e8c8e883464d6a5537566 100644 --- a/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialReader.hpp +++ b/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialReader.hpp @@ -12,11 +12,11 @@ #include <mutex> #include "ACTFW/Framework/ProcessCode.hpp" #include "ACTFW/Readers/IReaderT.hpp" +#include "Acts/Detector/TrackingVolume.hpp" #include "Acts/Material/IMaterialDecorator.hpp" #include "Acts/Material/ISurfaceMaterial.hpp" #include "Acts/Material/IVolumeMaterial.hpp" #include "Acts/Surfaces/Surface.hpp" -#include "Acts/Detector/TrackingVolume.hpp" #include "Acts/Utilities/Definitions.hpp" #include "Acts/Utilities/GeometryID.hpp" #include "Acts/Utilities/Logger.hpp" @@ -32,7 +32,6 @@ using VolumeMaterialMap = std::map<GeometryID, std::shared_ptr<const IVolumeMaterial>>; using DetectorMaterialMaps = std::pair<SurfaceMaterialMap, VolumeMaterialMap>; - } namespace FW { @@ -115,7 +114,7 @@ namespace Root { /// Read method /// - /// @param detMaterialMap the surface and volume material + /// @param detMaterialMap the surface and volume material /// @param skip is the number of skip reads (0 for this reader) /// @param is the AlgorithmContext pointer in case the reader would need /// information about the event context (not true in this case) @@ -188,8 +187,8 @@ namespace Root { } private: - RootMaterialReader::Config m_readerConfig; - Acts::DetectorMaterialMaps m_detectorMaterialMaps; + RootMaterialReader::Config m_readerConfig; + Acts::DetectorMaterialMaps m_detectorMaterialMaps; }; } // namespace Root