The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
RndmGenSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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
43
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& jos = serviceLocator()->getOptsSvc();
54 if ( !jos.isSet( machName + ".ThreadSafe" ) ) {
55 jos.set( machName + ".ThreadSafe", m_useThreadSafeEngine ? "true" : "false" );
56 }
57 auto engine = serviceLocator()->service<IRndmEngine>( machName, CREATE );
58 if ( !engine && mgr ) {
60 engine = mgr->createService( TypeNameString( machName, m_engineName ) );
61 }
62 if ( engine ) {
63 auto serial = engine.as<ISerialize>();
64 auto service = engine.as<IService>();
65 if ( serial && service ) {
66 status = service->sysInitialize();
67 if ( status.isSuccess() ) {
69 m_serialize = serial;
70 info() << "Using Random engine:" << m_engineName.value() << endmsg;
71 }
72 }
73 }
74 }
75 return status;
76}
77
81 m_serialize.reset();
82 if ( m_engine ) {
84 m_engine.reset();
85 }
86 return status;
87}
88
92 if ( m_serialize ) return m_serialize->serialize( str );
93 error() << "Cannot input serialize Generator settings!" << endmsg;
94 return str;
95}
96
99 if ( m_serialize ) return m_serialize->serialize( str );
100 error() << "Cannot output serialize Generator settings!" << endmsg;
101 return str;
102}
103
106
109 auto pGen = SmartIF<IRndmGen>( ObjFactory::create( par.type(), m_engine.get() ).release() );
110 if ( !pGen ) {
111 refpGen = nullptr;
112 return StatusCode::FAILURE;
113 }
114 refpGen = pGen.get();
115 refpGen->addRef(); // insure the caller gets something with a refCount of (at least) one back...
116 return refpGen->initialize( par );
117}
118
119// Single shot returning single random number
120double RndmGenSvc::rndm() const { return m_engine ? m_engine->rndm() : -1; }
121
122/* Multiple shots returning vector with flat random numbers.
123 @param array Array containing random numbers
124 @param howmany fill 'howmany' random numbers into array
125 @param start ... starting at position start
126 @return StatusCode indicating failure or success.
127*/
128StatusCode RndmGenSvc::rndmArray( std::vector<double>& array, long howmany, long start ) const {
129 return m_engine ? m_engine->rndmArray( array, howmany, start ) : StatusCode::FAILURE;
130}
131
132// Allow to set new seeds
133StatusCode RndmGenSvc::setSeeds( const std::vector<long>& seeds ) {
134 return m_engine ? m_engine->setSeeds( seeds ) : StatusCode::FAILURE;
135}
136
137// Allow to get the seeds
138StatusCode RndmGenSvc::seeds( std::vector<long>& seeds ) const {
139 return m_engine ? m_engine->seeds( seeds ) : StatusCode::FAILURE;
140}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Helper class to parse a string of format "type/name".
virtual unsigned long addRef() const =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:25
Definition of a interface for a generic random number generators.
Definition IRndmGen.h:40
virtual StatusCode initialize(const IRndmGen::Param &par)=0
Initialize the generator.
Object serialization interface definition.
Definition ISerialize.h:24
General service interface definition.
Definition IService.h:26
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition ISvcManager.h:30
Random Generator service definition.
Definition RndmGenSvc.h:58
StreamBuffer & serialize(StreamBuffer &str) override
IRndmGenSvc interface implementation.
StatusCode initialize() override
Service override: initialization.
Gaudi::Property< bool > m_useThreadSafeEngine
Definition RndmGenSvc.h:66
double rndm() const override
Single shot returning single random number.
SmartIF< ISerialize > m_serialize
Serialization interface of random number engine.
Definition RndmGenSvc.h:63
StatusCode seeds(std::vector< long > &seeds) const override
Allow to get seeds.
StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen) override
Retrieve a valid generator from the service.
StatusCode setSeeds(const std::vector< long > &seeds) override
Allow to set new seeds.
SmartIF< IRndmEngine > m_engine
Random number engine.
Definition RndmGenSvc.h:61
IRndmEngine * engine() override
Retrieve engine.
StatusCode rndmArray(std::vector< double > &array, long howmany, long start=0) const override
Multiple shots returning vector with flat random numbers.
StatusCode finalize() override
Service override: finalization.
Gaudi::Property< std::string > m_engineName
Definition RndmGenSvc.h:65
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
StatusCode finalize() override
Definition Service.cpp:223
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
StatusCode start() override
Definition Service.cpp:187
StatusCode initialize() override
Definition Service.cpp:118
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto FAILURE
Definition StatusCode.h:100
The stream buffer is a small object collecting object data.