yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
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-2024 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/Traits.h"
12//#include "YODA/Utils/sortedvector.h"
13#include "YODA/Utils/ndarray.h"
14#include <algorithm>
15#include <functional>
16#include <map>
17#include <utility>
18
19
20namespace YODA {
21
22
30 template<size_t N, typename... Args>
32 public:
33
35 using Pair = std::pair<double,double>;
36 using NdVal = typename Utils::ndarray<double, N>;
37 using NdValPair = typename Utils::ndarray<Pair, N>;
38 using Breakdown = std::map<std::string,std::pair<double,double>>;
39
40
43
46 Transformation() = delete;
47
49 Transformation(const std::function<double(double, Args...)>& fn) : _Fn(fn) { }
50
52 Transformation(std::function<double(double, Args...)>&& fn) : _Fn(std::move(fn)) { }
53
54
56 double operator()(const double v, Args&&... args) const { return _Fn(v, std::forward<Args>(args)...); }
57
59
60
63
65 void transform(double& val, Args&&... args) const { val = _Fn(val, std::forward<Args>(args)...); }
66
68 void transform(NdVal& vals, Args&&... args) const {
69 for (double& val : vals) {
70 transform(val, std::forward<Args>(args)...);
71 }
72 }
73
74 // @todo allow arbitrary arrays, not just NdVal ...
75
77 void transform(double& val, Breakdown& signed_errs, Args&&... args) const {
78 // keep track of original central value
79 const double oldval = val;
80 // transform central value
81 transform(val, std::forward<Args>(args)...);
82 // transform errors
83 for (auto& item : signed_errs) {
84 item.second.first = _Fn(oldval + item.second.first, std::forward<Args>(args)...) - val;
85 item.second.second = _Fn(oldval + item.second.second, std::forward<Args>(args)...) - val;
86 }
87 }
88
90 void transform(double& val, Pair& errs, Args&&... args) const {
91 // transform errors
92 const double trf_min = _Fn(val - errs.first, std::forward<Args>(args)...);
93 const double trf_max = _Fn(val + errs.second, std::forward<Args>(args)...);
94 // transform central value
95 transform(val, std::forward<Args>(args)...);
96 // Deal with possible inversions of min/max ordering under the transformation
97 const double new_min = std::min(trf_min, trf_max);
98 const double new_max = std::max(trf_min, trf_max);
99 errs = std::make_pair(val - new_min, new_max - val);
100 }
101
103 void transform(NdVal& vals, NdValPair& errs, Args&&... args) const {
104 for (size_t i = 0; i < N; ++i) {
105 transform(vals[i], errs[i], std::forward<Args>(args)...);
106 }
107 }
108
110
111 protected:
112
115
116 std::function<double(double, Args...)> _Fn;
117
119
120 };
121
122
123
126
127 template<size_t N>
129
131
132}
133
134#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.