Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-27 07:03:06

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023, 2024 Wouter Deconinck, Sylvester Joosten
0003 
0004 #pragma once
0005 
0006 
0007 #include <JANA/JApplication.h>
0008 #include <JANA/Services/JServiceLocator.h>
0009 #include <algorithms/geo.h>
0010 #include <algorithms/logger.h>
0011 #include <algorithms/random.h>
0012 #include <algorithms/service.h>
0013 #include <spdlog/common.h>
0014 #include <spdlog/logger.h>
0015 
0016 #include "algorithms/interfaces/ParticleSvc.h"
0017 #include "services/log/Log_service.h"
0018 #include "services/geometry/dd4hep/DD4hep_service.h"
0019 
0020 /**
0021  * The AlgorithmsInit_service centralizes use of ServiceSvc
0022  */
0023 class AlgorithmsInit_service : public JService
0024 {
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 
0036         // Logger for ServiceSvc
0037         m_log = m_log_service->logger("AlgorithmsInit");
0038 
0039         // Register DD4hep_service as algorithms::GeoSvc
0040         [[maybe_unused]] auto& geoSvc = algorithms::GeoSvc::instance();
0041         serviceSvc.setInit<algorithms::GeoSvc>([this](auto&& g) {
0042             this->m_log->debug("Initializing algorithms::GeoSvc");
0043             g.init(const_cast<dd4hep::Detector*>(this->m_dd4hep_service->detector().get()));
0044         });
0045 
0046         // Register Log_service as algorithms::LogSvc
0047         const algorithms::LogLevel level{
0048             static_cast<algorithms::LogLevel>(m_log->level())};
0049         serviceSvc.setInit<algorithms::LogSvc>([this,level](auto&& logger) {
0050             this->m_log->debug("Initializing algorithms::LogSvc");
0051             logger.init([this](const algorithms::LogLevel l, std::string_view caller, std::string_view msg){
0052                 static std::mutex m;
0053                 std::lock_guard<std::mutex> lock(m);
0054                 // storing the string_view is unsafe since it can become invalid
0055                 static std::map<std::string, std::shared_ptr<spdlog::logger>> loggers;
0056                 if (! loggers.contains(std::string(caller))) {
0057                     this->m_log->debug("Initializing algorithms::LogSvc logger {}", caller);
0058                     loggers[std::string(caller)] = this->m_log_service->logger(std::string(caller));
0059                 }
0060                 loggers[std::string(caller)]->log(static_cast<spdlog::level::level_enum>(l), msg);
0061             });
0062             logger.defaultLevel(level);
0063         });
0064 
0065         // Register a random service (JANA2 does not have one)
0066         [[maybe_unused]] auto& randomSvc = algorithms::RandomSvc::instance();
0067         serviceSvc.setInit<algorithms::RandomSvc>([this](auto&& r) {
0068             this->m_log->debug("Initializing algorithms::RandomSvc");
0069             r.setProperty("seed", static_cast<size_t>(1));
0070             r.init();
0071         });
0072 
0073         // Register a particle service
0074         [[maybe_unused]] auto& particleSvc = algorithms::ParticleSvc::instance();
0075 
0076         // Finally, initialize the ServiceSvc
0077         serviceSvc.init();
0078     }
0079 
0080   private:
0081     AlgorithmsInit_service() = default;
0082     std::shared_ptr<Log_service> m_log_service;
0083     std::shared_ptr<DD4hep_service> m_dd4hep_service;
0084     std::shared_ptr<spdlog::logger> m_log;
0085 };