File indexing completed on 2024-09-27 07:03:48
0001
0002
0003
0004 #include <string>
0005
0006 #include <GaudiKernel/Service.h>
0007 #include <JugAlgo/IAlgoServiceSvc.h>
0008 #include <k4Interface/IGeoSvc.h>
0009
0010 #include <algorithms/geo.h>
0011 #include <algorithms/logger.h>
0012 #include <algorithms/random.h>
0013 #include <algorithms/service.h>
0014
0015
0016 class AlgoServiceSvc : public extends<Service, IAlgoServiceSvc> {
0017 public:
0018 AlgoServiceSvc(const std::string& name, ISvcLocator* svc) : base_class(name, svc) {}
0019 virtual ~AlgoServiceSvc() = default;
0020
0021 virtual StatusCode initialize() final;
0022 virtual StatusCode finalize() final { return StatusCode::SUCCESS; }
0023
0024 private:
0025 SmartIF<IGeoSvc> m_geoSvc;
0026 Gaudi::Property<size_t> m_randomSeed{this, "randomSeed", 1};
0027 };
0028
0029 DECLARE_COMPONENT(AlgoServiceSvc)
0030
0031
0032
0033 StatusCode AlgoServiceSvc::initialize() {
0034 StatusCode sc = Service::initialize();
0035 if (!sc.isSuccess()) {
0036 fatal() << "Error initializing AlgoServiceSvc" << endmsg;
0037 return sc;
0038 }
0039
0040 try {
0041 auto& serviceSvc = algorithms::ServiceSvc::instance();
0042 info() << "ServiceSvc declared " << serviceSvc.services().size() << " services" << endmsg;
0043
0044
0045 const algorithms::LogLevel level{
0046 static_cast<algorithms::LogLevel>(msgLevel() > 0 ? msgLevel() - 1 : 0)};
0047 info() << "Setting up algorithms::LogSvc with default level " << algorithms::logLevelName(level)
0048 << endmsg;
0049 serviceSvc.setInit<algorithms::LogSvc>([this,level](auto&& logger) {
0050 this->info() << "Initializing the algorithms::LogSvc using the Gaudi logger" << endmsg;
0051 logger.defaultLevel(level);
0052 logger.init(
0053 [this](const algorithms::LogLevel l, std::string_view caller, std::string_view msg) {
0054 const std::string text = fmt::format("[{}] {}", caller, msg);
0055 if (l == algorithms::LogLevel::kCritical) {
0056 this->fatal() << text << endmsg;
0057 } else if (l == algorithms::LogLevel::kError) {
0058 this->error() << text << endmsg;
0059 } else if (l == algorithms::LogLevel::kWarning) {
0060 this->warning() << text << endmsg;
0061 } else if (l == algorithms::LogLevel::kInfo) {
0062 this->info() << text << endmsg;
0063 } else if (l == algorithms::LogLevel::kDebug) {
0064 this->debug() << text << endmsg;
0065 } else if (l == algorithms::LogLevel::kTrace) {
0066 this->verbose() << text << endmsg;
0067 }
0068 });
0069 });
0070
0071 info() << "Setting up algorithms::GeoSvc" << endmsg;
0072 m_geoSvc = service("GeoSvc");
0073 if (!m_geoSvc) {
0074 error() << "Unable to locate Geometry Service. "
0075 << "Make sure you have GeoSvc in the right order in the configuration." << endmsg;
0076 return StatusCode::FAILURE;
0077 }
0078 serviceSvc.setInit<algorithms::GeoSvc>([this](auto&& g) {
0079 this->info() << "Initializing algorithms::RandomSvc with the Juggler GeoSvc" << endmsg;
0080 g.init(m_geoSvc->getDetector());
0081 });
0082
0083 info() << "Setting up algorithms::RandomSvc\n"
0084 << " --> using internal STL 64-bit MT engine\n"
0085 << " --> seed set to" << m_randomSeed << endmsg;
0086 serviceSvc.setInit<algorithms::RandomSvc>([this](auto&& r) {
0087 this->info() << "Initializing the algorithms::RandomSvc" << endmsg;
0088 r.setProperty("seed", m_randomSeed);
0089 r.init();
0090 });
0091 info() << "Initializing services" << endmsg;
0092
0093
0094
0095 updateMsgStreamOutputLevel(MSG::VERBOSE);
0096
0097
0098 serviceSvc.init();
0099
0100 } catch (const std::exception& e) {
0101 fatal() << e.what() << endmsg;
0102 return StatusCode::FAILURE;
0103 }
0104
0105 info() << "AlgoServiceSvc initialized successfully" << endmsg;
0106 return StatusCode::SUCCESS;
0107 }