6#include "GaudiKernel/MsgStream.h"
13#include "ATOOLS/Org/CXXFLAGS_PACKAGES.H"
19#include "SHERPA/Main/Sherpa.H"
20#include "SHERPA/Initialization/Initialization_Handler.H"
22#include "ATOOLS/Phys/Variations.H"
24#include "SHERPA/Tools/Variations.H"
26#include "ATOOLS/Org/Exception.H"
27#include "ATOOLS/Org/Run_Parameter.H"
35#include "CLHEP/Random/RandFlat.h"
54 m_params.value().push_back(
"SHERPA_LDADD=Sherpa_iPlugin");
61 for (
auto& inputfile : m_inputfiles) {
64 inputfile.second.erase(0, inputfile.second.find(
"\n") + 1);
65 inputfile.second.pop_back();
73 m_inputfiles[
"Base.yaml"] +=
"SHERPA_LDADD: Sherpa_iPlugin \n";
75 m_params.value().push_back(
"SHERPA_LDADD=Sherpa_iPlugin");
84 if(
msg().level()==MSG::FATAL ||
msg().level()==MSG::ERROR ||
msg().level()==MSG::WARNING ){
85 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 0 \n";
87 else if(
msg().level()==MSG::INFO){
88 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 2 \n";
90 else if(
msg().level()==MSG::DEBUG){
91 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 15 \n";
94 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 15 \n";
98 for (
auto& inputfile : m_inputfiles) {
100 FILE *
file = fopen(inputfile.first.c_str(),
"w");
103 return StatusCode::FAILURE;
105 fputs(inputfile.second.c_str(),
file);
107 ATH_MSG_INFO(
"Sherpa_i using the following settings in "+inputfile.first);
113 char** argv =
new char*[2];
114 argv[0] =
new char[7];
115 argv[1] =
new char[34];
116 strcpy(argv[0],
"Sherpa");
117 strcpy(argv[1],
"RUNDATA: [Base.yaml, Sherpa.yaml]");
118 p_sherpa =
new SHERPA::Sherpa(argc, argv);
129 p_sherpa->InitializeTheEventHandler();
131 catch (
const ATOOLS::normal_exit& exception) {
135 return StatusCode::FAILURE;
137 catch (
const ATOOLS::Exception& exception) {
140 return StatusCode::FAILURE;
148 p_sherpa->InitializeTheRun(argc,(
char **)argv);
151 p_sherpa->InitializeTheEventHandler();
153 catch (
const ATOOLS::Exception& exception) {
154 if (exception.Class()==
"Matrix_Element_Handler" && exception.Type()==ATOOLS::ex::normal_exit) {
162 return StatusCode::FAILURE;
165 catch (
const std::exception& exception) {
167 return StatusCode::FAILURE;
171 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
173 struct HepMC3::GenRunInfo::ToolInfo generator = {
174 std::string(
"SHERPA"),
175 std::string(SHERPA_VERSION)+
"." + std::string(SHERPA_SUBVERSION),
176 std::string(
"Used generator")
178 m_runinfo->tools().push_back(std::move(generator));
180 return StatusCode::SUCCESS;
188 const EventContext& ctx = Gaudi::Hive::currentContext();
194 }
while (
p_sherpa->GenerateOneEvent()==
false);
196 const size_t genEvents = ATOOLS::rpa->gen.NumberOfGeneratedEvents();
197 if (genEvents%1000==0) {
201 return StatusCode::SUCCESS;
207 if (!event->run_info())
event->set_run_info(m_runinfo);
214 if (event->weights().size()>2) {
215 double nominal =
event->weight(
"Weight");
216 for (
const auto& name: event->weight_names()) {
217 if (name ==
"WeightNormalisation")
continue;
218 if (name ==
"NTrials")
continue;
219 if (name ==
"Weight")
continue;
220 if (name ==
"NTrials")
continue;
222 ATH_MSG_INFO(
"Capping variation" << name <<
" = " << event->weight(name)/nominal <<
"*nominal");
225 ATH_MSG_DEBUG(
"Sherpa WEIGHT " << name <<
" value="<< event->weight(name));
229 if (event->weights().size()>2) {
230 for (
size_t i=0; i<
event->weights().size(); ++i) {
234 ATH_MSG_INFO(
"Capping variation" << i <<
" = " << event->weights()[i]/event->weights()[0] <<
"*nominal");
235 event->weights()[i] *=
m_variation_weight_cap*std::abs(event->weights()[0])/std::abs(event->weights()[i]);
238 ATH_MSG_DEBUG(
"Sherpa WEIGHT " << i <<
" value="<< event->weights()[i]);
244 event->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
250 return StatusCode::SUCCESS;
257 std::cout <<
"MetaData: generator = Sherpa" << SHERPA_VERSION <<
"." << SHERPA_SUBVERSION << std::endl;
258 std::cout <<
"MetaData: cross-section (nb)= " <<
p_sherpa->TotalXS()/1000.0*
m_xsscale << std::endl;
260 std::cout <<
"MetaData: PDF = " <<
p_sherpa->PDFInfo() << std::endl;
262 std::cout <<
"Named variations initialised by Sherpa:" << std::endl;
263 std::cout << *
p_sherpa->GetInitHandler()->GetVariations() << std::endl;
268 ATH_MSG_INFO(
"Deleting left-over files from working directory.");
269 system(
"rm -rf Results.db Process MIG_*.db MPI_*.dat libSherpa*.so libProc*.so");
272 return StatusCode::SUCCESS;
278 std::vector<std::string> params;
281 params.push_back(
"EXTERNAL_RNG=Atlas_RNG");
287 std::string verbose_arg;
288 MsgStream log(msgSvc(), name());
289 if( log.level()==MSG::FATAL || log.level()==MSG::ERROR || log.level()==MSG::WARNING ){
290 params.push_back(
"OUTPUT=0");
292 else if(log.level()==MSG::INFO){
293 params.push_back(
"OUTPUT=2");
295 else if(log.level()==MSG::DEBUG){
296 params.push_back(
"OUTPUT=3");
299 params.push_back(
"OUTPUT=15");
306 params.insert(params.begin()+params.size(),
m_params.begin(),
m_params.end());
310 FILE *
file = fopen(
"Run.dat",
"w");
323 argc = 1+params.size();
324 argv =
new char * [ 1+params.size() ];
325 argv[0] =
new char[7];
326 strcpy(argv[0],
"Sherpa");
330 for(
size_t i=0; i<params.size(); i++) {
332 argv[i+1] =
new char[params[i].size()+1];
333 strcpy(argv[i+1], params[i].c_str());
336 ATH_MSG_INFO(
"Further Sherpa initialisation output will be redirected to the LOG_FILE specified above.");
343 FILE *
file = fopen(
"Sherpa_iPlugin.C",
"w");
348 fputs(pluginCode.c_str(),
file);
355 command +=
"tail -n +2 Sherpa_iPlugin.C | head -n -1 > Sherpa_iPlugin.C.tmp; mv Sherpa_iPlugin.C.tmp Sherpa_iPlugin.C; ";
356 command +=
"g++ -shared -std=c++0x -g ";
357 command +=
"-I`Sherpa-config --incdir` ";
358 command +=
"`Sherpa-config --ldflags` ";
359 command +=
"-I$FASTJETPATH/include ";
360 command +=
"-fPIC -o libSherpa_iPlugin.so Sherpa_iPlugin.C";
361 ATH_MSG_INFO(
"Now compiling plugin library using: "+command);
362 if (system(command.c_str())!=0) {
381 return CLHEP::RandFlat::shoot(
p_engine);
388 char alphabet[nMax] = {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
389 'h',
'i',
'j',
'k',
'l',
'm',
'n',
390 'o',
'p',
'q',
'r',
's',
't',
'u',
391 'v',
'w',
'x',
'y',
'z' };
392 for (
size_t i = 0; i < 6; ++i) {
393 result += alphabet[rand() % nMax];
402 if ( !stat(
"/dev/shm", &info)) {
406 std::cout <<
"RNG state being saved to: " <<
m_filename << std::endl;
416DECLARE_GETTER(
Atlas_RNG,
"Atlas_RNG",External_RNG,RNG_Key);
420 public Getter_Function<External_RNG,RNG_Key,std::less<std::string>> {
425 Object_Type *
operator()(
const Parameter_Type ¶meters)
const;
428 Getter_Function<External_RNG,RNG_Key,
std::less<
std::
string>>(
"Atlas_RNG")
432ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>
433ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::s_initializer(
true);
436External_RNG *ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::operator()(
const RNG_Key &)
const
439void ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::PrintInfo(std::ostream &
str,
const size_t)
const
440{
str<<
"Atlas RNG interface"; }
CLHEP::HepRandomEngine * p_rndEngine
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Atlas_RNG(CLHEP::HepRandomEngine *)
CLHEP::HepRandomEngine * p_engine
std::once_flag m_once_flag_atlas_rng
const std::string GenerateUID() const
void GeVToMeV(HepMC::GenEvent *evt)
Scale event energies/momenta by x 1000.
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
CLHEP::HepRandomEngine * getRandomEngineDuringInitialize(const std::string &streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun=1, unsigned int lbn=1) const
IntegerProperty m_randomSeed
Seed for random number engine.
StatusCode genFinalize()
For finalising the generator, if required.
StringProperty m_runcard
FIXME unused?
StatusCode genInitialize()
For initializing the generator, if required.
SHERPA::Sherpa * p_sherpa
StringProperty m_plugincode
Optional code for plugin library to compile and load at run time.
StringArrayProperty m_params
List of additional Sherpa parameters beyond run card snippet (from JO file)
BooleanProperty m_cleanup
void compilePlugin(const std::string &)
Sherpa_i(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode callGenerator()
For calling the generator on each iteration of the event loop.
DoubleProperty m_variation_weight_cap
Variation weight cap factor.
StatusCode fillEvt(HepMC::GenEvent *evt)
For filling the HepMC event object.
void getParameters(int &argc, char **&argv)
void calculateSeedsMC21(long *seeds, const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Set the random seed using a string (e.g.
DataModel_detail::iterator< DVL > remove(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, const T &value)
Specialization of remove for DataVector/List.