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