| # Gaudi Framework, version v21r9 |

Home |  | Generated: 3 May 2010

# HepRndmEngines.cpp

Go to the documentation of this file.

    00001 //====================================================================
    00002 //      Random Engine implementations
    00003 //--------------------------------------------------------------------
    00004 //
    00005 //      Package    : HepRndm ( The LHCb Offline System)
    00006 //      Author     : M.Frank
    00007 //  History    :
    00008 // +---------+----------------------------------------------+---------
    00009 // |    Date |                 Comment                      | Who
    00010 // +---------+----------------------------------------------+---------
    00011 // | 29/10/99| Initial version                              | MF
    00012 // +---------+----------------------------------------------+---------
    00013 //
    00014 //  Note: Currently all engines require only ONE seed
    00015 //        We aill return only the first number, because
    00016 //        the implementation in CLHEP does not allow to
    00017 //        determine the proper number of seeds used.
    00018 #define NUMBER_OF_SEEDS    1
    00019 //
    00020 //====================================================================
    00021 #define HEPRNDM_HEPRNDMENGINES_CPP
    00022
    00023 // STL include files
    00024 #include <iostream>
    00025 #include <cfloat>
    00026 #include <ctime>
    00027 #include <cmath>
    00028
    00029 // Framework include files
    00030 #include "GaudiKernel/SvcFactory.h"
    00031 #include "GaudiKernel/System.h"
    00032 #include "GaudiKernel/MsgStream.h"
    00033 #include "GaudiKernel/IIncidentSvc.h"
    00034
    00035 #include "RndmGenSvc.h"
    00036 #include "HepRndmEngine.h"
    00037
    00038 #include "CLHEP/Random/Random.h"
    00039 #include "CLHEP/Random/DualRand.h"
    00040 #include "CLHEP/Random/TripleRand.h"
    00041 #include "CLHEP/Random/DRand48Engine.h"
    00042 #include "CLHEP/Random/Hurd160Engine.h"
    00043 #include "CLHEP/Random/Hurd288Engine.h"
    00044 #include "CLHEP/Random/JamesRandom.h"
    00045 #include "CLHEP/Random/MTwistEngine.h"
    00046 #include "CLHEP/Random/RanecuEngine.h"
    00047 #include "CLHEP/Random/Ranlux64Engine.h"
    00048 #include "CLHEP/Random/RanluxEngine.h"
    00049 #include "CLHEP/Random/RanshiEngine.h"
    00050
    00051 namespace HepRndm  {
    00052
    00053   // Standard constructor
    00054   template <class TYPE> Engine<TYPE>::Engine(const std::string& nam, ISvcLocator* loc)
    00055   : BaseEngine (nam, loc)    {
    00056     declareProperty("Seeds",       m_seeds);
    00057     declareProperty("Column",      m_col = 0);
    00058     declareProperty("Row",         m_row = 1);
    00059     declareProperty("Luxury",      m_lux = 3);
    00060     declareProperty("UseTable",    m_useTable     = false);
    00061     declareProperty("SetSingleton",m_setSingleton = false);
    00062   }
    00063
    00064   // Standard destructor
    00065   template <class TYPE> Engine<TYPE>::~Engine()          {
    00066   }
    00067
    00068   // Initialize engine
    00069   template <class TYPE> StatusCode Engine<TYPE>::initialize()          {
    00070     m_seeds.erase(m_seeds.begin(), m_seeds.end());
    00071     StatusCode status = RndmEngine::initialize();
    00072     if ( m_seeds.size() == 0 )  {
    00073       // Default seeds
    00074       long theSeed = 1234567;
    00075       m_seeds.push_back(theSeed);
    00076       m_seeds.push_back(0);
    00077     }
    00078     MsgStream log(msgSvc(), name());
    00079     if ( status.isSuccess() )   {
    00080       status = initializeEngine();
    00081       if ( status.isSuccess() )   {
    00082         log << MSG::INFO << "Generator engine type:"
    00083                   << System::typeinfoName(typeid(TYPE))
    00084                   << endmsg;
    00085         if ( m_useTable )   {
    00086           if ( m_row > 214 || m_col > 1 )   {
    00087             log << MSG::ERROR << "Generator engine seed table has dimension [215][2], you gave:"
    00088                 << " Row=" << m_row << " Column:" << m_col << endmsg;
    00089             status = StatusCode::FAILURE;
    00090           }
    00091           else    {
    00092             log << MSG::INFO << "Generator engine seeds from table."
    00093                 << " Row=" << m_row << " Column:" << m_col << endmsg;
    00094           }
    00095         }
    00096         log << "Current Seed:" << m_hepEngine->getSeed();
    00097         log << " Luxury:" << m_lux;
    00098         log << endmsg;
    00099         // Use the default static engine if required (e.g. for GEANT4)
    00100         if ( m_setSingleton )   {
    00101           HepRandom::setTheEngine(m_hepEngine);
    00102           log << "This is the GEANT4 engine!" << endmsg;
    00103         }
    00104         return status;
    00105       }
    00106     }
    00107     log << MSG::ERROR << "Cannot initialze random engine of type:"
    00108           << System::typeinfoName(typeid(TYPE))
    00109         << endmsg;
    00110     return status;
    00111   }
    00112
    00113   // Finalize engine
    00114   template <class TYPE> StatusCode Engine<TYPE>::finalize()          {
    00115     if ( m_hepEngine )   {
    00116       HepRandom::setTheEngine(0);
    00117       delete m_hepEngine;
    00118     }
    00119     m_seeds.clear();
    00120     m_hepEngine = 0;
    00121     return RndmEngine::finalize();
    00122   }
    00123
    00124   // Retrieve single random number
    00125   template <class TYPE> double Engine<TYPE>::rndm() const  {
    00126     return m_hepEngine->flat();
    00127   }
    00128
    00129   // Retrieve seeds
    00130   template <class TYPE> StatusCode Engine<TYPE>::setSeeds(const std::vector<long>& seed)   {
    00131     typedef std::vector<long> seed_t;
    00132     m_seeds.clear();
    00133     for ( seed_t::const_iterator i = seed.begin(); i < seed.end(); i++ )   {
    00134       m_seeds.push_back(*i);
    00135     }
    00136     if ( m_seeds.size() > 0 )   {
    00137       if ( m_seeds.back() != 0 )    {
    00138         m_seeds.push_back(0);
    00139       }
    00140       m_hepEngine->setSeeds(&m_seeds[0], m_lux);
    00141       return StatusCode::SUCCESS;
    00142     }
    00143     return StatusCode::FAILURE;
    00144   }
    00145
    00146   // Retrieve seeds
    00147   template <class TYPE> StatusCode Engine<TYPE>::seeds(std::vector<long>& seed)  const  {
    00148     /*
    00149     const long *s = m_hepEngine->getSeeds();
    00150     for ( size_t i = 0; i < NUMBER_OF_SEEDS; i++ )   {
    00151       seed.push_back(s[i]);
    00152       if ( m_seeds.size() > i )
    00153         m_seeds[i] = s[i];
    00154       else
    00155         m_seeds.push_back(s[i]);
    00156     }
    00157     */
    00158     seed.push_back(m_hepEngine->getSeed());
    00159     return StatusCode::SUCCESS;
    00160   }
    00161
    00162   // Specialized shoot function for DualRand engine
    00163   template <> StatusCode Engine<DualRand>::initializeEngine()  {
    00164     m_hepEngine = (m_useTable) ? new DualRand(m_row, m_col) : new DualRand(m_seeds[0]);
    00165     return StatusCode::SUCCESS;
    00166   }
    00167   // Specialized shoot function for TripleRand engine
    00168   template <> StatusCode Engine<TripleRand>::initializeEngine()  {
    00169     m_hepEngine = (m_useTable) ? new TripleRand(m_row, m_col) : new TripleRand(m_seeds[0]);
    00170     return StatusCode::SUCCESS;
    00171   }
    00172   // Specialized shoot function for DRand48Engine
    00173   template <> StatusCode Engine<DRand48Engine>::initializeEngine()  {
    00174     m_hepEngine = (m_useTable) ? new DRand48Engine(m_row, m_col) : new DRand48Engine(m_seeds[0]);
    00175     return StatusCode::SUCCESS;
    00176   }
    00177   // Specialized shoot function for Hurd160Engine
    00178   template <> StatusCode Engine<Hurd160Engine>::initializeEngine()  {
    00179     m_hepEngine = (m_useTable) ? new Hurd160Engine(m_row, m_col) : new Hurd160Engine(m_seeds[0]);
    00180     return StatusCode::SUCCESS;
    00181   }
    00182   // Specialized shoot function for Hurd288Engine
    00183   template <> StatusCode Engine<Hurd288Engine>::initializeEngine()  {
    00184     m_hepEngine = (m_useTable) ? new Hurd288Engine(m_row, m_col) : new Hurd288Engine(m_seeds[0]);
    00185     return StatusCode::SUCCESS;
    00186   }
    00187   // Specialized shoot function for RanecuEngine
    00188   template <> StatusCode Engine<RanecuEngine>::initializeEngine()  {
    00189     m_hepEngine = (m_useTable) ? new RanecuEngine(m_row) : new RanecuEngine(m_seeds[0]);
    00190     return StatusCode::SUCCESS;
    00191   }
    00192   // Specialized shoot function for RanshiEngine
    00193   template <> StatusCode Engine<RanshiEngine>::initializeEngine()  {
    00194     m_hepEngine = (m_useTable) ? new RanshiEngine(m_row, m_col) : new RanshiEngine(m_seeds[0]);
    00195     return StatusCode::SUCCESS;
    00196   }
    00197   // Specialized shoot function for RanluxEngine
    00198   template <> StatusCode Engine<RanluxEngine>::initializeEngine()  {
    00199     m_hepEngine = (m_useTable) ? new RanluxEngine(m_row, m_col, m_lux) : new RanluxEngine(m_seeds[0], m_lux);
    00200     return StatusCode::SUCCESS;
    00201   }
    00202   // Specialized shoot function for Ranlux64Engine
    00203   template <> StatusCode Engine<Ranlux64Engine>::initializeEngine()  {
    00204     m_hepEngine = (m_useTable) ? new Ranlux64Engine(m_row, m_col, m_lux) : new Ranlux64Engine(m_seeds[0], m_lux);
    00205     return StatusCode::SUCCESS;
    00206   }
    00207   // Specialized shoot function for MTwistEngine
    00208   template <> StatusCode Engine<MTwistEngine>::initializeEngine()  {
    00209     m_hepEngine = (m_useTable) ? new MTwistEngine(m_row, m_col) : new MTwistEngine(m_seeds[0]);
    00210     return StatusCode::SUCCESS;
    00211   }
    00212   // Specialized shoot function for HepJamesRandom
    00213   template <> StatusCode Engine<HepJamesRandom>::initializeEngine()  {
    00214     m_hepEngine = (m_useTable) ? new HepJamesRandom(m_row, m_col) : new HepJamesRandom(m_seeds[0]);
    00215     return StatusCode::SUCCESS;
    00216   }
    00217
    00218 }
    00219
    00220 typedef HepRndm::Engine<DualRand> e1; DECLARE_SERVICE_FACTORY(e1)
    00221 typedef HepRndm::Engine<TripleRand> e2; DECLARE_SERVICE_FACTORY(e2)
    00222 typedef HepRndm::Engine<DRand48Engine> e3; DECLARE_SERVICE_FACTORY(e3)
    00223 typedef HepRndm::Engine<Hurd160Engine> e4; DECLARE_SERVICE_FACTORY(e4)
    00224 typedef HepRndm::Engine<Hurd288Engine> e5; DECLARE_SERVICE_FACTORY(e5)
    00225 typedef HepRndm::Engine<HepJamesRandom> e6; DECLARE_SERVICE_FACTORY(e6)
    00226 typedef HepRndm::Engine<MTwistEngine> e7; DECLARE_SERVICE_FACTORY(e7)
    00227 typedef HepRndm::Engine<RanecuEngine> e8; DECLARE_SERVICE_FACTORY(e8)
    00228 typedef HepRndm::Engine<Ranlux64Engine> e9; DECLARE_SERVICE_FACTORY(e9)
    00229 typedef HepRndm::Engine<RanluxEngine> e10; DECLARE_SERVICE_FACTORY(e10)
    00230 typedef HepRndm::Engine<RanshiEngine> e11; DECLARE_SERVICE_FACTORY(e11)
    00231
    00232