LDMX-Software / ldmx-sw

The Light Dark Matter eXperiment simulation and reconstruction framework.
https://ldmx-software.github.io
GNU General Public License v3.0
20 stars 16 forks source link

LHE PrimaryGenerator does not report non-existing file #1311

Open tomeichlersmith opened 3 months ago

tomeichlersmith commented 3 months ago

When attempting to run the LHE primary generator with a file that does not exist, we get a confusing no next <event> error. This implies to the user that the file does exist but has fewer events in it then requested by the simulation. While this type of error is not very difficult to deduce when running locally, it is very confusing on a computing cluster where the filesystem may be networked and therefore go down occasionally causing a previously-working setup to break. In addition, this is helpful to avoid path typos or using a path that is not mounted into the running container.

Fix

I think we should put in some helpful checks in the config module and (maybe) the C++ generator itself.

In the config module, we could check that the file exists and throw an exception if it doesn't.

https://github.com/LDMX-Software/SimCore/blob/c8a30c1f735eeaa020e178dc3a8b8e59bf8263b5/python/generators.py.in#L98

if not os.path.isfile(filePath):
    # could discuss a more helpful wording of this exception
    raise ValueError(f'Provided LHE file {filePath} does not exist or cannot be read by ldmx-sw.')

This simple update would catch path typos, unmounted paths, and filesystem drops as early as possible. In addition, we could (if possible) do a more thorough test within the C++ where we check that the input file has the LHE file format.

https://github.com/LDMX-Software/SimCore/blob/c8a30c1f735eeaa020e178dc3a8b8e59bf8263b5/src/SimCore/Generators/LHEPrimaryGenerator.cxx#L28-L29

This could be done within the linked constructor above or within the LHEReader. Implementing this additional check on the format of the input file might nicely fit into a new refactor of the LHE reading including LDMX-Software/SimCore#41 and maybe a more complete rewrite as discussed in LDMX-Software/ldmx-sw#1316

Replicate

I get the terminal error

Opening LHE file dne.lhe
 [ Process ] 1 : Processing 1 Run 9001 Event 1  (2024-02-19 15:33:22.867849000+0000)
WARNING: No next <event> element was found by the LHE reader.
[ LHEPrimaryGenerator ] : Ran out of input events so run will be aborted!
 [ fire ] 4 : [NoPrimaries] : No primary vertices were produced by any of the generators.
  at /code/SimCore/src/SimCore/G4User/PrimaryGeneratorAction.cxx:141 in GeneratePrimaries
Stack trace: 
    0 /usr/local/lib/libSimCore_G4User.so(+0x9f10) [0x7f608b043f10]
    1 G4RunManager::GenerateEvent(int) + 179 
    2 G4RunManager::ProcessOneEvent(int) + 24 
    3 simcore::Simulator::produce(framework::Event&) + 438 
    4 framework::Process::process(int, framework::Event&) const + 113 
    5 framework::Process::run() + 9030 
    6 main + 461 addr2line: 'fire': No such file

    7 /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f608e4f6d90]

when running the config (dne.lhe is a file that does not exist).

from LDMX.Framework import ldmxcfg
p = ldmxcfg.Process( "test" )
p.maxEvents = 10
p.logFrequency = 1
p.termLogLevel = 0
p.run = 9001
p.outputFiles = [ '/dev/null' ]
from LDMX.SimCore import simulator
import LDMX.Ecal.EcalGeometry
import LDMX.Hcal.HcalGeometry
mySim = simulator.simulator( "test-lhe" )
mySim.setDetector( 'ldmx-det-v14-8gev' , True )
from LDMX.SimCore import generators
mySim.generators = [
    generators.lhe('dne','dne.lhe')
]
mySim.description = 'lhe test'
p.sequence = [ mySim ]