artdaq_demo  v2_10_00
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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  std::vector<std::string> fragment_type_labels{"TOY1", "TOY2", "Container"};
93 
94  for (auto label : fragment_type_labels)
95  {
96  art::Handle<artdaq::Fragments> fragments_with_label;
97 
98  evt.getByLabel(raw_data_label_, label, fragments_with_label);
99  if (!fragments_with_label.isValid()) continue;
100 
101  // for (int i_l = 0; i_l < static_cast<int>(fragments_with_label->size()); ++i_l) {
102  // fragments.emplace_back( (*fragments_with_label)[i_l] );
103  // }
104 
105  if (label == "Container")
106  {
107  for (auto cont : *fragments_with_label)
108  {
109  artdaq::ContainerFragment contf(cont);
110  for (size_t ii = 0; ii < contf.block_count(); ++ii)
111  {
112  size_t fragSize = contf.fragSize(ii);
113  artdaq::Fragment thisfrag;
114  thisfrag.resizeBytes(fragSize);
115 
116  //mf::LogDebug("WFViewer") << "Copying " << fragSize << " bytes from " << contf.at(ii) << " to " << thisfrag.headerAddress();
117  memcpy(thisfrag.headerAddress(), contf.at(ii), fragSize);
118 
119  //mf::LogDebug("WFViewer") << "Putting new fragment into output vector";
120  fragments.push_back(thisfrag);
121  }
122  }
123  }
124  else
125  {
126  for (auto frag : *fragments_with_label)
127  {
128  fragments.emplace_back(frag);
129  }
130  }
131  }
132 
133  // look for raw Toy data
134 
135  std::cout << "######################################################################" << std::endl;
136  std::cout << std::endl;
137  std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
138  << ", event " << eventNumber << " has " << fragments.size()
139  << " fragment(s) of type TOY1 or TOY2" << std::endl;
140 
141  for (const auto& frag : fragments)
142  {
143  ToyFragment bb(frag);
144 
145  std::cout << std::endl;
146  std::cout << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " has total ADC counts = "
147  << bb.total_adc_values() << std::endl;
148  //std::cout << std::endl;
149 
150  if (frag.hasMetadata())
151  {
152  std::cout << std::endl;
153  std::cout << "Fragment metadata: " << std::endl;
154  ToyFragment::Metadata const* md =
155  frag.metadata<ToyFragment::Metadata>();
156  std::cout << std::showbase << "Board serial number = "
157  << ((int)md->board_serial_number) << ", sample bits = "
158  << ((int)md->num_adc_bits)
159  << " -> max ADC value = "
160  << bb.adc_range((int)md->num_adc_bits)
161  << std::endl;
162  //std::cout << std::endl;
163  }
164 
165  if (num_adcs_to_show_ == 0)
166  {
167  num_adcs_to_show_ = bb.total_adc_values();
168  }
169 
170  if (num_adcs_to_show_ > 0)
171  {
172  if (num_adcs_to_show_ > bb.total_adc_values())
173  {
174  throw cet::exception("num_adcs_to_show is larger than total number of adcs in fragment");
175  }
176  else
177  {
178  std::cout << std::endl;
179  std::cout << "First " << num_adcs_to_show_
180  << " ADC values in the fragment: "
181  << std::endl;
182  }
183 
184  if (dump_to_file_)
185  {
186  std::ofstream output("out.bin", std::ios::out | std::ios::app | std::ios::binary);
187  for (uint32_t i_adc = 0; i_adc < num_adcs_to_show_; ++i_adc)
188  {
189  output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
190  }
191  output.close();
192  }
193 
194  if (dump_to_screen_)
195  {
196  std::cout << std::right;
197  int rows = 1 + (int)((num_adcs_to_show_ - 1) / columns_to_display_on_screen_);
198  uint32_t adc_counter = 0;
199  for (int idx = 0; idx < rows; ++idx)
200  {
201  std::cout << std::setw(4) << std::setfill('.');
202  std::cout << (idx * columns_to_display_on_screen_) << ": ";
203  for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
204  {
205  if (adc_counter >= num_adcs_to_show_) { break; }
206  std::cout << std::setw(6) << std::setfill(' ');
207  std::cout << bb.adc_value(adc_counter);
208  ++adc_counter;
209  }
210  std::cout << std::endl;
211  }
212  }
213 
214  std::cout << std::endl;
215  }
216  }
217  std::cout << std::endl;
218 }
219 
220 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) ...