#!/usr/bin/perl

## usage: give BEGIN and END days as an input parameter

my $year  = 2002;
my $begin = $ARGV[0];
my $end   = $ARGV[1];
my $chain = $ARGV[2];

if (!(($chain eq "comp")||($chain eq "point"))) {
    die "Hey you! usage:\n reconstruct 302 305 comp\n";
}

my $dir;
my @listoffiles;

#$dir = "/net/dsk12/2002/s2ab_2002";
$dir = "/data/csong";

@listoffiles = ($begin..$end);

my $bin_dir = "/home/csong/bin.Linux";
my $deff = "$bin_dir/deff";
my $recoos = "$bin_dir/recoos";
my $munt = "$bin_dir/minimunt";
my @segs;
my $outfile;
my $cmd;

#################### RECOOS STUFF #############

## reco's 1-5 already exist from ofip:
##    1. Direct walk
##    2. WIMP
##    3. CFirst
##    4. UPandel
##    5. CascSPE
##    6. CascMPE
##    7. JAMS
##    8. SPASE

## standard parameters
## -p n=1000:4500       noise rate:time window
## -p j=15              PMT jitter
## -p a=96              absorption
## -X o=2               hole ice
## -X g=f               center on first residual
my $recopars = "-p n=1000:7000 -p j=15 -p a=96. -X o=2"; 


############## Chain for composition ###################

## almost standard parameters
## -p t=1:5             min strings:channels
my $spasepars = "-X s=n -p t=1:1";

## fixed-core with classic Upandel, no restrictions
## Fit #9
my $fixedcore = "$recoos -rm -if -mp -z a_upandel"
    . " -p f=8"
    . " -l xyzt,za -x zenith,azimuth,time"
    . " $spasepars $recopars";
## ADC/LDF fit for composition
## Fit #10
my $adcldf = "$recoos -rm -if -mp -z a_spaseadc2"
    . " -p f=9"
    . " -l energy,length -x energy,length"
    . " $spasepars $recopars";

########### Chain for calibration and pointing ##########
my $amandapars = "-X s=o -p t=1:5";

## Classic UPandel
## 9 
my $classic_jm = "$recoos -rm -if -mp -z a_upandel "
    . " -p f=7"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";
## 10
my $classic_dw = "$recoos -rm -if -mp -z a_upandel "
    . " -p f=1"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";
## 11
my $classic_sp = "$recoos -rm -if -mp -z a_upandel "
    . " -p f=8"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";

## Inverted Bayesian prior
## 12
my $invertedbayes_jm = "$recoos -rm -if -mp -z a_upandel+a_izwght "
    . " -p f=7"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";
## 12
my $invertedbayes_dw = "$recoos -rm -if -mp -z a_upandel+a_izwght "
    . " -p f=1"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";
## 14
my $invertedbayes_sp = "$recoos -rm -if -mp -z a_upandel+a_izwght "
    . " -p f=8"
    . " -x x,y,z,zenith,azimuth "
    . " $amandapars $recopars";




## Flags for checking the ADC gate
my $mblocks = "-XPU";
my $mflags = "-v -L";
my $head = "s2ab_"."$year"."_L1";

foreach $file (@listoffiles) {

    $comp_outfile = "$head.$file.comp.cal.f2k";
    $point_outfile = "$head.$file.point.cal.f2k";


    if ($chain eq "comp") {
	## Composition chain
	$cmd = "gunzip -c $dir/$head.$file.cal.f2k.gz ".
	    "| $fixedcore ".
            "| $adcldf ".
	    "> $comp_outfile";
	
	print "$cmd\n";
	`$cmd`;
	`gzip $comp_outfile`;
    } elsif ($chain eq "point") {
	## Pointing chain
	$cmd = "gunzip -c $dir/s2ab_$year_L1.$file.f2k.gz ".
	"| $classic_jm ".
	"| $classic_dw ".
	"| $classic_sp ".
	"| $invertedbayes_jm ".
	"| $invertedbayes_dw ".
	"| $invertedbayes_sp ".
	"> $point_outfile";

	print "$cmd\n";
	`$cmd`;
	`gzip $point_outfile`;
    } else {
	print "Warning... should not be here!\n";
    }
}


