Project
Loading...
Searching...
No Matches
RandomRing.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
21
28
29#ifndef ALICEO2_MATHUTILS_RANDOMRING_H_
30#define ALICEO2_MATHUTILS_RANDOMRING_H_
31
32#include <array>
33
34#include "TF1.h"
35#include "TRandom.h"
36#include <functional>
37
38
39namespace o2
40{
41namespace math_utils
42{
43
44template <size_t N = 4 * 100000>
46{
47 public:
48 enum class RandomType : char {
49 Gaus,
50 Flat,
51 CustomTF1,
53 };
54
58
61 RandomRing(TF1& function);
62
65 void initialize(const RandomType randomType = RandomType::Gaus);
66
69 void initialize(TF1& function);
70
73 void initialize(std::function<float()> function);
74
80 {
81 const float value = mRandomNumbers[mRingPosition];
82 ++mRingPosition;
83 if (mRingPosition >= mRandomNumbers.size()) {
84 mRingPosition = 0;
85 }
86 return value;
87 }
88
94 template <typename VcType>
96 {
97 // This function is templated so that we don't need to include the <Vc/Vc> header
98 // within this header file (to reduce memory problems during compilation).
99 // The hope is that the calling user calls this with a
100 // correct Vc type (Vc::float_v) in a source file.
101 const VcType value = VcType(&mRandomNumbers[mRingPosition]);
102 mRingPosition += VcType::size();
103 if (mRingPosition >= mRandomNumbers.size()) {
104 mRingPosition = 0;
105 }
106 return value;
107 }
108
111 unsigned int getRingPosition() const { return mRingPosition; }
112
113 private:
114 // =========================================================================
115 // ===| members |===========================================================
116 //
117
118 RandomType mRandomType;
119 std::array<float, N> mRandomNumbers;
120 size_t mRingPosition = 0;
121
122}; // end class RandomRing
123
124//______________________________________________________________________________
125template <size_t N>
126inline RandomRing<N>::RandomRing(const RandomType randomType)
127 : mRandomType(randomType),
128 mRandomNumbers()
129
130{
131 initialize(randomType);
132}
133
134//______________________________________________________________________________
135template <size_t N>
137 : mRandomType(RandomType::CustomTF1),
138 mRandomNumbers()
139{
140 initialize(function);
141}
142
143//______________________________________________________________________________
144template <size_t N>
145inline void RandomRing<N>::initialize(const RandomType randomType)
146{
147
148 for (auto& v : mRandomNumbers) {
149 // TODO: configurable mean and sigma
150 switch (randomType) {
151 case RandomType::Gaus: {
152 v = gRandom->Gaus(0, 1);
153 break;
154 }
155 case RandomType::Flat: {
156 v = gRandom->Rndm();
157 break;
158 }
159 default: {
160 v = 0;
161 break;
162 }
163 }
164 }
165}
166
167//______________________________________________________________________________
168template <size_t N>
169inline void RandomRing<N>::initialize(TF1& function)
170{
171 mRandomType = RandomType::CustomTF1;
172 for (auto& v : mRandomNumbers) {
173 v = function.GetRandom();
174 }
175}
176
177//______________________________________________________________________________
178template <size_t N>
179inline void RandomRing<N>::initialize(std::function<float()> function)
180{
181 mRandomType = RandomType::CustomLambda;
182 for (auto& v : mRandomNumbers) {
183 v = function();
184 }
185}
186
187} // namespace math_utils
188} // namespace o2
189#endif
@ CustomLambda
Initialized through external lambda.
@ CustomTF1
Custom TF1 function to be used.
void initialize(const RandomType randomType=RandomType::Gaus)
Definition RandomRing.h:145
void initialize(std::function< float()> function)
Definition RandomRing.h:179
RandomRing(const RandomType randomType=RandomType::Gaus)
Definition RandomRing.h:126
RandomRing(TF1 &function)
Definition RandomRing.h:136
unsigned int getRingPosition() const
Definition RandomRing.h:111
void initialize(TF1 &function)
Definition RandomRing.h:169
const GLdouble * v
Definition glcorearb.h:832
GLsizei const GLfloat * value
Definition glcorearb.h:819
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...