Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-20 02:14:56

0001 #ifndef IOMC_RandomEngine_TRandomAdaptor_h
0002 #define IOMC_RandomEngine_TRandomAdaptor_h
0003 
0004 #include "CLHEP/Random/RandomEngine.h"
0005 
0006 #include "TRandom3.h"
0007 
0008 #include <cmath>
0009 #include "FWCore/Utilities/interface/value_ptr.h"
0010 
0011 namespace edm {
0012 
0013   class TRandomAdaptor : public CLHEP::HepRandomEngine {
0014   public:
0015     typedef value_ptr<TRandom3> TRandom3Ptr;
0016 
0017     // Constructors and destructor.
0018     TRandomAdaptor();
0019     TRandomAdaptor(long seed);
0020     TRandomAdaptor(int rowIndex, int colIndex);
0021     TRandomAdaptor(std::istream& is);
0022     ~TRandomAdaptor() override;
0023 
0024     // Returns a pseudo random number in ]0,1[ (i. e., excluding the end points).
0025     double flat() override { return trand_->Rndm(); }
0026 
0027     // Fills an array "vect" of specified size with flat random values.
0028     void flatArray(int const size, double* vect) override { trand_->RndmArray(size, vect); }
0029 
0030     // Sets the state of the algorithm according to seed.
0031     void setSeed(long seed, int) override;
0032 
0033     // Sets the state of the algorithm according to the zero terminated
0034     // array of seeds. It is allowed to ignore one or many seeds in this array.
0035     void setSeeds(long const* seeds, int) override;
0036 
0037     // Saves the current engine status in the named file
0038     void saveStatus(char const filename[] = "TRandom.conf") const override { trand_->WriteRandom(filename); }
0039 
0040     // Reads from named file the the last saved engine status and restores it.
0041     void restoreStatus(char const filename[] = "TRandom.conf") override { trand_->ReadRandom(filename); }
0042 
0043     // Dumps the current engine status on the screen.
0044     void showStatus() const override { trand_->Dump(); }
0045 
0046     // Returns a float flat ]0,1[
0047     operator float() override { return (float)(trand_->Rndm()); }
0048 
0049     // Returns an unsigned int (32-bit) flat
0050     operator unsigned int() override { return (unsigned int)((trand_->Rndm()) * exponent_bit_32()); }
0051 
0052     std::ostream& put(std::ostream& os) const override;
0053     std::istream& get(std::istream& is) override;
0054     std::string beginTag() { return std::string(trand_->GetName()) + std::string("-begin"); }
0055     std::istream& getState(std::istream& is) override;
0056 
0057     // Returns the engine name as a string
0058     std::string name() const override { return std::string("T") + std::string(trand_->GetName()); }
0059     static std::string engineName() { return std::string("TRandomAdaptor"); }
0060 
0061     std::vector<unsigned long> put() const override;
0062     bool get(std::vector<unsigned long> const& v) override;
0063     bool getState(std::vector<unsigned long> const& v) override { return get(v); }
0064 
0065     // In case all else fails, let the user talk directly to the engine
0066     TRandom3* getRootEngine() { return trand_.operator->(); }
0067 
0068   private:
0069     void Grumble(std::string const& errortext) const;
0070 
0071     mutable TRandom3Ptr trand_;
0072 
0073   };  // TRandomAdaptor
0074 
0075 }  // namespace edm
0076 
0077 #endif  // IOMC_RandomEngine_TRandomAdaptor_h