Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:51

0001 #!/bin/bash
0002 
0003 # directory to search for RECO files
0004 reco_dir=""
0005 
0006 # directory with alignment code and results
0007 top_dir=`pwd -P`
0008 
0009 # template for CMSSW config file
0010 template="tb_template.py"
0011 
0012 # file names
0013 cfg_file="cfg.py"
0014 log_file="log"
0015 job_file="job"
0016 finished_file="finished"
0017 time_file="timestamp"
0018 
0019 # settings for usin LXBATCH
0020 bsub_options="-o /dev/null -e /dev/null"
0021 
0022 # name settings for subdirectory structure
0023 subdir="tb"
0024 label=""
0025 
0026 # name of geometry file
0027 geometry=""
0028 
0029 # rarely changing settings for alignment module
0030 iterationsWithOneRotZPerPot="0"
0031 runsWithoutHorizontalRPs=""
0032 
0033 # alignment result that is to be used as input for the next iteration
0034 iterationFile='results_cumulative_factored_Jan.xml'
0035 
0036 #----------------------------------------------------------------------------------------------------
0037 
0038 job_dir=""
0039 
0040 function InitJobDir()
0041 {
0042         job_id="$1"
0043     job_dir="$top_dir/$job_id"
0044     job_dir_rel="$job_id"
0045 
0046         rm -rf "$job_dir"
0047         mkdir -p "$job_dir"
0048 }
0049 
0050 #----------------------------------------------------------------------------------------------------
0051 
0052 function CleanJobDir()
0053 {
0054         echo ""
0055         #echo "rm -rf \"$job_dir\""
0056 }
0057 
0058 #----------------------------------------------------------------------------------------------------
0059 
0060 function BeginJobScript()
0061 {
0062         cwd=`pwd -P`
0063 
0064         echo "#!/bin/sh"
0065         echo "job_home=\"$cwd\""
0066         #echo "export HOME=\"$cwd\""
0067         echo "source \"/cvmfs/cms.cern.ch/cmsset_default.sh\""
0068         echo "cd \"$CMSSW_BASE\""
0069         echo "eval \`scram runtime -sh\`"
0070         echo ""
0071         echo "export CMSSW_SEARCH_PATH=\"\$CMSSW_SEARCH_PATH:$top_dir\""
0072         echo "export CMSSW_DATA_PATH=\"/afs\""
0073         echo ""
0074         echo "job_dir=\"$job_dir\""
0075         echo ""
0076         echo "cd \"\$job_home\""
0077 }
0078 
0079 #----------------------------------------------------------------------------------------------------
0080 
0081 function EndJobScript()
0082 {
0083         CleanJobDir
0084 
0085         echo "touch \"\$job_dir/$finished_file\""
0086 }
0087 
0088 #----------------------------------------------------------------------------------------------------
0089 
0090 function MakeJobScript()
0091 {
0092         (
0093                 BeginJobScript
0094 
0095                 for i in `seq $iterations`
0096                 do
0097                         echo ""
0098                         echo "### iteration $i ###"
0099                         echo "echo \"* iteration $i\""
0100                         echo "date > \"\$job_dir/iteration$i/$time_file\""
0101                         echo "cmsRun \"\$job_dir/iteration$i/$cfg_file\" &> \"\$job_dir/iteration$i/$log_file\""
0102                 done
0103                 
0104                 echo ""
0105                 echo "echo \"iterations finished\""
0106 
0107                 EndJobScript
0108         ) > "$job_dir/$job_file"
0109 
0110         chmod a+x "$job_dir/$job_file"
0111 }
0112 
0113 #----------------------------------------------------------------------------------------------------
0114 
0115 function AddInputFiles()
0116 {
0117     run="$1"
0118 
0119     # query the relevant storage
0120         if [[ "$reco_dir" == "/eos/"* ]]
0121         then
0122                 prefix="root://eostotem.cern.ch/"
0123                 file_list_raw=`eos ls "$reco_dir" | grep "run_${run}.*.root"`
0124         fi
0125 
0126         if [[ "$reco_dir" == "/castor/"* ]]
0127         then
0128                 prefix="rfio://"
0129                 file_list_raw=`nsls "$reco_dir" | grep "run_${run}.*.root"`
0130         fi
0131 
0132         if [ "$prefix" == "" ]
0133         then
0134                 prefix="file://"
0135                 file_list_raw=`ls -1 "$reco_dir" | grep "reco_${run}.*_reco.root"`
0136         fi
0137 
0138     # append the result to the file list
0139         for file in $file_list_raw
0140     do
0141         if [ -n "$file_list" ]
0142         then
0143             file_list="$file_list,\n"
0144         fi
0145 
0146         file_list="$file_list        '$prefix$reco_dir/$file'"
0147     done
0148 }
0149 
0150 #----------------------------------------------------------------------------------------------------
0151 
0152 function MakeCfgFiles()
0153 {
0154     # build input file list
0155         prefix=""
0156     file_list=""
0157         ls_list=""
0158 
0159         runs=($task)
0160         for runLsSpec in "${runs[@]}"
0161     do
0162                 local run="${runLsSpec%.*}"
0163                 local lsSpec="${runLsSpec#*.}"
0164 
0165         if [ -n "$ls_list" ]
0166         then
0167             ls_list="$ls_list, "
0168         fi
0169 
0170                 if [ "$lsSpec" == "$runLsSpec" ]
0171                 then
0172                         ls_list="${ls_list}\"$run:1-$run:999999\""
0173                 else
0174                         local ls_min="${lsSpec%-*}"
0175                         local ls_max="${lsSpec#*-}"
0176                         ls_list="${ls_list}\"$run:${ls_min}-$run:${ls_max}\""
0177                 fi
0178 
0179         AddInputFiles "$run" 
0180     done
0181 
0182     # make iteration directories
0183         for i in `seq $iterations`
0184         do
0185                 dir="iteration$i"
0186                 mkdir -p "$dir"
0187 
0188                 # resolve initial alignments
0189                 alignmentFiles=""
0190             if [ $i -eq 1 ]
0191         then
0192                         for af in ${initial_alignment[*]}
0193                         do
0194                                 #if [ ${af:0:1} != "/" ]; then af="${job_dir}/$af"; fi
0195 
0196                                 if [ -n "$alignmentFiles" ]; then alignmentFiles="$alignmentFiles, "; fi
0197                                 alignmentFiles="${alignmentFiles}'${af}'"
0198                         done
0199         else
0200                     let p=i-1
0201                         alignmentFiles="'$job_dir_rel/iteration$p/${iterationFile}'"
0202                 fi
0203 
0204                 # what is to be optimized in this iteration
0205         let li=${#optimize[@]}-1
0206         if [ $i -le $li ]; then let li=$i-1; fi
0207         optimize_value=${optimize[$li]}
0208 
0209                 resolveShR=False
0210                 resolveRotZ=False
0211                 if [ -n "`echo $optimize_value|grep s`" ]; then resolveShR=True; fi
0212                 if [ -n "`echo $optimize_value|grep r`" ]; then resolveRotZ=True; fi
0213 
0214                 # what constraints to be applied
0215         #let li=${#useExtendedConstraints[@]}-1
0216         #if [ $i -le $li ]; then let li=$i-1; fi
0217         #useExtendedConstraints_value=${useExtendedConstraints[$li]}
0218 
0219                 # use 1 rotz per RP?
0220                 oneRotZPerPot="False"
0221                 if [ $i -le "$iterationsWithOneRotZPerPot" ]; then oneRotZPerPot="True"; fi
0222 
0223                 # chi^2/ndf cut for this iteration
0224         let li=${#chiSqPerNdfCut[@]}-1
0225         if [ $i -le $li ]; then let li=$i-1; fi
0226         chiSqPerNdfCut_value=${chiSqPerNdfCut[$li]}
0227         
0228                 # R/sigma cut for this iteration 
0229                 let li=${#maxResidualToSigma[@]}-1
0230         if [ $i -le $li ]; then let li=$i-1; fi
0231         maxResidualToSigma_value=${maxResidualToSigma[$li]}
0232 
0233                 # max a_x cut for this iteration
0234         let li=${#maxTrackAx[@]}-1
0235         if [ $i -le $li ]; then let li=$i-1; fi
0236         maxTrackAx_value=${maxTrackAx[$li]}
0237 
0238                 # max a_y cut for this iteration
0239         let li=${#maxTrackAy[@]}-1
0240         if [ $i -le $li ]; then let li=$i-1; fi
0241         maxTrackAy_value=${maxTrackAy[$li]}
0242 
0243                 # build diagnotic plots?
0244         buildDiagnosticPlots="False"
0245         if [ "$i" -le "$iterationsWithDiagnostics" ]; then buildDiagnosticPlots="True"; fi
0246 
0247                 # fill in template
0248                 cat "$top_dir/$template" | sed "\
0249                                 s|\$inputFiles|$file_list|;\
0250                                 s|\$lsList|$ls_list|;\
0251                                 s|\$optimize|$optimize_value|;\
0252                                 s|\$rps|$rps|;\
0253                                 s|\$excludePlanes|$excludePlanes|;\
0254                                 s|\$z0|$z0|;\
0255                                 s|\$resolveShR|$resolveShR|;\
0256                                 s|\$resolveRotZ|$resolveRotZ|;\
0257                                 s|\$constraintsType|$constraintsType|;\
0258                                 s|\$fixed_planes_shr|$fixed_planes_shr|;\
0259                                 s|\$fixed_planes_values_shr|$fixed_planes_values_shr|;\
0260                                 s|\$fixed_planes_rotz|$fixed_planes_rotz|;\
0261                                 s|\$fixed_planes_values_rotz|$fixed_planes_values_rotz|;\
0262                                 s|\$geometry|$geometry|;\
0263                                 s|\$reco_dir|$reco_dir|;\
0264                                 s|\$results_dir|$job_dir/$dir|;\
0265                                 s|\$chiSqPerNdfCut|$chiSqPerNdfCut_value|;\
0266                                 s|\$alignmentFiles|$alignmentFiles|;\
0267                                 s|\$useExtendedConstraints|$useExtendedConstraints_value|;\
0268                                 s|\$useZeroThetaRotZConstraint|$useZeroThetaRotZConstraint|;\
0269                                 s|\$minimumHitsPerProjectionPerRP|$minimumHitsPerProjectionPerRP|;\
0270                                 s|\$maxResidualToSigma|$maxResidualToSigma_value|;\
0271                                 s|\$oneRotZPerPot|$oneRotZPerPot|;\
0272                                 s|\$requireNumberOfUnits|$requireNumberOfUnits|;\
0273                                 s|\$requireOverlap|$requireOverlap|;\
0274                                 s|\$requireAtLeast3PotsInOverlap|$requireAtLeast3PotsInOverlap|;\
0275                                 s|\$buildDiagnosticPlots|$buildDiagnosticPlots|;\
0276                                 s|\$maxTrackAx|$maxTrackAx_value|;\
0277                                 s|\$maxTrackAy|$maxTrackAy_value|;\
0278                                 s|\$useEqualMeanUMeanVRotZConstraints|$useEqualMeanUMeanVRotZConstraints|;\
0279                                 s|\$final_constraints_units|$final_constraints_units|;\
0280                                 s|\$maxEvents|$maxEvents|;\
0281                                 s|\$additionalAcceptedRPSets|$additionalAcceptedRPSets|;\
0282                         " > "$dir/$cfg_file"
0283         done
0284 }
0285 
0286 #----------------------------------------------------------------------------------------------------
0287 
0288 function SubmitOrExecute()
0289 {
0290         if [ "$test_only" == "y" ]
0291         then
0292                 echo "    execution inhibited (test_only=t)"
0293                 return 1
0294         fi
0295 
0296         if [ "$use_lxbatch" == "y" ]
0297         then
0298                 #echo "bsub $job_dir/$job_file"
0299                 res=`bsub -q $queue $bsub_options "$job_dir/$job_file"`
0300                 echo "    $res"
0301                 echo "$res" > "$job_dir/submitted"
0302         else
0303                 #echo "$job_dir/$job_file"
0304                 $job_dir/$job_file &
0305                 echo "    job executed in background"
0306         fi
0307 }
0308 
0309 #----------------------------------------------------------------------------------------------------
0310 
0311 function AddToCaption()
0312 {
0313         val="f"
0314         if [ "$2" == "True" ]; then val="t"; fi
0315         caption="${caption},$1=$val"
0316 }
0317 
0318 #----------------------------------------------------------------------------------------------------
0319 
0320 function CompileCaption()
0321 {
0322         opt_lab=""
0323         opt_prev=""
0324         for opt in "${optimize[@]}"
0325         do
0326                 if [ "$opt_prev" != "$opt" ]
0327                 then
0328                         if [ -n "$opt_lab" ]; then opt_lab="$opt_lab+"; fi
0329                         opt_lab="$opt_lab$opt"
0330                 fi
0331                 opt_prev="$opt"
0332         done
0333 
0334         caption="ev=${maxEvents},pl=${minimumHitsPerProjectionPerRP},units=${requireNumberOfUnits}"
0335         AddToCaption "ovlp" "$requireOverlap"
0336         AddToCaption "3rpsInO" "$requireAtLeast3PotsInOverlap"
0337         
0338         caption="$caption/${opt_lab},${constraintsLabel},1rotzIt=${iterationsWithOneRotZPerPot}"
0339         AddToCaption "eMuMvRot" "$useEqualMeanUMeanVRotZConstraints"
0340 
0341         caption="${caption}${label}"
0342 }
0343 
0344 #----------------------------------------------------------------------------------------------------
0345 
0346 function SubmitTrackBasedAlignment()
0347 {
0348         CompileCaption
0349 
0350         for task in "${tasks[@]}"
0351         do
0352             runs=($task)
0353         task_id=""
0354             for run in "${runs[@]}"
0355             do
0356                 if [ -n "$task_id" ]
0357                 then
0358                     task_id="$task_id,"
0359                 fi
0360 
0361                 task_id="${task_id}${run}"
0362             done
0363 
0364                 task_id=${task_id//</}
0365                 task_id=${task_id//>/}
0366 
0367                 input_tag="$rps"
0368                 if [ -n "$excludePlanes" ]
0369                 then
0370                         input_tag="${input_tag}-excl${excludePlanes}"
0371                 fi
0372 
0373                 id="${subdir}/${task_id}/${input_tag}/${caption}"
0374                 
0375                 echo "> $id"
0376 
0377                 InitJobDir "$id"
0378                 cd "$job_dir"
0379                 
0380                 MakeJobScript
0381                 MakeCfgFiles
0382         
0383                 SubmitOrExecute
0384         done
0385 }
0386 
0387 #----------------------------------------------------------------------------------------------------
0388 
0389 function NewDataSet()
0390 {
0391         # reset standard settings
0392     tasks=""
0393 }