#!/usr/bin/perl

$trklname = shift;

# set default parameters
$minpt = 100 / 1000.;
$bfield = 0.5;
$nbins = 19.4;
$tiltedcorr = 1;

$tpfs  =  4;
$tpfe  = 24;

$tpqs0 =  1;
$tpqe0 =  8;
$tpqs1 =  8;
$tpqe1 = 25;

$tpht  = 200;
$tpfp  =  40;
$tpcl  =   2;
$tpct  =  10;

$pidname = "uniform";

$trklengthcorr = 1;

# now parse config name
print "trklname: $trklname \n";

if ($trklname =~ /\-b([0-9])([np]?)/) {
    $bfield = $1/10.;
    $bfield *= -1. if ($2 eq 'n');
    $bfield = sprintf("%.2f", $bfield);
}

if ($trklname =~ /\-fs([0-9]+)e([0-9]+)/) {
    $tpfs = $1;
    $tpfe = $2;
}

if ($trklname =~ /\-ht([0-9]+)/) {
    $tpht = $1;
}

if ($trklname =~ /\-fp([0-9]+)/) {
    $tpfp = $1;
}

if ($trklname =~ /\-cl([0-9]+)/) {
    $tpcl = $1;
}

if ($trklname =~ /\-ct([0-9]+)/) {
    $tpct = $1;
}

if ($trklname =~ /\-qs([0-9]+)e([0-9]+)s([0-9]+)e([0-9]+)/) {
    $tpqs0 = $1;
    $tpqe0 = $2;
    $tpqs1 = $3;
    $tpqe1 = $4;
}

if ($trklname =~ /\-pid([a-z,0-9]+)/) {
    $pidname = $1;
}

if ($trklname =~ /\-pt([0-9]+)/) {
    $minpt = $1 / 1000.;
}

if ($trklname =~ /\-notrklen/) {
    $trklengthcorr = 0;
}

if ($trklname =~ /\-notilt/) {
    $tiltedcorr = 0;
}

if ($trklname =~ /\-nb([0-9]+)/) {
    $nbins = $1 / 10.;
}



# calculate derived quantities

if ($bfield == 0.5) {
    $omegatau = -0.16133;
}
elsif ($bfield == -0.5) {
    $omegatau =  0.16133;
}
elsif ($bfield == 0.2) {
    $omegatau =  -0.06453;
}
elsif ($bfield == -0.2) {
    $omegatau =  0.06453;
}
else {
    $omegatau =  sprintf("%.1f", 0);
}

print "src/trk/".$trklname.".auto.tcs\n";

open outfile, ">src/trk/$trklname.auto.tcs";


print outfile <<EOF;

// ***************************************************************************
// ***************************************************************************
// 
//                  THIS FILE HAS BEEN CREATED AUTOMATICALLY. 
//                  UPON RECREATION ALL CHANGES WILL BE LOST.
//
//                          DO NOT EDIT THIS FILE !!!!!
//
//
// ***************************************************************************
// ***************************************************************************
//
// Tracklet parameters
//
// ***************************************************************************

tracklet minpt $minpt;             // GeV
tracklet bfield $bfield;           // Tesla
tracklet omegatau $omegatau;       // dimensionless
tracklet driftbins $nbins;         // timebins
tracklet tiltedcorr $tiltedcorr;   // activate the tilted pad correction

write TPQS0,  $tpqs0;
write TPQE0,  $tpqe0;
write TPQS1,  $tpqs1;
write TPQE1, $tpqe1;

tracklet tracklengthcorr $trklengthcorr;    // deactivate track length corr

include src/pid_luts/$pidname-$tpqs0-$tpqe0-$tpqs1-$tpqe1.tcs

tracklet apply;

write TPFS,  $tpfs;
write TPFE,  $tpfe;

write TPHT, $tpht;

write TPCL, $tpcl;
write TPCT, $tpct;

write TPVBY, 0;
write TPVT, 0;
write TPFP, $tpfp;

include src/trk/poslut.tcs

// fit register latch delay must be set to zero
// for the software fix for the wrong latching in case of TPQE1 > TPFE + 1
// to avoid unnecessary delays for the sw-triggered re-latching
write TPD, 0;

EOF

close outfile;
