3 #include <cp3_llbb/Framework/interface/Histogram.h>
6 #include <unordered_map>
17 template <
typename T,
typename U>
19 typedef std::unordered_map<T, U> left_type;
20 typedef std::unordered_map<U, T> right_type;
25 bimap(std::initializer_list<typename left_type::value_type> l) {
28 right.insert(
typename right_type::value_type(v.second, v.first));
37 left = std::move(rhs.left);
38 right = std::move(rhs.right);
45 enum class BinningVariable {
55 struct hash<BinningVariable> {
56 typedef BinningVariable argument_type;
57 typedef std::size_t result_type;
59 hash<uint8_t> int_hash;
61 result_type operator()(argument_type
const& s)
const {
62 return int_hash(
static_cast<uint8_t
>(s));
69 typedef std::unordered_map<BinningVariable, float> value_type;
73 Parameters(std::initializer_list<typename value_type::value_type> init);
79 Parameters& set(
const BinningVariable& bin,
float value);
80 Parameters& set(
const typename value_type::value_type& value);
82 std::vector<float> toArray(
const std::vector<BinningVariable>&)
const;
113 template <
typename _Value>
115 std::size_t bin = h.findClosestBin(bins, &outOfRange);
117 std::stringstream msg;
118 msg <<
"Failed to found the right bin for a scale-factor. This should not happend. Bins: [";
119 for (
float b: bins) {
123 msg.seekp(msg.tellp() - 2l);
126 throw std::runtime_error(msg.str());
129 return {h.getBinContent(bin), h.getBinErrorLow(bin), h.getBinErrorHigh(bin)};
132 void setVariables(
const std::vector<std::string>&);
135 std::vector<BinningVariable> binning_variables;
137 bool use_formula =
false;
140 std::shared_ptr<Histogram<float>> binned;
143 std::shared_ptr<Histogram<std::shared_ptr<TFormula>,
float>> formula;
146 size_t formula_variable_index = -1;
154 std::vector<float> relative_errors_to_absolute(
const std::vector<float>& array)
const {
155 std::vector<float> result(3);
156 result[Nominal] = array[Nominal];
157 result[Up] = array[Nominal] * array[Up];
158 result[Down] = array[Nominal] * array[Down];
166 std::vector<float> variated_errors_to_absolute(
const std::vector<float>& array)
const {
167 std::vector<float> result(3);
168 result[Nominal] = array[Nominal];
169 result[Up] = std::abs(array[Up] - array[Nominal]);
170 result[Down] = std::abs(array[Nominal] - array[Down]);
175 std::vector<float> convert_errors(
const std::vector<float>& array)
const {
176 switch (error_type) {
177 case ErrorType::ABSOLUTE:
180 case ErrorType::RELATIVE:
181 return relative_errors_to_absolute(array);
183 case ErrorType::VARIATED:
184 return variated_errors_to_absolute(array);
187 throw std::runtime_error(
"Invalid error type");
194 void clamp(std::vector<float>& array)
const {
195 if ((array[Nominal] + array[Up]) > maximum) {
196 array[Up] = maximum - array[Nominal];
199 if ((array[Nominal] - array[Down]) < minimum) {
200 array[Down] = -(minimum - array[Nominal]);
205 virtual std::vector<float> get(
const Parameters& parameters)
const {
206 static auto double_errors = [](std::vector<float>& values) {
211 std::vector<float> variables = parameters.toArray(binning_variables);
213 bool outOfRange =
false;
219 std::vector<float> values = convert_errors(get<float>(*binned.get(), variables, outOfRange));
222 double_errors(values);
231 std::vector<std::shared_ptr<TFormula>> formulas = get<std::shared_ptr<TFormula>>(*formula.get(), variables, outOfRange);
232 std::vector<float> values;
235 variables = formula->clamp(variables);
237 for (
auto& formula: formulas) {
238 values.push_back(formula->Eval(variables[formula_variable_index]));
241 values = convert_errors(values);
244 double_errors(values);
Definition: BinnedValuesJSONParser.h:12
Definition: BinnedValues.h:88
ErrorType
Definition: BinnedValues.h:100
Definition: Histogram.h:7
Definition: BinnedValues.h:67
Definition: BinnedValues.h:18