9 #include "SHERPA/Main/Sherpa.H"
10 #include "ATOOLS/Math/Random.H"
12 #include "ATOOLS/Org/Run_Parameter.H"
13 #include "ATOOLS/Org/MyStrStream.H"
14 #include "ATOOLS/Org/CXXFLAGS.H"
15 #include "ATOOLS/Org/CXXFLAGS_PACKAGES.H"
16 #include "ATOOLS/Org/My_MPI.H"
24 #include "CLHEP/Random/RandomEngine.h"
35 CLHEP::HepRandomEngine *ExternalEngine =
nullptr;
36 CLHEP::HepRandomEngine *GetExternalEngine() {
return ExternalEngine; }
37 void SetExternalEngine(CLHEP::HepRandomEngine *
v) { ExternalEngine =
v; }
56 const char *
classname()
const {
return "SherpaHadronizer"; }
80 edm::LogVerbatim(
"SherpaHadronizer") <<
"Use stored reference for the external RNG";
86 double Get()
override;
92 if (cmsSherpaRng ==
nullptr) {
96 edm::LogVerbatim(
"SherpaHadronizer") <<
"Store assigned reference of the randomEngine";
102 <<
"The Sherpa interface got a randomEngine reference but there is "
103 "no reference to the external RNG to hand it over to\n";
112 : BaseHadronizer(params),
113 SherpaParameterSet(params.getParameter<edm::
ParameterSet>(
"SherpaParameters")),
114 isRNGinitialized(
false) {
115 if (!params.
exists(
"SherpaProcess"))
119 if (!params.
exists(
"SherpaPath"))
123 if (!params.
exists(
"SherpaPathPiece"))
127 if (!params.
exists(
"SherpaResultDir"))
131 if (!params.
exists(
"SherpaDefaultWeight"))
135 if (!params.
exists(
"maxEventsToPrint"))
147 if (!params.
exists(
"SherpaWeightsBlock")) {
152 if (WeightsBlock.
exists(
"SherpaWeights"))
155 throw cms::Exception(
"SherpaInterface") <<
"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
156 if (WeightsBlock.
exists(
"SherpaVariationWeights"))
160 <<
"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
161 edm::LogVerbatim(
"SherpaHadronizer") <<
"SherpaHadronizer will try rearrange the event weights according to "
162 "SherpaWeights and SherpaVariationWeights";
166 int retval = Fetcher.
Fetch();
168 throw cms::Exception(
"SherpaInterface") <<
"SherpaHadronizer: Preparation of Sherpack failed ... ";
174 for (
unsigned i = 0;
i < setNames.size(); ++
i) {
177 edm::LogVerbatim(
"SherpaHadronizer") <<
"Write Sherpa parameter set " << setNames[
i] <<
" to " << setNames[
i]
180 std::ofstream os(datfile.c_str());
182 for (std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar) {
183 os << (*itPar) << std::endl;
251 bool gen_event =
true;
252 while ((itry < 3) && gen_event) {
258 std::cerr <<
"Exception from Generator->GenerateOneEvent() catch. Call # " << itry <<
" for this event\n";
263 auto evt = std::make_unique<HepMC::GenEvent>();
275 bool unweighted =
false;
276 double weight_normalization = -1;
277 if (ATOOLS::ToType<int>(ATOOLS::rpa->
gen.Variable(
"EVENT_GENERATION_MODE")) == 1) {
278 if (evt->weights().size() > 2) {
280 weight_normalization = evt->weights()[2];
283 <<
"Requested unweighted production. Missing normalization weight." << std::endl;
288 std::vector<double> newWeights;
291 if (evt->weights().has_key(
i)) {
292 newWeights.push_back(evt->weights()[
i]);
295 <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
299 if (evt->weights().has_key(
i)) {
301 newWeights.push_back(evt->weights()[
i] / weight_normalization);
303 newWeights.push_back(evt->weights()[
i]);
307 <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
312 evt->weights().clear();
313 for (
auto &elem : newWeights) {
314 evt->weights().push_back(elem);
319 evt->weights()[0] /= weight_normalization;
343 ATOOLS::External_RNG *ATOOLS::Getter<ATOOLS::External_RNG, ATOOLS::RNG_Key, CMS_SHERPA_RNG>::operator()(
344 const ATOOLS::RNG_Key &)
const {
348 void ATOOLS::Getter<ATOOLS::External_RNG, ATOOLS::RNG_Key, CMS_SHERPA_RNG>::PrintInfo(std::ostream &
str,
349 const size_t)
const {
350 str <<
"CMS_SHERPA_RNG interface";
356 <<
"the engine pointer was null. This might mean that the code\n"
357 <<
"was modified to generate a random number outside the event and\n"
358 <<
"beginLuminosityBlock methods, which is not allowed.\n";
363 auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
366 edm::LogPrint(
"SherpaHadronizer") <<
"The order of event weights was changed!";
368 genLumiInfoHeader->weightNames().push_back(
i);
372 genLumiInfoHeader->weightNames().push_back(
i);
376 return genLumiInfoHeader;
Log< level::Info, true > LogVerbatim
#define DEFINE_FWK_MODULE(type)
CLHEP::HepRandomEngine * randomEngine
bool exists(std::string const ¶meterName) const
checks if a parameter exists
std::vector< std::string > weightlist
std::string SherpaPathPiece
unsigned int maxEventsToPrint
edm::GeneratorFilter< SherpaHadronizer, gen::ExternalDecayDriver > SherpaGeneratorFilter
void setInternalXSec(const XSec &xsec)
bool declareStableParticles(const std::vector< int > &pdgIds)
GenRunInfoProduct & runInfo()
edm::ParameterSet SherpaParameterSet
bool initializeForInternalPartons()
void resetEvent(std::unique_ptr< HepMC::GenEvent > event)
std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const override
std::vector< std::string > variationweightlist
std::unique_ptr< HepMC::GenEvent > & event()
Log< level::Warning, true > LogPrint
bool generatePartonsAndHadronize()
~SherpaHadronizer() override
bool declareSpecialSettings(const std::vector< std::string > &)
void setRandomEngine(CLHEP::HepRandomEngine *v)
double SherpaDefaultWeight
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::string SherpaProcess
T getParameter(std::string const &) const
std::string SherpaResultDir
std::vector< std::string > arguments
SherpaHadronizer(const edm::ParameterSet ¶ms)
DECLARE_GETTER(CMS_SHERPA_RNG,"CMS_SHERPA_RNG", ATOOLS::External_RNG, ATOOLS::RNG_Key)
const char * classname() const
std::string SherpaChecksum