CMS 3D CMS Logo

TestFindProduct.cc
Go to the documentation of this file.
1 
2 // This test module will try to get IntProducts in Events,
3 // Lumis, Runs and ProcessBlocks. The number of IntProducts
4 // and their InputTags (label, instance, process) must be configured.
5 
6 // One can also configure an expected value for the sum of
7 // all the values in the IntProducts that are found. Note
8 // that an IntProduct is just a test product that simply
9 // contains a single integer.
10 
11 // If the expected products are not found or some other
12 // unexpected behavior occurs, an exception will be thrown.
13 
16 #include "DataFormats/TestObjects/interface/ToyProducts.h"
33 
34 #include <functional>
35 #include <iostream>
36 #include <memory>
37 #include <tuple>
38 #include <vector>
39 
40 namespace edmtest {
41 
42  class TestFindProduct : public edm::one::EDAnalyzer<edm::one::WatchRuns,
43  edm::one::WatchLuminosityBlocks,
44  edm::WatchProcessBlock,
45  edm::InputProcessBlockCache<int, long long int>> {
46  public:
47  explicit TestFindProduct(edm::ParameterSet const& pset);
48  ~TestFindProduct() override;
49 
50  void analyze(edm::Event const& e, edm::EventSetup const& es) override;
51  void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
52  void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
53  void beginRun(edm::Run const&, edm::EventSetup const&) override;
54  void endRun(edm::Run const&, edm::EventSetup const&) override;
55  void beginProcessBlock(edm::ProcessBlock const&) override;
56  void accessInputProcessBlock(edm::ProcessBlock const&) override;
57  void endProcessBlock(edm::ProcessBlock const&) override;
58  void endJob() override;
59 
60  private:
61  std::vector<edm::InputTag> inputTags_;
64  int sum_;
65  std::vector<edm::InputTag> inputTagsNotFound_;
67  std::vector<edm::InputTag> inputTagsView_;
70 
71  std::vector<edm::InputTag> inputTagsUInt64_;
72  std::vector<edm::InputTag> inputTagsEndLumi_;
73  std::vector<edm::InputTag> inputTagsEndRun_;
74  std::vector<edm::InputTag> inputTagsBeginProcessBlock_;
75  std::vector<edm::InputTag> inputTagsInputProcessBlock_;
76  std::vector<edm::InputTag> inputTagsEndProcessBlock_;
77  std::vector<edm::InputTag> inputTagsEndProcessBlock2_;
78  std::vector<edm::InputTag> inputTagsEndProcessBlock3_;
79  std::vector<edm::InputTag> inputTagsEndProcessBlock4_;
80 
81  std::vector<edm::EDGetTokenT<IntProduct>> tokens_;
82  std::vector<edm::EDGetTokenT<IntProduct>> tokensNotFound_;
83  std::vector<edm::EDGetTokenT<edm::View<int>>> tokensView_;
84  std::vector<edm::EDGetTokenT<UInt64Product>> tokensUInt64_;
85  std::vector<edm::EDGetTokenT<IntProduct>> tokensEndLumi_;
86  std::vector<edm::EDGetTokenT<IntProduct>> tokensEndRun_;
87  std::vector<edm::EDGetTokenT<IntProduct>> tokensBeginProcessBlock_;
88  std::vector<edm::EDGetTokenT<IntProduct>> tokensInputProcessBlock_;
89  std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock_;
90  std::vector<edm::EDGetToken> tokensEndProcessBlock2_;
91  std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock3_;
92  std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock4_;
93 
95 
96  }; // class TestFindProduct
97 
98  //--------------------------------------------------------------------
99  //
100  // Implementation details
101 
103  : inputTags_(pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTags")),
104  expectedSum_(pset.getUntrackedParameter<int>("expectedSum", 0)),
105  expectedCache_(pset.getUntrackedParameter<int>("expectedCache", 0)),
106  sum_(0),
107  inputTagsNotFound_(),
108  getByTokenFirst_(pset.getUntrackedParameter<bool>("getByTokenFirst", false)),
109  inputTagsView_(),
110  runProducerParameterCheck_(pset.getUntrackedParameter<bool>("runProducerParameterCheck", false)),
111  testGetterOfProducts_(pset.getUntrackedParameter<bool>("testGetterOfProducts", false)),
112  getterOfProducts_(edm::ProcessMatch("*"), this, edm::InProcess) {
113  if (testGetterOfProducts_) {
115  }
116  std::vector<edm::InputTag> emptyTagVector;
117  inputTagsNotFound_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsNotFound", emptyTagVector);
118  inputTagsView_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsView", emptyTagVector);
119  inputTagsUInt64_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsUInt64", emptyTagVector);
120  inputTagsEndLumi_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndLumi", emptyTagVector);
121  inputTagsEndRun_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndRun", emptyTagVector);
123  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsBeginProcessBlock", emptyTagVector);
125  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsInputProcessBlock", emptyTagVector);
127  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock", emptyTagVector);
129  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock2", emptyTagVector);
131  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock3", emptyTagVector);
133  pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock4", emptyTagVector);
134 
135  for (auto const& tag : inputTags_) {
136  tokens_.push_back(consumes<IntProduct>(tag));
137  }
138  for (auto const& tag : inputTagsNotFound_) {
139  tokensNotFound_.push_back(consumes<IntProduct>(tag));
140  }
141  for (auto const& tag : inputTagsView_) {
142  tokensView_.push_back(consumes<edm::View<int>>(tag));
143  }
144  for (auto const& tag : inputTagsUInt64_) {
145  tokensUInt64_.push_back(consumes<UInt64Product>(tag));
146  }
147  for (auto const& tag : inputTagsEndLumi_) {
148  tokensEndLumi_.push_back(consumes<IntProduct, edm::InLumi>(tag));
149  }
150  for (auto const& tag : inputTagsEndRun_) {
151  tokensEndRun_.push_back(consumes<IntProduct, edm::InRun>(tag));
152  }
153  for (auto const& tag : inputTagsBeginProcessBlock_) {
154  tokensBeginProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
155  }
156  for (auto const& tag : inputTagsInputProcessBlock_) {
157  tokensInputProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
158  }
159  for (auto const& tag : inputTagsEndProcessBlock_) {
160  tokensEndProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
161  }
162  for (auto const& tag : inputTagsEndProcessBlock2_) {
163  tokensEndProcessBlock2_.push_back(consumes<IntProduct, edm::InProcess>(tag));
164  }
165  for (auto const& tag : inputTagsEndProcessBlock3_) {
166  tokensEndProcessBlock3_.push_back(consumes<IntProduct, edm::InProcess>(tag));
167  }
168  for (auto const& tag : inputTagsEndProcessBlock4_) {
169  tokensEndProcessBlock4_.push_back(consumes<IntProduct, edm::InProcess>(tag));
170  }
171 
172  if (!tokensInputProcessBlock_.empty()) {
173  registerProcessBlockCacheFiller<int>(
175  [this](edm::ProcessBlock const& processBlock, std::shared_ptr<int> const& previousCache) {
176  auto returnValue = std::make_shared<int>(0);
177  for (auto const& token : tokensInputProcessBlock_) {
178  *returnValue += processBlock.get(token).value;
179  }
180  return returnValue;
181  });
182  registerProcessBlockCacheFiller<1>(
184  [this](edm::ProcessBlock const& processBlock, std::shared_ptr<long long int> const& previousCache) {
185  auto returnValue = std::make_shared<long long int>(0);
186  for (auto const& token : tokensInputProcessBlock_) {
187  *returnValue += processBlock.get(token).value;
188  }
189  return returnValue;
190  });
191  }
192  }
193 
195 
200  edm::Handle<edm::View<int>> hViewToken;
201 
202  std::vector<edm::EDGetTokenT<IntProduct>>::const_iterator iToken = tokens_.begin();
203  for (std::vector<edm::InputTag>::const_iterator iter = inputTags_.begin(), iEnd = inputTags_.end(); iter != iEnd;
204  ++iter, ++iToken) {
205  if (getByTokenFirst_) {
206  event.getByToken(*iToken, hToken);
207  *hToken;
208  }
209 
210  event.getByLabel(*iter, h);
211  sum_ += h->value;
212 
213  event.getByToken(*iToken, hToken);
214  if (h->value != hToken->value) {
215  throw cms::Exception("TestFail")
216  << "TestFindProduct::analyze getByLabel and getByToken return inconsistent results";
217  }
218 
220  edm::ParameterSet const* producerPset =
221  edm::getProducerParameterSet(*hToken.provenance(), event.processHistory());
222  int par = producerPset->getParameter<int>("ivalue");
223  // These expected values are just from knowing the values in the
224  // configuration files for this test.
225  int expectedParameterValue = 3;
226  if (!iter->process().empty()) {
227  if (event.run() == 1) {
228  expectedParameterValue = 1;
229  } else {
230  expectedParameterValue = 2;
231  }
232  }
233  if (par != expectedParameterValue) {
234  throw cms::Exception("TestFail") << "TestFindProduct::analyze unexpected value from producer parameter set";
235  }
236  }
237  }
238  iToken = tokensNotFound_.begin();
239  for (std::vector<edm::InputTag>::const_iterator iter = inputTagsNotFound_.begin(), iEnd = inputTagsNotFound_.end();
240  iter != iEnd;
241  ++iter, ++iToken) {
242  event.getByLabel(*iter, h);
243  if (h.isValid()) {
244  throw cms::Exception("TestFail")
245  << "TestFindProduct::analyze: getByLabel found a product that should not be found "
246  << h.provenance()->moduleLabel();
247  }
248 
249  event.getByToken(*iToken, hToken);
250  if (hToken.isValid()) {
251  throw cms::Exception("TestFail")
252  << "TestFindProduct::analyze: getByToken found a product that should not be found "
253  << hToken.provenance()->moduleLabel();
254  }
255  }
256  std::vector<edm::EDGetTokenT<edm::View<int>>>::const_iterator iTokenView = tokensView_.begin();
257  for (std::vector<edm::InputTag>::const_iterator iter = inputTagsView_.begin(), iEnd = inputTagsView_.end();
258  iter != iEnd;
259  ++iter, ++iTokenView) {
260  if (getByTokenFirst_) {
261  event.getByToken(*iTokenView, hViewToken);
262  *hViewToken;
263  }
264 
265  event.getByLabel(*iter, hView);
266  sum_ += hView->at(0);
267 
268  event.getByToken(*iTokenView, hViewToken);
269  if (hView->at(0) != hViewToken->at(0)) {
270  throw cms::Exception("TestFail")
271  << "TestFindProduct::analyze getByLabel and getByToken return inconsistent results";
272  }
273  }
274 
275  // Get these also and add them into the sum
277  for (auto const& token : tokensUInt64_) {
278  event.getByToken(token, h64);
279  sum_ += h64->value;
280  }
281 
282  if (expectedCache_ != 0) {
283  std::tuple<edm::CacheHandle<int>, edm::CacheHandle<long long int>> valueTuple = processBlockCaches(event);
284  {
285  edm::CacheHandle<int> value = std::get<0>(valueTuple);
286  if (*value != expectedCache_) {
287  throw cms::Exception("TestFail") << "TestFindProduct::analyze 0 ProcessBlock cache has unexpected value "
288  << *value << " expected = " << expectedCache_;
289  }
290  }
291  {
292  edm::CacheHandle<long long int> value = std::get<1>(valueTuple);
293  if (*value != expectedCache_) {
294  throw cms::Exception("TestFail") << "TestFindProduct::analyze 1 ProcessBlock cache has unexpected value "
295  << *value << " expected = " << expectedCache_;
296  }
297  }
298  }
299  }
300 
302 
304  // Get these also and add them into the sum
306  for (auto const& token : tokensEndLumi_) {
307  lb.getByToken(token, h);
308  sum_ += h->value;
309  }
310  }
311 
313 
315  // Get these also and add them into the sum
317  for (auto const& token : tokensEndRun_) {
318  run.getByToken(token, h);
319  sum_ += h->value;
320  }
321  }
322 
324  for (auto const& token : tokensBeginProcessBlock_) {
325  sum_ += processBlock.get(token).value;
326  }
327  if (testGetterOfProducts_) {
328  std::vector<edm::Handle<IntProduct>> handles;
329  getterOfProducts_.fillHandles(processBlock, handles);
330  for (auto const& intHandle : handles) {
331  sum_ += intHandle->value;
332  }
333  }
334  }
335 
337  for (auto const& token : tokensInputProcessBlock_) {
338  int value = processBlock.get(token).value;
339  sum_ += value;
340  }
341  }
342 
344  std::vector<int> values;
345  for (auto const& token : tokensEndProcessBlock_) {
346  int value = processBlock.get(token).value;
347  values.push_back(value);
348  sum_ += value;
349  }
351  unsigned int i = 0;
352  for (auto val : values) {
353  if (i < tokensEndProcessBlock2_.size()) {
354  processBlock.getByToken(tokensEndProcessBlock2_[i], h);
355  if (h->value != val + 2) {
356  throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 2, received unexpected value";
357  }
358  }
359  if (i < tokensEndProcessBlock3_.size()) {
360  processBlock.getByToken(tokensEndProcessBlock3_[i], h);
361  if (h->value != val + 3) {
362  throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 3, received unexpected value";
363  }
364  }
365  if (i < tokensEndProcessBlock4_.size()) {
366  h = processBlock.getHandle(tokensEndProcessBlock4_[i]);
367  if (h->value != val + 4) {
368  throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 4, received unexpected value";
369  }
370  }
371  ++i;
372  }
373  if (testGetterOfProducts_) {
374  std::vector<edm::Handle<IntProduct>> handles;
375  getterOfProducts_.fillHandles(processBlock, handles);
376  for (auto const& intHandle : handles) {
377  sum_ += intHandle->value;
378  }
379  }
380  }
381 
383  std::cout << "TestFindProduct sum = " << sum_ << std::endl;
384  if (expectedSum_ != 0 && sum_ != expectedSum_) {
385  throw cms::Exception("TestFail")
386  << "TestFindProduct::endJob - Sum of test object values does not equal expected value";
387  }
388  }
389 
390 } // namespace edmtest
391 
std::vector< edm::InputTag > inputTagsUInt64_
std::vector< edm::InputTag > inputTagsEndProcessBlock2_
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::vector< edm::EDGetToken > tokensEndProcessBlock2_
std::vector< edm::InputTag > inputTagsEndProcessBlock_
std::vector< edm::EDGetTokenT< UInt64Product > > tokensUInt64_
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
std::vector< edm::EDGetTokenT< IntProduct > > tokensBeginProcessBlock_
PROD const & get(EDGetTokenT< PROD > token) const noexcept(false)
Definition: ProcessBlock.h:135
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: ProcessBlock.h:129
void beginRun(edm::Run const &, edm::EventSetup const &) override
Provenance const * provenance() const
Definition: HandleBase.h:74
std::vector< edm::EDGetTokenT< IntProduct > > tokensNotFound_
std::vector< edm::InputTag > inputTagsView_
std::vector< edm::EDGetTokenT< IntProduct > > tokens_
void beginProcessBlock(edm::ProcessBlock const &) override
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
std::vector< edm::InputTag > inputTagsEndLumi_
TestFindProduct(edm::ParameterSet const &pset)
edm::GetterOfProducts< IntProduct > getterOfProducts_
std::vector< BaseVolumeHandle * > handles
std::vector< edm::EDGetTokenT< IntProduct > > tokensEndRun_
std::vector< edm::EDGetTokenT< IntProduct > > tokensInputProcessBlock_
std::vector< edm::EDGetTokenT< IntProduct > > tokensEndProcessBlock4_
void endRun(edm::Run const &, edm::EventSetup const &) override
std::vector< edm::InputTag > inputTagsInputProcessBlock_
void callWhenNewProductsRegistered(std::function< void(BranchDescription const &)> const &func)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
void fillHandles(ProductContainer const &productContainer, std::vector< edm::Handle< T >> &handles) const
ParameterSet const * getProducerParameterSet(Provenance const &provenance, ProcessHistory const &)
std::vector< edm::InputTag > inputTags_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
std::vector< edm::EDGetTokenT< edm::View< int > > > tokensView_
std::vector< edm::EDGetTokenT< IntProduct > > tokensEndProcessBlock_
std::vector< edm::InputTag > inputTagsEndProcessBlock4_
bool isValid() const
Definition: HandleBase.h:70
void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
void analyze(edm::Event const &e, edm::EventSetup const &es) override
void accessInputProcessBlock(edm::ProcessBlock const &) override
HLT enums.
std::vector< edm::InputTag > inputTagsEndProcessBlock3_
std::vector< edm::InputTag > inputTagsNotFound_
void endProcessBlock(edm::ProcessBlock const &) override
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
std::vector< edm::EDGetTokenT< IntProduct > > tokensEndProcessBlock3_
std::vector< edm::EDGetTokenT< IntProduct > > tokensEndLumi_
std::vector< edm::InputTag > inputTagsEndRun_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: ProcessBlock.h:107
Definition: event.py:1
Definition: Run.h:45
std::vector< edm::InputTag > inputTagsBeginProcessBlock_