*$ 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 '(LTCLCM)'
      INCLUDE '(RTDFCM)'
*
      DIMENSION DTQUEN ( MXTRCK, MAXQMG )
*
      CHARACTER*20 FILNAM
      LOGICAL LFCOPE
      SAVE LFCOPE
      DATA LFCOPE / .FALSE. /

      LOGICAL LFIRST
      SAVE LFIRST
      SAVE NAIR,NBPIPE,NQUADRA,NTARGET,BANGLE
      DATA LFIRST / .TRUE. /
*
*----------------------------------------------------------------------*
*                                                                      *
*     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                                      *
*                                                                      *
*----------------------------------------------------------------------*
*                                                                      *
*  +-------------------------------------------------------------------*
      RETURN
*
*======================================================================*
*                                                                      *
*     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 )
*
      IF (LFIRST) THEN
         LFIRST= .FALSE.
         OPEN ( UNIT = 40, FILE = "dump40", STATUS = 'NEW')
         OPEN ( UNIT = 41, FILE = "dump41", STATUS = 'NEW')
         OPEN ( UNIT = 42, FILE = "dump42", STATUS = 'NEW')
         OPEN ( UNIT = 43, FILE = "dump43", STATUS = 'NEW')
         OPEN ( UNIT = 44, FILE = "dump44", STATUS = 'NEW')
         OPEN ( UNIT = 45, FILE = "dump45", STATUS = 'NEW')
         OPEN ( UNIT = 46, FILE = "dump46", STATUS = 'NEW')
         OPEN ( UNIT = 47, FILE = "dump47", STATUS = 'NEW')
         OPEN ( UNIT = 48, FILE = "dump48", STATUS = 'NEW')
         OPEN ( UNIT = 49, FILE = "dump49", STATUS = 'NEW')
*
         CALL GEON2R("rAir      ",NAIR,IERR)
         CALL GEON2R("rBPipe    ",NBPIPE,IERR)
         CALL GEON2R("rQuadrA   ",NQUADRA,IERR)
         CALL GEON2R("rTarget   ",NTARGET,IERR)
         BANGLE = WHASOU(5)         
      END IF
      IF(LTRACK.EQ.1 .AND. JTRACK.EQ.1) THEN
         LUNIT=-1
         IF (MREG.EQ.NAIR.AND.NEWREG.EQ.NBPIPE) THEN
            LUNIT=40
         END IF
         IF (MREG.EQ.NBPIPE.AND.NEWREG.EQ.NQUADRA) THEN
            IF (MLATTC.EQ.1) THEN
               LUNIT=41
            ELSE IF (MLATTC.EQ.2) THEN
               LUNIT=43
            ELSE IF (MLATTC.EQ.3) THEN
               LUNIT=45
            ELSE IF (MLATTC.EQ.4) THEN
               LUNIT=47
            END IF
         END IF
         IF (MREG.EQ.NQUADRA.AND.NEWREG.EQ.NBPIPE) THEN
            IF (MLATLD.EQ.1) THEN
               LUNIT=42
            ELSE IF (MLATLD.EQ.2) THEN
               LUNIT=44
            ELSE IF (MLATLD.EQ.3) THEN
               LUNIT=46
            ELSE IF (MLATLD.EQ.4) THEN
               LUNIT=48
            END IF
         END IF
         IF (MREG.EQ.NBPIPE.AND.NEWREG.EQ.NTARGET) THEN
            LUNIT=49
         END IF
         IF (LUNIT.NE.-1) THEN
            IF (LUNIT.LT.45) THEN
               TXSCO=XSCO*COS(BANGLE*DEGRAD)-ZSCO*SIN(BANGLE*DEGRAD)
               TYSCO=YSCO
               T1CXTRCK=CXTRCK*COS(BANGLE*DEGRAD)-
     *                  CZTRCK*SIN(BANGLE*DEGRAD)
               T1CYTRCK=CYTRCK
               T1CZTRCK=CXTRCK*SIN(BANGLE*DEGRAD)+
     *                  CZTRCK*COS(BANGLE*DEGRAD)
            ELSE
               TXSCO=XSCO
               TYSCO=YSCO
               TZSCO=ZSCO
               T1CXTRCK=CXTRCK
               T1CYTRCK=CYTRCK
               T1CZTRCK=CZTRCK
            END IF
            TCXTRCK=T1CXTRCK/T1CZTRCK
            TCYTRCK=T1CYTRCK/T1CZTRCK
            WRITE (LUNIT,*) TXSCO,TYSCO,TCXTRCK,TCYTRCK
         END IF
      END IF
      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 )
* No output by default:
      RETURN
*=== End of subrutine Mgdraw ==========================================*
      END
