ATLAS Offline Software
BaseSimulationSvc.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ISF_BASESIMULATIONSVC_H
6 #define ISF_BASESIMULATIONSVC_H 1
7 
8 // STL includes
9 #include <string>
10 
11 // FrameWork includes
13 #include "GaudiKernel/ServiceHandle.h"
14 #include <GaudiKernel/StatusCode.h>
15 #include "GaudiKernel/ToolHandle.h"
16 #include "GaudiKernel/IChronoStatSvc.h"
18 #include "StoreGate/StoreGateSvc.h"
19 
21 
22 // ISF includes
24 #include "ISF_Event/ISFParticle.h"
25 #include "ISF_Event/SimSvcID.h"
26 
27 namespace ISF {
28 
29  class IParticleBroker;
30  class ITruthSvc;
31 
41  class BaseSimulationSvc : public extends<AthService, ISimulationSvc> {
42  public:
43 
44  //** Constructor with parameters */
45  BaseSimulationSvc( const std::string& name, ISvcLocator* pSvcLocator):
46  base_class(name,pSvcLocator) {};
47 
49  virtual ~BaseSimulationSvc() {};
50 
52  virtual StatusCode sysInitialize() override
53  {
54  if ( AthService::sysInitialize().isFailure() ) {
55  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot initialize AthService! Abort.");
56  return StatusCode::FAILURE;
57  }
58  if ( m_chrono.retrieve().isFailure()){
59  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot retrieve ChronoStatSvc! Abort.");
60  return StatusCode::FAILURE;
61  }
62 
63  return StatusCode::SUCCESS;
64  }
65 
67  const std::string& simSvcDescriptor() override { return m_simDescr.value(); }
68 
70  virtual StatusCode setupEvent() override
71  { return StatusCode::SUCCESS; }
72 
74  virtual StatusCode releaseEvent() override
75  { return StatusCode::SUCCESS; }
76 
78  virtual StatusCode setParticleBroker( IParticleBroker *broker) override {
79  m_particleBroker = broker;
80  return StatusCode::SUCCESS;
81  }
82 
84  virtual StatusCode simulateVector(const ISFParticleVector& particles, McEventCollection* mcEventCollection, McEventCollection *) override {
85  // this implementation is a wrapper in case the simulator does
86  // implement particle-vector input
87  StatusCode sc = StatusCode::SUCCESS;
88  // simulate each particle individually
90  ATH_MSG_VERBOSE( m_screenOutputPrefix << "Starting simulation of particle: " << part );
91  if ( this->simulate(*part, mcEventCollection).isFailure()) {
92  ATH_MSG_WARNING("Simulation of particle failed!" << endmsg <<
93  " -> simulator: " << this->simSvcDescriptor() <<
94  " -> particle : " << *part );
95  sc = StatusCode::FAILURE;
96  }
97  }
98  return sc;
99  }
100 
102  virtual StatusCode simulate(ISFParticle& isp, McEventCollection* mcEventCollection) override;
103 
105  const ChronoEntity* chronoStart(const IChronoSvc::ChronoTag& tag ) {
106  if (m_chrono) return m_chrono->chronoStart( tag);
107  return nullptr;
108  }
109 
111  const ChronoEntity* chronoStop(const IChronoSvc::ChronoTag& tag ) {
112  if (m_chrono) return m_chrono->chronoStop( tag);
113  return nullptr;
114  }
115 
121 
127 
129  template <class T> StatusCode retrieveTool(ToolHandle<T>& thandle){
130  if (!thandle.empty() && thandle.retrieve().isFailure()){
131  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandle << ". Abort.");
132  return StatusCode::FAILURE;
133  } else
134  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandle);
135  return StatusCode::SUCCESS;
136  }
137 
139  template <class T>
140  StatusCode retrieveTools(ToolHandleArray<T>& thandleArray){
141  if (!thandleArray.empty() && thandleArray.retrieve().isFailure()){
142  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandleArray << ". Abort.");
143  return StatusCode::FAILURE;
144  } else
145  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandleArray);
146  return StatusCode::SUCCESS;
147  }
148 
150  template<class T>
151  StatusCode recordCollection( T*& coll, const std::string& collName) const{
152  // create if necessary
153  coll = coll ? coll : new T;
154  // record
155  if (evtStore()->record( coll, collName).isFailure()){
156  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot record collection " << collName << ". Abort." );
157  return StatusCode::FAILURE;
158  } else
159  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully recorded collection " << collName);
160  return StatusCode::SUCCESS;
161  }
162 
164  template<class T>
165  StatusCode retrieveCollection(T*& coll, const std::string& collName, bool forceBreak=true) const {
166  // check for existence in the soft case
167  if (!forceBreak && !evtStore()->contains<T>(collName)) {
168  coll = 0;
169  ATH_MSG_DEBUG(m_screenOutputPrefix << "Collection does not exists (not required). Ignore.");
170  return StatusCode::SUCCESS;
171  }
172  if ( evtStore()->retrieve(coll, collName).isFailure()){
173  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retireve collection " << collName << ". Abort." );
174  return StatusCode::FAILURE;
175  } else
176  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved collection " << collName);
177  return StatusCode::SUCCESS;
178  }
179 
180  private:
183 
185  ServiceHandle<StoreGateSvc> m_evtStore{this, "EvtStore", "StoreGateSvc/StoreGateSvc",
186  "Handle to a StoreGateSvc instance: it will be used to retrieve data during the course of the job"};
187 
189  ServiceHandle<StoreGateSvc> m_detStore{this, "DetStore", "StoreGateSvc/DetectorStore",
190  "Handle to a StoreGateSvc/DetectorStore instance: it will be used to retrieve data during the course of the job"};
191 
192 
193  protected:
195  Gaudi::Property<std::string> m_simDescr{this, "Identifier", {},
196  "A unique string to identify the simulator."};
197 
199  Gaudi::Property<std::string> m_screenOutputPrefix{this, "ScreenOutputPrefix", "isf >> ",
200  "Prefix for log output"};
201 
203  ServiceHandle<IChronoStatSvc> m_chrono{this, "ChronoStatService", "ChronoStatSvc"};
204 
207 
208  };
209 
210 
213  {
214  return StatusCode::SUCCESS;
215  }
216 }
217 
218 
219 #endif //> !ISF_BASESIMULATIONSVC_H
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
ISF::BaseSimulationSvc::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
Definition: BaseSimulationSvc.h:120
ISF::BaseSimulationSvc::m_simDescr
Gaudi::Property< std::string > m_simDescr
The simulator service descriptor.
Definition: BaseSimulationSvc.h:195
ISF::BaseSimulationSvc::m_chrono
ServiceHandle< IChronoStatSvc > m_chrono
The timing service for general usage.
Definition: BaseSimulationSvc.h:203
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ISF::BaseSimulationSvc::setupEvent
virtual StatusCode setupEvent() override
Setup Event chain - in case of a begin-of event action is needed.
Definition: BaseSimulationSvc.h:70
ISF::BaseSimulationSvc::sysInitialize
virtual StatusCode sysInitialize() override
Gaudi sysInitialize() methods.
Definition: BaseSimulationSvc.h:52
ISF::BaseSimulationSvc::BaseSimulationSvc
BaseSimulationSvc()
Default constructor.
ISF::BaseSimulationSvc::recordCollection
StatusCode recordCollection(T *&coll, const std::string &collName) const
templated record collection method, will create a new one if not existing
Definition: BaseSimulationSvc.h:151
ISF::BaseSimulationSvc::detStore
ServiceHandle< StoreGateSvc > & detStore()
Definition: BaseSimulationSvc.h:126
ISF::ISFParticle
Definition: ISFParticle.h:42
ISF::BaseSimulationSvc::releaseEvent
virtual StatusCode releaseEvent() override
Release Event chain - in case of an end-of event action is needed.
Definition: BaseSimulationSvc.h:74
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
ISF::BaseSimulationSvc::simulate
virtual StatusCode simulate(ISFParticle &isp, McEventCollection *mcEventCollection) override
Simulation call for individual particles.
Definition: BaseSimulationSvc.h:212
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
ISF::IParticleBroker
@ class IParticleBroker
Definition: IParticleBroker.h:39
ISF::BaseSimulationSvc::retrieveCollection
StatusCode retrieveCollection(T *&coll, const std::string &collName, bool forceBreak=true) const
templated retrieve collection method, boolean steers that force break
Definition: BaseSimulationSvc.h:165
ISFParticle.h
ISF::BaseSimulationSvc::simulateVector
virtual StatusCode simulateVector(const ISFParticleVector &particles, McEventCollection *mcEventCollection, McEventCollection *) override
Simulation call for vectors of particles.
Definition: BaseSimulationSvc.h:84
McEventCollection.h
ISF::BaseSimulationSvc::setParticleBroker
virtual StatusCode setParticleBroker(IParticleBroker *broker) override
Inform the SimulationSvc about the ParticleBroker svc.
Definition: BaseSimulationSvc.h:78
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ISF::BaseSimulationSvc::evtStore
const ServiceHandle< StoreGateSvc > & evtStore() const
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: BaseSimulationSvc.h:119
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ISF::ISFParticleVector
std::vector< ISF::ISFParticle * > ISFParticleVector
ISFParticle vector.
Definition: ISFParticleContainer.h:26
ISF::BaseSimulationSvc::m_screenOutputPrefix
Gaudi::Property< std::string > m_screenOutputPrefix
Screen output refinement.
Definition: BaseSimulationSvc.h:199
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:32
ISF::BaseSimulationSvc::chronoStop
const ChronoEntity * chronoStop(const IChronoSvc::ChronoTag &tag)
wrapper call to stop chrono with given tag
Definition: BaseSimulationSvc.h:111
ISF::BaseSimulationSvc::m_detStore
ServiceHandle< StoreGateSvc > m_detStore
Handle to StoreGate (detector store by default)
Definition: BaseSimulationSvc.h:189
ISF::BaseSimulationSvc::chronoStart
const ChronoEntity * chronoStart(const IChronoSvc::ChronoTag &tag)
wrapper call to start chrono with given tag
Definition: BaseSimulationSvc.h:105
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ISF::BaseSimulationSvc::simSvcDescriptor
const std::string & simSvcDescriptor() override
Return the simulation service descriptor.
Definition: BaseSimulationSvc.h:67
ISF
ISFParticleOrderedQueue.
Definition: PrimaryParticleInformation.h:13
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ISF::BaseSimulationSvc::~BaseSimulationSvc
virtual ~BaseSimulationSvc()
Destructor.
Definition: BaseSimulationSvc.h:49
ISF::BaseSimulationSvc::m_evtStore
ServiceHandle< StoreGateSvc > m_evtStore
Handle to StoreGate (event store by default)
Definition: BaseSimulationSvc.h:185
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
ISimulationSvc.h
AthService.h
ISF::BaseSimulationSvc::retrieveTool
StatusCode retrieveTool(ToolHandle< T > &thandle)
templated Tool retrieval - gives unique handling & look and feel
Definition: BaseSimulationSvc.h:129
ISF::BaseSimulationSvc
Definition: BaseSimulationSvc.h:41
ISF::BaseSimulationSvc::BaseSimulationSvc
BaseSimulationSvc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: BaseSimulationSvc.h:45
IOVSvcDefs.h
defines and typedefs for IOVSvc
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:23
ISF::BaseSimulationSvc::retrieveTools
StatusCode retrieveTools(ToolHandleArray< T > &thandleArray)
templated Tool retrieval - gives unique handling & look and feel
Definition: BaseSimulationSvc.h:140
ISF::BaseSimulationSvc::m_particleBroker
IParticleBroker * m_particleBroker
The particle service used to push particles into the simulation.
Definition: BaseSimulationSvc.h:206
ISF::BaseSimulationSvc::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: BaseSimulationSvc.h:125
StoreGateSvc.h
SimSvcID.h
ServiceHandle< StoreGateSvc >