Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-05-18 07:43:06

0001 #!/usr/bin/env ruby
0002 
0003 # SPDX-License-Identifier: LGPL-3.0-or-later
0004 # Copyright (C) 2023 Christopher Dilks
0005 
0006 require 'yaml'
0007 require 'optparse'
0008 require 'ostruct'
0009 
0010 # default CLI options
0011 options = OpenStruct.new
0012 options.sim_file     = 'out/sim.edm4hep.root'
0013 options.rec_file     = 'out/rec.edm4hep.root'
0014 options.config_file  = 'config/recon_main.yaml'
0015 options.dry_run      = false
0016 options.debug_run    = false
0017 options.eicrecon_bin = 'eicrecon'
0018 
0019 # parse CLI options
0020 OptionParser.new do |o|
0021   o.banner = "USAGE: #{$0} [OPTIONS]..."
0022   o.separator('')
0023   o.separator('OPTIONS:')
0024   o.on("-c", "--config [FILE]", "Configuration YAML file", "Default: #{options.config_file}"){ |a| options.config_file = a }
0025   o.separator('')
0026   o.on("-s", "--sim [FILE]", "Simulation input file", "Default: #{options.sim_file}"){ |a| options.sim_file = a }
0027   o.separator('')
0028   o.on("-r", "--rec [FILE]", "Reconstruction output file", "Default: #{options.rec_file}"){ |a| options.rec_file = a }
0029   o.separator('')
0030   o.on("-d", "--dry-run", "Dry run: just print the EICrecon command and exit"){ options.dry_run = true }
0031   o.separator('')
0032   o.on("-D", "--debug", "Run in GDB debugger"){
0033     options.debug_run    = true
0034     options.eicrecon_bin = 'gdb --args eicrecon'
0035   }
0036   o.separator('')
0037   o.on_tail("-h", "--help", "Show this message") do
0038     puts o
0039     exit 2
0040   end
0041 end.parse!(ARGV)
0042 # puts "OPTIONS: #{options}"
0043 
0044 # check for existence of input files
0045 [
0046   options.sim_file,
0047   options.config_file,
0048 ].each do |name|
0049   unless File.exist? name
0050     $stderr.puts "ERROR: file '#{name}' does not exist"
0051     exit 1
0052   end
0053 end
0054 
0055 # parse configuration file to Hash
0056 config_yaml = YAML.load_file options.config_file
0057 
0058 # parse a YAML tree of settings, returning an Array of strings with:
0059 # - list of node path keys combined with `String.join ':'`
0060 # - leaf node appended as "=#{leaf}"
0061 #   - Array leaves will be returned as `String.join ','`
0062 def traverse(tree, tree_name='')
0063   case tree.class.name
0064   when 'Hash' # if a sub-tree, recurse
0065     tree.map do |branch_name, branch|
0066       next_name = [tree_name, branch_name].join(':').sub /^:/, ''
0067       traverse branch, next_name
0068     end.flatten
0069   when 'Array' # if an array, return Array.join ','
0070     "#{tree_name}=#{tree.join ','}"
0071   else # if a leaf, append it to the final string and stop recursion
0072     [tree_name, tree.to_s].join '='
0073   end
0074 end
0075 
0076 # convert parsed config file settings into 'key=value' pairs JANA can use
0077 arg_list = traverse config_yaml
0078 
0079 # fix: key name of log level settings
0080 arg_list.map! do |it|
0081   if it.match? /^log_levels:/
0082     it.sub(/^log_levels:/,'').sub(/\=/,':LogLevel=')
0083   else
0084     it
0085   end
0086 end
0087 
0088 # append CLI settings
0089 arg_list += traverse({
0090   "podio:output_file" => options.rec_file,
0091 })
0092 
0093 # if debugging, override the timeout
0094 arg_list += traverse({ "jana:timeout" => "0" }) if options.debug_run
0095 
0096 # prepend '-P' to each setting, and add quotes around the value
0097 arg_list.map! do |it|
0098   '-P' + it.sub(/.*\=/, '\0"') + '"'
0099 end
0100 
0101 # finally, append the input file name
0102 arg_list << options.sim_file
0103 
0104 # build the eicrecon command
0105 eicrecon_cmd = [options.eicrecon_bin, *arg_list].join ' '
0106 
0107 # print eicrecon command
0108 puts """
0109 EICRECON ARGUMENTS: ["""
0110 arg_list.each{ |it| puts "  #{it}," }
0111 puts """]
0112 
0113 EICRECON COMMAND:
0114 #{eicrecon_cmd}
0115 """
0116 
0117 # if a dry run, exit prematurely
0118 if options.dry_run
0119   puts "\nThis is a dry run: stopping before running the above command\n\n"
0120   exit
0121 end
0122 
0123 # run eicrecon: `exec` hands process control over to `eicrecon_cmd`
0124 exec eicrecon_cmd