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