# all processes must be defined here

$procnames{"pes-align"} = "pesal_bb";
$procnames{"pes-gain"} = "pesgain_gb";
$procnames{"emtiming"} = "emtimingntuple_bb";
$procnames{"hadtiming"} = "hadtimingntuple_gb";
$procnames{"cal-had"} = "hadlerstntuple_jb";
$procnames{"miniplug"} = "mpstntuple_gb";
$procnames{"cal-em"} = "cemcal_bb";
$procnames{"tof"} = "tofcalib_gb";
$procnames{"badchannels"} = "calibhist_gb";

$streams{"pes-align"} = "b";
$streams{"pes-gain"} = "g";
$streams{"emtiming"} = "b";
$streams{"hadtiming"} = "g";
$streams{"cal-had"} = "j";
$streams{"miniplug"} = "g";
$streams{"cal-em"} = "b";
$streams{"tof"} = "g";
$streams{"badchannels"} = "g";
$streams{"plugcal"} = "b";

# all validation streams must be defined here

$stripnames{"mb"} = "mbstrip_gb";
$stripnames{"zee"} = "zeestrip_bb";
$stripnames{"zee_frontier_test"} = "zeestrip_bb";
$stripnames{"zmm"} = "zmmstrip_bb";
$stripnames{"wmn"} = "wmnstrip_bb";
$stripnames{"wen"} = "wenstrip_bb";
$stripnames{"jpsi"} = "jpsimumu_jb";

$streams{"mb"} = "g";
$streams{"zee"} = "b";
$streams{"zee_frontier_test"} = "b";
$streams{"zmm"} = "b";
$streams{"wmn"} = "b";
$streams{"wen"} = "b";
$streams{"jpsi"} = "j";

# all validation nutples must be defined here

$ntupleextensions{"tof"} = ".root";
$ntupleextensions{"cal-em"} = ".text";
$ntupleextensions{"mb"} = ".text";
$ntupleextensions{"miniplug"} = ".stn";
$ntupleextensions{"emtiming"} = ".stn";
$ntupleextensions{"pes-align"} = ".root";
$ntupleextensions{"pes-gain"} = ".root";
$ntupleextensions{"dqm"} = ".root";
$ntupleextensions{"zee"} = ".stn";
$ntupleextensions{"zmm"} = ".stn";
$ntupleextensions{"wmn"} = ".stn";
$ntupleextensions{"wen"} = ".stn";
$ntupleextensions{"jpsi"} = ".stn";
$ntupleextensions{"plug-energy-scale"} = ".topnt";


$ntuplenames{"tof"} = "/zee/tofvalid_";
$ntuplenames{"cal-em"} = "/zee/cemcal_";
$ntuplenames{"mb"} = "/mb/";
$ntuplenames{"emtiming"} = "/zee/zv";
$ntuplenames{"miniplug"} = "/zee/zv";
$ntuplenames{"pes-align"} = "/zee/pesal";
$ntuplenames{"pes-gain"} = "/mb/pesgain";
$ntuplenames{"dqm"} = "/mb/dqmfile";
$ntuplenames{"zee"} = "/zee/zv";
$ntuplenames{"plug-energy-scale"} = "/zee/zv";
$ntuplenames{"plugcal"} = "/zee/zv";
$ntuplenames{"zmm"} = "/zmm/";
$ntuplenames{"wmn"} = "/wmn/";
$ntuplenames{"wen"} = "/wen/";
$ntuplenames{"jpsi"} = "/jpsi/";

$valid_dir = "/export/data2/cdfopr/concatenated/current/";
$valid_id = "cdfopr";
$valid_host = "fcdfdata125.fnal.gov";

$ntuple_dir = "/export/data2/cdfopr/validation/";
$ntuple_id = "cdfopr";
$ntuple_host = "fcdfdata125.fnal.gov";

#$ntuple_dir = "/cdf/proj/102.calib/";
#$ntuple_id = "cdfprd_cal";
#$ntuple_host = "fcdfprj1.fnal.gov";

#$calib_dir = "/export/data2/cdfopr/concatenated/current/";
#$calib_id  = "cdfopr";
#$calib_host = "fcdfdata125.fnal.gov";
$calib_dir = "/cdf/home/cdfprd_cal/data2/concatenated/current/";
$calib_id  = "cdfprd_cal";
$calib_host = "fcdfdata401.fnal.gov";

my @spacedirs = ("data1","data2");

#my @pesfiles = &getCalibFiles("pes-align","210914");

#my $pf;
#foreach $pf (@pesfiles)
#{
#    printf("File is $pf");
#}

sub getSpaceLeft()
{
	my $res = "";
	foreach $whichDisk (@spacedirs)
	{
# 	  printf("ssh ${calib_id}\@${calib_host} df | grep $whichDisk | tail -n 1 | awk '{print \$5}' \n");
	  my $thisres = `ssh ${calib_id}\@${calib_host} df | grep $whichDisk | tail -n 1 | awk '{print \$5}'`;
	  chop($thisres);
	  if ($res ne "")
	  {
	   $res = $res.", ".$thisres;
	  } else {
	   $res = $thisres;
	  }
	}
	return $res;
}

sub getValidFiles()
{
    my $strip;
    my $run;
    ($strip,$run) = @_;
    my $hrun = `printf "%06x" $run`;
    my $stripname = $stripnames{$strip};
    my $streamname = $streams{$strip};
    if ($stripname eq "")
    {
        printf("No such strip $stripname\n");
        my @empty;
        return @empty;
    }
    else
    {   
	printf ("Looking for files: ssh ${valid_id}\@${valid_host} \"ls ${valid_dir}/stream${streamname}/${stripname}${hrun}*.root\"\n");
        my @rawfiles = `ssh ${valid_id}\@${valid_host} \"ls ${valid_dir}/stream${streamname}/${stripname}${hrun}* 2> /dev/null\"`;
        my @rootdfiles;
        my $rf;
        my $i=0;
        foreach $rf (@rawfiles)
        {
            chop($rf);
            $rootdfiles[$i++] = "rootd://${valid_host}/$rf";
        }
        return @rootdfiles
        }
}

sub getValidNtuples()
{
    my $ntuple;
    my $rungroup;
    ($ntuple,$rungroup) = @_;

    my $ntuplename = $ntuplenames{$ntuple};
    my $ntupleextension = $ntupleextensions{$ntuple};
    if ($ntuplename eq "" || $ntupleextension eq "")
    {
        #printf("<!-- No such ntuple $ntuple name=>$ntuplename< extenson = >$ntupleextension< -->\n");
        my @empty;
        return @empty;
    }
    else
    {   
        my @rawfiles = `ssh ${ntuple_id}\@${ntuple_host} \"ls ${ntuple_dir}/${rungroup}/${ntuplename}*${ntupleextension} 2> /dev/null\"`;
        my @rootdfiles;
        my $rf;
        my $i=0;
        foreach $rf (@rawfiles)
        {
            chop($rf);
            $rootdfiles[$i++] = "rootd://${ntuple_host}/$rf";
        }
        return @rootdfiles
        }
}

sub whichRun()
{
    my ($fname) = @_;
    my ($hrun) = ($fname =~ /([0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])\./);

    my $run = hex $hrun;
    return $run;
}

sub howManySections()
{
    my ($fname) = @_;
    my ($ns) = ($fname =~ /[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]\.n([0-9]*)\./);
    return $ns;
}

sub whichProcess()
{
    my ($fname) = @_;
    my @keys = keys %procnames;

    
    foreach $k (@keys)
    {
#	printf("Comparing $fname with $procnames{$k} for process $k\n");
	if ($fname =~ /$procnames{$k}/)
	{
	    return $k;
	}
    }

    my @keyss = keys %stripnames;
    foreach $k (@keyss)
    {
#	printf("Comparing $fname with $procnames{$k} for process $k\n");
	if ($fname =~ /$stripnames{$k}/)
	{
	    return $k;
	}
    }


    return "";
}

sub getAllCalibValidFiles()
{
    printf("Look command: ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ 2> /dev/null \"\n");
    my @rawfiles = `ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ 2> /dev/null \"`;
    my %cfiles;
    
    foreach $rf (@rawfiles)
    {
	chop($rf);
	my $run = &whichRun($rf);
	my $proc = &whichProcess($rf);

#	printf("----------\n");
#	if ($proc =~ /pes\-align/) { printf("  File $rf has run $run proc $proc\n"); }
	if ($run ne "" && $proc ne "")
	{
	    my $tag = $proc.".".$run;

	    my @cnewa = ("rootd://${calib_host}/${calib_dir}/stream$streams{$proc}/$rf"); 
#	    printf("  Full name: @cnewa\n");
	    
	    my $cold = $cfiles{$tag};
	    if ($cold eq "")
	    {
		
		$cfiles{$tag} = [ @cnewa ];
		my $ctot = $cfiles{$tag};
		my @totfiles = @$ctot;
	#	printf(" Total files for $run run $proc $proc are @totfiles\n");
	    }
	    else
	    {
		my @colda = @$cold;
		$cfiles{$tag} = [ (@colda,@cnewa) ];
		#printf(" New files for run $run proc $proc are : @cnewa\nOld files are: @colda\n\n");
	    }
	}
	else
	{
#	    printf(" --> No run or process defined\n");
	}
    }
    my @keysc = keys %cfiles;
    ##printf("<!-- All valid keys are: @keysc -->\n");
    return %cfiles;
}

      
sub getAllSamglia()
{
#    printf("Look command: ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ 2> /dev/null \"\n");
    my @rawfiles = `rm samglia2.py;wget -q http://fncdfsrv0.fnal.gov/cgi-bin/samglia2.py; grep CalibExe samglia2.py | awk -Fsrv0_ '{print \$2}' | awk -F\\" '{print \$1}'`;

    my %cfiles;

	#printf("<!-- sam raws: @rawfiles -->\n");
 foreach $rf (@rawfiles)
    {
        chop($rf);
	my @fields = split("\_",$rf);
	#printf("<!-- sam raw: $rf fields are @fields -->\n");

        my $run = $fields[4];
        my $proc = $fields[2];
	my $jid = $fields[6];
#       printf("----------\n");
#       if ($proc =~ /zee/) { printf("  File $rf has run $run proc $proc\n"); }
        if ($run ne "" && $proc ne "")
        {
            my $tag = $proc.".".$run;
	    $cfiles{$tag} = $jid;
        }
        else
        {
#           printf(" --> No run or process defined\n");
        }
    }
    my @keyssc = keys %cfiles;
#printf("<!-- All valid sam keys are: @keyssc -->\n");
    return %cfiles;
}


sub getAllSamgliaJID()
{
#    printf("Look command: ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ 2> /dev/null \"\n");
    my @rawfiles = `rm samglia2.py;wget -q http://fncdfsrv0.fnal.gov/cgi-bin/samglia2.py; grep CalibExe samglia2.py`;

    my %cfiles;

    #printf("<!-- sam raws: @rawfiles -->\n");
    foreach $rf (@rawfiles)
    {
        chop($rf);

	($run)   = ($rf =~ /(\d\d\d\d\d\d)_\d\d\d\d\d\d/);
	($proc) = ($rf =~ /CalibExe\_1\_([a-z]*)\_$run/);
	($jid)   = ($rf =~ /\<a\shref=\"(http.*)\"/);
#       printf("----------\n");
	#printf("<!--  File $rf has run $run proc $proc jid $jid -->\n"); 
        if ($run ne "" && $proc ne "")
        {
            my $tag = $proc.".".$run;
	    $cfiles{$tag} = $jid;
        }
        else
        {
#           printf(" --> No run or process defined\n");
        }
    }
    my @keyssc = keys %cfiles;
#printf("<!-- All valid sam keys are: @keyssc -->\n");
    return %cfiles;
}


sub getNumCalibValidFiles()
{
#    printf("Look command: ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ | grep -v list | grep -v log 2> /dev/null \"");
    my @rawfiles = `ssh ${calib_id}\@${calib_host} \"ls -1 ${calib_dir}/stream?/ | grep -v list | grep -v log 2> /dev/null \"`;
    my %cfiles;
    
    foreach $rf (@rawfiles)
    {
	chop($rf);
	my $run = &whichRun($rf);
	my $proc = &whichProcess($rf);
	my $num = &howManySections($rf);

#	printf("----------\n");
#	printf("  File $rf has run $run proc $proc num $num\n");
	if ($run ne "" && $proc ne "")
	{
	    my $tag = $proc.".".$run;
	    
	    $cfiles{$tag} = $num;	
	}
	else
	{
#	    printf(" --> No run or process defined for $rf \n");
	}
    }
    #my @keysc = keys %cfiles;
    ##printf("<!-- All valid keys are: @keysc -->\n");
    return %cfiles;
}


sub getCalibFiles()
{
    my $process;
    my $run;
    ($process,$run) = @_;
    my $hrun = `printf "%06x" $run`;
    my $procname = $procnames{$process};
    if ($procname eq "")
    {
	printf("No such process $procname\n");
	my @empty;
	return @empty;
    }
    else
    {
#	printf ("Looking for files: ssh ${calib_id}\@${calib_host} \"ls ${calib_dir}/${procname}${hrun}*.root\"\n");
	my @rawfiles = `ssh ${calib_id}\@${calib_host} \"ls ${calib_dir}/${procname}${hrun}* 2> /dev/null \"`;
	my @rootdfiles;
	my $rf;
	my $i=0;
	foreach $rf (@rawfiles)
	{
	    chop($rf);
	    $rootdfiles[$i++] = "rootd://${calib_host}/$rf";
	}
	return @rootdfiles
	}
}


sub getAllMaxCalibFiles()
{
    my ($runlo,$runhi) = @_;
    my @streams = ("b","g","j");
    my $stream;
    my %maxfiles;
    foreach $stream (@streams)
    {
	#printf("<!-- Sam query: sam translate constraints --dim=\"cdf.dataset ${stream}physr and run_number>=$runlo and run_number<=$runhi\"\n -->");
	@allfiles = `sam translate constraints --dim="cdf.dataset ${stream}physr and run_number>=$runlo and run_number<=$runhi" | grep phys | awk '{print \$1}'`;

	my $rf;
	foreach $rf (@allfiles)
	{
	    chop ($rf);
	    my $run = &whichRun($rf);

#	    #printf("<!--  File $rf has run $run -->");
	    if ($run ne "")
	    {
		my $tag = $stream.".".$run;
		$maxfiles{$tag}++;

	    }

	}
    }
    return %maxfiles;
}

sub getMaxCalibFiles()
{
    my $process;
    my $run;
    ($process,$run) = @_;
    my $hrun = `printf "%06x" $run`;
    my $stream = $streams{$process}."physr";
    if ($stream eq "")
    {
	printf("No stream for $process\n");
	my @empty;
	return @empty;
    }
    else
    {
#	printf ("Looking for files: ssh ${calib_id}\@${calib_host} \"ls ${calib_dir}/${procname}${hrun}*.root\"\n");

	$maxf = 
	chop($maxf);
	return $maxf;
    }
}
