Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JRandomSampler.hh
Go to the documentation of this file.
1#ifndef __JSUPPORT__JRANDOMSAMPLER__
2#define __JSUPPORT__JRANDOMSAMPLER__
3
4#include <istream>
5#include <ostream>
6
7#include "TRandom3.h"
8
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JSUPPORT {}
17namespace JPP { using namespace JSUPPORT; }
18
19namespace JSUPPORT {
20
21 using JLANG::skip_type;
22
23
24 /**
25 * Template class for randomly sampling from a JLANG::JRewindableObjectIterator using a JLANG::JObjectSampler.
26 *
27 * The JRandomSampler class provides for an implementation of randomly sampling objects.\n
28 * The sampling is controlled via two parameters, namely:
29 * - a sequential iteration of a predefined average number of objects (JRandomSampler::averageOn); and
30 * - a predefined average number of objects to skip (JRandomSampler::averageOff).
31 */
32 template<class T>
34 /**
35 * Default constructor.
36 *
37 * This constructor will sample all objects.
38 */
40 averageOn (1),
41 averageOff(0),
42 count (0)
43 {}
44
45
46 /**
47 * Constructor.
48 *
49 * \param on average number of consecutively accepted events
50 * \param off average number of consecutively rejected events
51 */
52 JRandomSampler(const unsigned int on,
53 const unsigned int off) :
54 averageOn (on),
55 averageOff(off),
56 count (0)
57 {}
58
59
60 /**
61 * Get random sampler.
62 *
63 * \return random sampler
64 */
66 {
67 return static_cast<const JRandomSampler&>(*this);
68 }
69
70
71 /**
72 * Get random sampler.
73 *
74 * \return random sampler
75 */
77 {
78 return static_cast<JRandomSampler&>(*this);
79 }
80
81
82 /**
83 * Acceptance test operator.
84 * Return value 0 means accept and >0 number of objects to skip.
85 *
86 * \param object object
87 * \return number of objects to skip
88 */
89 skip_type operator()(const T& object) const
90 {
91 if (count == 0) {
92
93 count = gRandom->Integer(2 * averageOn);
94
95 return (skip_type) gRandom->Integer(2 * averageOff);
96
97 } else {
98
99 count -= 1;
100
101 return 0;
102 }
103 }
104
105
106 /**
107 * Read random sampler from input.
108 *
109 * \param in input stream
110 * \param sampler random sampler
111 * \return input stream
112 */
113 friend inline std::istream& operator>>(std::istream& in, JRandomSampler& sampler)
114 {
115 in >> sampler.averageOn;
116 in >> sampler.averageOff;
117
118 sampler.count = 0;
119
120 return in;
121 }
122
123
124 /**
125 * Write random sampler to output.
126 *
127 * \param out output stream
128 * \param sampler random sampler
129 * \return output stream
130 */
131 friend inline std::ostream& operator<<(std::ostream& out, const JRandomSampler& sampler)
132 {
133 out << sampler.averageOn;
134 out << ' ';
135 out << sampler.averageOff;
136
137 return out;
138 }
139
140 protected:
144 };
145}
146
147#endif
unsigned int skip_type
Type definition for number of objects to skip.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
Template class for randomly sampling from a JLANG::JRewindableObjectIterator using a JLANG::JObjectSa...
JRandomSampler()
Default constructor.
skip_type operator()(const T &object) const
Acceptance test operator.
JRandomSampler(const unsigned int on, const unsigned int off)
Constructor.
JRandomSampler & getRandomSampler()
Get random sampler.
friend std::ostream & operator<<(std::ostream &out, const JRandomSampler &sampler)
Write random sampler to output.
const JRandomSampler & getRandomSampler() const
Get random sampler.
friend std::istream & operator>>(std::istream &in, JRandomSampler &sampler)
Read random sampler from input.