EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
EvaluatorSvc.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (C) 2024 Dmitry Kalinkin
3
4#pragma once
5
6#include <algorithms/logger.h>
7#include <cstddef>
8#include <exception>
9#include <functional>
10#include <mutex>
11#include <stdexcept>
12#include <string>
13#include <unordered_map>
14#include <utility>
15#include <vector>
16
17namespace eicrecon {
18
31class EvaluatorSvc : public algorithms::LoggedService<EvaluatorSvc> {
32public:
33 void init();
34
44 template <class... Args>
45 std::function<double(Args...)>
46 compile(const std::string& expr,
47 std::function<std::unordered_map<std::string, double>(Args...)> transform) {
48 try {
49 // trivial function if string is representation of float
50 std::size_t pos;
51 float expr_value = std::stof(expr, &pos);
52 if (pos < expr.size()) {
53 throw std::invalid_argument("unparsed trailing characters");
54 }
55 return [expr_value](Args... /* args */) { return expr_value; };
56 } catch (std::exception& e) {
57 // more complicated function otherwise
58 std::vector<std::string> params;
59 // Call transform with default values to detect parameter names
60 for (auto& p : transform(Args{}...)) {
61 params.push_back(p.first);
62 }
63 auto compiled_expr = _compile(expr, params);
64 return [compiled_expr, transform](Args... args) {
65 return compiled_expr(transform(std::forward<Args>(args)...));
66 };
67 }
68 };
69
78 std::function<double(const std::unordered_map<std::string, double>&)>
79 _compile(const std::string& expr, const std::vector<std::string>& params);
80
81private:
82 unsigned int m_function_id = 0;
83 std::mutex m_interpreter_mutex;
84
85 ALGORITHMS_DEFINE_LOGGED_SERVICE(EvaluatorSvc);
86};
87
88} // namespace eicrecon
Provides an interface to a compiler that converts string expressions to native std::function.
Definition EvaluatorSvc.h:31
std::function< double(const std::unordered_map< std::string, double > &)> _compile(const std::string &expr, const std::vector< std::string > &params)
Compile expression expr to std::function.
Definition EvaluatorSvc.cc:21
std::function< double(Args...)> compile(const std::string &expr, std::function< std::unordered_map< std::string, double >(Args...)> transform)
Compile expression expr to std::function.
Definition EvaluatorSvc.h:46
void init()
Definition EvaluatorSvc.cc:14
-client
Definition CalorimeterClusterRecoCoG.cc:37