artdaq_demo  v3_09_06
ToyDump_module.cc
1 // Class: ToyDump
3 // Module Type: analyzer
4 // File: ToyDump_module.cc
5 // Description: Prints out information about each event.
7 
8 #define TRACE_NAME "ToyDump"
9 
10 #include "art/Framework/Core/EDAnalyzer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "canvas/Utilities/Exception.h"
15 
16 #include "artdaq-core-demo/Overlays/FragmentType.hh"
17 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
18 #include "artdaq-core/Data/ContainerFragment.hh"
19 #include "artdaq-core/Data/Fragment.hh"
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <cmath>
24 #include <fstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <map>
28 #include <vector>
29 
30 namespace demo {
31 class ToyDump;
32 } // namespace demo
33 
37 class demo::ToyDump : public art::EDAnalyzer
38 {
39 public:
53  explicit ToyDump(fhicl::ParameterSet const& pset);
54 
58  ~ToyDump() override;
59 
64  void analyze(art::Event const& evt) override;
65 
70  void endSubRun(art::SubRun const& sr) override;
71 
72 private:
73  ToyDump(ToyDump const&) = delete;
74  ToyDump(ToyDump&&) = delete;
75  ToyDump& operator=(ToyDump const&) = delete;
76  ToyDump& operator=(ToyDump&&) = delete;
77 
78  std::string raw_data_label_;
79  int num_adcs_to_write_;
80  int num_adcs_to_print_;
81  bool binary_mode_;
82  uint32_t columns_to_display_on_screen_;
83  std::string output_file_name_;
84 
85  std::map<size_t, size_t> fragment_counts_;
86  size_t event_count_;
87 };
88 
89 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
90  : EDAnalyzer(pset)
91  , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
92  , num_adcs_to_write_(pset.get<int>("num_adcs_to_write", 0))
93  , num_adcs_to_print_(pset.get<int>("num_adcs_to_print", 10))
94  , binary_mode_(pset.get<bool>("binary_mode", true))
95  , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10))
96  , output_file_name_(pset.get<std::string>("output_file_name", "out.bin"))
97 {}
98 
99 demo::ToyDump::~ToyDump() = default;
100 
101 void demo::ToyDump::analyze(art::Event const& evt)
102 {
103  art::EventNumber_t eventNumber = evt.event();
104 
105  // ***********************
106  // *** Toy Fragments ***
107  // ***********************
108 
109  artdaq::Fragments fragments;
110  artdaq::FragmentPtrs containerFragments;
111 
112  std::vector<art::Handle<artdaq::Fragments>> fragmentHandles;
113  evt.getManyByType(fragmentHandles);
114 
115  for (const auto& handle : fragmentHandles)
116  {
117  if (!handle.isValid() || handle->empty())
118  {
119  continue;
120  }
121 
122  if (handle->front().type() == artdaq::Fragment::ContainerFragmentType)
123  {
124  for (const auto& cont : *handle)
125  {
126  artdaq::ContainerFragment contf(cont);
127  if (contf.fragment_type() != demo::FragmentType::TOY1 && contf.fragment_type() != demo::FragmentType::TOY2)
128  {
129  break;
130  }
131 
132  for (size_t ii = 0; ii < contf.block_count(); ++ii)
133  {
134  containerFragments.push_back(contf[ii]);
135  fragments.push_back(*containerFragments.back());
136  }
137  }
138  }
139  else
140  {
141  if (handle->front().type() == demo::FragmentType::TOY1 || handle->front().type() == demo::FragmentType::TOY2)
142  {
143  for (auto frag : *handle)
144  {
145  fragments.emplace_back(frag);
146  }
147  }
148  }
149  }
150 
151  // look for raw Toy data
152  TLOG(TLVL_INFO) << "Run " << evt.run() << ", subrun " << evt.subRun() << ", event " << eventNumber << " has "
153  << fragments.size() << " fragment(s) of type TOY1 or TOY2";
154  fragment_counts_[fragments.size()]++;
155  event_count_++;
156 
157  for (const auto& frag : fragments)
158  {
159  ToyFragment bb(frag);
160 
161  TLOG(TLVL_INFO) << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type()))
162  << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " and timestamp "
163  << frag.timestamp() << " has total ADC counts = " << bb.total_adc_values()
164  << ", trig # = " << bb.hdr_trigger_number()
165  << ", dist_type = " << static_cast<int>(bb.hdr_distribution_type());
166 
167  if (frag.hasMetadata())
168  {
169  auto const* md = frag.metadata<ToyFragment::Metadata>();
170  TLOG(TLVL_DEBUG) << "Fragment metadata: " << std::showbase
171  << "Board serial number = " << md->board_serial_number
172  << ", sample bits = " << md->num_adc_bits
173  << " -> max ADC value = " << demo::ToyFragment::adc_range(static_cast<int>(md->num_adc_bits));
174  }
175 
176  if (num_adcs_to_write_ >= 0)
177  {
178  uint32_t numAdcs = num_adcs_to_write_;
179  if (num_adcs_to_write_ == 0)
180  {
181  numAdcs = bb.total_adc_values();
182  }
183  else if (static_cast<uint32_t>(num_adcs_to_write_) > bb.total_adc_values())
184  {
185  TLOG(TLVL_WARNING)
186  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
187  numAdcs = bb.total_adc_values();
188  }
189  if (binary_mode_)
190  {
191  std::ofstream output(output_file_name_, std::ios::out | std::ios::app | std::ios::binary);
192  for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
193  {
194  output.write(reinterpret_cast<const char*>(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-reinterpret-cast)
195  }
196  output.close();
197  }
198  else
199  {
200  std::ofstream output(output_file_name_, std::ios::out | std::ios::app);
201  output << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << "\t"
202  << frag.fragmentID();
203 
204  for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
205  {
206  output << "\t" << std::to_string(*(bb.dataBeginADCs() + i_adc)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
207  }
208  output << std::endl;
209  output.close();
210  }
211  }
212 
213  if (num_adcs_to_print_ >= 0)
214  {
215  uint32_t numAdcs = num_adcs_to_print_;
216  if (num_adcs_to_print_ == 0)
217  {
218  numAdcs = bb.total_adc_values();
219  }
220  else if (static_cast<uint32_t>(num_adcs_to_print_) > bb.total_adc_values())
221  {
222  TLOG(TLVL_WARNING)
223  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
224  numAdcs = bb.total_adc_values();
225  }
226 
227  TLOG(TLVL_INFO) << "First " << numAdcs << " ADC values in the fragment:";
228  int rows = 1 + static_cast<int>((num_adcs_to_print_ - 1) / columns_to_display_on_screen_);
229  uint32_t adc_counter = 0;
230  for (int idx = 0; idx < rows; ++idx)
231  {
232  std::ostringstream o;
233  o << std::right;
234  o << std::setw(4) << std::setfill('.');
235  o << (idx * columns_to_display_on_screen_) << ": ";
236  for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
237  {
238  if (adc_counter >= numAdcs)
239  {
240  break;
241  }
242  o << std::setw(6) << std::setfill(' ');
243  o << bb.adc_value(adc_counter);
244  ++adc_counter;
245  }
246  TLOG(TLVL_INFO) << o.str();
247  }
248  }
249  }
250 }
251 
252 void demo::ToyDump::endSubRun(art::SubRun const& sr)
253 {
254  auto limit_save = traceControl_rwp->limit_cnt_limit;
255  traceControl_rwp->limit_cnt_limit = 0;
256  TLOG(TLVL_INFO) << "ENDSUBRUN: Run " << sr.id().run() << ", Subrun " << sr.id().subRun() << " has " << event_count_ << " events.";
257  for (auto const& c : fragment_counts_)
258  {
259  TLOG(TLVL_INFO) << "ENDSUBRUN: There were " << c.second << " events with " << c.first << " TOY1 or TOY2 Fragments";
260  }
261  traceControl_rwp->limit_cnt_limit = limit_save;
262  fragment_counts_.clear();
263  event_count_ = 0;
264 }
265 
266 DEFINE_ART_MODULE(demo::ToyDump) // NOLINT(performance-unnecessary-value-param)
void endSubRun(art::SubRun const &sr) override
Print summary information from a SubRun.
ToyDump(fhicl::ParameterSet const &pset)
ToyDump Constructor.
~ToyDump() override
ToyDump Destructor.
An art::EDAnalyzer module designed to display the data from demo::ToyFragment objects.
void analyze(art::Event const &evt) override
Analyze an event. Called by art for each event in run (based on command line options) ...