#!/usr/bin/perl -w

use strict;

##### Modify this part ################

my $nStart = 2;
my $nStop  = 7;   #nStop<=21

my $macro1 = "calc_SBRatio_DchKpipi2.C";
my $macro2 = "histMk_1D_cosBY_BpBm.C";
my $macro3 = "histMk_1D_cosBY_B0B0.C";
my $macro4 = "histMk_1D_cosBY_cc.C";
my $macro5 = "histMk_1D_cosBY_uds.C";
my $macro6 = "histMk_1D_cosBY_tau.C";
my $macro7 = "histMk_1D_cosBY_OnPeak.C";
my $macro8 = "histMk_1D_LepMom_BpBm.C";
my $macro9 = "histMk_1D_LepMom_B0B0.C";
my $macro10 = "histMk_1D_LepMom_cc.C";
my $macro11 = "histMk_1D_LepMom_uds.C";
my $macro12 = "histMk_1D_LepMom_tau.C";
my $macro13 = "histMk_1D_LepMom_OnPeak.C";
my $macro14 = "histMk_1D_DMom_BpBm.C";
my $macro15 = "histMk_1D_DMom_B0B0.C";
my $macro16 = "histMk_1D_DMom_cc.C";
my $macro17 = "histMk_1D_DMom_uds.C";
my $macro18 = "histMk_1D_DMom_tau.C";
my $macro19 = "histMk_1D_DMom_OnPeak.C";
my $macro20 = "fit_3DMC.C";
my $macro21 = "fit_3D.C";

my $jobNameBase = "runRootJobs";
my $selQueue = 0;  #0=express, 1=general, 2=long

#######################################

my $workDir = "/home1x/hep/khamano/releases/BToDlnu-a26_16.1.5/workdir";
my $scrDir = "./runRoot";
#my $logDir = "/home1x/OtherMounts/hep03/khamano/log";
my $logDir = "log";
my $queue = "express";
my $walt = "walltime=05:00:00";
if ($selQueue == 1) {
    $queue = "general";
    $walt = "walltime=48:00:00";
}
if ($selQueue == 2) {
    $queue = "long";
    $walt = "walltime=248:00:00";
}

###################################################################

chdir "$workDir";

my $macro = "histMk_DMass_DataMC.C";
my $script = "runRootJobs-1.pl";
my $jobName = "rootJob-1";

my $j = $nStart;
while ($j <= $nStop) {
    if ($j==1) {
	$macro = $macro1;
	$script = "$jobNameBase-1.pl";
	$jobName = "rootJob-1";
    }
    if ($j==2) {
	$macro = $macro2;
	$script = "$jobNameBase-2.pl";
	$jobName = "rootJob-2";
    }
    if ($j==3) {
	$macro = $macro3;
	$script = "$jobNameBase-3.pl";
	$jobName = "rootJob-3";
    }
    if ($j==4) {
	$macro = $macro4;
	$script = "$jobNameBase-4.pl";
	$jobName = "rootJob-4";
    }
    if ($j==5) {
	$macro = $macro5;
	$script = "$jobNameBase-5.pl";
	$jobName = "rootJob-5";
    }
    if ($j==6) {
	$macro = $macro6;
	$script = "$jobNameBase-6.pl";
	$jobName = "rootJob-6";
    }
    if ($j==7) {
	$macro = $macro7;
	$script = "$jobNameBase-7.pl";
	$jobName = "rootJob-7";
    }
    if ($j==8) {
	$macro = $macro8;
	$script = "$jobNameBase-8.pl";
	$jobName = "rootJob-8";
    }
    if ($j==9) {
	$macro = $macro9;
	$script = "$jobNameBase-9.pl";
	$jobName = "rootJob-9";
    }
    if ($j==10) {
	$macro = $macro10;
	$script = "$jobNameBase-10.pl";
	$jobName = "rootJob-10";
    }
    if ($j==11) {
	$macro = $macro11;
	$script = "$jobNameBase-11.pl";
	$jobName = "rootJob-11";
    }
    if ($j==12) {
	$macro = $macro12;
	$script = "$jobNameBase-12.pl";
	$jobName = "rootJob-12";
    }
    if ($j==13) {
	$macro = $macro13;
	$script = "$jobNameBase-13.pl";
	$jobName = "rootJob-13";
    }
    if ($j==14) {
	$macro = $macro14;
	$script = "$jobNameBase-14.pl";
	$jobName = "rootJob-14";
    }
    if ($j==15) {
	$macro = $macro15;
	$script = "$jobNameBase-15.pl";
	$jobName = "rootJob-15";
    }
    if ($j==16) {
	$macro = $macro16;
	$script = "$jobNameBase-16.pl";
	$jobName = "rootJob-16";
    }
    if ($j==17) {
	$macro = $macro17;
	$script = "$jobNameBase-17.pl";
	$jobName = "rootJob-17";
    }
    if ($j==18) {
	$macro = $macro18;
	$script = "$jobNameBase-18.pl";
	$jobName = "rootJob-18";
    }
    if ($j==19) {
	$macro = $macro19;
	$script = "$jobNameBase-19.pl";
	$jobName = "rootJob-19";
    }
    if ($j==20) {
	$macro = $macro20;
	$script = "$jobNameBase-20.pl";
	$jobName = "rootJob-20";
    }
    if ($j==21) {
	$macro = $macro21;
	$script = "$jobNameBase-21.pl";
	$jobName = "rootJob-21";
    }

    # Create script file to run
    open(SCRIN, ">$scrDir/$script") or die;
    print SCRIN "\#\!/usr/bin/perl -w\n";
    print SCRIN "chdir \"$workDir\"\;\n";
    print SCRIN "my \$cmd = \"bbrroot -b -q ./$macro\"\;\n";
    print SCRIN "print STDERR \"'\$cmd' ...\\n\"\;\n";
    print SCRIN "system(\$cmd)\;\n";
    print SCRIN "exit\;\n";
    close SCRIN;

    # Make submit command
    my $subCmd ="qsub -q $queue -l $walt -V -N $jobName -e $logDir -o $logDir $scrDir/$script";
    
    print STDERR "'$subCmd' ...\n";
    system($subCmd);

    $j++;
}

exit;
