Jpp  master_rocky
the software that should make you happy
JRandomTimeslice.hh
Go to the documentation of this file.
1 #ifndef __JTIMESLICE__JRANDOMTIMESLICE__
2 #define __JTIMESLICE__JRANDOMTIMESLICE__
3 
4 #include <vector>
5 #include <set>
6 
7 #include <TRandom3.h>
8 
11 #include "JDetector/JPMTAddress.hh"
12 #include "JDetector/JTimeRange.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace KM3NETDAQ {
23 
25 
26 
27  /**
28  * Timeslice with random data.
29  */
31  public JTimesliceL0
32  {
33  /**
34  * Default constructor.
35  */
37  {}
38 
39 
40  /**
41  * Constructor.
42  *
43  * \param chronometer chronometer
44  * \param simbad detector simulator
45  */
46  JRandomTimeslice(const JDAQChronometer& chronometer,
47  const JDetectorSimulator& simbad)
48  {
49  using namespace JPP;
50 
51  setDAQChronometer(chronometer);
52 
53  if (simbad.hasK40Simulator() &&
54  simbad.hasPMTSimulator() &&
55  simbad.hasCLBSimulator()) {
56 
57  const double Tmin = getTimeSinceRTS(chronometer.getFrameIndex()); // [ns]
58 
59  const JTimeRange period(Tmin, Tmin + getFrameTime()); // [ns]
60 
61  JModuleData buffer;
62 
63  for (JDetector::const_iterator module = simbad->begin(); module != simbad->end(); ++module) {
64 
65  if (!module->empty()) {
66 
67  buffer.reset(module->size());
68 
69  simbad.generateHits(*module, period, buffer);
70 
71  this->push_back(JDAQSuperFrame(JDAQSuperFrameHeader(chronometer, module->getID())));
72 
73  simbad(*module, buffer, *(this->rbegin()));
74  }
75  }
76  }
77  }
78 
79 
80  /**
81  * Recycle time slice by randomly shuffling time intervals of data.
82  *
83  * Hits within one time interval are swapped with hits within another -randomly selected- time interval.\n
84  * Time intervals in which a high-rate veto occurred are excluded.
85  *
86  * Note that the time interval should be:
87  * - (much) larger than time differences of hits in L1 coincidence; and
88  * - (much) smaller than time covered by data (as per KM3NETDAQ::getFrameTime()).
89  *
90  * \param T_ns time interval
91  */
92  void recycle(const double T_ns)
93  {
94  using namespace std;
95  using namespace JPP;
96 
98  typedef JDAQHit::JTDC_t JTDC_t;
99 
100  size_t N = (size_t) (getFrameTime() / T_ns + 0.5); // number of time intervals
101 
102  if (N < 100) { N = 100; } // allow to keep indices at time of high-rate veto
103  if (N > 50000) { N = 50000; } // maximum expected number of hits due to high-rate veto
104 
105  const JTDC_t Ts = (JTDC_t) (getFrameTime() / N); // TDC interval
106 
107  random_indices_t index (N); // indices of time intervals for swapping
108  vector<buffer_type> buffer(N); // data per time interval
109 
110  for (iterator frame = this->begin(); frame != this->end(); ++frame) {
111 
112  if (!frame->empty()) {
113 
114  for (size_t i = 0; i != N; ++i) {
115  buffer[i].clear();
116  }
117 
118  // store data per time interval
119 
121  numeric_limits<JTDC_t>::max()); // maximal time per PMT, e.g. due to high-rate veto
122 
123  for (JDAQSuperFrame::const_iterator hit = frame->begin(); hit != frame->end(); ++hit) {
124 
125  T_max[hit->getPMT()] = hit->getT();
126 
127  const int i = hit->getT() / Ts;
128 
129  buffer[i].push_back(*hit);
130  }
131 
132  set<size_t> keep; // indices corresponding to times of high-rate veto
133 
134  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
135  if (frame->testHighRateVeto(pmt)) {
136  keep.insert(T_max[pmt] / Ts);
137  }
138  }
139 
140  index.random_shuffle(keep); // randomly shuffle values between kept indices
141 
142  // Wall street shuflle
143 
144  JDAQSuperFrame::iterator hit = frame->begin();
145 
146  for (size_t in = 0; in != N; ++in) {
147 
148  const size_t out = index [in];
149  buffer_type& zbuf = buffer[out];
150 
151  const JTDC_t T_in = in * Ts;
152  const JTDC_t T_out = out * Ts;
153 
154  for (buffer_type::iterator i = zbuf.begin(); i != zbuf.end(); ++i, ++hit) {
155  *hit = JDAQHit(i->getPMT(), (i->getT() - T_out) + T_in, i->getToT());
156  }
157  }
158  }
159  }
160  }
161 
162 
163  /**
164  * Auxiliary data structure for randomisation of indices.
165  */
167  public std::vector<size_t>
168  {
169  /**
170  * Constructor.
171  *
172  * \param N number of indices
173  */
174  random_indices_t(const size_t N) :
175  std::vector<size_t>(N)
176  {}
177 
178  /**
179  * Randomly shuffle values between fixed indices.
180  *
181  * \param keep fixed indices
182  */
183  inline void random_shuffle(const std::set<size_t>& keep)
184  {
185  for (size_t i = 0; i != this->size(); ++i) {
186  (*this)[i] = i;
187  }
188 
189  size_t i1 = 0;
190 
191  for (const size_t i2 : keep) {
192 
193  random_shuffle(i1, i2);
194 
195  i1 = i2 + 1;
196  }
197 
198  random_shuffle(i1, this->size());
199  }
200 
201  private:
202  /**
203  * Randomly shuffle values between given indices.
204  *
205  * \param i1 first index (included)
206  * \param i2 last index (excluded)
207  */
208  inline void random_shuffle(const int i1, const int i2)
209  {
210  for (int i = i2 - 1; i > i1; --i) {
211 
212  const int l = i1 + gRandom->Integer(i - i1);
213 
214  std::swap((*this)[i], (*this)[l]);
215  }
216  }
217  };
218  };
219 }
220 
221 #endif
222 
KM3NeT DAQ constants, bit handling, etc.
Auxiliaries for creation of time slice data.
virtual void generateHits(const JModule &module, const JTimeRange &period, JModuleData &output) const override
Generate hits.
bool hasPMTSimulator() const
Check availability of PMT simulator.
bool hasK40Simulator() const
Check availability of K40 simulator.
bool hasCLBSimulator() const
Check availability of CLB simulator.
Data structure for PMT data corresponding to a detector module.
void reset(size_t size)
Reset buffers.
void setDAQChronometer(const JDAQChronometer &chronometer)
Set DAQ chronometer.
int getFrameIndex() const
Get frame index.
Hit data structure.
Definition: JDAQHit.hh:35
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:39
Data frame of one optical module.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:71
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
double getTimeSinceRTS(const int frame_index)
Get time in ns since last RTS for a given frame index.
Definition: JDAQClock.hh:263
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
Definition: JSTDTypes.hh:14
Auxiliary class for TDC constraints.
Definition: JTDC_t.hh:39
Auxiliary data structure for randomisation of indices.
void random_shuffle(const int i1, const int i2)
Randomly shuffle values between given indices.
void random_shuffle(const std::set< size_t > &keep)
Randomly shuffle values between fixed indices.
Timeslice with random data.
JRandomTimeslice(const JDAQChronometer &chronometer, const JDetectorSimulator &simbad)
Constructor.
JRandomTimeslice()
Default constructor.
void recycle(const double T_ns)
Recycle time slice by randomly shuffling time intervals of data.
Base class class for generation of time slice data.
Definition: JTimesliceL0.hh:20