CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RandExponential.cc,v 1.4.4.2 2005/04/15 16:32:53 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- RandExponential --- 00007 // class implementation file 00008 // ----------------------------------------------------------------------- 00009 // This file is part of Geant4 (simulation toolkit for HEP). 00010 00011 // ======================================================================= 00012 // Gabriele Cosmo - Created: 17th May 1996 00013 // - Added methods to shoot arrays: 28th July 1997 00014 // J.Marraffino - Added default mean as attribute and 00015 // operator() with mean: 16th Feb 1998 00016 // M Fischler - put and get to/from streams 12/15/04 00017 // M Fischler - put/get to/from streams uses pairs of ulongs when 00018 // + storing doubles avoid problems with precision 00019 // 4/14/05 00020 // ======================================================================= 00021 00022 #include "CLHEP/Random/defs.h" 00023 #include "CLHEP/Random/RandExponential.h" 00024 #include "CLHEP/Random/DoubConv.hh" 00025 00026 namespace CLHEP { 00027 00028 std::string RandExponential::name() const {return "RandExponential";} 00029 HepRandomEngine & RandExponential::engine() {return *localEngine;} 00030 00031 RandExponential::~RandExponential() { 00032 if ( deleteEngine ) delete localEngine; 00033 } 00034 00035 RandExponential::RandExponential(const RandExponential& right) 00036 : defaultMean(right.defaultMean) 00037 {;} 00038 00039 double RandExponential::operator()() { 00040 return fire( defaultMean ); 00041 } 00042 00043 double RandExponential::operator()( double mean ) { 00044 return fire( mean ); 00045 } 00046 00047 double RandExponential::shoot() { 00048 return -log(HepRandom::getTheEngine()->flat()); 00049 } 00050 00051 double RandExponential::shoot(double mean) { 00052 return -log(HepRandom::getTheEngine()->flat())*mean; 00053 } 00054 00055 void RandExponential::shootArray( const int size, double* vect, 00056 double mean ) 00057 { 00058 int i; 00059 00060 for (i=0; i<size; ++i) 00061 vect[i] = shoot(mean); 00062 } 00063 00064 void RandExponential::shootArray(HepRandomEngine* anEngine, const int size, 00065 double* vect, double mean ) 00066 { 00067 int i; 00068 00069 for (i=0; i<size; ++i) 00070 vect[i] = shoot(anEngine, mean); 00071 } 00072 00073 void RandExponential::fireArray( const int size, double* vect) 00074 { 00075 int i; 00076 00077 for (i=0; i<size; ++i) 00078 vect[i] = fire( defaultMean ); 00079 } 00080 00081 void RandExponential::fireArray( const int size, double* vect, 00082 double mean ) 00083 { 00084 int i; 00085 00086 for (i=0; i<size; ++i) 00087 vect[i] = fire( mean ); 00088 } 00089 00090 std::ostream & RandExponential::put ( std::ostream & os ) const { 00091 int pr=os.precision(20); 00092 std::vector<unsigned long> t(2); 00093 os << " " << name() << "\n"; 00094 os << "Uvec" << "\n"; 00095 t = DoubConv::dto2longs(defaultMean); 00096 os << defaultMean << " " << t[0] << " " << t[1] << "\n"; 00097 os.precision(pr); 00098 return os; 00099 #ifdef REMOVED 00100 int pr=os.precision(20); 00101 os << " " << name() << "\n"; 00102 os << defaultMean << "\n"; 00103 os.precision(pr); 00104 return os; 00105 #endif 00106 } 00107 00108 std::istream & RandExponential::get ( std::istream & is ) { 00109 std::string inName; 00110 is >> inName; 00111 if (inName != name()) { 00112 is.clear(std::ios::badbit | is.rdstate()); 00113 std::cerr << "Mismatch when expecting to read state of a " 00114 << name() << " distribution\n" 00115 << "Name found was " << inName 00116 << "\nistream is left in the badbit state\n"; 00117 return is; 00118 } 00119 if (possibleKeywordInput(is, "Uvec", defaultMean)) { 00120 std::vector<unsigned long> t(2); 00121 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t); 00122 return is; 00123 } 00124 // is >> defaultMean encompassed by possibleKeywordInput 00125 return is; 00126 } 00127 00128 00129 } // namespace CLHEP