Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JCLBDefaultSimulatorInterface.hh
Go to the documentation of this file.
1#ifndef __JDETECTOR__JCLBDEFAULTSIMULATORINTERFACE__
2#define __JDETECTOR__JCLBDEFAULTSIMULATORINTERFACE__
3
4#include <vector>
5#include <algorithm>
6
7#include "TRandom3.h"
8
14
16#include "JDAQ/JHighRateVeto.hh"
21
22
23/**
24 * \author mdejong
25 */
26
27namespace JDETECTOR {}
28namespace JPP { using namespace JDETECTOR; }
29
30namespace JDETECTOR {
31
34
35
36 /**
37 * Default CLB simulation.
38 *
39 * This class provides for a default implementation of the JCLBSimulator interface
40 * which is based on a simulation of the TDC and the state machine inside the CLB.
41 * The actual number of hits may change due to the high-rate veto and UDP packet loss
42 * (loss of hits) and the dynamic range of the time-over-threshold (gain of hits).
43 *
44 * The nested class JStateMachine constitutes a user interface for the simulation
45 * of the state machine through method JStateMachine::maybeSwapped().
46 * With the default implementation, the overall time ordering if hits is maintained.
47 * For a realistic simulation of the CLB, a pointer to a designated implementation
48 * of this interface should be provided.
49 *
50 * The implementation of the virtual method JCLBDefaultSimulatorInterface::processData
51 * provides for the settings of the status of the data frame.\n
52 * In this, the high-rate veto is set when:
53 * - virtual method JCLBDefaultSimulatorInterface::getHighRateVeto returns true for the given PMT; or
54 * - number of hits from the PMT exceeds KM3NETDAQ::getMaximalNumberOfHits.
55 */
57 public JCLBSimulator
58 {
59 public:
60
64
65
66 /**
67 * Interface for TDC.
68 */
69 class JTDC {
70 public:
71 /**
72 * Virtual destructor.
73 */
74 virtual ~JTDC()
75 {}
76
77
78 /**
79 * Make DAQ hit.
80 *
81 * \param pmt PMT channel
82 * \param t_ns time of hit [ns]
83 * \param tot_ns time over threshold [ns]
84 * \return DAQ hit
85 */
86 virtual JDAQHit makeHit(const JPMT_t pmt,
87 const double t_ns,
88 const JTOT_t tot_ns) const
89 {
90 return JDAQHit(pmt, (JTDC_t) t_ns, tot_ns);
91 }
92 };
93
94
95 /**
96 * Interface to mimic hit ordering effects due to state machine inside CLB.
97 */
99 public:
100 /**
101 * Virtual destructor.
102 */
104 {}
105
106
107 /**
108 * Test whether two consecutive hits may be swapped.
109 *
110 * \param first first DAQ hit
111 * \param second second DAQ hit
112 * \return true if first and second hit may be swapped; else false
113 */
114 virtual bool maybeSwapped(const JDAQHit& first, const JDAQHit& second) const
115 {
116 return false;
117 }
118 };
119
120
121 /**
122 * Constructor.
123 *
124 * This class owns the objects pointed to.
125 *
126 * \param TDC pointer to TDC simulator
127 * \param state_machine pointer to state machine
128 */
134
135
136
137 /**
138 * Get DAQ frame status of given module.
139 *
140 * \param id module identifier
141 * \return DAQ frame status
142 */
144 {
145 using namespace KM3NETDAQ;
146
149 int status = DAQ_WHITE_RABBIT.write(1); // TDC status
150 int fifo = DAQ_UDP_TRAILER .write(hasUDPTrailer(id) ? 1 : 0); // FIFO status
151
152 for (size_t pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
153 JBit(pmt).set(status, getHighRateVeto(JPMTIdentifier(id,pmt)) ? 1 : 0) ; // high-rate veto
154 }
155
156 return JDAQFrameStatus(daq, status, fifo);
157 }
158
159
160 /**
161 * Process data.
162 *
163 * \param id module identifier
164 * \param input PMT data
165 * \param output CLB data
166 */
167 virtual void processData(const JModuleIdentifier& id, const JCLBInput& input, JDAQSuperFrame& output) const override
168 {
169 using namespace std;
170 using namespace JPP;
171 using namespace KM3NETDAQ;
172
173
174 const double Tmin = 0.0; // Minimal TDC value [ns]
175 const double Tmax = getFrameTime(); // Maximal TDC value [ns]
176
178
179 buffer.clear();
180
181 size_t ns = 0; // current number of hits in data frame
182
183 for (size_t pmt = 0; pmt != input.size(); ++pmt) {
184
185 const JPMTData<JPMTPulse>& in = input[pmt];
186
187 // TDC
188
189 for (JPMTData<JPMTPulse>::const_iterator hit = in.begin(); hit != in.end(); ++hit) {
190
191 if (hit->t_ns >= Tmin && hit->t_ns <= Tmax) {
192
193 double t1 = hit->t_ns;
194 double tot = hit->tot_ns;
195
196 // generate multiple hits if time-over-threshold exceeds dynamic range
197
198 while (tot > JDAQHit::getMaximalToT()) {
199
200 buffer.push_back(TDC->makeHit((JPMT_t) pmt, t1, (JTOT_t) JDAQHit::getMaximalToT()));
201
203 tot -= JDAQHit::getMaximalToT();
204 }
205
206 if (tot > getMinimalToT()) {
207 buffer.push_back(TDC->makeHit((JPMT_t) pmt, t1, (JTOT_t) (tot + 0.5)));
208 }
209 }
210 }
211
212 if (buffer.size() > ns + getMaximalNumberOfHits()) {
213
214 output.setHighRateVeto(pmt, true);
215
216 buffer.resize(ns + getMaximalNumberOfHits());
217 }
218
219 inplace_merge(buffer.begin(), buffer.begin() + ns, buffer.end());
220
221 ns = buffer.size();
222 }
223
224
225 // simulate UDP packet loss
226
227 const int n1 = getUDPNumberOfReceivedPackets(id);
228 const int n2 = getUDPMaximalSequenceNumber (id);
229
230 if (n1 < n2 + 1) {
231
232 const int ns = getBayesianMedian(n2, n1, 0);
233
234 JTDC_t t0 = (JTDC_t) Tmin;
235 JTDC_t ts = (JTDC_t) ((Tmax - Tmin) / ns);
236
237 for (int i = 0; i != ns; ++i, t0 += ts) {
238
239 if (gRandom->Rndm() * ns >= (double) n1) {
240
241 std::vector<JDAQHit>::iterator p = lower_bound(buffer.begin(), buffer.end(), t0, compare);
242 std::vector<JDAQHit>::iterator q = lower_bound(buffer.begin(), buffer.end(), t0 + ts, compare);
243
244 buffer.erase(p,q);
245 }
246 }
247 }
248
249
250 // process data through state machine
251
252 if (buffer.size() > 1) {
253
254 for (std::vector<JDAQHit>::iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
255
256 if (state_machine->maybeSwapped(*p,*q)) {
257 iter_swap(p,q);
258 }
259 }
260 }
261
262
263 // store data
264
265 output.add(buffer.size(), buffer.data());
266 }
267
268
269 /**
270 * Get number of received UDP packets.
271 *
272 * \param id module identifier
273 * \return 2
274 */
276 {
277 return 2;
278 }
279
280
281 /**
282 * Get maximal sequence number of UDP packet.
283 *
284 * \param id module identifier
285 * \return 1
286 */
288 {
289 return 1;
290 }
291
292
293 /**
294 * Get UDP trailer status.
295 *
296 * \param id module identifier
297 * \return true
298 */
299 virtual bool hasUDPTrailer(const JModuleIdentifier& id) const
300 {
301 return true;
302 }
303
304
305 /**
306 * Get high-rate veto of given PMT.
307 *
308 * \param id PMT identifier
309 * \return false
310 */
311 virtual bool getHighRateVeto(const JPMTIdentifier& id) const
312 {
313 return false;
314 }
315
316
317 /**
318 * Get minimal pulse length of time-over-threshold measurement.
319 *
320 * \return TDC value [ns]
321 */
322 static double getMinimalToT()
323 {
324 return 0.5;
325 }
326
327
328 /**
329 * Auxiliary data structure for sorting of hits.
330 */
331 static const struct compare {
332 /**
333 * Compare hits by time.
334 *
335 * \param first first hit
336 * \param second second hit
337 * \return true if first earlier than second; else false
338 */
339 bool operator()(const JDAQHit& first, const JDAQHit& second) const
340 {
341 return first.getT() < second.getT();
342 }
343
344
345 /**
346 * Compare hit and TDC value.
347 *
348 * \param hit hit
349 * \param tdc TDC
350 * \return true if hit earlier than given TDC value; else false
351 */
352 bool operator()(const JDAQHit& hit, const JTDC_t tdc) const
353 {
354 return hit.getT() < tdc;
355 }
357
358
359 private:
363 };
364}
365
366#endif
KM3NeT DAQ constants, bit handling, etc.
Auxiliary methods for mathematics.
Interface to mimic hit ordering effects due to state machine inside CLB.
virtual bool maybeSwapped(const JDAQHit &first, const JDAQHit &second) const
Test whether two consecutive hits may be swapped.
virtual JDAQHit makeHit(const JPMT_t pmt, const double t_ns, const JTOT_t tot_ns) const
Make DAQ hit.
static const struct JDETECTOR::JCLBDefaultSimulatorInterface::compare compare
JLANG::JSinglePointer< JStateMachine > state_machine
static double getMinimalToT()
Get minimal pulse length of time-over-threshold measurement.
JDAQFrameStatus getDAQFrameStatus(const JModuleIdentifier &id) const
Get DAQ frame status of given module.
virtual bool hasUDPTrailer(const JModuleIdentifier &id) const
Get UDP trailer status.
virtual int getUDPMaximalSequenceNumber(const JModuleIdentifier &id) const
Get maximal sequence number of UDP packet.
virtual int getUDPNumberOfReceivedPackets(const JModuleIdentifier &id) const
Get number of received UDP packets.
JCLBDefaultSimulatorInterface(JTDC *TDC, JStateMachine *state_machine)
Constructor.
virtual bool getHighRateVeto(const JPMTIdentifier &id) const
Get high-rate veto of given PMT.
virtual void processData(const JModuleIdentifier &id, const JCLBInput &input, JDAQSuperFrame &output) const override
Process data.
Interface for CLB simulation.
Template data structure for PMT I/O.
std::vector< JElement_t >::const_iterator const_iterator
Auxiliary class for object identification.
Definition JObjectID.hh:25
The template JSinglePointer class can be used to hold a pointer to an object.
void setHighRateVeto(const int tdc, const bool value)
Set high-rate veto.
void setDAQFrameStatus(const JDAQFrameStatus &status)
Set DAQ frame status.
Hit data structure.
Definition JDAQHit.hh:35
unsigned int JTDC_t
leading edge [ns]
Definition JDAQHit.hh:39
unsigned char JTOT_t
time over threshold [ns]
Definition JDAQHit.hh:40
static JTOT_t getMaximalToT()
Get maximal time-over-threshold.
Definition JDAQHit.hh:108
JTDC_t getT() const
Get time.
Definition JDAQHit.hh:86
unsigned char JPMT_t
PMT channel in FPGA.
Definition JDAQHit.hh:38
Data frame of one optical module.
JDAQSuperFrame & add(const JDAQSuperFrame &super_frame)
Add data from same optical module.
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition io_ascii.hh:155
file Auxiliary data structures and methods for detector calibration.
Definition JAnchor.hh:12
size_t getBayesianMedian(const size_t m, const size_t k)
Get estimate of maximum number.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void inplace_merge(T __begin, const size_t N, const size_t *delimiter)
Merge multiple sorted ranges.
Definition JMergeSort.cc:29
KM3NeT DAQ data structures and auxiliaries.
Definition DataQueue.cc:39
size_t getMaximalNumberOfHits()
Get maximal number of hits from one PMT within data frame.
double getFrameTime()
Get frame time duration.
Definition JDAQClock.hh:162
static const JBits DAQ_UDP_RECEIVED_PACKETS(0, 15)
Mask of UDP received packets.
static const JBits DAQ_UDP_SEQUENCE_NUMBER(16, 31)
Mask of UDP sequence number.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition JDAQ.hh:26
static const JBit DAQ_WHITE_RABBIT(31)
White Rabbit status.
static const JBit DAQ_UDP_TRAILER(31)
UDP trailer.
bool operator()(const JDAQHit &first, const JDAQHit &second) const
Compare hits by time.
bool operator()(const JDAQHit &hit, const JTDC_t tdc) const
Compare hit and TDC value.
Wrapper for CLB input.
Auxiliary data structure for single bit.
Definition JDAQ.hh:36
int write(const int value) const
Write given value as bit mask.
Definition JDAQ.hh:115
void set(int &mask) const
Set bit in given bit mask.
Definition JDAQ.hh:77
int write(const int value) const
Write given value as bit mask.
Definition JDAQ.hh:238