Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:45

0001 #include <cassert>
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/BranchDescription.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::BranchDescription 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 int 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   std::cout << "ProductSelector from " << testname << ": " << gs << std::endl;
0034 
0035   std::vector<bool> results;
0036   apply_gs(gs, allbranches, results);
0037 
0038   int rc = 0;
0039   if (expected != results)
0040     rc = 1;
0041   if (rc == 1)
0042     std::cerr << "FAILURE: " << testname << '\n';
0043   std::cout << "---" << std::endl;
0044   return rc;
0045 }
0046 
0047 int work() {
0048   edm::ParameterSet dummyProcessPset;
0049   dummyProcessPset.registerIt();
0050   auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0051   processConfiguration->setParameterSetID(dummyProcessPset.id());
0052 
0053   edm::ParameterSet pset;
0054   pset.registerIt();
0055 
0056   edm::TypeWithDict dummyTypeWithDict;
0057   int rc = 0;
0058   // We pretend to have one module, with two products. The products
0059   // are of the same and, type differ in instance name.
0060   std::set<edm::ParameterSetID> psetsA;
0061   edm::ParameterSet modAparams;
0062   modAparams.addParameter<int>("i", 2112);
0063   modAparams.addParameter<std::string>("s", "hi");
0064   modAparams.registerIt();
0065   psetsA.insert(modAparams.id());
0066 
0067   edm::BranchDescription b1(
0068       edm::InEvent, "modA", "PROD", "UglyProdTypeA", "ProdTypeA", "i1", "", pset.id(), dummyTypeWithDict);
0069   edm::BranchDescription b2(
0070       edm::InEvent, "modA", "PROD", "UglyProdTypeA", "ProdTypeA", "i2", "", pset.id(), dummyTypeWithDict);
0071 
0072   // Our second pretend module has only one product, and gives it no
0073   // instance name.
0074   std::set<edm::ParameterSetID> psetsB;
0075   edm::ParameterSet modBparams;
0076   modBparams.addParameter<double>("d", 2.5);
0077   modBparams.registerIt();
0078   psetsB.insert(modBparams.id());
0079 
0080   edm::BranchDescription b3(
0081       edm::InEvent, "modB", "HLT", "UglyProdTypeB", "ProdTypeB", "", "", pset.id(), dummyTypeWithDict);
0082 
0083   // Our third pretend is like modA, except it hass processName_ of
0084   // "USER"
0085 
0086   edm::BranchDescription b4(
0087       edm::InEvent, "modA", "USER", "UglyProdTypeA", "ProdTypeA", "i1", "", pset.id(), dummyTypeWithDict);
0088   edm::BranchDescription b5(
0089       edm::InEvent, "modA", "USER", "UglyProdTypeA", "ProdTypeA", "i2", "", pset.id(), dummyTypeWithDict);
0090 
0091   // These are pointers to all the branches that are available. In a
0092   // framework program, these would come from the ProductRegistry
0093   // which is used to initialze the OutputModule being configured.
0094   VCBDP allbranches;
0095   allbranches.push_back(&b1);  // ProdTypeA_modA_i1. (PROD)
0096   allbranches.push_back(&b2);  // ProdTypeA_modA_i2. (PROD)
0097   allbranches.push_back(&b3);  // ProdTypeB_modB_HLT. (no instance name)
0098   allbranches.push_back(&b4);  // ProdTypeA_modA_i1_USER.
0099   allbranches.push_back(&b5);  // ProdTypeA_modA_i2_USER.
0100 
0101   // Test default parameters
0102   {
0103     bool wanted[] = {true, true, true, true, true};
0104     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0105     edm::ParameterSet noparams;
0106 
0107     rc += doTest(noparams, "default parameters", allbranches, expected);
0108   }
0109 
0110   // Keep all branches with instance name i2.
0111   {
0112     bool wanted[] = {false, true, false, false, true};
0113     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0114 
0115     edm::ParameterSet keep_i2;
0116     std::string const keep_i2_rule = "keep *_*_i2_*";
0117     std::vector<std::string> cmds;
0118     cmds.push_back(keep_i2_rule);
0119     keep_i2.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0120     keep_i2.registerIt();
0121 
0122     rc += doTest(keep_i2, "keep_i2 parameters", allbranches, expected);
0123   }
0124 
0125   // Drop all branches with instance name i2.
0126   {
0127     bool wanted[] = {true, false, true, true, false};
0128     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0129 
0130     edm::ParameterSet drop_i2;
0131     std::string const drop_i2_rule1 = "keep *";
0132     std::string const drop_i2_rule2 = "drop *_*_i2_*";
0133     std::vector<std::string> cmds;
0134     cmds.push_back(drop_i2_rule1);
0135     cmds.push_back(drop_i2_rule2);
0136     drop_i2.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0137     drop_i2.registerIt();
0138 
0139     rc += doTest(drop_i2, "drop_i2 parameters", allbranches, expected);
0140   }
0141 
0142   // Now try dropping all branches with product type "foo". There are
0143   // none, so all branches should be written.
0144   {
0145     bool wanted[] = {true, true, true, true, true};
0146     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0147 
0148     edm::ParameterSet drop_foo;
0149     std::string const drop_foo_rule1 = "keep *_*_*_*";  // same as "keep *"
0150     std::string const drop_foo_rule2 = "drop foo_*_*_*";
0151     std::vector<std::string> cmds;
0152     cmds.push_back(drop_foo_rule1);
0153     cmds.push_back(drop_foo_rule2);
0154     drop_foo.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0155     drop_foo.registerIt();
0156 
0157     rc += doTest(drop_foo, "drop_foo parameters", allbranches, expected);
0158   }
0159 
0160   // Now try dropping all branches with product type "ProdTypeA".
0161   {
0162     bool wanted[] = {false, false, true, false, false};
0163     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0164 
0165     edm::ParameterSet drop_ProdTypeA;
0166     std::string const drop_ProdTypeA_rule1 = "keep *";
0167     std::string const drop_ProdTypeA_rule2 = "drop ProdTypeA_*_*_*";
0168     std::vector<std::string> cmds;
0169     cmds.push_back(drop_ProdTypeA_rule1);
0170     cmds.push_back(drop_ProdTypeA_rule2);
0171     drop_ProdTypeA.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0172     drop_ProdTypeA.registerIt();
0173 
0174     rc += doTest(drop_ProdTypeA, "drop_ProdTypeA", allbranches, expected);
0175   }
0176 
0177   // Keep only branches with instance name 'i1', from Production.
0178   {
0179     bool wanted[] = {true, false, false, false, false};
0180     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0181 
0182     edm::ParameterSet keep_i1prod;
0183     std::string const keep_i1prod_rule = "keep *_*_i1_PROD";
0184     std::vector<std::string> cmds;
0185     cmds.push_back(keep_i1prod_rule);
0186     keep_i1prod.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0187     keep_i1prod.registerIt();
0188 
0189     rc += doTest(keep_i1prod, "keep_i1prod", allbranches, expected);
0190   }
0191 
0192   // First say to keep everything,  then  to drop everything, then  to
0193   // keep it again. The end result should be to keep everything.
0194   {
0195     bool wanted[] = {true, true, true, true, true};
0196     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0197 
0198     edm::ParameterSet indecisive;
0199     std::string const indecisive_rule1 = "keep *";
0200     std::string const indecisive_rule2 = "drop *";
0201     std::string const indecisive_rule3 = "keep *";
0202     std::vector<std::string> cmds;
0203     cmds.push_back(indecisive_rule1);
0204     cmds.push_back(indecisive_rule2);
0205     cmds.push_back(indecisive_rule3);
0206     indecisive.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0207     indecisive.registerIt();
0208 
0209     rc += doTest(indecisive, "indecisive", allbranches, expected);
0210   }
0211 
0212   // Keep all things, bu drop all things from modA, but later keep all
0213   // things from USER.
0214   {
0215     bool wanted[] = {false, false, true, true, true};
0216     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0217 
0218     edm::ParameterSet params;
0219     std::string const rule1 = "keep *";
0220     std::string const rule2 = "drop *_modA_*_*";
0221     std::string const rule3 = "keep *_*_*_USER";
0222     std::vector<std::string> cmds;
0223     cmds.push_back(rule1);
0224     cmds.push_back(rule2);
0225     cmds.push_back(rule3);
0226     params.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0227     params.registerIt();
0228 
0229     rc += doTest(params, "drop_modA_keep_user", allbranches, expected);
0230   }
0231 
0232   // Exercise the wildcards * and ?
0233   {
0234     bool wanted[] = {true, true, true, false, false};
0235     std::vector<bool> expected(wanted, wanted + sizeof(wanted) / sizeof(bool));
0236 
0237     edm::ParameterSet params;
0238     std::string const rule1 = "drop *";
0239     std::string const rule2 = "keep Pr*A_m?dA_??_P?O*";
0240     std::string const rule3 = "keep *?*?***??*????*?***_??***?__*?***T";
0241     std::vector<std::string> cmds;
0242     cmds.push_back(rule1);
0243     cmds.push_back(rule2);
0244     cmds.push_back(rule3);
0245     params.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0246     params.registerIt();
0247 
0248     rc += doTest(params, "excercise wildcards1", allbranches, expected);
0249   }
0250 
0251   {
0252     // Now try an illegal specification: not starting with 'keep' or 'drop'
0253     try {
0254       edm::ParameterSet bad;
0255       std::string const bad_rule = "beep *_*_i2_*";
0256       std::vector<std::string> cmds;
0257       cmds.push_back(bad_rule);
0258       bad.addUntrackedParameter<std::vector<std::string> >("outputCommands", cmds);
0259       bad.registerIt();
0260       edm::ProductSelectorRules gsr(bad, "outputCommands", "ProductSelectorTest");
0261       edm::ProductSelector gs;
0262       gs.initialize(gsr, allbranches);
0263       std::cerr << "Failed to throw required exception\n";
0264       rc += 1;
0265     } catch (edm::Exception const& x) {
0266       // OK, we should be here... now check exception type
0267       assert(x.categoryCode() == edm::errors::Configuration);
0268     } catch (...) {
0269       std::cerr << "Wrong exception type\n";
0270       rc += 1;
0271     }
0272   }
0273   return rc;
0274 }
0275 
0276 int main() {
0277   int rc = 0;
0278   try {
0279     rc = work();
0280   } catch (edm::Exception& x) {
0281     std::cerr << "edm::Exception caught:\n" << x << '\n';
0282     rc = 1;
0283   } catch (...) {
0284     std::cerr << "Unknown exception caught\n";
0285     rc = 2;
0286   }
0287   return rc;
0288 }