00001 #ifndef IOMC_RandomEngine_TRandomAdaptor_h 00002 #define IOMC_RandomEngine_TRandomAdaptor_h 00003 00004 #include "CLHEP/Random/defs.h" 00005 #include "CLHEP/Random/RandomEngine.h" 00006 00007 #include "TRandom3.h" 00008 #include "TFile.h" 00009 #include "TTree.h" 00010 00011 #include <cmath> 00012 00013 namespace edm { 00014 00015 class TRandomAdaptor : public CLHEP::HepRandomEngine { 00016 00017 public: 00018 00019 // Constructors and destructor. 00020 TRandomAdaptor() : trand_(new TRandom3()) {} 00021 TRandomAdaptor( long seed ) : trand_(new TRandom3(seed)) {} 00022 TRandomAdaptor( int rowIndex, int colIndex ) : trand_(new TRandom3(rowIndex*colIndex-1)) {} 00023 TRandomAdaptor( std::istream & is ); 00024 virtual ~TRandomAdaptor() { delete trand_; } 00025 00026 // Copy constructor and operator=. 00027 TRandomAdaptor( const TRandomAdaptor & p ) { TRandom3* tmp = new TRandom3(); 00028 *tmp = *(p.trand_); 00029 this->trand_ = tmp; } 00030 00031 TRandomAdaptor & operator=( const TRandomAdaptor & p ) { TRandom3* tmp = new TRandom3(); 00032 *tmp = *(p.trand_); 00033 this->trand_ = tmp; 00034 return *this; } 00035 00036 // Returns a pseudo random number in ]0,1[ (i. e., excluding the end points). 00037 double flat() { return trand_->Rndm(); } 00038 00039 // Fills an array "vect" of specified size with flat random values. 00040 void flatArray(const int size, double* vect) { trand_->RndmArray(size,vect); } 00041 00042 // Sets the state of the algorithm according to seed. 00043 void setSeed(long seed, int) { trand_->SetSeed(seed); } 00044 00045 // Sets the state of the algorithm according to the zero terminated 00046 // array of seeds. It is allowed to ignore one or many seeds in this array. 00047 void setSeeds(const long * seeds, int) { trand_->SetSeed(seeds[0]); } 00048 00049 // Saves the current engine status in the named file 00050 void saveStatus( const char filename[] = "TRandom.conf") const { trand_->WriteRandom(filename); } 00051 00052 // Reads from named file the the last saved engine status and restores it. 00053 void restoreStatus( const char filename[] = "TRandom.conf" ) { trand_->ReadRandom(filename); } 00054 00055 // Dumps the current engine status on the screen. 00056 void showStatus() const { trand_->Dump(); } 00057 00058 // Returns a float flat ]0,1[ 00059 operator float() { return (float)(trand_->Rndm()); } 00060 00061 // Returns an unsigned int (32-bit) flat 00062 operator unsigned int() { return (unsigned int)((trand_->Rndm())*exponent_bit_32); } 00063 00064 virtual std::ostream & put (std::ostream & os) const; 00065 virtual std::istream & get (std::istream & is); 00066 std::string beginTag ( ) { return std::string(trand_->GetName())+std::string("-begin"); } 00067 virtual std::istream & getState ( std::istream & is ); 00068 00069 // Returns the engine name as a string 00070 std::string name() const { return std::string("T")+std::string(trand_->GetName()); } 00071 static std::string engineName() { return std::string("TRandomAdaptor"); } 00072 00073 std::vector<unsigned long> put () const; 00074 bool get (const std::vector<unsigned long> & v); 00075 bool getState (const std::vector<unsigned long> & v) { return get(v); } 00076 00077 // In case all else fails, let the user talk directly to the engine 00078 TRandom3* getRootEngine() { return trand_; } 00079 00080 private: 00081 00082 void Grumble(std::string errortext) const; 00083 00084 TRandom3* trand_; 00085 00086 }; // TRandomAdaptor 00087 00088 } // namespace edm 00089 00090 #endif // IOMC_RandomEngine_TRandomAdaptor_h