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 RandomRing(const RandomType randomType = RandomType::Gaus, TRandom* rng = nullptr);
59
62 RandomRing(TF1& function);
63
67 void initialize(const RandomType randomType = RandomType::Gaus, TRandom* rng = nullptr);
68
72 void initialize(TF1& function, TRandom* rng = nullptr);
73
76 void initialize(std::function<float()> function);
77
83 {
84 const float value = mRandomNumbers[mRingPosition];
85 ++mRingPosition;
86 if (mRingPosition >= mRandomNumbers.size()) {
87 mRingPosition = 0;
88 }
89 return value;
90 }
91
97 template <typename VcType>
99 {
100 // This function is templated so that we don't need to include the <Vc/Vc> header
101 // within this header file (to reduce memory problems during compilation).
102 // The hope is that the calling user calls this with a
103 // correct Vc type (Vc::float_v) in a source file.
104 // a vector must not run past the end of the ring, also after setRandomPosition()
105 if (mRingPosition + VcType::size() > mRandomNumbers.size()) {
106 mRingPosition = 0;
107 }
108 const VcType value = VcType(&mRandomNumbers[mRingPosition]);
109 mRingPosition += VcType::size();
110 if (mRingPosition >= mRandomNumbers.size()) {
111 mRingPosition = 0;
112 }
113 return value;
114 }
115
118 unsigned int getRingPosition() const { return mRingPosition; }
119
122 void setRandomPosition(TRandom& rng) { mRingPosition = rng.Integer(N); }
123
124 private:
125 // =========================================================================
126 // ===| members |===========================================================
127 //
128
129 RandomType mRandomType;
130 std::array<float, N> mRandomNumbers;
131 size_t mRingPosition = 0;
132
133}; // end class RandomRing
134
135//______________________________________________________________________________
136template <size_t N>
137inline RandomRing<N>::RandomRing(const RandomType randomType, TRandom* rng)
138 : mRandomType(randomType),
139 mRandomNumbers()
140
141{
142 initialize(randomType, rng);
143}
144
145//______________________________________________________________________________
146template <size_t N>
148 : mRandomType(RandomType::CustomTF1),
149 mRandomNumbers()
150{
151 initialize(function);
152}
153
154//______________________________________________________________________________
155template <size_t N>
156inline void RandomRing<N>::initialize(const RandomType randomType, TRandom* rng)
157{
158 TRandom* random = rng ? rng : gRandom;
159 for (auto& v : mRandomNumbers) {
160 // TODO: configurable mean and sigma
161 switch (randomType) {
162 case RandomType::Gaus: {
163 v = random->Gaus(0, 1);
164 break;
165 }
166 case RandomType::Flat: {
167 v = random->Rndm();
168 break;
169 }
170 default: {
171 v = 0;
172 break;
173 }
174 }
175 }
176}
177
178//______________________________________________________________________________
179template <size_t N>
180inline void RandomRing<N>::initialize(TF1& function, TRandom* rng)
181{
182 mRandomType = RandomType::CustomTF1;
183 for (auto& v : mRandomNumbers) {
184 v = function.GetRandom(rng);
185 }
186}
187
188//______________________________________________________________________________
189template <size_t N>
190inline void RandomRing<N>::initialize(std::function<float()> function)
191{
192 mRandomType = RandomType::CustomLambda;
193 for (auto& v : mRandomNumbers) {
194 v = function();
195 }
196}
197
198} // namespace math_utils
199} // namespace o2
200#endif
@ CustomLambda
Initialized through external lambda.
@ CustomTF1
Custom TF1 function to be used.
RandomRing(const RandomType randomType=RandomType::Gaus, TRandom *rng=nullptr)
Definition RandomRing.h:137
void initialize(TF1 &function, TRandom *rng=nullptr)
Definition RandomRing.h:180
void initialize(const RandomType randomType=RandomType::Gaus, TRandom *rng=nullptr)
Definition RandomRing.h:156
void initialize(std::function< float()> function)
Definition RandomRing.h:190
RandomRing(TF1 &function)
Definition RandomRing.h:147
unsigned int getRingPosition() const
Definition RandomRing.h:118
void setRandomPosition(TRandom &rng)
Definition RandomRing.h:122
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 ...