artdaq_demo  v3_00_01
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 #include "art/Framework/Core/EDAnalyzer.h"
9 #include "art/Framework/Core/ModuleMacros.h"
10 #include "art/Framework/Principal/Event.h"
11 #include "art/Framework/Principal/Handle.h"
12 #include "canvas/Utilities/Exception.h"
13 
14 #include "artdaq-core-demo/Overlays/FragmentType.hh"
15 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
16 #include "artdaq-core/Data/ContainerFragment.hh"
17 #include "artdaq-core/Data/Fragment.hh"
18 
19 #include <algorithm>
20 #include <cassert>
21 #include <cmath>
22 #include <fstream>
23 #include <iomanip>
24 #include <vector>
25 #include <iostream>
26 
27 namespace demo
28 {
29  class ToyDump;
30 }
31 
35 class demo::ToyDump : public art::EDAnalyzer
36 {
37 public:
51  explicit ToyDump(fhicl::ParameterSet const& pset);
52 
56  virtual ~ToyDump();
57 
62  virtual void analyze(art::Event const& evt);
63 
64 private:
65  std::string raw_data_label_;
66  uint32_t num_adcs_to_show_;
67  bool dump_to_file_;
68  bool dump_to_screen_;
69  uint32_t columns_to_display_on_screen_;
70 };
71 
72 
73 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
74  : EDAnalyzer(pset)
75  , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
76  , num_adcs_to_show_(pset.get<uint32_t>("num_adcs_to_show", 0))
77  , dump_to_file_(pset.get<bool>("dump_to_file", true))
78  , dump_to_screen_(pset.get<bool>("dump_to_screen", false))
79  , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10)) {}
80 
82 
83 void demo::ToyDump::analyze(art::Event const& evt)
84 {
85  art::EventNumber_t eventNumber = evt.event();
86 
87  // ***********************
88  // *** Toy Fragments ***
89  // ***********************
90 
91  artdaq::Fragments fragments;
92  artdaq::FragmentPtrs containerFragments;
93  std::vector<std::string> fragment_type_labels{"TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2"};
94 
95  for (auto label : fragment_type_labels)
96  {
97  art::Handle<artdaq::Fragments> fragments_with_label;
98 
99  evt.getByLabel(raw_data_label_, label, fragments_with_label);
100  if (!fragments_with_label.isValid()) continue;
101 
102  // for (int i_l = 0; i_l < static_cast<int>(fragments_with_label->size()); ++i_l) {
103  // fragments.emplace_back( (*fragments_with_label)[i_l] );
104  // }
105 
106  if (label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2")
107  {
108  for (auto cont : *fragments_with_label)
109  {
110  artdaq::ContainerFragment contf(cont);
111  for (size_t ii = 0; ii < contf.block_count(); ++ii)
112  {
113  containerFragments.push_back(contf[ii]);
114  fragments.push_back(*containerFragments.back());
115  }
116  }
117  }
118  else
119  {
120  for (auto frag : *fragments_with_label)
121  {
122  fragments.emplace_back(frag);
123  }
124  }
125  }
126 
127  // look for raw Toy data
128 
129  std::cout << "######################################################################" << std::endl;
130  std::cout << std::endl;
131  std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
132  << ", event " << eventNumber << " has " << fragments.size()
133  << " fragment(s) of type TOY1 or TOY2" << std::endl;
134 
135  for (const auto& frag : fragments)
136  {
137  ToyFragment bb(frag);
138 
139  std::cout << std::endl;
140  std::cout << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " has total ADC counts = "
141  << bb.total_adc_values() << std::endl;
142  //std::cout << std::endl;
143 
144  if (frag.hasMetadata())
145  {
146  std::cout << std::endl;
147  std::cout << "Fragment metadata: " << std::endl;
148  ToyFragment::Metadata const* md =
149  frag.metadata<ToyFragment::Metadata>();
150  std::cout << std::showbase << "Board serial number = "
151  << ((int)md->board_serial_number) << ", sample bits = "
152  << ((int)md->num_adc_bits)
153  << " -> max ADC value = "
154  << bb.adc_range((int)md->num_adc_bits)
155  << std::endl;
156  //std::cout << std::endl;
157  }
158 
159  if (num_adcs_to_show_ == 0)
160  {
161  num_adcs_to_show_ = bb.total_adc_values();
162  }
163 
164  if (num_adcs_to_show_ > 0)
165  {
166  if (num_adcs_to_show_ > bb.total_adc_values())
167  {
168  throw cet::exception("num_adcs_to_show is larger than total number of adcs in fragment");
169  }
170  else
171  {
172  std::cout << std::endl;
173  std::cout << "First " << num_adcs_to_show_
174  << " ADC values in the fragment: "
175  << std::endl;
176  }
177 
178  if (dump_to_file_)
179  {
180  std::ofstream output("out.bin", std::ios::out | std::ios::app | std::ios::binary);
181  for (uint32_t i_adc = 0; i_adc < num_adcs_to_show_; ++i_adc)
182  {
183  output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
184  }
185  output.close();
186  }
187 
188  if (dump_to_screen_)
189  {
190  std::cout << std::right;
191  int rows = 1 + (int)((num_adcs_to_show_ - 1) / columns_to_display_on_screen_);
192  uint32_t adc_counter = 0;
193  for (int idx = 0; idx < rows; ++idx)
194  {
195  std::cout << std::setw(4) << std::setfill('.');
196  std::cout << (idx * columns_to_display_on_screen_) << ": ";
197  for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
198  {
199  if (adc_counter >= num_adcs_to_show_) { break; }
200  std::cout << std::setw(6) << std::setfill(' ');
201  std::cout << bb.adc_value(adc_counter);
202  ++adc_counter;
203  }
204  std::cout << std::endl;
205  }
206  }
207 
208  std::cout << std::endl;
209  }
210  }
211  std::cout << std::endl;
212 }
213 
214 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) ...