From 6c38a537e57d532cb40dbf9e1cea89d08e9aa8a3 Mon Sep 17 00:00:00 2001 From: Zhiwen Zhao Date: Sun, 15 Nov 2026 18:17:58 -0500 Subject: [PATCH] fix: normalize untracked Lund generated-particle units --- .../readers/gparticle_lund_reader.cc | 8 +-- tests/lund_generated_units.py | 57 +++++++++++++++++++ tests/meson.build | 9 +++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/lund_generated_units.py diff --git a/gemc/gparticle/readers/gparticle_lund_reader.cc b/gemc/gparticle/readers/gparticle_lund_reader.cc index 4e8bf43e..2fb74b91 100644 --- a/gemc/gparticle/readers/gparticle_lund_reader.cc +++ b/gemc/gparticle/readers/gparticle_lund_reader.cc @@ -169,12 +169,12 @@ GParticleRecord make_record_from_lund(const LundParticleLine& particle, particle.pid, particle.type, 1, - momentum, + momentum * CLHEP::GeV, theta, std::atan2(particle.py, particle.px), - particle.vx, - particle.vy, - particle.vz + particle.vx * CLHEP::cm, + particle.vy * CLHEP::cm, + particle.vz * CLHEP::cm }; } diff --git a/tests/lund_generated_units.py b/tests/lund_generated_units.py new file mode 100644 index 00000000..d90746c1 --- /dev/null +++ b/tests/lund_generated_units.py @@ -0,0 +1,57 @@ +"""Generated kinematics use internal units for tracked and untracked Lund rows.""" +import json +import csv +import re +from pathlib import Path +import subprocess +import sys +import os +import tempfile + +for argument in sys.argv[2:]: + if argument.startswith("--plugin-path="): + os.environ["GEMC_PLUGIN_PATH"] = argument.split("=", 1)[1] + +binary = str(Path(sys.argv[1]).resolve()) +with tempfile.TemporaryDirectory() as directory: + work = Path(directory) + (work/'particles.lund').write_text('3 1 1 0 0 0 0 0 0 0\n\n1 -1 0 11 0 0 0 0 1 1.00000013 0.000511 -1 2 3\n2 -1 1 11 0 0 0 0 1 1.00000013 0.000511 -1 2 3\n3 -1 0 11 0 0 0 0 0 0.000511 0.000511 0 -1 2\n') + formats='{format: json, filename: particles}, {format: csv, filename: mirror}'+(', {format: root, filename: mirror}' if '--root' in sys.argv else '') + result = subprocess.run([binary, '-n=1', '-nthreads=1', '-gparticlefile=[{format: lund, filename: particles.lund}]', '-gstreamer=['+formats+']'], cwd=work, capture_output=True, text=True) + assert result.returncode == 0, result.stdout+result.stderr + particles = json.loads((work/'particles_t0.json').read_text())['events'][0]['generated']['generated'] + assert len(particles) == 3 + for particle in [x for x in particles if x['p']!=0]: + assert particle['p'] == 1000, particle + assert [particle[k] for k in ['vx','vy','vz']] == [-10,20,30], particle + assert particle['theta'] == 0 and particle['phi'] == 0, particle + zero=[x for x in particles if x['p']==0] + assert len(zero)==1 and zero[0]['theta']==0 and zero[0]['phi']==0,zero + assert [zero[0][k] for k in ['vx','vy','vz']]==[0,-10,20],zero + keys=['p','vx','vy','vz','theta','phi'] + expected=[[row[k] for k in keys] for row in particles] + rows=list(csv.DictReader((work/'mirror_t0_generated.csv').open(),skipinitialspace=True)) + assert [[float(row[k]) for k in keys] for row in rows]==expected,rows + if '--root' in sys.argv: + (work/'inspect.C').write_text(r''' +#include "TFile.h" +#include "TTree.h" +#include "TSystem.h" +#include +#include +void inspect() { + TFile file("mirror_t0.root");auto* tree=dynamic_cast(file.Get("generated")); + if(!tree){gSystem->Exit(2);return;} + std::vector *p=nullptr,*vx=nullptr,*vy=nullptr,*vz=nullptr,*theta=nullptr,*phi=nullptr; + tree->SetBranchAddress("p",&p);tree->SetBranchAddress("vx",&vx);tree->SetBranchAddress("vy",&vy); + tree->SetBranchAddress("vz",&vz);tree->SetBranchAddress("theta",&theta);tree->SetBranchAddress("phi",&phi); + tree->GetEntry(0); + for(size_t i=0;isize();++i)std::cout<<"ROW "<at(i)<<" "<at(i)<<" "<at(i)<<" "<at(i)<<" "<at(i)<<" "<at(i)<Exit(0); +} +''') + result=subprocess.run(['root','-l','-b','-q','inspect.C'],cwd=work,capture_output=True,text=True) + assert result.returncode==0,result.stdout+result.stderr + actual=[[float(x) for x in line.split()[1:]] for line in result.stdout.splitlines() if line.startswith('ROW ')] + assert actual==expected,(actual,expected) +print('Lund tracked/untracked units, zero momentum, negative vertices and output-bank consistency passed.') diff --git a/tests/meson.build b/tests/meson.build index 32b204b1..6e7920f3 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -221,3 +221,12 @@ material_optics = executable('test_material_optics', 'material_optics.cc', dependencies: gui_probe_dependencies, link_with: all_libs) test('material_optics', material_optics, suite: 'gsystem') + +lund_units_args = [files('lund_generated_units.py'), gemc, '--plugin-path=' + build_root] +if root_dep.found() + lund_units_args += ['--root'] +endif +test('lund_generated_units', python_exe, + args: lund_units_args, + depends: plugin_target_maps.values(), + env: project_test_env, suite: 'gparticle')