10 #include "SHERPA/Main/Sherpa.H" 11 #include "ATOOLS/Math/Random.H" 13 #include "ATOOLS/Org/Run_Parameter.H" 14 #include "ATOOLS/Org/MyStrStream.H" 15 #include "ATOOLS/Org/CXXFLAGS.H" 16 #include "ATOOLS/Org/CXXFLAGS_PACKAGES.H" 17 #include "ATOOLS/Org/My_MPI.H" 25 #include "CLHEP/Random/RandomEngine.h" 36 CLHEP::HepRandomEngine* ExternalEngine=
nullptr;
37 CLHEP::HepRandomEngine* GetExternalEngine() {
return ExternalEngine; }
38 void SetExternalEngine(CLHEP::HepRandomEngine*
v) { ExternalEngine=
v; }
57 const char *
classname()
const {
return "SherpaHadronizer"; }
87 edm::LogVerbatim(
"SherpaHadronizer") <<
"Use stored reference for the external RNG";
93 double Get()
override;
100 if (cmsSherpaRng ==
nullptr) {
104 edm::LogVerbatim(
"SherpaHadronizer") <<
"Store assigned reference of the randomEngine";
105 SetExternalEngine(v);
110 <<
"The Sherpa interface got a randomEngine reference but there is " 111 "no reference to the external RNG to hand it over to\n";
144 if (!params.
exists(
"SherpaWeightsBlock")) {
149 if (WeightsBlock.
exists(
"SherpaWeights"))
152 throw cms::Exception(
"SherpaInterface") <<
"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
153 if (WeightsBlock.
exists(
"SherpaVariationWeights"))
156 throw cms::Exception(
"SherpaInterface") <<
"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
157 edm::LogVerbatim(
"SherpaHadronizer") <<
"SherpaHadronizer will try rearrange the event weights according to SherpaWeights and SherpaVariationWeights";
162 int retval=Fetcher.
Fetch();
164 throw cms::Exception(
"SherpaInterface") <<
"SherpaHadronizer: Preparation of Sherpack failed ... ";
170 for (
unsigned i=0;
i<setNames.size(); ++
i ) {
173 edm::LogVerbatim(
"SherpaHadronizer") <<
"Write Sherpa parameter set " << setNames[
i] <<
" to "<<setNames[
i]<<
".dat ";
175 std::ofstream os(datfile.c_str());
177 for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
178 os<<(*itPar)<<std::endl;
256 bool gen_event =
true;
257 while((itry < 3) && gen_event){
263 std::cerr <<
"Exception from Generator->GenerateOneEvent() catch. Call # " 264 << itry <<
" for this event\n";
269 auto evt = std::make_unique<HepMC::GenEvent>();
281 bool unweighted =
false;
282 double weight_normalization = -1;
283 if(ATOOLS::ToType<int>(ATOOLS::rpa->
gen.Variable(
"EVENT_GENERATION_MODE")) == 1){
284 if (evt->weights().size()>2) {
286 weight_normalization = evt->weights()[2];
288 throw cms::Exception(
"SherpaInterface") <<
"Requested unweighted production. Missing normalization weight." << std::endl;
293 std::vector<double> newWeights;
296 if (evt->weights().has_key(
i)) {
297 newWeights.push_back(evt->weights()[
i]);
299 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
303 if (evt->weights().has_key(
i)) {
305 newWeights.push_back(evt->weights()[
i]/weight_normalization);
307 newWeights.push_back(evt->weights()[
i]);
310 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
316 evt->weights().clear();
317 for (
auto&
elem: newWeights) {
318 evt->weights().push_back(
elem);
323 evt->weights()[0]/=weight_normalization;
356 ATOOLS::External_RNG *ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::operator()(
const ATOOLS::RNG_Key &)
const 359 void ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::PrintInfo(std::ostream &
str,
const size_t)
const 360 {
str<<
"CMS_SHERPA_RNG interface"; }
365 <<
"The Sherpa code attempted to a generate random number while\n" 366 <<
"the engine pointer was null. This might mean that the code\n" 367 <<
"was modified to generate a random number outside the event and\n" 368 <<
"beginLuminosityBlock methods, which is not allowed.\n";
377 edm::LogPrint(
"SherpaHadronizer") <<
"The order of event weights was changed!" ;
CLHEP::HepRandomEngine * randomEngine
T getParameter(std::string const &) const
CLHEP::HepRandomEngine * randomEngine
BaseHadronizer(edm::ParameterSet const &ps)
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()
std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const override
edm::ParameterSet SherpaParameterSet
#define DEFINE_FWK_MODULE(type)
bool initializeForInternalPartons()
void setRandomEngine(CLHEP::HepRandomEngine *v)
void resetEvent(std::unique_ptr< HepMC::GenEvent > event)
std::vector< std::string > variationweightlist
std::unique_ptr< HepMC::GenEvent > & event()
bool generatePartonsAndHadronize()
def elem(elemtype, innerHTML='', html_class='', kwargs)
~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
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