diff --git a/CepGen/Cards/LpairHandler.cpp b/CepGen/Cards/LpairHandler.cpp index d9378ab..ffb2c37 100644 --- a/CepGen/Cards/LpairHandler.cpp +++ b/CepGen/Cards/LpairHandler.cpp @@ -1,249 +1,251 @@ #include "CepGen/Cards/LpairHandler.h" #include "CepGen/Core/Exception.h" #include "CepGen/Core/ParametersList.h" #include "CepGen/Core/Integrator.h" #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" #include "CepGen/Physics/PDG.h" #include #include namespace cepgen { namespace card { const int LpairHandler::kInvalid = 99999; //----- specialization for LPAIR input cards LpairHandler::LpairHandler( const char* file ) : proc_params_( new ParametersList ), str_fun_( 11 ), sr_type_( 1 ), xi_min_( 0. ), xi_max_( 1. ), hi_1_( { 0, 0 } ), hi_2_( { 0, 0 } ) { std::ifstream f( file, std::fstream::in ); if ( !f.is_open() ) throw CG_FATAL( "LpairHandler" ) << "Failed to parse file \"" << file << "%s\"."; init(); //--- parse all fields std::unordered_map m_params; std::string key, value; std::ostringstream os; while ( f >> key >> value ) { if ( key[0] == '#' ) // FIXME need to ensure there is no extra space before! continue; setParameter( key, value ); m_params.insert( { key, value } ); - if ( getDescription( key ) != "null" ) - os << "\n>> " << key << " = " << std::setw( 15 ) << getParameter( key ) - << " (" << getDescription( key ) << ")"; + if ( description( key ) != "null" ) + os << "\n>> " << key << " = " << std::setw( 15 ) << parameter( key ) + << " (" << description( key ) << ")"; } f.close(); //--- parse the process name params_.setProcess( proc::ProcessesHandler::get().build( proc_name_, *proc_params_ ) ); const Limits lim_xi{ xi_min_, xi_max_ }; if ( lim_xi.valid() ) params_.kinematics.cuts.remnants.energy_single = ( lim_xi+(-1.) )*( -params_.kinematics.incoming_beams.first.pz ); //--- parse the structure functions code auto sf_params = ParametersList() .set( "id", str_fun_ ) .set( "sigmaRatio", ParametersList() .set( "id", sr_type_ ) ); const unsigned long kLHAPDFCodeDec = 10000000, kLHAPDFPartDec = 1000000; if ( str_fun_ / kLHAPDFCodeDec == 1 ) { // SF from parton const unsigned long icode = str_fun_ % kLHAPDFCodeDec; sf_params .set( "id", (int)strfun::Type::Partonic ) .set( "pdfId", icode % kLHAPDFPartDec ) .set( "mode", icode / kLHAPDFPartDec ); // 0, 1, 2 } else if ( str_fun_ == (int)strfun::Type::MSTWgrid ) sf_params .set( "gridPath", mstw_grid_path_ ); params_.kinematics.structure_functions = strfun::StructureFunctionsHandler::get().build( sf_params ); //--- parse the integration algorithm name if ( integr_type_ == "plain" ) params_.integration().type = IntegratorType::plain; else if ( integr_type_ == "Vegas" ) params_.integration().type = IntegratorType::Vegas; else if ( integr_type_ == "MISER" ) params_.integration().type = IntegratorType::MISER; else if ( integr_type_ != "" ) throw CG_FATAL( "LpairHandler" ) << "Unrecognized integrator type: " << integr_type_ << "!"; //--- parse the hadronisation algorithm name if ( !hadr_name_.empty() ) { params_.setHadroniser( cepgen::hadr::HadronisersHandler::get().build( hadr_name_, ParametersList() ) ); params_.hadroniser()->setParameters( params_ ); } //--- parse the output module name if ( !out_mod_name_.empty() ) { ParametersList outm; if ( !out_file_name_.empty() ) outm.set( "filename", out_file_name_ ); params_.setOutputModule( cepgen::io::ExportHandler::get().build( out_mod_name_, outm ) ); } if ( m_params.count( "IEND" ) ) setValue( "IEND", ( std::stoi( m_params["IEND"] ) > 1 ) ); if ( m_params.count( "KMRG" ) && !kmr_grid_path_.empty() ) kmr::GluonGrid::get( kmr_grid_path_.c_str() ); //--- check if we are dealing with heavy ions for incoming states HeavyIon hi1{ hi_1_.first, (Element)hi_1_.second }, hi2{ hi_2_.first, (Element)hi_2_.second }; if ( hi1 ) params_.kinematics.incoming_beams.first.pdg = hi1; if ( hi2 ) params_.kinematics.incoming_beams.second.pdg = hi2; CG_INFO( "LpairHandler" ) << "File '" << file << "' succesfully opened!\n\t" << "The following parameters are set:" << os.str(); } void LpairHandler::init() { //------------------------------------------------------------------------------------------- // Process/integration/hadronisation parameters //------------------------------------------------------------------------------------------- registerParameter( "PROC", "Process name to simulate", &proc_name_ ); registerParameter( "ITYP", "Integration algorithm", &integr_type_ ); registerParameter( "HADR", "Hadronisation algorithm", &hadr_name_ ); registerParameter( "OUTP", "Output module", &out_mod_name_ ); registerParameter( "OUTF", "Output file name", &out_file_name_ ); - registerParameter( "KMRG", "KMR grid interpolation path", &kmr_grid_path_ ); //------------------------------------------------------------------------------------------- // General parameters //------------------------------------------------------------------------------------------- registerParameter( "IEND", "Generation type", ¶ms_.generation().enabled ); registerParameter( "NTRT", "Smoothen the integrand", ¶ms_.generation().treat ); registerParameter( "DEBG", "Debugging verbosity", (int*)&utils::Logger::get().level ); registerParameter( "NCVG", "Number of function calls", (int*)¶ms_.integration().ncvg ); registerParameter( "ITVG", "Number of integration iterations", (int*)¶ms_.integration().vegas.iterations ); registerParameter( "SEED", "Random generator seed", (int*)¶ms_.integration().rng_seed ); registerParameter( "NTHR", "Number of threads to use for events generation", (int*)¶ms_.generation().num_threads ); registerParameter( "MODE", "Subprocess' mode", (int*)¶ms_.kinematics.mode ); registerParameter( "NCSG", "Number of points to probe", (int*)¶ms_.generation().num_points ); registerParameter( "NGEN", "Number of events to generate", (int*)¶ms_.generation().maxgen ); registerParameter( "NPRN", "Number of events before printout", (int*)¶ms_.generation().gen_print_every ); //------------------------------------------------------------------------------------------- // Process-specific parameters //------------------------------------------------------------------------------------------- registerParameter( "METH", "Computation method (kT-factorisation)", &proc_params_->operator[]( "method" ) ); registerParameter( "IPOL", "Polarisation states to consider", &proc_params_->operator[]( "polarisationStates" ) ); //------------------------------------------------------------------------------------------- // Process kinematics parameters //------------------------------------------------------------------------------------------- + registerParameter( "KMRG", "KMR grid interpolation path", &kmr_grid_path_ ); registerParameter( "MGRD", "MSTW grid interpolation path", &mstw_grid_path_ ); registerParameter( "PMOD", "Outgoing primary particles' mode", &str_fun_ ); registerParameter( "EMOD", "Outgoing primary particles' mode", &str_fun_ ); registerParameter( "RTYP", "R-ratio computation type", &sr_type_ ); registerParameter( "PAIR", "Outgoing particles' PDG id", (int*)&proc_params_->operator[]( "pair" ) ); registerParameter( "INA1", "Heavy ion atomic weight (1st incoming beam)", (int*)&hi_1_.first ); registerParameter( "INZ1", "Heavy ion atomic number (1st incoming beam)", (int*)&hi_1_.second ); registerParameter( "INA2", "Heavy ion atomic weight (1st incoming beam)", (int*)&hi_2_.first ); registerParameter( "INZ2", "Heavy ion atomic number (1st incoming beam)", (int*)&hi_2_.second ); registerParameter( "INP1", "Momentum (1st primary particle)", ¶ms_.kinematics.incoming_beams.first.pz ); registerParameter( "INP2", "Momentum (2nd primary particle)", ¶ms_.kinematics.incoming_beams.second.pz ); registerParameter( "INPP", "Momentum (1st primary particle)", ¶ms_.kinematics.incoming_beams.first.pz ); registerParameter( "INPE", "Momentum (2nd primary particle)", ¶ms_.kinematics.incoming_beams.second.pz ); registerParameter( "PTCT", "Minimal transverse momentum (single central outgoing particle)", ¶ms_.kinematics.cuts.central.pt_single.min() ); registerParameter( "MSCT", "Minimal central system mass", ¶ms_.kinematics.cuts.central.mass_sum.min() ); registerParameter( "ECUT", "Minimal energy (single central outgoing particle)", ¶ms_.kinematics.cuts.central.energy_single.min() ); registerParameter( "ETMN", "Minimal pseudo-rapidity (central outgoing particles)", ¶ms_.kinematics.cuts.central.eta_single.min() ); registerParameter( "ETMX", "Maximal pseudo-rapidity (central outgoing particles)", ¶ms_.kinematics.cuts.central.eta_single.max() ); registerParameter( "YMIN", "Minimal rapidity (central outgoing particles)", ¶ms_.kinematics.cuts.central.rapidity_single.min() ); registerParameter( "YMAX", "Maximal rapidity (central outgoing particles)", ¶ms_.kinematics.cuts.central.rapidity_single.max() ); registerParameter( "Q2MN", "Minimal Q² = -q² (exchanged parton)", ¶ms_.kinematics.cuts.initial.q2.min() ); registerParameter( "Q2MX", "Maximal Q² = -q² (exchanged parton)", ¶ms_.kinematics.cuts.initial.q2.max() ); registerParameter( "MXMN", "Minimal invariant mass of proton remnants", ¶ms_.kinematics.cuts.remnants.mass_single.min() ); registerParameter( "MXMX", "Maximal invariant mass of proton remnants", ¶ms_.kinematics.cuts.remnants.mass_single.max() ); registerParameter( "XIMN", "Minimal fractional momentum loss of outgoing proton (ξ)", &xi_min_ ); registerParameter( "XIMX", "Maximal fractional momentum loss of outgoing proton (ξ)", &xi_max_ ); } void LpairHandler::store( const char* file ) { std::ofstream f( file, std::fstream::out | std::fstream::trunc ); if ( !f.is_open() ) throw CG_ERROR( "LpairHandler" ) << "Failed to open file \"" << file << "%s\" for writing."; for ( const auto& it : p_strings_ ) if ( it.second.value ) f << it.first << " = " << *it.second.value << "\n"; for ( const auto& it : p_ints_ ) if ( it.second.value ) f << it.first << " = " << *it.second.value << "\n"; for ( const auto& it : p_doubles_ ) if ( it.second.value ) f << it.first << " = " << *it.second.value << "\n"; for ( const auto& it : p_bools_ ) if ( it.second.value ) f << it.first << " = " << *it.second.value << "\n"; f.close(); } void LpairHandler::setParameter( const std::string& key, const std::string& value ) { - try { setValue( key.c_str(), std::stod( value ) ); } catch ( const std::invalid_argument& ) {} - try { setValue( key.c_str(), std::stoi( value ) ); } catch ( const std::invalid_argument& ) {} - try { setValue( key.c_str(), value ); } catch ( const std::invalid_argument& ) { - throw CG_FATAL( "LpairHandler:setParameter" ) - << "Failed to add the parameter \"" << key << "\" → \"" << value << "\"!"; + try { setValue( key.c_str(), std::stod( value ) ); } catch ( const std::invalid_argument& ) { + try { setValue( key.c_str(), std::stoi( value ) ); } catch ( const std::invalid_argument& ) { + try { setValue( key.c_str(), value ); } catch ( const std::invalid_argument& ) { + throw CG_FATAL( "LpairHandler:setParameter" ) + << "Failed to add the parameter \"" << key << "\" → \"" << value << "\"!"; + } + } } } std::string - LpairHandler::getParameter( std::string key ) const + LpairHandler::parameter( std::string key ) const { double dd = getValue( key.c_str() ); if ( dd != -999. ) return std::to_string( dd ); int ui = getValue( key.c_str() ); if ( ui != 999 ) return std::to_string( ui ); //if ( out = getValue( key.c_str() ) ); return getValue( key.c_str() ); } std::string - LpairHandler::getDescription( std::string key ) const + LpairHandler::description( std::string key ) const { if ( p_strings_.count( key ) ) return p_strings_.find( key )->second.description; if ( p_ints_.count( key ) ) return p_ints_.find( key )->second.description; if ( p_doubles_.count( key ) ) return p_doubles_.find( key )->second.description; if ( p_bools_.count( key ) ) return p_bools_.find( key )->second.description; return "null"; } } } diff --git a/CepGen/Cards/LpairHandler.h b/CepGen/Cards/LpairHandler.h index fef13af..22503f1 100644 --- a/CepGen/Cards/LpairHandler.h +++ b/CepGen/Cards/LpairHandler.h @@ -1,121 +1,121 @@ #ifndef CepGen_Cards_LpairReader_h #define CepGen_Cards_LpairReader_h #include "CepGen/Cards/Handler.h" #include using std::string; namespace cepgen { class ParametersList; namespace card { /// LPAIR-like steering cards parser and writer class LpairHandler : public Handler { public: /// Read a LPAIR steering card explicit LpairHandler( const char* file ); /// Store a configuration into a LPAIR steering card void store( const char* file ); private: /// Single parameter handler /// \tparam T Parameter type template struct Parameter { Parameter( const char* key, const char* descr, T* value ) : key( key ), description( descr ), value( value ) {} std::string key, description; T* value; }; /// Register a parameter to be steered to a configuration variable template void registerParameter( const char* key, const char* description, T* def ) {} /// Set a parameter value template void setValue( const char* key, const T& value ) {} /// Retrieve a parameter value template T getValue( const char* key ) const {} void setParameter( const std::string& key, const std::string& value ); - std::string getParameter( std::string key ) const; - std::string getDescription( std::string key ) const; + std::string parameter( std::string key ) const; + std::string description( std::string key ) const; static const int kInvalid; std::unordered_map > p_strings_; std::unordered_map > p_doubles_; std::unordered_map > p_ints_; std::unordered_map > p_bools_; void init(); std::shared_ptr proc_params_; int str_fun_, sr_type_; double xi_min_, xi_max_; std::string proc_name_, hadr_name_, out_mod_name_; std::string out_file_name_; std::string integr_type_; std::string kmr_grid_path_, mstw_grid_path_; std::pair hi_1_, hi_2_; }; //----- specialised registerers /// Register a string parameter template<> inline void LpairHandler::registerParameter( const char* key, const char* description, std::string* def ) { p_strings_.insert( std::make_pair( key, Parameter( key, description, def ) ) ); } /// Register a double floating point parameter template<> inline void LpairHandler::registerParameter( const char* key, const char* description, double* def ) { p_doubles_.insert( std::make_pair( key, Parameter( key, description, def ) ) ); } /// Register an integer parameter template<> inline void LpairHandler::registerParameter( const char* key, const char* description, int* def ) { p_ints_.insert( std::make_pair( key, Parameter( key, description, def ) ) ); } /// Register a boolean parameter template<> inline void LpairHandler::registerParameter( const char* key, const char* description, bool* def ) { p_bools_.insert( std::make_pair( key, Parameter( key, description, def ) ) ); } //----- specialised setters template<> inline void LpairHandler::setValue( const char* key, const std::string& value ) { auto it = p_strings_.find( key ); if ( it != p_strings_.end() ) *it->second.value = value; } template<> inline void LpairHandler::setValue( const char* key, const double& value ) { auto it = p_doubles_.find( key ); if ( it != p_doubles_.end() ) *it->second.value = value; } template<> inline void LpairHandler::setValue( const char* key, const int& value ) { auto it = p_ints_.find( key ); if ( it != p_ints_.end() ) *it->second.value = value; } template<> inline void LpairHandler::setValue( const char* key, const bool& value ) { auto it = p_bools_.find( key ); if ( it != p_bools_.end() ) *it->second.value = value; } //----- specialised getters /// Retrieve a string parameter value template<> inline std::string LpairHandler::getValue( const char* key ) const { const auto& it = p_strings_.find( key ); if ( it != p_strings_.end() ) return *it->second.value; return "null"; } /// Retrieve a floating point parameter value template<> inline double LpairHandler::getValue( const char* key ) const { const auto& it = p_doubles_.find( key ); if ( it != p_doubles_.end() ) return *it->second.value; return -999.; } /// Retrieve an integer parameter value template<> inline int LpairHandler::getValue( const char* key ) const { const auto& it = p_ints_.find( key ); if ( it != p_ints_.end() ) return *it->second.value; return 999; } /// Retrieve a boolean parameter value template<> inline bool LpairHandler::getValue( const char* key ) const { const auto& it = p_bools_.find( key ); if ( it != p_bools_.end() ) return *it->second.value; return true; } } } #endif