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