From 1f3533dd3c25f67a67d865b72c2a535858129c01 Mon Sep 17 00:00:00 2001 From: Jost Migenda Date: Tue, 23 Apr 2024 18:05:57 +0100 Subject: [PATCH 1/2] =?UTF-8?q?In=20verbose=20mode,=20don=E2=80=99t=20writ?= =?UTF-8?q?e=20parameters=20to=20second=20output=20file=20for=20JUNO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sntools/genevts.py | 43 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/sntools/genevts.py b/src/sntools/genevts.py index 2e5cbce..199cc72 100644 --- a/src/sntools/genevts.py +++ b/src/sntools/genevts.py @@ -48,27 +48,28 @@ def main(): # Sort events by time and write them to an output file events.sort(key=lambda evt: evt.time) - with open(args.output, "w") as outfile: - if args.verbose: # write parameters to file as a comment - outfile.write(f"# Generated on {datetime.now()} with the options:\n") - outfile.write(f"# {args}\n") - if args.mcformat == 'NUANCE': - for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() - outfile.write(evt.nuance_string(i)) - outfile.write("$ stop\n") - if args.mcformat == 'RATPAC': - for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() - outfile.write(evt.ratpac_string(i, events)) - if args.mcformat == 'ROOT_JUNO': - fname = args.output+".root" - root_outfile = uproot.recreate(fname) - root_outfile.mktree("SNEvents",{"nparticles": "uint64", "origPDGID":"int32", "nuE":"double", "pdgid": ("int32",(2,)),"t": ("float64",(2,)), - "px": ("float64",(2,)),"py":("float64",(2,)),"pz":("float64",(2,)),"m":("float64",(2,)), "channel": "int64"}) - for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() - evt.juno_string(i, root_outfile) + if args.mcformat in ('NUANCE', 'RATPAC'): + with open(args.output, "w") as outfile: + if args.verbose: # write parameters to file as a comment + outfile.write(f"# Generated on {datetime.now()} with the options:\n") + outfile.write(f"# {args}\n") + if args.mcformat == 'NUANCE': + for (i, evt) in enumerate(events): + evt.vertex = args.detector.generate_random_vertex() + outfile.write(evt.nuance_string(i)) + outfile.write("$ stop\n") + if args.mcformat == 'RATPAC': + for (i, evt) in enumerate(events): + evt.vertex = args.detector.generate_random_vertex() + outfile.write(evt.ratpac_string(i, events)) + if args.mcformat == 'ROOT_JUNO': + fname = args.output+".root" + root_outfile = uproot.recreate(fname) + root_outfile.mktree("SNEvents",{"nparticles": "uint64", "origPDGID":"int32", "nuE":"double", "pdgid": ("int32",(2,)),"t": ("float64",(2,)), + "px": ("float64",(2,)),"py":("float64",(2,)),"pz":("float64",(2,)),"m":("float64",(2,)), "channel": "int64"}) + for (i, evt) in enumerate(events): + evt.vertex = args.detector.generate_random_vertex() + evt.juno_string(i, root_outfile) def parse_command_line_options(): """Define and parse command line options.""" From 0e6594708d0265e7d8c15adf02784f1f3eb6c69f Mon Sep 17 00:00:00 2001 From: Jost Migenda Date: Tue, 23 Apr 2024 18:07:11 +0100 Subject: [PATCH 2/2] avoid duplicate code for vertex generation --- src/sntools/genevts.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sntools/genevts.py b/src/sntools/genevts.py index 199cc72..919e4ee 100644 --- a/src/sntools/genevts.py +++ b/src/sntools/genevts.py @@ -48,6 +48,9 @@ def main(): # Sort events by time and write them to an output file events.sort(key=lambda evt: evt.time) + for evt in events: + evt.vertex = args.detector.generate_random_vertex() + if args.mcformat in ('NUANCE', 'RATPAC'): with open(args.output, "w") as outfile: if args.verbose: # write parameters to file as a comment @@ -55,12 +58,10 @@ def main(): outfile.write(f"# {args}\n") if args.mcformat == 'NUANCE': for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() outfile.write(evt.nuance_string(i)) outfile.write("$ stop\n") if args.mcformat == 'RATPAC': for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() outfile.write(evt.ratpac_string(i, events)) if args.mcformat == 'ROOT_JUNO': fname = args.output+".root" @@ -68,7 +69,6 @@ def main(): root_outfile.mktree("SNEvents",{"nparticles": "uint64", "origPDGID":"int32", "nuE":"double", "pdgid": ("int32",(2,)),"t": ("float64",(2,)), "px": ("float64",(2,)),"py":("float64",(2,)),"pz":("float64",(2,)),"m":("float64",(2,)), "channel": "int64"}) for (i, evt) in enumerate(events): - evt.vertex = args.detector.generate_random_vertex() evt.juno_string(i, root_outfile) def parse_command_line_options():