Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-01 07:07:09

0001 #!/usr/bin/env ruby
0002 
0003 # SPDX-License-Identifier: LGPL-3.0-or-later
0004 # Copyright (C) 2023 Christopher Dilks
0005 
0006 require 'fileutils'
0007 
0008 # arguments
0009 if ARGV.length != 2
0010   $stderr.puts """
0011   USAGE: #{$0} [config_file] [directory_name]
0012     - [config_file] will be split into several files, one
0013       config file per ROOT file; the new files will be
0014       written to [directory_name]
0015   """
0016   exit 2
0017 end
0018 ConfigFileN, OutDirN = ARGV
0019 
0020 # clean output directory
0021 puts "\nCleaning #{OutDirN} ..."
0022 FileUtils.mkdir_p OutDirN
0023 FileUtils.rm_f Dir.glob("#{OutDirN}/*config.part"), verbose: true
0024 
0025 
0026 # create config.template file, which we will use to generate config.part files
0027 # - the template file is a copy of the essential parts of the config file
0028 # - special strings that match the regex /__.*__/ will be modified when generating
0029 #   config.part files from the template
0030 templateFileN   = ConfigFileN + '.template'
0031 templateFile    = File.open templateFileN, 'w'
0032 configFileHash  = Hash.new
0033 numEventsHash   = Hash.new
0034 readingFileList = false
0035 rootFileNum     = 0
0036 q2key           = Proc.new{ configFileHash[':q2min'] + '_' + configFileHash[':q2max'] }
0037 puts "\nParsing #{ConfigFileN} and counting the number of events ..."
0038 File.open(ConfigFileN).readlines.each do |line_in|
0039 
0040   # remove comments and newlines
0041   line = line_in.gsub(/#.*/,'').chomp
0042 
0043   # parse line: if key-value pair
0044   if line.match? /^:/
0045 
0046     # if already reading a list of ROOT files, reset some settings for this Q2 range
0047     if readingFileList or configFileHash.size==0
0048       configFileHash[':q2min']        = '1.0'
0049       configFileHash[':q2max']        = '0.0'
0050       configFileHash[':crossSection'] = '0.0'
0051       templateFile.puts ":endGroup\n\n" if readingFileList
0052       readingFileList = false
0053     end
0054 
0055     # add this key-value pair to the hash
0056     key = line.split.first
0057     val = line.split[1..-1].join ' '
0058     configFileHash[key] = val
0059 
0060     # add to config.template file
0061     templateFile.puts line
0062 
0063   # parse line: if a ROOT file, produce a new config.part file
0064   elsif line.match? /\.root/
0065     rootFile = line.split.first
0066 
0067     # get the number of events in this ROOT file
0068     if numEventsHash[q2key.call].nil?
0069       numEventsHash[q2key.call] = {
0070         :numEvents => 0,
0071       }
0072     end
0073     puts "Counting events in #{rootFile}"
0074     numEvents = `hpc/src/count_events.exe #{rootFile}`.chomp.to_i
0075     numEventsHash[q2key.call][:numEvents] += numEvents
0076     puts "  => #{numEvents} events"
0077 
0078     # add template line for numEvents, if reading a new list of ROOT files
0079     unless readingFileList
0080       templateFile.puts "__numEvents__ #{q2key.call}"
0081     end
0082 
0083     # add ROOT file to template and prepare to parse the next line
0084     templateFile.puts "__ROOT_#{rootFileNum}__ #{rootFile}"
0085     readingFileList = true
0086     rootFileNum += 1
0087 
0088   end # end parsing
0089 end # end loop over config file lines
0090 templateFile.puts ":endGroup"
0091 templateFile.close
0092 
0093 
0094 # generate config.part files from the template
0095 puts "\nParsing #{templateFileN} to generate config.part files ..."
0096 rootFileNum.times do |i|
0097 
0098   # start new config.part file
0099   partFileN = OutDirN + '/' + File.basename(ConfigFileN).sub(/\.config$/,'') + ".%07d.config.part" % [i]
0100   rootFile = 'ERROR: UNKNOWN'
0101   File.open(partFileN,'w') do |partFile|
0102 
0103     # parse template, and write to config.part file
0104     File.open(templateFileN).readlines.each do |line|
0105       if line.match? /^__numEvents__/
0106         numEvents = numEventsHash[line.split.last][:numEvents]
0107         line = ":numEvents #{numEvents}"
0108       elsif line.match? /^__ROOT_#{i}__/
0109         rootFile = line.split.last
0110         line = rootFile
0111       elsif line.match? /^__ROOT/
0112         next
0113       end
0114       partFile.puts line
0115     end
0116 
0117   end
0118   puts "#{partFileN} => #{rootFile}"
0119 
0120 end
0121 puts "\nDone. Config files written to #{OutDirN}/"