The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
HepRndmBaseEngine.h
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// CLHEP Basic Random Engine definition file
13//--------------------------------------------------------------------
14//
15// Package : HepRndm ( 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#pragma once
26
27// Framework include files
28#include "RndmEngine.h"
29#include <CLHEP/Random/Random.h>
30#include <memory>
31#include <mutex>
32#include <utility>
33
34// Forward declarations
35namespace CLHEP {
36 class HepRandomEngine;
37}
38
39namespace HepRndm {
40
41 template <typename Engine>
42 class SynchronizedEngine : public Engine {
43
44 private:
45 mutable std::mutex m_mutex;
47 public:
48 using Engine::Engine;
49
50 public:
51 // reimplement anything that should be locked for thread safety
52
53 double flat() override {
54 std::lock_guard<std::mutex> lock( m_mutex );
55 return Engine::flat();
56 }
57 void flatArray( const int size, double* vect ) override {
58 std::lock_guard<std::mutex> lock( m_mutex );
59 Engine::flatArray( size, vect );
60 }
61 void setSeed( long seed, int n ) override {
62 std::lock_guard<std::mutex> lock( m_mutex );
63 return Engine::setSeed( seed, n );
64 }
65 void setSeeds( const long* seeds, int n ) override {
66 std::lock_guard<std::mutex> lock( m_mutex );
67 return Engine::setSeeds( seeds, n );
68 }
69 void saveStatus( const char filename[] = "Config.conf" ) const override {
70 std::lock_guard<std::mutex> lock( m_mutex );
71 return Engine::saveStatus( filename );
72 }
73 void restoreStatus( const char filename[] = "Config.conf" ) override {
74 std::lock_guard<std::mutex> lock( m_mutex );
75 return Engine::restoreStatus( filename );
76 }
77 void showStatus() const override {
78 std::lock_guard<std::mutex> lock( m_mutex );
79 return Engine::showStatus();
80 }
81 std::ostream& put( std::ostream& os ) const override {
82 std::lock_guard<std::mutex> lock( m_mutex );
83 return Engine::put( os );
84 }
85 std::istream& get( std::istream& is ) override {
86 std::lock_guard<std::mutex> lock( m_mutex );
87 return Engine::get( is );
88 }
89 std::istream& getState( std::istream& is ) override {
90 std::lock_guard<std::mutex> lock( m_mutex );
91 return Engine::getState( is );
92 }
93 std::vector<unsigned long> put() const override {
94 std::lock_guard<std::mutex> lock( m_mutex );
95 return Engine::put();
96 }
97 bool get( const std::vector<unsigned long>& v ) override {
98 std::lock_guard<std::mutex> lock( m_mutex );
99 return Engine::get( v );
100 }
101 bool getState( const std::vector<unsigned long>& v ) override {
102 std::lock_guard<std::mutex> lock( m_mutex );
103 return Engine::getState( v );
104 }
105 operator double() override {
106 std::lock_guard<std::mutex> lock( m_mutex );
107 return Engine::operator double();
108 }
109 operator float() override {
110 std::lock_guard<std::mutex> lock( m_mutex );
111 return Engine::operator float();
112 }
113 operator unsigned int() override {
114 std::lock_guard<std::mutex> lock( m_mutex );
115 return Engine::operator unsigned int();
116 }
117 };
118
119 class BaseEngine : public RndmEngine {
120 protected:
121 std::unique_ptr<CLHEP::HepRandomEngine> m_hepEngine;
122 Gaudi::Property<bool> m_threadSafe{ this, "ThreadSafe", true };
123
124 public:
125 BaseEngine( const std::string& name, ISvcLocator* loc ) : RndmEngine( name, loc ) {}
126 CLHEP::HepRandomEngine* hepEngine() { return m_hepEngine.get(); }
127 const CLHEP::HepRandomEngine* hepEngine() const { return m_hepEngine.get(); }
128 // Retrieve single random number
129 double rndm() const override { return m_hepEngine->flat(); }
130
131 StatusCode finalize() override {
132 if ( m_hepEngine ) { CLHEP::HepRandom::setTheEngine( nullptr ); }
133 m_hepEngine.reset();
134 return RndmEngine::finalize();
135 }
136
137 protected:
139 virtual std::unique_ptr<CLHEP::HepRandomEngine> createEngine() = 0;
140 template <typename Engine, typename... Args>
141 auto create_engine( Args&&... args ) {
142 return ( m_threadSafe.value() ? std::make_unique<SynchronizedEngine<Engine>>( std::forward<Args>( args )... )
143 : std::make_unique<Engine>( std::forward<Args>( args )... ) );
144 }
145 };
146} // namespace HepRndm
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
auto create_engine(Args &&... args)
const CLHEP::HepRandomEngine * hepEngine() const
double rndm() const override
StatusCode finalize() override
CLHEP::HepRandomEngine * hepEngine()
virtual std::unique_ptr< CLHEP::HepRandomEngine > createEngine()=0
BaseEngine(const std::string &name, ISvcLocator *loc)
Gaudi::Property< bool > m_threadSafe
std::unique_ptr< CLHEP::HepRandomEngine > m_hepEngine
StatusCode setSeeds(const std::vector< long > &seed) override
Set seeds.
StatusCode seeds(std::vector< long > &seed) const override
Retrieve seeds.
std::ostream & put(std::ostream &os) const override
void setSeed(long seed, int n) override
void restoreStatus(const char filename[]="Config.conf") override
std::istream & get(std::istream &is) override
std::vector< unsigned long > put() const override
std::istream & getState(std::istream &is) override
void flatArray(const int size, double *vect) override
void setSeeds(const long *seeds, int n) override
void saveStatus(const char filename[]="Config.conf") const override
bool getState(const std::vector< unsigned long > &v) override
bool get(const std::vector< unsigned long > &v) override
void showStatus() const override
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
Random Generator engine definition.
Definition RndmEngine.h:51
StatusCode finalize() override
Definition Service.cpp:223
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64