artdaq_demo  v2_10_00
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
ASCIIDump_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/AsciiFragment.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 ASCIIDump;
30 }
31 
35 class demo::ASCIIDump : public art::EDAnalyzer
36 {
37 public:
42  explicit ASCIIDump(fhicl::ParameterSet const& pset);
43 
44  virtual ~ASCIIDump();
45 
50  void analyze(art::Event const& evt) override;
51 
52 private:
53  std::string raw_data_label_;
54 };
55 
56 
57 demo::ASCIIDump::ASCIIDump(fhicl::ParameterSet const& pset)
58  : EDAnalyzer(pset)
59  , raw_data_label_(pset.get<std::string>("raw_data_label", "daq")) {}
60 
61 demo::ASCIIDump::~ASCIIDump() {}
62 
63 void demo::ASCIIDump::analyze(art::Event const& evt)
64 {
65  art::EventNumber_t eventNumber = evt.event();
66 
67  // ***********************
68  // *** ASCII Fragments ***
69  // ***********************
70 
71  artdaq::Fragments fragments;
72  std::vector<std::string> fragment_type_labels{"ASCII", "Container"};
73 
74  for (auto label : fragment_type_labels)
75  {
76  art::Handle<artdaq::Fragments> fragments_with_label;
77 
78  evt.getByLabel(raw_data_label_, label, fragments_with_label);
79  if (!fragments_with_label.isValid()) continue;
80 
81  // for (int i_l = 0; i_l < static_cast<int>(fragments_with_label->size()); ++i_l) {
82  // fragments.emplace_back( (*fragments_with_label)[i_l] );
83  // }
84 
85  if (label == "Container")
86  {
87  for (auto cont : *fragments_with_label)
88  {
89  artdaq::ContainerFragment contf(cont);
90  for (size_t ii = 0; ii < contf.block_count(); ++ii)
91  {
92  size_t fragSize = contf.fragSize(ii);
93  artdaq::Fragment thisfrag;
94  thisfrag.resizeBytes(fragSize);
95 
96  //mf::LogDebug("WFViewer") << "Copying " << fragSize << " bytes from " << contf.at(ii) << " to " << thisfrag.headerAddress();
97  memcpy(thisfrag.headerAddress(), contf.at(ii), fragSize);
98 
99  //mf::LogDebug("WFViewer") << "Putting new fragment into output vector";
100  fragments.push_back(thisfrag);
101  }
102  }
103  }
104  else
105  {
106  for (auto frag : *fragments_with_label)
107  {
108  fragments.emplace_back(frag);
109  }
110  }
111  }
112 
113  std::cout << "######################################################################" << std::endl;
114  std::cout << std::endl;
115  std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
116  << ", event " << eventNumber << " has " << fragments.size()
117  << " ASCII fragment(s)" << std::endl;
118 
119  for (const auto& frag : fragments)
120  {
121  AsciiFragment bb(frag);
122 
123  std::cout << std::endl;
124  std::cout << "Ascii fragment " << frag.fragmentID() << " is version " << frag.version() << std::endl;
125  std::cout << "Ascii fragment " << frag.fragmentID() << " has "
126  << bb.total_line_characters() << " characters in the line." << std::endl;
127  std::cout << std::endl;
128 
129  if (frag.hasMetadata())
130  {
131  std::cout << std::endl;
132  std::cout << "Fragment metadata: " << std::endl;
133  AsciiFragment::Metadata const* md =
134  frag.metadata<AsciiFragment::Metadata>();
135  std::cout << "Chars in line: ";
136  auto mdChars = md->charsInLine;
137  std::cout.write(reinterpret_cast<const char*>(&mdChars), sizeof(uint32_t) / sizeof(char));
138  std::cout << std::endl;
139  std::cout << std::endl;
140  }
141 
142  std::ofstream output("out.bin", std::ios::out | std::ios::app | std::ios::binary);
143  for (uint32_t i_adc = 0; i_adc < bb.total_line_characters(); ++i_adc)
144  {
145  output.write((char*)(bb.dataBegin() + i_adc), sizeof(char));
146  }
147  output.close();
148  std::cout << std::endl;
149  std::cout << std::endl;
150  }
151  std::cout << std::endl;
152 }
153 
154 DEFINE_ART_MODULE(demo::ASCIIDump)
void analyze(art::Event const &evt) override
Analyze an event. Called by art for each event in run (based on command line options) ...
An art::EDAnalyzer meant for decoding demo::ASCIIFragment objects.
ASCIIDump(fhicl::ParameterSet const &pset)
ASCIIDump Constructor.