otsdaq_mu2e  v2_04_02
STIBReceiver_generator.cc
1 #include "otsdaq-demo/Generators/STIBReceiver.hh"
2 #include "otsdaq/Macros/CoutMacros.h"
3 
4 //#include "art/Utilities/Exception.h"
5 #include "artdaq-core/Utilities/SimpleLookupPolicy.h"
6 #include "artdaq/Application/GeneratorMacros.hh"
7 #include "cetlib/exception.h"
8 #include "fhiclcpp/ParameterSet.h"
9 #include "otsdaq-demo/Overlays/FragmentType.hh"
10 #include "otsdaq-demo/Overlays/STIBFragmentWriter.hh"
11 
12 #include <sys/poll.h>
13 #include <fstream>
14 #include <iomanip>
15 #include <iostream>
16 #include <iterator>
17 
18 ots::STIBReceiver::STIBReceiver(fhicl::ParameterSet const& ps) : UDPReceiver(ps) {}
19 
20 void ots::STIBReceiver::ProcessData_(artdaq::FragmentPtrs& frags)
21 {
23  metadata.port = dataport_;
24  metadata.address = si_data_.sin_addr.s_addr;
25 
26  bool inData = false;
27  uint64_t bunch_counter = 0;
28  uint64_t triggerCounter = 0;
29  uint8_t triggerInput0 = 0;
30  uint8_t triggerInput1 = 0;
31  std::vector<uint32_t> data;
32 
33  std::ofstream output;
34  if(rawOutput_)
35  {
36  std::string outputPath =
37  rawPath_ + "/STIBReceiver-" + ip_ + ":" + std::to_string(dataport_) + ".bin";
38  output.open(outputPath, std::ios::out | std::ios::app | std::ios::binary);
39  }
40 
41  std::cout << __COUT_HDR_FL__ << "Starting STIBReceiver Packet Processing Loop"
42  << std::endl;
43  for(auto packet = packetBuffers_.begin(); packet != packetBuffers_.end(); ++packet)
44  {
45  for(size_t word = 0; word < (*packet)->size(); word += 4)
46  {
47  uint8_t byte3 = (*packet)->at(word);
48  uint8_t byte2 = (*packet)->at(word + 1);
49  uint8_t byte1 = (*packet)->at(word + 2);
50  uint8_t byte0 = (*packet)->at(word + 3);
51 
52  if(rawOutput_)
53  {
54  output.write((char*)&(byte3), sizeof(uint32_t));
55  }
56 
57  if((byte0 & 0x8) == 0)
58  {
59  if(inData) // We've reached the end of a data block, write it out
60  {
61  std::size_t initial_payload_size = 0;
62 
63  frags.emplace_back(
64  artdaq::Fragment::FragmentBytes(initial_payload_size,
65  ev_counter(),
66  fragment_id(),
67  ots::detail::FragmentType::STIB,
68  metadata));
69  // We now have a fragment to contain this event:
70  ev_counter_inc();
71  ots::STIBFragmentWriter thisFrag(*frags.back());
72  thisFrag.resize(4 * data.size()); // 4 bytes per 32-bit word
73  thisFrag.set_hdr_bunch_counter(bunch_counter);
74  thisFrag.set_hdr_trigger_counter(triggerCounter);
75  thisFrag.set_hdr_trigger_input0(triggerInput0);
76  thisFrag.set_hdr_trigger_input1(triggerInput1);
77  inData = false;
78  bunch_counter = 0;
79  triggerCounter = 0;
80  triggerInput0 = 0;
81  triggerInput1 = 0;
82  for(size_t ii = 0; ii < data.size(); ++ii)
83  {
84  *(thisFrag.dataBegin() + ii) = data[ii];
85  }
86  data.clear();
87  }
88 
89  if((byte0 & 0x10) == 0x10)
90  { // Bunch Counter Low
91  bunch_counter = (bunch_counter & 0xFFFFFF000000) + byte1 +
92  (byte2 << 8) + (byte3 << 16);
93  }
94  else if((byte0 & 0x20) == 0x20)
95  { // Bunch Counter High
96  bunch_counter = (bunch_counter & 0xFFFFFF) + ((uint64_t)byte1 << 24) +
97  ((uint64_t)byte2 << 32) + ((uint64_t)byte3 << 40);
98  }
99  else if((byte0 & 0xb0) == 0xb0)
100  { // Trigger Counter High
101  triggerCounter = (triggerCounter & 0xFFFF) + ((uint64_t)byte1 << 16) +
102  ((uint64_t)byte2 << 24) + ((uint64_t)byte3 << 32);
103  }
104  else if((byte0 & 0xa0) == 0xa0)
105  { // Trigger Counter Low
106  bunch_counter = (bunch_counter & 0xFFFFFFFFFF00) + byte1;
107  triggerCounter =
108  (triggerCounter & 0xFFFFFF0000) + byte2 + (byte3 << 8);
109  }
110  else if(((byte0 & 0xc0) == 0xc0) ||
111  ((byte0 & 0xd0) == 0xd0) || // Trigger Input
112  ((byte0 & 0xe0) == 0xe0) || ((byte0 & 0xf0) == 0xf0))
113  {
114  bunch_counter = (bunch_counter & 0xFFFFFFFFFF00) + byte1;
115  triggerInput0 = byte2;
116  triggerInput1 = byte3;
117  }
118  }
119  else if((byte0 & 1) == 1)
120  {
121  inData = true;
122  data.push_back((uint32_t)byte0 + ((uint32_t)byte1 << 8) +
123  ((uint64_t)byte2 << 16) + ((uint64_t)byte3 << 24));
124  }
125  }
126  }
127  return;
128 }
129 
130 // The following macro is defined in artdaq's GeneratorMacros.hh header
131 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(ots::STIBReceiver)