otsdaq  v1_01_03
 All Classes Namespaces Functions
UDPDump_module.cc
1 // Class: UDPDump
3 // Module Type: analyzer
4 // File: UDPDump_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-ots/Overlays/UDPFragment.hh"
15 #include "artdaq-core/Data/Fragments.hh"
16 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <cmath>
21 #include <fstream>
22 #include <iomanip>
23 #include <vector>
24 #include <iostream>
25 #include <arpa/inet.h>
26 
27 namespace ots {
28  class UDPDump;
29 }
30 
31 class ots::UDPDump : public art::EDAnalyzer {
32 public:
33  explicit UDPDump(fhicl::ParameterSet const & pset);
34  virtual ~UDPDump();
35 
36  virtual void analyze(art::Event const & evt);
37 
38 private:
39  std::string raw_data_label_;
40  std::string frag_type_;
41  unsigned int num_bytes_to_show_;
42 };
43 
44 
45 ots::UDPDump::UDPDump(fhicl::ParameterSet const & pset)
46  : EDAnalyzer(pset),
47  raw_data_label_(pset.get<std::string>("raw_data_label")),
48  frag_type_(pset.get<std::string>("frag_type")),
49  num_bytes_to_show_(pset.get<int>("num_bytes"))
50 {
51 }
52 
53 ots::UDPDump::~UDPDump()
54 {
55 }
56 
57 void ots::UDPDump::analyze(art::Event const & evt)
58 {
59  art::EventNumber_t eventNumber = evt.event();
60 
61  // ***********************
62  // *** Toy Fragments ***
63  // ***********************
64 
65  // look for raw UDP data
66 
67  art::Handle<artdaq::Fragments> raw;
68  evt.getByLabel(raw_data_label_, frag_type_, raw);
69 
70  if (raw.isValid()) {
71  std::cout << __COUT_HDR_FL__ << "######################################################################" << std::endl;
72 
73  std::cout << __COUT_HDR_FL__ << std::dec << "Run " << evt.run() << ", subrun " << evt.subRun()
74  << ", event " << eventNumber << " has " << raw->size()
75  << " fragment(s) of type " << frag_type_ << std::endl;
76 
77  for (size_t idx = 0; idx < raw->size(); ++idx) {
78  const auto& frag((*raw)[idx]);
79 
80  ots::UDPFragment bb(frag);
81 
82 
83  std::cout << __COUT_HDR_FL__ << "UDP fragment " << frag.fragmentID() << " has total byte count = "
84  << bb.udp_data_words() << std::endl;
85 
86 
87  if (frag.hasMetadata()) {
88 
89  std::cout << __COUT_HDR_FL__ << "Fragment metadata: " << std::endl;
90  ots::UDPFragment::Metadata const* md =
91  frag.metadata<ots::UDPFragment::Metadata>();
92 
93  char buf[sizeof(in_addr)];
94  struct sockaddr_in addr;
95  addr.sin_addr.s_addr = md->address;
96  inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN);
97 
98  std::cout << __COUT_HDR_FL__ << "Board port number = "
99  << ((int)md->port) << ", address = "
100  << std::string(buf)
101  << std::endl;
102 
103  }
104 
105  int type = bb.hdr_data_type();
106  if(type == 0 || type > 2)
107  {
108  auto it = bb.dataBegin();
109  std::cout << __COUT_HDR_FL__ << std::hex << "0x" << (int)*it << std::endl;
110  ++it;
111 
112  for(; it !=bb.dataEnd(); ++it)
113  {
114  std::cout << __COUT_HDR_FL__ << ", " << std::hex << "0x" << (int)*it << std::endl;
115  }
116 
117  }
118  else
119  {
120  std::string output = std::string((const char *)bb.dataBegin());
121  std::cout << __COUT_HDR_FL__ << output << std::endl;
122  }
123  }
124  }
125  else {
126  std::cout << __COUT_HDR_FL__ << std::dec << "Run " << evt.run() << ", subrun " << evt.subRun()
127  << ", event " << eventNumber << " has zero"
128  << " UDP fragments." << std::endl;
129  }
130 
131 
132 }
133 
134 DEFINE_ART_MODULE(ots::UDPDump)