| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
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
33 DECLARE_COMPONENT(RndmGenSvc)
34
35
36 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
43 RndmGenSvc::~RndmGenSvc() {
44 }
45
47 StatusCode RndmGenSvc::initialize() {
48 StatusCode status = Service::initialize();
49 MsgStream log(msgSvc(), name());
50 std::string machName = name()+".Engine";
51 SmartIF<IRndmEngine> engine;
52 SmartIF<ISvcManager> mgr(serviceLocator());
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() ) {
61 using Gaudi::Utils::TypeNameString;
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();
73 m_serialize->addRef();
74 log << MSG::INFO << "Using Random engine:" << m_engineName << endmsg;
75 return status;
76 }
77 }
78 }
79 }
80 }
81 return status;
82 }
83
85 StatusCode RndmGenSvc::finalize() {
86 StatusCode status = Service::finalize();
87 if ( m_serialize ) m_serialize->release();
88 m_serialize = 0;
89 if ( m_engine ) {
90 SmartIF<IService> service(m_engine);
91 service->finalize().ignore();
92 m_engine->release();
93 }
94 m_engine = 0;
95 return status;
96 }
97
99
100 StreamBuffer& RndmGenSvc::serialize(StreamBuffer& str) {
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
110 StreamBuffer& RndmGenSvc::serialize(StreamBuffer& str) const {
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
120 IRndmEngine* RndmGenSvc::engine() {
121 return m_engine;
122 }
123
125 StatusCode RndmGenSvc::generator(const IRndmGen::Param& par, IRndmGen*& refpGen) {
126 StatusCode status = StatusCode::FAILURE;
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