CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

RandExponential.cc
Go to the documentation of this file.
1 // $Id: RandExponential.cc,v 1.6 2010/06/16 17:24:53 garren Exp $
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.hh"
25 
26 namespace CLHEP {
27 
28 std::string RandExponential::name() const {return "RandExponential";}
29 HepRandomEngine & RandExponential::engine() {return *localEngine;}
30 
32 }
33 
35  return fire( defaultMean );
36 }
37 
38 double RandExponential::operator()( double mean ) {
39  return fire( mean );
40 }
41 
43  return -std::log(HepRandom::getTheEngine()->flat());
44 }
45 
46 double RandExponential::shoot(double mean) {
47  return -std::log(HepRandom::getTheEngine()->flat())*mean;
48 }
49 
50 void RandExponential::shootArray( const int size, double* vect,
51  double mean )
52 {
53  for( double* v = vect; v != vect+size; ++v )
54  *v = shoot(mean);
55 }
56 
57 void RandExponential::shootArray(HepRandomEngine* anEngine, const int size,
58  double* vect, double mean )
59 {
60  for( double* v = vect; v != vect+size; ++v )
61  *v = shoot(anEngine, mean);
62 }
63 
64 void RandExponential::fireArray( const int size, double* vect)
65 {
66  for( double* v = vect; v != vect+size; ++v )
67  *v = fire( defaultMean );
68 }
69 
70 void RandExponential::fireArray( const int size, double* vect,
71  double mean )
72 {
73  for( double* v = vect; v != vect+size; ++v )
74  *v = fire( mean );
75 }
76 
77 std::ostream & RandExponential::put ( std::ostream & os ) const {
78  int pr=os.precision(20);
79  std::vector<unsigned long> t(2);
80  os << " " << name() << "\n";
81  os << "Uvec" << "\n";
82  t = DoubConv::dto2longs(defaultMean);
83  os << defaultMean << " " << t[0] << " " << t[1] << "\n";
84  os.precision(pr);
85  return os;
86 #ifdef REMOVED
87  int pr=os.precision(20);
88  os << " " << name() << "\n";
89  os << defaultMean << "\n";
90  os.precision(pr);
91  return os;
92 #endif
93 }
94 
95 std::istream & RandExponential::get ( std::istream & is ) {
96  std::string inName;
97  is >> inName;
98  if (inName != name()) {
99  is.clear(std::ios::badbit | is.rdstate());
100  std::cerr << "Mismatch when expecting to read state of a "
101  << name() << " distribution\n"
102  << "Name found was " << inName
103  << "\nistream is left in the badbit state\n";
104  return is;
105  }
106  if (possibleKeywordInput(is, "Uvec", defaultMean)) {
107  std::vector<unsigned long> t(2);
108  is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t);
109  return is;
110  }
111  // is >> defaultMean encompassed by possibleKeywordInput
112  return is;
113 }
114 
115 
116 } // namespace CLHEP
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
static void shootArray(const int size, double *vect, double mean=1.0)
std::istream & get(std::istream &is)
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:106
HepRandomEngine & engine()
std::ostream & put(std::ostream &os) const
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:90
void fireArray(const int size, double *vect)
double flat()
Definition: Random.cc:97
std::string name() const