diff --git a/Simulation/G4Atlas/G4AtlasApps/python/G4Atlas_Metadata.py b/Simulation/G4Atlas/G4AtlasApps/python/G4Atlas_Metadata.py new file mode 100644 index 0000000000000000000000000000000000000000..aa0c63a8e55ffd5a7b78fa17a87c4a5923526f24 --- /dev/null +++ b/Simulation/G4Atlas/G4AtlasApps/python/G4Atlas_Metadata.py @@ -0,0 +1,297 @@ +# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + +### This module contains functions which may need to peek at the input file metadata + +## Get the logger +from AthenaCommon.Logging import * +simMDlog = logging.getLogger('Sim_Metadata') + +def getAthFile(): + ## Allow the input check to be skipped. This should only be done in production + ## jobs, in order to avoid peeking and spoiling performance on some systems + inputAthFileObject = None + import os + if not ('G4ATLAS_SKIPFILEPEEK' in os.environ and os.environ['G4ATLAS_SKIPFILEPEEK']): + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags + if athenaCommonFlags.PoolEvgenInput.statusOn: + try: + import PyUtils.AthFile as af + inputAthFileObject = af.fopen(athenaCommonFlags.PoolEvgenInput()[0]) + except: + simMDlog.warning("AthFile failed to open %s", athenaCommonFlags.PoolEvgenInput()[0]) + else: + simMDlog.info("G4ATLAS_SKIPFILEPEEK environment variable present, so skipping all input file peeking.") + return inputAthFileObject + +inputAthFileObject = getAthFile() + + +### Check that we aren't trying to pass a data file as the input to the simulation! +def inputFileValidityCheck(): + from G4AtlasApps.G4Atlas_Metadata import inputAthFileObject + if inputAthFileObject is not None: + ## Check that event type is SIMULATION (as it must be) + if "evt_type" in inputAthFileObject.infos.keys(): + evttypes = inputAthFileObject.infos["evt_type"] + evttype0 = str(evttypes[0]) + if not evttype0.startswith("IS_SIMULATION"): + msg = "This input file has incorrect evt_type: %s\n" % str(evttypes) + msg += "Please make sure you have set input file metadata correctly - " + msg += "consider using the job transforms for earlier steps if you aren't already doing so." + simMDlog.fatal(msg) + raise SystemExit("Input file evt_type is incorrect: please check your evgen jobs.") + else: + simMDlog.warning("Could not find 'evt_type' key in athfile.infos. Unable to that check evt_type is correct.") + else: + simMDlog.info("No input Evgen AthFile object available, so skipping check for input file validity.") + +### Read in special simulation job option fragments based on metadata passed by the evgen stage +def checkForSpecialConfigurationMetadata(): + from G4AtlasApps.G4Atlas_Metadata import inputAthFileObject + if inputAthFileObject is not None: + if "specialConfiguration" in inputAthFileObject.infos["tag_info"]: + from G4AtlasApps.SimFlags import simFlags + simFlags.specialConfiguration = dict() + item = inputAthFileObject.infos["tag_info"]["specialConfiguration"] + ## Parse the specialConfiguration string + ## Format is 'key1=value1;key2=value2;...'. or just ' + spcitems = item.split(";") + params = {} + someIncludes=[] + for spcitem in spcitems: + #print spcitem + ## Ignore empty or "NONE" substrings, e.g. from consecutive or trailing semicolons + if not spcitem or spcitem.upper() == "NONE": + continue + ## If not in key=value format, treat as v, with k="preInclude" + if "=" not in spcitem: + spcitem = "preInclude=" + spcitem + ## Handle k=v directives + k, v = spcitem.split("=") + if k == "preInclude": + someIncludes+=[v] + else: + params[k] = v + simMDlog.info(params) + simFlags.specialConfiguration.get_Value().update(params) + from AthenaCommon.Include import include + for inc in someIncludes: + include(inc) + else: + simMDlog.info("No input Evgen AthFile object available so skipping check for specialConfiguration metadata.") + +def fillCommonMetadata(dbFiller): + pass + +def fillAtlasMetadata(dbFiller): + ## Write sim parameters as metadata keys + from AthenaCommon.JobProperties import JobProperty + from G4AtlasApps.SimFlags import simFlags + simParams = [sf for sf in dir(simFlags) if isinstance(getattr(simFlags, sf), JobProperty)] + for sp in simParams: + ## Don't write out random number seeds or RunDict as metadata + if sp in ("RandomSeedList", "RandomSeedOffset", "RunDict"): + continue + ## Only store InitFunction names + if sp in ("InitFunctions"): + initfuncdict = dict() + for level in getattr(simFlags, sp).get_Value(): + initfuncdict.setdefault(level, []) + for element in getattr(simFlags, sp).get_Value()[level]: + initfuncdict[level].append(element.__name__) + testValue = str( initfuncdict ) if getattr(simFlags, sp).statusOn else 'default' + dbFiller.addSimParam(sp, testValue) + continue + testValue = str( getattr(simFlags, sp).get_Value() ) if getattr(simFlags, sp).statusOn else 'default' + dbFiller.addSimParam(sp, testValue) + import os + dbFiller.addSimParam('G4Version', str(os.environ['G4VERS'])) + dbFiller.addSimParam('RunType', 'atlas') + from AthenaCommon.BeamFlags import jobproperties + dbFiller.addSimParam('beamType', jobproperties.Beam.beamType.get_Value()) + + ## Simulated detector flags: add each enabled detector to the simulatedDetectors list + from AthenaCommon.DetFlags import DetFlags + simDets = [] + for det in ['pixel','SCT','TRT','BCM','DBM','Lucid','FwdRegion','ZDC','ALFA','AFP','LAr','HGTD','Tile','MDT','CSC','TGC','RPC','Micromegas','sTGC','Truth']: + attrname = det+"_on" + checkfn = getattr(DetFlags.geometry, attrname, None) + if checkfn is None: + continue + if checkfn(): + simDets.append(det) + dbFiller.addSimParam('SimulatedDetectors', repr(simDets)) + + ## Hard-coded simulation hit file magic number (for major changes) + dbFiller.addSimParam('hitFileMagicNumber', '0') ##FIXME Remove this? + +def fillTestBeamMetadata(dbFiller): + dbFiller.addSimParam('RunType','testbeam') + SimParams = ['CalibrationRun','DoLArBirk','LArParameterization', + 'MagneticField','PhysicsList','Seeds','SeedsG4','SimLayout', + 'WorldZRange','NeutronTimeCut','NeutronEnergyCut','ApplyEMCuts','RunNumber'] + from G4AtlasApps.SimFlags import simFlags + for o in [ o for o in SimParams if simFlags.__dict__.keys().__contains__(o) ]: + testValue = 'default' + if (simFlags.__dict__.__contains__(o) and simFlags.__dict__.get(o).statusOn) : + testValue = simFlags.__dict__.get(o).get_Value() + if not isinstance(testValue, str): + testValue = str(simFlags.__dict__.get(o).get_Value()) + dbFiller.addSimParam(o, testValue) + if (simFlags.__dict__.__contains__('EventFilter') and simFlags.EventFilter.statusOn) : + dbFiller.addSimParam('EtaPhiStatus', str(simFlags.EventFilter.get_Value()['EtaPhiFilters'])) + dbFiller.addSimParam('VRangeStatus', str(simFlags.EventFilter.get_Value()['VertexRangeChecker'])) + else : + dbFiller.addSimParam('EtaPhiStatus', 'default') + dbFiller.addSimParam('VRangeStatus', 'default') + import os + dbFiller.addSimParam('G4Version', str(os.environ['G4VERS'])) + from AthenaCommon.BeamFlags import jobproperties + dbFiller.addSimParam('beamType',jobproperties.Beam.beamType.get_Value()) + + ####### Hard coded simulation hit file magic number (for major changes) ###### + dbFiller.addSimParam('hitFileMagicNumber','0') + +def fillISFMetadata(dbFiller): + from ISF_Config.ISF_jobProperties import ISF_Flags + dbFiller.addSimParam('Simulator', ISF_Flags.Simulator()) + dbFiller.addSimParam('TruthStrategy', ISF_Flags.TruthStrategy()) + +def createSimulationParametersMetadata(): + from IOVDbMetaDataTools import ParameterDbFiller + dbFiller = ParameterDbFiller.ParameterDbFiller() + from G4AtlasApps.G4Atlas_Metadata import inputAthFileObject + ## Set run numbers + minrunnum = 0 + maxrunnum = 2147483647 # MAX + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags + from G4AtlasApps.SimFlags import simFlags + if hasattr(simFlags, 'RunNumber') and simFlags.RunNumber.statusOn: + minrunnum = simFlags.RunNumber() + ## FIXME need to use maxrunnum = 2147483647 for now to keep overlay working but in the future this should be set properly. + #maxrunnum = minrunnum + 1 + elif inputAthFileObject is not None: + if len(inputAthFileObject.run_numbers) > 0: + minrunnum = inputAthFileObject.run_numbers[0] + maxrunnum = minrunnum + 1 + else: + raise Exception('IllegalRunNumber') + else: + simMDlog.info('Skipping run number setting - would need to set simFlags.RunNumber for this.') + simMDlog.info("Using the following run number range for MetaData IOV: ("+str(minrunnum)+","+str(maxrunnum)+").") + dbFiller.setBeginRun(minrunnum) + dbFiller.setEndRun(maxrunnum) + + fillAtlasMetadata(dbFiller) + if simFlags.ISFRun: + fillISFMetadata(dbFiller) + + ## Write the db info + dbFiller.genSimDb() + folder = "/Simulation/Parameters" + dbConnection = "sqlite://;schema=SimParams.db;dbname=SIMPARAM" + import IOVDbSvc.IOVDb + from AthenaCommon.AppMgr import ServiceMgr + ServiceMgr.IOVDbSvc.Folders += [ folder + "<dbConnection>" + dbConnection + "</dbConnection>" ] + ServiceMgr.IOVDbSvc.FoldersToMetaData += [folder] + ServiceMgr.IOVSvc.partialPreLoadData = True + +def createTBSimulationParametersMetadata(): + from IOVDbMetaDataTools import ParameterDbFiller + dbFiller = ParameterDbFiller.ParameterDbFiller() + from G4AtlasApps.G4Atlas_Metadata import inputAthFileObject + ## Set run numbers + minrunnum = 0 + maxrunnum = 2147483647 # MAX + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags + from G4AtlasApps.SimFlags import simFlags + if hasattr(simFlags, 'RunNumber') and simFlags.RunNumber.statusOn: + minrunnum = simFlags.RunNumber() + ## FIXME need to use maxrunnum = 2147483647 for now to keep overlay working but in the future this should be set properly. + #maxrunnum = minrunnum + 1 + elif inputAthFileObject is not None: + if len(inputAthFileObject.run_numbers) > 0: + minrunnum = inputAthFileObject.run_numbers[0] + maxrunnum = minrunnum + 1 + else: + raise Exception('IllegalRunNumber') + else: + simMDlog.info('Skipping run number setting - would need to set simFlags.RunNumber for this.') + simMDlog.info("Using the following run number range for MetaData IOV: ("+str(minrunnum)+","+str(maxrunnum)+").") + dbFiller.setBeginRun(minrunnum) + dbFiller.setEndRun(maxrunnum) + + fillTestBeamMetadata(dbFiller) + if simFlags.ISFRun: + fillISFMetadata(dbFiller) + + ## Write the db info + dbFiller.genSimDb() + folder = "/Simulation/Parameters" + dbConnection = "sqlite://;schema=SimParams.db;dbname=SIMPARAM" + import IOVDbSvc.IOVDb + from AthenaCommon.AppMgr import ServiceMgr + ServiceMgr.IOVDbSvc.Folders += [ folder + "<dbConnection>" + dbConnection + "</dbConnection>" ] + ServiceMgr.IOVDbSvc.FoldersToMetaData += [folder] + ServiceMgr.IOVSvc.partialPreLoadData = True + +# Check on run numbers and update them if necessary +# copied from do_run_number_modifications() in PyG4Atlas.py +def configureRunNumberOverrides(): + myRunNumber = 1 + myFirstLB = 1 + myInitialTimeStamp = 0 + from G4AtlasApps.G4Atlas_Metadata import inputAthFileObject + from G4AtlasApps.SimFlags import simFlags + if hasattr(simFlags, "RunNumber") and simFlags.RunNumber.statusOn: + myRunNumber = simFlags.RunNumber.get_Value() + simMDlog.info('Found run number %d in sim flags.' % myRunNumber) + ## Set event selector details based on evgen metadata + + ######update the run/event info for each event + from AthenaCommon.AppMgr import ServiceMgr + if not hasattr(ServiceMgr,'EvtIdModifierSvc'): + import AthenaServices.Configurables as asc + ServiceMgr +=asc.EvtIdModifierSvc(EvtStoreName="StoreGateSvc") + from AthenaCommon.AppMgr import theApp + theApp.CreateSvc += ["EvtIdModifierSvc"] + else: + simMDlog.warning('Will override the settings of the EvtIdModifierSvc that was previously set up!') + #fix iov metadata + if not hasattr(ServiceMgr.ToolSvc, 'IOVDbMetaDataTool'): + from AthenaCommon import CfgMgr + ServiceMgr.ToolSvc += CfgMgr.IOVDbMetaDataTool() + ServiceMgr.ToolSvc.IOVDbMetaDataTool.MinMaxRunNumbers = [myRunNumber, 2147483647] + ## FIXME need to use maxRunNumber = 2147483647 for now to keep overlay working but in the future this should be set properly. + # Using event numbers to avoid "some very large number" setting + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags + totalNumber = 1000000 # TODO possibly get this from AthFile?? + if athenaCommonFlags.EvtMax() is not None and athenaCommonFlags.EvtMax()>0: totalNumber = athenaCommonFlags.EvtMax()+1 + if athenaCommonFlags.SkipEvents() is not None and athenaCommonFlags.SkipEvents()>0: totalNumber += athenaCommonFlags.SkipEvents() + try: + from RunDependentSimComps.RunDMCFlags import runDMCFlags + myInitialTimeStamp = runDMCFlags.RunToTimestampDict.getTimestampForRun(myRunNumber) + #print "FOUND TIMESTAMP ", str(myInitialTimeStamp) + except: + myInitialTimeStamp = 1 + ServiceMgr.EvtIdModifierSvc.add_modifier(run_nbr=myRunNumber, lbk_nbr=myFirstLB, time_stamp=myInitialTimeStamp, nevts=totalNumber) + if hasattr(ServiceMgr.EventSelector,'OverrideRunNumberFromInput'): ServiceMgr.EventSelector.OverrideRunNumberFromInput = True + elif inputAthFileObject is not None: + ## Get evgen run number and lumi block + if len(inputAthFileObject.run_numbers) > 0: + myRunNumber = inputAthFileObject.run_numbers[0] + simMDlog.info('Found run number %d in hits file metadata.'% myRunNumber) + else: + simMDlog.warning('Failed to find run number in hits file metadata.') + if inputAthFileObject.lumi_block: + myFirstLB = inputAthFileObject.lumi_block[0] + else: + simMDlog.warning('Requires simFlags.RunNumber to be specified in this running mode.\ + Using default value of 1 for RunNumber.') + + from AthenaCommon.AppMgr import ServiceMgr + ServiceMgr.EventSelector.OverrideRunNumber = True + ServiceMgr.EventSelector.RunNumber = myRunNumber + ServiceMgr.EventSelector.FirstLB = myFirstLB + # Necessary to avoid a crash + ServiceMgr.EventSelector.InitialTimeStamp = myInitialTimeStamp diff --git a/Simulation/G4Atlas/G4AtlasApps/python/PyG4Atlas.py b/Simulation/G4Atlas/G4AtlasApps/python/PyG4Atlas.py index c94e8f4c6afb977d1e79c9e82ba268fcecaec3a6..6672837aa2a25d2c33e5f7cb312f17f68aaf9a7c 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/PyG4Atlas.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/PyG4Atlas.py @@ -920,77 +920,11 @@ class SimSkeleton(object): - The RunNumber flag - The input file run number """ - from G4AtlasApps.SimFlags import simFlags - from AthenaCommon.AthenaCommonFlags import athenaCommonFlags - from AthenaCommon.AppMgr import ServiceMgr as svcMgr - import os G4AtlasEngine.log.verbose('SimSkeleton.do_run_number_modifications :: starting') + from G4AtlasApps.G4Atlas_Metadata import configureRunNumberOverrides + configureRunNumberOverrides() + G4AtlasEngine.log.verbose('SimSkeleton.do_run_number_modifications :: done') - myRunNumber = 1 - myFirstLB = 1 - myInitialTimeStamp = 0 - if hasattr(simFlags, "RunNumber") and simFlags.RunNumber.statusOn: - myRunNumber = simFlags.RunNumber.get_Value() - G4AtlasEngine.log.info('SimSkeleton.do_run_number_modifications :: Found run number %d in sim flags.', myRunNumber) - ## Set event selector details based on evgen metadata - - ######update the run/event info for each event - if not hasattr(svcMgr,'EvtIdModifierSvc'): - import AthenaServices.Configurables as asc - svcMgr +=asc.EvtIdModifierSvc(EvtStoreName="StoreGateSvc") - from AthenaCommon.AppMgr import theApp - theApp.CreateSvc += ["EvtIdModifierSvc"] - else: - G4AtlasEngine.log.warning('SimSkeleton.do_run_number_modifications :: Will override the settings of the EvtIdModifierSvc that was previously set up!') - #fix iov metadata - if not hasattr(svcMgr.ToolSvc, 'IOVDbMetaDataTool'): - from AthenaCommon import CfgMgr - svcMgr.ToolSvc += CfgMgr.IOVDbMetaDataTool() - svcMgr.ToolSvc.IOVDbMetaDataTool.MinMaxRunNumbers = [myRunNumber, 2147483647] - ## FIXME need to use maxRunNumber = 2147483647 for now to keep overlay working but in the future this should be set properly. - - # Using event numbers to avoid "some very large number" setting - from AthenaCommon.AthenaCommonFlags import athenaCommonFlags - totalNumber = 1000000 - if athenaCommonFlags.EvtMax() is not None and athenaCommonFlags.EvtMax()>0: totalNumber = athenaCommonFlags.EvtMax()+1 - if athenaCommonFlags.SkipEvents() is not None and athenaCommonFlags.SkipEvents()>0: totalNumber += athenaCommonFlags.SkipEvents() - try: - from RunDependentSimComps.RunDMCFlags import runDMCFlags - myInitialTimeStamp = runDMCFlags.RunToTimestampDict.getTimestampForRun(myRunNumber) - #print "FOUND TIMESTAMP ", str(myInitialTimeStamp) - except: - myInitialTimeStamp = 1 - svcMgr.EvtIdModifierSvc.add_modifier(run_nbr=myRunNumber, lbk_nbr=myFirstLB, time_stamp=myInitialTimeStamp, nevts=totalNumber) - if hasattr(svcMgr.EventSelector,'OverrideRunNumberFromInput'): svcMgr.EventSelector.OverrideRunNumberFromInput = True - elif 'G4ATLAS_SKIPFILEPEEK' in os.environ and os.environ['G4ATLAS_SKIPFILEPEEK']: - G4AtlasEngine.log.warning('SimSkeleton.do_run_number_modifications\ - :: Requires simFlags.RunNUmber to be specified in this running mode.\ - Using default value of 1 for RunNumber.') - elif athenaCommonFlags.PoolEvgenInput.statusOn: - ## Read input file metadata - import PyUtils.AthFile as af - hitfile = athenaCommonFlags.PoolEvgenInput.get_Value()[0] - f = af.fopen(hitfile) - - ## Get evgen run number and lumi block - if len(f.run_numbers) > 0: - myRunNumber = f.run_numbers[0] - G4AtlasEngine.log.info('SimSkeleton.do_run_number_modifications :: Found run number %d in hits file metadata.', myRunNumber) - else: - G4AtlasEngine.log.warning("SimSkeleton.do_run_number_modifications :: Failed to find run number in hits file metadata.") - if f.lumi_block: - myFirstLB = f.lumi_block[0] - else: - G4AtlasEngine.log.warning('SimSkeleton.do_run_number_modifications\ - :: Require at least one of the the following to be\ - specified: input file or simFlags.RunNumber to be\ - specified. Using default value of 1 for RunNumber.') - - svcMgr.EventSelector.OverrideRunNumber = True - svcMgr.EventSelector.RunNumber = myRunNumber - svcMgr.EventSelector.FirstLB = myFirstLB - # Necessary to avoid a crash - svcMgr.EventSelector.InitialTimeStamp = myInitialTimeStamp @classmethod def _do_persistency(cls): @@ -1010,7 +944,8 @@ class SimSkeleton(object): ## Default setting for one output stream from AthenaCommon.AppMgr import ServiceMgr as svcMgr svcMgr.AthenaPoolCnvSvc.PoolAttributes += ["TREE_BRANCH_OFFSETTAB_LEN = '100'"] - svcMgr.AthenaPoolCnvSvc.PoolAttributes += ["DEFAULT_BUFFERSIZE = '2048'"] + # Recommendations from Peter vG 16.08.25 + svcMgr.AthenaPoolCnvSvc.PoolAttributes += [ "DatabaseName = '" + athenaCommonFlags.PoolHitsOutput() + "'; ContainerName = 'TTree=CollectionTree'; TREE_AUTO_FLUSH = '1'" ] ## Write geometry tag info import EventInfoMgt.EventInfoMgtInit @@ -1057,29 +992,8 @@ class SimSkeleton(object): svcMgr.EventSelector.InputCollections = athenaCommonFlags.PoolEvgenInput() if athenaCommonFlags.SkipEvents.statusOn: svcMgr.EventSelector.SkipEvents = athenaCommonFlags.SkipEvents() - - import os - if 'G4ATLAS_SKIPFILEPEEK' in os.environ and os.environ['G4ATLAS_SKIPFILEPEEK']: - G4AtlasEngine.log.info('SimSkeleton._do_readevgen: Skipping IS_SIMULATION check') - else: - ## Read input file metadata - import PyUtils.AthFile as af - hitfile = athenaCommonFlags.PoolEvgenInput.get_Value()[0] - f = af.fopen(hitfile) - - ## Check that event type is SIMULATION (as it must be) - if "evt_type" in f.infos.keys(): - evttypes = f.infos["evt_type"] - evttype0 = str(evttypes[0]) - if not evttype0.startswith("IS_SIMULATION"): - msg = "SimSkeleton._do_readevgen :: This input file has incorrect evt_type: %s\n" % str(evttypes) - msg += "Please make sure you have set input file metadata correctly - " - msg += "consider using the job transforms for earlier steps if you aren't already doing so." - G4AtlasEngine.log.error(msg) - raise SystemExit("Input file evt_type is incorrect: please check your evgen jobs.") - else: - G4AtlasEngine.log.warning("SimSkeleton._do_readevgen :: Could not find 'evt_type' key in athfile.infos. Unable to that check evt_type is correct.") - + from G4AtlasApps.G4Atlas_Metadata import inputFileValidityCheck + inputFileValidityCheck() else: ## No input file so assume that we are running a Generator in the same job if not hasattr(svcMgr, 'EventSelector'): diff --git a/Simulation/G4Atlas/G4AtlasApps/python/SimAtlasKernel.py b/Simulation/G4Atlas/G4AtlasApps/python/SimAtlasKernel.py index 03c2d31ed733287baaa062f1914677781846087f..c80503bbf56fdb731ae0f977d5e023ff64061e7f 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/SimAtlasKernel.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/SimAtlasKernel.py @@ -115,6 +115,10 @@ class AtlasSimSkeleton(SimSkeleton): #athenaCommonFlags.RuntimeStrictness = 'abort' #AtlasG4Eng.G4Eng.log.debug('AtlasSimSkeleton._do_jobproperties :: Enabled floating point exceptions') + if not simFlags.ISFRun: + from G4AtlasApps.G4Atlas_Metadata import checkForSpecialConfigurationMetadata + checkForSpecialConfigurationMetadata() + ## Print flags if AtlasG4Eng.G4Eng.log.getEffectiveLevel() < 40: AtlasG4Eng.G4Eng.log.info('AtlasSimSkeleton._do_jobproperties :: printing detector flags DetFlags') @@ -342,135 +346,14 @@ class AtlasSimSkeleton(SimSkeleton): """ Setup and add metadata to the HIT file. """ - import AtlasG4Eng,os + import AtlasG4Eng AtlasG4Eng.G4Eng.log.verbose('AtlasSimSkeleton._do_metadata :: starting') - from AthenaCommon.AthenaCommonFlags import athenaCommonFlags - ## Read in special simulation job option fragments based on metadata passed by the evgen stage - if 'G4ATLAS_SKIPFILEPEEK' in os.environ and os.environ['G4ATLAS_SKIPFILEPEEK']: - AtlasG4Eng.G4Eng.log.info('Skipping reading of extra metadata - will not work with G4ATLAS_SKIPFILEPEEK set') - elif athenaCommonFlags.PoolEvgenInput.statusOn: - AtlasG4Eng.G4Eng.log.verbose('AtlasSimSkeleton._do_metadata :: checking input file metadata.') - import PyUtils.AthFile as af - f = af.fopen(athenaCommonFlags.PoolEvgenInput()[0]) - #for k, v in f.infos.iteritems(): - # print k, v - if "specialConfiguration" in f.infos["tag_info"]: - item = f.infos["tag_info"]["specialConfiguration"] - ## Parse the specialConfiguration string - ## Format is 'key1=value1;key2=value2;...'. or just ' - AtlasG4Eng.G4Eng.log.debug("AtlasSimSkeleton._do_metadata :: Evgen specialConfiguration directive: ", item) - spcitems = item.split(";") - params = {} - for spcitem in spcitems: - #print spcitem - ## Ignore empty or "NONE" substrings, e.g. from consecutive or trailing semicolons - if not spcitem or spcitem.upper() == "NONE": - continue - ## If not in key=value format, treat as v, with k="preInclude" - if "=" not in spcitem: - spcitem = "preInclude=" + spcitem - ## Handle k=v directives - k, v = spcitem.split("=") - AtlasG4Eng.G4Eng.log.info("AtlasSimSkeleton._do_metadata :: specialConfiguration metadata item: %s => %s" % (k, v)) - if k == "preInclude": - AtlasG4Eng.G4Eng.log.info("AtlasSimSkeleton._do_metadata :: Including %s as instructed by specialConfiguration metadata" % v) - include(v) - else: - params[k] = v - AtlasG4Eng.G4Eng.Dict_SpecialConfiguration.update( params ) - from G4AtlasApps.SimFlags import simFlags if not simFlags.ISFRun: AtlasG4Eng.G4Eng.log.debug('AtlasSimSkeleton._do_metadata :: recording metadata') - - from IOVDbMetaDataTools import ParameterDbFiller - dbFiller = ParameterDbFiller.ParameterDbFiller() - - ## Set run numbers - minrunnum = 0 - maxrunnum = 2147483647 - import os - from G4AtlasApps.SimFlags import simFlags - - if hasattr(simFlags, 'RunNumber') and simFlags.RunNumber.statusOn: - minrunnum = simFlags.RunNumber() - ## FIXME need to use maxrunnum = 2147483647 for now to keep overlay working but in the future this should be set properly. - # maxrunnum = minrunnum + 1 - elif 'G4ATLAS_SKIPFILEPEEK' in os.environ and os.environ['G4ATLAS_SKIPFILEPEEK']: - AtlasG4Eng.G4Eng.log.info('Skipping run number setting - would need to set simFlags.RunNumber for this') - elif athenaCommonFlags.PoolEvgenInput.statusOn: - import PyUtils.AthFile as af - try: - f = af.fopen(athenaCommonFlags.PoolEvgenInput()[0]) - if len(f.run_numbers) > 0: - minrunnum = f.run_numbers[0] - maxrunnum = minrunnum + 1 - else: - raise Exception('IllegalRunNumber') - except Exception : - myCommand = 'dumpRunNumber.py '+jobproperties.AthenaCommonFlags.PoolEvgenInput.get_Value()[0] +' | grep "run number:" | sed \'s/run number://\'' - myOutput = os.popen3(myCommand)[1].read().strip() #read in child_stdout only - if (len(myOutput) > 0 and int(myOutput) > 0) : - AtlasG4Eng.G4Eng.log.info( "Setting run number from file to ",myOutput," with a range of one" ) - minrunnum = myOutput - maxrunnum = myOutput+1 - else: - AtlasG4Eng.G4Eng.log.info( "Setting run number from 0 to 2147483647 (could not open evgen file)" ) - - AtlasG4Eng.G4Eng.log.info("AtlasSimSkeleton._do_metadata :: Setting IOV run number range to [%d, %d]" % (minrunnum, maxrunnum)) - dbFiller.setBeginRun(minrunnum) - dbFiller.setEndRun(maxrunnum) - - ## Write sim parameters as metadata keys - from AthenaCommon.JobProperties import JobProperty - simParams = [sf for sf in dir(simFlags) if isinstance(getattr(simFlags, sf), JobProperty)] - for sp in simParams: - ## Don't write out random number seeds or RunDict as metadata - if sp in ("RandomSeedList", "RandomSeedOffset", "RunDict"): - continue - ## Only store InitFunction names - if sp in ("InitFunctions"): - initfuncdict = dict() - for level in getattr(simFlags, sp).get_Value(): - initfuncdict.setdefault(level, []) - for element in getattr(simFlags, sp).get_Value()[level]: - initfuncdict[level].append(element.__name__) - testValue = str( initfuncdict ) if getattr(simFlags, sp).statusOn else 'default' - dbFiller.addSimParam(sp, testValue) - continue - testValue = str( getattr(simFlags, sp).get_Value() ) if getattr(simFlags, sp).statusOn else 'default' - dbFiller.addSimParam(sp, testValue) - import os - dbFiller.addSimParam('G4Version', str(os.environ['G4VERS'])) - dbFiller.addSimParam('RunType', 'atlas') - dbFiller.addSimParam('beamType', jobproperties.Beam.beamType.get_Value()) - - ## Simulated detector flags: add each enabled detector to the simulatedDetectors list - simDets = [] - for det in ['pixel','SCT','TRT','BCM','DBM','Lucid','FwdRegion','ZDC','ALFA','AFP','LAr','Tile','MDT','CSC','TGC','RPC','Micromegas','sTGC','Truth']: - attrname = det+"_on" - checkfn = getattr(DetFlags.geometry, attrname, None) - if checkfn is None: - AtlasG4Eng.G4Eng.log.warning("AtlasSimSkeleton._do_metadata :: No attribute '%s' found on DetFlags.geometry" % attrname) - continue - if checkfn(): - simDets.append(det) - AtlasG4Eng.G4Eng.log.info("AtlasSimSkeleton._do_metadata :: Setting 'SimulatedDetectors' = %s" % repr(simDets)) - dbFiller.addSimParam('SimulatedDetectors', repr(simDets)) - - ## Hard-coded simulation hit file magic number (for major changes) - dbFiller.addSimParam('hitFileMagicNumber', '0') ##FIXME Remove this? - - ## Write the db info - dbFiller.genSimDb() - folder = "/Simulation/Parameters" - dbConnection = "sqlite://;schema=SimParams.db;dbname=SIMPARAM" - import IOVDbSvc.IOVDb - from AthenaCommon.AppMgr import ServiceMgr - ServiceMgr.IOVDbSvc.Folders += [ folder + "<dbConnection>" + dbConnection + "</dbConnection>" ] - ServiceMgr.IOVDbSvc.FoldersToMetaData += [folder] - ServiceMgr.IOVSvc.partialPreLoadData = True - + from G4AtlasApps.G4Atlas_Metadata import createSimulationParametersMetadata + createSimulationParametersMetadata() + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags if not athenaCommonFlags.PoolHitsOutput.statusOn: AtlasG4Eng.G4Eng.log.info('AtlasSimSkeleton._do_metadata :: no output HITS file, so no metadata writing required.') else: diff --git a/Simulation/G4Atlas/G4AtlasApps/python/SimCtbKernel.py b/Simulation/G4Atlas/G4AtlasApps/python/SimCtbKernel.py index 38b19231fc965365e6f60323fa486cc4bd17d774..a10d9443d7a45c00aec8055572856805dd008a0b 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/SimCtbKernel.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/SimCtbKernel.py @@ -66,78 +66,13 @@ class TBSimSkeleton(SimSkeleton): Setup and add metadata to the HIT file """ import AtlasG4Eng - from G4AtlasApps.SimFlags import simFlags - if (jobproperties.AthenaCommonFlags.PoolHitsOutput.statusOn) : + AtlasG4Eng.G4Eng.log.verbose('TBSimSkeleton :: _do_metadata :: starting') + from G4AtlasApps.G4Atlas_Metadata import createTBSimulationParametersMetadata + createTBSimulationParametersMetadata() + + from AthenaCommon.AthenaCommonFlags import athenaCommonFlags + if (athenaCommonFlags.PoolHitsOutput.statusOn) : AtlasG4Eng.G4Eng.log.info('SimCtbKernel : Recording Metadata') - from IOVDbMetaDataTools import ParameterDbFiller - dbFiller = ParameterDbFiller.ParameterDbFiller() - myOutput = "-1" - import os - - # sort out the run number that we need to use - myMinRunNumber = 0 - myMaxRunNumber = 2147483647 - if simFlags.__dict__.__contains__('RunNumber') and simFlags.RunNumber.statusOn : - AtlasG4Eng.G4Eng.log.info( "Setting run number to input: ",simFlags.RunNumber()," with a range of 1" ) - myMinRunNumber = simFlags.RunNumber() - myMaxRunNumber = simFlags.RunNumber()+1 - elif jobproperties.AthenaCommonFlags.PoolEvgenInput.statusOn: - import PyUtils.AthFile as af - try: - f = af.fopen(jobproperties.AthenaCommonFlags.PoolEvgenInput()[0]) - if (f.run_numbers[0]>0) : - myOutput = f.run_numbers[0] - AtlasG4Eng.G4Eng.log.info( "Setting run number from file to ",myOutput," with a range of one" ) - myMinRunNumber = myOutput - myMaxRunNumber = myOutput+1 - else: - raise Exception('IllegalRunNumber') - except Exception : - myCommand = 'dumpRunNumber.py '+jobproperties.AthenaCommonFlags.PoolEvgenInput.get_Value()[0] +' | grep "run number:" | sed \'s/run number://\'' - myOutput = os.popen3(myCommand)[1].read().strip() #read in child_stdout only - if (len(myOutput) > 0 and int(myOutput) > 0) : - AtlasG4Eng.G4Eng.log.info( "Setting run number from file to ",myOutput," with a range of one" ) - myMinRunNumber = myOutput - myMaxRunNumber = myOutput+1 - else: - AtlasG4Eng.G4Eng.log.info( "Setting run number from 0 to 2147483647 (could not open evgen file)" ) - - dbFiller.setBeginRun(myMinRunNumber) - dbFiller.setEndRun(myMaxRunNumber) - - dbFiller.addSimParam('RunType','testbeam') - SimParams = ['CalibrationRun','DoLArBirk','LArParameterization', - 'MagneticField','PhysicsList','Seeds','SeedsG4','SimLayout', - 'WorldZRange','NeutronTimeCut','NeutronEnergyCut','ApplyEMCuts','RunNumber'] - for o in [ o for o in SimParams if simFlags.__dict__.keys().__contains__(o) ]: - testValue = 'default' - if (simFlags.__dict__.__contains__(o) and simFlags.__dict__.get(o).statusOn) : - testValue = simFlags.__dict__.get(o).get_Value() - if not isinstance(testValue, str): - testValue = str(simFlags.__dict__.get(o).get_Value()) - dbFiller.addSimParam(o, testValue) - if (simFlags.__dict__.__contains__('EventFilter') and simFlags.EventFilter.statusOn) : - dbFiller.addSimParam('EtaPhiStatus', str(simFlags.EventFilter.get_Value()['EtaPhiFilters'])) - dbFiller.addSimParam('VertexStatus', str(simFlags.EventFilter.get_Value()['VertexPositioner'])) - dbFiller.addSimParam('VRangeStatus', str(simFlags.EventFilter.get_Value()['VertexRangeChecker'])) - else : - dbFiller.addSimParam('EtaPhiStatus', 'default') - dbFiller.addSimParam('VertexStatus', 'default') - dbFiller.addSimParam('VRangeStatus', 'default') - dbFiller.addSimParam('G4Version', str(os.environ['G4VERS'])) - dbFiller.addSimParam('beamType',jobproperties.Beam.beamType.get_Value()) - - ####### Hard coded simulation hit file magic number (for major changes) ###### - dbFiller.addSimParam('hitFileMagicNumber','0') - - dbFiller.genSimDb() - folder = "/Simulation/Parameters" - dbConnection = "sqlite://;schema=SimParams.db;dbname=SIMPARAM" - from AthenaCommon.AppMgr import ServiceMgr - import IOVDbSvc.IOVDb - ServiceMgr.IOVDbSvc.Folders += [ folder + "<dbConnection>" + dbConnection + "</dbConnection>" ] - ServiceMgr.IOVDbSvc.FoldersToMetaData += [ folder ] - ServiceMgr.IOVSvc.partialPreLoadData = True from AthenaServices.AthenaServicesConf import AthenaOutputStream stream1_SimMetaData = AthenaOutputStream("StreamHITS_SimMetaData") stream1_SimMetaData.ItemList += [ "IOVMetaDataContainer#*" ] @@ -249,6 +184,10 @@ class CtbSim(TBSimSkeleton): if not hasattr(ServiceMgr.IOVDbSvc, 'GlobalTag') or not ServiceMgr.IOVDbSvc.GlobalTag: ServiceMgr.IOVDbSvc.GlobalTag = globalflags.ConditionsTag.get_Value() + if not simFlags.ISFRun: + from G4AtlasApps.G4Atlas_Metadata import checkForSpecialConfigurationMetadata + checkForSpecialConfigurationMetadata() + ## Print out flags if AtlasG4Eng.G4Eng.log.getEffectiveLevel()<40: AtlasG4Eng.G4Eng.log.info('SimCtbKernel : printing detector flags DetFlags') @@ -693,6 +632,10 @@ class Tile2000_2003(TBSimSkeleton): if not hasattr(ServiceMgr.IOVDbSvc, 'GlobalTag') or not ServiceMgr.IOVDbSvc.GlobalTag: ServiceMgr.IOVDbSvc.GlobalTag = globalflags.ConditionsTag.get_Value() + if not simFlags.ISFRun: + from G4AtlasApps.G4Atlas_Metadata import checkForSpecialConfigurationMetadata + checkForSpecialConfigurationMetadata() + ## Print flags if AtlasG4Eng.G4Eng.log.getEffectiveLevel()<40: AtlasG4Eng.G4Eng.log.info('SimCtbKernel : printing detector flags DetFlags') @@ -946,6 +889,10 @@ class LArH6_TB(TBSimSkeleton): if not hasattr(ServiceMgr.IOVDbSvc, 'GlobalTag') or not ServiceMgr.IOVDbSvc.GlobalTag: ServiceMgr.IOVDbSvc.GlobalTag = globalflags.ConditionsTag.get_Value() + if not simFlags.ISFRun: + from G4AtlasApps.G4Atlas_Metadata import checkForSpecialConfigurationMetadata + checkForSpecialConfigurationMetadata() + ## Print flags if AtlasG4Eng.G4Eng.log.getEffectiveLevel()<40: AtlasG4Eng.G4Eng.log.info('SimCtbKernel : printing detector flags DetFlags') diff --git a/Simulation/G4Atlas/G4AtlasApps/python/SimFlags.py b/Simulation/G4Atlas/G4AtlasApps/python/SimFlags.py index b484745b6a3d20f9cb24395910146ff0d3796847..77472cfdf19cd53d62368d0d4013d68ac99fa6c2 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/SimFlags.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/SimFlags.py @@ -229,6 +229,15 @@ class PhysicsList(JobProperty): JobProperty.__setattr__(self, name, n_value) +class PhysicsOptions(JobProperty): + """ + PhysicsOptionTools to be used in this job + """ + statusOn = True + allowedTypes = ['list'] + StoredValue = [] + + class SimLayout(JobProperty): """ Simulation layout tag to use: specifies the geometry to be used. @@ -674,6 +683,14 @@ class OptionalUserActionList(JobProperty): except ValueError: print "WARNING Attempt to remove unkown action",actionTool,"from role ",role +class G4Commands(JobProperty): + """ + Commands to send to the G4 user interface once initialization is complete + """ + statusOn = True + allowedTypes = ['list'] + StoredValue = [] + class UserActionConfig(JobProperty): """Configuration for UserActions The name of the action must be a name retrievable through the ConfigurableFactory """ @@ -688,6 +705,14 @@ class UserActionConfig(JobProperty): self.StoredValue[actionTool]={prop:value} +class specialConfiguration(JobProperty): + """ contains information on configuring simulation for special physics models. + Populated, if possible, by evgen file metadata. + """ + statusOn = False + allowedTypes = ['dict'] + StoredValue = dict() + ## Definition and registration of the simulation flag container class SimFlags(JobPropertyContainer): diff --git a/Simulation/G4Atlas/G4AtlasApps/python/atlas_flags.py b/Simulation/G4Atlas/G4AtlasApps/python/atlas_flags.py index 57aa748d89cff973b0b4c2ebfd83f5b74a2eca2a..f9df1d6ecf2f27606078ce19962ff352cfc617b2 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/atlas_flags.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/atlas_flags.py @@ -123,6 +123,15 @@ class TightMuonStepping(JobProperty): StoredValue = False +class MuonFieldOnlyInCalo(JobProperty): + """ + Switch so that only muons see the B-field in the calo + """ + statusOn = True + allowedTypes = ['bool'] + StoredValue = False + + class ForwardDetectors(JobProperty): """ Decide whether simulation must be run for forward detectors. @@ -173,15 +182,6 @@ class VertexTimeOffset(JobProperty): StoredValue = False -class PhysicsOptions(JobProperty): - """ - PhysicsOptionTools to be used in this job - """ - statusOn = True - allowedTypes = ['list'] - StoredValue = [] - - class CavernBG(JobProperty): """ Flag to turn on the simulation of cavern background (and all that goes with it) @@ -399,7 +399,7 @@ class TRTRangeCut(JobProperty): statusOn = True allowedTypes = ['float'] allowedValues = [0.05,30.0] - StoredValue = 0.05 + StoredValue = 30.0 class IsEventOverlayInputSim(JobProperty): """ diff --git a/Simulation/G4Atlas/G4AtlasApps/python/callbacks.py b/Simulation/G4Atlas/G4AtlasApps/python/callbacks.py index 9fbbe8f2bc426a1c58ac468cb92fd4611acef634..af06489bdf96572d138a99dc6604ff1a9780e36a 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/callbacks.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/callbacks.py @@ -10,16 +10,16 @@ SimFlags.InitFunctions.add_function("postInit", callbacks.use_classicalrk4_stepp ## Change the field stepper def use_simplerunge_stepper(): - from G4AtlasApps import AtlasG4Eng - AtlasG4Eng.G4Eng._ctrl.fldMenu.SetDefaultStepper('SimpleRunge') + from G4AtlasApps.SimFlags import simFlags + simFlags.G4Stepper = 'SimpleRunge' def use_classicalrk4_stepper(): - from G4AtlasApps import AtlasG4Eng - AtlasG4Eng.G4Eng._ctrl.fldMenu.SetDefaultStepper('ClassicalRK4') + from G4AtlasApps.SimFlags import simFlags + simFlags.G4Stepper = 'ClassicalRK4' def use_nystromrk4_stepper(): - from G4AtlasApps import AtlasG4Eng - AtlasG4Eng.G4Eng._ctrl.fldMenu.SetDefaultStepper('NystromRK4') + from G4AtlasApps.SimFlags import simFlags + simFlags.G4Stepper = 'NystromRK4' ## Use verbose G4 tracking def use_verbose_tracking(): diff --git a/Simulation/G4Atlas/G4AtlasApps/python/tbtile_flags.py b/Simulation/G4Atlas/G4AtlasApps/python/tbtile_flags.py index 648c78bda8d08dcea33e276333dc4a5608748893..a90ed8b6bd1472333bb9e69a6fc82301ff417fe2 100644 --- a/Simulation/G4Atlas/G4AtlasApps/python/tbtile_flags.py +++ b/Simulation/G4Atlas/G4AtlasApps/python/tbtile_flags.py @@ -12,7 +12,7 @@ during the years 2000-2003. """ __author__= 'M. Gallas' -__version__ = "$Revision: 728941 $" +__version__ = "$Revision: 787225 $" from AthenaCommon.JobProperties import JobProperty @@ -84,7 +84,7 @@ class Theta(JobProperty): if name == "StoredValue": if type(n_value) == int or type (n_value) == float: import math - if abs(n_value) >= 60. and not (abs(abs(theta)-90.0) < 0.01) : + if abs(n_value) >= 60. and not (abs(abs(n_value)-90.0) < 0.01) : raise ValueError,('THETA MUST BE IN [-60,60] or +/-90 !!! The selected value %s is not in the range.' %n_value) JobProperty.__setattr__(self, name, n_value) diff --git a/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas.py b/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas.py index 74768f899e43f9b832727442948187ae6bc5a447..c816c914ab8e33798f0a140b2dbc41711a407cad 100644 --- a/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas.py +++ b/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas.py @@ -60,7 +60,7 @@ simFlags.EventFilter.set_On() ## Change the field stepper or use verbose G4 tracking #from G4AtlasApps import callbacks -#simFlags.InitFunctions.add_function("postInit", callbacks.use_simplerunge_stepper) +#callbacks.use_simplerunge_stepper() #simFlags.InitFunctions.add_function("postInit", callbacks.use_verbose_tracking) ## Use single particle generator diff --git a/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas_ReadEvgen.py b/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas_ReadEvgen.py index d6ac16a821df98c8a90622ad7ee108a6f5cdd017..d4b5657646def05fd2eec3cceee58189622076a8 100644 --- a/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas_ReadEvgen.py +++ b/Simulation/G4Atlas/G4AtlasApps/share/jobOptions.G4Atlas_ReadEvgen.py @@ -60,7 +60,7 @@ simFlags.EventFilter.set_On() ## Change the field stepper or use verbose G4 tracking #from G4AtlasApps import callbacks -#simFlags.InitFunctions.add_function("postInit", callbacks.use_simplerunge_stepper) +#callbacks.use_simplerunge_stepper() #simFlags.InitFunctions.add_function("postInit", callbacks.use_verbose_tracking) from AthenaCommon.CfgGetter import getAlgorithm