7#include "GaudiKernel/MsgStream.h"
14#include "ATOOLS/Org/CXXFLAGS_PACKAGES.H"
16#include "SHERPA/Main/Sherpa.H"
17#include "SHERPA/Initialization/Initialization_Handler.H"
19#include "ATOOLS/Phys/Variations.H"
21#include "SHERPA/Tools/Variations.H"
23#include "ATOOLS/Org/Exception.H"
24#include "ATOOLS/Org/Run_Parameter.H"
33#include "CLHEP/Random/RandFlat.h"
52 m_params.value().push_back(
"SHERPA_LDADD=Sherpa_iPlugin");
59 for (
auto& inputfile : m_inputfiles) {
62 inputfile.second.erase(0, inputfile.second.find(
"\n") + 1);
63 inputfile.second.pop_back();
71 m_inputfiles[
"Base.yaml"] +=
"SHERPA_LDADD: Sherpa_iPlugin \n";
73 m_params.value().push_back(
"SHERPA_LDADD=Sherpa_iPlugin");
82 if(
msg().level()==MSG::FATAL ||
msg().level()==MSG::ERROR ||
msg().level()==MSG::WARNING ){
83 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 0 \n";
85 else if(
msg().level()==MSG::INFO){
86 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 2 \n";
88 else if(
msg().level()==MSG::DEBUG){
89 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 15 \n";
92 m_inputfiles[
"Base.yaml"] +=
"\nEVT_OUTPUT: 15 \n";
96 for (
auto& inputfile : m_inputfiles) {
98 FILE *
file = fopen(inputfile.first.c_str(),
"w");
101 return StatusCode::FAILURE;
103 fputs(inputfile.second.c_str(),
file);
105 ATH_MSG_INFO(
"Sherpa_i using the following settings in "+inputfile.first);
111 char** argv =
new char*[2];
112 argv[0] =
new char[7];
113 argv[1] =
new char[34];
114 strcpy(argv[0],
"Sherpa");
115 strcpy(argv[1],
"RUNDATA: [Base.yaml, Sherpa.yaml]");
116 p_sherpa =
new SHERPA::Sherpa(argc, argv);
127 p_sherpa->InitializeTheEventHandler();
129 catch (
const ATOOLS::normal_exit& exception) {
133 return StatusCode::FAILURE;
135 catch (
const ATOOLS::Exception& exception) {
138 return StatusCode::FAILURE;
146 p_sherpa->InitializeTheRun(argc,(
char **)argv);
149 p_sherpa->InitializeTheEventHandler();
151 catch (
const ATOOLS::Exception& exception) {
152 if (exception.Class()==
"Matrix_Element_Handler" && exception.Type()==ATOOLS::ex::normal_exit) {
160 return StatusCode::FAILURE;
163 catch (
const std::exception& exception) {
165 return StatusCode::FAILURE;
168 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
170 struct HepMC3::GenRunInfo::ToolInfo generator = {
171 std::string(
"SHERPA"),
172 std::string(SHERPA_VERSION)+
"." + std::string(SHERPA_SUBVERSION),
173 std::string(
"Used generator")
175 m_runinfo->tools().push_back(std::move(generator));
176 return StatusCode::SUCCESS;
184 const EventContext& ctx = Gaudi::Hive::currentContext();
190 }
while (
p_sherpa->GenerateOneEvent()==
false);
192 const size_t genEvents = ATOOLS::rpa->gen.NumberOfGeneratedEvents();
193 if (genEvents%1000==0) {
197 return StatusCode::SUCCESS;
202 if (!event->run_info())
event->set_run_info(
m_runinfo);
207 if (event->weights().size()>2) {
208 double nominal =
event->weight(
"Weight");
209 for (
const auto& name: event->weight_names()) {
210 if (name ==
"WeightNormalisation")
continue;
211 if (name ==
"NTrials")
continue;
212 if (name ==
"Weight")
continue;
214 ATH_MSG_INFO(
"Capping variation" << name <<
" = " << event->weight(name)/nominal <<
"*nominal");
217 ATH_MSG_DEBUG(
"Sherpa WEIGHT " << name <<
" value="<< event->weight(name));
221 event->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
224 return StatusCode::SUCCESS;
231 std::cout <<
"MetaData: generator = Sherpa" << SHERPA_VERSION <<
"." << SHERPA_SUBVERSION << std::endl;
232 std::cout <<
"MetaData: cross-section (nb)= " <<
p_sherpa->TotalXS()/1000.0*
m_xsscale << std::endl;
234 std::cout <<
"MetaData: PDF = " <<
p_sherpa->PDFInfo() << std::endl;
236 std::cout <<
"Named variations initialised by Sherpa:" << std::endl;
237 std::cout << *
p_sherpa->GetInitHandler()->GetVariations() << std::endl;
242 ATH_MSG_INFO(
"Deleting left-over files from working directory.");
243 system(
"rm -rf Results.db Process MIG_*.db MPI_*.dat libSherpa*.so libProc*.so");
246 return StatusCode::SUCCESS;
252 std::vector<std::string> params;
255 params.push_back(
"EXTERNAL_RNG=Atlas_RNG");
261 std::string verbose_arg;
262 MsgStream log(msgSvc(), name());
263 if( log.level()==MSG::FATAL || log.level()==MSG::ERROR || log.level()==MSG::WARNING ){
264 params.push_back(
"OUTPUT=0");
266 else if(log.level()==MSG::INFO){
267 params.push_back(
"OUTPUT=2");
269 else if(log.level()==MSG::DEBUG){
270 params.push_back(
"OUTPUT=3");
273 params.push_back(
"OUTPUT=15");
280 params.insert(params.begin()+params.size(),
m_params.begin(),
m_params.end());
284 FILE *
file = fopen(
"Run.dat",
"w");
297 argc = 1+params.size();
298 argv =
new char * [ 1+params.size() ];
299 argv[0] =
new char[7];
300 strcpy(argv[0],
"Sherpa");
304 for(
size_t i=0; i<params.size(); i++) {
306 argv[i+1] =
new char[params[i].size()+1];
307 strcpy(argv[i+1], params[i].c_str());
310 ATH_MSG_INFO(
"Further Sherpa initialisation output will be redirected to the LOG_FILE specified above.");
317 FILE *
file = fopen(
"Sherpa_iPlugin.C",
"w");
322 fputs(pluginCode.c_str(),
file);
329 command +=
"tail -n +2 Sherpa_iPlugin.C | head -n -1 > Sherpa_iPlugin.C.tmp; mv Sherpa_iPlugin.C.tmp Sherpa_iPlugin.C; ";
330 command +=
"g++ -shared -std=c++0x -g ";
331 command +=
"-I`Sherpa-config --incdir` ";
332 command +=
"`Sherpa-config --ldflags` ";
333 command +=
"-I$FASTJETPATH/include ";
334 command +=
"-fPIC -o libSherpa_iPlugin.so Sherpa_iPlugin.C";
335 ATH_MSG_INFO(
"Now compiling plugin library using: "+command);
336 if (system(command.c_str())!=0) {
355 return CLHEP::RandFlat::shoot(
p_engine);
360 std::string result{
""};
362 char alphabet[nMax] = {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
363 'h',
'i',
'j',
'k',
'l',
'm',
'n',
364 'o',
'p',
'q',
'r',
's',
't',
'u',
365 'v',
'w',
'x',
'y',
'z' };
366 for (
size_t i = 0; i < 6; ++i) {
369 result += alphabet[rand() % nMax];
378 if ( !stat(
"/dev/shm", &info)) {
382 std::cout <<
"RNG state being saved to: " <<
m_filename << std::endl;
392DECLARE_GETTER(
Atlas_RNG,
"Atlas_RNG",External_RNG,RNG_Key);
396 public Getter_Function<External_RNG,RNG_Key,std::less<std::string>> {
401 Object_Type *
operator()(
const Parameter_Type ¶meters)
const;
404 Getter_Function<External_RNG,RNG_Key,
std::less<
std::
string>>(
"Atlas_RNG")
408ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>
409ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::s_initializer(
true);
412External_RNG *ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::operator()(
const RNG_Key &)
const
415void ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::PrintInfo(std::ostream &
str,
const size_t)
const
416{
str<<
"Atlas RNG interface"; }
ATLAS-specific HepMC functions.
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
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.
std::shared_ptr< HepMC3::GenRunInfo > m_runinfo
The run info for HepMC3.
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.
HepMC3::GenEvent GenEvent
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.