File indexing completed on 2024-04-06 12:12:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "DataFormats/Common/interface/Handle.h"
0014 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0015 #include "FWCore/Framework/interface/Frameworkfwd.h"
0016 #include "FWCore/Framework/interface/GetterOfProducts.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/Framework/interface/global/OutputModule.h"
0019 #include "FWCore/Framework/interface/TypeMatch.h"
0020 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0023 #include "FWCore/Utilities/interface/BranchType.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025
0026 #include <atomic>
0027 #include <vector>
0028
0029 namespace edm {
0030
0031 class TestOutputWithGetterOfProductsGlobal : public global::OutputModule<RunCache<int>, LuminosityBlockCache<int>> {
0032 public:
0033 explicit TestOutputWithGetterOfProductsGlobal(ParameterSet const&);
0034 static void fillDescriptions(ConfigurationDescriptions&);
0035
0036 private:
0037 void write(EventForOutput const&) override;
0038 void writeLuminosityBlock(LuminosityBlockForOutput const&) override;
0039 void writeRun(RunForOutput const&) override;
0040
0041 std::shared_ptr<int> globalBeginRun(RunForOutput const&) const override;
0042 void globalEndRun(RunForOutput const&) const override;
0043
0044 std::shared_ptr<int> globalBeginLuminosityBlock(LuminosityBlockForOutput const&) const override;
0045 void globalEndLuminosityBlock(LuminosityBlockForOutput const&) const override;
0046 void endJob() override;
0047
0048 int sumThings(std::vector<Handle<edmtest::ThingCollection>> const& collections) const;
0049
0050 mutable std::atomic<unsigned int> sum_ = 0;
0051 unsigned int expectedSum_;
0052 GetterOfProducts<edmtest::ThingCollection> getterOfProductsRun_;
0053 GetterOfProducts<edmtest::ThingCollection> getterOfProductsLumi_;
0054 GetterOfProducts<edmtest::ThingCollection> getterOfProductsEvent_;
0055 };
0056
0057 TestOutputWithGetterOfProductsGlobal::TestOutputWithGetterOfProductsGlobal(ParameterSet const& pset)
0058 : global::OutputModuleBase(pset),
0059 global::OutputModule<RunCache<int>, LuminosityBlockCache<int>>(pset),
0060 expectedSum_(pset.getUntrackedParameter<unsigned int>("expectedSum")),
0061 getterOfProductsRun_(TypeMatch(), this, InRun),
0062 getterOfProductsLumi_(edm::TypeMatch(), this, InLumi),
0063 getterOfProductsEvent_(edm::TypeMatch(), this) {
0064 callWhenNewProductsRegistered([this](edm::BranchDescription const& bd) {
0065 getterOfProductsRun_(bd);
0066 getterOfProductsLumi_(bd);
0067 getterOfProductsEvent_(bd);
0068 });
0069 }
0070
0071 void TestOutputWithGetterOfProductsGlobal::fillDescriptions(ConfigurationDescriptions& descriptions) {
0072 ParameterSetDescription desc;
0073 OutputModule::fillDescription(desc);
0074 desc.addUntracked<unsigned int>("expectedSum", 0);
0075 descriptions.addDefault(desc);
0076 }
0077
0078 void TestOutputWithGetterOfProductsGlobal::write(EventForOutput const& event) {
0079 std::vector<Handle<edmtest::ThingCollection>> handles;
0080 getterOfProductsEvent_.fillHandles(event, handles);
0081 sum_ += sumThings(handles);
0082 }
0083
0084 void TestOutputWithGetterOfProductsGlobal::writeLuminosityBlock(LuminosityBlockForOutput const& lumi) {
0085 std::vector<Handle<edmtest::ThingCollection>> handles;
0086 getterOfProductsLumi_.fillHandles(lumi, handles);
0087 sum_ += sumThings(handles);
0088 }
0089
0090 void TestOutputWithGetterOfProductsGlobal::writeRun(RunForOutput const& run) {
0091 std::vector<Handle<edmtest::ThingCollection>> handles;
0092 getterOfProductsRun_.fillHandles(run, handles);
0093 sum_ += sumThings(handles);
0094 }
0095
0096 std::shared_ptr<int> TestOutputWithGetterOfProductsGlobal::globalBeginRun(RunForOutput const& run) const {
0097 std::vector<Handle<edmtest::ThingCollection>> handles;
0098 getterOfProductsRun_.fillHandles(run, handles);
0099 return std::make_shared<int>(sumThings(handles));
0100 }
0101
0102 void TestOutputWithGetterOfProductsGlobal::globalEndRun(RunForOutput const& run) const {
0103 sum_ += *runCache(run.index());
0104 std::vector<Handle<edmtest::ThingCollection>> handles;
0105 getterOfProductsRun_.fillHandles(run, handles);
0106 sum_ += sumThings(handles);
0107 }
0108
0109 std::shared_ptr<int> TestOutputWithGetterOfProductsGlobal::globalBeginLuminosityBlock(
0110 LuminosityBlockForOutput const& lumi) const {
0111 std::vector<Handle<edmtest::ThingCollection>> handles;
0112 getterOfProductsLumi_.fillHandles(lumi, handles);
0113 return std::make_shared<int>(sumThings(handles));
0114 }
0115
0116 void TestOutputWithGetterOfProductsGlobal::globalEndLuminosityBlock(LuminosityBlockForOutput const& lumi) const {
0117 sum_ += *luminosityBlockCache(lumi.index());
0118 std::vector<Handle<edmtest::ThingCollection>> handles;
0119 getterOfProductsLumi_.fillHandles(lumi, handles);
0120 sum_ += sumThings(handles);
0121 }
0122
0123 void TestOutputWithGetterOfProductsGlobal::endJob() {
0124 if (expectedSum_ != 0 && expectedSum_ != sum_.load()) {
0125 throw cms::Exception("TestFailure") << "TestOutputWithGetterOfProductsGlobal::endJob, sum = " << sum_.load()
0126 << " which is not equal to the expected value " << expectedSum_;
0127 }
0128 }
0129
0130 int TestOutputWithGetterOfProductsGlobal::sumThings(
0131 std::vector<Handle<edmtest::ThingCollection>> const& collections) const {
0132 int sum = 0;
0133 for (auto const& collection : collections) {
0134 for (auto const& thing : *collection) {
0135 sum += thing.a;
0136 }
0137 }
0138 return sum;
0139 }
0140 }
0141
0142 using edm::TestOutputWithGetterOfProductsGlobal;
0143 DEFINE_FWK_MODULE(TestOutputWithGetterOfProductsGlobal);