All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
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), m_engine(0), m_serialize(0)
38 {
39  declareProperty("Engine", m_engineName = "HepRndm::Engine<CLHEP::RanluxEngine>");
40 }
41 
44 }
45 
49  MsgStream log(msgSvc(), name());
50  std::string machName = name()+".Engine";
53 
54  if ( status.isSuccess() ) {
55  status = setProperties();
56  if ( status.isSuccess() ) { // Check if the Engine service exists:
57  // FIXME: (MCl) why RndmGenSvc cannot create the engine service in a standard way?
58  const bool CREATE = false;
59  engine = serviceLocator()->service(machName, CREATE);
60  if ( !engine.isValid() && mgr.isValid() ) {
62  engine = mgr->createService(TypeNameString(machName, m_engineName));
63  }
64  if ( engine.isValid() ) {
65  SmartIF<ISerialize> serial(engine);
66  SmartIF<IService> service(engine);
67  if ( serial.isValid( ) && service.isValid( ) ) {
68  status = service->sysInitialize();
69  if ( status.isSuccess() ) {
70  m_engine = engine;
71  m_serialize = serial;
72  m_engine->addRef();
74  log << MSG::INFO << "Using Random engine:" << m_engineName << endmsg;
75  return status;
76  }
77  }
78  }
79  }
80  }
81  return status;
82 }
83 
86  StatusCode status = Service::finalize();
88  m_serialize = 0;
89  if ( m_engine ) {
91  service->finalize().ignore();
92  m_engine->release();
93  }
94  m_engine = 0;
95  return status;
96 }
97 
101  if ( 0 != m_serialize ) {
102  return m_serialize->serialize(str);
103  }
104  MsgStream log(msgSvc(), name());
105  log << MSG::ERROR << "Cannot input serialize Generator settings!" << endmsg;
106  return str;
107 }
108 
111  if ( 0 != m_serialize ) {
112  return m_serialize->serialize(str);
113  }
114  MsgStream log(msgSvc(), name());
115  log << MSG::ERROR << "Cannot output serialize Generator settings!" << endmsg;
116  return str;
117 }
118 
121  return m_engine;
122 }
123 
127  IInterface* iface = ObjFactory::create(par.type(),(IInterface*)m_engine);
128  if ( iface ) {
129  // query requested interface (adds ref count)
130  status = iface->queryInterface(IRndmGen::interfaceID(), (void**)& refpGen);
131  if ( status.isSuccess() ) {
132  status = refpGen->initialize(par);
133  }
134  else {
135  iface->release();
136  }
137  }
138  // Error!
139  return status;
140 }
141 
142 // Single shot returning single random number
143 double RndmGenSvc::rndm() const {
144  if ( 0 != m_engine ) {
145  return m_engine->rndm();
146  }
147  return -1;
148 }
149 
150 /* Multiple shots returning vector with flat random numbers.
151  @param array Array containing random numbers
152  @param howmany fill 'howmany' random numbers into array
153  @param start ... starting at position start
154  @return StatusCode indicating failure or success.
155 */
156 StatusCode RndmGenSvc::rndmArray( std::vector<double>& array, long howmany, long start) const {
157  if ( 0 != m_engine ) {
158  return m_engine->rndmArray(array, howmany, start);
159  }
160  return StatusCode::FAILURE;
161 }
162 
163 // Allow to set new seeds
164 StatusCode RndmGenSvc::setSeeds(const std::vector<long>& seeds) {
165  if ( 0 != m_engine ) {
166  return m_engine->setSeeds(seeds);
167  }
168  return StatusCode::FAILURE;
169 }
170 
171 // Allow to get the seeds
172 StatusCode RndmGenSvc::seeds(std::vector<long>& seeds) const {
173  if ( 0 != m_engine ) {
174  return m_engine->seeds(seeds);
175  }
176  return StatusCode::FAILURE;
177 }
178 
virtual StatusCode initialize(const IRndmGen::Param &par)=0
Initialize the generator.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
virtual StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const =0
Multiple shots returning vector with flat random numbers.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual StatusCode seeds(std::vector< long > &seeds) const
Allow to get seeds.
Definition: RndmGenSvc.cpp:172
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:40
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
virtual const InterfaceID & type() const
Parameter's type.
Definition: IRndmGen.h:51
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:36
std::string m_engineName
Engine name.
Definition: RndmGenSvc.h:56
virtual double rndm() const
Single shot returning single random number.
Definition: RndmGenSvc.cpp:143
virtual StatusCode initialize()
Service override: initialization.
Definition: RndmGenSvc.cpp:47
virtual StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const
Multiple shots returning vector with flat random numbers.
Definition: RndmGenSvc.cpp:156
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
virtual StatusCode setSeeds(const std::vector< long > &seeds)
Allow to set new seeds.
Definition: RndmGenSvc.cpp:164
Random Generator service definition.
Definition: RndmGenSvc.h:49
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen)
Retrieve a valid generator from the service.
Definition: RndmGenSvc.cpp:125
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual double rndm() const =0
Single shot returning single random number.
virtual StreamBuffer & serialize(StreamBuffer &str)=0
Input serialization from stream buffer.
Definition of the basic interface.
Definition: IInterface.h:160
virtual StatusCode finalize()
Service override: finalization.
Definition: RndmGenSvc.cpp:85
virtual IRndmEngine * engine()
Retrieve engine.
Definition: RndmGenSvc.cpp:120
virtual StatusCode setSeeds(const std::vector< long > &seeds)=0
Allow to set new seeds.
virtual ~RndmGenSvc()
Standard Service destructor.
Definition: RndmGenSvc.cpp:43
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:371
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
IRndmEngine * m_engine
Random number engine.
Definition: RndmGenSvc.h:52
virtual StreamBuffer & serialize(StreamBuffer &str)
IRndmGenSvc interface implementation.
Definition: RndmGenSvc.cpp:100
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
virtual unsigned long release()=0
Release Interface instance.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
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:142
Definition of a interface for a generic random number generator giving randomly distributed numbers i...
Definition: IRndmEngine.h:20
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
virtual StatusCode seeds(std::vector< long > &seeds) const =0
Allow to retrieve seeds.
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
tuple start
Definition: IOTest.py:88
ISerialize * m_serialize
Serialization interface of random number engine.
Definition: RndmGenSvc.h:54
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.