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