ATLAS Offline Software
Loading...
Searching...
No Matches
VariableFactory.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "VariableFactory.h"
5#include <iostream>
6using json = nlohmann::json;
7
9
10 // for simple variables
11 if (cfg.is_string()) {
12 std::string name = cfg.get<std::string>();
13 return [acc = SG::ConstAccessor<float>(name)](const SG::AuxElement& el) -> float {
14 return acc(el);
15 };
16 }
17
18 // structured variables
19 if (cfg.is_object()) {
20 auto buildTerm = [](const json& terms) {
21 std::vector<std::pair<float, SG::ConstAccessor<float>>> accessors;
22
23 for (const auto& t : terms) {
24 std::string var = t[0];
25 float weight = t[1];
26 accessors.emplace_back(weight, SG::ConstAccessor<float>(var));
27 }
28
29 return [accessors](const SG::AuxElement& el) -> float {
30 float sum = 0.0f;
31 for (const auto& [weight, acc] : accessors) {
32 sum += weight * acc(el);
33 }
34 return sum;
35 };
36 };
37
38 auto num_func = buildTerm(cfg.at("numerator"));
39 auto den_func = buildTerm(cfg.at("denominator"));
40 std::string mode = cfg.contains("mode") ? cfg.at("mode").get<std::string>() : "log_ratio";
41
42 return [num_func, den_func, mode] (const SG::AuxElement& el) -> float {
43 float num = num_func(el);
44 float den = den_func(el);
45 if (mode == "log_ratio" ) {
46 // Smallest positive normal float; below this, reciprocals may overflow.
47 const float ep = std::numeric_limits<float>::min();
48 const float ratio = (std::abs(den) < ep ? std::numeric_limits<float>::infinity()
49 : num / den);
50 return (std::abs(ratio) < ep ? -std::numeric_limits<float>::infinity()
51 : std::log(ratio));
52 }
53 else {
54 throw std::runtime_error("Unknown variable mode: " + mode);
55 }
56 };
57 }
58
59 throw std::runtime_error("Invalid variable config");
60
61}
62
64 if (cfg.is_string()) {
65 std::string name = cfg.get<std::string>();
66 return [acc = SG::ConstAccessor<int>(name)](const SG::AuxElement& el) -> int {
67 return acc(el);
68 };
69 }
70 throw std::runtime_error("Invalid variable config");
71}
72
nlohmann::json json
Helper class to provide constant type-safe access to aux data.
static FloatFunc floatVariableFactory(const nlohmann::json &cfg)
std::function< float(const SG::AuxElement &)> FloatFunc
static IntFunc intVariableFactory(const nlohmann::json &cfg)
std::function< int(const SG::AuxElement &)> IntFunc
AuxElement(SG::AuxVectorData *container, size_t index)
Base class for elements of a container that can have aux data.