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/ToolHandle.h"
15 #include "GaudiKernel/IChronoStatSvc.h"
17 #include "StoreGate/StoreGateSvc.h"
18 
20 
21 // ISF includes
23 #include "ISF_Event/ISFParticle.h"
24 #include "ISF_Event/SimSvcID.h"
25 
26 namespace ISF {
27 
28  class IParticleBroker;
29  class ITruthSvc;
30 
40  class BaseSimulationSvc : public extends<AthService, ISimulationSvc> {
41  public:
42 
43  //** Constructor with parameters */
44  BaseSimulationSvc( const std::string& name, ISvcLocator* pSvcLocator):
45  base_class(name,pSvcLocator) {};
46 
48  virtual ~BaseSimulationSvc() {};
49 
51  virtual StatusCode sysInitialize() override
52  {
53  if ( AthService::sysInitialize().isFailure() ) {
54  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot initialize AthService! Abort.");
55  return StatusCode::FAILURE;
56  }
57  if ( m_chrono.retrieve().isFailure()){
58  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot retrieve ChronoStatSvc! Abort.");
59  return StatusCode::FAILURE;
60  }
61 
62  return StatusCode::SUCCESS;
63  }
64 
66  const std::string& simSvcDescriptor() override { return m_simDescr.value(); }
67 
69  virtual StatusCode setupEvent() override
70  { return StatusCode::SUCCESS; }
71 
73  virtual StatusCode releaseEvent() override
74  { return StatusCode::SUCCESS; }
75 
77  virtual StatusCode setParticleBroker( IParticleBroker *broker) override {
78  m_particleBroker = broker;
79  return StatusCode::SUCCESS;
80  }
81 
83  virtual StatusCode simulateVector(const ISFParticleVector& particles, McEventCollection* mcEventCollection, McEventCollection *) override {
84  // this implementation is a wrapper in case the simulator does
85  // implement particle-vector input
86  bool success = true;
87  // simulate each particle individually
89  ATH_MSG_VERBOSE( m_screenOutputPrefix << "Starting simulation of particle: " << part );
90  if ( this->simulate(*part, mcEventCollection).isFailure()) {
91  ATH_MSG_WARNING("Simulation of particle failed!" << endmsg <<
92  " -> simulator: " << this->simSvcDescriptor() <<
93  " -> particle : " << *part );
94  success = false;
95  }
96  }
97  return ( success ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
98  }
99 
101  virtual StatusCode simulate(ISFParticle& isp, McEventCollection* mcEventCollection) override;
102 
104  const ChronoEntity* chronoStart(const IChronoSvc::ChronoTag& tag ) {
105  if (m_chrono) return m_chrono->chronoStart( tag);
106  return nullptr;
107  }
108 
110  const ChronoEntity* chronoStop(const IChronoSvc::ChronoTag& tag ) {
111  if (m_chrono) return m_chrono->chronoStop( tag);
112  return nullptr;
113  }
114 
120 
126 
128  template <class T> StatusCode retrieveTool(ToolHandle<T>& thandle){
129  if (!thandle.empty() && thandle.retrieve().isFailure()){
130  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandle << ". Abort.");
131  return StatusCode::FAILURE;
132  } else
133  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandle);
134  return StatusCode::SUCCESS;
135  }
136 
138  template <class T>
139  StatusCode retrieveTools(ToolHandleArray<T>& thandleArray){
140  if (!thandleArray.empty() && thandleArray.retrieve().isFailure()){
141  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandleArray << ". Abort.");
142  return StatusCode::FAILURE;
143  } else
144  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandleArray);
145  return StatusCode::SUCCESS;
146  }
147 
149  template<class T>
150  StatusCode recordCollection( T*& coll, const std::string& collName) const{
151  // create if necessary
152  coll = coll ? coll : new T;
153  // record
154  if (evtStore()->record( coll, collName).isFailure()){
155  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot record collection " << collName << ". Abort." );
156  return StatusCode::FAILURE;
157  } else
158  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully recorded collection " << collName);
159  return StatusCode::SUCCESS;
160  }
161 
163  template<class T>
164  StatusCode retrieveCollection(T*& coll, const std::string& collName, bool forceBreak=true) const {
165  // check for existence in the soft case
166  if (!forceBreak && !evtStore()->contains<T>(collName)) {
167  coll = 0;
168  ATH_MSG_DEBUG(m_screenOutputPrefix << "Collection does not exists (not required). Ignore.");
169  return StatusCode::SUCCESS;
170  }
171  if ( evtStore()->retrieve(coll, collName).isFailure()){
172  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retireve collection " << collName << ". Abort." );
173  return StatusCode::FAILURE;
174  } else
175  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved collection " << collName);
176  return StatusCode::SUCCESS;
177  }
178 
179  private:
182 
184  ServiceHandle<StoreGateSvc> m_evtStore{this, "EvtStore", "StoreGateSvc/StoreGateSvc",
185  "Handle to a StoreGateSvc instance: it will be used to retrieve data during the course of the job"};
186 
188  ServiceHandle<StoreGateSvc> m_detStore{this, "DetStore", "StoreGateSvc/DetectorStore",
189  "Handle to a StoreGateSvc/DetectorStore instance: it will be used to retrieve data during the course of the job"};
190 
191 
192  protected:
194  Gaudi::Property<std::string> m_simDescr{this, "Identifier", {},
195  "A unique string to identify the simulator."};
196 
198  Gaudi::Property<std::string> m_screenOutputPrefix{this, "ScreenOutputPrefix", "isf >> ",
199  "Prefix for log output"};
200 
202  ServiceHandle<IChronoStatSvc> m_chrono{this, "ChronoStatService", "ChronoStatSvc"};
203 
206 
207  };
208 
209 
212  {
213  return StatusCode::SUCCESS;
214  }
215 }
216 
217 
218 #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:119
ISF::BaseSimulationSvc::m_simDescr
Gaudi::Property< std::string > m_simDescr
The simulator service descriptor.
Definition: BaseSimulationSvc.h:194
ISF::BaseSimulationSvc::m_chrono
ServiceHandle< IChronoStatSvc > m_chrono
The timing service for general usage.
Definition: BaseSimulationSvc.h:202
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:69
ISF::BaseSimulationSvc::sysInitialize
virtual StatusCode sysInitialize() override
Gaudi sysInitialize() methods.
Definition: BaseSimulationSvc.h:51
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:150
ISF::BaseSimulationSvc::detStore
ServiceHandle< StoreGateSvc > & detStore()
Definition: BaseSimulationSvc.h:125
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:73
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:211
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:164
ISFParticle.h
ISF::BaseSimulationSvc::simulateVector
virtual StatusCode simulateVector(const ISFParticleVector &particles, McEventCollection *mcEventCollection, McEventCollection *) override
Simulation call for vectors of particles.
Definition: BaseSimulationSvc.h:83
McEventCollection.h
ISF::BaseSimulationSvc::setParticleBroker
virtual StatusCode setParticleBroker(IParticleBroker *broker) override
Inform the SimulationSvc about the ParticleBroker svc.
Definition: BaseSimulationSvc.h:77
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:118
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:198
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
ISF::BaseSimulationSvc::chronoStop
const ChronoEntity * chronoStop(const IChronoSvc::ChronoTag &tag)
wrapper call to stop chrono with given tag
Definition: BaseSimulationSvc.h:110
ISF::BaseSimulationSvc::m_detStore
ServiceHandle< StoreGateSvc > m_detStore
Handle to StoreGate (detector store by default)
Definition: BaseSimulationSvc.h:188
ISF::BaseSimulationSvc::chronoStart
const ChronoEntity * chronoStart(const IChronoSvc::ChronoTag &tag)
wrapper call to start chrono with given tag
Definition: BaseSimulationSvc.h:104
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ISF::BaseSimulationSvc::simSvcDescriptor
const std::string & simSvcDescriptor() override
Return the simulation service descriptor.
Definition: BaseSimulationSvc.h:66
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:48
ISF::BaseSimulationSvc::m_evtStore
ServiceHandle< StoreGateSvc > m_evtStore
Handle to StoreGate (event store by default)
Definition: BaseSimulationSvc.h:184
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:128
ISF::BaseSimulationSvc
Definition: BaseSimulationSvc.h:40
ISF::BaseSimulationSvc::BaseSimulationSvc
BaseSimulationSvc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: BaseSimulationSvc.h:44
IOVSvcDefs.h
defines and typedefs for IOVSvc
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
ISF::BaseSimulationSvc::retrieveTools
StatusCode retrieveTools(ToolHandleArray< T > &thandleArray)
templated Tool retrieval - gives unique handling & look and feel
Definition: BaseSimulationSvc.h:139
ISF::BaseSimulationSvc::m_particleBroker
IParticleBroker * m_particleBroker
The particle service used to push particles into the simulation.
Definition: BaseSimulationSvc.h:205
ISF::BaseSimulationSvc::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: BaseSimulationSvc.h:124
StoreGateSvc.h
SimSvcID.h
ServiceHandle< StoreGateSvc >