Skip to content

Commit

Permalink
Core: refactoring of run parameters object (#46)
Browse files Browse the repository at this point in the history
* Core: can specify elasticity of beams

* Utils: specify when num=0 in utils::s

* Core: renamed the Parameters object into a RunParameters one, and uniformised API

* Utils: made the steering card printer an utility
  • Loading branch information
forthommel committed Feb 2, 2024
1 parent ba5cd99 commit 53e4e72
Show file tree
Hide file tree
Showing 52 changed files with 477 additions and 545 deletions.
10 changes: 5 additions & 5 deletions CepGen/Cards/CommandLineHandler.cpp
Expand Up @@ -20,14 +20,14 @@

#include "CepGen/Cards/Handler.h"
#include "CepGen/Core/Exception.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/Event/Event.h"
#include "CepGen/EventFilter/EventExporter.h"
#include "CepGen/EventFilter/EventModifier.h"
#include "CepGen/Modules/CardsHandlerFactory.h"
#include "CepGen/Modules/EventExporterFactory.h"
#include "CepGen/Modules/EventModifierFactory.h"
#include "CepGen/Modules/ProcessFactory.h"
#include "CepGen/Parameters.h"
#include "CepGen/Physics/PDG.h"
#include "CepGen/Process/Process.h"
#include "CepGen/Utils/TimeKeeper.h"
Expand All @@ -53,14 +53,14 @@ namespace cepgen {
return desc;
}

Parameters* parseString(const std::string&, Parameters*) override;
Parameters* parseFile(const std::string&, Parameters*) override;
RunParameters* parseString(const std::string&, RunParameters*) override;
RunParameters* parseFile(const std::string&, RunParameters*) override;

private:
std::vector<std::string> argv_;
};

Parameters* CommandLineHandler::parseFile(const std::string& filename, Parameters* params) {
RunParameters* CommandLineHandler::parseFile(const std::string& filename, RunParameters* params) {
rt_params_ = params;
if (filename.empty())
throw CG_FATAL("CommandLineHandler") << "Empty filename to be parsed! Aborting.";
Expand All @@ -72,7 +72,7 @@ namespace cepgen {
return rt_params_;
}

Parameters* CommandLineHandler::parseString(const std::string& arg, Parameters* params) {
RunParameters* CommandLineHandler::parseString(const std::string& arg, RunParameters* params) {
ParametersList pars;
pars.feed(arg);
CG_INFO("CommandLineHandler") << "Arguments list: " << arg << " unpacked to:\n\t" << pars << ".";
Expand Down
16 changes: 8 additions & 8 deletions CepGen/Cards/Handler.cpp
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2021 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,39 +19,39 @@
#include "CepGen/Cards/Handler.h"
#include "CepGen/Core/Exception.h"
#include "CepGen/Core/ParametersList.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/Modules/CardsHandlerFactory.h"
#include "CepGen/Parameters.h"
#include "CepGen/Utils/Filesystem.h"

namespace cepgen {
namespace card {
Handler::Handler(const ParametersList& params)
: NamedModule(params), filename_(steer<std::string>("filename")), rt_params_(new Parameters) {
: NamedModule(params), filename_(steer<std::string>("filename")), rt_params_(new RunParameters) {
if (!filename_.empty())
parseFile(filename_, rt_params_);
}

Parameters* Handler::parseString(const std::string& filename) {
RunParameters* Handler::parseString(const std::string& filename) {
try {
auto parser = CardsHandlerFactory::get().build(utils::fileExtension(filename));
return parser->parseString(filename, new Parameters);
return parser->parseString(filename, new RunParameters);
} catch (const std::invalid_argument& err) {
throw CG_FATAL("Cards:handler") << "Failed to parse the steering card at \"" << filename << "\"! "
<< err.what();
}
}

Parameters* Handler::parseFile(const std::string& filename) {
RunParameters* Handler::parseFile(const std::string& filename) {
try {
auto parser = CardsHandlerFactory::get().build(utils::fileExtension(filename));
return parser->parseFile(filename, new Parameters);
return parser->parseFile(filename, new RunParameters);
} catch (const std::invalid_argument& err) {
throw CG_FATAL("Cards:handler") << "Failed to parse the steering card at \"" << filename << "\"! "
<< err.what();
}
}

void Handler::write(const Parameters* params, const std::string& filename) {
void Handler::write(const RunParameters* params, const std::string& filename) {
try {
auto writer = CardsHandlerFactory::get().build(utils::fileExtension(filename));
writer->pack(params);
Expand Down
41 changes: 17 additions & 24 deletions CepGen/Cards/Handler.h
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2023 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,41 +22,34 @@
#include "CepGen/Modules/NamedModule.h"

namespace cepgen {
class Parameters;
class RunParameters;
/// Location for all steering card parsers/writers
namespace card {
/// Base steering card module
class Handler : public NamedModule<std::string> {
public:
/// Build a configuration from an external steering card
explicit Handler(const ParametersList&);
explicit Handler(const ParametersList&); ///< Build a configuration from an external steering card
virtual ~Handler() = default;

static ParametersDescription description();

/// Get the list of runtime parameters as parsed
const Parameters* runtimeParameters() const { return rt_params_; }
/// Get the list of runtime parameters as parsed
Parameters* runtimeParameters() { return rt_params_; }
/// Specify runtime parameters to the handler
virtual void pack(const Parameters*){};
virtual Parameters* parseString(const std::string&, Parameters* params) { return params; }
const RunParameters* runParameters() const { return rt_params_; } ///< Parsed list of runtime parameters
RunParameters* runParameters() { return rt_params_; } ///< Parsed list of runtime parameters

virtual void pack(const RunParameters*) {} ///< Specify runtime parameters to the handler

virtual RunParameters* parseString(const std::string&, RunParameters* params) { return params; }
/// Retrieve a configuration from a parsed steering card
virtual Parameters* parseFile(const std::string&, Parameters* params) { return params; }
/// Build a configuration from a steering card
static Parameters* parseString(const std::string&);
/// Build a configuration from a steering card
static Parameters* parseFile(const std::string&);
/// Write the current configuration into a steering card
virtual void write(const std::string&) const {}
/// Write a steering card from a configuration
static void write(const Parameters*, const std::string&);
virtual RunParameters* parseFile(const std::string&, RunParameters* params) { return params; }
static RunParameters* parseString(const std::string&); ///< Build a configuration from a steering card
static RunParameters* parseFile(const std::string&); ///< Build a configuration from a steering card

virtual void write(const std::string&) const {} ///< Write the current configuration into a steering card
static void write(const RunParameters*, const std::string&); ///< Write a steering card from a configuration

protected:
/// Input filename
const std::string filename_;
/// List of parameters parsed from a card handler
Parameters* rt_params_;
const std::string filename_; ///< Input filename
RunParameters* rt_params_; ///< List of parameters parsed from a card handler
};
} // namespace card
} // namespace cepgen
Expand Down
10 changes: 5 additions & 5 deletions CepGen/Cards/LpairHandler.cpp
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2023 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +21,7 @@
#include "CepGen/Cards/LpairHandler.h"
#include "CepGen/Core/Exception.h"
#include "CepGen/Core/ParametersList.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/EventFilter/EventExporter.h"
#include "CepGen/EventFilter/EventModifier.h"
#include "CepGen/Generator.h" // for library loading
Expand All @@ -29,7 +30,6 @@
#include "CepGen/Modules/EventModifierFactory.h"
#include "CepGen/Modules/ProcessFactory.h"
#include "CepGen/Modules/StructureFunctionsFactory.h"
#include "CepGen/Parameters.h"
#include "CepGen/Physics/GluonGrid.h"
#include "CepGen/Physics/MCDFileParser.h"
#include "CepGen/Process/Process.h"
Expand Down Expand Up @@ -152,7 +152,7 @@ namespace cepgen {
registerKinematicsParameter<double>("MXMAX", "Maximal invariant mass of proton remnants", "mxmax");
}

Parameters* LpairHandler::parseFile(const std::string& filename, Parameters* params) {
RunParameters* LpairHandler::parseFile(const std::string& filename, RunParameters* params) {
if (!utils::fileExists(filename))
throw CG_FATAL("LpairHandler") << "Unable to locate steering card \"" << filename << "\".";
rt_params_ = params;
Expand Down Expand Up @@ -265,8 +265,8 @@ namespace cepgen {
f.close();
}

void LpairHandler::pack(const Parameters* params) {
rt_params_ = const_cast<Parameters*>(params);
void LpairHandler::pack(const RunParameters* params) {
rt_params_ = const_cast<RunParameters*>(params);
str_fun_ = rt_params_->kinematics().incomingBeams().structureFunctions().name<int>();
sr_type_ = rt_params_->kinematics().incomingBeams().structureFunctions().get<int>("sigmaRatio");
//kmr_grid_path_ = kmr::GluonGrid::get().path();
Expand Down
6 changes: 3 additions & 3 deletions CepGen/Cards/LpairHandler.h
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2021 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,8 +37,8 @@ namespace cepgen {

static ParametersDescription description();

void pack(const Parameters*) override;
Parameters* parseFile(const std::string&, Parameters*) override;
void pack(const RunParameters*) override;
RunParameters* parseFile(const std::string&, RunParameters*) override;
/// Store a configuration into a LPAIR steering card
void write(const std::string& file) const override;

Expand Down
26 changes: 18 additions & 8 deletions CepGen/Core/Generator.cpp
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2023 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,20 +20,20 @@

#include "CepGen/Core/Exception.h"
#include "CepGen/Core/GeneratorWorker.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/EventFilter/EventExporter.h"
#include "CepGen/EventFilter/EventModifier.h"
#include "CepGen/Generator.h"
#include "CepGen/Integration/Integrator.h"
#include "CepGen/Integration/ProcessIntegrand.h"
#include "CepGen/Modules/GeneratorWorkerFactory.h"
#include "CepGen/Modules/IntegratorFactory.h"
#include "CepGen/Parameters.h"
#include "CepGen/Process/Process.h"
#include "CepGen/Utils/String.h"
#include "CepGen/Utils/TimeKeeper.h"

namespace cepgen {
Generator::Generator(bool safe_mode) : parameters_(new Parameters) {
Generator::Generator(bool safe_mode) : parameters_(new RunParameters) {
static bool init = false;
if (!init) {
cepgen::initialise(safe_mode);
Expand All @@ -45,7 +45,7 @@ namespace cepgen {
srandom(time.time_since_epoch().count());
}

Generator::Generator(Parameters* ip) : parameters_(ip) {}
Generator::Generator(RunParameters* ip) : parameters_(ip) {}

Generator::~Generator() {
if (parameters_->timeKeeper())
Expand All @@ -61,15 +61,25 @@ namespace cepgen {
if (!integrator_)
resetIntegrator();

worker_->setRuntimeParameters(const_cast<const Parameters*>(parameters_.get()));
worker_->setRunParameters(const_cast<const RunParameters*>(parameters_.get()));
worker_->setIntegrator(integrator_.get());
xsect_ = Value{-1., -1.};
parameters_->prepareRun();
}

Parameters& Generator::parametersRef() { return *parameters_; }
const RunParameters& Generator::runParameters() const {
if (!parameters_)
throw CG_FATAL("Generator:runParameters") << "Run parameters object is not yet initialised.";
return *parameters_;
}

RunParameters& Generator::runParameters() {
if (!parameters_)
throw CG_FATAL("Generator:runParameters") << "Run parameters object is not yet initialised.";
return *parameters_;
}

void Generator::setParameters(Parameters* ip) { parameters_.reset(ip); }
void Generator::setRunParameters(RunParameters* ip) { parameters_.reset(ip); }

double Generator::computePoint(const std::vector<double>& coord) {
if (!worker_)
Expand Down Expand Up @@ -168,7 +178,7 @@ namespace cepgen {
integrate();

// prepare the run parameters for event generation
parameters_->initialise();
parameters_->initialiseModules();
worker_->initialise();

initialised_ = true;
Expand Down
12 changes: 6 additions & 6 deletions CepGen/Core/GeneratorWorker.cpp
@@ -1,6 +1,6 @@
/*
* CepGen: a central exclusive processes event generator
* Copyright (C) 2013-2023 Laurent Forthomme
* Copyright (C) 2013-2024 Laurent Forthomme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,23 +18,23 @@

#include "CepGen/Core/Exception.h"
#include "CepGen/Core/GeneratorWorker.h"
#include "CepGen/Core/RunParameters.h"
#include "CepGen/Event/Event.h"
#include "CepGen/EventFilter/EventExporter.h"
#include "CepGen/Integration/Integrator.h"
#include "CepGen/Integration/ProcessIntegrand.h"
#include "CepGen/Parameters.h"
#include "CepGen/Process/Process.h"
#include "CepGen/Utils/String.h"
#include "CepGen/Utils/TimeKeeper.h"

namespace cepgen {
GeneratorWorker::GeneratorWorker(const ParametersList& params) : SteeredObject(params) {}

void GeneratorWorker::setRuntimeParameters(const Parameters* params) {
void GeneratorWorker::setRunParameters(const RunParameters* params) {
params_ = params;
integrand_.reset(new ProcessIntegrand(params));
CG_DEBUG("GeneratorWorker") << "New generator worker initialised for integration/event generation.\n\t"
<< "Parameters at " << (void*)params_ << ".";
<< "Run parameters at " << (void*)params_ << ".";
}

GeneratorWorker::~GeneratorWorker() {
Expand All @@ -56,7 +56,7 @@ namespace cepgen {
}

bool GeneratorWorker::storeEvent() {
CG_TICKER(const_cast<Parameters*>(params_)->timeKeeper());
CG_TICKER(const_cast<RunParameters*>(params_)->timeKeeper());

if (!integrand_->process().hasEvent())
return true;
Expand All @@ -69,7 +69,7 @@ namespace cepgen {
callback_proc_(integrand_->process());
for (const auto& mod : params_->eventExportersSequence())
*mod << event;
const_cast<Parameters*>(params_)->addGenerationTime(event.metadata.at("time:total"));
const_cast<RunParameters*>(params_)->addGenerationTime(event.metadata.at("time:total"));
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions CepGen/Core/GeneratorWorker.h
Expand Up @@ -27,7 +27,7 @@

namespace cepgen {
class Integrator;
class Parameters;
class RunParameters;
class ProcessIntegrand;
namespace proc {
class Process;
Expand All @@ -42,7 +42,7 @@ namespace cepgen {
static ParametersDescription description();

/// Specify the runtime parameters
void setRuntimeParameters(const Parameters*);
void setRunParameters(const RunParameters*);
/// Specify the integrator instance handled by the mother generator
void setIntegrator(const Integrator* integ);
/// Launch the event generation
Expand All @@ -68,7 +68,7 @@ namespace cepgen {
const Integrator* integrator_{nullptr};
/// Steering parameters for the event generation
/// \note NOT owning
const Parameters* params_{nullptr};
const RunParameters* params_{nullptr};
/// Local event weight evaluator
std::unique_ptr<ProcessIntegrand> integrand_;
/// Callback function on process for each new event
Expand Down

0 comments on commit 53e4e72

Please sign in to comment.