ATLAS Offline Software
Loading...
Searching...
No Matches
GenModule.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GaudiKernel/ISvcLocator.h"
7#include "GaudiKernel/DataSvc.h"
8#include "GaudiKernel/Incident.h"
10#include "CLHEP/Random/RandomEngine.h"
11#include <fstream>
12
13
14GenModule::GenModule(const std::string& name, ISvcLocator* pSvcLocator)
15 : GenBase(name, pSvcLocator)
16{
17 m_mkMcEvent = true;
18}
19
20
22 // Base class initializations
24 // Get the random number service
25 CHECK(m_rndmSvc.retrieve());
26 // Get the incident service
27 CHECK(m_incidentSvc.retrieve());
30 return StatusCode::SUCCESS;
31}
32
33
34CLHEP::HepRandomEngine* GenModule::getRandomEngine(const std::string& streamName,
35 const EventContext& ctx) const
36{
37 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, streamName);
38 std::string rngName = name()+streamName;
39 rngWrapper->setSeed( rngName, ctx );
40 return rngWrapper->getEngine(ctx);
41}
42
43
44CLHEP::HepRandomEngine* GenModule::getRandomEngine(const std::string& streamName, unsigned long int randomSeedOffset,
45 const EventContext& ctx) const
46{
47 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, streamName);
48 rngWrapper->setSeed( streamName, ctx.slot(), randomSeedOffset, ctx.eventID().run_number() );
49 return rngWrapper->getEngine(ctx);
50}
51
52
53CLHEP::HepRandomEngine* GenModule::getRandomEngineDuringInitialize(const std::string& streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun, unsigned int lbn) const
54{
55 const size_t slot=0;
56 EventContext ctx;
57 ctx.setSlot( slot );
58 ctx.setEventID (EventIDBase (conditionsRun,
59 EventIDBase::UNDEFEVT, // event
60 EventIDBase::UNDEFNUM, // timestamp
61 EventIDBase::UNDEFNUM, // timestamp ns
62 lbn));
64 Atlas::ExtendedEventContext( evtStore()->hiveProxyDict(),
65 conditionsRun) );
66 return getRandomEngine(streamName, randomSeedOffset, ctx);
67}
68
69
70StatusCode GenModule::execute() {
71 // Examples of how to retrieve the random number engine for a given
72 // stream.
73 // NB getRandomEngine should only be called once per event for a
74 // given stream, as it causes the stream to be re-seeded each time
75 // it is called.
76
77 // Example 1 - seeded based on the current event number (+ slot, run, streamName)
78 //const EventContext& ctx = Gaudi::Hive::currentContext();
79 //CLHEP::HepRandomEngine* rndmEngine = this->getRandomEngine("MyStream", ctx);
80
81 // Example 2 - seeded based on the m_randomSeed property (+ slot, run, streamName)
82 //const EventContext& ctx = Gaudi::Hive::currentContext();
83 //CLHEP::HepRandomEngine* rndmEngine = this->getRandomEngine("MyStream", m_randomSeed.value(), ctx);
84
85 // Call the code that generates an event
86 CHECK(this->callGenerator());
87
88 // Create the MC event and send the GeneratorEvent stored in it to fillEvt
89 HepMC::GenEvent* evt = HepMC::newGenEvent(1,1);
90 CHECK(this->fillEvt(evt));
92
93 // Add the event to the MC event collection
94 if (events()) {
95 // If this is an "afterburner" generator, replace the last event rather than add a new one
97 if (m_isAfterburner.value() || name() == "Tauola" || name() == "Photos") {
98 events()->pop_back();
99 }
100 // Add the event to the end of the collection
101 events()->push_back(evt);
102 ATH_MSG_DEBUG("MC event added to McEventCollection");
103
104 // remove the empty event in case of ParticleDecayer
105 if (name() == "ParticleDecayer") {
106 events()->pop_back();
107 }
108 }
109
110 // Call the incident service to notify that an event has been made
111 m_incidentSvc->fireIncident( Incident(name(), "McEventGenerated") );
112 return StatusCode::SUCCESS;
113}
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:169
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:134
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
BooleanProperty m_mkMcEvent
Flag to determine if a new MC event collection should be made if it doesn't exist.
Definition GenBase.h:139
ServiceHandle< IIncidentSvc > m_incidentSvc
Handle on the incident service.
Definition GenModule.h:101
virtual StatusCode callGenerator()
For calling the generator on each iteration of the event loop.
Definition GenModule.h:66
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenModule.cxx:14
virtual StatusCode fillEvt(HepMC::GenEvent *evt)=0
For filling the HepMC event object.
StatusCode initialize()
Definition GenModule.cxx:21
ServiceHandle< IAthRNGSvc > m_rndmSvc
Data members.
Definition GenModule.h:99
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
Definition GenModule.cxx:34
StatusCode execute()
Definition GenModule.cxx:70
CLHEP::HepRandomEngine * getRandomEngineDuringInitialize(const std::string &streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun=1, unsigned int lbn=1) const
Definition GenModule.cxx:53
virtual StatusCode genuserInitialize()
For initialization of user code, if required. Called after genInitialize.
Definition GenModule.h:64
virtual StatusCode genInitialize()
For initializing the generator, if required.
Definition GenModule.h:62
BooleanProperty m_isAfterburner
Flag for normal vs. afterburner generators.
Definition GenModule.h:87
void setExtendedEventContext(EventContext &ctx, ExtendedEventContext &&ectx)
Move an extended context into a context object.
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:628
GenEvent * newGenEvent(const int a, const int b)
Definition GenEvent.h:626