Jpp  18.5.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JSUPPORT {}
15 namespace JPP { using namespace JSUPPORT; }
16 
17 namespace JSUPPORT {
18 
19  using JLANG::skip_type;
20 
21 
22  /**
23  * Template class for randomly sampling from a JLANG::JRewindableObjectIterator using a JLANG::JObjectSampler.
24  *
25  * The JRandomSampler class provides for an implementation of randomly sampling objects.\n
26  * The sampling is controlled via two parameters, namely:
27  * - a sequential iteration of a predefined average number of objects (JRandomSampler::averageOn); and
28  * - a predefined average number of objects to skip (JRandomSampler::averageOff).
29  */
30  template<class T>
31  struct JRandomSampler {
32  /**
33  * Default constructor.
34  *
35  * This constructor will sample all objects.
36  */
38  averageOn (1),
39  averageOff(0),
40  count (0)
41  {}
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param on average number of consecutively accepted events
48  * \param off average number of consecutively rejected events
49  */
50  JRandomSampler(const unsigned int on,
51  const unsigned int off) :
52  averageOn (on),
53  averageOff(off),
54  count (0)
55  {}
56 
57 
58  /**
59  * Get random sampler.
60  *
61  * \return random sampler
62  */
64  {
65  return static_cast<const JRandomSampler&>(*this);
66  }
67 
68 
69  /**
70  * Get random sampler.
71  *
72  * \return random sampler
73  */
75  {
76  return static_cast<JRandomSampler&>(*this);
77  }
78 
79 
80  /**
81  * Acceptance test operator.
82  * Return value 0 means accept and >0 number of objects to skip.
83  *
84  * \param object object
85  * \return number of objects to skip
86  */
87  skip_type operator()(const T& object) const
88  {
89  if (count == 0) {
90 
91  count = gRandom->Integer(2 * averageOn);
92 
93  return (skip_type) gRandom->Integer(2 * averageOff);
94 
95  } else {
96 
97  count -= 1;
98 
99  return 0;
100  }
101  }
102 
103 
104  /**
105  * Read random sampler from input.
106  *
107  * \param in input stream
108  * \param sampler random sampler
109  * \return input stream
110  */
111  friend inline std::istream& operator>>(std::istream& in, JRandomSampler& sampler)
112  {
113  in >> sampler.averageOn;
114  in >> sampler.averageOff;
115 
116  sampler.count = 0;
117 
118  return in;
119  }
120 
121 
122  /**
123  * Write random sampler to output.
124  *
125  * \param out output stream
126  * \param sampler random sampler
127  * \return output stream
128  */
129  friend inline std::ostream& operator<<(std::ostream& out, const JRandomSampler& sampler)
130  {
131  out << sampler.averageOn;
132  out << ' ';
133  out << sampler.averageOff;
134 
135  return out;
136  }
137 
138  protected:
141  mutable skip_type count;
142  };
143 }
144 
145 #endif
const JRandomSampler & getRandomSampler() const
Get random sampler.
unsigned int skip_type
Type definition for number of objects to skip.
friend std::istream & operator>>(std::istream &in, JRandomSampler &sampler)
Read random sampler from input.
JRandomSampler()
Default constructor.
skip_type operator()(const T &object) const
Acceptance test operator.
friend std::ostream & operator<<(std::ostream &out, const JRandomSampler &sampler)
Write random sampler to output.
Template class for randomly sampling from a JLANG::JRewindableObjectIterator using a JLANG::JObjectSa...
do set_variable OUTPUT_DIRECTORY $WORKDIR T
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
JRandomSampler & getRandomSampler()
Get random sampler.
JRandomSampler(const unsigned int on, const unsigned int off)
Constructor.