Project
Loading...
Searching...
No Matches
DataSamplingConditionRandom.cxx
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
19#include "Headers/DataHeader.h"
21
22#include "PCG/pcg_random.hpp"
23#include <random>
24
25#include <boost/property_tree/ptree.hpp>
26
27using namespace o2::framework;
28
29namespace o2::utilities
30{
31
32// todo: consider using run number as a seed
33using namespace o2::header;
34
37{
38
39 public:
42 mThreshold(0),
43 mGenerator(0),
44 mCurrentTimesliceID(0),
45 mLastDecision(false){};
47 ~DataSamplingConditionRandom() override = default;
48
50 void configure(const boost::property_tree::ptree& config) override
51 {
52 mThreshold = static_cast<uint32_t>(config.get<double>("fraction") * std::numeric_limits<uint32_t>::max());
53
54 auto seed = config.get<uint64_t>("seed");
55 mGenerator.seed((seed == 0) ? std::random_device()() : seed);
56
57 mCurrentTimesliceID = 0;
58 mLastDecision = false;
59
60 auto timeslideID = config.get_optional<std::string>("timesliceId").value_or("startTime");
61 if (timeslideID == "startTime") {
62 mGetTimesliceID = [](const o2::framework::DataRef& dataRef) {
63 const auto* dph = get<DataProcessingHeader*>(dataRef.header);
64 assert(dph);
65 return dph->startTime;
66 };
67 } else if (timeslideID == "tfCounter") {
68 mGetTimesliceID = [](const o2::framework::DataRef& dataRef) {
69 const auto* dh = get<DataHeader*>(dataRef.header);
70 assert(dh);
71 return dh->tfCounter;
72 };
73 } else if (timeslideID == "firstTForbit") {
74 mGetTimesliceID = [](const o2::framework::DataRef& dataRef) {
75 const auto* dh = get<DataHeader*>(dataRef.header);
76 assert(dh);
77 return dh->firstTForbit;
78 };
79 } else {
80 throw std::runtime_error("Data Sampling Condition Random does not support timesliceId '" + timeslideID + "'");
81 }
82 };
85 bool decide(const o2::framework::DataRef& dataRef) override
86 {
87 auto tid = mGetTimesliceID(dataRef);
88
89 int64_t diff = tid - mCurrentTimesliceID;
90 if (diff == -1) {
91 return mLastDecision;
92 } else if (diff < -1) {
93 mGenerator.backstep(static_cast<uint64_t>(-diff));
94 } else if (diff > 0) {
95 mGenerator.advance(static_cast<uint64_t>(diff));
96 }
97
98 mLastDecision = mGenerator() < mThreshold;
99 mCurrentTimesliceID = tid + 1;
100 return mLastDecision;
101 }
102
103 private:
104 uint32_t mThreshold;
105 pcg32_fast mGenerator;
106 bool mLastDecision;
107 uint64_t mCurrentTimesliceID;
108 std::function<uint64_t(const o2::framework::DataRef&)> mGetTimesliceID;
109};
110
112{
113 return std::make_unique<DataSamplingConditionRandom>();
114}
115
116} // namespace o2::utilities
A definition of DataSamplingConditionFactory.
A standarised data sampling condition, to decide if given data sample should be passed forward.
static std::unique_ptr< DataSamplingCondition > createDataSamplingConditionRandom()
Getter for DataSamplingConditionRandom.
A DataSamplingCondition which makes decisions randomly, but with determinism.
~DataSamplingConditionRandom() override=default
Default destructor.
void configure(const boost::property_tree::ptree &config) override
Reads 'fraction' parameter (type double, between 0 and 1) and seed (int).
bool decide(const o2::framework::DataRef &dataRef) override
Makes pseudo-random, deterministic decision based on TimesliceID. The reason behind using TimesliceID...
A standardised data sampling condition, to decide if given data sample should be passed forward.
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
O2 data header classes and API, v0.1.
Definition DetID.h:49
A header which contains some meta-data generated by Data Sampling.