RndmGenSvc.cpp
Go to the documentation of this file.
1 //====================================================================
2 // Random Generator service implementation
3 //--------------------------------------------------------------------
4 //
5 // Package : Gaudi/RndmGen ( The LHCb Offline System)
6 // Author : M.Frank
7 // History :
8 // +---------+----------------------------------------------+---------
9 // | Date | Comment | Who
10 // +---------+----------------------------------------------+---------
11 // | 29/10/99| Initial version | MF
12 // +---------+----------------------------------------------+---------
13 //
14 //====================================================================
15 #define GAUDI_RANDOMGENSVC_RNDMGENSVC_CPP
16 
17 // STL include files
18 #include <cfloat>
19 
20 // Framework include files
21 #include "GaudiKernel/SmartIF.h"
22 #include "GaudiKernel/ObjectFactory.h"
23 #include "GaudiKernel/ISvcManager.h"
24 #include "GaudiKernel/IRndmEngine.h"
25 
26 #include "GaudiKernel/MsgStream.h"
27 
28 #include "RndmGen.h"
29 #include "RndmGenSvc.h"
30 
31 // Instantiation of a static factory class used by clients to create
32 // instances of this service
34 
35 RndmGenSvc::RndmGenSvc(const std::string& nam, ISvcLocator* svc)
37 : base_class(nam, svc)
38 {
39  declareProperty("Engine", m_engineName = "HepRndm::Engine<CLHEP::RanluxEngine>");
40 }
41 
45 
46  MsgStream log(msgSvc(), name());
47  auto mgr = serviceLocator()->as<ISvcManager>();
48 
49  if ( status.isSuccess() ) {
50  status = setProperties();
51  if ( status.isSuccess() ) { // Check if the Engine service exists:
52  // FIXME: (MCl) why RndmGenSvc cannot create the engine service in a standard way?
53  const bool CREATE = false;
54  std::string machName = name()+".Engine";
55  auto engine = serviceLocator()->service<IRndmEngine>(machName, CREATE);
56  if ( !engine && mgr ) {
58  engine = mgr->createService(TypeNameString(machName, m_engineName));
59  }
60  if ( engine ) {
61  auto serial = engine.as<ISerialize>();
62  auto service = engine.as<IService>();
63  if ( serial && service ) {
64  status = service->sysInitialize();
65  if ( status.isSuccess() ) {
66  m_engine = engine;
67  m_serialize = serial;
68  log << MSG::INFO << "Using Random engine:" << m_engineName << endmsg;
69  }
70  }
71  }
72  }
73  }
74  return status;
75 }
76 
79  StatusCode status = Service::finalize();
81  if ( m_engine ) {
83  m_engine.reset();
84  }
85  return status;
86 }
87 
91  if ( m_serialize ) return m_serialize->serialize(str);
92  MsgStream log(msgSvc(), name());
93  log << MSG::ERROR << "Cannot input serialize Generator settings!" << endmsg;
94  return str;
95 }
96 
99  if ( m_serialize ) return m_serialize->serialize(str);
100  MsgStream log(msgSvc(), name());
101  log << MSG::ERROR << "Cannot output serialize Generator settings!" << endmsg;
102  return str;
103 }
104 
107  return m_engine.get();
108 }
109 
112  auto pGen = SmartIF<IRndmGen>( ObjFactory::create(par.type(),m_engine.get()) );
113  if (!pGen) {
114  refpGen = nullptr;
115  return StatusCode::FAILURE;
116  }
117  refpGen = pGen.get();
118  refpGen->addRef(); // insure the caller gets something with a refCount of (at least) one back...
119  return refpGen->initialize(par);
120 }
121 
122 // Single shot returning single random number
123 double RndmGenSvc::rndm() const {
124  return m_engine ? m_engine->rndm() : -1;
125 }
126 
127 /* Multiple shots returning vector with flat random numbers.
128  @param array Array containing random numbers
129  @param howmany fill 'howmany' random numbers into array
130  @param start ... starting at position start
131  @return StatusCode indicating failure or success.
132 */
133 StatusCode RndmGenSvc::rndmArray( std::vector<double>& array, long howmany, long start) const {
134  return m_engine ? m_engine->rndmArray(array, howmany, start)
136 }
137 
138 // Allow to set new seeds
139 StatusCode RndmGenSvc::setSeeds(const std::vector<long>& seeds) {
140  return m_engine ? m_engine->setSeeds(seeds)
142 }
143 
144 // Allow to get the seeds
145 StatusCode RndmGenSvc::seeds(std::vector<long>& seeds) const {
146  return m_engine ? m_engine->seeds(seeds)
148 }
149 
virtual StatusCode initialize(const IRndmGen::Param &par)=0
Initialize the generator.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
virtual StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const =0
Multiple shots returning vector with flat random numbers.
StreamBuffer & serialize(StreamBuffer &str) override
IRndmGenSvc interface implementation.
Definition: RndmGenSvc.cpp:90
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode initialize() override
Service override: initialization.
Definition: RndmGenSvc.cpp:43
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Definition: Service.cpp:188
StatusCode finalize() override
Service override: finalization.
Definition: RndmGenSvc.cpp:78
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
virtual const InterfaceID & type() const
Parameter's type.
Definition: IRndmGen.h:50
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:35
std::string m_engineName
Engine name.
Definition: RndmGenSvc.h:56
SmartIF< IRndmEngine > m_engine
Random number engine.
Definition: RndmGenSvc.h:52
STL namespace.
Random Generator service definition.
Definition: RndmGenSvc.h:49
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:110
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual double rndm() const =0
Single shot returning single random number.
virtual StreamBuffer & serialize(StreamBuffer &str)=0
Input serialization from stream buffer.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode seeds(std::vector< long > &seeds) const override
Allow to get seeds.
Definition: RndmGenSvc.cpp:145
StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen) override
Retrieve a valid generator from the service.
Definition: RndmGenSvc.cpp:111
virtual StatusCode setSeeds(const std::vector< long > &seeds)=0
Allow to set new seeds.
StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const override
Multiple shots returning vector with flat random numbers.
Definition: RndmGenSvc.cpp:133
double rndm() const override
Single shot returning single random number.
Definition: RndmGenSvc.cpp:123
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
StatusCode setSeeds(const std::vector< long > &seeds) override
Allow to set new seeds.
Definition: RndmGenSvc.cpp:139
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
Definition of a interface for a generic random number generator giving randomly distributed numbers i...
Definition: IRndmEngine.h:19
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
IRndmEngine * engine() override
Retrieve engine.
Definition: RndmGenSvc.cpp:106
SmartIF< ISerialize > m_serialize
Serialization interface of random number engine.
Definition: RndmGenSvc.h:54
void ignore() const
Definition: StatusCode.h:108
Object serialization interface definition.
Definition: ISerialize.h:17
virtual StatusCode seeds(std::vector< long > &seeds) const =0
Allow to retrieve seeds.
tuple start
Definition: IOTest.py:88