Skip to content

Commit

Permalink
Renamed output -> io namespace, increased modularity for output modul…
Browse files Browse the repository at this point in the history
…e definition
  • Loading branch information
forthommel committed Jul 13, 2019
1 parent a49e78d commit b8e5927
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 29 deletions.
19 changes: 19 additions & 0 deletions CepGen/Cards/PythonHandler.cpp
Expand Up @@ -7,6 +7,7 @@

#include "CepGen/Processes/ProcessesHandler.h"
#include "CepGen/Hadronisers/HadronisersHandler.h"
#include "CepGen/IO/ExportHandler.h"
#include "CepGen/StructureFunctions/StructureFunctions.h"

#include "CepGen/Physics/GluonGrid.h"
Expand Down Expand Up @@ -130,6 +131,12 @@ namespace cepgen
Py_CLEAR( pgen );
}

PyObject* pout = PyObject_GetAttrString( cfg, OUTPUT_NAME ); // new
if ( pout ) {
parseOutputModule( pout );
Py_CLEAR( pout );
}

//--- finalisation
Py_CLEAR( cfg );
}
Expand Down Expand Up @@ -352,6 +359,18 @@ namespace cepgen
}
}

void
PythonHandler::parseOutputModule( PyObject* pout )
{
if ( !is<ParametersList>( pout ) )
throwPythonError( "Invalid type for output parameters list!" );

PyObject* pname = element( pout, MODULE_NAME ); // borrowed
if ( !pname )
throwPythonError( "Output module name is required!" );
params_.setOutputModule( io::ExportHandler::get().build( get<std::string>( pname ), get<ParametersList>( pout ) ) );
}

void
PythonHandler::parseExtraParticles( PyObject* pparts )
{
Expand Down
4 changes: 3 additions & 1 deletion CepGen/Cards/PythonHandler.h
Expand Up @@ -28,6 +28,7 @@ namespace cepgen
static constexpr const char* LOGGER_NAME = "logger";
static constexpr const char* INTEGRATOR_NAME = "integrator";
static constexpr const char* GENERATOR_NAME = "generator";
static constexpr const char* OUTPUT_NAME = "output";

static constexpr const char* PDGLIST_NAME = "PDG";

Expand Down Expand Up @@ -60,7 +61,8 @@ namespace cepgen
void parseIntegrator( PyObject* );
void parseGenerator( PyObject* );
void parseHadroniser( PyObject* );
void parseExtraParticles( PyObject* parts );
void parseOutputModule( PyObject* );
void parseExtraParticles( PyObject* );
};
template<> bool PythonHandler::is<bool>( PyObject* obj ) const;
template<> bool PythonHandler::is<int>( PyObject* obj ) const;
Expand Down
30 changes: 25 additions & 5 deletions CepGen/Core/Parameters.cpp
Expand Up @@ -8,6 +8,7 @@
#include "CepGen/Physics/PDG.h"
#include "CepGen/Processes/GenericProcess.h"
#include "CepGen/Hadronisers/GenericHadroniser.h"
#include "CepGen/IO/GenericExportHandler.h"

#include "CepGen/StructureFunctions/StructureFunctions.h"

Expand All @@ -27,18 +28,17 @@ namespace cepgen
taming_functions( param.taming_functions ),
process_( std::move( param.process_ ) ),
hadroniser_( std::move( param.hadroniser_ ) ),
out_module_( std::move( param.out_module_ ) ),
store_( false ), total_gen_time_( param.total_gen_time_ ), num_gen_events_( param.num_gen_events_ ),
integration_( param.integration_ ), generation_( param.generation_ ),
out_params_( param.out_params_ )
integration_( param.integration_ ), generation_( param.generation_ )
{}

Parameters::Parameters( const Parameters& param ) :
general( param.general ),
kinematics( param.kinematics ),
taming_functions( param.taming_functions ),
store_( false ), total_gen_time_( param.total_gen_time_ ), num_gen_events_( param.num_gen_events_ ),
integration_( param.integration_ ), generation_( param.generation_ ),
out_params_( param.out_params_ )
integration_( param.integration_ ), generation_( param.generation_ )
{}

Parameters::~Parameters() // required for unique_ptr initialisation!
Expand All @@ -52,11 +52,11 @@ namespace cepgen
taming_functions = param.taming_functions;
process_ = std::move( param.process_ );
hadroniser_ = std::move( param.hadroniser_ );
out_module_ = std::move( param.out_module_ );
total_gen_time_ = param.total_gen_time_;
num_gen_events_ = param.num_gen_events_;
integration_ = param.integration_;
generation_ = param.generation_;
out_params_ = param.out_params_;
return *this;
}

Expand Down Expand Up @@ -162,6 +162,24 @@ namespace cepgen
hadroniser_.reset( hadr );
}

io::GenericExportHandler*
Parameters::outputModule()
{
return out_module_.get();
}

void
Parameters::setOutputModule( std::unique_ptr<io::GenericExportHandler> mod )
{
out_module_ = std::move( mod );
}

void
Parameters::setOutputModule( io::GenericExportHandler* mod )
{
out_module_.reset( mod );
}

std::ostream&
operator<<( std::ostream& os, const Parameters* param )
{
Expand Down Expand Up @@ -192,6 +210,8 @@ namespace cepgen
<< ( pretty ? yesno( param->generation_.enabled ) : std::to_string( param->generation_.enabled ) ) << "\n"
<< std::setw( wt ) << "Number of events to generate"
<< ( pretty ? boldify( param->generation_.maxgen ) : std::to_string( param->generation_.maxgen ) ) << "\n";
if ( param->out_module_ )
os << std::setw( wt ) << "Output module" << param->out_module_->name() << "\n";
if ( param->generation_.num_threads > 1 )
os
<< std::setw( wt ) << "Number of threads" << param->generation_.num_threads << "\n";
Expand Down
4 changes: 2 additions & 2 deletions CepGen/IO/ExportHandler.h
Expand Up @@ -5,15 +5,15 @@
#include "CepGen/IO/GenericExportHandler.h"

#define REGISTER_IO_MODULE( name, obj ) \
namespace cepgen { namespace output { \
namespace cepgen { namespace io { \
struct BUILDERNM( name ) { \
BUILDERNM( name )() { ExportHandler::get().registerModule<obj>( STRINGIFY( name ) ); } }; \
static BUILDERNM( name ) g ## name; \
} }

namespace cepgen
{
namespace output
namespace io
{
/// A hadroniser modules factory
typedef ModuleFactory<GenericExportHandler> ExportHandler;
Expand Down
9 changes: 6 additions & 3 deletions CepGen/IO/GenericExportHandler.cpp
Expand Up @@ -10,10 +10,13 @@

namespace cepgen
{
namespace output
namespace io
{
GenericExportHandler::GenericExportHandler() :
event_num_( 0. )
GenericExportHandler::GenericExportHandler( const std::string& name ) :
name_( name ), event_num_( 0. )
{}

GenericExportHandler::~GenericExportHandler()
{}

std::string
Expand Down
9 changes: 7 additions & 2 deletions CepGen/IO/GenericExportHandler.h
Expand Up @@ -2,13 +2,14 @@
#define CepGen_IO_GenericExportHandler_h

#include <iosfwd>
#include <string>

namespace cepgen
{
class Event;
class Parameters;
/// Location for all output generators
namespace output
namespace io
{
/**
* \brief Output format handler for events export
Expand All @@ -19,7 +20,9 @@ namespace cepgen
{
public:
/// Class constructor
explicit GenericExportHandler();
explicit GenericExportHandler( const std::string& );
virtual ~GenericExportHandler();
const std::string& name() const { return name_; }
/// Initialise the handler and its inner parameterisation
virtual void initialise( const Parameters& ) = 0;
/// Set the process cross section and its associated error
Expand All @@ -31,6 +34,8 @@ namespace cepgen

protected:
static std::string banner( const Parameters& );
/// Module unique name
const std::string name_;
/// Event index
unsigned int event_num_;
};
Expand Down
3 changes: 2 additions & 1 deletion CepGen/IO/GenericTextHandler.cpp
Expand Up @@ -9,7 +9,7 @@

namespace cepgen
{
namespace output
namespace io
{
/**
* \brief Handler for the generic text file output
Expand All @@ -36,6 +36,7 @@ namespace cepgen
const std::regex GenericTextHandler::rgx_select_role_( "\\w\\(([a-z]+\\d?)\\)" );

GenericTextHandler::GenericTextHandler( const ParametersList& params ) :
GenericExportHandler( "text" ),
file_( params.get<std::string>( "filename", "output.txt" ) ),
variables_( params.get<std::vector<std::string> >( "variables" ) )
{
Expand Down
3 changes: 2 additions & 1 deletion CepGen/IO/HepMCHandler.cpp
Expand Up @@ -46,7 +46,7 @@ using namespace HepMC;

namespace cepgen
{
namespace output
namespace io
{
/// Handler for the HepMC file output
/// \tparam T HepMC writer handler (format-dependent)
Expand Down Expand Up @@ -83,6 +83,7 @@ namespace cepgen

template<typename T>
HepMCHandler<T>::HepMCHandler( const ParametersList& params ) :
GenericExportHandler( "hepmc" ),
output_( new T( params.get<std::string>( "filename", "output.hepmc" ) ) ),
xs_( new GenCrossSection ),
#ifdef HEPMC3
Expand Down
3 changes: 2 additions & 1 deletion CepGen/IO/LHEFHepMCHandler.cpp
Expand Up @@ -21,7 +21,7 @@ using namespace std; // account for improper scoping in following includes

namespace cepgen
{
namespace output
namespace io
{
/**
* \brief Handler for the LHE file output
Expand All @@ -46,6 +46,7 @@ namespace cepgen
};

LHEFHepMCHandler::LHEFHepMCHandler( const ParametersList& params ) :
GenericExportHandler( "lhef" ),
lhe_output_( new LHEF::Writer( params.get<std::string>( "filename", "output.lhe" ) ) )
{}

Expand Down
3 changes: 2 additions & 1 deletion CepGen/IO/LHEFPythiaHandler.cpp
Expand Up @@ -10,7 +10,7 @@

namespace cepgen
{
namespace output
namespace io
{
/**
* \brief Handler for the LHE file output
Expand All @@ -35,6 +35,7 @@ namespace cepgen
};

LHEFPythiaHandler::LHEFPythiaHandler( const ParametersList& params ) :
GenericExportHandler( "lhef" ),
pythia_( new Pythia8::Pythia ), lhaevt_( new Pythia8::CepGenEvent )
{
lhaevt_->openLHEF( params.get<std::string>( "filename", "output.lhe" ) );
Expand Down
19 changes: 11 additions & 8 deletions CepGen/Parameters.h
Expand Up @@ -2,7 +2,6 @@
#define CepGen_Parameters_h

#include "CepGen/Physics/Kinematics.h"
#include "CepGen/Core/ParametersList.h"

#include <memory>

Expand All @@ -12,8 +11,10 @@
namespace cepgen
{
class Event;
class ParametersList;
namespace proc { class GenericProcess; }
namespace hadr { class GenericHadroniser; }
namespace io { class GenericExportHandler; }
namespace utils { class TamingFunctionsCollection; }
enum class IntegratorType;
/// List of parameters used to start and run the simulation job
Expand Down Expand Up @@ -95,16 +96,18 @@ namespace cepgen
Generation& generation() { return generation_; }
const Generation& generation() const { return generation_; }

/// Set parameters for the output format definition
void setOutputParameters( const ParametersList& params ) { out_params_ = params; }
/// List of steering parameters for the output format definition
const ParametersList& outputParameters() const { return out_params_; }

/// Specify if the generated events are to be stored
void setStorage( bool store ) { store_ = store; }
/// Are the events generated in this run to be stored in the output file ?
bool storage() const { return store_; }

/// Set a new output module definition
void setOutputModule( std::unique_ptr<io::GenericExportHandler> mod );
/// Set the pointer to a output module
void setOutputModule( io::GenericExportHandler* mod );
/// Output module definition
io::GenericExportHandler* outputModule();

//----- hadronisation algorithm

/// Hadronisation algorithm to use for the proton(s) fragmentation
Expand Down Expand Up @@ -136,6 +139,8 @@ namespace cepgen
private:
std::unique_ptr<proc::GenericProcess> process_;
std::unique_ptr<hadr::GenericHadroniser> hadroniser_;
/// Storage object
std::unique_ptr<io::GenericExportHandler> out_module_;

bool store_;
/// Total generation time (in seconds)
Expand All @@ -146,8 +151,6 @@ namespace cepgen
Integration integration_;
/// Events generation parameters
Generation generation_;
/// Storage parameters
ParametersList out_params_;
};
}

Expand Down
6 changes: 3 additions & 3 deletions test/cepgen-event.cpp
Expand Up @@ -14,7 +14,7 @@
using namespace std;

// we use polymorphism here
std::unique_ptr<cepgen::output::GenericExportHandler> writer;
std::unique_ptr<cepgen::io::GenericExportHandler> writer;

void storeEvent( const cepgen::Event& ev, unsigned long )
{
Expand All @@ -34,7 +34,7 @@ int main( int argc, char* argv[] )
{
ostringstream os;
string delim;
for ( const auto& mod : cepgen::output::ExportHandler::get().modules() )
for ( const auto& mod : cepgen::io::ExportHandler::get().modules() )
os << delim << mod, delim = ",";

if ( argc < 2 )
Expand All @@ -57,7 +57,7 @@ int main( int argc, char* argv[] )

const string format = ( argc > 2 ) ? argv[2] : "lhef";
const char* filename = ( argc > 3 ) ? argv[3] : "example.dat";
writer = cepgen::output::ExportHandler::get().build( format, cepgen::ParametersList()
writer = cepgen::io::ExportHandler::get().build( format, cepgen::ParametersList()
.set<std::string>( "filename", filename ) );

//-----------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion test/test_event_writer.cpp
Expand Up @@ -7,7 +7,7 @@ using namespace cepgen;

int main()
{
auto writer = output::ExportHandler::get().build( "hepmc" );
auto writer = io::ExportHandler::get().build( "hepmc" );
writer->setCrossSection( 1., 2. );

Event ev;
Expand Down

0 comments on commit b8e5927

Please sign in to comment.