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