ATLAS Offline Software
Loading...
Searching...
No Matches
Sherpa_i.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GaudiKernel/MsgStream.h"
9
10#include "Sherpa_i/Sherpa_i.h"
11
12
13#include "ATOOLS/Org/CXXFLAGS_PACKAGES.H"
14#ifdef HEPMC3
15#undef USING__HEPMC2
16#else
17#undef USING__HEPMC3
18#endif
19#include "SHERPA/Main/Sherpa.H"
20#include "SHERPA/Initialization/Initialization_Handler.H"
21#ifdef IS_SHERPA_3
22#include "ATOOLS/Phys/Variations.H"
23#else
24#include "SHERPA/Tools/Variations.H"
25#endif
26#include "ATOOLS/Org/Exception.H"
27#include "ATOOLS/Org/Run_Parameter.H"
28
29#include <cstdio>
30#include <cstring>
31#include <mutex>
32#include <signal.h>
33#include <stdlib.h>
34#include <sys/stat.h>
35#include "CLHEP/Random/RandFlat.h"
36
37CLHEP::HepRandomEngine* p_rndEngine{};
38
39Sherpa_i::Sherpa_i(const std::string& name, ISvcLocator* pSvcLocator)
40 : GenModule(name, pSvcLocator)
41{
42 #ifdef IS_SHERPA_3
43 declareProperty("BaseFragment", m_inputfiles["Base.yaml"] = "");
44 declareProperty("RunCard", m_inputfiles["Sherpa.yaml"] = "");
45 #endif
46}
47
48
49
51 if (m_plugincode != "") {
53 #ifndef IS_SHERPA_3
54 m_params.value().push_back("SHERPA_LDADD=Sherpa_iPlugin");
55 #endif
56 }
57
58 ATH_MSG_INFO("Sherpa initialising...");
59
60 #ifdef IS_SHERPA_3
61 for (auto& inputfile : m_inputfiles) {
62 // remove first line and last character containing '"'
63 // TODO fix Python/C++ string passing, to not contain " in first place
64 inputfile.second.erase(0, inputfile.second.find("\n") + 1);
65 inputfile.second.pop_back();
66 }
67 #endif
68
69 ATH_MSG_DEBUG("... compiling plugin code");
70 if (m_plugincode != "") {
72 #ifdef IS_SHERPA_3
73 m_inputfiles["Base.yaml"] += "SHERPA_LDADD: Sherpa_iPlugin \n";
74 #else
75 m_params.value().push_back("SHERPA_LDADD=Sherpa_iPlugin");
76 #endif
77 }
78
79 ATH_MSG_DEBUG("... seeding Athena random number generator");
80 p_rndEngine = getRandomEngineDuringInitialize("SHERPA", m_randomSeed, m_dsid); // NOT THREAD-SAFE
81
82 #ifdef IS_SHERPA_3
83 ATH_MSG_DEBUG("... adapting output level");
84 if( msg().level()==MSG::FATAL || msg().level()==MSG::ERROR || msg().level()==MSG::WARNING ){
85 m_inputfiles["Base.yaml"] += "\nEVT_OUTPUT: 0 \n";
86 }
87 else if(msg().level()==MSG::INFO){
88 m_inputfiles["Base.yaml"] += "\nEVT_OUTPUT: 2 \n";
89 }
90 else if(msg().level()==MSG::DEBUG){
91 m_inputfiles["Base.yaml"] += "\nEVT_OUTPUT: 15 \n";
92 }
93 else{
94 m_inputfiles["Base.yaml"] += "\nEVT_OUTPUT: 15 \n";
95 }
96
97 ATH_MSG_DEBUG("... writing input files to directory");
98 for (auto& inputfile : m_inputfiles) {
99 // write input content to file in working directory
100 FILE *file = fopen(inputfile.first.c_str(),"w");
101 if (!file) {
102 ATH_MSG_ERROR("Cannot open " << inputfile.first);
103 return StatusCode::FAILURE;
104 }
105 fputs(inputfile.second.c_str(),file);
106 fclose(file);
107 ATH_MSG_INFO("Sherpa_i using the following settings in "+inputfile.first);
108 ATH_MSG_INFO("\n"+inputfile.second+"\n");
109 }
110
111 ATH_MSG_DEBUG("... go Sherpa!");
112 int argc = 2;
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);
119 delete [] argv;
120 #else
121 p_sherpa = new SHERPA::Sherpa();
122 #endif
123
124
125 #ifdef IS_SHERPA_3
126
127 try {
128 p_sherpa->InitializeTheRun();
129 p_sherpa->InitializeTheEventHandler();
130 }
131 catch (const ATOOLS::normal_exit& exception) {
132 ATH_MSG_ERROR("Normal exit caught, this probably means:");
133 ATH_MSG_ERROR("Have to compile Amegic libraries");
134 ATH_MSG_ERROR("You probably want to run ./makelibs");
135 return StatusCode::FAILURE;
136 }
137 catch (const ATOOLS::Exception& exception) {
138 ATH_MSG_ERROR("Unwanted ATOOLS::exception caught.");
139 ATH_MSG_ERROR(exception);
140 return StatusCode::FAILURE;
141 }
142 #else
143
144 try {
145 int argc;
146 char** argv;
147 getParameters(argc, argv);
148 p_sherpa->InitializeTheRun(argc,(char **)argv);
149 delete [] argv;
150
151 p_sherpa->InitializeTheEventHandler();
152 }
153 catch (const ATOOLS::Exception& exception) {
154 if (exception.Class()=="Matrix_Element_Handler" && exception.Type()==ATOOLS::ex::normal_exit) {
155 ATH_MSG_ERROR("Have to compile Amegic libraries");
156 ATH_MSG_ERROR("You probably want to run ./makelibs");
157 }
158 else {
159 ATH_MSG_ERROR("Unwanted ATOOLS::exception caught.");
160 ATH_MSG_ERROR(exception);
161 }
162 return StatusCode::FAILURE;
163 }
164 #endif
165 catch (const std::exception& exception) {
166 ATH_MSG_ERROR("std::exception caught.");
167 return StatusCode::FAILURE;
168 }
169
170 #ifdef HEPMC3
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")
177 };
178 m_runinfo->tools().push_back(std::move(generator));
179 #endif
180 return StatusCode::SUCCESS;
181}
182
183
185 ATH_MSG_DEBUG("Sherpa_i in callGenerator()");
186 //Re-seed the random number stream
187 long seeds[7];
188 const EventContext& ctx = Gaudi::Hive::currentContext();
189 ATHRNG::calculateSeedsMC21(seeds, "SHERPA", ctx.eventID().event_number(), m_dsid, m_randomSeed);
190 p_rndEngine->setSeeds(seeds, 0); // NOT THREAD-SAFE
191
192 do {
193 ATH_MSG_DEBUG("Trying to generate event with Sherpa");
194 } while (p_sherpa->GenerateOneEvent()==false);
195
196 const size_t genEvents = ATOOLS::rpa->gen.NumberOfGeneratedEvents();
197 if (genEvents%1000==0) {
198 ATH_MSG_INFO("Passed "<<genEvents<<" events.");
199 }
200
201 return StatusCode::SUCCESS;
202}
203
204StatusCode Sherpa_i::fillEvt(HepMC::GenEvent* event) {
205 ATH_MSG_DEBUG( "Sherpa_i Filling event");
206#ifdef HEPMC3
207 if (!event->run_info()) event->set_run_info(m_runinfo);
208#endif
209 p_sherpa->FillHepMCEvent(*event);
210
211
212#ifdef HEPMC3
213//Weight, MEWeight, WeightNormalisation, NTrials
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;
221 if (std::abs(event->weight(name)) > m_variation_weight_cap*std::abs(nominal)) {
222 ATH_MSG_INFO("Capping variation" << name << " = " << event->weight(name)/nominal << "*nominal");
223 event->weight(name) *= m_variation_weight_cap*std::abs(nominal)/std::abs(event->weight(name));
224 }
225 ATH_MSG_DEBUG("Sherpa WEIGHT " << name << " value="<< event->weight(name));
226 }
227 }
228#else
229 if (event->weights().size()>2) {
230 for (size_t i=0; i<event->weights().size(); ++i) {
231 if (i>3) { // cap variation weights
232 // cap variation weights at m_variation_weight_cap*nominal to avoid spikes from numerical instability
233 if (std::abs(event->weights()[i]) > m_variation_weight_cap*std::abs(event->weights()[0])) {
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]);
236 }
237 }
238 ATH_MSG_DEBUG("Sherpa WEIGHT " << i << " value="<< event->weights()[i]);
239 }
240 }
241#endif
242
243#ifdef HEPMC3
244 event->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
245#else
246 GeVToMeV(event); //unit check
247#endif
248
249
250 return StatusCode::SUCCESS;
251}
252
254
255 ATH_MSG_INFO("Sherpa_i finalize()");
256
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;
259
260 std::cout << "MetaData: PDF = " << p_sherpa->PDFInfo() << std::endl;
261
262 std::cout << "Named variations initialised by Sherpa:" << std::endl;
263 std::cout << *p_sherpa->GetInitHandler()->GetVariations() << std::endl;
264
265 p_sherpa->SummarizeRun();
266
267 if (m_cleanup) {
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");
270 }
271
272 return StatusCode::SUCCESS;
273}
274
275
276#ifndef IS_SHERPA_3
277void Sherpa_i::getParameters(int &argc, char** &argv) {
278 std::vector<std::string> params;
279
280 // set some ATLAS specific default values as a starting point
281 params.push_back("EXTERNAL_RNG=Atlas_RNG");
282
283 /***
284 Adopt Atlas Debug Level Scheme
285 ***/
286
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");
291 }
292 else if(log.level()==MSG::INFO){
293 params.push_back("OUTPUT=2");
294 }
295 else if(log.level()==MSG::DEBUG){
296 params.push_back("OUTPUT=3");
297 }
298 else{
299 params.push_back("OUTPUT=15");
300 }
301
302 // disregard manual RUNDATA setting if run card given in JO
303 if (m_runcard != "") m_params.value().push_back("RUNDATA=Run.dat");
304
305 // allow to overwrite all parameters from JO file
306 params.insert(params.begin()+params.size(), m_params.begin(), m_params.end());
307
308 // create Run.dat file if runcard explicitely given
309 if (m_runcard != "") {
310 FILE *file = fopen("Run.dat","w");
311 if (!file) {
312 ATH_MSG_ERROR("Cannot open Run.dat");
313 }
314 else {
315 fputs(m_runcard.value().c_str(),file);
316 fclose(file);
317 }
318 }
319
320 /***
321 Convert into Sherpas argc/argv arguments
322 ***/
323 argc = 1+params.size();
324 argv = new char * [ 1+params.size() ];
325 argv[0] = new char[7];
326 strcpy(argv[0], "Sherpa");
327
328 ATH_MSG_INFO("Sherpa_i using the following Arguments");
330 for(size_t i=0; i<params.size(); i++) {
331 ATH_MSG_INFO(" [ " << params[i] << " ] ");
332 argv[i+1] = new char[params[i].size()+1];
333 strcpy(argv[i+1], params[i].c_str());
334 }
335 ATH_MSG_INFO("End Sherpa_i Argument List");
336 ATH_MSG_INFO("Further Sherpa initialisation output will be redirected to the LOG_FILE specified above.");
337
338}
339#endif
340
341void Sherpa_i::compilePlugin(const std::string& pluginCode) {
342 // TODO: not very pretty, should we eventually do this in Python instead (base fragment)
343 FILE *file = fopen("Sherpa_iPlugin.C","w");
344 if (!file) {
345 ATH_MSG_ERROR("Cannot open Sherpa_iPlugin.C");
346 }
347 else {
348 fputs(pluginCode.c_str(),file);
349 fclose(file);
350 }
351 std::string command;
352 // Python -> C++ string conversion seems to add quote character as first
353 // and last line if the string contains quotes (like always in a plugin)
354 // thus removing them here
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) {
363 ATH_MSG_ERROR("Error compiling plugin library.");
364 }
365}
366
370using namespace ATOOLS;
371
372Atlas_RNG::Atlas_RNG(CLHEP::HepRandomEngine* engine) :
373 External_RNG(), p_engine(engine), m_filename("Config.conf")
374{
375}
376
378
380
381 return CLHEP::RandFlat::shoot(p_engine);
382
383}
384
385const std::string Atlas_RNG::GenerateUID() const {
386 std::string result{""};
387 const int nMax = 26;
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];
394 }
395 return result;
396}
397
399 // We set the file name first time the worker calls SaveStatus
400 std::call_once(m_once_flag_atlas_rng, [&](){
401 struct stat info;
402 if ( !stat("/dev/shm", &info)) {
403 m_filename = "/dev/shm/Config.conf.";
404 }
406 std::cout << "RNG state being saved to: " << m_filename << std::endl;
407 });
408 p_engine->saveStatus(m_filename.c_str());
409}
410
411void Atlas_RNG::RestoreStatus() { p_engine->restoreStatus(m_filename.c_str()); }
412
413// some getter magic to make this random number generator available to sherpa
414// DECLARE_GETTER doesn't compile with c++20 in Sherpa versions before 3.
415#ifdef IS_SHERPA_3
416DECLARE_GETTER(Atlas_RNG,"Atlas_RNG",External_RNG,RNG_Key);
417#else
418namespace ATOOLS {
419template <> class Getter<External_RNG,RNG_Key,Atlas_RNG,std::less<std::string>>:
420 public Getter_Function<External_RNG,RNG_Key,std::less<std::string>> {
421private:
423protected:
424 void PrintInfo(std::ostream &str,const size_t width) const;
425 Object_Type *operator()(const Parameter_Type &parameters) const;
426public:
427 Getter(const bool &d=true):
428 Getter_Function<External_RNG,RNG_Key,std::less<std::string>>("Atlas_RNG")
429 { SetDisplay(d); }
430};
431}
432ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>
433ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::s_initializer(true);
434#endif
435
436External_RNG *ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::operator()(const RNG_Key &) const
437{ return new Atlas_RNG(p_rndEngine); }
438
439void ATOOLS::Getter<External_RNG,RNG_Key,Atlas_RNG>::PrintInfo(std::ostream &str,const size_t) const
440{ str<<"Atlas RNG interface"; }
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
CLHEP::HepRandomEngine * p_rndEngine
Definition Sherpa_i.cxx:37
const double width
Object_Type * operator()(const Parameter_Type &parameters) const
void PrintInfo(std::ostream &str, const size_t width) const
static Getter< External_RNG, RNG_Key, Atlas_RNG, std::less< std::string > > s_initializer
Definition Sherpa_i.cxx:422
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Atlas_RNG(CLHEP::HepRandomEngine *)
Definition Sherpa_i.cxx:372
void SaveStatus()
Definition Sherpa_i.cxx:398
CLHEP::HepRandomEngine * p_engine
Definition Sherpa_i.h:78
double Get()
Definition Sherpa_i.cxx:379
void RestoreStatus()
Definition Sherpa_i.cxx:411
std::string m_filename
Definition Sherpa_i.h:79
std::once_flag m_once_flag_atlas_rng
Definition Sherpa_i.h:80
const std::string GenerateUID() const
Definition Sherpa_i.cxx:385
void GeVToMeV(HepMC::GenEvent *evt)
Scale event energies/momenta by x 1000.
Definition GenBase.cxx:58
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenModule.cxx:14
CLHEP::HepRandomEngine * getRandomEngineDuringInitialize(const std::string &streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun=1, unsigned int lbn=1) const
Definition GenModule.cxx:53
IntegerProperty m_randomSeed
Seed for random number engine.
Definition GenModule.h:84
StatusCode genFinalize()
For finalising the generator, if required.
Definition Sherpa_i.cxx:253
StringProperty m_runcard
FIXME unused?
Definition Sherpa_i.h:42
StatusCode genInitialize()
For initializing the generator, if required.
Definition Sherpa_i.cxx:50
SHERPA::Sherpa * p_sherpa
Definition Sherpa_i.h:32
StringProperty m_plugincode
Optional code for plugin library to compile and load at run time.
Definition Sherpa_i.h:61
StringArrayProperty m_params
List of additional Sherpa parameters beyond run card snippet (from JO file)
Definition Sherpa_i.h:45
BooleanProperty m_cleanup
Definition Sherpa_i.h:67
IntegerProperty m_dsid
Definition Sherpa_i.h:70
void compilePlugin(const std::string &)
Definition Sherpa_i.cxx:341
Sherpa_i(const std::string &name, ISvcLocator *pSvcLocator)
Definition Sherpa_i.cxx:39
StatusCode callGenerator()
For calling the generator on each iteration of the event loop.
Definition Sherpa_i.cxx:184
DoubleProperty m_variation_weight_cap
Variation weight cap factor.
Definition Sherpa_i.h:64
DoubleProperty m_xsscale
Definition Sherpa_i.h:66
StatusCode fillEvt(HepMC::GenEvent *evt)
For filling the HepMC event object.
Definition Sherpa_i.cxx:204
void getParameters(int &argc, char **&argv)
Definition Sherpa_i.cxx:277
STL class.
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.
STL namespace.
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.
MsgStream & msg
Definition testRead.cxx:32
TFile * file