CheckDataProductSize_module.cc
Go to the documentation of this file.
1 /**
2  * @file CheckDataProductSize_module.cc
3  * @brief Checks the size of a collection
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date June 3, 2016
6  * @ingroup RemoveIsolatedSpacePoints
7  *
8  */
9 
10 // LArSoft libraries
12 
13 // framework libraries
17 #include "art/Framework/Principal/Handle.h" // art::ValidHandle
19 #include "fhiclcpp/types/Atom.h"
21 #include "cetlib_except/exception.h"
22 
23 // C/C++ standard libraries
24 
25 
26 namespace lar {
27  namespace example {
28  namespace tests {
29 
30 
31  // BEGIN RemoveIsolatedSpacePoints group -----------------------------------
32  /// @ingroup RemoveIsolatedSpacePoints
33  /// @{
34  /**
35  * @brief Checks the size of the specified collection
36  *
37  * Throws an exception if the size of the collection in the specified
38  * data product is not as expected.
39  * The expectation can be expressed directly as a number of elements in the
40  * collection, or indirectly as the requirement that the tested collection
41  * has the same size as another one (still of `recob::SpacePoint`).
42  *
43  * Configuration parameters
44  * =========================
45  *
46  * * *inputLabel* (input tag, _mandatory_): label of the data product with
47  * the collection
48  * * *expectedSize* (integer): expected number of elements in
49  * the collection
50  * * *sameSizeAs* (inputTag): expected number of elements is the same as
51  * this other data product
52  *
53  */
55 
58 
59  public:
60 
61  struct Config {
62 
63  using Name = fhicl::Name;
65 
67  Name("inputLabel"),
68  Comment("label of the data product to be checked")
69  };
70 
72  Name("expectedSize"),
73  Comment("number of expected entries in the data product")
74  };
75 
77  Name("sameSizeAs"),
78  Comment("label of a data product with the same size as the input")
79  };
80 
81  }; // Config
82 
84 
85  /// Constructor; see the class documentation for the configuration
87  : art::EDAnalyzer(config)
88  , inputLabel(config().inputLabel())
89  {
90  doCheckExpectedSize = config().expectedSize(expectedSize);
91  doCheckSameSize = config().sameSizeAs(sameSizeAs);
92  consumes<std::vector<Data_t>>(inputLabel);
93  }
94 
95  virtual void analyze(art::Event const& event) override;
96 
97 
98  private:
99  art::InputTag inputLabel; ///< label of the input data product
100 
101  bool doCheckExpectedSize; ///< check that the size is the specified one
102  bool doCheckSameSize; ///< check that size is the same as another product
103 
104  size_t expectedSize; ///< expected size of the data product collection
105  art::InputTag sameSizeAs; ///< label of the data product with same size
106 
107  }; // class CheckDataProductSize
108 
109 
110  /// @}
111  // END RemoveIsolatedSpacePoints group -------------------------------------
112 
113 
114  } // namespace tests
115  } // namespace example
116 } // namespace lar
117 
118 
119 
120 //------------------------------------------------------------------------------
121 //--- CheckDataProductSize
122 //---
124 {
125 
126  //
127  // read the input
128  //
129  auto collectionHandle = event.getValidHandle<std::vector<Data_t>>(inputLabel);
130 
131  if (doCheckExpectedSize) {
132  if (collectionHandle->size() != expectedSize) {
133  throw cet::exception("CheckDataProductSize")
134  << "Data product '" << inputLabel.encode() << "' has "
135  << collectionHandle->size() << " elements, " << expectedSize
136  << " were expected!\n";
137  }
138  } // if doCheckExpectedSize
139 
140  if (doCheckSameSize) {
141  auto otherCollectionHandle
142  = event.getValidHandle<std::vector<OtherData_t>>(sameSizeAs);
143  if (collectionHandle->size() != otherCollectionHandle->size()) {
144  throw cet::exception("CheckDataProductSize")
145  << "Data product '" << inputLabel.encode() << "' has "
146  << collectionHandle->size() << " elements, " << expectedSize
147  << " were expected as in '" << sameSizeAs.encode() << "'!\n";
148  }
149  } // if doCheckSameSize
150 
151 } // lar::example::tests::CheckDataProductSize::analyze()
152 
153 
154 //------------------------------------------------------------------------------
156 
157 
158 //------------------------------------------------------------------------------
159 
art::InputTag sameSizeAs
label of the data product with same size
art::InputTag inputLabel
label of the input data product
ChannelGroupService::Name Name
virtual void analyze(art::Event const &event) override
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
bool doCheckSameSize
check that size is the same as another product
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
CheckDataProductSize(Parameters const &config)
Constructor; see the class documentation for the configuration.
Checks the size of the specified collection.
#define Comment
bool doCheckExpectedSize
check that the size is the specified one
LArSoft-specific namespace.
size_t expectedSize
expected size of the data product collection
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Event finding and building.