TestOutputWithGetterOfProductsLimited

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
// -*- C++ -*-
//
// Package:    FWCore/Integration
// Class:      TestOutputWithGetterOfProductsLimited
//
/**\class edm::TestOutputWithGetterOfProductsLimited

  Description: Test GetterOfProducts with OutputModule
*/
// Original Author:  W. David Dagenhart
//         Created:  26 April 2023

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/TestObjects/interface/ThingCollection.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/GetterOfProducts.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/limited/OutputModule.h"
#include "FWCore/Framework/interface/TypeMatch.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/BranchType.h"
#include "FWCore/Utilities/interface/Exception.h"

#include <atomic>
#include <vector>

namespace edm {

  class TestOutputWithGetterOfProductsLimited : public limited::OutputModule<RunCache<int>, LuminosityBlockCache<int>> {
  public:
    explicit TestOutputWithGetterOfProductsLimited(ParameterSet const&);
    static void fillDescriptions(ConfigurationDescriptions&);

  private:
    void write(EventForOutput const&) override;
    void writeLuminosityBlock(LuminosityBlockForOutput const&) override;
    void writeRun(RunForOutput const&) override;

    std::shared_ptr<int> globalBeginRun(RunForOutput const&) const override;
    void globalEndRun(RunForOutput const&) const override;

    std::shared_ptr<int> globalBeginLuminosityBlock(LuminosityBlockForOutput const&) const override;
    void globalEndLuminosityBlock(LuminosityBlockForOutput const&) const override;
    void endJob() override;

    int sumThings(std::vector<Handle<edmtest::ThingCollection>> const& collections) const;

    mutable std::atomic<unsigned int> sum_ = 0;
    unsigned int expectedSum_;
    GetterOfProducts<edmtest::ThingCollection> getterOfProductsRun_;
    GetterOfProducts<edmtest::ThingCollection> getterOfProductsLumi_;
    GetterOfProducts<edmtest::ThingCollection> getterOfProductsEvent_;
  };

  TestOutputWithGetterOfProductsLimited::TestOutputWithGetterOfProductsLimited(ParameterSet const& pset)
      : limited::OutputModuleBase(pset),
        limited::OutputModule<RunCache<int>, LuminosityBlockCache<int>>(pset),
        expectedSum_(pset.getUntrackedParameter<unsigned int>("expectedSum")),
        getterOfProductsRun_(TypeMatch(), this, InRun),
        getterOfProductsLumi_(edm::TypeMatch(), this, InLumi),
        getterOfProductsEvent_(edm::TypeMatch(), this) {
    callWhenNewProductsRegistered([this](edm::ProductDescription const& bd) {
      getterOfProductsRun_(bd);
      getterOfProductsLumi_(bd);
      getterOfProductsEvent_(bd);
    });
  }

  void TestOutputWithGetterOfProductsLimited::fillDescriptions(ConfigurationDescriptions& descriptions) {
    ParameterSetDescription desc;
    OutputModule::fillDescription(desc);
    desc.addUntracked<unsigned int>("expectedSum", 0);
    descriptions.addDefault(desc);
  }

  void TestOutputWithGetterOfProductsLimited::write(EventForOutput const& event) {
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsEvent_.fillHandles(event, handles);
    sum_ += sumThings(handles);
  }

  void TestOutputWithGetterOfProductsLimited::writeLuminosityBlock(LuminosityBlockForOutput const& lumi) {
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsLumi_.fillHandles(lumi, handles);
    sum_ += sumThings(handles);
  }

  void TestOutputWithGetterOfProductsLimited::writeRun(RunForOutput const& run) {
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsRun_.fillHandles(run, handles);
    sum_ += sumThings(handles);
  }

  std::shared_ptr<int> TestOutputWithGetterOfProductsLimited::globalBeginRun(RunForOutput const& run) const {
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsRun_.fillHandles(run, handles);
    return std::make_shared<int>(sumThings(handles));
  }

  void TestOutputWithGetterOfProductsLimited::globalEndRun(RunForOutput const& run) const {
    sum_ += *runCache(run.index());
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsRun_.fillHandles(run, handles);
    sum_ += sumThings(handles);
  }

  std::shared_ptr<int> TestOutputWithGetterOfProductsLimited::globalBeginLuminosityBlock(
      LuminosityBlockForOutput const& lumi) const {
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsLumi_.fillHandles(lumi, handles);
    return std::make_shared<int>(sumThings(handles));
  }

  void TestOutputWithGetterOfProductsLimited::globalEndLuminosityBlock(LuminosityBlockForOutput const& lumi) const {
    sum_ += *luminosityBlockCache(lumi.index());
    std::vector<Handle<edmtest::ThingCollection>> handles;
    getterOfProductsLumi_.fillHandles(lumi, handles);
    sum_ += sumThings(handles);
  }

  void TestOutputWithGetterOfProductsLimited::endJob() {
    if (expectedSum_ != 0 && expectedSum_ != sum_.load()) {
      throw cms::Exception("TestFailure") << "TestOutputWithGetterOfProductsLimited::endJob, sum = " << sum_.load()
                                          << " which is not equal to the expected value " << expectedSum_;
    }
  }

  int TestOutputWithGetterOfProductsLimited::sumThings(
      std::vector<Handle<edmtest::ThingCollection>> const& collections) const {
    int sum = 0;
    for (auto const& collection : collections) {
      for (auto const& thing : *collection) {
        sum += thing.a;
      }
    }
    return sum;
  }
}  // namespace edm

using edm::TestOutputWithGetterOfProductsLimited;
DEFINE_FWK_MODULE(TestOutputWithGetterOfProductsLimited);