      SUBROUTINE GUOUT
C
C        $Id: guout.F,v 1.17 2001/02/18 05:09:16 ramberg Exp $
C        $Author: ramberg $
C
C      Original Author:  Dima Vavilov, October 1999
C
      IMPLICIT   NONE

#include "gtnum.inc"
#include "gcnum.inc"
#include "gtflag.inc"
#include "gcflag.inc"
#include "gtkine.inc"
#include "gckine.inc"

#include "ckm_para.inc"
#include "ckm_files.inc"
#include "ckm_units.inc"
c
C     Local variables
      Logical LFirst
      Character*24 username
#ifdef LINUX
      Character ctime
#else
      Character*24 ctime
#endif
      Integer time,nvert,nubuf,blklen,it,j,lipart
#ifndef LINUX
      Integer ipart
#endif
      Real lvert(3),lpvert(4),ubuf(10)
      Data LFirst /.True./

      Save LFirst

C  Output of different packages

c      Write(*,*) output_mode

c
c  On first event, print out run information
c
        If (LFirst) Then

          Open(UOUTPUT,File=outputfile,Status='Unknown')

          If (output_mode(1).ne.0.or.output_mode(2).ne.0) Then  

            Write(UOUTPUT,*) ctime(time()) 
            If (IKINE.eq.-1) Then
              Write(UOUTPUT,1010) IFIX(PKINE(1)),(PKINE(j),j=2,7)
            ElseIf (IKINE.eq.-2) Then
              Write(UOUTPUT,1013) IFIX(PKINE(1)),(PKINE(j),j=2,7),
     &           (1000*PKINE(j),j=8,9)
            ElseIf (IKINE.eq.0 .or. IKINE.eq.1) Then
              Write(UOUTPUT,1011) IKINE,beamfile(:blklen(beamfile))
            Else
              Write(UOUTPUT,1012) IKINE
            EndIf
 1010     Format(' IKINE -1, PKINE ',I2,1X,6(F8.2,1X))
 1011     Format(' IKINE ',I2,' Beamfile ',A)
 1012     Format(' IKINE ',I2)
 1013     Format(' IKINE -2, PKINE ',I2,1X,3(F8.2,1X),2(F7.4,1X),
     &         1(F5.2,1X),2(F4.1,1X))
            Call getlog(username)
            Write(UOUTPUT,*) 'User ', username

          EndIf
        EndIf
c
c  Track information for each event
c
        Write(UOUTPUT,*) '---------------------------------------------'

        if(output_mode(1).ne.0.or.output_mode(2).ne.0)then

          Write(UOUTPUT,*) 'GENERAL'
          Write(UOUTPUT,*) IEVENT,ckm_nrndm(1),ckm_nrndm(2)
          Write(UOUTPUT,*) NTRACK
          Do it = 1,NTRACK
            Call GFKINE(it,lvert,lpvert,lipart,nvert,ubuf,nubuf) ! kine info
            IF (NUBUF.GT.0) THEN                    ! there is user info
              If (IFIX(UBUF(1)).eq.0) Then          ! Particle is original
                Write(UOUTPUT,1002) 0,it,lipart,lvert,lpvert,ubuf(2)
              EndIf
            EndIf
          EndDo
          Do it = 1,NTRACK
            Call GFKINE(it,lvert,lpvert,lipart,nvert,ubuf,nubuf) ! kine info
            IF (NUBUF.GT.0) THEN                    ! there is user info
              If (IFIX(UBUF(1)).ne.0) Then          ! Particle is decay
                Write(UOUTPUT,1002) 1,IFIX(UBUF(1)),lipart,lvert,
     &             lpvert,UBUF(2)
              EndIf
            EndIf
          EndDo
 1002     Format(I1,1X,I2,1X,I2,1X,3(F10.3,1X),2(E14.7,1X),
     &       2(F9.5,1X),E14.7)

      end if

      if(output_mode(1).ne.0.or.output_mode(3).ne.0)Call UMS_GUOUT

      if(output_mode(1).ne.0.or.output_mode(4).ne.0)Call KRCH_GUOUT
      
      if(output_mode(1).ne.0.or.output_mode(5).ne.0)Call KEAT_GUOUT

c     if(output_mode(1).ne.0.or.output_mode(6).ne.0)Call BIVS_GUOUT

c     if(output_mode(1).ne.0.or.output_mode(7).ne.0)Call VVS_GUOUT

      if(output_mode(1).ne.0.or.output_mode(8).ne.0)Call STRAW_GUOUT

      if(output_mode(1).ne.0.or.output_mode(9).ne.0)Call PRCH_GUOUT

      if(output_mode(1).ne.0.or.output_mode(10).ne.0)Call FVS_GUOUT

      if(output_mode(1).ne.0.or.output_mode(11).ne.0)Call MVS_GUOUT

C
C     User actions
C
      CALL UUOUT

      LFirst = .False.

      RETURN
      END
C
C
C  $Log: guout.F,v $
C  Revision 1.17  2001/02/18 05:09:16  ramberg
C  Added new OUTPUT_MODE switching for calls to sub-detector output
C
C  Revision 1.16  2001/02/15 23:25:09  jurgen
C  actually use local ipart
C
C  Revision 1.15  2001/02/12 18:23:07  jurgen
C  Fix overwriting of ipart in gfkine calls
C
C  Revision 1.14  2001/01/29 21:29:07  caroline
C       call bivs_guout- to write the  output from BIVS
C
C  Revision 1.13  2001/01/05 02:09:30  jurgen
C  introduce and handle ikine=-2 (angle smear)
C
C  Revision 1.12  2001/01/03 04:42:37  ramberg
C  Added output of # of tracks for read routine
C
C  Revision 1.11  2000/12/19 18:00:57  ramberg
C  Added call to KEAT guout routine
C
C  Revision 1.10  2000/11/03 10:48:42  ramberg
C  added call to ums_guout
C
C  Revision 1.8  2000/09/08 14:46:35  jurgen
C  fix typo.
C
C  Revision 1.7  2000/09/05 23:38:59  jurgen
C  add writing of random number seed at begin of event
C
C  Revision 1.6  2000/08/17 01:06:29  ckm
C  add LINUX compatability.
C
C  Revision 1.5  2000/08/02 17:36:25  ckm
C  Totally re-written by Jurgen
C  Outputs all info into one file in ascii format
C
C  Revision 1.4  1999/10/28 00:13:12  jurgen
C  Changes by Dima Vavilov:
C  1. Remove everything (contained just Erik's veto checks).
C
C
C
