TestBitsOutput_module.cc
Go to the documentation of this file.
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <cstdlib>
15 #include <iostream>
16 #include <iterator>
17 #include <numeric>
18 #include <string>
19 
20 using namespace art;
21 
22 namespace {
23 
24  void
25  printBits(unsigned char c)
26  {
27  // cout << "HEX: "<< "0123456789ABCDEF"[((c >> 4) & 0xF)] << std::endl;
28  for (int i = 7; i >= 0; --i) {
29  int bit = ((c >> i) & 1);
30  std::cout << " " << bit;
31  }
32  }
33 
34  void
35  packIntoString(std::vector<unsigned char> const& source,
36  std::vector<unsigned char>& package)
37  {
38  unsigned int const packInOneByte{4};
39  // Two bits per HLT.
40  std::size_t const sizeOfPackage{
41  source.empty() ? 0u : 1u + ((source.size() - 1) / packInOneByte)};
42  package.resize(sizeOfPackage);
43  memset(&package[0], 0x00, sizeOfPackage);
44  for (unsigned int i = 0; i != source.size(); ++i) {
45  unsigned int whichByte = i / packInOneByte;
46  unsigned int indexWithinByte = i % packInOneByte;
47  package[whichByte] =
48  package[whichByte] | (source[i] << (indexWithinByte * 2));
49  }
50  // for (unsigned int i=0; i !=package.size() ; ++i)
51  // printBits(package[i]);
52  std::cout << std::endl;
53  }
54 
55 } // unnamed namespace
56 
57 namespace arttest {
58  class TestBitsOutput;
59 }
60 
62 public:
63  struct Config {
65  fhicl::Atom<int> bitMask{fhicl::Name("bitMask")};
66  fhicl::Atom<bool> expectTriggerResults{fhicl::Name("expectTriggerResults"),
67  true};
68  };
69 
70  using Parameters =
72  explicit TestBitsOutput(Parameters const&);
73 
74 private:
75  void write(art::EventPrincipal& e) override;
76  void
78  {}
79  void
81  {}
82 
83  void endJob() override;
84 
85  std::string name_{};
86  std::vector<unsigned char> hltbits_{};
87  art::ModuleDescription moduleDescription_{};
88  int bitMask_;
90 };
91 
92 // -----------------------------------------------------------------
93 
96  : art::OutputModule{ps().omConfig, ps.get_PSet()}
97  , bitMask_{ps().bitMask()}
98  , expectTriggerResults_{ps().expectTriggerResults()}
99 {}
100 
101 void
103 {
105  Event const ev{ep, mc};
106  // There should not be a TriggerResults object in the event if all
107  // three of the following requirements are met:
108  //
109  // 1. MakeTriggerResults has not been explicitly set true
110  // 2. There are no filter modules in any path
111  // 3. The input file of the job does not have a TriggerResults object
112  //
113  // The user of this test module is expected to know whether these
114  // conditions are met and let the module know if no TriggerResults
115  // object is expected using the configuration file. In this case,
116  // the next few lines of code will abort if a TriggerResults object
117  // is found.
118  if (!expectTriggerResults_) {
119  try {
121  // throw doesn't happen until we dereference
122  *prod;
123  }
124  catch (cet::exception const&) {
125  // We did not find one as expected, nothing else to test.
126  return;
127  }
128  std::cerr
129  << "\narttest::TestBitsOutput::write\n"
130  << "Expected there to be no TriggerResults object but we found one"
131  << std::endl;
132  abort();
133  }
134 
135  // Now deal with the other case where we expect the object to be
136  // present.
138  // TriggerResults objects should have no parents.
139  assert(prod.provenance()->parents().empty());
140  std::vector<unsigned char> vHltState;
141  // std::vector<std::string> const& hlts = ServiceHandle<TriggerNamesService
142  // const>{}->getTrigPaths();
143  std::vector<std::string> const& hlts =
145  unsigned int hltSize = hlts.size();
146  for (unsigned int i = 0; i != hltSize; ++i) {
147  vHltState.push_back(prod->at(i).state());
148  }
149  // Pack into member hltbits_
150  packIntoString(vHltState, hltbits_);
151  std::cout << "Size of hltbits:" << hltbits_.size() << std::endl;
152  auto intp = reinterpret_cast<char*>(&bitMask_);
153  bool matched{false};
154  for (int i = hltbits_.size() - 1; i != -1; --i) {
155  std::cout << std::endl << "Current Bits Mask byte:";
156  printBits(hltbits_[i]);
157  auto tmp = static_cast<unsigned char>(*(intp + i));
158  std::cout << std::endl << "Original Byte:";
159  printBits(tmp);
160  std::cout << std::endl;
161  if (tmp == hltbits_[i]) {
162  matched = true;
163  }
164  }
165  std::cout << "\n";
166  if (!matched && hltSize > 0) {
167  std::cerr << "\ncfg bitMask is different from event..aborting."
168  << std::endl;
169  abort();
170  }
171  std::cout << "\nSUCCESS: Found Matching Bits" << std::endl;
172 }
173 
174 void
176 {}
177 
void write(art::EventPrincipal &e) override
void writeSubRun(art::SubRunPrincipal &) override
art::ModuleDescription moduleDescription_
Handle< TriggerResults > getTriggerResults(Event const &e) const
Definition: Observer.cc:90
static const double ps
Definition: Units.h:103
std::string string
Definition: nybbler.cc:12
auto const & get_PSet() const
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:68
TestBitsOutput(Parameters const &)
fhicl::TableFragment< art::OutputModule::Config > omConfig
void writeRun(art::RunPrincipal &) override
static Globals * instance()
Definition: Globals.cc:26
std::vector< unsigned char > hltbits_
std::vector< std::string > const & triggerPathNames() const
Definition: Globals.cc:81
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
AdcRoiViewer::Name Name