Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include "FWCore/Framework/interface/maker/WorkerMaker.h"
0003 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0004 #include "FWCore/ParameterSet/interface/Registry.h"
0005 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Utilities/interface/ConvertException.h"
0008 #include "FWCore/Utilities/interface/Exception.h"
0009 #include "FWCore/Utilities/interface/EDMException.h"
0010 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0011 
0012 #include <sstream>
0013 #include <exception>
0014 namespace edm {
0015 
0016   Maker::~Maker() {}
0017 
0018   ModuleDescription Maker::createModuleDescription(MakeModuleParams const& p) const {
0019     ParameterSet const& conf = *p.pset_;
0020     ModuleDescription md(conf.id(),
0021                          conf.getParameter<std::string>("@module_type"),
0022                          conf.getParameter<std::string>("@module_label"),
0023                          p.processConfiguration_.get(),
0024                          ModuleDescription::getUniqueID());
0025     return md;
0026   }
0027 
0028   void Maker::throwValidationException(MakeModuleParams const& p, cms::Exception& iException) const {
0029     ParameterSet const& conf = *p.pset_;
0030     std::string moduleName = conf.getParameter<std::string>("@module_type");
0031     std::string moduleLabel = conf.getParameter<std::string>("@module_label");
0032 
0033     std::ostringstream ost;
0034     ost << "Validating configuration of module: class=" << moduleName << " label='" << moduleLabel << "'";
0035     iException.addContext(ost.str());
0036     throw;
0037   }
0038 
0039   void Maker::throwConfigurationException(ModuleDescription const& md, cms::Exception& iException) const {
0040     std::ostringstream ost;
0041     ost << "Constructing module: class=" << md.moduleName() << " label='" << md.moduleLabel() << "'";
0042     iException.addContext(ost.str());
0043     throw;
0044   }
0045 
0046   void Maker::validateEDMType(std::string const& edmType, MakeModuleParams const& p) const {
0047     std::string expected = p.pset_->getParameter<std::string>("@module_edm_type");
0048     if (edmType != expected) {
0049       throw Exception(errors::Configuration)
0050           << "The base type in the python configuration is " << expected << ", but the base type\n"
0051           << "for the module's C++ class is " << edmType << ". "
0052           << "Please fix the configuration.\n"
0053           << "It must use the same base type as the C++ class.\n";
0054     }
0055   }
0056 
0057   std::shared_ptr<maker::ModuleHolder> Maker::makeModule(
0058       MakeModuleParams const& p,
0059       signalslot::Signal<void(ModuleDescription const&)>& pre,
0060       signalslot::Signal<void(ModuleDescription const&)>& post) const {
0061     // Add process_name for SwitchProducer
0062     if (p.pset_->getParameter<std::string>("@module_type") == "SwitchProducer") {
0063       p.pset_->addUntrackedParameter("@process_name", p.processConfiguration_->processName());
0064     }
0065 
0066     ConfigurationDescriptions descriptions(baseType(), p.pset_->getParameter<std::string>("@module_type"));
0067     fillDescriptions(descriptions);
0068     try {
0069       convertException::wrap([&]() {
0070         descriptions.validate(*p.pset_, p.pset_->getParameter<std::string>("@module_label"));
0071         validateEDMType(baseType(), p);
0072       });
0073     } catch (cms::Exception& iException) {
0074       throwValidationException(p, iException);
0075     }
0076     p.pset_->registerIt();
0077     //Need to be certain top level untracked parameters are stored in
0078     // the registry even if another PSet already exists in the
0079     // registry from a previous process
0080     //NOTE: a better implementation would be to change ParameterSet::registerIt
0081     // but that would require rebuilding much more code so will be done at
0082     // a later date.
0083     edm::pset::Registry::instance()->insertMapped(*(p.pset_), true);
0084 
0085     ModuleDescription md = createModuleDescription(p);
0086     std::shared_ptr<maker::ModuleHolder> module;
0087     bool postCalled = false;
0088     try {
0089       convertException::wrap([&]() {
0090         pre(md);
0091         module = makeModule(*(p.pset_));
0092         module->setModuleDescription(md);
0093         module->preallocate(*(p.preallocate_));
0094         module->registerProductsAndCallbacks(p.reg_);
0095         // if exception then post will be called in the catch block
0096         postCalled = true;
0097         post(md);
0098       });
0099     } catch (cms::Exception& iException) {
0100       if (!postCalled) {
0101         CMS_SA_ALLOW try { post(md); } catch (...) {
0102           // If post throws an exception ignore it because we are already handling another exception
0103         }
0104       }
0105       throwConfigurationException(md, iException);
0106     }
0107     return module;
0108   }
0109 
0110   std::unique_ptr<Worker> Maker::makeWorker(ExceptionToActionTable const* actions,
0111                                             maker::ModuleHolder const* mod) const {
0112     return makeWorker(actions, mod->moduleDescription(), mod);
0113   }
0114 
0115 }  // namespace edm