Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-24 04:45:01

0001 #include "FWCore/TestProcessor/interface/TestProcessor.h"
0002 #include "FWCore/TestProcessor/interface/TestSourceProcessor.h"
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 
0005 #include "DataFormats/TestObjects/interface/Thing.h"
0006 #include <vector>
0007 #include <filesystem>
0008 #include "catch.hpp"
0009 
0010 static constexpr auto s_tag = "[PoolOutputSource]";
0011 
0012 namespace {
0013   std::string setOutputFile(std::string const& iConfig, std::string const& iFileName) {
0014     using namespace std::string_literals;
0015     return iConfig + "\nprocess.out.fileName = '"s + iFileName + "'\n";
0016   }
0017 
0018   std::string setInputFile(std::string const& iConfig, std::string const& iFileName) {
0019     using namespace std::string_literals;
0020     return iConfig + "\nprocess.source.fileNames = ['file:"s + iFileName + "']\n";
0021   }
0022 }  // namespace
0023 TEST_CASE("Tests of PoolOuput -> PoolSource", s_tag) {
0024   const std::string baseOutConfig{
0025       R"_(from FWCore.TestProcessor.TestProcess import *
0026 process = TestProcess()
0027 process.out = cms.OutputModule('PoolOutputModule',
0028                       fileName = cms.untracked.string('')
0029 )
0030 process.add_(cms.Service("InitRootHandlers"))
0031 process.add_(cms.Service("JobReportService"))
0032 
0033 process.moduleToTest(process.out)
0034 )_"};
0035 
0036   const std::string baseSourceConfig{
0037       R"_(from FWCore.TestProcessor.TestSourceProcess import *
0038 process = TestSourceProcess()
0039 process.source = cms.Source("PoolSource", fileNames = cms.untracked.vstring(''))
0040 process.add_(cms.Service("InitRootHandlers"))
0041 process.add_(cms.Service("SiteLocalConfigService"))
0042 process.add_(cms.Service("JobReportService"))
0043     )_"};
0044 
0045   SECTION("OneEmptyEvent") {
0046     const std::string fileName = "one_event.root";
0047     {
0048       auto configString = setOutputFile(baseOutConfig, fileName);
0049 
0050       edm::test::TestProcessor::Config config{configString};
0051 
0052       edm::test::TestProcessor tester(config);
0053       tester.test();
0054     }
0055     {
0056       auto config = setInputFile(baseSourceConfig, fileName);
0057       edm::test::TestSourceProcessor tester(config);
0058 
0059       {
0060         auto n = tester.findNextTransition();
0061         REQUIRE(n == edm::InputSource::ItemType::IsFile);
0062         auto f = tester.openFile();
0063         REQUIRE(bool(f));
0064       }
0065       {
0066         auto n = tester.findNextTransition();
0067         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsRun);
0068         auto r = tester.readRun();
0069         REQUIRE(r.run() == 1);
0070       }
0071       {
0072         auto n = tester.findNextTransition();
0073         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsLumi);
0074         auto r = tester.readLuminosityBlock();
0075         REQUIRE(r.run() == 1);
0076         REQUIRE(r.luminosityBlock() == 1);
0077       }
0078       {
0079         auto n = tester.findNextTransition();
0080         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsEvent);
0081         auto r = tester.readEvent();
0082         REQUIRE(r.run() == 1);
0083         REQUIRE(r.luminosityBlock() == 1);
0084         REQUIRE(r.event() == 1);
0085       }
0086       {
0087         auto n = tester.findNextTransition();
0088         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsStop);
0089       }
0090     }
0091     std::filesystem::remove(fileName);
0092   }
0093 
0094   SECTION("EventWithThing") {
0095     const std::string fileName = "thing.root";
0096     {
0097       auto configString = setOutputFile(baseOutConfig, fileName);
0098 
0099       edm::test::TestProcessor::Config config{configString};
0100       auto thingToken = config.produces<std::vector<edmtest::Thing>>("thing");
0101 
0102       edm::test::TestProcessor tester(config);
0103       tester.test(std::make_pair(thingToken, std::make_unique<std::vector<edmtest::Thing>>(1, edmtest::Thing{1})));
0104     }
0105     {
0106       auto config = setInputFile(baseSourceConfig, fileName);
0107       edm::test::TestSourceProcessor tester(config);
0108 
0109       {
0110         auto n = tester.findNextTransition();
0111         REQUIRE(n == edm::InputSource::ItemType::IsFile);
0112         auto f = tester.openFile();
0113         REQUIRE(bool(f));
0114       }
0115       {
0116         auto n = tester.findNextTransition();
0117         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsRun);
0118         auto r = tester.readRun();
0119         REQUIRE(r.run() == 1);
0120       }
0121       {
0122         auto n = tester.findNextTransition();
0123         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsLumi);
0124         auto r = tester.readLuminosityBlock();
0125         REQUIRE(r.run() == 1);
0126         REQUIRE(r.luminosityBlock() == 1);
0127       }
0128       {
0129         auto n = tester.findNextTransition();
0130         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsEvent);
0131         auto r = tester.readEvent();
0132         REQUIRE(r.run() == 1);
0133         REQUIRE(r.luminosityBlock() == 1);
0134         REQUIRE(r.event() == 1);
0135         auto v = r.get<std::vector<edmtest::Thing>>("thing", "", "TEST");
0136         REQUIRE(v->size() == 1);
0137         REQUIRE((*v)[0].a == 1);
0138       }
0139       {
0140         auto n = tester.findNextTransition();
0141         REQUIRE(n.itemType() == edm::InputSource::ItemType::IsStop);
0142       }
0143     }
0144     std::filesystem::remove(fileName);
0145   }
0146 }