#!/bin/tcsh

source ~cdfsoft/cdf2.cshrc
setup sam
setup cdfsoft2

setenv do_sam_test 1

# used to show time stamp
alias tsecho 'echo `date "+%Y/%m/%d %H:%M:%S"` = '

setenv src_hist_dir	/cdf/opr/cdfprd/BeamExe_5.3.1_1/hist
setenv sum_hist_dir	sum_hist
setenv bad_hist_dir	bad_hist
setenv history_dir	history
setenv log_dir		logs

setenv working_dir	/cdf/opr/cdfprd/concate_hist

setenv log		${src_hist_dir}/${log_dir}/concate_hist_`date "+%Y%m%d%H%M%S"`.log

mkdir -p ${src_hist_dir}/${sum_hist_dir}
mkdir -p ${src_hist_dir}/${bad_hist_dir}
mkdir -p ${src_hist_dir}/${history_dir}
mkdir -p ${src_hist_dir}/${log_dir}

if ( -f lock ) then
	tsecho Another concatenate_histograms is running, exit >> ${log}
	exit 0
endif

#foreach try (first second final)
#	tsecho ${try} to detect concatenate_histograms >> ${log}
#	ps -C concatenate_histograms >& /dev/null
#	setenv test $?
#
#	if ${test} == 0 then
#		tsecho Another concatenate_histograms is running, exit >> ${log}
#		exit  1
#	endif
#
#	sleep 2
#end

tsecho no concatenate_histograms is running, do concatenation >> ${log}

cd ${working_dir}

tsecho make lock >> ${log}
touch lock

# input file name	Hist186318_i.1.ir02d7ce.0001phys.root
# output file name	sum_hist_190697_g.root
foreach hist (`ls ${src_hist_dir}/Hist*_?.?.*.root`)
	setenv bnfn `basename ${hist}`
	setenv run `echo ${bnfn} | awk -F_ '{print substr($1, 5)}'`
	setenv stream `echo ${bnfn} | awk -F_ '{print substr($2, 1, 1)}'`
	setenv history_log sum_hist_${run}_${stream}.lst
	setenv TheOutputFile sum_hist_${run}_${stream}.root


	# history check
	touch ${src_hist_dir}/${history_dir}/${history_log}
	setenv doit 0
	foreach done_hist (`cat ${src_hist_dir}/${history_dir}/${history_log} | awk -F, '{print $1}'`)
		if ${bnfn} == ${done_hist} then
			setenv doit 1
			break
		endif
	end
	if ${doit} == 1 then
		tsecho ${hist} had been concatenated before, skip >> ${log}
		continue
	else
		tsecho ${hist} not yet been concatenated before, do it >> ${log}
	endif


	# SAM test
	if ${do_sam_test} == 1 then
		tsecho sam locate ${bnfn} >> ${log}
		sam locate ${bnfn} >>& ${log}
		if $? != 0 then
			tsecho From SAM we miss ${bnfn}, skip it to continue >> ${log}
			continue
		endif
	endif


	tsecho ./concatenate_histograms ${hist} -o ${src_hist_dir}/${sum_hist_dir}/${TheOutputFile} >> ${log}

	./concatenate_histograms ${hist} -o ${src_hist_dir}/${sum_hist_dir}/${TheOutputFile} >>& ${log}
	if $? == 0 then
		tsecho process ${hist} ok >> ${log}
		echo ${bnfn},${TheOutputFile} >> ${src_hist_dir}/${history_dir}/${history_log}
		tsecho remove ${hist} >> ${log}
		#/bin/rm -f ${hist}
	else
		tsecho process ${hist} failed >> ${log}
		tsecho move ${hist} to ${src_hist_dir}/${bad_hist_dir}/BAD_${bnfn} >> ${log}
		touch ${bad_hist_dir}/BAD_${bnfn}
		#/bin/mv -f ${hist} ${src_hist_dir}/${bad_hist_dir}/BAD_${bnfn}
	endif

	tsecho finish to process ${hist} to ${src_hist_dir}/${sum_hist_dir}/${TheOutputFile} >> ${log}
end

tsecho remove lock file >> ${log}
/bin/rm -f lock
tsecho exit >> ${log}

gzip ${log}

exit 0

