artdaq_demo  v3_00_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/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 <vector>
27 #include <iostream>
28 
29 namespace demo
30 {
31  class ToyDump;
32 }
33 
37 class demo::ToyDump : public art::EDAnalyzer
38 {
39 public:
53  explicit ToyDump(fhicl::ParameterSet const& pset);
54 
58  virtual ~ToyDump();
59 
64  virtual void analyze(art::Event const& evt);
65 
66 private:
67  std::string raw_data_label_;
68  int num_adcs_to_write_;
69  int num_adcs_to_print_;
70  bool dump_to_file_;
71  bool dump_to_screen_;
72  uint32_t columns_to_display_on_screen_;
73  std::string output_file_name_;
74 };
75 
76 
77 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
78  : EDAnalyzer(pset)
79  , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
80  , num_adcs_to_write_(pset.get<int>("num_adcs_to_write", 0))
81  , num_adcs_to_print_(pset.get<int>("num_adcs_to_print", 10))
82  , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10))
83  , output_file_name_(pset.get<std::string>("output_file_name", "out.bin"))
84 {}
85 
87 
88 void demo::ToyDump::analyze(art::Event const& evt)
89 {
90  art::EventNumber_t eventNumber = evt.event();
91 
92  // ***********************
93  // *** Toy Fragments ***
94  // ***********************
95 
96  artdaq::Fragments fragments;
97  artdaq::FragmentPtrs containerFragments;
98  std::vector<std::string> fragment_type_labels{ "TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2" };
99 
100  for (auto label : fragment_type_labels)
101  {
102  art::Handle<artdaq::Fragments> fragments_with_label;
103 
104  evt.getByLabel(raw_data_label_, label, fragments_with_label);
105  if (!fragments_with_label.isValid()) continue;
106 
107  if (label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2")
108  {
109  for (auto cont : *fragments_with_label)
110  {
111  artdaq::ContainerFragment contf(cont);
112  for (size_t ii = 0; ii < contf.block_count(); ++ii)
113  {
114  containerFragments.push_back(contf[ii]);
115  fragments.push_back(*containerFragments.back());
116  }
117  }
118  }
119  else
120  {
121  for (auto frag : *fragments_with_label)
122  {
123  fragments.emplace_back(frag);
124  }
125  }
126  }
127 
128  // look for raw Toy data
129  TLOG(TLVL_INFO) << "Run " << evt.run() << ", subrun " << evt.subRun()
130  << ", event " << eventNumber << " has " << fragments.size()
131  << " fragment(s) of type TOY1 or TOY2";
132 
133  for (const auto& frag : fragments)
134  {
135  ToyFragment bb(frag);
136 
137  TLOG(TLVL_INFO) << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID()
138  << " w/ seqID " << frag.sequenceID()
139  << " has total ADC counts = " << bb.total_adc_values();
140 
141  if (frag.hasMetadata())
142  {
143  ToyFragment::Metadata const* md =
144  frag.metadata<ToyFragment::Metadata>();
145  TLOG(TLVL_DEBUG) << "Fragment metadata: " << std::showbase << "Board serial number = "
146  << md->board_serial_number << ", sample bits = "
147  << md->num_adc_bits
148  << " -> max ADC value = "
149  << bb.adc_range((int)md->num_adc_bits);
150  }
151 
152  if (num_adcs_to_write_ >= 0)
153  {
154  uint32_t numAdcs = num_adcs_to_write_;
155  if (num_adcs_to_write_ == 0) numAdcs = bb.total_adc_values();
156  else if (static_cast<uint32_t>(num_adcs_to_write_) > bb.total_adc_values())
157  {
158  TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
159  numAdcs = bb.total_adc_values();
160  }
161  std::ofstream output(output_file_name_, std::ios::out | std::ios::app | std::ios::binary);
162  for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
163  {
164  output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
165  }
166  output.close();
167  }
168 
169  if (num_adcs_to_print_ >= 0)
170  {
171  uint32_t numAdcs = num_adcs_to_print_;
172  if (num_adcs_to_print_ == 0) numAdcs = bb.total_adc_values();
173  else if (static_cast<uint32_t>(num_adcs_to_print_) > bb.total_adc_values())
174  {
175  TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
176  numAdcs = bb.total_adc_values();
177  }
178 
179  TLOG(TLVL_INFO) << "First " << numAdcs << " ADC values in the fragment:";
180  int rows = 1 + (int)((num_adcs_to_print_ - 1) / columns_to_display_on_screen_);
181  uint32_t adc_counter = 0;
182  for (int idx = 0; idx < rows; ++idx)
183  {
184  std::ostringstream o;
185  o << std::right;
186  o << std::setw(4) << std::setfill('.');
187  o << (idx * columns_to_display_on_screen_) << ": ";
188  for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
189  {
190  if (adc_counter >= numAdcs) { break; }
191  o << std::setw(6) << std::setfill(' ');
192  o << bb.adc_value(adc_counter);
193  ++adc_counter;
194  }
195  TLOG(TLVL_INFO) << o.str();
196  }
197  }
198  }
199 }
200 
201 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) ...