Jpp  18.5.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCLB.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <limits>
5 #include <vector>
6 #include <map>
7 #include <algorithm>
8 
10 
14 
15 #include "JDAQ/JDAQTimesliceIO.hh"
16 
18 #include "JSupport/JSupport.hh"
19 #include "JSupport/JMeta.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 namespace {
26 
27  const uint32_t TDC = 1414808643; //!< TDC data type
28 
30 
31  typedef std::vector<char> buffer_type; //!< UDP packet
32  typedef std::vector<buffer_type> collection_type; //!< collection of UDP packets
33  typedef std::map<int, collection_type> timeslice_type; //!< module identifier -> collection of UDP packets
34 
35 
36  /**
37  * Auxiliary data structure to organise CLB data.
38  */
39  struct map_type :
40  public std::map<uint64_t, timeslice_type> // UTC time -> time slice data
41  {
42  /**
43  * Constructor.
44  *
45  * \param detector detector identifier
46  */
47  map_type(int detector) :
48  detector(detector),
49  frame_index(0)
50  {}
51 
52 
53  /**
54  * Convert first element in container and remove it.
55  *
56  * \return time slice
57  */
58  const JDAQTimeslice& pop()
59  {
60  using namespace std;
61  using namespace KM3NETDAQ;
62 
63  frame_index += 1;
64 
65  timeslice.clear();
66 
67  for (timeslice_type::iterator collection = this->begin()->second.begin(); collection != this->begin()->second.end(); ++collection) {
68 
69  sort(collection->second.begin(), collection->second.end(), compare);
70 
71  const CLBCommonHeader* header = (const CLBCommonHeader*) collection->second.rbegin()->data();
72 
73  const JDAQChronometer chronometer(detector,
74  header->runNumber(),
75  frame_index,
76  JDAQUTCExtended(header->timeStamp().sec(), header->timeStamp().tics()));
77 
78  if (timeslice.empty()) {
79  timeslice = JDAQTimeslice(chronometer);
80  }
81 
82  const uint32_t number_of_packets = collection->second.size();
83  const uint32_t sequence_number = header->udpSequenceNumber();
84 
85  const JDAQFrameStatus status(DAQ_UDP_RECEIVED_PACKETS.write(number_of_packets) | //
86  DAQ_UDP_SEQUENCE_NUMBER .write(sequence_number), // DAQ status
87  header->domStatus(1), // TDC status
88  header->domStatus(2) | //
89  DAQ_UDP_TRAILER.write(sequence_number + 1 == number_of_packets)); // FIFO status
90 
91  JDAQSuperFrame frame(JDAQSuperFrameHeader(chronometer, collection->first, status));
92 
93  for (collection_type::const_iterator i = collection->second.begin(); i != collection->second.end(); ++i) {
94 
95  const size_t size = (i->size() - sizeof(CLBCommonHeader)) / sizeof(JDAQHit);
96  const JDAQHit* data = (const JDAQHit*) (i->data() + sizeof(CLBCommonHeader));
97 
98  frame.add(size, data);
99  }
100 
101  timeslice.push_back(frame);
102  }
103 
104  this->erase(this->begin());
105 
106  return timeslice;
107  }
108 
109 
110  /**
111  * Compare two frames by UDP sequence number.
112  *
113  * \param first first UDP packet
114  * \param second second UDP packet
115  * \return true if first packet before second; else false
116  */
117  static inline bool compare(const buffer_type& first,const buffer_type& second)
118  {
119  return (((const CLBCommonHeader*) first .data())->udpSequenceNumber() <
120  ((const CLBCommonHeader*) second.data())->udpSequenceNumber());
121  }
122 
123  private:
124  int detector;
125  int frame_index;
126  JDAQTimeslice timeslice;
127  };
128 }
129 
130 
131 /**
132  * \file
133  *
134  * Auxiliary program to convert raw CLB data to KM3NETDAQ::JDAQTimeslice data.
135  * \author mdejong
136  */
137 int main(int argc, char **argv)
138 {
139  using namespace std;
140  using namespace JPP;
141  using namespace KM3NETDAQ;
142 
143 
144  string inputFile;
145  int numberOfEvents;
147  int detector;
148  size_t queue;
149  int debug;
150 
151  try {
152 
153  JParser<> zap("Auxiliary program to convert raw CLB data to KM3NETDAQ::JDAQTimeslice data.");
154 
155  zap['f'] = make_field(inputFile, "input file).");
156  zap['o'] = make_field(outputFile, "output file.") = "clb.root";
157  zap['n'] = make_field(numberOfEvents) = numeric_limits<int>::max();
158  zap['Q'] = make_field(queue, "queue depth") = 1000;
159  zap['D'] = make_field(detector, "detector identifier") = 1;
160  zap['d'] = make_field(debug, "debug.") = 1;
161 
162  zap(argc, argv);
163  }
164  catch(const exception& error) {
165  FATAL(error.what() << endl);
166  }
167 
168 
169  outputFile.open();
170 
171  if (!outputFile.is_open()) {
172  FATAL("Error opening file " << outputFile << endl);
173  }
174 
175  outputFile.put(JMeta(argc, argv));
176 
178 
179  uint32_t run = numeric_limits<uint32_t>::max();
180  uint32_t size;
181 
182  ifstream in(inputFile.c_str(), ios::binary);
183 
184  for (int count = 0; count != numberOfEvents && in.read((char*) &size, sizeof(uint32_t)); ++count) {
185 
186  STATUS(setw(8) << count << '\r'); DEBUG(endl);
187 
188  buffer_type buffer(size);
189 
190  if (in.read(buffer.data(), size)) {
191 
192  if (size >= sizeof(CLBCommonHeader)) {
193 
194  const CLBCommonHeader* header = (const CLBCommonHeader*) buffer.data();
195 
196  if (run == numeric_limits<uint32_t>::max()) {
197  run = header->runNumber();
198  }
199 
200  DEBUG("data: "
201  << setw(8) << header->runNumber() << ' '
202  << setw(9) << header->domIdentifier() << ' '
203  << setw(2) << header->udpSequenceNumber() << endl);
204 
205  if (header->dataType() == TDC) {
206 
207  if (header->runNumber() == run) {
208 
209  data[header->timeStamp().inMilliSeconds()][header->domIdentifier()].push_back(buffer);
210 
211  while (data.size() > queue) {
212  outputFile.put(data.pop());
213  }
214 
215  } else {
216 
217  WARNING("Run numbers differ " << run << ' ' << header->runNumber() << " -> skip data." << endl);
218  }
219  }
220  }
221  }
222  }
223  STATUS(endl);
224 
225  in.close();
226 
227  while (!data.empty()) {
228  outputFile.put(data.pop());
229  }
230 
231  outputFile.close();
232 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
static const JBits DAQ_UDP_RECEIVED_PACKETS(0, 15)
Mask of UDP received packets.
Utility class to parse command line options.
Definition: JParser.hh:1514
void clear()
Clear data.
#define WARNING(A)
Definition: JMessage.hh:65
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
int write(const int value) const
Write given value as bit mask.
Definition: JDAQ.hh:115
uint32_t domIdentifier() const
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
int write(const int value) const
Write given value as bit mask.
Definition: JDAQ.hh:238
uint32_t runNumber() const
UTCTime timeStamp() const
string outputFile
Data structure for UTC time.
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:70
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Detector file.
Definition: JHead.hh:226
Hit data structure.
Definition: JDAQHit.hh:34
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
ROOT I/O of application specific meta data.
Data time slice.
uint32_t udpSequenceNumber() const
static const JBit DAQ_UDP_TRAILER(31)
UDP trailer.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
uint32_t dataType() const
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
Utility class to parse command line options.
then echo Submitting reweighting and histogram comparison jobs to nikhef stbc batch queue
set_variable TDC
Definition: JPrintTDC.sh:20
static const JBits DAQ_UDP_SEQUENCE_NUMBER(16, 31)
Mask of UDP sequence number.
std::map< int, range_type > map_type
do set_variable DETECTOR_TXT $WORKDIR detector
Data frame of one optical module.
uint64_t inMilliSeconds() const
Definition: utctime.hh:27
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62