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"
26 #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"; }
88 edm::LogVerbatim(
"SherpaHadronizer") <<
"Use stored reference for the external RNG";
94 double Get()
override;
102 if (cmsSherpaRng ==
nullptr) {
106 edm::LogVerbatim(
"SherpaHadronizer") <<
"Store assigned reference of the randomEngine";
107 SetExternalEngine(v);
111 <<
"The Sherpa interface got a randomEngine reference but there is no reference to the external RNG to hand it over to\n";
119 BaseHadronizer(params),
120 SherpaParameterSet(params.getParameter<edm::
ParameterSet>(
"SherpaParameters")),
121 isRNGinitialized(
false)
143 if (!params.
exists(
"SherpaWeightsBlock")) {
148 if (WeightsBlock.
exists(
"SherpaWeights"))
151 throw cms::Exception(
"SherpaInterface") <<
"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
152 if (WeightsBlock.
exists(
"SherpaVariationWeights"))
155 throw cms::Exception(
"SherpaInterface") <<
"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
156 edm::LogVerbatim(
"SherpaHadronizer") <<
"SherpaHadronizer will try rearrange the event weights according to SherpaWeights and SherpaVariationWeights";
161 int retval=Fetcher.
Fetch();
163 throw cms::Exception(
"SherpaInterface") <<
"SherpaHadronizer: Preparation of Sherpack failed ... ";
169 for (
unsigned i=0;
i<setNames.size(); ++
i ) {
172 edm::LogVerbatim(
"SherpaHadronizer") <<
"Write Sherpa parameter set " << setNames[
i] <<
" to "<<setNames[
i]<<
".dat ";
174 std::ofstream os(datfile.c_str());
176 for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
177 os<<(*itPar)<<std::endl;
257 bool gen_event =
true;
258 while((itry < 3) && gen_event){
264 std::cerr <<
"Exception from Generator->GenerateOneEvent() catch. Call # "
265 << itry <<
" for this event\n";
270 HepMC::GenEvent* evt =
new HepMC::GenEvent();
282 bool unweighted =
false;
283 double weight_normalization = -1;
284 if(ATOOLS::ToType<int>(ATOOLS::rpa->
gen.Variable(
"EVENT_GENERATION_MODE")) == 1){
285 if (evt->weights().size()>2) {
287 weight_normalization = evt->weights()[2];
289 throw cms::Exception(
"SherpaInterface") <<
"Requested unweighted production. Missing normalization weight." << std::endl;
294 std::vector<double> newWeights;
297 if (evt->weights().has_key(
i)) {
298 newWeights.push_back(evt->weights()[
i]);
300 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
304 if (evt->weights().has_key(
i)) {
306 newWeights.push_back(evt->weights()[
i]/weight_normalization);
308 newWeights.push_back(evt->weights()[
i]);
311 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
317 evt->weights().clear();
318 for (
auto&
elem: newWeights) {
319 evt->weights().push_back(
elem);
324 evt->weights()[0]/=weight_normalization;
357 ATOOLS::External_RNG *ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::operator()(
const ATOOLS::RNG_Key &)
const
360 void ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::PrintInfo(std::ostream &str,
const size_t)
const
361 { str<<
"CMS_SHERPA_RNG interface"; }
366 <<
"The Sherpa code attempted to a generate random number while\n"
367 <<
"the engine pointer was null. This might mean that the code\n"
368 <<
"was modified to generate a random number outside the event and\n"
369 <<
"beginLuminosityBlock methods, which is not allowed.\n";
379 edm::LogPrint(
"SherpaHadronizer") <<
"The order of event weights was changed!" ;
389 return genLumiInfoHeader;
T getParameter(std::string const &) const
#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)
std::auto_ptr< HepMC::GenEvent > & event()
GenRunInfoProduct & runInfo()
edm::ParameterSet SherpaParameterSet
bool initializeForInternalPartons()
std::vector< std::string > variationweightlist
bool generatePartonsAndHadronize()
bool declareSpecialSettings(const std::vector< std::string > &)
void setRandomEngine(CLHEP::HepRandomEngine *v)
double SherpaDefaultWeight
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::string SherpaProcess
GenLumiInfoHeader * getGenLumiInfoHeader() const override
std::string SherpaResultDir
std::vector< std::string > arguments
SherpaHadronizer(const edm::ParameterSet ¶ms)
void resetEvent(HepMC::GenEvent *event)
volatile std::atomic< bool > shutdown_flag false
DECLARE_GETTER(CMS_SHERPA_RNG,"CMS_SHERPA_RNG", ATOOLS::External_RNG, ATOOLS::RNG_Key)
const char * classname() const
std::string SherpaChecksum