The Gaudi Framework  v33r0 (d5ea422b)
RndmGenSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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  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  info() << "Using Random engine:" << m_engineName.value() << 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  error() << "Cannot input serialize Generator settings!" << endmsg;
93  return str;
94 }
95 
98  if ( m_serialize ) return m_serialize->serialize( str );
99  error() << "Cannot output serialize Generator settings!" << endmsg;
100  return str;
101 }
102 
105 
108  auto pGen = SmartIF<IRndmGen>( ObjFactory::create( par.type(), m_engine.get() ).release() );
109  if ( !pGen ) {
110  refpGen = nullptr;
111  return StatusCode::FAILURE;
112  }
113  refpGen = pGen.get();
114  refpGen->addRef(); // insure the caller gets something with a refCount of (at least) one back...
115  return refpGen->initialize( par );
116 }
117 
118 // Single shot returning single random number
119 double RndmGenSvc::rndm() const { return m_engine ? m_engine->rndm() : -1; }
120 
121 /* Multiple shots returning vector with flat random numbers.
122  @param array Array containing random numbers
123  @param howmany fill 'howmany' random numbers into array
124  @param start ... starting at position start
125  @return StatusCode indicating failure or success.
126 */
128  return m_engine ? m_engine->rndmArray( array, howmany, start ) : StatusCode::FAILURE;
129 }
130 
131 // Allow to set new seeds
134 }
135 
136 // Allow to get the seeds
139 }
virtual StatusCode initialize(const IRndmGen::Param &par)=0
Initialize the generator.
StatusCode initialize() override
Definition: Service.cpp:70
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:287
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
StatusCode initialize() override
Service override: initialization.
Definition: RndmGenSvc.cpp:44
StatusCode finalize() override
Definition: Service.cpp:174
StatusCode finalize() override
Service override: finalization.
Definition: RndmGenSvc.cpp:78
StatusCode start() override
Definition: Service.cpp:139
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:38
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:51
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:44
SmartIF< IRndmEngine > m_engine
Random number engine.
Definition: RndmGenSvc.h:62
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
Gaudi::Property< std::string > m_engineName
Definition: RndmGenSvc.h:66
SmartIF< IFace > as()
Definition: ISvcLocator.h:113
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
def start
Definition: IOTest.py:108
STL class.
Random Generator service definition.
Definition: RndmGenSvc.h:59
#define DECLARE_COMPONENT(type)
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Helper class to parse a string of format "type/name".
General service interface definition.
Definition: IService.h:28
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
virtual double rndm() const =0
Single shot returning single random number.
virtual StreamBuffer & serialize(StreamBuffer &str)=0
Input serialization from stream buffer.
StatusCode seeds(std::vector< long > &seeds) const override
Allow to get seeds.
Definition: RndmGenSvc.cpp:137
StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen) override
Retrieve a valid generator from the service.
Definition: RndmGenSvc.cpp:107
virtual StatusCode setSeeds(const std::vector< long > &seeds)=0
Allow to set new seeds.
bool isSuccess() const
Definition: StatusCode.h:361
StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const override
Multiple shots returning vector with flat random numbers.
Definition: RndmGenSvc.cpp:127
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:290
double rndm() const override
Single shot returning single random number.
Definition: RndmGenSvc.cpp:119
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:164
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:132
constexpr static const auto FAILURE
Definition: StatusCode.h:97
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:93
Definition of a interface for a generic random number generator giving randomly distributed numbers i...
Definition: IRndmEngine.h:29
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
IRndmEngine * engine() override
Retrieve engine.
Definition: RndmGenSvc.cpp:104
SmartIF< ISerialize > m_serialize
Serialization interface of random number engine.
Definition: RndmGenSvc.h:64
Object serialization interface definition.
Definition: ISerialize.h:27
virtual StatusCode seeds(std::vector< long > &seeds) const =0
Allow to retrieve seeds.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202