Geant4-11
RandExponential.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// -----------------------------------------------------------------------
4// HEP Random
5// --- RandExponential ---
6// class implementation file
7// -----------------------------------------------------------------------
8// This file is part of Geant4 (simulation toolkit for HEP).
9
10// =======================================================================
11// Gabriele Cosmo - Created: 17th May 1996
12// - Added methods to shoot arrays: 28th July 1997
13// J.Marraffino - Added default mean as attribute and
14// operator() with mean: 16th Feb 1998
15// M Fischler - put and get to/from streams 12/15/04
16// M Fischler - put/get to/from streams uses pairs of ulongs when
17// + storing doubles avoid problems with precision
18// 4/14/05
19// =======================================================================
20
23#include <cmath>
24#include <iostream>
25#include <string>
26#include <vector>
27
28namespace CLHEP {
29
30std::string RandExponential::name() const {return "RandExponential";}
32
34}
35
37 return fire( defaultMean );
38}
39
40double RandExponential::operator()( double mean ) {
41 return fire( mean );
42}
43
45 return -std::log(HepRandom::getTheEngine()->flat());
46}
47
48double RandExponential::shoot(double mean) {
49 return -std::log(HepRandom::getTheEngine()->flat())*mean;
50}
51
52void RandExponential::shootArray( const int size, double* vect,
53 double mean )
54{
55 for( double* v = vect; v != vect+size; ++v )
56 *v = shoot(mean);
57}
58
59void RandExponential::shootArray(HepRandomEngine* anEngine, const int size,
60 double* vect, double mean )
61{
62 for( double* v = vect; v != vect+size; ++v )
63 *v = shoot(anEngine, mean);
64}
65
66void RandExponential::fireArray( const int size, double* vect)
67{
68 for( double* v = vect; v != vect+size; ++v )
69 *v = fire( defaultMean );
70}
71
72void RandExponential::fireArray( const int size, double* vect,
73 double mean )
74{
75 for( double* v = vect; v != vect+size; ++v )
76 *v = fire( mean );
77}
78
79std::ostream & RandExponential::put ( std::ostream & os ) const {
80 int pr=os.precision(20);
81 std::vector<unsigned long> t(2);
82 os << " " << name() << "\n";
83 os << "Uvec" << "\n";
85 os << defaultMean << " " << t[0] << " " << t[1] << "\n";
86 os.precision(pr);
87 return os;
88}
89
90std::istream & RandExponential::get ( std::istream & is ) {
91 std::string inName;
92 is >> inName;
93 if (inName != name()) {
94 is.clear(std::ios::badbit | is.rdstate());
95 std::cerr << "Mismatch when expecting to read state of a "
96 << name() << " distribution\n"
97 << "Name found was " << inName
98 << "\nistream is left in the badbit state\n";
99 return is;
100 }
101 if (possibleKeywordInput(is, "Uvec", defaultMean)) {
102 std::vector<unsigned long> t(2);
103 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t);
104 return is;
105 }
106 // is >> defaultMean encompassed by possibleKeywordInput
107 return is;
108}
109
110
111} // namespace CLHEP
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
static HepRandomEngine * getTheEngine()
Definition: Random.cc:268
double flat()
Definition: Random.cc:199
std::ostream & put(std::ostream &os) const
std::shared_ptr< HepRandomEngine > localEngine
static void shootArray(const int size, double *vect, double mean=1.0)
std::istream & get(std::istream &is)
std::string name() const
void fireArray(const int size, double *vect)
HepRandomEngine & engine()
Definition: DoubConv.h:17
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:166