The Gaudi Framework  master (37c0b60a)
RndmGenSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 //====================================================================
12 // Random Generator service implementation
13 //--------------------------------------------------------------------
14 //
15 // Package : Gaudi/RndmGen ( The LHCb Offline System)
16 // Author : M.Frank
17 // History :
18 // +---------+----------------------------------------------+---------
19 // | Date | Comment | Who
20 // +---------+----------------------------------------------+---------
21 // | 29/10/99| Initial version | MF
22 // +---------+----------------------------------------------+---------
23 //
24 //====================================================================
25 #define GAUDI_RANDOMGENSVC_RNDMGENSVC_CPP
26 
27 // STL include files
28 #include <cfloat>
29 
30 // Framework include files
34 #include <GaudiKernel/SmartIF.h>
35 
36 #include "RndmGen.h"
37 #include "RndmGenSvc.h"
38 
39 // Instantiation of a static factory class used by clients to create
40 // instances of this service
42 
46 
47  auto mgr = serviceLocator()->as<ISvcManager>();
48 
49  if ( status.isSuccess() ) {
50  // FIXME: (MCl) why RndmGenSvc cannot create the engine service in a standard way?
51  const bool CREATE = false;
52  std::string machName = name() + ".Engine";
53  auto engine = serviceLocator()->service<IRndmEngine>( machName, CREATE );
54  if ( !engine && mgr ) {
56  engine = mgr->createService( TypeNameString( machName, m_engineName ) );
57  }
58  if ( engine ) {
59  auto serial = engine.as<ISerialize>();
60  auto service = engine.as<IService>();
61  if ( serial && service ) {
62  status = service->sysInitialize();
63  if ( status.isSuccess() ) {
64  m_engine = engine;
65  m_serialize = serial;
66  info() << "Using Random engine:" << m_engineName.value() << endmsg;
67  }
68  }
69  }
70  }
71  return status;
72 }
73 
76  StatusCode status = Service::finalize();
78  if ( m_engine ) {
80  m_engine.reset();
81  }
82  return status;
83 }
84 
88  if ( m_serialize ) return m_serialize->serialize( str );
89  error() << "Cannot input serialize Generator settings!" << endmsg;
90  return str;
91 }
92 
95  if ( m_serialize ) return m_serialize->serialize( str );
96  error() << "Cannot output serialize Generator settings!" << endmsg;
97  return str;
98 }
99 
102 
105  auto pGen = SmartIF<IRndmGen>( ObjFactory::create( par.type(), m_engine.get() ).release() );
106  if ( !pGen ) {
107  refpGen = nullptr;
108  return StatusCode::FAILURE;
109  }
110  refpGen = pGen.get();
111  refpGen->addRef(); // insure the caller gets something with a refCount of (at least) one back...
112  return refpGen->initialize( par );
113 }
114 
115 // Single shot returning single random number
116 double RndmGenSvc::rndm() const { return m_engine ? m_engine->rndm() : -1; }
117 
118 /* Multiple shots returning vector with flat random numbers.
119  @param array Array containing random numbers
120  @param howmany fill 'howmany' random numbers into array
121  @param start ... starting at position start
122  @return StatusCode indicating failure or success.
123 */
125  return m_engine ? m_engine->rndmArray( array, howmany, start ) : StatusCode::FAILURE;
126 }
127 
128 // Allow to set new seeds
130  return m_engine ? m_engine->setSeeds( seeds ) : StatusCode::FAILURE;
131 }
132 
133 // Allow to get the seeds
135  return m_engine ? m_engine->seeds( seeds ) : StatusCode::FAILURE;
136 }
IService
Definition: IService.h:28
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
Service::start
StatusCode start() override
Definition: Service.cpp:187
std::vector< double >
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
IRndmEngine
Definition: IRndmEngine.h:29
RndmGen.h
ObjectFactory.h
RndmGenSvc::m_engine
SmartIF< IRndmEngine > m_engine
Random number engine.
Definition: RndmGenSvc.h:62
IOTest.start
start
Definition: IOTest.py:110
ISvcManager
Definition: ISvcManager.h:38
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
StreamBuffer
Definition: StreamBuffer.h:52
RndmGenSvc::initialize
StatusCode initialize() override
Service override: initialization.
Definition: RndmGenSvc.cpp:44
RndmGenSvc.h
compareOutputFiles.par
par
Definition: compareOutputFiles.py:477
RndmGenSvc::m_serialize
SmartIF< ISerialize > m_serialize
Serialization interface of random number engine.
Definition: RndmGenSvc.h:64
IRndmEngine.h
RndmGenSvc::finalize
StatusCode finalize() override
Service override: finalization.
Definition: RndmGenSvc.cpp:75
SmartIF.h
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
RndmGenSvc::generator
StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen) override
Retrieve a valid generator from the service.
Definition: RndmGenSvc.cpp:104
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
RndmGenSvc::m_engineName
Gaudi::Property< std::string > m_engineName
Definition: RndmGenSvc.h:66
IRndmGen::Param
Definition: IRndmGen.h:49
SmartIF< IRndmGen >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
RndmGenSvc::setSeeds
StatusCode setSeeds(const std::vector< long > &seeds) override
Allow to set new seeds.
Definition: RndmGenSvc.cpp:129
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
RndmGenSvc::rndmArray
StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const override
Multiple shots returning vector with flat random numbers.
Definition: RndmGenSvc.cpp:124
IRndmGen
Definition: IRndmGen.h:44
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
Containers::array
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
Definition: KeyedObjectManager.h:37
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Io::CREATE
@ CREATE
Definition: IFileMgr.h:41
RndmGenSvc::rndm
double rndm() const override
Single shot returning single random number.
Definition: RndmGenSvc.cpp:116
RndmGenSvc::serialize
StreamBuffer & serialize(StreamBuffer &str) override
IRndmGenSvc interface implementation
Definition: RndmGenSvc.cpp:87
RndmGenSvc::seeds
StatusCode seeds(std::vector< long > &seeds) const override
Allow to get seeds.
Definition: RndmGenSvc.cpp:134
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
RndmGenSvc
Random Generator service definition.
Definition: RndmGenSvc.h:59
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:89
ISerialize
Definition: ISerialize.h:27
IInterface::addRef
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
IRndmGen::initialize
virtual StatusCode initialize(const IRndmGen::Param &par)=0
Initialize the generator.
ISvcManager.h
RndmGenSvc::engine
IRndmEngine * engine() override
Retrieve engine.
Definition: RndmGenSvc.cpp:101
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335