Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:22

0001 """Helper object for random number generator objects"""
0002 
0003 from DDSim.Helper.ConfigHelper import ConfigHelper
0004 import random
0005 import logging
0006 
0007 logger = logging.getLogger(__name__)
0008 
0009 
0010 class Random (ConfigHelper):
0011   """Properties for the random number generator"""
0012 
0013   def __init__(self):
0014     super(Random, self).__init__()
0015     self.seed = None
0016     self.type = None
0017     self.luxury = 1
0018     self.replace_gRandom = True
0019     self.file = None
0020     self._random = None
0021 
0022     self._enableEventSeed_EXTRA = {'help': "If True, calculate random seed for each event based"
0023                                            "on eventID and runID\nAllows reproducibility even when"
0024                                            "SkippingEvents"}
0025     self.enableEventSeed = False
0026     self._closeProperties()
0027 
0028   def initialize(self, DDG4, kernel, output):
0029     """ initialize the random generator
0030 
0031     :param DDG4: DDG4 module
0032     :param kernel: Geant4 kernel
0033     :param int output: output level
0034     :returns: Geant4Random instance
0035     """
0036     if self._random:
0037       return self._random
0038     self._random = DDG4.Action(kernel, 'Geant4Random/R1')
0039 
0040     if self.seed is None:
0041       # System provided random source, truely random according to documentation
0042       self.seed = random.SystemRandom().randint(0, 2**31 - 1)
0043       logger.info("Choosing random seed for you: %s", self.seed)
0044 
0045     self._random.Seed = self.seed
0046     self._random.Luxury = self.luxury
0047 
0048     if self.type is not None:
0049       self._random.Type = self.type
0050 
0051     self._random.initialize()
0052 
0053     if self.seed is not None and self.enableEventSeed:
0054       self._eventseed = DDG4.RunAction(kernel, 'Geant4EventSeed/EventSeeder1')
0055 
0056     # Needs to be called after initilisation
0057     if output <= 3:
0058       self._random.showStatus()
0059     return self._random