Skip to content
Snippets Groups Projects
Random.py 1.76 KiB
Newer Older
"""Helper object for random number generator objects"""

from __future__ import absolute_import, unicode_literals
Marko Petric's avatar
Marko Petric committed
from DDSim.Helper.ConfigHelper import ConfigHelper
import logging

logger = logging.getLogger(__name__)
Marko Petric's avatar
Marko Petric committed

class Random (ConfigHelper):
  """Properties for the random number generator"""
Marko Petric's avatar
Marko Petric committed

  def __init__(self):
    super(Random, self).__init__()
    self.seed = None
    self.type = None
    self.luxury = 1
    self.replace_gRandom = True
    self.file = None
Marko Petric's avatar
Marko Petric committed
    self._enableEventSeed_EXTRA = {'help': "If True, calculate random seed for each event based"
                                           "on eventID and runID\nAllows reproducibility even when"
    self.enableEventSeed = False

  def initialize(self, DDG4, kernel, output):
    """ initialize the random generator

    :param DDG4: DDG4 module
    :param kernel: Geant4 kernel
    :param int output: output level
    :returns: Geant4Random instance
    """
    if self._random:
      return self._random
Marko Petric's avatar
Marko Petric committed
    self._random = DDG4.Action(kernel, 'Geant4Random/R1')
Marko Petric's avatar
Marko Petric committed
      # System provided random source, truely random according to documentation
      self.seed = random.SystemRandom().randint(0, 2**31 - 1)
      logger.info("Choosing random seed for you: %s", self.seed)

    self._random.Seed = self.seed
    self._random.Luxury = self.luxury

    if self.type is not None:
      self._random.Type = self.type

    self._random.initialize()

    if self.seed is not None and self.enableEventSeed:
Marko Petric's avatar
Marko Petric committed
      self._eventseed = DDG4.RunAction(kernel, 'Geant4EventSeed/EventSeeder1')
Marko Petric's avatar
Marko Petric committed
    # Needs to be called after initilisation
    if output <= 3:
      self._random.showStatus()
    return self._random