LDMX-Software / SimCore

Integration of Geant4 simulation framework into a more generalized event processing framework.
2 stars 0 forks source link

Allow kaon physics to be configurable at runtime by the user #108

Closed EinarElen closed 11 months ago

EinarElen commented 11 months ago

This PR adds the ability to change the kaon physics at runtime by modifying the properties of the G4KaonMinus and G4KaonPlus particle definitions rather than doing it manually in the Geant4 sources. This should significantly simplify the study of challenging kaon backgrounds, especially together with techniques like https://ldmx-software.github.io/Alternative-Photo-Nuclear-Models.html

I also add "Decay" as one of the processes registered in the ProcessMap so that you can tell that a daughter particle came from a decay.

To verify that this works, I used a configuration like the following and some checks in the SimObjects DQM code to check basic things like "if I force only one decay type, I always get daughter particles of that type" etc

from LDMX.Framework import ldmxcfg
p = ldmxcfg.Process('test')

p.maxTriesPerEvent = 1

from LDMX.SimCore import generators as gen
from LDMX.SimCore import simulator
from LDMX.SimCore import kaon_physics
mySim = simulator.simulator('kaon_test')

gun = gen.gun('kaon_gun')
gun.particle='kaon+'
gun.position = [0.,0.,0.]
gun.direction = [0.,0.,1.]
gun.energy = 0.
mySim.setDetector('ldmx-det-v14-8gev')
mySim.beamSpotSmear = [20.,80.,0.]
mySim.description = 'ECal PN Test Simulation'
mySim.generators = [gun]
mySim.kaon_parameters = kaon_physics.KaonPhysics.upKaons()
mySim.kaon_parameters.kplus_lifetime_factor = 1/10000000000000000.
mySim.kaon_parameters.kminus_lifetime_factor = 1/10000000000000000.
mySim.kaon_parameters.kplus_branching_ratios = [0., 1., 0., 0., 0., 0.]
p.sequence = [ mySim ]

##################################################################
# Below should be the same for all sim scenarios

import os
import sys

if 'LDMX_NUM_EVENTS' in os.environ:
    p.maxEvents = int(os.environ['LDMX_NUM_EVENTS'])
else:
    p.maxEvents = int(sys.argv[1])
if 'LDMX_RUN_NUMBER' in os.environ:
    p.run = int(os.environ['LDMX_RUN_NUMBER'])
else:
    p.run = 1

output_base = 'kaon_test'
if len(sys.argv) > 2:
    output_base = sys.argv[2]

p.histogramFile = f'{output_base}_hist.root'
p.outputFiles = [f'{output_base}_events.root']

import LDMX.Ecal.EcalGeometry
import LDMX.Hcal.HcalGeometry

from LDMX.DQM import dqm

p.sequence.extend([
    dqm.SimObjects()
])

This resolves #106 and part of #107

EinarElen commented 11 months ago

Ok after some minor bugs in the updated python config, I've tested and it handles all the edge cases well (e.g. BR sum > 1, BR sum << 1, negative lifetimes and whatnot) and the upKaons call works!