1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#ifndef CondFormats_JetMETObjects_Utilities_h
#define CondFormats_JetMETObjects_Utilities_h
#ifdef STANDALONE
#include <stdexcept>
#else
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#endif
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include <tuple>
#include <cmath>
#include <utility>
namespace std {
//These functions print a tuple using a provided std::ostream
template <typename Type, unsigned N, unsigned Last>
struct tuple_printer {
static void print(std::ostream& out, const Type& value) {
out << std::get<N>(value) << ", ";
tuple_printer<Type, N + 1, Last>::print(out, value);
}
};
template <typename Type, unsigned N>
struct tuple_printer<Type, N, N> {
static void print(std::ostream& out, const Type& value) { out << std::get<N>(value); }
};
template <typename... Types>
std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
out << "(";
tuple_printer<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
out << ")";
return out;
}
//----------------------------------------------------------------------
//Returns a list of type indices
template <size_t... n>
struct ct_integers_list {
template <size_t m>
struct push_back {
typedef ct_integers_list<n..., m> type;
};
};
template <size_t max>
struct ct_iota_1 {
typedef typename ct_iota_1<max - 1>::type::template push_back<max>::type type;
};
template <>
struct ct_iota_1<0> {
typedef ct_integers_list<> type;
};
//----------------------------------------------------------------------
//Return a tuple which is a subset of the original tuple
//This function pops an entry off the font of the tuple
template <size_t... indices, typename Tuple>
auto tuple_subset(const Tuple& tpl,
ct_integers_list<indices...>) -> decltype(std::make_tuple(std::get<indices>(tpl)...)) {
return std::make_tuple(std::get<indices>(tpl)...);
// this means:
// make_tuple(get<indices[0]>(tpl), get<indices[1]>(tpl), ...)
}
template <typename Head, typename... Tail>
std::tuple<Tail...> tuple_tail(const std::tuple<Head, Tail...>& tpl) {
return tuple_subset(tpl, typename ct_iota_1<sizeof...(Tail)>::type());
// this means:
// tuple_subset<1, 2, 3, ..., sizeof...(Tail)-1>(tpl, ..)
}
//----------------------------------------------------------------------
//Recursive hashing function for tuples
template <typename Head, typename... ndims>
struct hash_specialization {
typedef std::tuple<Head, ndims...> argument_type;
typedef std::size_t result_type;
result_type operator()(const argument_type& t) const {
const uint32_t& b = reinterpret_cast<const uint32_t&>(std::get<0>(t));
//const uint32_t& more = (*this)(tuple_tail(t));
const uint32_t& more = hash_specialization<ndims...>()(tuple_tail(t));
return b ^ more;
}
};
//Base case
template <>
struct hash_specialization<float> {
typedef std::tuple<float> argument_type;
typedef std::size_t result_type;
result_type operator()(const argument_type& t) const {
const uint32_t& b = reinterpret_cast<const uint32_t&>(std::get<0>(t));
return static_cast<result_type>(b);
}
};
//Overloaded verions of std::hash for tuples
template <typename Head, typename... ndims>
struct hash<std::tuple<Head, ndims...>> {
typedef std::tuple<Head, ndims...> argument_type;
typedef std::size_t result_type;
result_type operator()(const argument_type& t) const { return hash_specialization<Head, ndims...>()(t); }
};
template <>
struct hash<std::tuple<>> {
typedef std::tuple<> argument_type;
typedef std::size_t result_type;
result_type operator()(const argument_type& t) const { return -1; }
};
} // namespace std
namespace {
inline void handleError(const std::string& fClass, const std::string& fMessage) {
#ifdef STANDALONE
std::stringstream sserr;
sserr << fClass << " ERROR: " << fMessage;
throw std::runtime_error(sserr.str());
#else
edm::LogError(fClass) << fMessage;
#endif
}
//----------------------------------------------------------------------
inline float getFloat(const std::string& token) {
char* endptr;
float result = strtod(token.c_str(), &endptr);
if (endptr == token.c_str()) {
std::stringstream sserr;
sserr << "can't convert token " << token << " to float value";
handleError("getFloat", sserr.str());
}
return result;
}
//----------------------------------------------------------------------
inline unsigned getUnsigned(const std::string& token) {
char* endptr;
unsigned result = strtoul(token.c_str(), &endptr, 0);
if (endptr == token.c_str()) {
std::stringstream sserr;
sserr << "can't convert token " << token << " to unsigned value";
handleError("getUnsigned", sserr.str());
}
return result;
}
inline long int getSigned(const std::string& token) {
char* endptr;
unsigned result = strtol(token.c_str(), &endptr, 0);
if (endptr == token.c_str()) {
std::stringstream sserr;
sserr << "can't convert token " << token << " to signed value";
handleError("getSigned", sserr.str());
}
return result;
}
//----------------------------------------------------------------------
inline std::string getSection(const std::string& token) {
size_t iFirst = token.find('[');
size_t iLast = token.find(']');
if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
return std::string(token, iFirst + 1, iLast - iFirst - 1);
return "";
}
//----------------------------------------------------------------------
inline std::vector<std::string> getTokens(const std::string& fLine) {
std::vector<std::string> tokens;
std::string currentToken;
for (unsigned ipos = 0; ipos < fLine.length(); ++ipos) {
char c = fLine[ipos];
if (c == '#')
break; // ignore comments
else if (c == ' ') { // flush current token if any
if (!currentToken.empty()) {
tokens.push_back(currentToken);
currentToken.clear();
}
} else
currentToken += c;
}
if (!currentToken.empty())
tokens.push_back(currentToken); // flush end
return tokens;
}
//----------------------------------------------------------------------
inline std::string getDefinitions(const std::string& token) {
size_t iFirst = token.find('{');
size_t iLast = token.find('}');
if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
return std::string(token, iFirst + 1, iLast - iFirst - 1);
return "";
}
//------------------------------------------------------------------------
inline float quadraticInterpolation(float fZ, const float fX[3], const float fY[3]) {
// Quadratic interpolation through the points (x[i],y[i]). First find the parabola that
// is defined by the points and then calculate the y(z).
float D[4], a[3];
D[0] = fX[0] * fX[1] * (fX[0] - fX[1]) + fX[1] * fX[2] * (fX[1] - fX[2]) + fX[2] * fX[0] * (fX[2] - fX[0]);
D[3] = fY[0] * (fX[1] - fX[2]) + fY[1] * (fX[2] - fX[0]) + fY[2] * (fX[0] - fX[1]);
D[2] = fY[0] * (pow(fX[2], 2) - pow(fX[1], 2)) + fY[1] * (pow(fX[0], 2) - pow(fX[2], 2)) +
fY[2] * (pow(fX[1], 2) - pow(fX[0], 2));
D[1] = fY[0] * fX[1] * fX[2] * (fX[1] - fX[2]) + fY[1] * fX[0] * fX[2] * (fX[2] - fX[0]) +
fY[2] * fX[0] * fX[1] * (fX[0] - fX[1]);
if (D[0] != 0) {
a[0] = D[1] / D[0];
a[1] = D[2] / D[0];
a[2] = D[3] / D[0];
} else {
a[0] = 0.0;
a[1] = 0.0;
a[2] = 0.0;
}
float r = a[0] + fZ * (a[1] + fZ * a[2]);
return r;
}
//------------------------------------------------------------------------
//Generates a std::tuple type based on a stored type and the number of
// objects in the tuple.
//Note: All of the objects will be of the same type
template <typename /*LEFT_TUPLE*/, typename /*RIGHT_TUPLE*/>
struct join_tuples {};
template <typename... LEFT, typename... RIGHT>
struct join_tuples<std::tuple<LEFT...>, std::tuple<RIGHT...>> {
typedef std::tuple<LEFT..., RIGHT...> type;
};
template <typename T, unsigned N>
struct generate_tuple_type {
typedef typename generate_tuple_type<T, N / 2>::type left;
typedef typename generate_tuple_type<T, N / 2 + N % 2>::type right;
typedef typename join_tuples<left, right>::type type;
};
template <typename T>
struct generate_tuple_type<T, 1> {
typedef std::tuple<T> type;
};
template <typename T>
struct generate_tuple_type<T, 0> {
typedef std::tuple<> type;
};
//------------------------------------------------------------------------
//C++11 implementation of make_index_sequence, which is a C++14 function
// using aliases for cleaner syntax
template <class T>
using Invoke = typename T::type;
template <unsigned...>
struct seq {
using type = seq;
};
template <class S1, class S2>
struct concat;
template <unsigned... I1, unsigned... I2>
struct concat<seq<I1...>, seq<I2...>> : seq<I1..., (sizeof...(I1) + I2)...> {};
template <class S1, class S2>
using Concat = Invoke<concat<S1, S2>>;
template <unsigned N>
struct gen_seq;
template <unsigned N>
using GenSeq = Invoke<gen_seq<N>>;
template <unsigned N>
struct gen_seq : Concat<GenSeq<N / 2>, GenSeq<N - N / 2>> {};
template <>
struct gen_seq<0> : seq<> {};
template <>
struct gen_seq<1> : seq<0> {};
//------------------------------------------------------------------------
//Generates a tuple based on a given function (i.e. lambda expression)
template <typename F, unsigned... Is>
auto gen_tuple_impl(F func, seq<Is...>) -> decltype(std::make_tuple(func(Is)...)) {
return std::make_tuple(func(Is)...);
}
template <unsigned N, typename F>
auto gen_tuple(F func) -> decltype(gen_tuple_impl(func, GenSeq<N>())) {
return gen_tuple_impl(func, GenSeq<N>());
}
} // namespace
#endif
|