CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: RandExponential.cc,v 1.6 2010/06/16 17:24: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 } 00033 00034 double RandExponential::operator()() { 00035 return fire( defaultMean ); 00036 } 00037 00038 double RandExponential::operator()( double mean ) { 00039 return fire( mean ); 00040 } 00041 00042 double RandExponential::shoot() { 00043 return -std::log(HepRandom::getTheEngine()->flat()); 00044 } 00045 00046 double RandExponential::shoot(double mean) { 00047 return -std::log(HepRandom::getTheEngine()->flat())*mean; 00048 } 00049 00050 void RandExponential::shootArray( const int size, double* vect, 00051 double mean ) 00052 { 00053 for( double* v = vect; v != vect+size; ++v ) 00054 *v = shoot(mean); 00055 } 00056 00057 void RandExponential::shootArray(HepRandomEngine* anEngine, const int size, 00058 double* vect, double mean ) 00059 { 00060 for( double* v = vect; v != vect+size; ++v ) 00061 *v = shoot(anEngine, mean); 00062 } 00063 00064 void RandExponential::fireArray( const int size, double* vect) 00065 { 00066 for( double* v = vect; v != vect+size; ++v ) 00067 *v = fire( defaultMean ); 00068 } 00069 00070 void RandExponential::fireArray( const int size, double* vect, 00071 double mean ) 00072 { 00073 for( double* v = vect; v != vect+size; ++v ) 00074 *v = fire( mean ); 00075 } 00076 00077 std::ostream & RandExponential::put ( std::ostream & os ) const { 00078 int pr=os.precision(20); 00079 std::vector<unsigned long> t(2); 00080 os << " " << name() << "\n"; 00081 os << "Uvec" << "\n"; 00082 t = DoubConv::dto2longs(defaultMean); 00083 os << defaultMean << " " << t[0] << " " << t[1] << "\n"; 00084 os.precision(pr); 00085 return os; 00086 #ifdef REMOVED 00087 int pr=os.precision(20); 00088 os << " " << name() << "\n"; 00089 os << defaultMean << "\n"; 00090 os.precision(pr); 00091 return os; 00092 #endif 00093 } 00094 00095 std::istream & RandExponential::get ( std::istream & is ) { 00096 std::string inName; 00097 is >> inName; 00098 if (inName != name()) { 00099 is.clear(std::ios::badbit | is.rdstate()); 00100 std::cerr << "Mismatch when expecting to read state of a " 00101 << name() << " distribution\n" 00102 << "Name found was " << inName 00103 << "\nistream is left in the badbit state\n"; 00104 return is; 00105 } 00106 if (possibleKeywordInput(is, "Uvec", defaultMean)) { 00107 std::vector<unsigned long> t(2); 00108 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t); 00109 return is; 00110 } 00111 // is >> defaultMean encompassed by possibleKeywordInput 00112 return is; 00113 } 00114 00115 00116 } // namespace CLHEP