Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 07:54:39

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/ParticleSvc.h"
0017 #include "services/log/Log_service.h"
0018 #include "services/geometry/acts/ACTSGeo_service.h"
0019 #include "services/geometry/dd4hep/DD4hep_service.h"
0020 
0021 /**
0022  * The AlgorithmsInit_service centralizes use of ServiceSvc
0023  */
0024 class AlgorithmsInit_service : public JService {
0025 public:
0026   AlgorithmsInit_service(JApplication* /* app */){};
0027   virtual ~AlgorithmsInit_service(){};
0028 
0029   void acquire_services(JServiceLocator* srv_locator) override {
0030     auto& serviceSvc = algorithms::ServiceSvc::instance();
0031 
0032     // Get services
0033     m_log_service     = srv_locator->get<Log_service>();
0034     m_dd4hep_service  = srv_locator->get<DD4hep_service>();
0035     m_actsgeo_service = srv_locator->get<ACTSGeo_service>();
0036 
0037     // Logger for ServiceSvc
0038     m_log = m_log_service->logger("AlgorithmsInit");
0039 
0040     // Register DD4hep_service as algorithms::GeoSvc
0041     [[maybe_unused]] auto& geoSvc = algorithms::GeoSvc::instance();
0042     serviceSvc.setInit<algorithms::GeoSvc>([this](auto&& g) {
0043       this->m_log->debug("Initializing algorithms::GeoSvc");
0044       g.init(const_cast<dd4hep::Detector*>(this->m_dd4hep_service->detector().get()));
0045     });
0046 
0047     // Register DD4hep_service as algorithms::ActsSvc
0048     [[maybe_unused]] auto& actsSvc = algorithms::ActsSvc::instance();
0049     serviceSvc.setInit<algorithms::ActsSvc>([this](auto&& g) {
0050       this->m_log->debug("Initializing algorithms::ActsSvc");
0051       try {
0052         g.init(this->m_actsgeo_service->actsGeoProvider());
0053       } catch (...) {
0054         g.init(std::move(std::current_exception()));
0055       }
0056     });
0057 
0058     // Register Log_service as algorithms::LogSvc
0059     const algorithms::LogLevel level{static_cast<algorithms::LogLevel>(m_log->level())};
0060     serviceSvc.setInit<algorithms::LogSvc>([this, level](auto&& logger) {
0061       this->m_log->debug("Initializing algorithms::LogSvc");
0062       logger.init(
0063           [this](const algorithms::LogLevel l, std::string_view caller, std::string_view msg) {
0064             static std::mutex m;
0065             std::lock_guard<std::mutex> lock(m);
0066             // storing the string_view is unsafe since it can become invalid
0067             static std::map<std::string, std::shared_ptr<spdlog::logger>> loggers;
0068             if (!loggers.contains(std::string(caller))) {
0069               this->m_log->debug("Initializing algorithms::LogSvc logger {}", caller);
0070               loggers[std::string(caller)] = this->m_log_service->logger(std::string(caller));
0071             }
0072             loggers[std::string(caller)]->log(static_cast<spdlog::level::level_enum>(l), msg);
0073           });
0074       logger.defaultLevel(level);
0075     });
0076 
0077     // Register a random service (JANA2 does not have one)
0078     [[maybe_unused]] auto& randomSvc = algorithms::RandomSvc::instance();
0079     serviceSvc.setInit<algorithms::RandomSvc>([this](auto&& r) {
0080       this->m_log->debug("Initializing algorithms::RandomSvc");
0081       r.setProperty("seed", static_cast<std::size_t>(1));
0082       r.init();
0083     });
0084 
0085     // Register a particle service
0086     [[maybe_unused]] auto& particleSvc = algorithms::ParticleSvc::instance();
0087 
0088     // Finally, initialize the ServiceSvc
0089     serviceSvc.init();
0090   }
0091 
0092 private:
0093   AlgorithmsInit_service() = default;
0094   std::shared_ptr<Log_service> m_log_service;
0095   std::shared_ptr<DD4hep_service> m_dd4hep_service;
0096   std::shared_ptr<ACTSGeo_service> m_actsgeo_service;
0097   std::shared_ptr<spdlog::logger> m_log;
0098 };