#!/usr/bin/perl
use strict;
# This script was written by A. Saavedra 2005
# and modified by S. Viret for KStarMuMu standalone generation
#
# This scripts creates:
# --> a shell script that will submit all the jobs              --> launch_GAUSS.x
# --> an individual shell script that will be executed in batch --> Gauss_Test_(...).csh
# --> the corresponding options file                            --> Gauss_Test_(...).opts

# the job number and max number of jobs                                                                                       
my $n_run = 0;
my $n_maxrun = 10;

# the time for the batch 
my $batch_queue = "1nd";

# the number of events per job
my $incre = 2500;

# information for the all the filenames
my $version = "v30r3";
my $suffix = "newCondDB";

# the workdir where the script is executed (ADAPT IT TO YOUR CONDITIONS)
my $path = "~/public/Production/";

# the directory where the results are stored (ADAPT IT TO YOUR CONDITIONS)
my $pathout = "\$CASTOR_HOME/MC/simul/v30r3/";

#
# 1. Create the script that launch all the batch jobs
#

my $qsub_filename = "launch_GAUSS.x";
open(qsub_file,">$qsub_filename") or die "Can't open file $qsub_filename";
while ($n_run <= $n_maxrun) 
{
  my $sh_filename = $path ."scripts/Gauss_" . $suffix . "_" . $incre . "_" . $n_run . ".csh";
  my $string = "prod.Gauss_" . $n_run;
  print qsub_file "chmod 755 $sh_filename \n"; 
  print qsub_file "bsub -q $batch_queue -J $string $sh_filename \n"; 
  CreateOptionsFile($suffix,$incre,$n_run);
  CreateShellScript($suffix,$incre,$version,$path,$pathout,$n_run);
  $n_run  = $n_run + 1;
}
close(qsub_file);


#
# 2. Create the script that will be used to run gauss in batch 
#

sub CreateShellScript{

# suffix to the filename
 my $suffix = $_[0];
# the event number per run
 my $max_eventN = $_[1];
# the Gauss Version
 my $gauss_version = $_[2];
# the path where the specific options file will be retrieved
 my $path = $_[3];
# the path where the simulated data are stored 
 my $pathout = $_[4];
# the run number
 my $run_number = $_[5];

# Here we define the path and the name of the script file (NOTE that you should have a directory 
# named /scripts/ in your WORKDIR) 
 my $sh_filename = "scripts/Gauss_" . $suffix . "_" . $max_eventN . "_" . $run_number . ".csh";

# Same remark than before: options files are store in an /opts/ directory 
 my $options_filename = "Gauss_" . $suffix . "_" . $max_eventN . "_" . $run_number . ".opts";
 my $options_completefilename = $path . "opts/" . $options_filename;

 my $output_filename = "Gauss_" . $suffix . "_" . $max_eventN . "_" . $run_number . ".sim";
 my $output_completefilename = $pathout . "sim/" . $output_filename;
 
 my $log_filename = "Gauss_" . $suffix . "_" . $max_eventN . "_" . $run_number . ".log";
 my $log_completefilename = $pathout . "log/" . $log_filename;

 open(sh_file,">$sh_filename") or die "Can't open file $sh_filename";

# set the lhcb environment
 print sh_file "#!/bin/csh\n";
# where the job is run
 print sh_file "set MODE = \$CMTCONFIG\n";
 print sh_file "echo \"----> Executing Gauss $gauss_version (mode \$MODE)\"\n";
 print sh_file "set WORKDIR = \$PWD\n";
 print sh_file "setenv MYJOBROOT ~/cmtuser/Gauss_$gauss_version/Sim/Gauss/$gauss_version\n";
 print sh_file "echo \"----> MYJOBROOT is set to:\"\n";
 print sh_file "echo \$MYJOBROOT\n";
 print sh_file "echo \"----> CMTPATH is set to:\"\n";
 print sh_file "echo \$CMTPATH\n";
 print sh_file "source \$MYJOBROOT/cmt/setup.csh -tag=\$MODE\n";
 print sh_file "# Go to working directory\n";
 print sh_file "cd \$WORKDIR\n";
 print sh_file "echo \"----> Working in \$PWD\"\n";
 print sh_file "# == in batch copy the executable and options files to \$WORKDIR\n";
 print sh_file "cp \$MYJOBROOT/\$MODE/Gauss.exe .\n";
 print sh_file "cp \$MYJOBROOT/options/*.opts .\n";
 print sh_file "cp $options_completefilename .\n";
 print sh_file "setenv GAUSSOPTS .\n";
 print sh_file "# == run the executable ================================\n";
 print sh_file "time .\/Gauss.exe \$GAUSSOPTS/$options_filename > Gauss.log\n";
 print sh_file "# == job is finished: remove temporary files ==========\n";
 print sh_file "rm -f Gauss.exe\n";
 print sh_file "# if in batch copy Gauss.sim file to permanent storage (example for CERN)\n";
 print sh_file "rfcp Gauss.sim $output_completefilename\n";
 print sh_file "rfcp Gauss.log $log_completefilename\n";
 print sh_file "exit 0\n";
 close(sh_file);
}


#
# 3. Create the corresponding options file
#    This option file replace the classic v200601.opts file.
#

sub CreateOptionsFile{
# suffix to the filename
 my $suffix = $_[0];
# maximum number of events
 my $max_eventN = $_[1];
# run number
 my $run_number = $_[2];

# lets create the filename and the open the file
 my $filename = "opts/Gauss_" . $suffix . "_" . $max_eventN . "_" . $run_number . ".opts";

 open(option_file,">$filename") or die "Can't open file $filename";

 print option_file "#pragma print off\n";
 print option_file "//--------------------------------------------------------------------------\n";
 print option_file "// Execute a standard production Gauss job\n";
 print option_file "//--------------------------------------------------------------------------\n";
 print option_file "#include \"\$GAUSSOPTS/Gauss.opts\"\n";
 print option_file "\n";
 print option_file "//--------------------------------------------------------------------------\n";
 print option_file "// Geometry database dependent options\n";
 print option_file "//--------------------------------------------------------------------------\n";
 print option_file "#include \"\$DDDBROOT/options/DC06.opts\"\n";
 print option_file "\n";
 print option_file "//---------------------------------------------------------------------------\n";
 print option_file "// Number of events to process + seeding\n";
 print option_file "//---------------------------------------------------------------------------\n";
 print option_file "\n";
 print option_file "GaussGen.RunNumber        = $run_number;\n";
 print option_file "ApplicationMgr.EvtMax     = $max_eventN;\n";
 print option_file "\n";
 print option_file "#include \"\$GAUSSOPTS/VeloAlignment_$run_number.opts\"\n";
}

