Jpp  15.0.5
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 
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JSUPPORT {}
17 namespace JPP { using namespace JSUPPORT; }
18 
19 namespace 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>
33  struct JRandomSampler {
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:
143  mutable skip_type count;
144  };
145 }
146 
147 #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
JRandomSampler & getRandomSampler()
Get random sampler.
JRandomSampler(const unsigned int on, const unsigned int off)
Constructor.
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41