#!/usr/bin/env perl

# This script is used to convert the TOSCA generated field map file
# dsolenoid.table into C-code that can be compiled and statically
# linked in to the reconstruction programs. It is mainly just a copy
# of the script Richard put at the end of the dsolenoid.table file
# itself only the print command was changed.
#
# To use it, do the following steps:
#
# 1.) Cut the field map out from the end of
#     src/libraries/TRACKING/DMagneticFieldMap.cc 
#
# 2.) Run this script to append the new map to the C++ source
#    convert_fieldmap.pl dsolenoid.table > DMagneticFieldMap.cc
#
# NOTE: You will also need to make sure the values default_rDim
#       default_phiDim, and default_zDim at the top of
#       DMagneticFieldMap.cc reflect the number of grid points
#       in each dimension.


print "// ======================= Below here is auto-generated code ===================\n";
print "\n\n";
print "DMagneticFieldMapGlueX::DBfieldPoint_t default_mag_field[] = {\n";


open(FD,$ARGV[0]);
while (<FD>)
{
   if (/[^-.Ee0-9]$/)
   {
      @f = split(" ");
      next if (@f != 5);
      $f[3]=0 if (abs($f[3])<1e-10);
      $f[4]=0 if (abs($f[4])<1e-10);
      $f[5]=0 if (abs($f[5])<1e-10);
      print "{$f[0], $f[1], $f[2], $f[3], $f[4], $f[5]},\n";
		
		# Use the following for biggrid1500.table
      #print "{$f[0], $f[1], $f[2], $f[3], $f[5], $f[4]},\n";
   }
}


print "};\n";
