EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
AlgorithmsInit_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (C) 2023, 2024 Wouter Deconinck, Sylvester Joosten
3
4#pragma once
5
6#include <JANA/JApplicationFwd.h>
7#include <JANA/Services/JServiceLocator.h>
8#include <algorithms/geo.h>
9#include <algorithms/logger.h>
10#include <algorithms/random.h>
11#include <algorithms/service.h>
12#include <spdlog/common.h>
13#include <spdlog/logger.h>
14
21
25class AlgorithmsInit_service : public JService {
26public:
29
30 void acquire_services(JServiceLocator* srv_locator) override {
31 auto& serviceSvc = algorithms::ServiceSvc::instance();
32
33 // Get services
34 m_log_service = srv_locator->get<Log_service>();
35 m_dd4hep_service = srv_locator->get<DD4hep_service>();
36 m_actsgeo_service = srv_locator->get<ACTSGeo_service>();
37
38 // Logger for ServiceSvc
39 m_log = m_log_service->logger("AlgorithmsInit");
40
41 // Register DD4hep_service as algorithms::GeoSvc
42 [[maybe_unused]] auto& geoSvc = algorithms::GeoSvc::instance();
43 serviceSvc.setInit<algorithms::GeoSvc>([this](auto&& g) {
44 this->m_log->debug("Initializing algorithms::GeoSvc");
45 g.init(const_cast<dd4hep::Detector*>(this->m_dd4hep_service->detector().get()));
46 });
47
48 // Register DD4hep_service as algorithms::ActsSvc
49 [[maybe_unused]] auto& actsSvc = algorithms::ActsSvc::instance();
50 serviceSvc.setInit<algorithms::ActsSvc>([this](auto&& g) {
51 this->m_log->debug("Initializing algorithms::ActsSvc");
52 try {
53 g.init(this->m_actsgeo_service->actsGeoProvider());
54 } catch (...) {
55 g.init(std::move(std::current_exception()));
56 }
57 });
58
59 // Register Log_service as algorithms::LogSvc
60 const algorithms::LogLevel level{static_cast<algorithms::LogLevel>(m_log->level())};
61 serviceSvc.setInit<algorithms::LogSvc>([this, level](auto&& logger) {
62 this->m_log->debug("Initializing algorithms::LogSvc");
63 logger.init(
64 [this](const algorithms::LogLevel l, std::string_view caller, std::string_view msg) {
65 static std::mutex m;
66 std::lock_guard<std::mutex> lock(m);
67 // storing the string_view is unsafe since it can become invalid
68 static std::map<std::string, std::shared_ptr<spdlog::logger>> loggers;
69 if (!loggers.contains(std::string(caller))) {
70 this->m_log->debug("Initializing algorithms::LogSvc logger {}", caller);
71 loggers[std::string(caller)] = this->m_log_service->logger(std::string(caller));
72 }
73 loggers[std::string(caller)]->log(static_cast<spdlog::level::level_enum>(l), msg);
74 });
75 logger.defaultLevel(level);
76 });
77
78 // Register a random service (JANA2 does not have one)
79 [[maybe_unused]] auto& randomSvc = algorithms::RandomSvc::instance();
80 serviceSvc.setInit<algorithms::RandomSvc>([this](auto&& r) {
81 this->m_log->debug("Initializing algorithms::RandomSvc");
82 r.setProperty("seed", static_cast<std::size_t>(1));
83 r.init();
84 });
85
86 // Register a particle service
87 [[maybe_unused]] auto& particleSvc = algorithms::ParticleSvc::instance();
88 serviceSvc.add<algorithms::ParticleSvc>(&particleSvc);
89
90 // Register a unique ID service
91 [[maybe_unused]] auto& uniqueIDGenSvc = algorithms::UniqueIDGenSvc::instance();
92 for (const auto& [key, prop] : uniqueIDGenSvc.getProperties()) {
93 std::visit(
94 [this, &uniqueIDGenSvc, key = key](auto&& val) {
95 this->GetApplication()->SetDefaultParameter(std::string(key),
96 val); // FIXME add description
97 uniqueIDGenSvc.setProperty(key, val);
98 },
99 prop.get());
100 }
101 serviceSvc.add<algorithms::UniqueIDGenSvc>(&uniqueIDGenSvc);
102
103 // Finally, initialize the ServiceSvc
104 serviceSvc.init();
105 }
106
107private:
108 AlgorithmsInit_service() = default;
109 std::shared_ptr<Log_service> m_log_service;
110 std::shared_ptr<DD4hep_service> m_dd4hep_service;
111 std::shared_ptr<ACTSGeo_service> m_actsgeo_service;
112 std::shared_ptr<spdlog::logger> m_log;
113};
Definition ACTSGeo_service.h:15
Definition AlgorithmsInit_service.h:25
virtual ~AlgorithmsInit_service()
Definition AlgorithmsInit_service.h:28
void acquire_services(JServiceLocator *srv_locator) override
Definition AlgorithmsInit_service.h:30
AlgorithmsInit_service(JApplication *)
Definition AlgorithmsInit_service.h:27
Definition DD4hep_service.h:18
Definition Log_service.h:15
Definition ActsSvc.h:13
Definition ParticleSvc.h:13
Definition UniqueIDGenSvc.h:23
virtual void init()
Definition UniqueIDGenSvc.h:37