DUNE_FelixFragment_t.cc
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <bitset>
3 #include <fstream>
4 #include <iostream>
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 #include "artdaq-core/Data/Fragment.hh"
13 
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wsign-compare"
16 
17 #define BOOST_TEST_MODULE(MilliSlice_t)
19 
20 BOOST_AUTO_TEST_SUITE(FelixFragment_test)
21 
22 BOOST_AUTO_TEST_CASE(BaselineTest) {
23  // Get all files.
24  // std::vector<int> event_nums = {3059515, 3059537, 3059542, 3059574, 3059575,
25  // 3059577, 3059599, 3059603, 3059620, 3059622};
26  std::vector<int> event_nums = {7258, 7263, 7264, 7269, 7276, 7283, 7284, 7287, 7294, 7296};
27  std::vector<std::string> filenames;
28  for (auto event : event_nums) {
29  for (unsigned p = 0; p < 3; ++p) {
30  for (unsigned f = 1; f < 10; ++f) {
31  filenames.push_back(
32  "/nfs/sw/felix/fragment-ana/uBdat/Run_8700-SubRun_145-Event_" +
33  std::to_string(event) + "-Plane_" + std::to_string(p) + "-Frame_0" +
34  std::to_string(f) + ".dat");
35  }
36  if (p == 2) {
37  for (unsigned f = 10; f < 14; ++f) {
38  filenames.push_back(
39  "/nfs/sw/felix/fragment-ana/uBdat/Run_8700-SubRun_145-Event_" +
40  std::to_string(event) + "-Plane_" + std::to_string(p) +
41  "-Frame_" + std::to_string(f) + ".dat");
42  }
43  }
44  }
45  }
46 
47  // Write a file with results.
48  std::ofstream ofile("prev_compression_results.dat");
49  ofile << "#Compression factor\tCompression time\tNoise RMS\n";
50 
51  // Averaging values of different files.
52  std::vector<double> comp_factors;
53  std::vector<size_t> comp_times;
54 
55  for (auto filename : filenames) {
56  // std::string filename = filenames[0];
57  std::ifstream in(filename, std::ios::binary);
58  if (!in.is_open()) {
59  std::cout << "Could not open file " << filename << ".\n";
60  return;
61  }
62  std::cout << "Reading from " << filename << ".\n";
63  std::string contents((std::istreambuf_iterator<char>(in)),
64  (std::istreambuf_iterator<char>()));
65 
67  std::unique_ptr<artdaq::Fragment> frag_ptr(artdaq::Fragment::FragmentBytes(
68  contents.size(), 1, 1, dune::toFragmentType("FELIX"), meta));
69  frag_ptr->resizeBytes(contents.size());
70  memcpy(frag_ptr->dataBeginBytes(), contents.c_str(), contents.size());
71  in.close();
72 
73  dune::FelixFragment flxfrg(*frag_ptr);
74 
75  std::cout << "### WOOF -> Test for the presence of 6000 frames...\n";
76  const size_t frames = 6000;
77 
78  std::cout << " -> Total words: " << flxfrg.total_words() << '\n';
79  std::cout << " -> Total frames: " << flxfrg.total_frames() << '\n';
80  std::cout << " -> Total adc values: " << flxfrg.total_adc_values() << '\n';
81 
82  BOOST_REQUIRE_EQUAL(flxfrg.total_words(), frames * 116);
83  BOOST_REQUIRE_EQUAL(flxfrg.total_frames(), frames);
84  BOOST_REQUIRE_EQUAL(flxfrg.total_adc_values(), frames * 256);
85  std::cout << "\n\n";
86 
87  std::cout << "### WOOF -> WIB frame test.\n";
88  BOOST_REQUIRE_EQUAL(sizeof(dune::FelixFrame), 464);
89  std::cout << " -> SOF: " << unsigned(flxfrg.sof(0)) << "\n";
90  std::cout << " -> Version: " << unsigned(flxfrg.version(0)) << "\n";
91  std::cout << " -> FiberNo: " << unsigned(flxfrg.fiber_no(0)) << "\n";
92  std::cout << " -> SlotNo: " << unsigned(flxfrg.slot_no(0)) << "\n";
93  std::cout << " -> CrateNo: " << unsigned(flxfrg.crate_no(0)) << "\n";
94  std::cout << " -> Timestamp: " << std::hex << flxfrg.timestamp(0)
95  << std::dec;
96  std::cout << "\n\n";
97 
98  // Noise characterisation per channel, then average over fragment.
99  double frag_rms = 0;
100  std::vector<double> chan_avg(256,0);
101  std::vector<double> chan_rms(256,0);
102  // Determine the average per channel.
103  for(unsigned vi = 0; vi < 256; ++vi) {
104  for(unsigned ti = 1; ti < frames; ++ti) {
105  chan_avg[vi] += (double)flxfrg.get_ADC(ti, vi) - flxfrg.get_ADC(ti-1, vi);
106  }
107  chan_avg[vi] /= frames;
108  }
109  // Determine the RMS per channel.
110  for(unsigned vi = 0; vi < 256; ++vi) {
111  for(unsigned ti = 1; ti < frames; ++ti) {
112  chan_rms[vi] += pow(flxfrg.get_ADC(ti, vi) - flxfrg.get_ADC(ti-1, vi) - chan_avg[vi], 2);
113  }
114  chan_rms[vi] = sqrt(chan_rms[vi]/(frames-2));
115  frag_rms += chan_rms[vi];
116  }
117  // Determine the average RMS per fragment.
118  frag_rms /= 256;
119  std::cout << "FRAGMENT RMS: " << frag_rms << '\n';
120 
121  // Compression tests.
122  std::cout << "### MEOW -> WIB frame compression test.\n";
123 
124  auto comp_begin = std::chrono::high_resolution_clock::now();
125  std::vector<char> compfrg(dune::FelixCompress(flxfrg));
126  auto comp_end = std::chrono::high_resolution_clock::now();
127  artdaq::Fragment decompfrg(dune::FelixDecompress(compfrg));
128  auto decomp_end = std::chrono::high_resolution_clock::now();
129 
130  std::cout << "Compressed buffer size: " << compfrg.size() << ".\n"
131  << "Compression factor: " << (double)flxfrg.dataSizeBytes() / compfrg.size() << '\n'
132  << "Compression time taken: "
133  << std::chrono::duration_cast<std::chrono::microseconds>(
134  comp_end - comp_begin)
135  .count()
136  << " us.\n"
137  << "Decompression time taken: "
138  << std::chrono::duration_cast<std::chrono::microseconds>(
139  decomp_end - comp_end)
140  .count()
141  << " us.\n\n"
142  << "Noise RMS: " << frag_rms << '\n';
143 
144  comp_factors.push_back((double)flxfrg.dataSizeBytes() / compfrg.size());
145  comp_times.push_back(std::chrono::duration_cast<std::chrono::microseconds>(
146  comp_end - comp_begin)
147  .count());
148 
149  ofile << (double)flxfrg.dataSizeBytes() / compfrg.size() << '\t' << std::chrono::duration_cast<std::chrono::microseconds>(
150  comp_end - comp_begin)
151  .count() << '\t' << frag_rms << '\n';
152 
153  // // Test whether the original and decompressed frames correspond.
154  // const dune::FelixFrame* orig =
155  // reinterpret_cast<dune::FelixFrame const*>(flxfrg.dataBeginBytes());
156  // const dune::FelixFrame* decomp =
157  // reinterpret_cast<dune::FelixFrame
158  // const*>(decompfrg.dataBeginBytes());
159  // for (unsigned i = 0; i < frames; ++i) {
160  // BOOST_REQUIRE_EQUAL((orig + i)->version(), (decomp + i)->version());
161  // BOOST_REQUIRE_EQUAL((orig + i)->fiber_no(), (decomp + i)->fiber_no());
162  // BOOST_REQUIRE_EQUAL((orig + i)->crate_no(), (decomp + i)->crate_no());
163  // BOOST_REQUIRE_EQUAL((orig + i)->slot_no(), (decomp + i)->slot_no());
164  // BOOST_REQUIRE_EQUAL((orig + i)->mm(), (decomp + i)->mm());
165  // BOOST_REQUIRE_EQUAL((orig + i)->oos(), (decomp + i)->oos());
166  // BOOST_REQUIRE_EQUAL((orig + i)->wib_errors(), (decomp + i)->wib_errors());
167  // BOOST_REQUIRE_EQUAL((orig + i)->timestamp(), (decomp + i)->timestamp());
168  // BOOST_REQUIRE_EQUAL((orig + i)->wib_counter(), (decomp + i)->wib_counter());
169  // BOOST_REQUIRE_EQUAL((orig + i)->z(), (decomp + i)->z());
170  // for(unsigned j = 0; j < 4; ++j) {
171  // BOOST_REQUIRE_EQUAL((orig + i)->s1_error(j), (decomp + i)->s1_error(j));
172  // BOOST_REQUIRE_EQUAL((orig + i)->s2_error(j), (decomp + i)->s2_error(j));
173  // BOOST_REQUIRE_EQUAL((orig + i)->coldata_convert_count(j), (decomp + i)->coldata_convert_count(j));
174  // BOOST_REQUIRE_EQUAL((orig + i)->error_register(j), (decomp + i)->error_register(j));
175  // }
176  // for (unsigned j = 0; j < 256; ++j) {
177  // BOOST_REQUIRE_EQUAL((orig + i)->channel(j), (decomp +
178  // i)->channel(j));
179  // }
180  // }
181  } // Loop over files.
182 
183  ofile.close();
184 
185  // // Calculate average compression factor and time, complete with error.
186  // double comp_factor = 0;
187  // for(auto c : comp_factors) { comp_factor += c;}
188  // comp_factor /= comp_factors.size();
189 
190  // double comp_time = 0;
191  // for(auto c : comp_times) { comp_time += c;}
192  // comp_time /= comp_times.size();
193 
194  // double comp_factor_err = 0;
195  // for(auto c : comp_factors) { comp_factor_err += pow(comp_factor - c, 2); }
196  // comp_factor_err = sqrt(comp_factor_err/(comp_factors.size()-1));
197 
198  // double comp_time_err = 0;
199  // for(auto c : comp_times) { comp_time_err += pow(comp_time - c, 2); }
200  // comp_time_err = sqrt(comp_time_err/(comp_times.size()-1));
201 
202  // std::cout << "Average compression factor: " << comp_factor << " +- " << comp_factor_err << "\nAverage compression time: " << comp_time << " +- " << comp_time_err << '\n';
203 
204  std::cout << "### WOOF WOOF -> Done...\n";
205 }
206 
207 BOOST_AUTO_TEST_SUITE_END()
208 
209 #pragma GCC diagnostic pop
size_t total_frames() const
uint8_t version(const unsigned &frame_ID=0) const
uint8_t slot_no(const unsigned &frame_ID=0) const
std::string string
Definition: nybbler.cc:12
uint8_t crate_no(const unsigned &frame_ID=0) const
artdaq::Fragment FelixDecompress(const std::vector< char > &buff)
constexpr T pow(T x)
Definition: pow.h:72
BOOST_AUTO_TEST_CASE(BaselineTest)
std::vector< char > FelixCompress(const dune::FelixFragment &frag)
QTextStream & hex(QTextStream &s)
microsecond microseconds
Alias for common language habits.
Definition: spacetime.h:122
string filename
Definition: train.py:213
adc_t get_ADC(const unsigned &frame_ID, const uint8_t channel_ID) const
p
Definition: test.py:223
size_t total_words() const
QTextStream & dec(QTextStream &s)
FragmentType toFragmentType(std::string t_string)
Definition: FragmentType.cc:10
uint8_t fiber_no(const unsigned &frame_ID=0) const
size_t total_adc_values() const
size_t dataSizeBytes() const
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
uint8_t sof(const unsigned &frame_ID=0) const
Event finding and building.
uint64_t timestamp(const unsigned &frame_ID=0) const