Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
raw_data_converter.cpp
Go to the documentation of this file.
1 /**
2 
3  Program name: RawDataConverter
4 
5  Description:
6  This program converts dump raw data into the DAQ frame format.
7 
8  Use
9  $ RawDataConverter -h
10  for a detailed help.
11 
12  Author: Carmelo Pellegrino
13  E-mail: carmelo.pellegrino@bo.infn.it
14  Date: 29 April 2015
15 
16  \author Carmelo Pellegrino
17 */
18 
19 #include <iostream>
20 #include <iomanip>
21 #include <algorithm>
22 #include <string>
23 #include <fstream>
24 #include <list>
25 #include <cstring>
26 
29 #include <FrameFactory/frame.hh>
33 
34 #include <boost/program_options.hpp>
35 namespace po = boost::program_options;
36 
37 inline
38 int64_t abs_time(Packet const& p)
39 {
40  return p.CLBHeader()->timeStamp().tics() * 16 + p.CLBHeader()->timeStamp().sec() * 1000000000LL;
41 }
42 
43 struct ID {
44  int64_t abs_time;
45  uint32_t domid;
46 };
47 
48 bool operator ==(Packet const& p, ID const& id)
49 {
50  int64_t const time = abs_time(p);
51  uint32_t const domid = p.CLBHeader()->domIdentifier();
52 
53  return time == id.abs_time && domid == id.domid;
54 }
55 
56 int main(int argc, char* argv[])
57 {
58  std::string input_filename;
59  std::string output_filename;
60  uint64_t run_start_time = 0;
61  int timeslice_duration = 100;
62  unsigned int detector_id = 0;
63 
64  po::options_description desc("Options");
65  desc.add_options()
66  ("help,h", "Print this help and exit.")
67  ("input,i",
68  po::value<std::string>(&input_filename)->required(),
69  "Set the name of the dump data file.")
70  ("output,o",
71  po::value<std::string>(&output_filename)->required(),
72  "Set the name of the output DAQ file.")
73  ("runstart,s",
74  po::value<uint64_t>(&run_start_time)->default_value(run_start_time),
75  "Set the run start time.")
76  ("timeslice,t",
77  po::value<int>(&timeslice_duration)->default_value(timeslice_duration),
78  "Set the value of the time slice duration in milliseconds.")
79  ("detector-id,",
80  po::value<unsigned int>(&detector_id)->default_value(detector_id),
81  "Set the detector id.");
82 
83  try {
84  po::variables_map vm;
85  po::store(
86  po::command_line_parser(argc, argv).options(desc).run(),
87  vm);
88 
89  if (vm.count("help")) {
90  std::cout << desc << std::endl;
91  return EXIT_SUCCESS;
92  }
93 
94  po::notify(vm);
95  } catch (const po::error& e) {
96  std::cerr << "RawDataConverter: Error: " << e.what() << '\n'
97  << desc << std::endl;
98  return EXIT_FAILURE;
99  } catch (const std::runtime_error& e) {
100  std::cerr << "RawDataConverter: Error: " << e.what() << '\n'
101  << desc << std::endl;
102  return EXIT_FAILURE;
103  }
104 
105  DumpFile df(input_filename);
106 
107  assert(df.size() && "It seems that the file that you provided is empty");
108 
109  std::list<Packet> all_data;
110 
111  std::set<uint32_t> dom_ids;
112  std::set<int64_t> times;
113 
114  for (DumpFile::iterator it = df.begin(), et = df.end(); it != et; ++it) {
115  Packet const p = *it;
116 
117  dom_ids.insert(p.CLBHeader()->domIdentifier());
118 
119  int64_t const time = abs_time(p);
120  times.insert(time);
121 
122  all_data.push_back(*it);
123  }
124 
125  std::cout << all_data.size() << " datagrams found\n";
126 
127  std::ofstream outfile(output_filename.c_str());
128 
129  int const print_suppression_factor = all_data.size() > 50 ? all_data.size() / 50 : 1;
130 
131  Frame frame;
132 
133  PuzzledFrame pframe;
134 
135  ID id;
136 
137  for (std::set<int64_t>::const_iterator tit = times.begin(), tet = times.end(); tit != tet ; ++tit) {
138  id.abs_time = *tit;
139  for (std::set<uint32_t>::const_iterator it = dom_ids.begin(), et = dom_ids.end(); it != et; ++it) {
140  id.domid = *it;
141 
142  std::list<Packet>::iterator dg = all_data.begin();
143  bool found = false;
144 
145  while (true) {
146  dg = std::find(dg, all_data.end(), id);
147 
148  if (dg == all_data.end()) {
149  break;
150  }
151 
152  found = true;
153  Packet const& p = *dg;
154 
156 
157  std::memcpy(datagram->raw(), p.data(), p.size());
158  datagram->resize(p.size());
159 
160  pframe.setDataType(p.CLBHeader()->dataType());
161  pframe.setDetectorId(detector_id);
162 
163  std::pair<uint32_t, bool> const seq_num = seq_number(
164  *p.CLBHeader(),
165  run_start_time,
166  timeslice_duration
167  );
168 
169  if (seq_num.second) {
170  pframe.setSeqNumber(seq_num.first);
171 
172  pframe.setFrameIndex(
173  data2idx(
174  *p.CLBHeader(),
175  run_start_time,
176  timeslice_duration
177  )
178  );
179 
180  pframe.insert(datagram);
181  } else {
182  std::cerr
183  << "Datagram timestamp before the start of the run:\n"
184  << *p.CLBHeader() << '\n';
185  }
186 
187  all_data.erase(dg++);
188  }
189 
190  if (all_data.size() % print_suppression_factor == 0)
191  std::cout << std::setfill(' ')
192  << std::setw(3)
193  << 100 - static_cast<int>(all_data.size() * 100. / df.size())
194  << "%\r" << std::flush;
195 
196  if (found) {
197  pframe.getFrame(frame);
198  if (frame.size() && frame.getNItems()) outfile.write(frame.data(), frame.size());
199  }
200  pframe.reset();
201  }
202  }
203 }
uint32_t domid
int main(int argc, char *argv[])
Definition: Main.cc:15
uint32_t domIdentifier() const
uint32_t sec() const
Definition: utctime.hh:17
void resize(size_t s)
Definition: clb_datagram.hh:54
int64_t abs_time(Packet const &p)
Program name: RawDataConverter.
frame_idx_t data2idx(CLBCommonHeader const &header, uint64_t start_run_ms, int ts_duration_ms)
UTCTime timeStamp() const
bool insert(CLBDataGram *datagram)
iterator begin()
Definition: dqdumpfile.hh:155
std::size_t size() const
Definition: dqdumpfile.hh:178
void setDetectorId(unsigned int detector_id)
std::size_t size() const
Definition: dqdumpfile.hh:49
then usage $script[port]< option > nPossible options
bool operator==(Packet const &p, ID const &id)
unsigned int getNItems() const
Definition: frame.hh:39
char * raw()
Definition: clb_datagram.hh:65
void setDataType(unsigned int datatype)
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
then awk string
char * data()
Definition: dqdumpfile.hh:54
void setFrameIndex(frame_idx_t frame_idx)
CLBDataGram * getEmptyDataGram()
uint32_t tics() const
Definition: utctime.hh:22
static InBufferCollector & getCollector()
uint32_t dataType() const
std::pair< uint32_t, bool > seq_number(CLBCommonHeader const &header, uint64_t start_run_ms, int ts_duration_ms)
iterator end()
Definition: dqdumpfile.hh:160
int64_t abs_time
CLBCommonHeader const * CLBHeader() const
Definition: dqdumpfile.hh:37
Template Frame for ARS data.
Definition: frame.hh:12
void getFrame(Frame &frame)
void setSeqNumber(unsigned int seqnumber)