ATLAS Offline Software
Loading...
Searching...
No Matches
Pythia8_i.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
8
10#include <ranges>
11#include <charconv>
12
13// calls to fortran routines
16
17#include <sstream>
18// For limits
19#include <limits>
20
21
22namespace {
23 // Name of random number stream
24 std::string s_pythia_stream{"PYTHIA8_INIT"};
25}
26
27
28// fix Pythia8 shower weights change in conventions
29#define PYTHIA8_NWEIGHTS nWeights
30#define PYTHIA8_WEIGHT weight
31#define PYTHIA8_WLABEL weightLabel
32#define PYTHIA8_CONVERSION 1.0
33
34#ifdef PYTHIA_VERSION_INTEGER
35 #undef PYTHIA8_NWEIGHTS
36 #undef PYTHIA8_WEIGHT
37 #undef PYTHIA8_WLABEL
38 #if PYTHIA_VERSION_INTEGER > 8303
39 #define PYTHIA8_NWEIGHTS nWeightGroups
40 #else
41 #define PYTHIA8_NWEIGHTS nVariationGroups
42 #endif
43 #define PYTHIA8_WEIGHT getGroupWeight
44 #define PYTHIA8_WLABEL getGroupName
45#endif
46
50
51namespace {
52
53std::string py8version()
54{
55 static const std::string incdir (PY8INCLUDE_DIR);
56 std::string::size_type pos = incdir.find ("/pythia8/");
57 if (pos == std::string::npos) return "";
58 pos += 9;
59 std::string::size_type pos2 = incdir.find ("/", pos);
60 if (pos2 == std::string::npos) pos2 = incdir.size();
61 return incdir.substr (pos, pos2-pos);
62}
63
64
65}
66
68Pythia8_i::Pythia8_i(const std::string &name, ISvcLocator *pSvcLocator)
69: GenModule(name, pSvcLocator)
70{
71 m_particleIDs["PROTON"] = PROTON;
72 m_particleIDs["ANTIPROTON"] = ANTIPROTON;
73 m_particleIDs["ELECTRON"] = ELECTRON;
74 m_particleIDs["POSITRON"] = POSITRON;
75 m_particleIDs["NEUTRON"] = NEUTRON;
76 m_particleIDs["ANTINEUTRON"] = ANTINEUTRON;
77 m_particleIDs["MUON"] = MUON;
78 m_particleIDs["ANTIMUON"] = ANTIMUON;
79 m_particleIDs["LEAD"] = LEAD;
80 m_particleIDs["OXYGEN"] = OXYGEN;
81 m_particleIDs["HELIUM"] = HELIUM;
82
83}
84
87
88 #ifndef PYTHIA8_3SERIES
89// if(m_userHookPtr != 0) delete m_userHookPtr;
90
91// for(UserHooksPtrType ptr: m_userHooksPtrs){
92// delete ptr;
93// }
94 #endif
95}
96
99
100 ATH_MSG_DEBUG("Pythia8_i from genInitialize()");
101
102 ATH_MSG_INFO("XML Path is " + xmlpath());
103 m_pythia = std::make_unique<Pythia8::Pythia> (xmlpath());
104 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
106 struct HepMC3::GenRunInfo::ToolInfo generator={std::string("Pythia8"),py8version(),std::string("Used generator")};
107 m_runinfo->tools().push_back(std::move(generator));
108
109 bool canInit = true;
110
111 m_version = m_pythia->settings.parm("Pythia:versionNumber");
112
113 s_pythia_stream = "PYTHIA8_INIT";
114
115 // By default add "nominal" to the list of shower weight names
117 m_showerWeightNames.insert(m_showerWeightNames.begin(), "nominal");
118
119 // We do explicitly set tune 4C, since it is the starting point for many other tunes
120 // Tune 4C for pp collisions
121 m_pythia->readString("Tune:pp = 5");
122
123 // also use CTEQ6L1 from LHAPDF 6 by default
124 // can be over-written using JO
125 m_pythia->readString("PDF:pSet= LHAPDF6:cteq6l1");
126
127 // switch off verbose event print out
128 m_pythia->readString("Next:numberShowEvent = 0");
129
130 // Add flag to switch off from JO the Pythia8ToHepMC::print_inconsistency internal variable
131 m_pythia->settings.addFlag("AthenaPythia8ToHepMC:print_inconsistency",true);
132
133 // Revert the recoil strategy to the old default of 1, ie. 'recoil to color'
134 // In 8.314, the default option was changed to 0, 'recoil to top' and we
135 // revert it back unless 'recoil to top'
136 // is explicitly needed for the samples
137 if (m_version > 8.313) m_pythia->readString("TimeShower:recoilStrategyRF = 1");
138
139 // Add UserHooks first because these potentially add new settings that must exist prior to parsing commands
140
141 for(const auto &hook: m_userHooks){
142 ATH_MSG_INFO("Adding user hook " + hook + ".");
143 bool canSetHook = true;
144 if (hook == "SuppressSmallPT") {
145 m_SuppressSmallPT = new Pythia8::SuppressSmallPT(m_pt0timesMPI,m_numberAlphaS,m_sameAlphaSAsMPI);
146 canSetHook=m_pythia->setUserHooksPtr(PYTHIA8_PTRWRAP(m_SuppressSmallPT));
147}
148 else {
150 canSetHook = m_pythia->addUserHooksPtr(m_userHooksPtrs.back());
151}
152 if(!canSetHook){
153 ATH_MSG_ERROR("Unable to set requested user hook.");
154 ATH_MSG_ERROR("Pythia 8 initialisation will FAIL!");
155 canInit=false;
156 }
157 }
158
159 for(const std::pair<const std::string, double> &param : Pythia8_UserHooks::UserHooksFactory::userSettings<double>()){
160 m_pythia->settings.addParm(param.first, param.second, false, false, 0., 0.);
161 }
162
163 for(const std::pair<const std::string, int> &param : Pythia8_UserHooks::UserHooksFactory::userSettings<int>()){
164 m_pythia->settings.addMode(param.first, param.second, false, false, 0., 0.);
165 }
166
167 for(const std::pair<const std::string, bool> &param : Pythia8_UserHooks::UserHooksFactory::userSettings<bool>()){
168 m_pythia->settings.addFlag(param.first, param.second);
169 }
170
171 for(const std::pair<const std::string, std::string> &param : Pythia8_UserHooks::UserHooksFactory::userSettings<std::string>()){
172 m_pythia->settings.addWord(param.first, param.second);
173 }
174
175 if( ! m_athenaTool.empty() ){
176 if(m_athenaTool.retrieve().isFailure()){
177 ATH_MSG_ERROR("Unable to retrieve Athena Tool for custom Pythia processing");
178 return StatusCode::FAILURE;
179 }
180 else {
181 StatusCode status = m_athenaTool->InitializePythiaInfo(*m_pythia);
182 if(status != StatusCode::SUCCESS) return status;
183 }
184 }
185
186 // Now apply the settings from the JO
187 for(const std::string &cmd : m_commands){
188
189 if(cmd.compare("")==0) continue;
190 else if (cmd.find("Beams:id") != std::string::npos ) {
191 ATH_MSG_ERROR("With command '" << cmd << "' you are trying to set a beam different from p: please use the Beam1/Beam2 properties instead:");
192 ATH_MSG_ERROR(" example: genSeq.Pythia8.Beam1 = 'LEAD'");
193 ATH_MSG_ERROR(" genSeq.Pythia8.Beam2 = 'ANTINEUTRON'");
194 return StatusCode::FAILURE;
195 }
196 else if (cmd.find("Beams:frameType") != std::string::npos ) {
198 ATH_MSG_WARNING(" Found an explicit 'Beams:frameType' command: this will override transform beams momenta/energy parameters, regardless of its requested value. ");
199 }
200
201 bool read = m_pythia->readString(cmd);
202
203 if(!read){
204 ATH_MSG_ERROR("Pythia could not understand the command '"<< cmd<<"'");
205 return StatusCode::FAILURE;
206 }
207 }
208
209 PDGID beam1 = m_particleIDs[m_beam1];
210 PDGID beam2 = m_particleIDs[m_beam2];
211
212 ATH_MSG_INFO("Beam1 = "<<beam1);
213 ATH_MSG_INFO("Beam2 = "<<beam2);
214
215 if(beam1 == 0 || beam2 == 0){
216 ATH_MSG_ERROR("Invalid beam particle!");
217 return StatusCode::FAILURE;
218 }
219
220
221
222 if(m_useRndmGenSvc){
223
224 ATH_MSG_INFO(" !!!!!!!!!!!! WARNING ON PYTHIA RANDOM NUMBERS !!!!!!!!!! ");
225 ATH_MSG_INFO(" THE ATHENA SERVICE AthRNGenSvc IS USED.");
226 ATH_MSG_INFO(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
227
228 m_atlasRndmEngine = std::make_shared<customRndm>();
229 CLHEP::HepRandomEngine* rndmEngine = getRandomEngineDuringInitialize(s_pythia_stream, m_randomSeed, m_dsid); // NOT THREAD-SAFE
230 m_atlasRndmEngine->init(rndmEngine);
231#if PYTHIA_VERSION_INTEGER >= 8310
232 m_pythia->setRndmEnginePtr(m_atlasRndmEngine);
233#else
234 m_pythia->setRndmEnginePtr(m_atlasRndmEngine.get());
235#endif
236 s_pythia_stream = "PYTHIA8";
237 }else{
238 ATH_MSG_INFO(" !!!!!!!!!!!! WARNING ON PYTHIA RANDOM NUMBERS !!!!!!!!!! ");
239 ATH_MSG_INFO(" THE STANDARD PYTHIA8 RANDOM NUMBER SERVICE IS USED.");
240 ATH_MSG_INFO(" THE ATHENA SERVICE AthRNGSvc IS ***NOT*** USED.");
241 ATH_MSG_INFO(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
242 }
243
244 if(m_userProcess != ""){
246 }
247
248
249
250 if(m_userResonances.value() != ""){
251
252 std::vector<std::string> resonanceArgs;
253
254 for (auto&& part : std::views::split(m_userResonances.value(), ':')) resonanceArgs.emplace_back(part.begin(), part.end());
255 if(resonanceArgs.size() != 2){
256 ATH_MSG_ERROR("Cannot Understand UserResonance job option!");
257 ATH_MSG_ERROR("You should specify it as a 'name:id1,id2,id3...'");
258 ATH_MSG_ERROR("Where name is the name of your UserResonance, and id1,id2,id3 are a comma separated list of PDG IDs to which it is applied");
259 canInit = false;
260 }
261 std::vector<std::string> resonanceIds;
262 for (auto&& part : std::views::split(resonanceArgs.back(), ',')) resonanceIds.emplace_back(part.begin(), part.end());
263 if(resonanceIds.size()==0){
264 ATH_MSG_ERROR("You did not specifiy any PDG ids to which your user resonance width should be applied!");
265 ATH_MSG_ERROR("You should specify a list as 'name:id1,id2,id3...'");
266 ATH_MSG_ERROR("Where name is the name of your UserResonance, and id1,id2,id3 are a comma separated list of PDG IDs to which it is applied");
267 canInit=false;
268 }
269
270 for(std::vector<std::string>::const_iterator sId = resonanceIds.begin();
271 sId != resonanceIds.end(); ++sId){
272 int idResIn = 0;
273 auto result = std::from_chars(sId->data(), sId->data() + sId->size(), idResIn);
274 if (result.ec != std::errc()) {
275 ATH_MSG_ERROR("Invalid resonance ID: " + *sId);
276 }
277 m_userResonancePtrs.push_back(Pythia8_UserResonance::UserResonanceFactory::create(resonanceArgs.front(), idResIn));
278 }
279
280 for(std::shared_ptr<Pythia8::ResonanceWidths>& resonance : m_userResonancePtrs) {
281#if PYTHIA_VERSION_INTEGER >= 8310
282 m_pythia->setResonancePtr(resonance);
283#else
284 m_pythia->setResonancePtr(resonance.get());
285#endif
286 }
287
288 }
289
290 if(m_particleDataFile != "") {
291 if(!m_pythia->particleData.reInit(m_particleDataFile, true)){
292 ATH_MSG_ERROR("Unable to read requested particle data table: " + m_particleDataFile + " !!");
293 ATH_MSG_ERROR("Pythia 8 initialisation will FAIL!");
294 canInit = false;
295 }
296 }
297
298 if(m_lheFile != ""){
299 if(m_procPtr){
300 ATH_MSG_ERROR("Both LHE file and user process have been specified");
301 ATH_MSG_ERROR("LHE input does not make sense with a user process!");
302 canInit = false;
303 }
304
305 canInit = canInit && m_pythia->readString("Beams:frameType = 4");
306 canInit = canInit && m_pythia->readString("Beams:LHEF = " + m_lheFile);
307 if(!canInit){
308 ATH_MSG_ERROR("Unable to read requested LHE file: " + m_lheFile + " !");
309 ATH_MSG_ERROR("Pythia 8 initialisation will FAIL!");
310 }
311 }else{
313 canInit = canInit && m_pythia->readString("Beams:frameType = 1");
314 canInit = canInit && m_pythia->readString("Beams:eCM = " + std::to_string(m_collisionEnergy));
315 }
316 canInit = canInit && m_pythia->readString("Beams:idA = " + std::to_string(beam1));
317 canInit = canInit && m_pythia->readString("Beams:idB = " + std::to_string(beam2));
318 }
319
320 if(m_procPtr){
321#if PYTHIA_VERSION_INTEGER >= 8310
322 if(!m_pythia->setSigmaPtr(m_procPtr))
323#else
324 if(!m_pythia->setSigmaPtr(m_procPtr.get()))
325#endif
326 {
327 ATH_MSG_ERROR("Unable to set requested user process: " + m_userProcess + " !!");
328 ATH_MSG_ERROR("Pythia 8 initialisation will FAIL!");
329 canInit = false;
330 }
331 }
332
333 StatusCode returnCode = StatusCode::SUCCESS;
334
335 m_pythia->particleData.listXML(m_outputParticleDataFile.value().substr(0,m_outputParticleDataFile.value().find("xml"))+"orig.xml");
336 m_pythia->settings.writeFile("Settings_before.log",true);
337
338 if(canInit){
339 canInit = m_pythia->init();
340 }
341
342 if(!canInit){
343 returnCode = StatusCode::FAILURE;
344 ATH_MSG_ERROR(" *** Unable to initialise Pythia !! ***");
345 }
346
347 m_pythia->particleData.listXML(m_outputParticleDataFile);
348 m_pythia->settings.writeFile("Settings_after.log",true);
349
350 //counter for event failures;
351 m_failureCount = 0;
352
354
355 // Set set_print_inconsistency to Athena corresponding flag (allowing to change it from JO)
356 m_pythiaToHepMC.set_print_inconsistency( m_pythia->settings.flag("AthenaPythia8ToHepMC:print_inconsistency") );
357
358 m_pythiaToHepMC.set_store_pdf(true);
359 m_pythiaToHepMC.set_store_weights(false);
360
361 return returnCode;
362}
363
366
367 ATH_MSG_DEBUG(">>> Pythia8_i from callGenerator");
369 //Re-seed the random number stream
370 long seeds[7];
371 const EventContext& ctx = Gaudi::Hive::currentContext();
372 ATHRNG::calculateSeedsMC21(seeds, s_pythia_stream, ctx.eventID().event_number(), m_dsid, m_randomSeed);
373 m_atlasRndmEngine->getEngine()->setSeeds(seeds, 0); // NOT THREAD-SAFE
374 m_seeds.clear();
375 m_seeds.push_back(seeds[0]);
376 m_seeds.push_back(seeds[1]);
377 }
378
379 bool status = m_pythia->next();
380
381 StatusCode returnCode = StatusCode::SUCCESS;
382
383 if(!status){
386 ATH_MSG_ERROR("Exceeded the max number of consecutive event failures.");
387 returnCode = StatusCode::FAILURE;
388 }else{
389 ATH_MSG_INFO("Event generation failed - re-trying.");
390 returnCode = this->callGenerator();
391 }
392 }
393
394 ATH_MSG_DEBUG("Now checking with Tool.empty() second time");
395 if( ! m_athenaTool.empty() ){
396 StatusCode stat = m_athenaTool->ModifyPythiaEvent(*m_pythia);
397 if(stat != StatusCode::SUCCESS) returnCode = stat;
398 }
399
400 m_failureCount = 0;
401
402 m_nAccepted += 1.;
403 // some CKKWL merged events have zero weight (or unfilled event).
404 // start again with such events
405
406 double eventWeight = m_pythia->info.mergingWeight()*m_pythia->info.weight();
407
408
409 if(returnCode != StatusCode::FAILURE &&
410 // Here this is a double, but it may be converted into float at some point downstream
411 (std::abs(eventWeight) < std::numeric_limits<float>::min() ||
412 m_pythia->event.size() < 2)){
413
414 returnCode = this->callGenerator();
415 } else if ( std::abs(eventWeight) < std::numeric_limits<float>::min() &&
416 std::abs(eventWeight) > std::numeric_limits<double>::min() ){
417 ATH_MSG_WARNING("Found event weight " << eventWeight << " between the float and double precision limits. Rejecting event.");
418 } else {
419 m_nMerged += eventWeight;
421
422 // For FxFx cross section:
423 m_sigmaTotal+=m_pythia->info.weight();
424 }
425
426 return returnCode;
427}
428
431
432 ATH_MSG_DEBUG(">>> Pythia8_i from fillEvt");
433
434 evt->set_event_number(m_internal_event_number);
435
436 // if using "getGroupWeight" and | lhastrategy | = 4, then need to convert mb to pb ( done otherwise when calling info.weight(), [...] )
437 if( m_internal_event_number == 1 && std::abs(m_pythia->info.lhaStrategy()) == 4 ) {
438 m_conversion = ( (double) PYTHIA8_CONVERSION);
439 ATH_MSG_DEBUG(" LHA strategy needs a conversion to fix Pythia8 shower weights bug(s) equal to " << m_conversion);
440 }
441
442
443 if(m_pythia->event.size() < 2){
444 ATH_MSG_ERROR("Something wrong with this event - it contains fewer than 2 particles!");
445 ATH_MSG_ERROR("internal event number is "<<m_internal_event_number);
446 return StatusCode::FAILURE;
447 }
448
449 m_pythiaToHepMC.fill_next_event(*m_pythia, evt, m_internal_event_number);
450
451 // In case we're asked, save the LHE event as an attribute.
452 if(m_saveLHE){
453 auto evtlhe = std::make_shared<HepMC::GenEvent>();
454 m_pythiaToHepMC.fill_next_event(m_pythia->process, evtlhe.get(), m_internal_event_number, &m_pythia->info, &m_pythia->settings);
455 auto extra = std::make_shared<HepMC::ShortEventAttribute>(evtlhe.get());
456 evt->add_attribute(HepMCStr::LHERecord, extra);
457 }
458
459 // in debug mode you can check whether the pdf information is stored
460 if(evt->pdf_info()){
461 ATH_MSG_DEBUG("PDFinfo id1:" << evt->pdf_info()->parton_id[0]);
462 ATH_MSG_DEBUG("PDFinfo id2:" << evt->pdf_info()->parton_id[1]);
463 ATH_MSG_DEBUG("PDFinfo x1:" << evt->pdf_info()->x[0]);
464 ATH_MSG_DEBUG("PDFinfo x2:" << evt->pdf_info()->x[1]);
465 ATH_MSG_DEBUG("PDFinfo scalePDF:" << evt->pdf_info()->scale);
466 ATH_MSG_DEBUG("PDFinfo pdf1:" << evt->pdf_info()->pdf_id[0]);
467 ATH_MSG_DEBUG("PDFinfo pdf2:" << evt->pdf_info()->pdf_id[1]);
468 }
469 else
470 ATH_MSG_DEBUG("No PDF information available in HepMC::GenEvent!");
471
472 // set the randomseeds
474
475 // fill weights
477
478 return StatusCode::SUCCESS;
479}
480
483
484 // reset
485 m_mergingWeight = m_pythia->info.mergingWeightNLO(); // first initialisation, good for the nominal weight in any case (see Merging:includeWeightInXsection).
486 m_enhanceWeight = 1.0; // better to keep the enhancement from UserHooks in a separate variable, to keep the code clear
487
488#ifndef PYTHIA8_304SERIES
489 // include Enhance userhook weight
490 for(const auto &hook: m_userHooksPtrs) {
491 if (hook->canEnhanceEmission()) {
492 m_enhanceWeight *= hook->getEnhancedEventWeight();
493 }
494 }
495#endif // not PYTHIA8_304SERIES
496
497 // DO NOT try to distinguish between phase space and merging contributions: only their product contains both, so always multiply them in order to be robust
498 double eventWeight = m_pythia->info.weight() * m_mergingWeight * m_enhanceWeight;
499
500 std::map<std::string,double> fWeights; // temporary weight vector, needed while managing hepmc2 and 3 simultaneously
501
502 // save as Default the usual pythia8 weight, including enhancements
503 // NB: info.weight() is built upon info.eventWeightLHEF (but includes shower "decorations"), not from the rwgt LHE vector.
504 size_t atlas_specific_weights = 1;
505 fWeights["Default"]=eventWeight;
506 if(m_internal_event_number == 1) {
507 ATH_MSG_INFO("found LHEF version "<< m_pythia->info.LHEFversion() );
508 if (m_pythia->info.LHEFversion() > 1) m_doLHE3Weights = true;
509 m_weightIDs.clear();
510 m_weightIDs.push_back("Default"); // notice that we are assigning a weightname = weightID
511 }
512
513 // Now systematic weights: a) generator and b) shower variations
514 // merging implies reading from lhe files (while the opposite is not true, ok, but still works)
515 if (m_lheFile!="") {
516 // remember that the true merging component can be disentangled only when there are LHE input events
517
518 // make sure to get the merging weight correctly: this is needed -only- for systematic weights (generator and parton shower variations)
519 // NB: most robust way, regardless of the specific value of Merging:includeWeightInXsection (if on, info.mergingWeight() is always 1.0 ! )
520 m_mergingWeight *= ( m_pythia->info.weight() / m_pythia->info.eventWeightLHEF ); // this is the actual merging component, regardless of conventions,
521 // needed by systematic variation weights
522 // this includes rare compensation or selection-biased weights
523 }
524
525
526 ATH_MSG_DEBUG("Event weights: enhancing weight, merging weight, total weight = "<<m_enhanceWeight<<", "<<m_mergingWeight<<", "<<eventWeight);
527
528 // we already filled the "Default" weight
529 std::vector<std::string>::const_iterator id = m_weightIDs.begin()+atlas_specific_weights;
530
531 // a) fill generator weights
532 if(m_pythia->info.getWeightsDetailedSize() != 0){
533 for(std::map<std::string, Pythia8::LHAwgt>::const_iterator wgt = m_pythia->info.rwgt->wgts.begin();
534 wgt != m_pythia->info.rwgt->wgts.end(); ++wgt){
535
537 m_weightIDs.push_back(wgt->first);
538 }else{
539 if(*id != wgt->first){
540 ATH_MSG_ERROR("Mismatch in LHE3 weight id. Found "<<wgt->first<<", expected "<<*id);
541 return StatusCode::FAILURE;
542 }
543 ++id;
544 }
545
546 std::map<std::string, Pythia8::LHAweight>::const_iterator weightName = m_pythia->info.init_weights->find(wgt->first);
547 if(weightName != m_pythia->info.init_weights->end()){
548 fWeights[weightName->second.contents] = m_mergingWeight * m_enhanceWeight * wgt->second.contents;
549 }else{
550 // if no name was assigned/found, assign a weightname = weightid
551 fWeights[wgt->first] = m_mergingWeight * m_enhanceWeight * wgt->second.contents;
552 }
553
554 }
555 }
556
557 // b) shower weight variations
558 // always start from second shower weight: the first was saved already as "Default"
559
560 for(int iw = 1; iw < m_pythia->info.PYTHIA8_NWEIGHTS(); ++iw){
561
562 std::string wtName = ((int)m_showerWeightNames.size() == m_pythia->info.PYTHIA8_NWEIGHTS())? m_showerWeightNames[iw]: "ShowerWt_" +std::to_string(iw);
563
564 fWeights[wtName] = m_mergingWeight * m_enhanceWeight * m_pythia->info.PYTHIA8_WEIGHT(iw)*m_conversion;
565
566 if(m_internal_event_number == 1) {
567 m_weightIDs.push_back(wtName);
568 ATH_MSG_DEBUG("Shower weight name, value, conversion: "<<wtName<<", "<< fWeights[wtName] <<","<<m_conversion );
569 }
570 }
571
572 // Now save, as a backup, also the LHEF weight without enhancements (useful for possible, rare, non-default cases)
573 // to make clear that it should not be used in analyses, save it always with a -10 factor (so that its goal is clear even if not checking its name)
574 if (m_lheFile!="")
575 {
576 fWeights["EXTRA_bare_LHE_weight"]=(-10.0)*m_pythia->info.eventWeightLHEF;
577 if(m_internal_event_number == 1) m_weightIDs.push_back("EXTRA_bare_LHE_weight");
578 }
579
580 // Sad, but needed: create a string vector with acceptable order of weight names
582 m_weightNames.clear();
583 for(const std::string &id : m_weightIDs){
584 if(m_doLHE3Weights) {
585 std::map<std::string, Pythia8::LHAweight>::const_iterator weight = m_pythia->info.init_weights->find(id);
586 if(weight != m_pythia->info.init_weights->end()) m_weightNames.push_back(weight->second.contents);
587 else m_weightNames.push_back(id);
588 }
589 else {
590 m_weightNames.push_back(id);
591 }
592 }
593 if (m_weightNames.size() != fWeights.size() ) {
594 ATH_MSG_ERROR("Something wrong when building list of weight names: " << m_weightNames.size() << " vs "<< fWeights.size() << ", exiting ...");
595 return StatusCode::FAILURE;
596 }
597 }
598
599 // The following depends on the specific hepmc2/3 implementation
600 if (!evt->run_info()) {
601 evt->set_run_info(m_runinfo);
602 }
603 evt->run_info()->set_weight_names(m_weightNames);
604
605 // for the first event, weight EXTRA_bare_LHE_weight is not present in evt->weights(), so we need to book a place for it by hand
606 if (m_internal_event_number == 1 && evt->run_info()->weight_names().size() == evt->weights().size()+1 ) {
607 evt->weights().push_back(1.0);
608 }
609
610 // added conversion GeV -> MeV to ensure correct units
611 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
612
613 evt->weights().resize(fWeights.size(), 1.0);
614 for (const auto & w: fWeights) {
615 evt->weight(w.first)=w.second;
616 }
617
618 auto beams=evt->beams();
619 ATH_MSG_DEBUG( " Energy of the beams " << beams[0]->momentum().e() );
620
621//uncomment to list HepMC events
622// std::cout << " print::listing Pythia8 " << std::endl;
623// HepMC3::Print::listing(std::cout, *evt);
624
625
626 return StatusCode::SUCCESS;
627}
628
631
632 ATH_MSG_INFO(">>> Pythia8_i from genFinalize");
633 m_pythia->stat();
634
635 Pythia8::Info info = m_pythia->info;
636 double xs = info.sigmaGen(); // in mb
637
639 const double accfactor = m_nMerged / info.nAccepted();
640 ATH_MSG_DEBUG("Multiplying cross-section by CKKWL merging acceptance of "<<m_nMerged <<"/" <<info.nAccepted() << " = " << accfactor
641 << ": " << xs << " -> " << xs*accfactor);
642 xs *= accfactor;
643 }
644
645 if(m_doFxFxXS){
646 ATH_MSG_DEBUG("Using FxFx cross section recipe: xs = "<< m_sigmaTotal << " / " << 1e9*info.nTried());
647 xs = m_sigmaTotal / (1e9*info.nTried());
648 std::cout << "Using FxFx cross section recipe: xs = "<< m_sigmaTotal << " / " << 1e9*info.nTried() << std::endl;
649 }
650
651 if( ! m_athenaTool.empty()){
652 double xsmod = m_athenaTool->CrossSectionScaleFactor();
653 ATH_MSG_DEBUG("Multiplying cross-section by Pythia Modifier tool factor " << xsmod );
654 xs *= xsmod;
655 }
656
657 xs *= 1000. * 1000.;//convert to nb
658
659 std::cout << "MetaData: cross-section (nb)= " << xs <<std::endl;
660 std::cout << "MetaData: generator= Pythia 8." << std::string(py8version()) <<std::endl;
661
662 if(m_doLHE3Weights || m_weightIDs.size()>1 ){
663 std::cout<<"MetaData: weights = ";
664 for (const auto& w : m_weightNames ) std::cout<< w <<" | ";
665 std::cout<<std::endl;
666 }
667
669 if (info.nTried()>0) ATH_MSG_INFO("Pythia8 efficiency (nAccepted/nTried %) = " << info.nAccepted()*100./info.nTried());
670 else ATH_MSG_INFO("Pythia8 efficiency cannot be computed, nTried <=0");
671 }
672
674 ATH_MSG_INFO("Number of random numbers produced " << m_atlasRndmEngine->getRNCalls());
675 if (m_useReseed) ATH_MSG_INFO("Each event was reseeded ");
676 else ATH_MSG_INFO("Events were not reseeded ");
677}
678
679 return StatusCode::SUCCESS;
680}
681
684 return m_version;
685}
686
688const std::string& Pythia8_i::pythia_stream() {
689 return s_pythia_stream;
690}
691
693std::string Pythia8_i::xmlpath(){
694
695
696 std::string foundpath = "";
697
698// Try to find the xmldoc directory using PathResolver:
699 foundpath = PathResolverFindCalibDirectory( "Pythia8/xmldoc" );
700
701 return foundpath;
702}
703
705
706// fix Pythia8 shower weights change in conventions
707#ifdef PYTHIA8_NWEIGHTS
708 #undef PYTHIA8_NWEIGHTS
709 #undef PYTHIA8_WEIGHT
710 #undef PYTHIA8_WLABEL
711 #undef PYTHIA8_CONVERSION
712#endif
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::string PathResolverFindCalibDirectory(const std::string &logical_file_name)
#define PYTHIA8_CONVERSION
Definition Pythia8_i.cxx:32
#define PYTHIA8_PTRWRAP(A)
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
std::shared_ptr< HepMC3::GenRunInfo > m_runinfo
The run info for HepMC3.
Definition GenModule.h:90
static std::map< std::string, T > & userSettings()
static UserHooks * create(const std::string &hookName)
static std::shared_ptr< Sigma2Process > create(const std::string &procName)
static std::shared_ptr< ResonanceWidths > create(const std::string &name, int pdgid)
Call this with the name of the ResonanceWidth and PDG ID to which it will be applied e....
bool m_override_transform_beamenergy
Definition Pythia8_i.h:131
virtual StatusCode genInitialize()
For initializing the generator, if required.
Definition Pythia8_i.cxx:98
IntegerProperty m_dsid
Definition Pythia8_i.h:97
BooleanProperty m_sameAlphaSAsMPI
Definition Pythia8_i.h:102
virtual StatusCode fillWeights(HepMC::GenEvent *evt)
virtual StatusCode fillEvt(HepMC::GenEvent *evt)
For filling the HepMC event object.
StringProperty m_userProcess
Definition Pythia8_i.h:149
BooleanProperty m_useReseed
Definition Pythia8_i.h:95
static std::string xmlpath()
DoubleProperty m_numberAlphaS
Definition Pythia8_i.h:101
Pythia8::SuppressSmallPT * m_SuppressSmallPT
Definition Pythia8_i.h:180
double m_nAccepted
Definition Pythia8_i.h:138
unsigned int m_failureCount
Definition Pythia8_i.h:143
bool m_doLHE3Weights
Definition Pythia8_i.h:169
DoubleProperty m_collisionEnergy
Definition Pythia8_i.h:126
StringProperty m_lheFile
Definition Pythia8_i.h:133
StringProperty m_beam1
Definition Pythia8_i.h:129
BooleanProperty m_computeEfficiency
Definition Pythia8_i.h:137
std::vector< std::shared_ptr< Pythia8::ResonanceWidths > > m_userResonancePtrs
Definition Pythia8_i.h:158
std::shared_ptr< Pythia8::Sigma2Process > m_procPtr
Definition Pythia8_i.h:152
double m_sigmaTotal
Definition Pythia8_i.h:140
BooleanProperty m_saveLHE
Definition Pythia8_i.h:176
HepMC::Pythia8ToHepMC m_pythiaToHepMC
Definition Pythia8_i.h:89
double m_nMerged
Definition Pythia8_i.h:139
PublicToolHandle< IPythia8Custom > m_athenaTool
Definition Pythia8_i.h:174
StringProperty m_userResonances
Definition Pythia8_i.h:156
virtual StatusCode callGenerator()
For calling the generator on each iteration of the event loop.
BooleanProperty m_doFxFxXS
Definition Pythia8_i.h:136
std::vector< long int > m_seeds
Definition Pythia8_i.h:147
std::vector< std::string > m_showerWeightNames
Definition Pythia8_i.h:171
std::vector< std::string > m_weightIDs
Definition Pythia8_i.h:167
DoubleProperty m_pt0timesMPI
Definition Pythia8_i.h:100
std::vector< std::string > m_weightNames
Definition Pythia8_i.h:168
int m_internal_event_number
Definition Pythia8_i.h:108
static const std::string & pythia_stream()
std::shared_ptr< customRndm > m_atlasRndmEngine
Definition Pythia8_i.h:93
double pythiaVersion() const
StringArrayProperty m_userHooks
Definition Pythia8_i.h:98
StringProperty m_beam2
Definition Pythia8_i.h:130
virtual StatusCode genFinalize()
For finalising the generator, if required.
std::unique_ptr< Pythia8::Pythia > m_pythia
Definition Pythia8_i.h:88
double m_enhanceWeight
Definition Pythia8_i.h:166
double m_version
Definition Pythia8_i.h:110
double m_conversion
Definition Pythia8_i.h:141
std::map< std::string, PDGID > m_particleIDs
Definition Pythia8_i.h:145
BooleanProperty m_doCKKWLAcceptance
Definition Pythia8_i.h:135
double m_mergingWeight
Definition Pythia8_i.h:165
UnsignedIntegerProperty m_maxFailures
Definition Pythia8_i.h:90
BooleanProperty m_useRndmGenSvc
Definition Pythia8_i.h:92
StringProperty m_particleDataFile
Definition Pythia8_i.h:162
std::vector< UserHooksPtrType > m_userHooksPtrs
Definition Pythia8_i.h:154
StringArrayProperty m_showerWeightNamesProp
Definition Pythia8_i.h:172
StringProperty m_outputParticleDataFile
Definition Pythia8_i.h:163
StringArrayProperty m_commands
Definition Pythia8_i.h:116
Pythia8_i(const std::string &name, ISvcLocator *pSvcLocator)
Definition Pythia8_i.cxx:68
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.
const std::string LHERecord
void set_random_states(GenEvent *e, std::vector< long int > &a)
Definition GenEvent.h:588
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)