Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:36

0001 // -*- C++ -*-
0002 //
0003 // Package:    FWCore/Integration
0004 // Class:      TestOutputWithGetterOfProducts
0005 //
0006 /**\class edm::TestOutputWithGetterOfProducts
0007 
0008   Description: Test GetterOfProducts with OutputModule
0009 */
0010 // Original Author:  W. David Dagenhart
0011 //         Created:  26 April 2023
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/one/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 <vector>
0027 
0028 namespace edm {
0029 
0030   class TestOutputWithGetterOfProducts : public one::OutputModule<RunCache<int>, LuminosityBlockCache<int>> {
0031   public:
0032     explicit TestOutputWithGetterOfProducts(ParameterSet const&);
0033     static void fillDescriptions(ConfigurationDescriptions&);
0034 
0035   private:
0036     void write(EventForOutput const&) override;
0037     void writeLuminosityBlock(LuminosityBlockForOutput const&) override;
0038     void writeRun(RunForOutput const&) override;
0039 
0040     std::shared_ptr<int> globalBeginRun(RunForOutput const&) const override;
0041     void globalEndRun(RunForOutput const&) override;
0042 
0043     std::shared_ptr<int> globalBeginLuminosityBlock(LuminosityBlockForOutput const&) const override;
0044     void globalEndLuminosityBlock(LuminosityBlockForOutput const&) override;
0045     void endJob() override;
0046 
0047     int sumThings(std::vector<Handle<edmtest::ThingCollection>> const& collections) const;
0048 
0049     unsigned int sum_ = 0;
0050     unsigned int expectedSum_;
0051     GetterOfProducts<edmtest::ThingCollection> getterOfProductsRun_;
0052     GetterOfProducts<edmtest::ThingCollection> getterOfProductsLumi_;
0053     GetterOfProducts<edmtest::ThingCollection> getterOfProductsEvent_;
0054   };
0055 
0056   TestOutputWithGetterOfProducts::TestOutputWithGetterOfProducts(ParameterSet const& pset)
0057       : one::OutputModuleBase(pset),
0058         one::OutputModule<RunCache<int>, LuminosityBlockCache<int>>(pset),
0059         expectedSum_(pset.getUntrackedParameter<unsigned int>("expectedSum")),
0060         getterOfProductsRun_(TypeMatch(), this, InRun),
0061         getterOfProductsLumi_(edm::TypeMatch(), this, InLumi),
0062         getterOfProductsEvent_(edm::TypeMatch(), this) {
0063     callWhenNewProductsRegistered([this](edm::BranchDescription const& bd) {
0064       getterOfProductsRun_(bd);
0065       getterOfProductsLumi_(bd);
0066       getterOfProductsEvent_(bd);
0067     });
0068   }
0069 
0070   void TestOutputWithGetterOfProducts::fillDescriptions(ConfigurationDescriptions& descriptions) {
0071     ParameterSetDescription desc;
0072     OutputModule::fillDescription(desc);
0073     desc.addUntracked<unsigned int>("expectedSum", 0);
0074     descriptions.addDefault(desc);
0075   }
0076 
0077   void TestOutputWithGetterOfProducts::write(EventForOutput const& event) {
0078     std::vector<Handle<edmtest::ThingCollection>> handles;
0079     getterOfProductsEvent_.fillHandles(event, handles);
0080     sum_ += sumThings(handles);
0081   }
0082 
0083   void TestOutputWithGetterOfProducts::writeLuminosityBlock(LuminosityBlockForOutput const& lumi) {
0084     std::vector<Handle<edmtest::ThingCollection>> handles;
0085     getterOfProductsLumi_.fillHandles(lumi, handles);
0086     sum_ += sumThings(handles);
0087   }
0088 
0089   void TestOutputWithGetterOfProducts::writeRun(RunForOutput const& run) {
0090     std::vector<Handle<edmtest::ThingCollection>> handles;
0091     getterOfProductsRun_.fillHandles(run, handles);
0092     sum_ += sumThings(handles);
0093   }
0094 
0095   std::shared_ptr<int> TestOutputWithGetterOfProducts::globalBeginRun(RunForOutput const& run) const {
0096     std::vector<Handle<edmtest::ThingCollection>> handles;
0097     getterOfProductsRun_.fillHandles(run, handles);
0098     return std::make_shared<int>(sumThings(handles));
0099   }
0100 
0101   void TestOutputWithGetterOfProducts::globalEndRun(RunForOutput const& run) {
0102     sum_ += *runCache(run.index());
0103     std::vector<Handle<edmtest::ThingCollection>> handles;
0104     getterOfProductsRun_.fillHandles(run, handles);
0105     sum_ += sumThings(handles);
0106   }
0107 
0108   std::shared_ptr<int> TestOutputWithGetterOfProducts::globalBeginLuminosityBlock(
0109       LuminosityBlockForOutput const& lumi) const {
0110     std::vector<Handle<edmtest::ThingCollection>> handles;
0111     getterOfProductsLumi_.fillHandles(lumi, handles);
0112     return std::make_shared<int>(sumThings(handles));
0113   }
0114 
0115   void TestOutputWithGetterOfProducts::globalEndLuminosityBlock(LuminosityBlockForOutput const& lumi) {
0116     sum_ += *luminosityBlockCache(lumi.index());
0117     std::vector<Handle<edmtest::ThingCollection>> handles;
0118     getterOfProductsLumi_.fillHandles(lumi, handles);
0119     sum_ += sumThings(handles);
0120   }
0121 
0122   void TestOutputWithGetterOfProducts::endJob() {
0123     if (expectedSum_ != 0 && expectedSum_ != sum_) {
0124       throw cms::Exception("TestFailure") << "TestOutputWithGetterOfProducts::endJob, sum = " << sum_
0125                                           << " which is not equal to the expected value " << expectedSum_;
0126     }
0127   }
0128 
0129   int TestOutputWithGetterOfProducts::sumThings(std::vector<Handle<edmtest::ThingCollection>> const& collections) const {
0130     int sum = 0;
0131     for (auto const& collection : collections) {
0132       for (auto const& thing : *collection) {
0133         sum += thing.a;
0134       }
0135     }
0136     return sum;
0137   }
0138 }  // namespace edm
0139 
0140 using edm::TestOutputWithGetterOfProducts;
0141 DEFINE_FWK_MODULE(TestOutputWithGetterOfProducts);