Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-26 07:06:05

0001 #!/usr/bin/env ruby
0002 
0003 # SPDX-License-Identifier: LGPL-3.0-or-later
0004 # Copyright (C) 2023 Christopher Dilks
0005 
0006 # generate a config file, from a set of `list` files
0007 
0008 require 'pp'
0009 
0010 # constants
0011 CrossingAngle = -25 # default beam crossing angle [mrad]
0012 
0013 # usage guide
0014 if ARGV.length < 3
0015   puts "ERROR: not enough arguments" if ARGV.length > 0
0016   puts """
0017   USAGE: #{$0} [config_file] [energy] [list_files...]
0018 
0019   [config_file]:   name of output config file
0020   [energy]:        beam energy, such as 5x41 or 10x100
0021   [list_files...]: list of \"list files\", generated by one of the
0022                    `s3tools/s3tool.rb` scripts
0023   """
0024   exit 2
0025 end
0026 
0027 # parse arguments
0028 globalSettings = Hash.new
0029 configFile = ARGV[0]
0030 globalSettings[:eleBeamEn], globalSettings[:ionBeamEn] = ARGV[1].split('x').map{ |e| e.gsub /-.*/, '' }
0031 listFiles = ARGV[2..]
0032 globalSettings[:crossingAngle] = CrossingAngle
0033 puts "="*50, "generating config file".upcase, "="*50
0034 puts "configFile: #{configFile}"
0035 pp "globalSettings:", globalSettings
0036 pp "listFiles:", listFiles
0037 
0038 # parse list files, forming groups keyed by `[Q2min,Q2max]`
0039 groupSettings = Hash.new
0040 listFiles.map{ |f| File.open(f,'r').readlines }.each do |listFileLines|
0041   # parse the list files
0042   listFileLines.each do |line|
0043     # tokenize; these must be consistent with lists from `s3tool.rb`
0044     rootFile, crossSection, q2min, q2max = line.split.map do |token|
0045       token.match?(/\.root$/) ? token : token.to_f # assume `token` is a number, if not a ROOT file
0046     end
0047     # create a unique key for this Q2 range
0048     q2key = "#{q2min},#{q2max}"
0049     # fill `groupSettings[q2key]`
0050     unless groupSettings.has_key? q2key
0051       groupSettings[q2key]                = Hash.new
0052       groupSettings[q2key][:files]        = Array.new
0053       groupSettings[q2key][:q2min], groupSettings[q2key][:q2max] = q2key.split(',').map &:to_f
0054       groupSettings[q2key][:crossSection] = crossSection.to_f
0055     end
0056     groupSettings[q2key][:files] << rootFile
0057     puts "ERROR: conflicting cross section" if crossSection!=groupSettings[q2key][:crossSection]
0058   end
0059 end
0060 # puts "\ngroupSettings:"
0061 # pp groupSettings
0062 
0063 # sort groups by decreasing cross section
0064 groupSettingsSorted = groupSettings.sort_by{ |s| s.last[:crossSection] }.reverse.to_h
0065 puts "\ngroupSettings, sorted by cross section:"
0066 pp groupSettingsSorted
0067 
0068 # assume the largest cross section is the total cross section (may not always be true!)
0069 globalSettings[:totalCrossSection] = groupSettings.values.map{ |s| s[:crossSection] }.max
0070 
0071 # start config file
0072 configOut = File.open configFile, 'w'
0073 writeSetting = Proc.new{ |k,v| configOut.puts ":#{k} #{v}" }
0074 configOut.puts "\#"*60
0075 configOut.puts "\# configuration file, auto-generated from:".upcase
0076 listFiles.each{ |f| configOut.puts "\#   #{f}" }
0077 configOut.puts "\#"*60
0078 
0079 # write global settings
0080 configOut.puts """
0081 \# Global Settings
0082 \# ===============
0083 """
0084 globalSettings.each{ |k,v| writeSetting.call k, v }
0085 
0086 # write group settings
0087 configOut.puts """
0088 \# Group Settings   | NOTE: they must be sorted by increasing strictness
0089 \# ==============   | of Q2 cuts, or at least by decreasing cross section
0090 """
0091 groupSettingsSorted.values.each_with_index do |settingsHash,i|
0092   configOut.puts "\n\# Q2 range #{i+1}"
0093   settingsHash.each do |key,value|
0094     unless key == :files
0095       next if key==:q2max and value==0.0  # skip case with no q2max
0096       writeSetting.call key, value
0097     end
0098   end
0099   settingsHash[:files].each{ |f| configOut.puts f }
0100 end
0101 
0102 # cleanup
0103 configOut.close
0104 puts """
0105 #{"="*50}
0106 CONFIG FILE: #{configFile}
0107 #{"="*50}
0108 """
0109 system "cat #{configFile}"
0110 puts "="*50, "PRODUCED CONFIG FILE #{configFile}\n\n"