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"
22 #include "CLHEP/Random/RandomEngine.h"
32 CLHEP::HepRandomEngine* ExternalEngine=
nullptr;
33 CLHEP::HepRandomEngine* GetExternalEngine() {
return ExternalEngine; }
34 void SetExternalEngine(CLHEP::HepRandomEngine*
v) { ExternalEngine=
v; }
51 const char *
classname()
const {
return "SherpaHadronizer"; }
82 edm::LogVerbatim(
"SherpaHadronizer") <<
"Use stored reference for the external RNG";
88 double Get()
override;
96 if (cmsSherpaRng ==
nullptr) {
100 edm::LogVerbatim(
"SherpaHadronizer") <<
"Store assigned reference of the randomEngine";
101 SetExternalEngine(v);
105 <<
"The Sherpa interface got a randomEngine reference but there is no reference to the external RNG to hand it over to\n";
113 BaseHadronizer(params),
114 SherpaParameterSet(params.getParameter<edm::
ParameterSet>(
"SherpaParameters")),
115 isRNGinitialized(
false)
137 if (!params.
exists(
"SherpaWeightsBlock")) {
142 if (WeightsBlock.
exists(
"SherpaWeights"))
145 throw cms::Exception(
"SherpaInterface") <<
"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
146 if (WeightsBlock.
exists(
"SherpaVariationWeights"))
149 throw cms::Exception(
"SherpaInterface") <<
"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
150 edm::LogVerbatim(
"SherpaHadronizer") <<
"SherpaHadronizer will try rearrange the event weights according to SherpaWeights and SherpaVariationWeights";
155 int retval=Fetcher.
Fetch();
157 throw cms::Exception(
"SherpaInterface") <<
"SherpaHadronizer: Preparation of Sherpack failed ... ";
163 for (
unsigned i=0;
i<setNames.size(); ++
i ) {
166 edm::LogVerbatim(
"SherpaHadronizer") <<
"Write Sherpa parameter set " << setNames[
i] <<
" to "<<setNames[
i]<<
".dat ";
168 std::ofstream os(datfile.c_str());
170 for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
171 os<<(*itPar)<<std::endl;
190 arguments.push_back(shPathPiece.c_str());
227 for(std::vector<int>::const_iterator
iter = pdgIds.begin();
229 if (!markStable(*
iter))
252 edm::LogPrint(
"SherpaHadronizer") <<
"The order of event weights was changed!" ;
269 bool gen_event =
true;
270 while((itry < 3) && gen_event){
276 std::cerr <<
"Exception from Generator.GenerateOneEvent() catch. Call # "
277 << itry <<
" for this event\n";
282 HepMC::GenEvent* evt =
new HepMC::GenEvent();
294 bool unweighted =
false;
295 double weight_normalization = -1;
296 if(ATOOLS::ToType<int>(ATOOLS::rpa->
gen.Variable(
"EVENT_GENERATION_MODE")) == 1){
297 if (evt->weights().size()>2) {
299 weight_normalization = evt->weights()[2];
301 throw cms::Exception(
"SherpaInterface") <<
"Requested unweighted production. Missing normalization weight." << std::endl;
306 std::vector<double> newWeights;
309 if (evt->weights().has_key(
i)) {
310 newWeights.push_back(evt->weights()[
i]);
312 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
316 if (evt->weights().has_key(
i)) {
318 newWeights.push_back(evt->weights()[
i]/weight_normalization);
320 newWeights.push_back(evt->weights()[
i]);
323 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
330 evt->weights().clear();
331 for (
auto&
elem: newWeights) {
332 evt->weights().push_back(
elem);
336 evt->weights()[0]/=weight_normalization;
359 for(HepMC::GenEvent::particle_iterator
iter =
event->particles_begin();
360 iter !=
event->particles_end();
iter++)
361 (*iter)->set_status(getStatus(*
iter));
374 ATOOLS::External_RNG *ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::operator()(
const ATOOLS::RNG_Key &)
const
377 void ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::PrintInfo(std::ostream &str,
const size_t)
const
378 { str<<
"CMS_SHERPA_RNG interface"; }
383 <<
"The Sherpa code attempted to a generate random number while\n"
384 <<
"the engine pointer was null. This might mean that the code\n"
385 <<
"was modified to generate a random number outside the event and\n"
386 <<
"beginLuminosityBlock methods, which is not allowed.\n";
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
virtual void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::string SherpaProcess
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