Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-09 23:33:33

0001 // CepGen-CMSSW interfacing module
0002 //   2022-2024, Laurent Forthomme
0003 
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/StreamID.h"
0007 
0008 #include "GeneratorInterface/CepGenInterface/interface/CepGenEventGenerator.h"
0009 #include "GeneratorInterface/CepGenInterface/interface/CepGenParametersConverter.h"
0010 
0011 #include <CepGen/Core/Exception.h>
0012 #include <CepGen/Core/RunParameters.h>
0013 #include <CepGen/Event/Event.h>
0014 #include <CepGen/EventFilter/EventExporter.h>
0015 #include <CepGen/EventFilter/EventModifier.h>
0016 #include <CepGen/Generator.h>
0017 #include <CepGen/Modules/EventExporterFactory.h>
0018 #include <CepGen/Modules/EventModifierFactory.h>
0019 #include <CepGen/Modules/ProcessFactory.h>
0020 #include <CepGen/Process/Process.h>
0021 #include <CepGenAddOns/HepMC2Wrapper/HepMC2EventInterface.h>
0022 
0023 #include <memory>
0024 
0025 using namespace gen;
0026 
0027 CepGenEventGenerator::CepGenEventGenerator(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC)
0028     : BaseHadronizer(iConfig),
0029       proc_params_(cepgen::fromParameterSet(iConfig.getParameter<edm::ParameterSet>("process"))) {
0030   // specify the overall module verbosity
0031   cepgen::utils::Logger::get().setLevel(
0032       static_cast<cepgen::utils::Logger::Level>(iConfig.getUntrackedParameter<int>("verbosity", 0)));
0033 
0034   // build the process
0035   edm::LogInfo("CepGenEventGenerator") << "Process to be generated: " << proc_params_ << ".";
0036 
0037   const auto modif_mods = cepgen::fromParameterSet(
0038       iConfig.getUntrackedParameter<edm::ParameterSet>("modifierModules", edm::ParameterSet{}));
0039   edm::LogInfo("CepGenEventGenerator") << "Event modifier modules: " << modif_mods << ".";
0040   for (const auto& mod : modif_mods.keys())
0041     modif_modules_.emplace_back(std::make_pair(mod, modif_mods.get<cepgen::ParametersList>(mod)));
0042 
0043   const auto output_mods =
0044       cepgen::fromParameterSet(iConfig.getUntrackedParameter<edm::ParameterSet>("outputModules", edm::ParameterSet{}));
0045   edm::LogInfo("CepGenEventGenerator") << "Output modules: " << output_mods << ".";
0046   for (const auto& mod : output_mods.keys())
0047     output_modules_.emplace_back(std::make_pair(mod, output_mods.get<cepgen::ParametersList>(mod)));
0048 }
0049 
0050 CepGenEventGenerator::~CepGenEventGenerator() { edm::LogInfo("CepGenEventGenerator") << "Destructor called."; }
0051 
0052 bool CepGenEventGenerator::initializeForInternalPartons() {
0053   gen_ = new cepgen::Generator(true /* "safe" mode: start without plugins */);
0054 
0055   auto pproc = proc_params_;
0056   {  // little treatment to allow for standard CepGen configurations to be copy-pasted in place
0057     pproc += proc_params_.get<cepgen::ParametersList>("processParameters");
0058     pproc.erase("processParameters");
0059     auto& pkin = pproc.operator[]<cepgen::ParametersList>("kinematics");
0060     pkin += pproc.get<cepgen::ParametersList>("inKinematics");
0061     pproc.erase("inKinematics");
0062     pkin += pproc.get<cepgen::ParametersList>("outKinematics");
0063     pproc.erase("outKinematics");
0064     if (pproc.has<unsigned long long>("mode"))
0065       pkin.set<int>("mode", pproc.get<unsigned long long>("mode"));
0066   }
0067 
0068   gen_->runParameters().setProcess(cepgen::ProcessFactory::get().build(pproc));
0069   if (!gen_->runParameters().hasProcess())
0070     throw cms::Exception("CepGenEventGenerator") << "Failed to retrieve a process from the configuration";
0071   for (const auto& mod : modif_modules_) {
0072     auto modifier = cepgen::EventModifierFactory::get().build(mod.first, mod.second);
0073     for (const auto& cfg : mod.second.get<std::vector<std::string> >("preConfiguration"))
0074       modifier->readString(cfg);
0075     for (const auto& cfg : mod.second.get<std::vector<std::string> >("processConfiguration"))
0076       modifier->readString(cfg);
0077     gen_->runParameters().addModifier(std::move(modifier));
0078   }
0079   for (const auto& mod : output_modules_)
0080     gen_->runParameters().addEventExporter(cepgen::EventExporterFactory::get().build(mod.first, mod.second));
0081 
0082   edm::LogInfo("CepGenEventGenerator") << "Run parameters:\n" << gen_->runParameters();
0083   const auto xsec = gen_->computeXsection();
0084   xsec_.set_cross_section(xsec, xsec.uncertainty());
0085   runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec, xsec.uncertainty()));
0086   return true;
0087 }
0088 
0089 bool CepGenEventGenerator::generatePartonsAndHadronize() {
0090   event() = std::make_unique<HepMC::CepGenEvent>(gen_->next());
0091   event()->set_cross_section(xsec_);
0092   event()->weights().push_back(1.);
0093   return true;
0094 }