*$ CREATE MAGFLD.FOR
*COPY MAGFLD
*
*===magfld=============================================================*
*
      SUBROUTINE MAGFLD ( X, Y, Z, BTX, BTY, BTZ, B, NREG, IDISC )

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1988-2005      by Alberto Fasso` & Alfredo Ferrari *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     Created  in 1988    by     Alberto Fasso`, CERN - TIS            *
*                                                                      *
*     Last change on 11-dec-92     by    Alfredo Ferrari               *
*                                                                      *
*     Input variables:                                                 *
*            x,y,z = current position                                  *
*            nreg  = current region                                    *
*     Output variables:                                                *
*            btx,bty,btz = cosines of the magn. field vector           *
*            B = magnetic field intensity (Tesla)                      *
*            idisc = set to 1 if the particle has to be discarded      *
*                                                                      *
*----------------------------------------------------------------------*
*
      IDISC = 0
 
    
         BTY = 0.D+00  
* solenoid field is at 14 degrees to target
         vcos14=0.970
         vsin14=0.242
* find B as a function of x and z positions (x and z are perpendicular and aligned with the target respectively)
         zcl=Z*vcos14-X*vsin14
         if (zcl.lt.50) then
         B=2.5+1*((zcl-50)/100+2.5)
         endif
         if (zcl.ge.50) then
         B=5-2.5*(zcl/100)
         endif
          
* define direction cosines
         BTZ=vcos14
         BTX=vsin14
      
 
      RETURN
*=== End of subroutine magfld =========================================*
      END

