yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.1.0
Transformation.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2025 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_TRANSFORMATION_H
7#define YODA_TRANSFORMATION_H
8
9#include "YODA/Exceptions.h"
11#include "YODA/Utils/ndarray.h"
12#include <algorithm>
13#include <functional>
14#include <map>
15#include <utility>
16
17
18namespace YODA {
19
20
28 template<size_t N, typename... Args>
30 public:
31
33 using Pair = std::pair<double,double>;
34 using NdVal = typename Utils::ndarray<double, N>;
35 using NdValPair = typename Utils::ndarray<Pair, N>;
36 using Breakdown = std::map<std::string,std::pair<double,double>>;
37
38
41
44 Transformation() = delete;
45
47 Transformation(const std::function<double(double, Args...)>& fn) : _Fn(fn) { }
48
50 Transformation(std::function<double(double, Args...)>&& fn) : _Fn(std::move(fn)) { }
51
52
54 double operator()(const double v, Args&&... args) const { return _Fn(v, std::forward<Args>(args)...); }
55
57
58
61
63 void transform(double& val, Args&&... args) const { val = _Fn(val, std::forward<Args>(args)...); }
64
66 void transform(NdVal& vals, Args&&... args) const {
67 for (double& val : vals) {
68 transform(val, std::forward<Args>(args)...);
69 }
70 }
71
72 // @todo allow arbitrary arrays, not just NdVal ...
73
75 void transform(double& val, Breakdown& signed_errs, Args&&... args) const {
76 // keep track of original central value
77 const double oldval = val;
78 // transform central value
79 transform(val, std::forward<Args>(args)...);
80 // transform errors
81 for (auto& item : signed_errs) {
82 item.second.first = _Fn(oldval + item.second.first, std::forward<Args>(args)...) - val;
83 item.second.second = _Fn(oldval + item.second.second, std::forward<Args>(args)...) - val;
84 }
85 }
86
88 void transform(double& val, Pair& errs, Args&&... args) const {
89 // transform errors
90 const double trf_min = _Fn(val - errs.first, std::forward<Args>(args)...);
91 const double trf_max = _Fn(val + errs.second, std::forward<Args>(args)...);
92 // transform central value
93 transform(val, std::forward<Args>(args)...);
94 // Deal with possible inversions of min/max ordering under the transformation
95 const double new_min = std::min(trf_min, trf_max);
96 const double new_max = std::max(trf_min, trf_max);
97 errs = std::make_pair(val - new_min, new_max - val);
98 }
99
101 void transform(NdVal& vals, NdValPair& errs, Args&&... args) const {
102 for (size_t i = 0; i < N; ++i) {
103 transform(vals[i], errs[i], std::forward<Args>(args)...);
104 }
105 }
106
108
109 protected:
110
113
114 std::function<double(double, Args...)> _Fn;
115
117
118 };
119
120
121
124
125 template<size_t N>
127
129
130}
131
132#endif
std::map< std::string, std::pair< double, double > > Breakdown
std::pair< double, double > Pair
Convenient aliases.
void transform(double &val, Breakdown &signed_errs, Args &&... args) const
Transform value val and breakdown of signed error pairs signed_errs.
Transformation(std::function< double(double, Args...)> &&fn)
Default constructor setting the trf method.
double operator()(const double v, Args &&... args) const
Operator calling the functor.
void transform(double &val, Args &&... args) const
Transform value val.
void transform(NdVal &vals, NdValPair &errs, Args &&... args) const
Transform values vals and error pairs errs.
void transform(double &val, Pair &errs, Args &&... args) const
Transform value val and error pair errs.
typename Utils::ndarray< double, N > NdVal
typename Utils::ndarray< Pair, N > NdValPair
Transformation(const std::function< double(double, Args...)> &fn)
Default constructor setting the trf method.
void transform(NdVal &vals, Args &&... args) const
Transform values vals.
Anonymous namespace to limit visibility.