ATLAS Offline Software
BaseSimulationSvc.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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  m_evtStore( "StoreGateSvc/StoreGateSvc", name ),
47  m_detStore( "StoreGateSvc/DetectorStore", name )
48  {
49  // to be set to a unique descriptor
50  declareProperty( "Identifier",
51  m_simDescr,
52  "A unique string to identify the simulator.");
53 
54  declareProperty( "EvtStore",
55  m_evtStore = StoreGateSvc_t ("StoreGateSvc", name),
56  "Handle to a StoreGateSvc instance: it will be used to retrieve data during the course of the job" );
57  declareProperty( "DetStore",
58  m_detStore = StoreGateSvc_t ("StoreGateSvc/DetectorStore", name),
59  "Handle to a StoreGateSvc/DetectorStore instance: it will be used to retrieve data during the course of the job" );
60  // refine the screen output for debugging
61  declareProperty("ScreenOutputPrefix", m_screenOutputPrefix);
62  // Service handling
63  declareProperty("ChronoStatService", m_chronoSvcName );
64 
65  };
66 
68  virtual ~BaseSimulationSvc() {};
69 
71  virtual StatusCode sysInitialize() override
72  {
73  if ( AthService::sysInitialize().isFailure() ) {
74  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot initialize AthService! Abort.");
75  return StatusCode::FAILURE;
76  }
77  if ( serviceLocator()->service(m_chronoSvcName, m_chrono ).isFailure()){
78  ATH_MSG_FATAL( m_screenOutputPrefix << " Cannot retrieve ChronoStatSvc! Abort.");
79  return StatusCode::FAILURE;
80  }
81 
82  return StatusCode::SUCCESS;
83  }
84 
86  std::string& simSvcDescriptor() override { return m_simDescr; }
87 
89  virtual StatusCode setupEvent() override
90  { return StatusCode::SUCCESS; }
91 
93  virtual StatusCode releaseEvent() override
94  { return StatusCode::SUCCESS; }
95 
97  virtual StatusCode setParticleBroker( IParticleBroker *broker) override {
98  m_particleBroker = broker;
99  return StatusCode::SUCCESS;
100  }
101 
104  // this implementation is a wrapper in case the simulator does
105  // implement particle-vector input
106  bool success = true;
107  // simulate each particle individually
108  for (ISF::ISFParticle* part : particles) {
109  ATH_MSG_VERBOSE( m_screenOutputPrefix << "Starting simulation of particle: " << part );
110  if ( this->simulate(*part, mcEventCollection).isFailure()) {
111  ATH_MSG_WARNING("Simulation of particle failed!" << endmsg <<
112  " -> simulator: " << this->simSvcDescriptor() <<
113  " -> particle : " << *part );
114  success = false;
115  }
116  }
117  return ( success ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
118  }
119 
121  virtual StatusCode simulate(ISFParticle& isp, McEventCollection* mcEventCollection) override;
122 
124  const ChronoEntity* chronoStart(const IChronoSvc::ChronoTag& tag ) {
125  if (m_chrono) return m_chrono->chronoStart( tag);
126  return nullptr;
127  }
128 
130  const ChronoEntity* chronoStop(const IChronoSvc::ChronoTag& tag ) {
131  if (m_chrono) return m_chrono->chronoStop( tag);
132  return nullptr;
133  }
134 
136 
140  const StoreGateSvc_t& evtStore() const {return m_evtStore;};
142 
146  const StoreGateSvc_t& detStore() const {return m_detStore;};
148 
150  template <class T> StatusCode retrieveTool(ToolHandle<T>& thandle){
151  if (!thandle.empty() && thandle.retrieve().isFailure()){
152  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandle << ". Abort.");
153  return StatusCode::FAILURE;
154  } else
155  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandle);
156  return StatusCode::SUCCESS;
157  }
158 
160  template <class T>
161  StatusCode retrieveTools(ToolHandleArray<T>& thandleArray){
162  if (!thandleArray.empty() && thandleArray.retrieve().isFailure()){
163  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retrieve " << thandleArray << ". Abort.");
164  return StatusCode::FAILURE;
165  } else
166  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved " << thandleArray);
167  return StatusCode::SUCCESS;
168  }
169 
171  template<class T>
172  StatusCode recordCollection( T*& coll, const std::string& collName) const{
173  // create if necessary
174  coll = coll ? coll : new T;
175  // record
176  if (evtStore()->record( coll, collName).isFailure()){
177  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot record collection " << collName << ". Abort." );
178  return StatusCode::FAILURE;
179  } else
180  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully recorded collection " << collName);
181  return StatusCode::SUCCESS;
182  }
183 
185  template<class T>
186  StatusCode retrieveCollection(T*& coll, const std::string& collName, bool forceBreak=true) const {
187  // check for existence in the soft case
188  if (!forceBreak && !evtStore()->contains<T>(collName)) {
189  coll = 0;
190  ATH_MSG_DEBUG(m_screenOutputPrefix << "Collection does not exists (not required). Ignore.");
191  return StatusCode::SUCCESS;
192  }
193  if ( evtStore()->retrieve(coll, collName).isFailure()){
194  ATH_MSG_FATAL( m_screenOutputPrefix << "Cannot retireve collection " << collName << ". Abort." );
195  return StatusCode::FAILURE;
196  } else
197  ATH_MSG_DEBUG(m_screenOutputPrefix << "Successfully retrieved collection " << collName);
198  return StatusCode::SUCCESS;
199  }
200 
201  private:
204 
207 
210 
211 
212  protected:
214  std::string m_simDescr{""};
215 
217  std::string m_screenOutputPrefix{"isf >> "};
218 
220  std::string m_chronoSvcName{"ChronoStatSvc"};
221 
223  IChronoStatSvc* m_chrono{};
224 
227 
228  };
229 
230 
233  {
234  return StatusCode::SUCCESS;
235  }
236 }
237 
238 
239 #endif //> !ISF_BASESIMULATIONSVC_H
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
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:89
ISF::BaseSimulationSvc::sysInitialize
virtual StatusCode sysInitialize() override
Gaudi sysInitialize() methods.
Definition: BaseSimulationSvc.h:71
ISF::BaseSimulationSvc::simSvcDescriptor
std::string & simSvcDescriptor() override
Return the simulation service descriptor.
Definition: BaseSimulationSvc.h:86
ISF::BaseSimulationSvc::evtStore
const StoreGateSvc_t & evtStore() const
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: BaseSimulationSvc.h:140
ISF::BaseSimulationSvc::StoreGateSvc_t
ServiceHandle< StoreGateSvc > StoreGateSvc_t
Definition: BaseSimulationSvc.h:135
ISF::BaseSimulationSvc::BaseSimulationSvc
BaseSimulationSvc()
Default constructor.
ISF::BaseSimulationSvc::detStore
StoreGateSvc_t & detStore()
Definition: BaseSimulationSvc.h:147
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:172
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:93
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
ISF::BaseSimulationSvc::detStore
const StoreGateSvc_t & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: BaseSimulationSvc.h:146
ISF::BaseSimulationSvc::evtStore
StoreGateSvc_t & evtStore()
Definition: BaseSimulationSvc.h:141
ISF::BaseSimulationSvc::simulate
virtual StatusCode simulate(ISFParticle &isp, McEventCollection *mcEventCollection) override
Simulation call for individual particles.
Definition: BaseSimulationSvc.h:232
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:186
ISFParticle.h
ISF::BaseSimulationSvc::simulateVector
virtual StatusCode simulateVector(const ISFParticleVector &particles, McEventCollection *mcEventCollection, McEventCollection *) override
Simulation call for vectors of particles.
Definition: BaseSimulationSvc.h:103
McEventCollection.h
ISF::BaseSimulationSvc::setParticleBroker
virtual StatusCode setParticleBroker(IParticleBroker *broker) override
Inform the SimulationSvc about the ParticleBroker svc.
Definition: BaseSimulationSvc.h:97
ISF::BaseSimulationSvc::m_evtStore
StoreGateSvc_t m_evtStore
Pointer to StoreGate (event store by default)
Definition: BaseSimulationSvc.h:206
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
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_simDescr
std::string m_simDescr
The simulator service descriptor.
Definition: BaseSimulationSvc.h:214
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
ISF::BaseSimulationSvc::m_screenOutputPrefix
std::string m_screenOutputPrefix
Screen output refinement - can be changed by declareProperty()
Definition: BaseSimulationSvc.h:217
ISF::BaseSimulationSvc::chronoStop
const ChronoEntity * chronoStop(const IChronoSvc::ChronoTag &tag)
wrapper call to stop chrono with given tag
Definition: BaseSimulationSvc.h:130
ISF::BaseSimulationSvc::chronoStart
const ChronoEntity * chronoStart(const IChronoSvc::ChronoTag &tag)
wrapper call to start chrono with given tag
Definition: BaseSimulationSvc.h:124
ISF::BaseSimulationSvc::m_chronoSvcName
std::string m_chronoSvcName
Name of the timing service - can be set by declareProperty()
Definition: BaseSimulationSvc.h:220
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
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:68
ISF::BaseSimulationSvc::m_chrono
IChronoStatSvc * m_chrono
The timing service for general usage.
Definition: BaseSimulationSvc.h:223
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
ISimulationSvc.h
ISF::BaseSimulationSvc::m_detStore
StoreGateSvc_t m_detStore
Pointer to StoreGate (detector store by default)
Definition: BaseSimulationSvc.h:209
AthService.h
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
ISF::BaseSimulationSvc::retrieveTool
StatusCode retrieveTool(ToolHandle< T > &thandle)
templated Tool retrieval - gives unique handling & look and feel
Definition: BaseSimulationSvc.h:150
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:161
ISF::BaseSimulationSvc::m_particleBroker
IParticleBroker * m_particleBroker
The particle service used to push particles into the simulation.
Definition: BaseSimulationSvc.h:226
StoreGateSvc.h
SimSvcID.h
ServiceHandle< StoreGateSvc >