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

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1990-2006      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  05-may-06       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
      CHARACTER*8 MRGNAM, NRGNAM, MRGNAM1, NRGNAM1, MRGNAM2, NRGNAM2,
     & MRGNAM3, NRGNAM3, MRGNAM4, NRGNAM4, MRGNAM5, NRGNAM5    
      LOGICAL LFCOPE
      SAVE LFCOPE
      DATA LFCOPE / .FALSE. /
*
      RETURN
*
*======================================================================*
 

      ENTRY BXDRAW ( ICODE, MREG, NEWREG, XSCO, YSCO, ZSCO )
      IF ( .NOT. LFCOPE ) THEN
         LFCOPE = .TRUE.
         OPEN ( UNIT = 88, FILE = "collfile", STATUS = 'UNKNOWN')
         OPEN ( UNIT = 11, FILE = "collfile1", STATUS = 'UNKNOWN' )
         OPEN ( UNIT = 22, FILE = "collfile2", STATUS = 'UNKNOWN' )
         OPEN ( UNIT = 33, FILE = "collfile3", STATUS = 'UNKNOWN' )
         OPEN ( UNIT = 44, FILE = "collfile4", STATUS = 'UNKNOWN' )
         OPEN ( UNIT = 77, FILE = "collfile5", STATUS = 'UNKNOWN' )
      END IF
      CALL GEOR2N ( MREG, MRGNAM, IERR1 )
      CALL GEOR2N ( NEWREG, NRGNAM, IERR2 )
      CALL GEOR2N ( MREG, MRGNAM1, IERR3 )
      CALL GEOR2N ( NEWREG, NRGNAM1, IERR4 )
      CALL GEOR2N ( MREG, MRGNAM2, IERR5 )
      CALL GEOR2N ( NEWREG, NRGNAM2, IERR6 )
      CALL GEOR2N ( MREG, MRGNAM3, IERR7 )
      CALL GEOR2N ( NEWREG, NRGNA3, IERR8 )
      CALL GEOR2N ( MREG, MRGNAM4, IERR9 )
      CALL GEOR2N ( NEWREG, NRGNAM4, IERR10 )
      CALL GEOR2N ( MREG, MRGNAM5, IERR11 )
      CALL GEOR2N ( NEWREG, NRGNAM5, IERR12 )
      IF(IERR1 .NE. 0 .OR. IERR2 .NE. 0) STOP "Error in name conversion"
      IF(IERR3 .NE. 0 .OR. IERR4 .NE. 0) STOP "Error in name conversion"
      IF(IERR5 .NE. 0 .OR. IERR6 .NE. 0) STOP "Error in name conversion"
      IF(IERR7 .NE. 0 .OR. IERR8 .NE. 0) STOP "Error in name conversion"
      IF(IERR9 .NE. 0 .OR. IERR10 .NE. 0) STOP "Error in nameconversion"
      IF(IERR11 .NE. 0 .OR. IERR12 .NE. 0) STOP "Error innameconversion"
      IF(MRGNAM .EQ. "VOID" .AND. NRGNAM .EQ. "CEILING") THEN
         IF(JTRACK .EQ. 7) WRITE(88,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      IF(MRGNAM1 .EQ. "VOID" .AND. NRGNAM1 .EQ. "FLOOR") THEN
         IF(JTRACK .EQ. 7) WRITE(11,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      IF(MRGNAM2 .EQ. "VOID" .AND. NRGNAM2 .EQ. "WALL_1") THEN
         IF(JTRACK .EQ. 7) WRITE(22,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      IF(MRGNAM3 .EQ. "VOID" .AND. NRGNAM3 .EQ. "WALL_2") THEN
         IF(JTRACK .EQ. 7) WRITE(33,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      IF(MRGNAM4 .EQ. "VOID" .AND. NRGNAM4 .EQ. "WALL_3") THEN
         IF(JTRACK .EQ. 7) WRITE(44,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      IF(MRGNAM5 .EQ. "VOID" .AND. NRGNAM5 .EQ. "WALL_4") THEN
         IF(JTRACK .EQ. 7) WRITE(77,'(1P,8G25.15)') 
     &      XSCO, YSCO, ZSCO, ETRACK, WTRACK,CXTRCK, CYTRCK, CZTRCK
      END IF
      END IF
      END IF
      END IF
      END IF
      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 SUBROUTINE Mgdraw ==========================================*
      END

