tdaq-develop-2025-02-12
UDPDump_module.cc
1 
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
72  << __COUT_HDR_FL__
73  << "######################################################################"
74  << std::endl;
75 
76  std::cout << __COUT_HDR_FL__ << std::dec << "Run " << evt.run() << ", subrun "
77  << evt.subRun() << ", event " << eventNumber << " has " << raw->size()
78  << " fragment(s) of type " << frag_type_ << std::endl;
79 
80  for(size_t idx = 0; idx < raw->size(); ++idx)
81  {
82  const auto& frag((*raw)[idx]);
83 
84  ots::UDPFragment bb(frag);
85 
86  std::cout << __COUT_HDR_FL__ << "UDP fragment " << frag.fragmentID()
87  << " has total byte count = " << bb.udp_data_words() << std::endl;
88 
89  if(frag.hasMetadata())
90  {
91  std::cout << __COUT_HDR_FL__ << "Fragment metadata: " << std::endl;
92  ots::UDPFragment::Metadata const* md =
93  frag.metadata<ots::UDPFragment::Metadata>();
94 
95  char buf[sizeof(in_addr)];
96  struct sockaddr_in addr;
97  addr.sin_addr.s_addr = md->address;
98  inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN);
99 
100  std::cout << __COUT_HDR_FL__ << "Board port number = " << ((int)md->port)
101  << ", address = " << std::string(buf) << std::endl;
102  }
103 
104  int type = bb.hdr_data_type();
105  if(type == 0 || type > 2)
106  {
107  auto it = bb.dataBegin();
108  std::cout << __COUT_HDR_FL__ << std::hex << "0x" << (int)*it << std::endl;
109  ++it;
110 
111  for(; it != bb.dataEnd(); ++it)
112  {
113  std::cout << __COUT_HDR_FL__ << ", " << std::hex << "0x" << (int)*it
114  << std::endl;
115  }
116  }
117  else
118  {
119  std::string output = std::string((const char*)bb.dataBegin());
120  std::cout << __COUT_HDR_FL__ << output << std::endl;
121  }
122  }
123  }
124  else
125  {
126  std::cout << __COUT_HDR_FL__ << std::dec << "Run " << evt.run() << ", subrun "
127  << evt.subRun() << ", event " << eventNumber << " has zero"
128  << " UDP fragments." << std::endl;
129  }
130 }
131 
132 DEFINE_ART_MODULE(ots::UDPDump)