Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#!/bin/bash

# directory to search for RECO files
reco_dir=""

# directory with alignment code and results
top_dir=`pwd -P`

# template for CMSSW config file
template="tb_template.py"

# file names
cfg_file="cfg.py"
log_file="log"
job_file="job"
finished_file="finished"
time_file="timestamp"

# settings for usin LXBATCH
bsub_options="-o /dev/null -e /dev/null"

# name settings for subdirectory structure
subdir="tb"
label=""

# name of geometry file
geometry=""

# rarely changing settings for alignment module
iterationsWithOneRotZPerPot="0"
runsWithoutHorizontalRPs=""

# alignment result that is to be used as input for the next iteration
iterationFile='results_cumulative_factored_Jan.xml'

#----------------------------------------------------------------------------------------------------

job_dir=""

function InitJobDir()
{
	job_id="$1"
    job_dir="$top_dir/$job_id"
    job_dir_rel="$job_id"

	rm -rf "$job_dir"
	mkdir -p "$job_dir"
}

#----------------------------------------------------------------------------------------------------

function CleanJobDir()
{
	echo ""
	#echo "rm -rf \"$job_dir\""
}

#----------------------------------------------------------------------------------------------------

function BeginJobScript()
{
	cwd=`pwd -P`

	echo "#!/bin/sh"
	echo "job_home=\"$cwd\""
	#echo "export HOME=\"$cwd\""
	echo "source \"/cvmfs/cms.cern.ch/cmsset_default.sh\""
	echo "cd \"$CMSSW_BASE\""
	echo "eval \`scram runtime -sh\`"
	echo ""
	echo "export CMSSW_SEARCH_PATH=\"\$CMSSW_SEARCH_PATH:$top_dir\""
	echo "export CMSSW_DATA_PATH=\"/afs\""
	echo ""
	echo "job_dir=\"$job_dir\""
	echo ""
	echo "cd \"\$job_home\""
}

#----------------------------------------------------------------------------------------------------

function EndJobScript()
{
	CleanJobDir

	echo "touch \"\$job_dir/$finished_file\""
}

#----------------------------------------------------------------------------------------------------

function MakeJobScript()
{
	(
		BeginJobScript

		for i in `seq $iterations`
		do
			echo ""
			echo "### iteration $i ###"
			echo "echo \"* iteration $i\""
			echo "date > \"\$job_dir/iteration$i/$time_file\""
			echo "cmsRun \"\$job_dir/iteration$i/$cfg_file\" &> \"\$job_dir/iteration$i/$log_file\""
		done
		
		echo ""
		echo "echo \"iterations finished\""

		EndJobScript
	) > "$job_dir/$job_file"

	chmod a+x "$job_dir/$job_file"
}

#----------------------------------------------------------------------------------------------------

function AddInputFiles()
{
    run="$1"

    # query the relevant storage
	if [[ "$reco_dir" == "/eos/"* ]]
	then
		prefix="root://eostotem.cern.ch/"
		file_list_raw=`eos ls "$reco_dir" | grep "run_${run}.*.root"`
	fi

	if [[ "$reco_dir" == "/castor/"* ]]
	then
		prefix="rfio://"
		file_list_raw=`nsls "$reco_dir" | grep "run_${run}.*.root"`
	fi

	if [ "$prefix" == "" ]
	then
		prefix="file://"
		file_list_raw=`ls -1 "$reco_dir" | grep "reco_${run}.*_reco.root"`
	fi

    # append the result to the file list
	for file in $file_list_raw
    do
        if [ -n "$file_list" ]
        then
            file_list="$file_list,\n"
        fi

        file_list="$file_list        '$prefix$reco_dir/$file'"
    done
}

#----------------------------------------------------------------------------------------------------

function MakeCfgFiles()
{
    # build input file list
	prefix=""
    file_list=""
	ls_list=""

	runs=($task)
	for runLsSpec in "${runs[@]}"
    do
		local run="${runLsSpec%.*}"
		local lsSpec="${runLsSpec#*.}"

        if [ -n "$ls_list" ]
        then
            ls_list="$ls_list, "
        fi

		if [ "$lsSpec" == "$runLsSpec" ]
		then
			ls_list="${ls_list}\"$run:1-$run:999999\""
		else
			local ls_min="${lsSpec%-*}"
			local ls_max="${lsSpec#*-}"
			ls_list="${ls_list}\"$run:${ls_min}-$run:${ls_max}\""
		fi

        AddInputFiles "$run" 
    done

    # make iteration directories
	for i in `seq $iterations`
	do
		dir="iteration$i"
		mkdir -p "$dir"

		# resolve initial alignments
		alignmentFiles=""
	    if [ $i -eq 1 ]
        then
			for af in ${initial_alignment[*]}
			do
				#if [ ${af:0:1} != "/" ]; then af="${job_dir}/$af"; fi

				if [ -n "$alignmentFiles" ]; then alignmentFiles="$alignmentFiles, "; fi
				alignmentFiles="${alignmentFiles}'${af}'"
			done
        else
		    let p=i-1
			alignmentFiles="'$job_dir_rel/iteration$p/${iterationFile}'"
		fi

		# what is to be optimized in this iteration
        let li=${#optimize[@]}-1
        if [ $i -le $li ]; then let li=$i-1; fi
        optimize_value=${optimize[$li]}

		resolveShR=False
		resolveRotZ=False
		if [ -n "`echo $optimize_value|grep s`" ]; then resolveShR=True; fi
		if [ -n "`echo $optimize_value|grep r`" ]; then resolveRotZ=True; fi

		# what constraints to be applied
        #let li=${#useExtendedConstraints[@]}-1
        #if [ $i -le $li ]; then let li=$i-1; fi
        #useExtendedConstraints_value=${useExtendedConstraints[$li]}

		# use 1 rotz per RP?
		oneRotZPerPot="False"
		if [ $i -le "$iterationsWithOneRotZPerPot" ]; then oneRotZPerPot="True"; fi

		# chi^2/ndf cut for this iteration
        let li=${#chiSqPerNdfCut[@]}-1
        if [ $i -le $li ]; then let li=$i-1; fi
        chiSqPerNdfCut_value=${chiSqPerNdfCut[$li]}
        
		# R/sigma cut for this iteration 
		let li=${#maxResidualToSigma[@]}-1
        if [ $i -le $li ]; then let li=$i-1; fi
        maxResidualToSigma_value=${maxResidualToSigma[$li]}

		# max a_x cut for this iteration
        let li=${#maxTrackAx[@]}-1
        if [ $i -le $li ]; then let li=$i-1; fi
        maxTrackAx_value=${maxTrackAx[$li]}

		# max a_y cut for this iteration
        let li=${#maxTrackAy[@]}-1
        if [ $i -le $li ]; then let li=$i-1; fi
        maxTrackAy_value=${maxTrackAy[$li]}

		# build diagnotic plots?
        buildDiagnosticPlots="False"
        if [ "$i" -le "$iterationsWithDiagnostics" ]; then buildDiagnosticPlots="True"; fi

		# fill in template
		cat "$top_dir/$template" | sed "\
				s|\$inputFiles|$file_list|;\
				s|\$lsList|$ls_list|;\
				s|\$optimize|$optimize_value|;\
				s|\$rps|$rps|;\
				s|\$excludePlanes|$excludePlanes|;\
				s|\$z0|$z0|;\
				s|\$resolveShR|$resolveShR|;\
				s|\$resolveRotZ|$resolveRotZ|;\
				s|\$constraintsType|$constraintsType|;\
				s|\$fixed_planes_shr|$fixed_planes_shr|;\
				s|\$fixed_planes_values_shr|$fixed_planes_values_shr|;\
				s|\$fixed_planes_rotz|$fixed_planes_rotz|;\
				s|\$fixed_planes_values_rotz|$fixed_planes_values_rotz|;\
				s|\$geometry|$geometry|;\
				s|\$reco_dir|$reco_dir|;\
				s|\$results_dir|$job_dir/$dir|;\
				s|\$chiSqPerNdfCut|$chiSqPerNdfCut_value|;\
				s|\$alignmentFiles|$alignmentFiles|;\
				s|\$useExtendedConstraints|$useExtendedConstraints_value|;\
				s|\$useZeroThetaRotZConstraint|$useZeroThetaRotZConstraint|;\
				s|\$minimumHitsPerProjectionPerRP|$minimumHitsPerProjectionPerRP|;\
				s|\$maxResidualToSigma|$maxResidualToSigma_value|;\
				s|\$oneRotZPerPot|$oneRotZPerPot|;\
				s|\$requireNumberOfUnits|$requireNumberOfUnits|;\
				s|\$requireOverlap|$requireOverlap|;\
				s|\$requireAtLeast3PotsInOverlap|$requireAtLeast3PotsInOverlap|;\
				s|\$buildDiagnosticPlots|$buildDiagnosticPlots|;\
				s|\$maxTrackAx|$maxTrackAx_value|;\
				s|\$maxTrackAy|$maxTrackAy_value|;\
				s|\$useEqualMeanUMeanVRotZConstraints|$useEqualMeanUMeanVRotZConstraints|;\
				s|\$final_constraints_units|$final_constraints_units|;\
				s|\$maxEvents|$maxEvents|;\
				s|\$additionalAcceptedRPSets|$additionalAcceptedRPSets|;\
			" > "$dir/$cfg_file"
	done
}

#----------------------------------------------------------------------------------------------------

function SubmitOrExecute()
{
	if [ "$test_only" == "y" ]
	then
		echo "    execution inhibited (test_only=t)"
		return 1
	fi

	if [ "$use_lxbatch" == "y" ]
	then
		#echo "bsub $job_dir/$job_file"
		res=`bsub -q $queue $bsub_options "$job_dir/$job_file"`
		echo "    $res"
		echo "$res" > "$job_dir/submitted"
	else
		#echo "$job_dir/$job_file"
		$job_dir/$job_file &
		echo "    job executed in background"
	fi
}

#----------------------------------------------------------------------------------------------------

function AddToCaption()
{
	val="f"
	if [ "$2" == "True" ]; then val="t"; fi
	caption="${caption},$1=$val"
}

#----------------------------------------------------------------------------------------------------

function CompileCaption()
{
	opt_lab=""
	opt_prev=""
	for opt in "${optimize[@]}"
	do
		if [ "$opt_prev" != "$opt" ]
		then
			if [ -n "$opt_lab" ]; then opt_lab="$opt_lab+"; fi
			opt_lab="$opt_lab$opt"
		fi
		opt_prev="$opt"
	done

	caption="ev=${maxEvents},pl=${minimumHitsPerProjectionPerRP},units=${requireNumberOfUnits}"
	AddToCaption "ovlp" "$requireOverlap"
	AddToCaption "3rpsInO" "$requireAtLeast3PotsInOverlap"
	
	caption="$caption/${opt_lab},${constraintsLabel},1rotzIt=${iterationsWithOneRotZPerPot}"
	AddToCaption "eMuMvRot" "$useEqualMeanUMeanVRotZConstraints"

	caption="${caption}${label}"
}

#----------------------------------------------------------------------------------------------------

function SubmitTrackBasedAlignment()
{
	CompileCaption

	for task in "${tasks[@]}"
	do
	    runs=($task)
        task_id=""
	    for run in "${runs[@]}"
	    do
	        if [ -n "$task_id" ]
	        then
	            task_id="$task_id,"
	        fi

	        task_id="${task_id}${run}"
	    done

		task_id=${task_id//</}
		task_id=${task_id//>/}

		input_tag="$rps"
		if [ -n "$excludePlanes" ]
		then
			input_tag="${input_tag}-excl${excludePlanes}"
		fi

		id="${subdir}/${task_id}/${input_tag}/${caption}"
		
		echo "> $id"

		InitJobDir "$id"
		cd "$job_dir"
		
		MakeJobScript
		MakeCfgFiles
	
		SubmitOrExecute
	done
}

#----------------------------------------------------------------------------------------------------

function NewDataSet()
{
	# reset standard settings
    tasks=""
}