#!/usr/bin/perl -w

$dir="/data/weidong/odb/";
$file="run";
$num1="21136";
$num2="21137";
$ext=".odb";

if(!open(FIN2,"<time0.dat")) {
    print "can't open time0.dat";
}


for($num=$num1;$num<$num2;$num++) {

    $fin="<".$dir.$file.$num.$ext;
    print $fin, "\n";
    $fout=">".$dir."ntof_3".$num.$ext;
    print $fout, "\n";
    if(!open(FIN, $fin)) {
	print "cant open $fin\n";
    }
    
    if(!open(FOUT, $fout)) {
	print "cant open $fout\n";
    }
    if(!open(FIN2,"<time0.dat")) {
	print "can't open time0.dat";
    }
    
    while($line=<FIN>) {
	$_=$line;
	if(m!/TDC calibration]!) {
	    print FOUT $line;
	    $line=<FIN>;
	    print FOUT $line;
	    $line=<FIN>;
	    print FOUT $line;
	    for($i=0;$i<220;$i++) {
		$old = <FIN>;
		@array = split(/ +/,$old);
		
		$correct = <FIN2>;
		chomp($correct);
		$new = $array[1]+$correct;
		print FOUT "[$i] $new\n";
	    }
	    for($i=220;$i<480;$i++) {
		$old = <FIN>;
		print FOUT $old;
	    }
	    last;
	}
    }
    close(FIN);
    close(FOUT);
    close(FIN2);
}






