RandExponential.cc

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

Generated on Mon May 27 17:50:33 2013 for Geant4 by  doxygen 1.4.7