#!/usr/bin/perl
#
# Copyright Zaher Salman and PSI LEM Group 2009.
# zaher.salman@psi.ch
######## Relevant subdirectories and urls ########
# May need modification on different servers
$WEBDIR = "/~l_salman";
$REALDIR = "/home/l_salman/public_html";
$BINDIR = "/usr/local/bin";
$MAINPAGE = "/cgi-bin/musrfit.cgi";
$OUTPUT = $WEBDIR . "/msrfit_out";
$OUTPUT_REAL = $REALDIR . "/msrfit_out";
$SUMM_DIR = "/mnt/data/nemu/summ";
# Programs used
if ( $TITLE eq $EMPTY || $TITLE eq "TITLE" ) {
$MUSRFIT = $BINDIR . "/musrfit -t";
}
else {
$MUSRFIT = $BINDIR . "/musrfit";
}
$MUSRVIEW = $BINDIR . "/musrview --png";
$DUMPASCII = "$MUSRFIT --dump ascii";
$MSR2DAT = $BINDIR . "/msr2dat";
#######################################################################
# Nothing need to be changed from here on
$EMPTY=q{};
$SPACE=q{ };
$COMMA=q{,};
# Additions to paremeters' names
$erradd = "d";
$minadd = "_min";
$maxadd = "_max";
%BeamLines = ( "LEM", "MUE4", "GPS", "PIM3", "LTF", "PIM3", "Dolly", "PIE1" , "GPD", "PIE1");
# Commands used often
$cln_out = "rm -f $OUTPUT_REAL/FILENAME*";
$cln = "rm -f MINUIT2.* $OUTPUT_REAL/FILENAME* FILENAME.*";
$plotonly =
"$MUSRVIEW FILENAME.msr >> $OUTPUT_REAL/FILENAME.out; convert FILENAME\_0.png -crop 590x460+1+50 $OUTPUT_REAL/FILENAME.png; rm -f FILENAME\_0.png";
$fitplot =
"$MUSRFIT FILENAME.msr > $OUTPUT_REAL/FILENAME.out; $MUSRVIEW FILENAME.msr >> $OUTPUT_REAL/FILENAME.out; convert FILENAME\_0.png -crop 590x460+1+50 $OUTPUT_REAL/FILENAME.png; rm -f FILENAME\_0.png";
$dumpascii =
"$DUMPASCII FILENAME.msr > $OUTPUT_REAL/FILENAME_ascii.out; /bin/tar -cvzf $OUTPUT_REAL/FILENAME.tgz FILENAME*.dat >> $OUTPUT_REAL/FILENAME_ascii.out; rm -f FILENAME*.dat";
# TODO: Replace this with a call to $TABLE=MSR::ExportParams
$AllParameters{"Header"}=1;
$TABLE=MSR::ExportParams(\%AllParameters);
$msr2dat = "$MSR2DAT FILENAME.msr > $OUTPUT_REAL/FILENAME_par.dat";
# "Smart" default value of the fit parameters.
%Defaults = (
"Asy", "0.15", "dAsy", "0.01",
"Asy_min", "0", "Asy_max", "0",
"Alpha", "1.0", "dAlpha", "0.01",
"Alpha_min", "0", "Alpha_max", "0",
"N0", "300.0", "dN0", "0.01",
"N0_min", "0", "N0_max", "0",
"NBg", "30.0", "dNBg", "0.01",
"NBg_min", "0", "NBg_max", "0",
"Lam", "1.0", "dLam", "0.01",
"Lam_min", "0", "Lam_max", "0",
"Gam", "1.0", "dGam", "0.01",
"Gam_min", "0", "Gam_max", "0",
"Bet", "0.5", "dBet", "0.01",
"Bet_min", "0", "Bet_max", "0",
"Two", "2.0", "dTwo", "0.0",
"Two_min", "0", "Two_max", "0",
"Del", "0.1", "dDel", "0.01",
"Del_min", "0", "Del_max", "0",
"Sgm", "0.1", "dSgm", "0.01",
"Sgm_min", "0", "Sgm_max", "0",
"Aa", "0.1", "dAa", "0.01",
"Aa_min", "0", "Aa_max", "0",
"q", "0.1", "dq", "0.01",
"q_min", "0", "q_max", "0",
"Bg", "0.036", "dBg", "0.01",
"Bg_min", "0", "Bg_max", "0",
"bgrlx", "0.", "dbgrlx", "0.0",
"bgrlx_min", "0", "bgrlx_max", "0",
"Frq", "1.0", "dFrq", "1.",
"Frq_min", "0", "Frq_max", "0",
"Field", "100.0", "dField", "1.",
"Field_min", "0", "Field_max", "0",
"Energy", "14.1", "dEnergy", "0.",
"Energy_min", "0", "Energy_max", "0",
"DeadLayer", "10.", "dDeadLayer", "0.1",
"DeadLayer_min", "0", "DeadLayer_max", "0",
"Lambda", "128.1", "dLambda", "0.1",
"Lambda_min", "0", "Lambda_max", "0",
"Phi", "1.", "dPhi", "0.01",
"Phi_min", "0", "Phi_max", "0"
);
#######################################################################
######## Main body of code ########
use CGI;
use MSR;
my $in = new CGI;
#######################################################################
# Common to all steps
# If you are coming from a previous round pass in various parameters
PassIn();
# This is the content of the html page..
$Content = $EMPTY;
# This is just for temporary debugging html text
$TC = $EMPTY;
# Put all input values in a hash
@All = $in->param();
$N = $#All;
$i = 0;
while ( $i <= $N ) {
$One = $All[$i];
$AllParameters{ $One } = $in->param($One);
# $TC=$TC."$One=".$AllParameters{ $One }." ";
++$i;
}
# See which step or command we are coming from
# Step Value Command
#
# -1 CLEAR/START NEW
# 1 Defin Shared
# 2 Initialize parameters
# 3 PLOT
# 4 MIGRAD
# 5 MINOS
# 6 SIMPLEX
#######################################################################
$Step = $in->param("go");
$FITMINTTYPE = "PLOT";
if ( $Step eq "PLOT" ) {
$Step = 3;
$FITMINTYPE = $EMPTY;
}
elsif ( $Step eq "MIGRAD" || $Step eq "FIT") {
$Step = 4;
$FITMINTYPE = "MINIMIZE\nMIGRAD\nHESSE";
}
elsif ( $Step eq "MINOS" ) {
$Step = 5;
$FITMINTYPE = "MIGRAD\nMINOS";
}
elsif ( $Step eq "SIMPLEX" ) {
$Step = 6;
$FITMINTYPE = "SCAN\nSIMPLEX";
}
elsif ( $Step eq "Restart" ) {
$Step = -1;
}
if ( $All{"Minimization"} ne "" && $All{"ErrorCalc"} ne "" && $Step ne "PLOT" ) {
$FITMINTYPE = $All{"Minimization"}."\n".$All{"ErrorCalc"};
}
#$TC=$TC."OUT: ".MSR::Test(\%AllParameters);
#$tmp="Frq";
#$TC=$TC."Sh_Frq= ".$AllParameters{"Sh_$tmp"}." ";
# Skip the shared parameters step if you have only one
if ( $Step == 1 && $#RUNS == 0 ) { $Step = 2; }
#######################################################################
# Choose shared parameters if you have multiple runs
#######################################################################
if ( $Step == 1 ) {
# First create the THEORY block to get a parameters list
($Full_T_Block,$Paramcomp_ref)=MSR::CreateTheory(@FitTypes);
@Paramcomp = @$Paramcomp_ref;
StepShared();
}
#######################################################################
#######################################################################
# Initialize/Fix parameters values
#######################################################################
elsif ( $Step == 2 ) {
$RUN = $RUNS[0];
if ( $RUN < 10 ) { $RUN = "000" . $RUN; }
elsif ( $RUN < 100 ) { $RUN = "00" . $RUN; }
elsif ( $RUN < 1000 ) { $RUN = "0" . $RUN; }
# First create the THEORY block to get a parameters list
($Full_T_Block,$Paramcomp_ref)=MSR::CreateTheory(@FitTypes);
@Paramcomp = @$Paramcomp_ref;
if ( $BeamLine eq "LEM" ) {
# Take the temperature from data file if you can
ExtractInfo( $RUN, $YEAR, "Temp" );
$TEMPERATURE = $RTRN_Val;
}
# If TITLE is empty or eq TITLE get it from the first run if you can
if ( $TITLE eq $EMPTY || $TITLE eq "TITLE" ) {
# This is a flag to keep setting TITLE to default
$T_Keep = 1;
}
else {
$T_Keep = 0;
}
# If the filename is empty or eq RUN put default
if ( $FILENAME eq $EMPTY || $FILENAME eq "RUN" || $FILENAME eq "0" ) {
# Flag to keep default filename
$FN_Keep = 1;
if ( $RUNS[0] ne $EMPTY ) {
( $TMP1, $TMP2, $BLine ) = split( /\//, $BeamLine );
$FILENAME = $RUNS[0] . "_" . $BeamLine . "_" . $YEAR;
}
}
else {
$FN_Keep = 0;
}
# If the file already exists do not continue running. The file may be
# used by someone else.
# TODO: Maybe create a different name and not die.
# if (-e "$FILENAME.msr") {
# $Content = $Content."The file name \"$FILENAME\" is being used, choose another name or delete it!";
# &SpitHTML;
# die;
# }
InitFixStep();
}
#######################################################################
#######################################################################
# Create musrfit .msr file and fit/plot the result
#######################################################################
elsif ( $Step <= 6 && $Step >= 3 ) {
# Then create appropriate file
if ($FitAsyType eq "SingleHist") {
($Full_T_Block,$Paramcomp_ref)= MSR::CreateMSRSingleHist(\%AllParameters);
} else {
($Full_T_Block,$Paramcomp_ref)= MSR::CreateMSR(\%AllParameters);
}
@Paramcomp = @$Paramcomp_ref;
if ( $Step == 3 ) {
$cmd = $plotonly;
}
elsif ( $Step <=6 && $Step >= 4 ) {
$cmd = $fitplot;
}
# Fit and plot results
$cmd =~ s/FILENAME/$FILENAME/g;
$tmp = system("$cmd");
# Generate ascii data and fit
$cmd = $dumpascii;
$cmd =~ s/FILENAME/$FILENAME/g;
$tmp = system("$cmd");
# Generate parameters table
$cmd = $msr2dat;
$cmd =~ s/FILENAME/$FILENAME/g;
$tmp = system("$cmd");
$Spectra = ( $#RUNS + 1 ) * ( $#TiVals + 1 );
if ( $RRF != 0 && $Spectra == 1 ) { $Spectra = 2; }
PrintOut();
}
else {
if ( $Step == -1 ) {
# Clean up
$cmd = $cln;
$cmd =~ s/FILENAME/$FILENAME/g;
$tmp = system("$cmd");
# Check flag for default
$T_Keep = $in->param("T_Keep");
if ( $T_Keep == 1 ) {
$TITLE = "TITLE";
}
# Check flag for default
$FN_Keep = $in->param("FN_Keep");
if ( $FN_Keep == 1 ) { $FILENAME = "RUN"; }
$Step = 0;
$Content = $EMPTY;
}
# Take title, runnumber, ti, tf, bin ...
Step0();
}
# Insert passed on variables into the HTML code
PassOn();
SpitHTML();
exit(0);
######## Split functions and subroutines ########
########################
#sub SpitHTML
#
#This function prints the start of the feedback form web page.
########################
sub SpitHTML {
print("Content-type: text/html; charset=iso-8859-1 \n\n");
print(
"
$TITLE
$TC
$Content
If you have any problems please send me an email about it
"
);
return;
}
########################
########################
# Step 0
#
# Is the begining of the fitting pocess
# Take title and run numbers and the type of fitting function
########################
sub Step0 {
# Prepare body of the HTML code
$Content = $Content . "
Enter the label (default is run title)
";
# Get current year
( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime( time() );
$year = $year + 1900;
$Years = $EMPTY;
# Creat a drop down menu for years from 1994 until current
for ( $i = $year ; $i >= 1994 ; $i-- ) {
# for ($i=1994..$year) {
$Years =
$Years . " ";
}
for my $BeamLine ( keys %BeamLines ) {
$BeamLineDDM = $BeamLineDDM
. "\n";
}
# Insert the years into the HTML code
$Content =~ s/INSERT_YEARS_HERE/$Years/g;
# Insert the beamlines into the HTML code
$Content =~ s/INSERT_BEAMLINES_HERE/$BeamLineDDM/g;
return;
}
########################
########################
# StepShared
#
# Choose shared parameters if applicable
#
########################
sub StepShared {
# Here we need first a list of parameters which depends
# on the functions chosen and number of components
$Content = $Content . "
Choose the shared parameters
";
# Prepare table of parameters
$component = 0;
$Param_Table = $EMPTY;
foreach my $FitType (@FitTypes) {
++$component;
$Parameters = $Paramcomp[ $component - 1 ];
$Param_Table = $Param_Table . "
Component $component: $FitType
Parameter
Share
";
@Params = split( /\s+/, $Parameters );
# For the first component we need Alpha for Asymmetry fits
if ( $component == 1 && $FitAsyType eq "Asymmetry" ) {
unshift( @Params, "Alpha" );
}
elsif ( $component == 1 && $FitAsyType eq "SingleHist" ) {
unshift( @Params, ( "N0", "NBg" ) );
}
foreach my $Param (@Params) {
if ( ($#FitTypes != 0)
&& ( $Param ne "Alpha" && $Param ne "N0" && $Param ne "NBg" ) )
{
$Param = join( $EMPTY, $Param, "_", "$component");
}
$ischecked = $in->param("Sh_$Param");
if ($ischecked) {
$checked = "checked";
}
else {
$checked = $EMPTY;
}
$Param_Table = $Param_Table . "
$Param
";
}
$Param_Table = $Param_Table . "
";
}
# Insert the table of parameters into the HTML code
$Content =~ s/INSERT_PARAMETERS_TABLE_HERE/$Param_Table/g;
return;
}
########################
########################
# InitFixStep
########################
sub InitFixStep {
$Content = $Content . "
Initialize parameters:
";
# Counter for runs
$Param_Table = $EMPTY;
$i = 0;
foreach my $RUN (@RUNS) {
++$i;
$Param_Table = $Param_Table . "Run Number: $RUN ";
$component = 0;
foreach my $FitType (@FitTypes) {
++$component;
$Parameters = $Paramcomp[ $component - 1 ];
@Params = split( /\s+/, $Parameters );
# For the first component we need Alpha for Asymmetry fits
if ( $component == 1 && $FitAsyType eq "Asymmetry" ) {
unshift( @Params, "Alpha" );
}
elsif ( $component == 1 && $FitAsyType eq "SingleHist" ) {
unshift( @Params, ( "N0", "NBg" ) );
}
$Param_Table = $Param_Table . "
Component $component: $FitType
";
}
}
# Insert the table of parameters into the HTML code
$Content =~ s/INSERT_PARAMETERS_TABLE_HERE/$Param_Table/g;
return;
}
########################
########################
# PrintOut
#
# Make nice plot and preset values of fit parameters nicely
########################
sub PrintOut {
$Content = $Content . "
Download $FILENAME.msr.
Download ascii files $FILENAME.tgz.
Download parameters table file $FILENAME\_par.dat.
";
# Initialize the extra pass on parameters
system("cp -f $FILENAME.* $OUTPUT_REAL/.; chmod -R a+r $OUTPUT_REAL");
# Reset the values of the TITLE and FILENAME if default was chosen
if ( $T_Keep == 1 ) {
$TITLE = "TITLE";
}
if ( $FN_Keep == 1 ) {
$FILENAME = "RUN";
}
# Check log x and/or log y by default depending on previous setting
if ( $logx eq "y" ) {
$logxcheck = "checked";
}
else {
$logxcheck = $EMPTY;
}
if ( $logy eq "y" ) {
$logycheck = "checked";
}
else {
$logycheck = $EMPTY;
}
# Check lifetime correction by default depending on previous settings
if ( $ltc eq "y" ) {
$ltccheck = "checked";
}
else {
$ltccheck = $EMPTY;
}
# Reset log x and log y flags
$logx = "n";
$logy = "n";
$ltc = "n";
$Param_Table = $EMPTY;
open( IFILE,q{<}, "$FILENAME.msr" );
@lines = ;
close(IFILE);
# Get Chi line and reformat
@Chi_Line = grep { /chisq/ } @lines;
if ( $TITLE eq $EMPTY || $TITLE eq "TITLE" ) { $TITLE = $lines[0]; }
if ( $Chi_Line[0] eq $EMPTY ) { $Chi_Line[0] = "Fit did not converge yet"; }
# Remove comment lines
@lines = grep {!/^\#/} @lines;
# Remove empty lines
@lines = grep {/\S/} @lines;
# Identify different blocks
$i=0;
foreach $line (@lines)
{
if (grep {/FITPARAMETER/} $line) {$NFITPARAMETERS=$i;}
if (grep {/THEORY/} $line) { $NTHEORY=$i;}
if ((grep {/RUN/} $line) & $NRUN==0) { $NRUN=$i;}
$i++;
}
@FPBlock=@lines[$NFITPARAMETERS+1..$NTHEORY-1];
@TBlock=@lines[$NTHEORY+1..$NRUN-1];
foreach my $line (@FPBlock) {
# TODO: Better treatement for FPBlock is to remove lines starting with # or empty.
$line =~ s/\n//g;
(
$tmp, $order, $Param, $value, $error, $errorp, $minvalue,
$maxvalue
) = split(/\s+/,$line);
# $TC=$TC.$line." ";
# $TC=$TC."$tmp,$order,$Param,$value,$error,$errorp,$minvalue,$maxvalue ";
# Take the number/order of the spectrum
@itmp = split( /\_/, $Param );
# If it is a multiple fit take also the run number
if ( $itmp[1] ne $EMPTY ) {
if ( $RUNS[ $itmp[1] - 1 ] ne $RUN ) {
$RUN = $RUNS[ $itmp[1] - 1 ];
$PRUN = $RUN;
}
else {
$PRUN = $SPACE;
}
}
# Keep the values from the fit for the next round..
$errParam = $erradd . $Param;
$minParam = $Param . $minadd;
$maxParam = $Param . $maxadd;
if ( $value eq $EMPTY ) {
# Take original defaults
( $Param_ORG, $tmp ) = split( '_', $Param );
$value = $Defaults{$Param_ORG};
$error = $Defaults{ join( $EMPTY, $erradd, $Param_ORG ) };
$minvalue = $Defaults{ join( $EMPTY, $Param_ORG, $minadd ) };
$maxvalue = $Defaults{ join( $EMPTY, $Param_ORG, $maxadd ) };
}
else {
# This is the trick to make an expression from the names :)
$$Param = $value;
$$errParam = $error;
$$minParam = $minvalue;
$$maxParam = $maxvalue;
}
# $TC=$TC."$order,$Param,$value,$error,$errorp,$minvalue,$maxvalue ";
$Param_Table = $Param_Table . "