artdaq_demo  v3_10_02
CheckIntegrity_module.cc
1 // Class: CheckIntegrity
3 // Module Type: analyzer
4 // File: CheckIntegrity_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/ToyFragment.hh"
16 #include "artdaq-core/Data/ContainerFragment.hh"
17 #include "artdaq-core/Data/Fragment.hh"
18 
19 #include "TRACE/tracemf.h" // TLOG
20 #define TRACE_NAME "CheckIntegrity"
21 
22 #include <algorithm>
23 #include <cassert>
24 #include <cmath>
25 #include <fstream>
26 #include <iomanip>
27 #include <iostream>
28 #include <vector>
29 
30 namespace demo {
31 class CheckIntegrity;
32 } // namespace demo
33 
37 class demo::CheckIntegrity : public art::EDAnalyzer
38 {
39 public:
48  explicit CheckIntegrity(fhicl::ParameterSet const& pset);
49 
53  ~CheckIntegrity() override = default;
54 
59  void analyze(art::Event const& evt) override;
60 
61 private:
62  CheckIntegrity(CheckIntegrity const&) = delete;
63  CheckIntegrity(CheckIntegrity&&) = delete;
64  CheckIntegrity& operator=(CheckIntegrity const&) = delete;
65  CheckIntegrity& operator=(CheckIntegrity&&) = delete;
66 
67  std::string raw_data_label_;
68 };
69 
70 demo::CheckIntegrity::CheckIntegrity(fhicl::ParameterSet const& pset)
71  : EDAnalyzer(pset), raw_data_label_(pset.get<std::string>("raw_data_label"))
72 {}
73 
74 void demo::CheckIntegrity::analyze(art::Event const& evt)
75 {
76  artdaq::Fragments fragments;
77  artdaq::FragmentPtrs containerFragments;
78 
79  std::vector<art::Handle<artdaq::Fragments>> fragmentHandles;
80 #if ART_HEX_VERSION < 0x30900
81  evt.getManyByType(fragmentHandles);
82 #else
83  fragmentHandles = evt.getMany<std::vector<artdaq::Fragment>>();
84 #endif
85 
86  for (const auto& handle : fragmentHandles)
87  {
88  if (!handle.isValid() || handle->empty())
89  {
90  continue;
91  }
92 
93  if (handle->front().type() == artdaq::Fragment::ContainerFragmentType)
94  {
95  for (const auto& cont : *handle)
96  {
97  artdaq::ContainerFragment contf(cont);
98  if (contf.fragment_type() != demo::FragmentType::TOY1 && contf.fragment_type() != demo::FragmentType::TOY2)
99  {
100  break;
101  }
102 
103  for (size_t ii = 0; ii < contf.block_count(); ++ii)
104  {
105  containerFragments.push_back(contf[ii]);
106  fragments.push_back(*containerFragments.back());
107  }
108  }
109  }
110  else
111  {
112  if (handle->front().type() == demo::FragmentType::TOY1 || handle->front().type() == demo::FragmentType::TOY2)
113  {
114  for (auto frag : *handle)
115  {
116  fragments.emplace_back(frag);
117  }
118  }
119  }
120  }
121 
122  TLOG(TLVL_DEBUG) << "Run " << evt.run() << ", subrun " << evt.subRun() << ", event " << evt.event() << " has "
123  << fragments.size() << " fragment(s) of type TOY1 or TOY2";
124 
125  bool err = false;
126  for (const auto& frag : fragments)
127  {
128  ToyFragment bb(frag);
129 
130  if (bb.hdr_event_size() * sizeof(ToyFragment::Header::data_t) !=
131  frag.dataSize() * sizeof(artdaq::RawDataType))
132  {
133  TLOG(TLVL_ERROR) << "Error: in run " << evt.run() << ", subrun " << evt.subRun() << ", event "
134  << evt.event() << ", seqID " << frag.sequenceID() << ", fragID " << frag.fragmentID()
135  << ": Size mismatch!"
136  << " ToyFragment Header reports size of "
137  << bb.hdr_event_size() * sizeof(ToyFragment::Header::data_t)
138  << " bytes, Fragment report size of " << frag.dataSize() * sizeof(artdaq::RawDataType)
139  << " bytes.";
140  continue;
141  }
142 
143  {
144  auto adc_iter = bb.dataBeginADCs();
145  ToyFragment::adc_t expected_adc = 1;
146 
147  for (; adc_iter != bb.dataEndADCs(); adc_iter++, expected_adc++)
148  {
149  if (expected_adc > demo::ToyFragment::adc_range(frag.metadata<ToyFragment::Metadata>()->num_adc_bits))
150  {
151  expected_adc = 0;
152  }
153 
154  // ELF 7/10/18: Distribution type 2 is the monotonically-increasing one
155  if (bb.hdr_distribution_type() == 2 && *adc_iter != expected_adc)
156  {
157  TLOG(TLVL_ERROR) << "Error: in run " << evt.run() << ", subrun " << evt.subRun() << ", event "
158  << evt.event() << ", seqID " << frag.sequenceID() << ", fragID "
159  << frag.fragmentID() << ": expected an ADC value of " << expected_adc << ", got "
160  << *adc_iter;
161  err = true;
162  break;
163  }
164 
165  // ELF 7/10/18: As of now, distribution types 3 and 4 are uninitialized, and can therefore produce
166  // out-of-range counts.
167  if (bb.hdr_distribution_type() < 3 &&
168  *adc_iter > demo::ToyFragment::adc_range(frag.metadata<ToyFragment::Metadata>()->num_adc_bits))
169  {
170  TLOG(TLVL_ERROR) << "Error: in run " << evt.run() << ", subrun " << evt.subRun() << ", event "
171  << evt.event() << ", seqID " << frag.sequenceID() << ", fragID "
172  << frag.fragmentID() << ": " << *adc_iter
173  << " is out-of-range for this Fragment type";
174  err = true;
175  break;
176  }
177  }
178  }
179  }
180  if (!err)
181  {
182  TLOG(TLVL_DEBUG) << "In run " << evt.run() << ", subrun " << evt.subRun() << ", event " << evt.event()
183  << ", everything is fine";
184  }
185 }
186 
187 DEFINE_ART_MODULE(demo::CheckIntegrity) // NOLINT(performance-unnecessary-value-param)
~CheckIntegrity() override=default
Default destructor.
void analyze(art::Event const &evt) override
Analyze an event. Called by art for each event in run (based on command line options) ...
Demonstration art::EDAnalyzer which checks that all ToyFragment ADC counts are in the defined range...
CheckIntegrity(fhicl::ParameterSet const &pset)
CheckIntegrity Constructor.