Jpp
 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 JRewindableObjectIterator.
26  */
27  template<class T>
28  struct JRandomSampler {
29  /**
30  * Default constructor.
31  *
32  * This constructor will sample all objects.
33  */
35  averageOn (1),
36  averageOff(0),
37  count (0)
38  {}
39 
40 
41  /**
42  * Constructor.
43  *
44  * \param on average number of consecutively accepted events
45  * \param off average number of consecutively rejected events
46  */
47  JRandomSampler(const unsigned int on,
48  const unsigned int off) :
49  averageOn (on),
50  averageOff(off),
51  count (0)
52  {}
53 
54 
55  /**
56  * Get random sampler.
57  *
58  * \return random sampler
59  */
61  {
62  return static_cast<const JRandomSampler&>(*this);
63  }
64 
65 
66  /**
67  * Get random sampler.
68  *
69  * \return random sampler
70  */
72  {
73  return static_cast<JRandomSampler&>(*this);
74  }
75 
76 
77  /**
78  * Acceptance test operator.
79  *
80  * \param object object
81  * \return number of object to skip
82  */
83  skip_type operator()(const T& object) const
84  {
85  if (count == 0) {
86 
87  count = gRandom->Integer(2 * averageOn);
88 
89  return (unsigned int) gRandom->Integer(2 * averageOff);
90 
91  } else {
92 
93  count -= 1;
94 
95  return 0;
96  }
97  }
98 
99 
100  /**
101  * Read random sampler from input.
102  *
103  * \param in input stream
104  * \param sampler random sampler
105  * \return input stream
106  */
107  friend inline std::istream& operator>>(std::istream& in, JRandomSampler& sampler)
108  {
109  in >> sampler.averageOn;
110  in >> sampler.averageOff;
111 
112  sampler.count = 0;
113 
114  return in;
115  }
116 
117 
118  /**
119  * Write random sampler to output.
120  *
121  * \param out output stream
122  * \param sampler random sampler
123  * \return output stream
124  */
125  friend inline std::ostream& operator<<(std::ostream& out, const JRandomSampler& sampler)
126  {
127  out << sampler.averageOn;
128  out << ' ';
129  out << sampler.averageOff;
130 
131  return out;
132  }
133 
134  protected:
137  mutable skip_type count;
138  };
139 }
140 
141 #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 JRewindableObjectIterator.
JRandomSampler & getRandomSampler()
Get random sampler.
JRandomSampler(const unsigned int on, const unsigned int off)
Constructor.