Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-31 02:19:31

0001 #include "catch.hpp"
0002 #include <iostream>
0003 #include <string>
0004 #include <vector>
0005 
0006 #include <memory>
0007 
0008 #include "FWCore/Framework/interface/ProductSelectorRules.h"
0009 #include "FWCore/Framework/interface/ProductSelector.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "DataFormats/Provenance/interface/ProductDescription.h"
0012 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0013 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0014 #include "FWCore/Utilities/interface/EDMException.h"
0015 #include "FWCore/Reflection/interface/TypeWithDict.h"
0016 
0017 typedef std::vector<edm::ProductDescription const*> VCBDP;
0018 
0019 void apply_gs(edm::ProductSelector const& gs, VCBDP const& allbranches, std::vector<bool>& results) {
0020   VCBDP::const_iterator it = allbranches.begin();
0021   VCBDP::const_iterator end = allbranches.end();
0022   for (; it != end; ++it)
0023     results.push_back(gs.selected(**it));
0024 }
0025 
0026 void doTest(edm::ParameterSet const& params,
0027             char const* testname,
0028             VCBDP const& allbranches,
0029             std::vector<bool>& expected) {
0030   edm::ProductSelectorRules gsr(params, "outputCommands", testname);
0031   edm::ProductSelector gs;
0032   gs.initialize(gsr, allbranches);
0033 
0034   std::vector<bool> results;
0035   apply_gs(gs, allbranches, results);
0036 
0037   CHECK(expected == results);
0038 }
0039 
0040 TEST_CASE("test ProductSelector", "[ProductSelector]") {
0041   edm::ParameterSet dummyProcessPset;
0042   dummyProcessPset.registerIt();
0043   auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0044   processConfiguration->setParameterSetID(dummyProcessPset.id());
0045 
0046   edm::TypeWithDict dummyTypeWithDict;
0047   // We pretend to have one module, with two products. The products
0048   // are of the same and, type differ in instance name.
0049   std::set<edm::ParameterSetID> psetsA;
0050   edm::ParameterSet modAparams;
0051   modAparams.addParameter<int>("i", 2112);
0052   modAparams.addParameter<std::string>("s", "hi");
0053   modAparams.registerIt();
0054   psetsA.insert(modAparams.id());
0055 
0056   edm::ProductDescription b1(edm::InEvent, "modA", "PROD", "UglyProdTypeA", "ProdTypeA", "i1", dummyTypeWithDict);
0057   edm::ProductDescription b2(edm::InEvent, "modA", "PROD", "UglyProdTypeA", "ProdTypeA", "i2", dummyTypeWithDict);
0058 
0059   // Our second pretend module has only one product, and gives it no
0060   // instance name.
0061   std::set<edm::ParameterSetID> psetsB;
0062   edm::ParameterSet modBparams;
0063   modBparams.addParameter<double>("d", 2.5);
0064   modBparams.registerIt();
0065   psetsB.insert(modBparams.id());
0066 
0067   edm::ProductDescription b3(edm::InEvent, "modB", "HLT", "UglyProdTypeB", "ProdTypeB", "", dummyTypeWithDict);
0068 
0069   // Our third pretend is like modA, except it hass processName_ of
0070   // "USER"
0071 
0072   edm::ProductDescription b4(edm::InEvent, "modA", "USER", "UglyProdTypeA", "ProdTypeA", "i1", dummyTypeWithDict);
0073   edm::ProductDescription b5(edm::InEvent, "modA", "USER", "UglyProdTypeA", "ProdTypeA", "i2", dummyTypeWithDict);
0074 
0075   // These are pointers to all the branches that are available. In a
0076   // framework program, these would come from the ProductRegistry
0077   // which is used to initialze the OutputModule being configured.
0078   VCBDP allbranches;
0079   allbranches.push_back(&b1);  // ProdTypeA_modA_i1. (PROD)
0080   allbranches.push_back(&b2);  // ProdTypeA_modA_i2. (PROD)
0081   allbranches.push_back(&b3);  // ProdTypeB_modB_HLT. (no instance name)
0082   allbranches.push_back(&b4);  // ProdTypeA_modA_i1_USER.
0083   allbranches.push_back(&b5);  // ProdTypeA_modA_i2_USER.
0084 
0085   // Test default parameters
0086   SECTION("default parameters") {
0087     bool wanted[] = {true, true, true, true, true};
0088     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0089     edm::ParameterSet noparams;
0090 
0091     doTest(noparams, "default parameters", allbranches, expected);
0092   }
0093 
0094   // Keep all branches with instance name i2.
0095   SECTION("keep_i2 parameters") {
0096     bool wanted[] = {false, true, false, false, true};
0097     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0098 
0099     edm::ParameterSet keep_i2;
0100     std::string const keep_i2_rule = "keep *_*_i2_*";
0101     std::vector<std::string> cmds;
0102     cmds.push_back(keep_i2_rule);
0103     keep_i2.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0104     keep_i2.registerIt();
0105 
0106     doTest(keep_i2, "keep_i2 parameters", allbranches, expected);
0107   }
0108 
0109   // Drop all branches with instance name i2.
0110   SECTION("drop_i2 parameters") {
0111     bool wanted[] = {true, false, true, true, false};
0112     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0113 
0114     edm::ParameterSet drop_i2;
0115     std::string const drop_i2_rule1 = "keep *";
0116     std::string const drop_i2_rule2 = "drop *_*_i2_*";
0117     std::vector<std::string> cmds;
0118     cmds.push_back(drop_i2_rule1);
0119     cmds.push_back(drop_i2_rule2);
0120     drop_i2.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0121     drop_i2.registerIt();
0122 
0123     doTest(drop_i2, "drop_i2 parameters", allbranches, expected);
0124   }
0125 
0126   // Now try dropping all branches with product type "foo". There are
0127   // none, so all branches should be written.
0128   SECTION("drop_foo parameters") {
0129     bool wanted[] = {true, true, true, true, true};
0130     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0131 
0132     edm::ParameterSet drop_foo;
0133     std::string const drop_foo_rule1 = "keep *_*_*_*";  // same as "keep *"
0134     std::string const drop_foo_rule2 = "drop foo_*_*_*";
0135     std::vector<std::string> cmds;
0136     cmds.push_back(drop_foo_rule1);
0137     cmds.push_back(drop_foo_rule2);
0138     drop_foo.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0139     drop_foo.registerIt();
0140 
0141     doTest(drop_foo, "drop_foo parameters", allbranches, expected);
0142   }
0143 
0144   // Now try dropping all branches with product type "ProdTypeA".
0145   SECTION("drop_ProdTypeA") {
0146     bool wanted[] = {false, false, true, false, false};
0147     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0148 
0149     edm::ParameterSet drop_ProdTypeA;
0150     std::string const drop_ProdTypeA_rule1 = "keep *";
0151     std::string const drop_ProdTypeA_rule2 = "drop ProdTypeA_*_*_*";
0152     std::vector<std::string> cmds;
0153     cmds.push_back(drop_ProdTypeA_rule1);
0154     cmds.push_back(drop_ProdTypeA_rule2);
0155     drop_ProdTypeA.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0156     drop_ProdTypeA.registerIt();
0157 
0158     doTest(drop_ProdTypeA, "drop_ProdTypeA", allbranches, expected);
0159   }
0160 
0161   // Keep only branches with instance name 'i1', from Production.
0162   SECTION("keep_i1prod") {
0163     bool wanted[] = {true, false, false, false, false};
0164     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0165 
0166     edm::ParameterSet keep_i1prod;
0167     std::string const keep_i1prod_rule = "keep *_*_i1_PROD";
0168     std::vector<std::string> cmds;
0169     cmds.push_back(keep_i1prod_rule);
0170     keep_i1prod.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0171     keep_i1prod.registerIt();
0172 
0173     doTest(keep_i1prod, "keep_i1prod", allbranches, expected);
0174   }
0175 
0176   // First say to keep everything,  then  to drop everything, then  to
0177   // keep it again. The end result should be to keep everything.
0178   SECTION("indecisive") {
0179     bool wanted[] = {true, true, true, true, true};
0180     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0181 
0182     edm::ParameterSet indecisive;
0183     std::string const indecisive_rule1 = "keep *";
0184     std::string const indecisive_rule2 = "drop *";
0185     std::string const indecisive_rule3 = "keep *";
0186     std::vector<std::string> cmds;
0187     cmds.push_back(indecisive_rule1);
0188     cmds.push_back(indecisive_rule2);
0189     cmds.push_back(indecisive_rule3);
0190     indecisive.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0191     indecisive.registerIt();
0192 
0193     doTest(indecisive, "indecisive", allbranches, expected);
0194   }
0195 
0196   // Keep all things, bu drop all things from modA, but later keep all
0197   // things from USER.
0198   SECTION("drop_modA_keep_user") {
0199     bool wanted[] = {false, false, true, true, true};
0200     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0201 
0202     edm::ParameterSet params;
0203     std::string const rule1 = "keep *";
0204     std::string const rule2 = "drop *_modA_*_*";
0205     std::string const rule3 = "keep *_*_*_USER";
0206     std::vector<std::string> cmds;
0207     cmds.push_back(rule1);
0208     cmds.push_back(rule2);
0209     cmds.push_back(rule3);
0210     params.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0211     params.registerIt();
0212 
0213     doTest(params, "drop_modA_keep_user", allbranches, expected);
0214   }
0215 
0216   // Exercise the wildcards * and ?
0217   SECTION("excercise wildcards1") {
0218     bool wanted[] = {true, true, true, false, false};
0219     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0220 
0221     edm::ParameterSet params;
0222     std::string const rule1 = "drop *";
0223     std::string const rule2 = "keep Pr*A_m?dA_??_P?O*";
0224     std::string const rule3 = "keep *?*?***??*????*?***_??***?__*?***T";
0225     std::vector<std::string> cmds;
0226     cmds.push_back(rule1);
0227     cmds.push_back(rule2);
0228     cmds.push_back(rule3);
0229     params.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0230     params.registerIt();
0231 
0232     doTest(params, "excercise wildcards1", allbranches, expected);
0233   }
0234 
0235   SECTION("illegal") {
0236     // Now try an illegal specification: not starting with 'keep' or 'drop'
0237     edm::ParameterSet bad;
0238     std::string const bad_rule = "beep *_*_i2_*";
0239     std::vector<std::string> cmds;
0240     cmds.push_back(bad_rule);
0241     bad.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0242     bad.registerIt();
0243     REQUIRE_THROWS_MATCHES(
0244         edm::ProductSelectorRules(bad, "outputCommands", "ProductSelectorTest"),
0245         edm::Exception,
0246         Catch::Predicate<edm::Exception>([](auto const& x) { return x.categoryCode() == edm::errors::Configuration; }));
0247   }
0248 }