| # Gaudi Framework, version v23r0 |

Home |  | Generated: Mon Jan 30 2012

# 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 // Handle CLHEP 2.0.x move to CLHEP namespace
    00052 namespace CLHEP { }
    00053 using namespace CLHEP;
    00054
    00055 namespace HepRndm  {
    00056
    00057   // Standard constructor
    00058   template <class TYPE> Engine<TYPE>::Engine(const std::string& nam, ISvcLocator* loc)
    00059   : BaseEngine (nam, loc)    {
    00060     declareProperty("Seeds",       m_seeds);
    00061     declareProperty("Column",      m_col = 0);
    00062     declareProperty("Row",         m_row = 1);
    00063     declareProperty("Luxury",      m_lux = 3);
    00064     declareProperty("UseTable",    m_useTable     = false);
    00065     declareProperty("SetSingleton",m_setSingleton = false);
    00066   }
    00067
    00068   // Standard destructor
    00069   template <class TYPE> Engine<TYPE>::~Engine()          {
    00070   }
    00071
    00072   // Initialize engine
    00073   template <class TYPE> StatusCode Engine<TYPE>::initialize()          {
    00074     m_seeds.erase(m_seeds.begin(), m_seeds.end());
    00075     StatusCode status = RndmEngine::initialize();
    00076     if ( m_seeds.size() == 0 )  {
    00077       // Default seeds
    00078       long theSeed = 1234567;
    00079       m_seeds.push_back(theSeed);
    00080       m_seeds.push_back(0);
    00081     }
    00082     MsgStream log(msgSvc(), name());
    00083     if ( status.isSuccess() )   {
    00084       status = initializeEngine();
    00085       if ( status.isSuccess() )   {
    00086         log << MSG::INFO << "Generator engine type:"
    00087                   << System::typeinfoName(typeid(TYPE))
    00088                   << endmsg;
    00089         if ( m_useTable )   {
    00090           if ( m_row > 214 || m_col > 1 )   {
    00091             log << MSG::ERROR << "Generator engine seed table has dimension [215][2], you gave:"
    00092                 << " Row=" << m_row << " Column:" << m_col << endmsg;
    00093             status = StatusCode::FAILURE;
    00094           }
    00095           else    {
    00096             log << MSG::INFO << "Generator engine seeds from table."
    00097                 << " Row=" << m_row << " Column:" << m_col << endmsg;
    00098           }
    00099         }
    00100         log << "Current Seed:" << m_hepEngine->getSeed();
    00101         log << " Luxury:" << m_lux;
    00102         log << endmsg;
    00103         // Use the default static engine if required (e.g. for GEANT4)
    00104         if ( m_setSingleton )   {
    00105             HepRandom::setTheEngine(m_hepEngine);
    00106           log << "This is the GEANT4 engine!" << endmsg;
    00107         }
    00108         return status;
    00109       }
    00110     }
    00111     log << MSG::ERROR << "Cannot initialze random engine of type:"
    00112           << System::typeinfoName(typeid(TYPE))
    00113         << endmsg;
    00114     return status;
    00115   }
    00116
    00117   // Finalize engine
    00118   template <class TYPE> StatusCode Engine<TYPE>::finalize()          {
    00119     if ( m_hepEngine )   {
    00120       HepRandom::setTheEngine(0);
    00121       delete m_hepEngine;
    00122     }
    00123     m_seeds.clear();
    00124     m_hepEngine = 0;
    00125     return RndmEngine::finalize();
    00126   }
    00127
    00128   // Retrieve single random number
    00129   template <class TYPE> double Engine<TYPE>::rndm() const  {
    00130     return m_hepEngine->flat();
    00131   }
    00132
    00133   // Retrieve seeds
    00134   template <class TYPE> StatusCode Engine<TYPE>::setSeeds(const std::vector<long>& seed)   {
    00135     typedef std::vector<long> seed_t;
    00136     m_seeds.clear();
    00137     for ( seed_t::const_iterator i = seed.begin(); i < seed.end(); i++ )   {
    00138       m_seeds.push_back(*i);
    00139     }
    00140     if ( m_seeds.size() > 0 )   {
    00141       if ( m_seeds.back() != 0 )    {
    00142         m_seeds.push_back(0);
    00143       }
    00144       m_hepEngine->setSeeds(&m_seeds[0], m_lux);
    00145       return StatusCode::SUCCESS;
    00146     }
    00147     return StatusCode::FAILURE;
    00148   }
    00149
    00150   // Retrieve seeds
    00151   template <class TYPE> StatusCode Engine<TYPE>::seeds(std::vector<long>& seed)  const  {
    00152     /*
    00153     const long *s = m_hepEngine->getSeeds();
    00154     for ( size_t i = 0; i < NUMBER_OF_SEEDS; i++ )   {
    00155       seed.push_back(s[i]);
    00156       if ( m_seeds.size() > i )
    00157         m_seeds[i] = s[i];
    00158       else
    00159         m_seeds.push_back(s[i]);
    00160     }
    00161     */
    00162     seed.push_back(m_hepEngine->getSeed());
    00163     return StatusCode::SUCCESS;
    00164   }
    00165
    00166   // Specialized shoot function for DualRand engine
    00167   template <> StatusCode Engine<DualRand>::initializeEngine()  {
    00168     m_hepEngine = (m_useTable) ? new DualRand(m_row, m_col) : new DualRand(m_seeds[0]);
    00169     return StatusCode::SUCCESS;
    00170   }
    00171   // Specialized shoot function for TripleRand engine
    00172   template <> StatusCode Engine<TripleRand>::initializeEngine()  {
    00173     m_hepEngine = (m_useTable) ? new TripleRand(m_row, m_col) : new TripleRand(m_seeds[0]);
    00174     return StatusCode::SUCCESS;
    00175   }
    00176   // Specialized shoot function for DRand48Engine
    00177   template <> StatusCode Engine<DRand48Engine>::initializeEngine()  {
    00178     m_hepEngine = (m_useTable) ? new DRand48Engine(m_row, m_col) : new DRand48Engine(m_seeds[0]);
    00179     return StatusCode::SUCCESS;
    00180   }
    00181   // Specialized shoot function for Hurd160Engine
    00182   template <> StatusCode Engine<Hurd160Engine>::initializeEngine()  {
    00183     m_hepEngine = (m_useTable) ? new Hurd160Engine(m_row, m_col) : new Hurd160Engine(m_seeds[0]);
    00184     return StatusCode::SUCCESS;
    00185   }
    00186   // Specialized shoot function for Hurd288Engine
    00187   template <> StatusCode Engine<Hurd288Engine>::initializeEngine()  {
    00188     m_hepEngine = (m_useTable) ? new Hurd288Engine(m_row, m_col) : new Hurd288Engine(m_seeds[0]);
    00189     return StatusCode::SUCCESS;
    00190   }
    00191   // Specialized shoot function for RanecuEngine
    00192   template <> StatusCode Engine<RanecuEngine>::initializeEngine()  {
    00193     m_hepEngine = (m_useTable) ? new RanecuEngine(m_row) : new RanecuEngine(m_seeds[0]);
    00194     return StatusCode::SUCCESS;
    00195   }
    00196   // Specialized shoot function for RanshiEngine
    00197   template <> StatusCode Engine<RanshiEngine>::initializeEngine()  {
    00198     m_hepEngine = (m_useTable) ? new RanshiEngine(m_row, m_col) : new RanshiEngine(m_seeds[0]);
    00199     return StatusCode::SUCCESS;
    00200   }
    00201   // Specialized shoot function for RanluxEngine
    00202   template <> StatusCode Engine<RanluxEngine>::initializeEngine()  {
    00203     m_hepEngine = (m_useTable) ? new RanluxEngine(m_row, m_col, m_lux) : new RanluxEngine(m_seeds[0], m_lux);
    00204     return StatusCode::SUCCESS;
    00205   }
    00206   // Specialized shoot function for Ranlux64Engine
    00207   template <> StatusCode Engine<Ranlux64Engine>::initializeEngine()  {
    00208     m_hepEngine = (m_useTable) ? new Ranlux64Engine(m_row, m_col, m_lux) : new Ranlux64Engine(m_seeds[0], m_lux);
    00209     return StatusCode::SUCCESS;
    00210   }
    00211   // Specialized shoot function for MTwistEngine
    00212   template <> StatusCode Engine<MTwistEngine>::initializeEngine()  {
    00213     m_hepEngine = (m_useTable) ? new MTwistEngine(m_row, m_col) : new MTwistEngine(m_seeds[0]);
    00214     return StatusCode::SUCCESS;
    00215   }
    00216   // Specialized shoot function for HepJamesRandom
    00217   template <> StatusCode Engine<HepJamesRandom>::initializeEngine()  {
    00218     m_hepEngine = (m_useTable) ? new HepJamesRandom(m_row, m_col) : new HepJamesRandom(m_seeds[0]);
    00219     return StatusCode::SUCCESS;
    00220   }
    00221
    00222 }
    00223
    00224 typedef HepRndm::Engine<DualRand> e1; DECLARE_SERVICE_FACTORY(e1)
    00225 typedef HepRndm::Engine<TripleRand> e2; DECLARE_SERVICE_FACTORY(e2)
    00226 typedef HepRndm::Engine<DRand48Engine> e3; DECLARE_SERVICE_FACTORY(e3)
    00227 typedef HepRndm::Engine<Hurd160Engine> e4; DECLARE_SERVICE_FACTORY(e4)
    00228 typedef HepRndm::Engine<Hurd288Engine> e5; DECLARE_SERVICE_FACTORY(e5)
    00229 typedef HepRndm::Engine<HepJamesRandom> e6; DECLARE_SERVICE_FACTORY(e6)
    00230 typedef HepRndm::Engine<MTwistEngine> e7; DECLARE_SERVICE_FACTORY(e7)
    00231 typedef HepRndm::Engine<RanecuEngine> e8; DECLARE_SERVICE_FACTORY(e8)
    00232 typedef HepRndm::Engine<Ranlux64Engine> e9; DECLARE_SERVICE_FACTORY(e9)
    00233 typedef HepRndm::Engine<RanluxEngine> e10; DECLARE_SERVICE_FACTORY(e10)
    00234 typedef HepRndm::Engine<RanshiEngine> e11; DECLARE_SERVICE_FACTORY(e11)
    00235
    00236

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines