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