*$ 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
      LOGICAL LFCOPE
      SAVE LFCOPE
      DATA LFCOPE / .FALSE. /

* User introduced variables
      CHARACTER*8 MRGNAM, NRGNAM, ORGNAM
      LOGICAL FIRSTCAL
      SAVE FIRSTCAL
      DATA FIRSTCAL / .TRUE. /


* First call (initialize file and variables)
      OPEN ( UNIT = 89, FILE="enter_det.dat",STATUS ='UNKNOWN')
      OPEN ( UNIT = 88, FILE="leave_wat.dat",STATUS ='UNKNOWN')

      IF ( FIRSTCAL ) THEN
       WRITE(88,*) "   EventID    PartID            ToA         E[GeV]
     &            x              y              z           Cosx        
     &   Cosy           Cosz"
      ENDIF
      
      IF ( FIRSTCAL ) THEN
       WRITE(89,*) "   EventID    PartID            ToA         E[GeV]
     &            x              y              z           Cosx        
     &   Cosy           Cosz"
       FIRSTCAL = .FALSE.
      ENDIF

*
*----------------------------------------------------------------------*
*                                                                      *
*     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                                    *
*                                                                      *
*     Useful variables                                                 *
*     JTRACK    =   Particle code (7 photon, 3 electron)               *
*     ATRACK    =   Age of the particle                                *
*     TKESUM    =   Energy of the primary particle                     *
*======================================================================*
*                                                                      *
      ENTRY BXDRAW ( ICODE, MREG, NEWREG, XSCO, YSCO, ZSCO )

* Boundary crossing. Check the type of incoming particle
      CALL GEOR2N ( MREG, ORGNAM, IERR2 )
      CALL GEOR2N ( NEWREG, NRGNAM, IERR2 )

      
      IF ((ORGNAM.EQ.'target').AND.(NRGNAM(1:3).EQ.'air')) THEN 
       OPEN ( UNIT = 88, FILE="leave_wat.dat",STATUS ='UNKNOWN')
998    FORMAT ( I10, I10, 8(E15.5))
       WRITE(88,998) NCASE, JTRACK, ATRACK, Etrack,
     & XSCO, YSCO, ZSCO, CXTRCK, CYTRCK, CZTRCK
      ENDIF 
      
      
      IF ((ORGNAM.EQ.'air3').AND.(NRGNAM.EQ.'detector')) THEN
* Write to file the type of particle and the ToF
       OPEN ( UNIT = 89, FILE="enter_det.dat",STATUS ='UNKNOWN')
997    FORMAT ( I10, I10, 8(E15.5))
       WRITE(89,997) NCASE, JTRACK, ATRACK, Etrack,
     & XSCO, YSCO, ZSCO, CXTRCK, CYTRCK, CZTRCK
      ENDIF
    
      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                                            *
*                                                                      *
*     Useful variables:                                                *
*     RULL      =     Local energy deposition (i.e. point-like depos.) *  
*======================================================================*
*                                                                      *

      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                  *
*             237: mu pair     production 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 (ICODE.EQ.101) THEN  
        ISPUSR(1)=MREG
      ENDIF
      
      If (ICODE.EQ.100) THEN
        ISPUSR(2)=MREG
      ENDIF
      
      ISPUSR(3)=MREG
      ISPUSR(4)=ICODE
      RETURN

*=== End of subrutine Mgdraw ==========================================*

      END

