*$ 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)'
      INCLUDE '(RESNUC)'
      character*8 mrgnam
*
      DIMENSION DTQUEN ( MXTRCK, MAXQMG )
*
      CHARACTER*20 FILNAM
      LOGICAL LFCOPE
      SAVE LFCOPE
      DATA LFCOPE / .FALSE. /
*
*----------------------------------------------------------------------*
*                                                                      *
*     Icode = 1: call from Kaskad                                      *
*     Icode = 2: call from Emfsco                                      *
*     Icode = 3: call from Kasneu                                      *
*     Icode = 4: call from Kashea                                      *
*     Icode = 5: call from Kasoph                                      *
*                                                                      *
*----------------------------------------------------------------------*
*                                                                      *
*
*======================================================================*
*                                                                      *
*     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 )
      RETURN
*
*======================================================================*
*                                                                      *
*     Event End DRAWing:                                               *
*                                                                      *
*======================================================================*
*                                                                      *
      ENTRY EEDRAW ( ICODE )
      RETURN
*
*======================================================================*
*                                                                      *
*     ENergy deposition DRAWing:                                       *
*                                                                      *
*     Icode = 1x: call from Kaskad                                     *
*             10: elastic interaction recoil                           *
*             11: inelastic interaction recoil                         *
*             12: stopping particle                                    *
*             13: pseudo-neutron deposition                            *
*             14: escape                                               *
*             15: time kill                                            *
*     Icode = 2x: call from Emfsco                                     *
*             20: local energy deposition (i.e. photoelectric)         *
*             21: below threshold, iarg=1                              *
*             22: below threshold, iarg=2                              *
*             23: escape                                               *
*             24: time kill                                            *
*     Icode = 3x: call from Kasneu                                     *
*             30: target recoil                                        *
*             31: below threshold                                      *
*             32: escape                                               *
*             33: time kill                                            *
*     Icode = 4x: call from Kashea                                     *
*             40: escape                                               *
*             41: time kill                                            *
*             42: delta ray stack overflow                             *
*     Icode = 5x: call from Kasoph                                     *
*             50: optical photon absorption                            *
*             51: escape                                               *
*             52: time kill                                            *
*                                                                      *
*======================================================================*
*                                                                      *
      ENTRY ENDRAW ( ICODE, MREG, RULL, XSCO, YSCO, ZSCO )
      RETURN
*
*======================================================================*
*                                                                      *
*     SOurce particle DRAWing:                                         *
*                                                                      *
*======================================================================*
*
      ENTRY SODRAW
      RETURN
*
*======================================================================*
*                                                                      *
*     USer dependent DRAWing:                                          *
*                                                                      *
*     Icode = 10x: call from Kaskad                                    *
*             100: elastic   interaction secondaries                   *
*             101: inelastic interaction secondaries                   *
*             102: particle decay  secondaries                         *
*             103: delta ray  generation secondaries                   *
*             104: pair production secondaries                         *
*             105: bremsstrahlung  secondaries                         *
*             110: decay products                                      *
*     Icode = 20x: call from Emfsco                                    *
*             208: bremsstrahlung secondaries                          *
*             210: Moller secondaries                                  *
*             212: Bhabha secondaries                                  *
*             214: in-flight annihilation secondaries                  *
*             215: annihilation at rest   secondaries                  *
*             217: pair production        secondaries                  *
*             219: Compton scattering     secondaries                  *
*             221: photoelectric          secondaries                  *
*             225: Rayleigh scattering    secondaries                  *
*     Icode = 30x: call from Kasneu                                    *
*             300: interaction secondaries                             *
*     Icode = 40x: call from Kashea                                    *
*             400: delta ray  generation secondaries                   *
*  For all interactions secondaries are put on GENSTK common (kp=1,np) *
*  but for KASHEA delta ray generation where only the secondary elec-  *
*  tron is present and stacked on FLKSTK common for kp=npflka          *
*                                                                      *
*======================================================================*
*
      ENTRY USDRAW ( ICODE, MREG, XSCO, YSCO, ZSCO )
      IF ( .NOT. LFCOPE ) THEN
         LFCOPE = .TRUE.
*         OPEN ( UNIT = 30, FILE = "Be-7.dat",
*     $     STATUS = 'unknown', FORM = 'FORMATTED' )
*         OPEN ( UNIT = 31, FILE = "C-11.dat",
*     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 32, FILE = "Na-22.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 33, FILE = "Na-24.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 34, FILE = "K-42.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 35, FILE = "K-43.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 36, FILE = "Sc-44.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 37, FILE = "Sc-46.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 38, FILE = "Sc-47.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 39, FILE = "Sc-48.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 40, FILE = "V-48.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 41, FILE = "Cr-48.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 42, FILE = "Cr-51.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 43, FILE = "Mn-52.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 44, FILE = "Mn-54.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 45, FILE = "Co-55.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
         OPEN ( UNIT = 46, FILE = "Co-56.dat",
     $     STATUS = 'unknown', FORM = 'FORMATTED' )
      END IF
*
      call geor2n ( mreg, mrgnam, ierr1 )
      call USRDCI (jtrack, IONA, IONZ, IONM)
*
      If (IONA .eq. 56 .and. IONZ .eq. 26) then
*
* Write energy, coordinate, statistical wight of the products
       DO 100 ievap=1, npheav, 1
         IF (icheav(kheavy(ievap)) .eq. 11 .and.
     $       ibheav(kheavy(ievap)) .eq. 22) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(32,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 11 .and.
     $       ibheav(kheavy(ievap)) .eq. 24) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(33,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 19 .and.
     $       ibheav(kheavy(ievap)) .eq. 42) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(34,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 19 .and.
     $       ibheav(kheavy(ievap)) .eq. 43) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(35,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 21 .and.
     $       ibheav(kheavy(ievap)) .eq. 44) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(36,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 21 .and.
     $       ibheav(kheavy(ievap)) .eq. 46) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(37,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 21 .and.
     $       ibheav(kheavy(ievap)) .eq. 47) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(38,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 21 .and.
     $       ibheav(kheavy(ievap)) .eq. 48) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(39,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 23 .and.
     $       ibheav(kheavy(ievap)) .eq. 48) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(40,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 24 .and.
     $       ibheav(kheavy(ievap)) .eq. 48) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(41,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 24 .and.
     $       ibheav(kheavy(ievap)) .eq. 51) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(42,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 25 .and.
     $       ibheav(kheavy(ievap)) .eq. 52) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(43,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 25 .and.
     $       ibheav(kheavy(ievap)) .eq. 54) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(44,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 27 .and.
     $       ibheav(kheavy(ievap)) .eq. 55) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(45,'(2g10.4)') prie, wtrack
*
         ELSEIF (icheav(kheavy(ievap)) .eq. 27 .and.
     $       ibheav(kheavy(ievap)) .eq. 56) then
            prie = (etrack-am(jtrack))/(am(jtrack)/ AMUGEV)
            write(46,'(2g10.4)') prie, wtrack
*
         ENDIF
  100  CONTINUE
*
      ENDIF
      RETURN
*=== End of subrutine Mgdraw ==========================================*
      END

