Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
9 #include "JDAQ/JDAQClock.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JDETECTOR {}
17 namespace JPP { using namespace JDETECTOR; }
18 
19 namespace JDETECTOR {
20 
21  using KM3NETDAQ::JDAQHit;
23  using KM3NETDAQ::getRTS;
24 
25 
26  /**
27  * Default CLB simulation.
28  *
29  * This class provides for a default implementation of the JCLBSimulator interface
30  * which is based on a simulation of the TDC and the state machine inside the CLB.
31  * In this, the number of hits may actually change due to the dynamic range of
32  * the TDC (loss of hits) and the dynamic range of the time-over-threshold (gain
33  * of hits).
34  * The nested class JStateMachine constitutes a user interface for the simulation
35  * of the state machine through method JStateMachine::maybeSwapped().
36  * With the default implementation, the overall time ordering if hits is maintained.
37  * For a realistic simulation of the CLB, a pointer to an implementation of this
38  * interface should be provided.
39  */
41  public JCLBSimulator
42  {
43  protected:
44 
48 
49 
50  public:
51  /**
52  * Interface for TDC.
53  */
54  class JTDC {
55  public:
56  /**
57  * Virtual destructor.
58  */
59  virtual ~JTDC()
60  {}
61 
62 
63  /**
64  * Make DAQ hit.
65  *
66  * \param pmt PMT channel
67  * \param t_ns time of hit [ns]
68  * \param tot_ns time over threshold [ns]
69  * \return DAQ hit
70  */
71  virtual JDAQHit makeHit(const JPMT_t pmt,
72  const double t_ns,
73  const JTOT_t tot_ns) const
74  {
75  return JDAQHit(pmt, (JTDC_t) t_ns, tot_ns);
76  }
77  };
78 
79 
80  /**
81  * Interface to mimic hit ordering effects due to state machine inside CLB.
82  */
83  class JStateMachine {
84  public:
85  /**
86  * Virtual destructor.
87  */
88  virtual ~JStateMachine()
89  {}
90 
91 
92  /**
93  * Test whether two consecutive hits may be swapped.
94  *
95  * \param first first DAQ hit
96  * \param second second DAQ hit
97  * \return true if first and second hit may be swapped; else false
98  */
99  virtual bool maybeSwapped(const JDAQHit& first, const JDAQHit& second) const
100  {
101  return false;
102  }
103  };
104 
105 
106  /**
107  * Constructor.
108  * N.B: This class owns the object pointed to using JSinglePointer<>.
109  *
110  * \param __TDC pointer TDC simulator
111  * \param __state_machine pointer state machine
112  */
114  JCLBSimulator(),
115  TDC (__TDC),
116  state_machine(__state_machine)
117  {}
118 
119 
120  /**
121  * Process data.
122  *
123  * \param input PMT data
124  * \param output CLB data
125  */
126  virtual void processData(const JCLBInput& input,
127  JDAQFrame& output) const
128  {
129  using namespace std;
130 
131 
132  const JTDC_t TMIN = 0; // [ns]
133  const JTDC_t TMAX = (JTDC_t) getRTS(); // [ns]
134 
135  buffer.clear();
136 
137 
138  for (JPMT_t pmt = 0; pmt != (JPMT_t) input.size(); ++pmt) {
139 
140 
141  const JPMTData<JPMTPulse>& in = input[pmt];
142 
143 
144  // TDC
145 
146  JPMTData<JPMTPulse>::const_iterator hit = in.begin();
147 
148  for ( ; hit != in.end() && hit->t_ns < TMIN; ++hit) {}
149 
150  for ( ; hit != in.end() && hit->t_ns < TMAX; ++hit) {
151 
152  double t1 = hit->t_ns;
153  double tot = hit->tot_ns;
154 
155  // generate multiple hits if time-over-threshold exceeds dynamic range
156 
157  while (tot > JDAQHit::getMaximalToT()) {
158 
159  buffer.push_back(TDC->makeHit(pmt, t1, JDAQHit::getMaximalToT()));
160 
161  t1 += JDAQHit::getMaximalToT();
162  tot -= JDAQHit::getMaximalToT();
163  }
164 
165  if (tot > getMinimalToT()) {
166  buffer.push_back(TDC->makeHit(pmt, t1, (JTOT_t) (tot + 0.5)));
167  }
168  }
169  }
170 
171 
172  if (buffer.size() > 1) {
173 
174  // process data through state machine
175 
176  sort(buffer.begin(), buffer.end());
177 
178  for (std::vector<JDAQHit>::iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
179 
180  if (state_machine->maybeSwapped(*p,*q)) {
181  iter_swap(p,q);
182  }
183  }
184  }
185 
186 
187  output.add(buffer.size(), buffer.data());
188  }
189 
190 
191  /**
192  * Get maximal pulse length of time-over-threshold measurement.
193  *
194  * \return TDC value [ns]
195  */
197  {
198  return 0xFE;
199  }
200 
201 
202  /**
203  * Get minimal pulse length of time-over-threshold measurement.
204  *
205  * \return TDC value [ns]
206  */
207  static double getMinimalToT()
208  {
209  return 0.5;
210  }
211 
212  private:
216  };
217 }
218 
219 #endif
Interface to mimic hit ordering effects due to state machine inside CLB.
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:41
virtual bool maybeSwapped(const JDAQHit &first, const JDAQHit &second) const
Test whether two consecutive hits may be swapped.
The template JSinglePointer class can be used to hold a pointer to an object.
unsigned char JTOT_t
time over threshold [ns]
Definition: JDAQHit.hh:42
Hit data structure.
Definition: JDAQHit.hh:36
Data frame.
Definition: JDAQFrame.hh:70
JLANG::JSinglePointer< JStateMachine > state_machine
JDAQFrame & add(const JDAQFrame &frame)
Add data.
Definition: JDAQFrame.hh:193
virtual JDAQHit makeHit(const JPMT_t pmt, const double t_ns, const JTOT_t tot_ns) const
Make DAQ hit.
JCLBDefaultSimulatorInterface(JTDC *__TDC, JStateMachine *__state_machine)
Constructor.
double getRTS()
Get TDC dynamic range.
Definition: JDAQClock.hh:173
Template data structure for PMT I/O.
virtual void processData(const JCLBInput &input, JDAQFrame &output) const
Process data.
std::vector< JElement_t >::const_iterator const_iterator
static JTOT_t getMaximalToT()
Get maximal pulse length of time-over-threshold measurement.
Interface for CLB simulation.
unsigned char JPMT_t
PMT channel in FPGA.
Definition: JDAQHit.hh:40
static JTOT_t getMaximalToT()
Get maximal time-over-threshold.
Definition: JDAQHit.hh:107
static double getMinimalToT()
Get minimal pulse length of time-over-threshold measurement.