Jpp - 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  *
85  * \param object object
86  * \return number of object to skip
87  */
88  skip_type operator()(const T& object) const
89  {
90  if (count == 0) {
91 
92  count = gRandom->Integer(2 * averageOn);
93 
94  return (unsigned int) gRandom->Integer(2 * averageOff);
95 
96  } else {
97 
98  count -= 1;
99 
100  return 0;
101  }
102  }
103 
104 
105  /**
106  * Read random sampler from input.
107  *
108  * \param in input stream
109  * \param sampler random sampler
110  * \return input stream
111  */
112  friend inline std::istream& operator>>(std::istream& in, JRandomSampler& sampler)
113  {
114  in >> sampler.averageOn;
115  in >> sampler.averageOff;
116 
117  sampler.count = 0;
118 
119  return in;
120  }
121 
122 
123  /**
124  * Write random sampler to output.
125  *
126  * \param out output stream
127  * \param sampler random sampler
128  * \return output stream
129  */
130  friend inline std::ostream& operator<<(std::ostream& out, const JRandomSampler& sampler)
131  {
132  out << sampler.averageOn;
133  out << ' ';
134  out << sampler.averageOff;
135 
136  return out;
137  }
138 
139  protected:
142  mutable skip_type count;
143  };
144 }
145 
146 #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:38