artdaq_demo  v3_08_00
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 <vector>
28 
29 namespace demo {
30 class ToyDump;
31 }
32 
36 class demo::ToyDump : public art::EDAnalyzer
37 {
38 public:
52  explicit ToyDump(fhicl::ParameterSet const& pset);
53 
57  virtual ~ToyDump();
58 
63  virtual void analyze(art::Event const& evt);
64 
65 private:
66  std::string raw_data_label_;
67  int num_adcs_to_write_;
68  int num_adcs_to_print_;
69  bool binary_mode_;
70  uint32_t columns_to_display_on_screen_;
71  std::string output_file_name_;
72 };
73 
74 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
75  : EDAnalyzer(pset)
76  , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
77  , num_adcs_to_write_(pset.get<int>("num_adcs_to_write", 0))
78  , num_adcs_to_print_(pset.get<int>("num_adcs_to_print", 10))
79  , binary_mode_(pset.get<bool>("binary_mode", true))
80  , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10))
81  , output_file_name_(pset.get<std::string>("output_file_name", "out.bin"))
82 {}
83 
85 
86 void demo::ToyDump::analyze(art::Event const& evt)
87 {
88  art::EventNumber_t eventNumber = evt.event();
89 
90  // ***********************
91  // *** Toy Fragments ***
92  // ***********************
93 
94  artdaq::Fragments fragments;
95  artdaq::FragmentPtrs containerFragments;
96 
97  std::vector<art::Handle<artdaq::Fragments>> fragmentHandles;
98  evt.getManyByType(fragmentHandles);
99 
100  for (auto handle : fragmentHandles)
101  {
102  if (!handle.isValid() || handle->size() == 0) continue;
103 
104  if (handle->front().type() == artdaq::Fragment::ContainerFragmentType)
105  {
106  for (auto cont : *handle)
107  {
108  artdaq::ContainerFragment contf(cont);
109  if (contf.fragment_type() != demo::FragmentType::TOY1 && contf.fragment_type() != demo::FragmentType::TOY2)
110  {
111  break;
112  }
113 
114  for (size_t ii = 0; ii < contf.block_count(); ++ii)
115  {
116  containerFragments.push_back(contf[ii]);
117  fragments.push_back(*containerFragments.back());
118  }
119  }
120  }
121  else
122  {
123  if (handle->front().type() == demo::FragmentType::TOY1 || handle->front().type() == demo::FragmentType::TOY2)
124  {
125  for (auto frag : *handle)
126  {
127  fragments.emplace_back(frag);
128  }
129  }
130  }
131  }
132 
133  // look for raw Toy data
134  TLOG(TLVL_INFO) << "Run " << evt.run() << ", subrun " << evt.subRun() << ", event " << eventNumber << " has "
135  << fragments.size() << " fragment(s) of type TOY1 or TOY2";
136 
137  for (const auto& frag : fragments)
138  {
139  ToyFragment bb(frag);
140 
141  TLOG(TLVL_INFO) << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type()))
142  << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " and timestamp "
143  << frag.timestamp() << " has total ADC counts = " << bb.total_adc_values()
144  << ", trig # = " << bb.hdr_trigger_number()
145  << ", dist_type = " << static_cast<int>(bb.hdr_distribution_type());
146 
147  if (frag.hasMetadata())
148  {
149  ToyFragment::Metadata const* md = frag.metadata<ToyFragment::Metadata>();
150  TLOG(TLVL_DEBUG) << "Fragment metadata: " << std::showbase
151  << "Board serial number = " << md->board_serial_number
152  << ", sample bits = " << md->num_adc_bits
153  << " -> max ADC value = " << bb.adc_range((int)md->num_adc_bits);
154  }
155 
156  if (num_adcs_to_write_ >= 0)
157  {
158  uint32_t numAdcs = num_adcs_to_write_;
159  if (num_adcs_to_write_ == 0)
160  numAdcs = bb.total_adc_values();
161  else if (static_cast<uint32_t>(num_adcs_to_write_) > bb.total_adc_values())
162  {
163  TLOG(TLVL_WARNING)
164  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
165  numAdcs = bb.total_adc_values();
166  }
167  if (binary_mode_)
168  {
169  std::ofstream output(output_file_name_, std::ios::out | std::ios::app | std::ios::binary);
170  for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
171  {
172  output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
173  }
174  output.close();
175  }
176  else
177  {
178  std::ofstream output(output_file_name_, std::ios::out | std::ios::app);
179  output << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << "\t"
180  << frag.fragmentID();
181 
182  for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
183  {
184  output << "\t" << std::to_string(*(bb.dataBeginADCs() + i_adc));
185  }
186  output << std::endl;
187  output.close();
188  }
189  }
190 
191  if (num_adcs_to_print_ >= 0)
192  {
193  uint32_t numAdcs = num_adcs_to_print_;
194  if (num_adcs_to_print_ == 0)
195  numAdcs = bb.total_adc_values();
196  else if (static_cast<uint32_t>(num_adcs_to_print_) > bb.total_adc_values())
197  {
198  TLOG(TLVL_WARNING)
199  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
200  numAdcs = bb.total_adc_values();
201  }
202 
203  TLOG(TLVL_INFO) << "First " << numAdcs << " ADC values in the fragment:";
204  int rows = 1 + (int)((num_adcs_to_print_ - 1) / columns_to_display_on_screen_);
205  uint32_t adc_counter = 0;
206  for (int idx = 0; idx < rows; ++idx)
207  {
208  std::ostringstream o;
209  o << std::right;
210  o << std::setw(4) << std::setfill('.');
211  o << (idx * columns_to_display_on_screen_) << ": ";
212  for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
213  {
214  if (adc_counter >= numAdcs)
215  {
216  break;
217  }
218  o << std::setw(6) << std::setfill(' ');
219  o << bb.adc_value(adc_counter);
220  ++adc_counter;
221  }
222  TLOG(TLVL_INFO) << o.str();
223  }
224  }
225  }
226 }
227 
228 DEFINE_ART_MODULE(demo::ToyDump)
ToyDump(fhicl::ParameterSet const &pset)
ToyDump Constructor.
An art::EDAnalyzer module designed to display the data from demo::ToyFragment objects.
virtual ~ToyDump()
ToyDump Destructor.
virtual void analyze(art::Event const &evt)
Analyze an event. Called by art for each event in run (based on command line options) ...