Back to home page

EIC code displayed by LXR

 
 

    


Warning, /tutorial-developing-benchmarks/episodes/03-filling_out_your_benchmark.md is written in an unsupported language. File is not indexed.

0001 ---
0002 title: "Exercise 3: Filling out your benchmark"
0003 teaching: 20
0004 exercises: 10
0005 ---
0006 
0007 ::::::::::::::::::::::::::::::::::::::::::::::: questions
0008 
0009 - How do we fill in each stage of the benchmark pipeline?
0010 
0011 :::::::::::::::::::::::::::::::::::::::::::::::
0012 
0013 ::::::::::::::::::::::::::::::::::::::::::::::: objectives
0014 
0015 - Fill out the many steps of your benchmark
0016 - Collect templates for the benchmark stages
0017 
0018 :::::::::::::::::::::::::::::::::::::::::::::::
0019 
0020 In this lesson we will be beefing up our benchmark by filling out several of the pipeline stages.
0021 
0022 ## Setting up
0023 
0024 Before filling out the stages for GitLab's CI and pipelines, we want to first create a file that contains some settings used by our benchmark.
0025 
0026 Create a new file [`benchmarks/your_benchmark/setup.config`](files/setup.config) with the following contents
0027 
0028 ```bash
0029 #!/bin/bash
0030 source strict-mode.sh
0031 
0032 USE_SIMULATION_CAMPAIGN=true
0033 
0034 N_EVENTS=100
0035 
0036 FILE_BASE=sim_output/rho_10x100_uChannel_Q2of0to10_hiDiv.hepmc3.tree
0037 INPUT_FILE=root://dtn-eic.jlab.org//volatile/eic/EPIC/EVGEN/EXCLUSIVE/UCHANNEL_RHO/10x100/rho_10x100_uChannel_Q2of0to10_hiDiv.hepmc3.tree.root
0038 OUTPUT_FILE=${FILE_BASE}.detectorsim.root
0039 
0040 REC_FILE_BASE=${FILE_BASE}.detectorsim.edm4eic
0041 REC_FILE=${REC_FILE_BASE}.root
0042 ```
0043 
0044 The `export ENV_MODE=eicweb` lets our Snakefile know to use the paths for running on eicweb.
0045 
0046 Here we've defined a switch `USE_SIMULATION_CAMPAIGN` which will allow us to alternate between using output from the simulation campaign, and dynamically simulating new events.
0047 
0048 When not using the simulation campaign, the `N_EVENTS` variable defines how many events the benchmark should run.
0049 The rest of these variables define file names to be used in the benchmark.
0050 
0051 Also create a new file [`benchmarks/your_benchmark/simulate.sh`](files/simulate.sh) with the following contents:
0052 
0053 ```bash
0054 #!/bin/bash
0055 source strict-mode.sh
0056 source benchmarks/your_benchmark/setup.config $*
0057 
0058 if [ -f ${INPUT_FILE} ]; then
0059   echo "ERROR: Input simulation file does ${INPUT_FILE} not exist."
0060 else
0061   echo "GOOD: Input simulation file ${INPUT_FILE} exists!"
0062 fi
0063 
0064 # Simulate
0065 ddsim --runType batch \
0066       -v WARNING \
0067       --numberOfEvents ${N_EVENTS} \
0068       --part.minimalKineticEnergy 100*GeV  \
0069       --filter.tracker edep0 \
0070       --compactFile ${DETECTOR_PATH}/${DETECTOR_CONFIG}.xml \
0071       --inputFiles ${INPUT_FILE} \
0072       --outputFile  ${OUTPUT_FILE}
0073 if [[ "$?" -ne "0" ]] ; then
0074   echo "ERROR running ddsim"
0075   exit 1
0076 fi
0077 ```
0078 
0079 This script uses ddsim to simulate the detector response to your benchmark events.
0080 
0081 Create a script named [`benchmarks/your_benchmark/reconstruct.sh`](files/reconstruct.sh) to manage the reconstruction:
0082 
0083 ```bash
0084 #!/bin/bash
0085 source strict-mode.sh
0086 source benchmarks/your_benchmark/setup.config $*
0087 
0088 # Reconstruct
0089 if [ ${RECO} == "eicrecon" ] ; then
0090   eicrecon ${OUTPUT_FILE} -Ppodio:output_file=${REC_FILE}
0091   if [[ "$?" -ne "0" ]] ; then
0092     echo "ERROR running eicrecon"
0093     exit 1
0094   fi
0095 fi
0096 
0097 if [[ ${RECO} == "juggler" ]] ; then
0098   gaudirun.py options/reconstruction.py || [ $? -eq 4 ]
0099   if [ "$?" -ne "0" ] ; then
0100     echo "ERROR running juggler"
0101     exit 1
0102   fi
0103 fi
0104 
0105 if [ -f jana.dot ] ; then cp jana.dot ${REC_FILE_BASE}.dot ; fi
0106 
0107 #rootls -t ${REC_FILE_BASE}.tree.edm4eic.root
0108 rootls -t ${REC_FILE}
0109 ```
0110 
0111 Create a file called [`benchmarks/your_benchmark/analyze.sh`](files/analyze.sh) which will run the analysis and plotting scripts:
0112 
0113 ```bash
0114 #!/bin/bash
0115 source strict-mode.sh
0116 source benchmarks/your_benchmark/setup.config $*
0117 
0118 OUTPUT_PLOTS_DIR=sim_output/nocampaign
0119 mkdir -p ${OUTPUT_PLOTS_DIR}
0120 # Analyze
0121 command time -v \
0122 root -l -b -q "benchmarks/your_benchmark/analysis/uchannelrho.cxx(\"${REC_FILE}\",\"${OUTPUT_PLOTS_DIR}/plots.root\")"
0123 if [[ "$?" -ne "0" ]] ; then
0124   echo "ERROR analysis failed"
0125   exit 1
0126 fi
0127 
0128 if [ ! -d "${OUTPUT_PLOTS_DIR}/plots_figures" ]; then
0129     mkdir "${OUTPUT_PLOTS_DIR}/plots_figures"
0130     echo "${OUTPUT_PLOTS_DIR}/plots_figures directory created successfully."
0131 else
0132     echo "${OUTPUT_PLOTS_DIR}/plots_figures directory already exists."
0133 fi
0134 root -l -b -q "benchmarks/your_benchmark/macros/plot_rho_physics_benchmark.C(\"${OUTPUT_PLOTS_DIR}/plots.root\")"
0135 cat benchmark_output/*.json
0136 ```
0137 
0138 Let's copy over our analysis script, our plotting macro & header, and our Snakefile:
0139 
0140 ```bash
0141 mkdir benchmarks/your_benchmark/analysis
0142 mkdir benchmarks/your_benchmark/macros
0143 
0144 cp ../starting_script/Snakefile benchmarks/your_benchmark/
0145 cp ../starting_script/analysis/uchannelrho.cxx benchmarks/your_benchmark/analysis/
0146 cp ../starting_script/macros/RiceStyle.h benchmarks/your_benchmark/macros/
0147 cp ../starting_script/macros/plot_rho_physics_benchmark.C benchmarks/your_benchmark/macros/
0148 ```
0149 
0150 Your benchmark directory should now look like this: 
0151 ![File listing of the completed your_benchmark directory](fig/your_bench_dir_new.png) 
0152 
0153 In order to use your Snakefile, let GitLab know it's there. Open the main `Snakefile`, NOT this one `benchmarks/your_benchmark/Snakefile`, but the one at the same level as the `benchmarks` directory.
0154 
0155 Go to the very end of the file and include a path to your own Snakefile:
0156 
0157 ```python
0158 include: "benchmarks/diffractive_vm/Snakefile"
0159 include: "benchmarks/dis/Snakefile"
0160 include: "benchmarks/demp/Snakefile"
0161 include: "benchmarks/your_benchmark/Snakefile"
0162 ```
0163 
0164 Once that's all setup, we can move on to actually adding these to our pipeline!
0165 
0166 ## The "simulate" pipeline stage
0167 
0168 We now fill out the `simulate` stage in GitLab's pipelines. Currently the instructions for this rule should be contained in `benchmarks/your_benchmark/config.yml` as: 
0169 
0170 ```yaml
0171 your_benchmark:simulate:
0172   extends: .phy_benchmark
0173   stage: simulate
0174   script:
0175     - echo "I will simulate detector response here!"
0176 ```
0177 
0178 In order to make sure the previous stages finish before this one starts, add a new line below `stage:simulate`: `needs: ["common:setup"]`.
0179 
0180 This step can take a long time if you simulate too many events. So let's add an upper limit on the allowed run time of 10 hours:
0181 In a new line below `needs: ["common:setup"]`, add this: `timeout: 10 hour`.
0182 
0183 Now in the `script` section of the rule, add two new lines to source the `setup.config` file:
0184 
0185 ```yaml
0186     - config_file=benchmarks/your_benchmark/setup.config
0187     - source $config_file
0188 ```
0189 
0190 Add instructions that if using the simulation campaign you can skip detector simulations. Otherwise simulate
0191 
0192 ```yaml
0193     - if [ "$USE_SIMULATION_CAMPAIGN" = true ] ; then
0194     -     echo "Using simulation campaign so skipping this step!"
0195     - else
0196     -     echo "Grabbing raw events from XRootD and running Geant4"
0197     -     bash benchmarks/your_benchmark/simulate.sh
0198     -     echo "Geant4 simulations done! Starting eicrecon now!"
0199     -     bash benchmarks/your_benchmark/reconstruct.sh
0200     - fi
0201     - echo "Finished simulating detector response"
0202 ```
0203 
0204 Finally, add an instruction to retry the simulation if it fails:
0205 
0206 ```yaml
0207   retry:
0208     max: 2
0209     when:
0210       - runner_system_failure
0211 ```
0212 
0213 The final `simulate` rule should look like this:
0214 
0215 ```yaml
0216 your_benchmark:simulate:
0217   extends: .phy_benchmark
0218   stage: simulate
0219   needs: ["common:setup"]
0220   timeout: 10 hour
0221   script:
0222     - echo "I will simulate detector response here!"
0223     - config_file=benchmarks/your_benchmark/setup.config
0224     - source $config_file
0225     - if [ "$USE_SIMULATION_CAMPAIGN" = true ] ; then
0226     -     echo "Using simulation campaign!"
0227     - else
0228     -     echo "Grabbing raw events from XRootD and running Geant4"
0229     -     bash benchmarks/your_benchmark/simulate.sh
0230     -     echo "Geant4 simulations done! Starting eicrecon now!"
0231     -     bash benchmarks/your_benchmark/reconstruct.sh
0232     - fi
0233     - echo "Finished simulating detector response"
0234   retry:
0235     max: 2
0236     when:
0237       - runner_system_failure
0238 ```
0239 
0240 ## The "results" pipeline stage
0241 
0242 The `results` stage in `config.yml` is right now just this:
0243 
0244 ```yaml
0245 your_benchmark:results:
0246   extends: .phy_benchmark
0247   stage: collect
0248   script:
0249     - echo "I will collect results here!"
0250 ```
0251 
0252 Specify that we need to finish the simulate stage first:
0253 
0254 ```yaml
0255   needs:
0256     - ["your_benchmark:simulate"]
0257 ```
0258 
0259 Now make two directories to contain output from the benchmark analysis and source `setup.config` again:
0260 
0261 ```yaml
0262     - mkdir -p results/your_benchmark
0263     - mkdir -p benchmark_output
0264     - config_file=benchmarks/your_benchmark/setup.config
0265     - source $config_file
0266 ```
0267 
0268 If using the simulation campaign, we can request the rho mass benchmark with snakemake. Once snakemake has finished creating the benchmark figures, we copy them over to `results/your_benchmark/` in order to make them into artifacts:
0269 
0270 ```yaml
0271     - if [ "$USE_SIMULATION_CAMPAIGN" = true ] ; then
0272     -     echo "Using simulation campaign!"
0273     -     snakemake --cores 2 ../../sim_output/campaign_25.10.2_combined_45files.eicrecon.edm4eic.plots_figures/benchmark_rho_mass.pdf
0274     -     cp ../../sim_output/campaign_25.10.2_combined_45files.eicrecon.edm4eic.plots_figures/*.pdf results/your_benchmark/
0275 ```
0276 
0277 If not using the simulation campaign, we can just run the `analyze.sh` script and copy the results into `results/your_benchmark/` in order to make them into artifacts:
0278 
0279 ```yaml
0280     - else
0281     -     echo "Not using simulation campaign!"
0282     -     bash benchmarks/your_benchmark/analyze.sh
0283     -     cp sim_output/nocampaign/plots_figures/*.pdf results/your_benchmark/
0284     - fi
0285     - echo "Finished copying!"
0286 ```
0287 
0288 Your final `config.yml` should look like:
0289 
0290 ```yaml
0291 your_benchmark:compile:
0292   extends: .phy_benchmark 
0293   stage: compile
0294   script:
0295     - echo "You can compile your code here!"
0296 
0297 your_benchmark:simulate:
0298   extends: .phy_benchmark
0299   stage: simulate
0300   needs: ["common:setup"]
0301   timeout: 10 hour
0302   script:
0303     - echo "Simulating everything here!"
0304     - config_file=benchmarks/your_benchmark/setup.config
0305     - source $config_file
0306     - if [ "$USE_SIMULATION_CAMPAIGN" = true ] ; then
0307     -     echo "Using simulation campaign!"
0308     - else
0309     -     echo "Grabbing raw events from XRootD and running Geant4"
0310     -     bash benchmarks/your_benchmark/simulate.sh
0311     -     echo "Geant4 simulations done! Starting eicrecon now!"
0312     -     bash benchmarks/your_benchmark/reconstruct.sh
0313     - fi
0314     - echo "Finished simulating detector response"
0315   retry:
0316     max: 2
0317     when:
0318       - runner_system_failure
0319 
0320 your_benchmark:results:
0321   extends: .phy_benchmark
0322   stage: collect
0323   script:
0324     - echo "I will collect results here!"
0325   needs:
0326     - ["your_benchmark:simulate"]
0327   script:
0328     - mkdir -p results/your_benchmark
0329     - mkdir -p benchmark_output
0330     - config_file=benchmarks/your_benchmark/setup.config
0331     - source $config_file
0332     - if [ "$USE_SIMULATION_CAMPAIGN" = true ] ; then
0333     -     echo "Using simulation campaign!"
0334     -     snakemake --cores 2 ../../sim_output/campaign_25.10.2_combined_45files.eicrecon.edm4eic.plots_figures/benchmark_rho_mass.pdf
0335     -     cp ../../sim_output/campaign_25.10.2_combined_45files.eicrecon.edm4eic.plots_figures/*.pdf results/your_benchmark/
0336     - else
0337     -     echo "Not using simulation campaign!"
0338     -     bash benchmarks/your_benchmark/analyze.sh
0339     -     cp sim_output/nocampaign/plots_figures/*.pdf results/your_benchmark/
0340     - fi
0341     - echo "Finished copying!"
0342 ```
0343 
0344 ## Testing Real Pipelines
0345 
0346 We've set up our benchmark to do some real analysis! As a first test, let's make sure we're still running only over the simulation campaign. The `USE_SIMULATION_CAMPAIGN` in `setup.config` should be set to true.
0347 
0348 Now let's add our changes and push them to GitHub!
0349 
0350 ```bash
0351 git status
0352 ```
0353 
0354 This command should show something like this:
0355 ![Example output of git status listing the new benchmark files](fig/gitstatus_example.png) 
0356 
0357 Now add all our changes:
0358 
0359 ```bash
0360 git add Snakefile
0361 git add benchmarks/your_benchmark/config.yml
0362 git add benchmarks/your_benchmark/Snakefile
0363 git add benchmarks/your_benchmark/analysis/uchannelrho.cxx 
0364 git add benchmarks/your_benchmark/analyze.sh
0365 git add benchmarks/your_benchmark/macros/plot_rho_physics_benchmark.C 
0366 git add benchmarks/your_benchmark/macros/RiceStyle.h 
0367 git add benchmarks/your_benchmark/reconstruct.sh
0368 git add benchmarks/your_benchmark/setup.config
0369 git add benchmarks/your_benchmark/simulate.sh
0370 
0371 git commit -m "I'm beefing up my benchmark!"
0372 git push origin pr/your_benchmark_<mylastname>
0373 ```
0374 
0375 Now monitor the pipeline you created:
0376 
0377 - [physics benchmark pipelines](https://eicweb.phy.anl.gov/EIC/benchmarks/physics_benchmarks/-/pipelines)
0378 - [detector benchmark pipleines](https://eicweb.phy.anl.gov/EIC/benchmarks/detector_benchmarks/-/pipelines)
0379 
0380 ::::::::::::::::::::::::::::::::::::::::::::::: keypoints
0381 
0382 - Create `setup.config` to switch between using the simulation campaign and re-simulating events
0383 - Each stage of the benchmark pipeline is defined in `config.yml`
0384 - `config.yml` takes normal bash scripts as input
0385 - Copy resulting figures over to the `results` directory to turn them into artifacts
0386 
0387 :::::::::::::::::::::::::::::::::::::::::::::::