*$ CREATE USREIN.FOR
*COPY USREIN
*
*=== Usrein ===========================================================*
*
      SUBROUTINE USREIN

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
      INCLUDE '(FLKSTK)'
      INCLUDE '(FHEAVY)'
      INCLUDE '(PAPROP)'
     
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1991-2005      by    Alfredo Ferrari & Paola Sala  *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     USeR Event INitialization: this routine is called before the     *
*     showering of an event is started, but after the source particles *
*     of that event have been already loaded on the stack              *
*                                                                      *
*     Created on 01 january 1991   by    Alfredo Ferrari & Paola Sala  *
*                                                   Infn - Milan       *
*                                                                      *
*     Last change on 09-apr-99     by    Alfredo Ferrari               *
*                                                                      *
*                                                                      *
*----------------------------------------------------------------------*
*     
      INTEGER ii
      real*8 xvertex,yvertex,zvertex
      LOGICAL LFIRST
      SAVE LFIRST
      DATA LFIRST /.TRUE./

* return message from first call
      IF (LFIRST) THEN
	WRITE(LUNOUT,*) 'Routine usrein called'
	LFIRST = .FALSE.
        OPEN(UNIT = 22, FILE = 'gen_for.out', STATUS = 'NEW')
c        OPEN(UNIT = 23, FILE = 'gen_unf.dat', FORM='UNFORMATTED',
c     +  STATUS = 'NEW')
        iev=0
      ENDIF

      if(NPFLKA.ge.1) then
      iev=iev+1
      xvertex = xflk(1)
      yvertex = yflk(1)
      zvertex = zflk(1)
      WRITE(22, 1011) iev, NPFLKA
      write(22,1013) xvertex,yvertex,zvertex
      DO ii = 1, NPFLKA
      WRITE(22,1012) Iloflk(ii), Pmoflk(ii), Txflk(ii), Tyflk(ii),
     +        Tzflk(ii), Tkeflk(ii)
      ENDDO
c      write(23) NPFLKA,xvertex,yvertex,zvertex
c      write(23) (Iloflk(ii),Txflk(ii),Tyflk(ii),Tzflk(ii),Pmoflk(ii),
c     +ii=1,npflka)
      ENDIF
1011  FORMAT(1x,'*** Event no.',i5,'   fluka particles in stack = ',I6)
1012  FORMAT(1x,'id =',I9,'  E,ux,uy,uz,T',5F14.5)
1013  FORMAT(1x,'x,y,z collision vertex =',3F14.5)

      RETURN
*===  End of subroutine Usrein =========================================*
      END
