ATLAS Offline Software
Loading...
Searching...
No Matches
TFCS1DFunctionTemplateHelpers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ISF_FASTCALOSIMEVENT_TFCS1DFunctionTemplateHelpers_h
6#define ISF_FASTCALOSIMEVENT_TFCS1DFunctionTemplateHelpers_h
7
8#include "TBuffer.h"
9
10#include <vector>
11#include <cstring>
12#include <algorithm>
13#include <cmath>
14
15// For the purpose of FastCaloSim, 32bit are sufficient for bin counting
16typedef uint32_t TFCS1DFunction_size_t;
17
19template <typename T, typename Tfloat = float> class TFCS1DFunction_Numeric {
20public:
21 static constexpr T MaxValue = 1;
22 static constexpr Tfloat MaxValueFloat = MaxValue;
23
24 static inline T MaxCeilOnlyForInt(const Tfloat value) { return value; };
25 static inline T ExpandToMaxRange(const Tfloat value) { return value; };
26 static inline Tfloat ToNormalizedRange(const T value) { return value; };
27};
28
30template <typename Tfloat> class TFCS1DFunction_Numeric<uint8_t, Tfloat> {
31public:
32 static constexpr uint8_t MaxValue = UINT8_MAX;
33 static constexpr Tfloat MaxValueFloat = MaxValue;
34
35 static inline uint8_t MaxCeilOnlyForInt(const Tfloat value) {
36 return std::ceil(MaxValueFloat * value);
37 };
38 static inline uint8_t ExpandToMaxRange(const Tfloat value) {
39 return value * (1 + MaxValueFloat);
40 };
41 static inline Tfloat ToNormalizedRange(const uint8_t value) {
42 return value / (1 + MaxValueFloat);
43 };
44};
45
47template <typename Tfloat> class TFCS1DFunction_Numeric<uint16_t, Tfloat> {
48public:
49 static constexpr uint16_t MaxValue = UINT16_MAX;
50 static constexpr Tfloat MaxValueFloat = MaxValue;
51
52 static inline uint16_t MaxCeilOnlyForInt(const Tfloat value) {
53 return std::ceil(MaxValueFloat * value);
54 };
55 static inline uint16_t ExpandToMaxRange(const Tfloat value) {
56 return value * (1 + MaxValueFloat);
57 };
58 static inline Tfloat ToNormalizedRange(const uint16_t value) {
59 return value / (1 + MaxValueFloat);
60 };
61};
62
64template <typename Tfloat> class TFCS1DFunction_Numeric<uint32_t, Tfloat> {
65public:
66 static constexpr uint32_t MaxValue = UINT32_MAX;
67 static constexpr Tfloat MaxValueFloat = MaxValue;
68
69 static inline uint32_t MaxCeilOnlyForInt(const Tfloat value) {
70 return std::ceil(MaxValueFloat * value);
71 };
72 static inline uint32_t ExpandToMaxRange(const Tfloat value) {
73 return value * (1 + MaxValueFloat);
74 };
75 static inline Tfloat ToNormalizedRange(const uint32_t value) {
76 return value / (1 + MaxValueFloat);
77 };
78};
79
80template <typename T> class TFCS1DFunction_Array {
81public:
83
87 if (m_content)
88 delete[] m_content;
89 };
90
91 std::size_t MemorySizeArray() const { return size() * sizeof(T); };
92 std::size_t MemorySize() const { return sizeof(*this) + MemorySizeArray(); };
93
94 inline size_t size() const { return m_size; };
95
97 void resize(size_t count) {
98 T *new_cont = nullptr;
99 if (count > 0)
100 new_cont = new T[count];
101 if (m_content && new_cont) {
102 size_t ncopy = count;
103 if (size() < ncopy)
104 ncopy = size();
105 ncopy *= sizeof(T);
106 std::memcpy(new_cont, m_content, ncopy);
107 }
108 if (m_content)
109 delete[] m_content;
110 m_size = count;
111 m_content = new_cont;
112 };
113
115 inline T *data() { return m_content; };
116 inline const T *data() const { return m_content; };
117
120 inline T &operator[](size_t pos) { return m_content[pos]; };
121 inline const T &operator[](size_t pos) const { return m_content[pos]; };
122
124 inline T *begin() { return m_content; };
125 inline const T *begin() const { return m_content; };
126
128 inline T *end() { return m_content + size(); };
129 inline const T *end() const { return m_content + size(); };
130
131private:
132 T *m_content{nullptr};
133 size_t m_size{0};
134
135 // Use ClassDef without virtual functions. Saves 8 bytes per instance
136 ClassDefNV(TFCS1DFunction_Array, 1) // TFCS1DFunction_Array
137};
138
140template <typename T> void TFCS1DFunction_Array<T>::Streamer(TBuffer &b) {
141 if (b.IsReading()) {
142 size_t count;
143 b >> count;
144 resize(count);
145 if (m_size > 0)
146 b.ReadFastArray(m_content, m_size);
147 } else {
148 b << m_size;
149 if (m_size > 0)
150 b.WriteFastArray(m_content, m_size);
151 }
152}
153
154// Class to represent histogram content. Trandom should be a type with a
155// floating point range [0,1]
156template <typename T, typename Trandom = float>
158public:
160 typedef T value_type;
161 typedef Trandom random_type;
162
164 : m_array(nbins >= 1 ? nbins - 1 : 0){};
165
166 std::size_t MemorySizeArray() const { return m_array.MemorySizeArray(); };
167 std::size_t MemorySize() const { return sizeof(*this) + MemorySizeArray(); };
168
171 void set_fraction(size_t pos, Trandom value) {
172 if (pos >= size())
173 return;
175 };
176
178 inline Trandom get_fraction(size_t pos) const {
179 if (pos >= size())
180 return 1;
182 };
183
185 inline Trandom get_binfraction(size_t pos) const {
186 if (pos == 0)
188 if (pos == size())
189 return 1 - (m_array[size() - 1] /
191 return (m_array[pos] - m_array[pos - 1]) /
193 };
194
198 void set_nbins(size_t nbins) { m_array.resize(nbins >= 1 ? nbins - 1 : 0); };
199
203 inline size_t get_nbins() const { return size() + 1; };
204
208 size_t get_bin(Trandom drnd, Trandom &residual_rnd) const {
209 if (drnd <= 0)
210 drnd = 0;
211 if (drnd >= 1)
212 drnd = std::nextafter((Trandom)1.0, (Trandom)0.0);
213 if (size() == 0) {
214 residual_rnd = drnd;
215 return 0;
216 }
218 auto it = std::upper_bound(m_array.begin(), m_array.end(), rnd);
219
220 T basecont = 0;
221 if (it != m_array.begin())
222 basecont = *(it - 1);
223
225 if (it != m_array.end())
226 fullcont = *it;
227
228 T dcont = fullcont - basecont;
229 if (dcont > 0) {
230 residual_rnd = ((Trandom)(rnd - basecont)) / dcont;
231 if (residual_rnd > 1)
232 residual_rnd = std::nextafter((Trandom)1.0, (Trandom)0.0);
233 } else {
234 residual_rnd = 0.5;
235 }
236
237 if (it == m_array.end())
238 return size();
239 return std::distance(m_array.begin(), it);
240 };
241
242private:
244 inline size_t size() const { return m_array.size(); };
245
246 // Use ClassDef without virtual functions. Saves 8 bytes per instance
248 1) // TFCS1DFunction_HistogramContent
249};
250
251// Class to represent histogram bin edges. Trandom should be a type with a
252// floating point range [0,1]
253template <typename T, typename Trandom = float>
255public:
257 typedef T value_type;
258 typedef Trandom random_type;
259
260 TFCS1DFunction_HistogramBinEdges(size_t nbins = 0) : m_array(nbins + 1){};
262
263 std::size_t MemorySizeArray() const { return m_array.MemorySizeArray(); };
264 std::size_t MemorySize() const { return sizeof(*this) + MemorySizeArray(); };
265
267 void set_nbins(size_t nbins) { m_array.resize(nbins + 1); };
268
270 inline size_t get_nbins() const { return size() - 1; };
271
273 void SetBinLowEdge(size_t pos, const T &value) { m_array[pos] = value; };
274
276 inline const T &GetBinLowEdge(size_t pos) const { return m_array[pos]; };
277
279 inline const T GetBinLength(size_t pos) const {
280 return GetBinLowEdge(pos + 1) - GetBinLowEdge(pos);
281 };
282
284 void SetMin(const T &value) { m_array[0] = value; };
285 inline const T &GetMin() const { return m_array[0]; };
286
288 void SetMax(const T &value) { m_array[get_nbins()] = value; };
289 inline const T &GetMax() const { return m_array[get_nbins()]; };
290
292 void SetMinMax(const T &valuemin, const T &valuemax) {
293 SetMin(valuemin);
294 SetMax(valuemax);
295 };
296
298 inline const T Length() const { return GetMax() - GetMin(); };
299
302 inline T position(size_t pos, const Trandom &drnd) const {
303 return (1 - drnd) * m_array[pos] + drnd * m_array[pos + 1];
304 };
305
309 inline T position_lin(size_t pos, Trandom m, const Trandom &drnd) const {
310 if (m > 2)
311 m = 2;
312 if (m < -2)
313 m = -2;
314 Trandom x =
315 fabs(m) > 0.001
316 ? (0.5 * std::sqrt(m * (m + 8 * drnd - 4) + 4) - 1) / m + 0.5
317 : drnd;
318 return (1 - x) * m_array[pos] + x * m_array[pos + 1];
319 };
320
325 inline T position_exp(size_t pos, Trandom beta, const Trandom &drnd) const {
326 Trandom z = drnd;
327 T pos1 = GetBinLowEdge(pos);
328 T pos2 = GetBinLowEdge(pos + 1);
329 if (fabs(beta) < 1.0e-8)
330 return (1 - z) * pos1 + z * pos2;
331 else {
332 T deltax = m_array[pos + 1] - m_array[pos];
333 if (deltax == 0)
334 return m_array[pos];
335 else {
336 z = 1 / beta * log(1.0 + drnd * (exp(beta * deltax) - 1.0)) / deltax;
337 }
338 }
339
340 return (1 - z) * pos1 + z * pos2;
341 };
342
343private:
345 inline size_t size() const { return m_array.size(); };
346
347 // Use ClassDef without virtual functions. Saves 8 bytes per instance
349 1) // TFCS1DFunction_HistogramBinEdges
350};
351
353 : public TFCS1DFunction_HistogramBinEdges<float, float> {
354public:
356 : TFCS1DFunction_HistogramBinEdges<float, float>(nbins){};
357
359 1) // TFCS1DFunction_HistogramFloatBinEdges
360};
361
363 : public TFCS1DFunction_HistogramBinEdges<double, double> {
364public:
366 : TFCS1DFunction_HistogramBinEdges<double, double>(nbins){};
367
369 1) // TFCS1DFunction_HistogramDoubleBinEdges
370};
371
372// Class to represent histogram bin edges, where the interval between GetMin()
373// and GetMax() can be stored as a different (smaller) data type Tint Trandom
374// should be a type with a floating point range [0,1]
375template <typename T, typename Tint, typename Trandom = float>
377public:
379 typedef T value_type;
380 typedef Trandom random_type;
382
384 : m_array(nbins >= 1 ? nbins - 1 : 0){};
386
387 std::size_t MemorySizeArray() const { return m_array.MemorySizeArray(); };
388 std::size_t MemorySize() const { return sizeof(*this) + MemorySizeArray(); };
389
391 void set_nbins(size_t nbins) { m_array.resize(nbins >= 1 ? nbins - 1 : 0); };
392
394 inline size_t get_nbins() const { return size() + 1; };
395
398 void SetBinLowEdge(size_t pos, const T &value) {
399 if (pos == 0) {
400 SetMin(value);
401 return;
402 }
403 if (pos >= get_nbins()) {
404 SetMax(value);
405 return;
406 }
408 (value - GetMin()) / Length());
409 };
410
413 inline const T GetBinLowEdge(size_t pos) const {
414 if (pos == 0)
415 return GetMin();
416 if (pos >= get_nbins())
417 return GetMax();
418 return GetMin() +
420 m_array[pos - 1]);
421 };
422
424 inline const T GetBinLength(size_t pos) const {
425 return GetBinLowEdge(pos + 1) - GetBinLowEdge(pos);
426 };
427
429 void SetMin(const T &value) { m_Min = value; };
430 inline const T &GetMin() const { return m_Min; };
431
433 void SetMax(const T &value) { m_Max = value; };
434 inline const T &GetMax() const { return m_Max; };
435
437 void SetMinMax(const T &valuemin, const T &valuemax) {
438 SetMin(valuemin);
439 SetMax(valuemax);
440 };
441
443 inline const T Length() const { return GetMax() - GetMin(); };
444
447 inline T position(size_t pos, const Trandom &drnd) const {
448 T pos1 = GetBinLowEdge(pos);
449 T pos2 = GetBinLowEdge(pos + 1);
450 return (1 - drnd) * pos1 + drnd * pos2;
451 };
452
456
460 inline T position_lin(size_t pos, Trandom m, const Trandom &drnd) const {
461 if (m > 2)
462 m = 2;
463 if (m < -2)
464 m = -2;
465 Trandom x =
466 fabs(m) > 0.001
467 ? (0.5 * std::sqrt(m * (m + 8 * drnd - 4) + 4) - 1) / m + 0.5
468 : drnd;
469 T pos1 = GetBinLowEdge(pos);
470 T pos2 = GetBinLowEdge(pos + 1);
471 return (1 - x) * pos1 + x * pos2;
472 };
473
478 inline T position_exp(size_t pos, Trandom beta, const Trandom &drnd) const {
479 Trandom z = drnd;
480 T pos1 = GetBinLowEdge(pos);
481 T pos2 = GetBinLowEdge(pos + 1);
482 if (fabs(beta) < 1.0e-8)
483 return (1 - z) * pos1 + z * pos2;
484 else {
485 T deltax = m_array[pos + 1] - m_array[pos];
486 if (deltax == 0)
487 return m_array[pos];
488 else {
489 z = 1 / beta * log(1.0 + drnd * (exp(beta * deltax) - 1.0)) / deltax;
490 }
491 }
492
493 return (1 - z) * pos1 + z * pos2;
494 };
495
496private:
498 inline size_t size() const { return m_array.size(); };
499 T m_Min{0};
500 T m_Max{0};
501
503 1) // TFCS1DFunction_HistogramCompactBinEdges
504};
505
507 : public TFCS1DFunction_HistogramCompactBinEdges<float, uint8_t, float> {
508public:
510 : TFCS1DFunction_HistogramCompactBinEdges<float, uint8_t, float>(nbins){};
511
513 1) // TFCS1DFunction_HistogramInt8BinEdges
514};
515
517 : public TFCS1DFunction_HistogramCompactBinEdges<float, uint16_t, float> {
518public:
520 : TFCS1DFunction_HistogramCompactBinEdges<float, uint16_t, float>(
521 nbins){};
522
524 1) // TFCS1DFunction_HistogramInt16BinEdges
525};
526
528 : public TFCS1DFunction_HistogramCompactBinEdges<float, uint32_t, float> {
529public:
531 : TFCS1DFunction_HistogramCompactBinEdges<float, uint32_t, float>(
532 nbins){};
533
535 1) // TFCS1DFunction_HistogramInt32BinEdges
536};
537
538#if defined(__ROOTCLING__) && defined(__FastCaloSimStandAlone__)
539#pragma link C++ class TFCS1DFunction_Numeric < uint8_t, float> + ;
540#pragma link C++ class TFCS1DFunction_Numeric < uint16_t, float> + ;
541#pragma link C++ class TFCS1DFunction_Numeric < uint32_t, float> + ;
542#pragma link C++ class TFCS1DFunction_Numeric < float, float> + ;
543#pragma link C++ class TFCS1DFunction_Numeric < double, float> + ;
544#pragma link C++ class TFCS1DFunction_Numeric < double, double> + ;
545
546#pragma link C++ class TFCS1DFunction_Array < float> - ;
547#pragma link C++ class TFCS1DFunction_Array < double> - ;
548#pragma link C++ class TFCS1DFunction_Array < uint8_t> - ;
549#pragma link C++ class TFCS1DFunction_Array < uint16_t> - ;
550#pragma link C++ class TFCS1DFunction_Array < uint32_t> - ;
551
552#pragma link C++ class TFCS1DFunction_HistogramContent < float, float> + ;
553#pragma link C++ class TFCS1DFunction_HistogramContent < double, float> + ;
554#pragma link C++ class TFCS1DFunction_HistogramContent < double, double> + ;
555#pragma link C++ class TFCS1DFunction_HistogramContent < uint8_t, float> + ;
556#pragma link C++ class TFCS1DFunction_HistogramContent < uint16_t, float> + ;
557#pragma link C++ class TFCS1DFunction_HistogramContent < uint32_t, float> + ;
558
559#pragma link C++ class TFCS1DFunction_HistogramBinEdges < float, float> + ;
560#pragma link C++ class TFCS1DFunction_HistogramBinEdges < double, float> + ;
561#pragma link C++ class TFCS1DFunction_HistogramBinEdges < double, double> + ;
562
563#pragma link C++ class TFCS1DFunction_HistogramCompactBinEdges < float, \
564 uint8_t, float> + \
565 ;
566#pragma link C++ class TFCS1DFunction_HistogramCompactBinEdges < float, \
567 uint16_t, float> + \
568 ;
569#pragma link C++ class TFCS1DFunction_HistogramCompactBinEdges < float, \
570 uint32_t, float> + \
571 ;
572
573#pragma link C++ class TFCS1DFunction_HistogramInt8BinEdges + ;
574#pragma link C++ class TFCS1DFunction_HistogramInt16BinEdges + ;
575#pragma link C++ class TFCS1DFunction_HistogramInt32BinEdges + ;
576#pragma link C++ class TFCS1DFunction_HistogramFloatBinEdges + ;
577#pragma link C++ class TFCS1DFunction_HistogramDoubleBinEdges + ;
578
579#endif
580
581#endif
static TRandom * rnd
uint32_t TFCS1DFunction_size_t
#define x
#define z
void resize(size_t count)
resize to given count, copying old content
const T & operator[](size_t pos) const
T * data()
Direct data pointer.
T & operator[](size_t pos)
Direct access operators.
void SetBinLowEdge(size_t pos, const T &value)
set position of lower edge of bins
T position_lin(size_t pos, Trandom m, const Trandom &drnd) const
return linearly interpolated position for bin pos, such that histograming the position gives a linear...
const T & GetBinLowEdge(size_t pos) const
get position of lower edge of bins
T position_exp(size_t pos, Trandom beta, const Trandom &drnd) const
return exponetially interpolated position for bin pos, such that histograming the position gives a li...
void SetMinMax(const T &valuemin, const T &valuemax)
set minimum and maximum
size_t get_nbins() const
return number of bins
void SetMax(const T &value)
set and get maximum
T position(size_t pos, const Trandom &drnd) const
return linear interpolated position for bin pos.
const T GetBinLength(size_t pos) const
get the length of a bin
void SetMin(const T &value)
set and get minimum
void set_nbins(size_t nbins)
set number of bins
const T Length() const
Get length of interval of all bins.
void SetBinLowEdge(size_t pos, const T &value)
set position of lower edge of bins.
void SetMinMax(const T &valuemin, const T &valuemax)
set minimum and maximum
T position_exp(size_t pos, Trandom beta, const Trandom &drnd) const
return exponentially interpolated position for bin pos, such that histograming the position gives a l...
void set_nbins(size_t nbins)
set number of bins
T position(size_t pos, const Trandom &drnd) const
return linear interpolated position for bin pos.
const T GetBinLowEdge(size_t pos) const
get position of lower edge of bins.
const T GetBinLength(size_t pos) const
get the length of a bin
T position_lin(size_t pos, Trandom m, const Trandom &drnd) const
return interpolated position for bin pos, such that histograming the position gives a linear slope m,...
void SetMin(const T &value)
set and get minimum
const T Length() const
Get length of interval of all bins.
void SetMax(const T &value)
set and get maximum
Trandom get_binfraction(size_t pos) const
Get the content at bin pos as fraction in the range [0,1].
size_t get_bin(Trandom drnd, Trandom &residual_rnd) const
Get the matching bin for a given random value in the range [0,1).
Trandom get_fraction(size_t pos) const
Get the cumulative content at bin pos as fraction in the range [0,1].
void set_nbins(size_t nbins)
set number of bins.
void set_fraction(size_t pos, Trandom value)
Set the content of bin pos to a given value, where value is in the range [0,1].
size_t get_nbins() const
return number of bins.
static uint16_t MaxCeilOnlyForInt(const Tfloat value)
static Tfloat ToNormalizedRange(const uint16_t value)
static uint16_t ExpandToMaxRange(const Tfloat value)
static uint32_t MaxCeilOnlyForInt(const Tfloat value)
static uint32_t ExpandToMaxRange(const Tfloat value)
static Tfloat ToNormalizedRange(const uint32_t value)
static Tfloat ToNormalizedRange(const uint8_t value)
static uint8_t ExpandToMaxRange(const Tfloat value)
static uint8_t MaxCeilOnlyForInt(const Tfloat value)
Converter functions that does nothing for floats.
static Tfloat ToNormalizedRange(const T value)
static T ExpandToMaxRange(const Tfloat value)
static T MaxCeilOnlyForInt(const Tfloat value)
static constexpr Tfloat MaxValueFloat
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146