Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:24

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 using namespace gen;
0024 
0025 CepGenEventGenerator::CepGenEventGenerator(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC)
0026     : BaseHadronizer(iConfig),
0027       proc_params_(cepgen::fromParameterSet(iConfig.getParameter<edm::ParameterSet>("process"))) {
0028   // specify the overall module verbosity
0029   cepgen::utils::Logger::get().setLevel(
0030       static_cast<cepgen::utils::Logger::Level>(iConfig.getUntrackedParameter<int>("verbosity", 0)));
0031 
0032   // build the process
0033   edm::LogInfo("CepGenEventGenerator") << "Process to be generated: " << proc_params_ << ".";
0034 
0035   const auto modif_mods = cepgen::fromParameterSet(
0036       iConfig.getUntrackedParameter<edm::ParameterSet>("modifierModules", edm::ParameterSet{}));
0037   edm::LogInfo("CepGenEventGenerator") << "Event modifier modules: " << modif_mods << ".";
0038   for (const auto& mod : modif_mods.keys())
0039     modif_modules_.emplace_back(std::make_pair(mod, modif_mods.get<cepgen::ParametersList>(mod)));
0040 
0041   const auto output_mods =
0042       cepgen::fromParameterSet(iConfig.getUntrackedParameter<edm::ParameterSet>("outputModules", edm::ParameterSet{}));
0043   edm::LogInfo("CepGenEventGenerator") << "Output modules: " << output_mods << ".";
0044   for (const auto& mod : output_mods.keys())
0045     output_modules_.emplace_back(std::make_pair(mod, output_mods.get<cepgen::ParametersList>(mod)));
0046 }
0047 
0048 CepGenEventGenerator::~CepGenEventGenerator() { edm::LogInfo("CepGenEventGenerator") << "Destructor called."; }
0049 
0050 bool CepGenEventGenerator::initializeForInternalPartons() {
0051   gen_ = new cepgen::Generator(true /* "safe" mode: start without plugins */);
0052 
0053   auto pproc = proc_params_;
0054   {  // little treatment to allow for standard CepGen configurations to be copy-pasted in place
0055     pproc += proc_params_.get<cepgen::ParametersList>("processParameters");
0056     pproc.erase("processParameters");
0057     auto& pkin = pproc.operator[]<cepgen::ParametersList>("kinematics");
0058     pkin += pproc.get<cepgen::ParametersList>("inKinematics");
0059     pproc.erase("inKinematics");
0060     pkin += pproc.get<cepgen::ParametersList>("outKinematics");
0061     pproc.erase("outKinematics");
0062     if (pproc.has<unsigned long long>("mode"))
0063       pkin.set<int>("mode", pproc.get<unsigned long long>("mode"));
0064   }
0065 
0066   gen_->runParameters().setProcess(cepgen::ProcessFactory::get().build(pproc));
0067   if (!gen_->runParameters().hasProcess())
0068     throw cms::Exception("CepGenEventGenerator") << "Failed to retrieve a process from the configuration";
0069   for (const auto& mod : modif_modules_) {
0070     auto modifier = cepgen::EventModifierFactory::get().build(mod.first, mod.second);
0071     for (const auto& cfg : mod.second.get<std::vector<std::string> >("preConfiguration"))
0072       modifier->readString(cfg);
0073     for (const auto& cfg : mod.second.get<std::vector<std::string> >("processConfiguration"))
0074       modifier->readString(cfg);
0075     gen_->runParameters().addModifier(std::move(modifier));
0076   }
0077   for (const auto& mod : output_modules_)
0078     gen_->runParameters().addEventExporter(cepgen::EventExporterFactory::get().build(mod.first, mod.second));
0079 
0080   edm::LogInfo("CepGenEventGenerator") << "Run parameters:\n" << gen_->runParameters();
0081   const auto xsec = gen_->computeXsection();
0082   xsec_.set_cross_section(xsec, xsec.uncertainty());
0083   runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec, xsec.uncertainty()));
0084   return true;
0085 }
0086 
0087 bool CepGenEventGenerator::generatePartonsAndHadronize() {
0088   event().reset(new HepMC::CepGenEvent(gen_->next()));
0089   event()->set_cross_section(xsec_);
0090   event()->weights().push_back(1.);
0091   return true;
0092 }