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; }
53 const char *
classname()
const {
return "SherpaHadronizer"; }
83 edm::LogVerbatim(
"SherpaHadronizer") <<
"Use stored reference for the external RNG";
89 double Get()
override;
97 if (cmsSherpaRng ==
nullptr) {
101 edm::LogVerbatim(
"SherpaHadronizer") <<
"Store assigned reference of the randomEngine";
102 SetExternalEngine(v);
106 <<
"The Sherpa interface got a randomEngine reference but there is no reference to the external RNG to hand it over to\n";
114 BaseHadronizer(params),
115 SherpaParameterSet(params.getParameter<edm::
ParameterSet>(
"SherpaParameters")),
116 isRNGinitialized(
false)
138 if (!params.
exists(
"SherpaWeightsBlock")) {
143 if (WeightsBlock.
exists(
"SherpaWeights"))
146 throw cms::Exception(
"SherpaInterface") <<
"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
147 if (WeightsBlock.
exists(
"SherpaVariationWeights"))
150 throw cms::Exception(
"SherpaInterface") <<
"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
151 edm::LogVerbatim(
"SherpaHadronizer") <<
"SherpaHadronizer will try rearrange the event weights according to SherpaWeights and SherpaVariationWeights";
156 int retval=Fetcher.
Fetch();
158 throw cms::Exception(
"SherpaInterface") <<
"SherpaHadronizer: Preparation of Sherpack failed ... ";
164 for (
unsigned i=0;
i<setNames.size(); ++
i ) {
167 edm::LogVerbatim(
"SherpaHadronizer") <<
"Write Sherpa parameter set " << setNames[
i] <<
" to "<<setNames[
i]<<
".dat ";
169 std::ofstream os(datfile.c_str());
171 for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
172 os<<(*itPar)<<std::endl;
191 arguments.push_back(shPathPiece.c_str());
228 for(std::vector<int>::const_iterator iter = pdgIds.begin();
229 iter != pdgIds.end(); ++iter)
230 if (!markStable(*iter))
262 bool gen_event =
true;
263 while((itry < 3) && gen_event){
269 std::cerr <<
"Exception from Generator.GenerateOneEvent() catch. Call # "
270 << itry <<
" for this event\n";
275 HepMC::GenEvent* evt =
new HepMC::GenEvent();
287 bool unweighted =
false;
288 double weight_normalization = -1;
289 if(ATOOLS::ToType<int>(ATOOLS::rpa->
gen.Variable(
"EVENT_GENERATION_MODE")) == 1){
290 if (evt->weights().size()>2) {
292 weight_normalization = evt->weights()[2];
294 throw cms::Exception(
"SherpaInterface") <<
"Requested unweighted production. Missing normalization weight." << std::endl;
299 std::vector<double> newWeights;
302 if (evt->weights().has_key(
i)) {
303 newWeights.push_back(evt->weights()[
i]);
305 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
309 if (evt->weights().has_key(
i)) {
311 newWeights.push_back(evt->weights()[
i]/weight_normalization);
313 newWeights.push_back(evt->weights()[
i]);
316 throw cms::Exception(
"SherpaInterface") <<
"Missing weights! Key " <<
i <<
" not found, please check the weight definition!" << std::endl;
322 evt->weights().clear();
323 for (
auto&
elem: newWeights) {
324 evt->weights().push_back(
elem);
329 evt->weights()[0]/=weight_normalization;
352 for(HepMC::GenEvent::particle_iterator iter =
event->particles_begin();
353 iter !=
event->particles_end(); iter++)
354 (*iter)->set_status(getStatus(*iter));
367 ATOOLS::External_RNG *ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::operator()(
const ATOOLS::RNG_Key &)
const
370 void ATOOLS::Getter<ATOOLS::External_RNG,ATOOLS::RNG_Key,CMS_SHERPA_RNG>::PrintInfo(std::ostream &str,
const size_t)
const
371 { str<<
"CMS_SHERPA_RNG interface"; }
376 <<
"The Sherpa code attempted to a generate random number while\n"
377 <<
"the engine pointer was null. This might mean that the code\n"
378 <<
"was modified to generate a random number outside the event and\n"
379 <<
"beginLuminosityBlock methods, which is not allowed.\n";
388 edm::LogPrint(
"SherpaHadronizer") <<
"The order of event weights was changed!" ;
398 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()
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
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
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