*$ CREATE MGDRAW.FOR
*COPY MGDRAW
*                                                                      *
*=== mgdraw ===========================================================*
*                                                                      *
      SUBROUTINE MGDRAW ( ICODE, MREG )

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1990-2013      by        Alfredo Ferrari           *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     MaGnetic field trajectory DRAWing: actually this entry manages   *
*                                        all trajectory dumping for    *
*                                        drawing                       *
*                                                                      *
*     Created on   01 March 1990   by        Alfredo Ferrari           *
*                                              INFN - Milan            *
*     Last change   12-Nov-13      by        Alfredo Ferrari           *
*                                              INFN - Milan            *
*                                                                      *
*----------------------------------------------------------------------*
*
      INCLUDE '(CASLIM)'
      INCLUDE '(COMPUT)'
      INCLUDE '(SOURCM)'
      INCLUDE '(FHEAVY)'
      INCLUDE '(FLKSTK)'
      INCLUDE '(GENSTK)'
      INCLUDE '(MGDDCM)'
      INCLUDE '(PAPROP)'
      INCLUDE '(QUEMGD)'
      INCLUDE '(SUMCOU)'
      INCLUDE '(TRACKR)'
*
      DIMENSION DTQUEN ( MXTRCK, MAXQMG )
*
      CHARACTER*20 FILNAM
*     "I have added at the beginning a statement CHARACTER*8 MRGNAM, NRGNAM, in order to be able to use region names instead of numbers (see below)" (FASSO, Alberto)
      CHARACTER*8 MRGNAM, NRGNAM
      LOGICAL LFCOPE
      SAVE LFCOPE
      DATA LFCOPE / .FALSE. /
*
*======================================================================*
*                                                                      *
*     Boundary-(X)crossing DRAWing:                                    *
*                                                                      *
*     Icode = 1x: call from Kaskad                                     *
*             19: boundary crossing                                    *
*     Icode = 2x: call from Emfsco                                     *
*             29: boundary crossing                                    *
*     Icode = 3x: call from Kasneu                                     *
*             39: boundary crossing                                    *
*     Icode = 4x: call from Kashea                                     *
*             49: boundary crossing                                    *
*     Icode = 5x: call from Kasoph                                     *
*             59: boundary crossing                                    *
*                                                                      *
*======================================================================*
*                                                                      *                                                                    *
      ENTRY BXDRAW ( ICODE, MREG, NEWREG, XSCO, YSCO, ZSCO )
*     During the first call, a file as open using the unit 88 and the name "collfile"
      IF ( .NOT. LFCOPE ) THEN
         LFCOPE = .TRUE.
         OPEN ( UNIT = 88, FILE = "viajante", STATUS = 'UNKNOWN')
      END IF
*     "GEOR2N is a FLUKA routine which converts the region numbers MREG and NEWREG (obtained as calling arguments) to the corresponding region names" (FASSO, Alberto)
      CALL GEOR2N ( MREG, MRGNAM, IERR1 )
      CALL GEOR2N ( NEWREG, NRGNAM, IERR2 )
      IF(IERR1 .NE. 0 .OR. IERR2 .NE. 0) STOP "Error in name conversion"
      IF(MRGNAM .EQ. "Pipe_FE" .AND. nrgnam .EQ. "FE_Zero") THEN
*      EnCinetica=ETRACK - AM(JTRACK)
      WRITE(88,*) JTRACK, XSCO, YSCO, ZSCO, CXTRCK, CYTRCK, CZTRCK
     &      ETRACK - AM(JTRACK), WTRACK
      END IF
      RETURN
*
*======================================================================*                                                                      *
*                                                                      *
*      ENTRY EEDRAW ( ICODE )
*      RETURN
*
*======================================================================*
*                                                                      *
*      ENTRY ENDRAW ( ICODE, MREG, RULL, XSCO, YSCO, ZSCO )
*      RETURN
*
*======================================================================*
*
*      ENTRY SODRAW
*      RETURN
*                                                                      *
*======================================================================*
*
*      ENTRY USDRAW ( ICODE, MREG, XSCO, YSCO, ZSCO )
*      RETURN
*=== End of subrutine Mgdraw ==========================================*
      END

