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