Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-17 08:10:30

0001 #!/usr/bin/env bash
0002 
0003 # This script updates the `Det` fields in the Flair file, to the ones observed during the simulations.
0004 # This is needed because the observed particles in the final state are DYNAMICALLY observed: 
0005 # the number of particles changes from one simulation to the next, depending on the physics scenario, the number of events, etc.
0006 # Hence, one needs to look what is the index of the `protons` histogram in the .hist file, etc.
0007 # The use of this script is not needed in the XS G4 example case, because there, a FIXED number of XS are printed in the .hist files.
0008 
0009 # Choose input files / number of comparison plots here
0010 flair_file="study_final_state.flair"
0011 num_comparison_plots=3
0012 plot_block_max_length=100
0013 
0014 
0015 # All plots
0016 all_particles=$(cat $flair_file | grep "Plot:" | cut -d' ' -f2)
0017 
0018 # Loop on all plots
0019 for particle in ${all_particles[@]}; do
0020 
0021         echo "particle=$particle";
0022         
0023         # Line number of the "Plot: " block.
0024         plot_line_number=$(grep -n -m1 "Plot: $particle" $flair_file | cut -d':' -f1)
0025         echo "plot_line_number=$plot_line_number"
0026         
0027         # Find out if last plot block in the flair file.
0028         next_plot=$(grep -A$plot_block_max_length "Plot: $particle" $flair_file | grep -n -m2 "Plot: " | wc -l)
0029         if [ "$next_plot" -eq "2" ]; then
0030                 has_next_plot=true
0031         else
0032                 has_next_plot=false
0033         fi
0034         echo "has_next_plot=$has_next_plot";
0035         
0036         # Line number of the next "Plot: " block.
0037         if [ "$has_next_plot" = true ] ; then
0038                 next_plot_line_number=$(grep -A$plot_block_max_length "Plot: $particle" $flair_file | grep -n -m2 "Plot: " | tail -n1 | cut -d':' -f1)
0039                 # (next_plot_line_number - 1) is the extra number of lines taken by the plot block.
0040                 next_plot_line_number=$(($plot_line_number + $next_plot_line_number - 1)) 
0041         else
0042                 # Last line in the flair file.
0043                 next_plot_line_number=$(cat $flair_file | wc -l)
0044         fi
0045         echo "next_plot_line_number=$next_plot_line_number"
0046         
0047         # Loop on all comparison plots.
0048         for ((i=0; i < $num_comparison_plots; i++)); do
0049                 data_file=$(sed -n "$plot_line_number,$next_plot_line_number p" $flair_file | grep "file\.$i" | cut -d' ' -f2)
0050                 echo "data_file=$data_file"
0051                 # Data file not found: do nothing.
0052                 if [ -z "$data_file" ]; then 
0053                         echo "Warning: Tried to look for data file $data_file, which does not exist! Field is not updated for this file."
0054                 # Found the data file.
0055                 else
0056                         # Get detector value in the data file.
0057                         data_det=$(grep "# Detector:.* $particle" $data_file | cut -d' ' -f4)                   
0058                         data_det=$(($data_det - 1)) # An extra -1 because Flair detector indexing starts from 0.
0059                         echo "data_det=$data_det"
0060         
0061                         # Found detector value in data file.
0062                         if [ ! -z "$data_det" ]; then
0063                                         
0064                                 # Look for detector field in the flair file.
0065                                 flair_det=$(sed -n "$plot_line_number,$next_plot_line_number p" $flair_file | grep "det\.$i")
0066                                 echo "flair_det=$flair_det"
0067                         
0068                                 # Update value in detector field in flair file.
0069                                 if [ ! -z "$flair_det" ]; then
0070                                         sed -i "$plot_line_number,$next_plot_line_number s/det\.$i.*/det\.$i: $data_det/g" $flair_file
0071                                 # Flair file has no detector field in that plot: do not update field.
0072                                 else
0073                                         #sed -i "/file\.$i/i \ \tdet\.$i: $data_det" $flair_file
0074                                         echo "Warning: No det.$i defined for plot $particle, file $data_file: field was not updated."
0075                                 fi
0076                                 
0077                         # Detector not found in data file: remove value in detector field in flair file.
0078                         else
0079                                 sed -i "$plot_line_number,$next_plot_line_number s/det\.$i.*//" $flair_file
0080                         fi
0081                 fi
0082         done
0083         
0084 done