Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:30:55

0001 #!/usr/bin/env ruby
0002 # -----------------------------------------------------------------------------
0003 # 'RunDD4hepInSerial.rb'
0004 # Derek Anderson
0005 # 03.21.2024
0006 #
0007 # Runs a series of DD4hep (ddsim/npsim) jobs. Two options for running:
0008 #   type = :gun   -- run particle gun with parameters specified in
0009 #                    provided steering file
0010 #   type = :hepmc -- run specified hepmc file through DD4hep
0011 #
0012 # When providing command-line options (either via condor or locally), the
0013 # order of arguments is:
0014 #
0015 #   ./RunDD4hepInSerial.rb
0016 #     <num-iterations> <run-label> <steering-file> <input-file>
0017 # -----------------------------------------------------------------------------
0018 
0019 require 'fileutils'
0020 
0021 
0022 
0023 # main body of script ---------------------------------------------------------
0024 
0025 END {
0026 
0027   # default and configurable parameters ---------------------------------------
0028 
0029   # i/o parameters
0030   type    = :gun
0031   numiter = ARGV[0]
0032   input   = "forJetInitFix.e10h11pipXpim.d14m3y2024.hepmc"
0033   output  = "testOutOnLocal.d22m3y2024.edm4hep.root"
0034   steerer = ARGV[2]
0035 
0036   # input, output, and runnning directories
0037   in_dir  = "/sphenix/user/danderson/"
0038   out_dir = "/sphenix/user/danderson/"
0039   run_dir = "/sphenix/u/danderson/eic"
0040 
0041   # i/o parameters for condor running
0042   out_prefix = "testOutOnCondor"
0043   out_label  = ARGV[1]
0044   out_index  = "file"
0045   out_suffix = ".d22m3y2024.edm4hep.root"
0046 
0047   # fixed parameters ----------------------------------------------------------
0048 
0049   # job parameters
0050   numevts = 1000
0051   compact = "/opt/detector/epic-nightly/share/epic/epic.xml"
0052 
0053   # environment parameters
0054   exec  = "npsim"
0055   shell = "/sphenix/u/danderson/scripts/eic-shell"
0056   setup = "/opt/detector/setup.sh"
0057 
0058   # script names
0059   runner    = "RunDD4hepInShell.sh"
0060   simulator = "DoDD4hepSimulation.sh"
0061 
0062   # parse and run -------------------------------------------------------------
0063 
0064   iter = 0
0065   while iter < numiter.to_i
0066 
0067     # parse options
0068     parser = ArgParser.new
0069     parser.configure(
0070       args:  ARGV,
0071       steer: steerer,
0072       label: out_label,
0073       pref:  out_prefix,
0074       suff:  out_suffix,
0075       name:  output,
0076       sim:   simulator,
0077       run:   runner,
0078       index: out_index,
0079       count: iter
0080     )
0081     parser.parse()
0082 
0083     # run simulation
0084     handler = SimHandler.new
0085     handler.configure(
0086       outname:  parser.out_name,
0087       outdir:   out_dir,
0088       intype:   type,
0089       infile:   input,
0090       indir:    in_dir,
0091       optnum:   numevts,
0092       optdet:   compact,
0093       optsteer: parser.out_steer,
0094       sysexec:  exec,
0095       syssim:   parser.out_sim,
0096       sysrun:   parser.out_run,
0097       sysshell: shell,
0098       sysinit:  setup,
0099       rundir:   run_dir
0100     )
0101     handler.run()
0102 
0103     # increment counter
0104     iter = iter + 1
0105 
0106   end  # end while loop
0107 
0108 }  # end main body of script
0109 
0110 
0111 
0112 # class to parse input and return sensible job parameters ---------------------
0113 
0114 class ArgParser
0115 
0116   # input
0117   attr_accessor :in_args
0118   attr_accessor :in_steer
0119   attr_accessor :in_label
0120   attr_accessor :in_pref
0121   attr_accessor :in_suff
0122   attr_accessor :in_name
0123   attr_accessor :in_sim
0124   attr_accessor :in_run
0125   attr_accessor :in_index
0126   attr_accessor :in_count
0127 
0128   # output
0129   attr_reader :out_steer
0130   attr_reader :out_label
0131   attr_reader :out_name
0132   attr_reader :out_sim
0133   attr_reader :out_run
0134 
0135 
0136 
0137   # external methods ----------------------------------------------------------
0138 
0139   def configure(
0140     args:  [],
0141     steer: "",
0142     label: "",
0143     pref:  "",
0144     suff:  "",
0145     name:  "",
0146     sim:   "DoDDSim.sh",
0147     run:   "RunShell.sh",
0148     index: out_num,
0149     count: iter
0150   )
0151 
0152     @in_args  = args
0153     @in_steer = steer
0154     @in_label = label
0155     @in_pref  = pref
0156     @in_suff  = suff
0157     @in_name  = name
0158     @in_sim   = sim
0159     @in_run   = run
0160     @in_index = index
0161     @in_count = count
0162     return
0163 
0164   end  # end :configure
0165 
0166 
0167 
0168   def parse()
0169 
0170     @out_steer = pick_steering_file()
0171     @out_label = pick_label()
0172     @out_name  = construct_output_name(@out_label)
0173     @out_sim   = construct_script_name(@in_sim, @out_label)
0174     @out_run   = construct_script_name(@in_run, @out_label)
0175     return
0176 
0177   end  # end :parse
0178 
0179 
0180 
0181   # internal methods ----------------------------------------------------------
0182 
0183   private
0184 
0185     def pick_steering_file()
0186 
0187       # use steering file from arguments if available
0188       steer = ""
0189       if @in_args.empty?
0190         steer = @in_steer.clone
0191       else
0192         steer = @in_args[2].clone
0193       end
0194       return steer
0195 
0196     end  # end :pick_steering_file
0197 
0198 
0199 
0200     def pick_label()
0201 
0202       # use label from arguments if available
0203       label = ""
0204       if @in_args.empty?
0205         label = @in_label.clone
0206       else
0207         label = @in_args[1].clone
0208       end
0209 
0210       # if iteration # provided, add to label
0211       if not @in_index.nil?
0212         label = label + @in_index + @in_count.to_s
0213       end
0214       return label
0215 
0216     end  # end :pick_label
0217 
0218 
0219 
0220     def construct_output_name(label)
0221 
0222       # first determine if output name is or should be empty
0223       name = ""
0224       if @in_args.empty?
0225         name = @in_name.clone
0226       end
0227 
0228       # then if output name is empty, construct one
0229       #   or append label if index name provided 
0230       if name.empty?
0231         name = @in_pref + "." + label + "." + @in_suff
0232         name.gsub!("..", ".")
0233       elsif not @in_index.nil?
0234         ending = "." + label + ".edm4hep.root"
0235         name.gsub!(".edm4hep.root", ending)
0236         name.gsub!("..", ".")
0237       end
0238       return name
0239 
0240     end  # end :construct_output_name
0241 
0242 
0243 
0244     def construct_script_name(input, label)
0245 
0246       # check if provided name ends in ".sh"
0247       script = input.clone
0248       if script.end_with?(".sh") and not label.empty?
0249         script.gsub!(".sh", ".#{label}.sh")
0250       end
0251       return script
0252 
0253    end  # end "construct_script_name
0254 
0255 end  # end ArgParser
0256 
0257 
0258 
0259 # class to consolidate options and run simulation -----------------------------
0260 
0261 class SimHandler
0262 
0263   # getters
0264   attr_reader :out_file
0265   attr_reader :out_dir
0266   attr_reader :out_path
0267   attr_reader :in_type
0268   attr_reader :in_file
0269   attr_reader :in_dir
0270   attr_reader :in_orig
0271   attr_reader :in_path
0272   attr_reader :opt_nevt
0273   attr_reader :opt_det
0274   attr_reader :opt_steer
0275   attr_reader :sys_exec
0276   attr_reader :sys_sim
0277   attr_reader :sys_run
0278   attr_reader :sys_shell
0279   attr_reader :sys_init
0280   attr_reader :sim_path
0281   attr_reader :run_path
0282   attr_reader :run_dir
0283 
0284 
0285 
0286   # external methods ----------------------------------------------------------
0287 
0288   public
0289 
0290     def configure(
0291       outname:  "",
0292       outdir:   "./",
0293       intype:   :gun,
0294       infile:   nil,
0295       indir:    "./",
0296       optnum:   1,
0297       optdet:   "epic.xml",
0298       optsteer: "./steer.py",
0299       sysexec:  "ddsim",
0300       syssim:   "DoSim.sh",
0301       sysrun:   "RunShell.sh",
0302       sysshell: "~/scripts/eic-shell",
0303       sysinit:  "~/scripts/initialize-eic-detectors",
0304       rundir:   "./"
0305     )
0306 
0307       @out_file  = outname
0308       @out_dir   = outdir
0309       @in_type   = intype
0310       @in_file   = infile
0311       @in_dir    = indir
0312       @opt_nevt  = optnum
0313       @opt_det   = optdet
0314       @opt_steer = optsteer
0315       @sys_exec  = sysexec
0316       @sys_sim   = syssim
0317       @sys_run   = sysrun
0318       @sys_shell = sysshell
0319       @sys_init  = sysinit
0320       @run_dir   = rundir
0321       return
0322 
0323     end  # end :configure
0324 
0325 
0326 
0327     def run()
0328 
0329       # parse what was given to handler
0330       prepare_for_running()
0331 
0332       # make job scripts to run
0333       create_sim_script(@sim_path) if not File.exists?(@sim_path)
0334       create_run_script(@run_path) if not File.exists?(@run_path)
0335 
0336       # run simulation
0337       system("#{@sys_shell} -- #{@run_path}")
0338 
0339       # remove job scripts
0340       clean_up()
0341       return
0342 
0343     end  # end :run
0344 
0345 
0346 
0347   # internal methods ----------------------------------------------------------
0348   private
0349 
0350     def prepare_for_running()
0351 
0352       # construct paths to sim/run scripts and output
0353       @sim_path = @run_dir + "/" + @sys_sim
0354       @run_path = @run_dir + "/" + @sys_run
0355       @out_path = @run_dir + "/" + @out_file
0356       @sim_path.gsub!("//", "/")
0357       @run_path.gsub!("//", "/")
0358       @out_path.gsub!("//", "/")
0359       @sim_path.gsub!("..", ".")
0360       @run_path.gsub!("..", ".")
0361       @out_path.gsub!("..", ".")
0362 
0363       # if needed make sure input is in run directory
0364       if in_type == :hepmc
0365 
0366         # construct paths
0367         @in_orig = @in_dir  + "/" + @in_file
0368         @in_path = @run_dir + "/" + @in_file
0369         @in_orig.gsub!("..", ".")
0370         @in_path.gsub!("//", "/")
0371         @in_orig.gsub!("//", "/")
0372         @in_path.gsub!("..", ".")
0373 
0374         # copy to run dir
0375         if not File.exists?(@in_path)
0376           FileUtils.cp(@in_orig, @in_path)
0377         end
0378       end
0379       return
0380 
0381     end  # end :prepare_for_running
0382 
0383 
0384 
0385     def create_sim_script(path)
0386 
0387       # script input
0388       args_array = ["setup", "rundir", "steerer", "compact", "numevts", "output"]
0389       args_array << "input" if in_type == :hepmc
0390 
0391       # create block to assign arguments
0392       args_block = ""
0393       args_array.each_with_index do |arg, iArg|
0394         args_num    = iArg + 1
0395         args_block += arg
0396         args_block += "=$"
0397         args_block += "#{args_num}"
0398         args_block += "\n"
0399       end
0400 
0401       # create block for setup
0402       setup_block  = ["source $setup", "cd $rundir"].join("\n")
0403       setup_block += "\n"
0404 
0405       # create command
0406       command = ""
0407       if in_type == :gun
0408         command += "#{@sys_exec} --steeringFile $steerer --compactFile $compact -G -N $numevts --outputFile $output"
0409       elsif in_type == :hepmc
0410         command += "#{@sys_exec} --steeringFile $steerer --compactFile $compact -I $input -N $numevts --outputFile $output"
0411       end
0412       command += "\n"
0413 
0414       # create script
0415       body = [args_block, setup_block, command].join("\n")
0416       File.write(path, body)
0417       FileUtils.chmod("u+x", path)
0418       return
0419 
0420     end # end :create_sim_script
0421 
0422 
0423 
0424     def create_run_script(path)
0425 
0426       # arguments to give to script
0427       args_array = [@sys_init, @run_dir, @opt_steer, @opt_det, @opt_nevt, @out_path]
0428       args_array << @in_path if in_type == :hepmc
0429 
0430       # create command to run
0431       arguments = args_array.join(" ")
0432       command   = ["source", @sim_path, arguments].join(" ")
0433 
0434       File.write(path, command)
0435       FileUtils.chmod("u+x", path)
0436       return
0437 
0438     end  # end :create_run_script
0439 
0440 
0441 
0442     def clean_up()
0443 
0444       # move output to output directory
0445       FileUtils.mv(@out_path, @out_dir)
0446 
0447       # remove sim/run scripts
0448       FileUtils.rm @sim_path
0449       FileUtils.rm @run_path
0450 
0451       # delete input if needed
0452       FileUtils.rm @in_path if in_type == :hepmc
0453       return
0454 
0455     end  # end :clean_up
0456 
0457 end  # end SimHandler
0458 
0459 # end -------------------------------------------------------------------------