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