Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:17:56

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023, 2024 Wouter Deconinck, Sylvester Joosten
0003 
0004 #pragma once
0005 
0006 #include <JANA/JApplicationFwd.h>
0007 #include <JANA/Services/JServiceLocator.h>
0008 #include <algorithms/geo.h>
0009 #include <algorithms/logger.h>
0010 #include <algorithms/random.h>
0011 #include <algorithms/service.h>
0012 #include <spdlog/common.h>
0013 #include <spdlog/logger.h>
0014 
0015 #include "algorithms/interfaces/ActsSvc.h"
0016 #include "algorithms/interfaces/UniqueIDGenSvc.h"
0017 #include "services/geometry/acts/ACTSGeo_service.h"
0018 #include "services/geometry/dd4hep/DD4hep_service.h"
0019 #include "services/log/Log_service.h"
0020 #include "services/particle/ParticleSvc.h"
0021 
0022 /**
0023  * The AlgorithmsInit_service centralizes use of ServiceSvc
0024  */
0025 class AlgorithmsInit_service : public JService {
0026 public:
0027   AlgorithmsInit_service(JApplication* /* app */) {};
0028   virtual ~AlgorithmsInit_service() {};
0029 
0030   void acquire_services(JServiceLocator* srv_locator) override {
0031     auto& serviceSvc = algorithms::ServiceSvc::instance();
0032 
0033     // Get services
0034     m_log_service     = srv_locator->get<Log_service>();
0035     m_dd4hep_service  = srv_locator->get<DD4hep_service>();
0036     m_actsgeo_service = srv_locator->get<ACTSGeo_service>();
0037 
0038     // Logger for ServiceSvc
0039     m_log = m_log_service->logger("AlgorithmsInit");
0040 
0041     // Register DD4hep_service as algorithms::GeoSvc
0042     [[maybe_unused]] auto& geoSvc = algorithms::GeoSvc::instance();
0043     serviceSvc.setInit<algorithms::GeoSvc>([this](auto&& g) {
0044       this->m_log->debug("Initializing algorithms::GeoSvc");
0045       g.init(const_cast<dd4hep::Detector*>(this->m_dd4hep_service->detector().get()));
0046     });
0047 
0048     // Register DD4hep_service as algorithms::ActsSvc
0049     [[maybe_unused]] auto& actsSvc = algorithms::ActsSvc::instance();
0050     serviceSvc.setInit<algorithms::ActsSvc>([this](auto&& g) {
0051       this->m_log->debug("Initializing algorithms::ActsSvc");
0052       try {
0053         g.init(this->m_actsgeo_service->actsGeoProvider());
0054       } catch (...) {
0055         g.init(std::move(std::current_exception()));
0056       }
0057     });
0058 
0059     // Register Log_service as algorithms::LogSvc
0060     const algorithms::LogLevel level{static_cast<algorithms::LogLevel>(m_log->level())};
0061     serviceSvc.setInit<algorithms::LogSvc>([this, level](auto&& logger) {
0062       this->m_log->debug("Initializing algorithms::LogSvc");
0063       logger.init(
0064           [this](const algorithms::LogLevel l, std::string_view caller, std::string_view msg) {
0065             static std::mutex m;
0066             std::lock_guard<std::mutex> lock(m);
0067             // storing the string_view is unsafe since it can become invalid
0068             static std::map<std::string, std::shared_ptr<spdlog::logger>> loggers;
0069             if (!loggers.contains(std::string(caller))) {
0070               this->m_log->debug("Initializing algorithms::LogSvc logger {}", caller);
0071               loggers[std::string(caller)] = this->m_log_service->logger(std::string(caller));
0072             }
0073             loggers[std::string(caller)]->log(static_cast<spdlog::level::level_enum>(l), msg);
0074           });
0075       logger.defaultLevel(level);
0076     });
0077 
0078     // Register a random service (JANA2 does not have one)
0079     [[maybe_unused]] auto& randomSvc = algorithms::RandomSvc::instance();
0080     serviceSvc.setInit<algorithms::RandomSvc>([this](auto&& r) {
0081       this->m_log->debug("Initializing algorithms::RandomSvc");
0082       r.setProperty("seed", static_cast<std::size_t>(1));
0083       r.init();
0084     });
0085 
0086     // Register a particle service
0087     [[maybe_unused]] auto& particleSvc = algorithms::ParticleSvc::instance();
0088     serviceSvc.add<algorithms::ParticleSvc>(&particleSvc);
0089 
0090     // Register a unique ID service
0091     [[maybe_unused]] auto& uniqueIDGenSvc = algorithms::UniqueIDGenSvc::instance();
0092     for (const auto& [key, prop] : uniqueIDGenSvc.getProperties()) {
0093       std::visit(
0094           [this, &uniqueIDGenSvc, key = key](auto&& val) {
0095             this->GetApplication()->SetDefaultParameter(std::string(key),
0096                                                         val); // FIXME add description
0097             uniqueIDGenSvc.setProperty(key, val);
0098           },
0099           prop.get());
0100     }
0101     serviceSvc.add<algorithms::UniqueIDGenSvc>(&uniqueIDGenSvc);
0102 
0103     // Finally, initialize the ServiceSvc
0104     serviceSvc.init();
0105   }
0106 
0107 private:
0108   AlgorithmsInit_service() = default;
0109   std::shared_ptr<Log_service> m_log_service;
0110   std::shared_ptr<DD4hep_service> m_dd4hep_service;
0111   std::shared_ptr<ACTSGeo_service> m_actsgeo_service;
0112   std::shared_ptr<spdlog::logger> m_log;
0113 };