File indexing completed on 2024-09-27 07:03:06
0001
0002
0003
0004 #include <TInterpreter.h>
0005 #include <TInterpreterValue.h>
0006 #include <algorithm>
0007 #include <fmt/core.h>
0008 #include <memory>
0009 #include <sstream>
0010
0011 #include "EvaluatorSvc.h"
0012
0013 namespace eicrecon {
0014
0015 void EvaluatorSvc::init() {
0016
0017
0018 level(algorithms::LogLevel::kTrace);
0019 }
0020
0021 std::function<double(const std::unordered_map<std::string, double>&)>
0022 EvaluatorSvc::_compile(const std::string& expr, std::vector<std::string> params) {
0023 std::lock_guard<std::mutex> guard(m_interpreter_mutex);
0024
0025 std::string func_name = fmt::format("_eicrecon_{}", m_function_id++);
0026 std::ostringstream sstr;
0027 sstr << "double " << func_name << "(double params[]){";
0028 for (unsigned int param_ix = 0; const auto& p : params) {
0029 sstr << "double " << p << " = params[" << (param_ix++) << "];";
0030 }
0031 sstr << "return " << expr << ";";
0032 sstr << "}";
0033
0034 TInterpreter* interp = TInterpreter::Instance();
0035 debug("Compiling {}", sstr.str());
0036 interp->ProcessLine(sstr.str().c_str());
0037 std::unique_ptr<TInterpreterValue> func_val{gInterpreter->MakeInterpreterValue()};
0038 interp->Evaluate(func_name.c_str(), *func_val);
0039 typedef double (*func_t)(double params[]);
0040 func_t func = ((func_t)(func_val->GetAsPointer()));
0041
0042 return [params, func](const std::unordered_map<std::string, double>& param_values) {
0043 std::vector<double> value_list;
0044 value_list.reserve(params.size());
0045 for (const auto& p : params) {
0046 value_list.push_back(param_values.at(p));
0047 }
0048 return func(value_list.data());
0049 };
0050 }
0051
0052 }