*$ CREATE DETSUW.FOR
*COPY DETSUW
*
*=== Detsuw ===========================================================*
*
      PROGRAM DETSUW
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1990-2010      by    Alfredo Ferrari & Paola Sala  *
*     All Rights Reserved.                                             *
*                                                                      *
*----------------------------------------------------------------------*
*
      PARAMETER (PI    = 3.141592653589793)
      PARAMETER (PI2   = PI/2.)
      PARAMETER (NBNMX = 4096 )
      PARAMETER (MXDET =  100 )
      CHARACTER FILE*80,RUNTIM*32,RUNTIT*80,CHNAME*10
      DIMENSION IV(NBNMX,MXDET),IE(NBNMX)
      DIMENSION NDET(MXDET),CHNAME(MXDET),NBIN(MXDET),EMIN(MXDET),
     &          EBIN(MXDET),ECUT(MXDET)
      DOUBLE PRECISION WCTOT,WEIBIN,WEIERR,EBNMIN,EBNMAX
*
      MDET =0
      NCTOT=0
      MCTOT=0
      WCTOT=0.D+00
      DO N = 1, MXDET
         DO I  = 1, NBNMX
            IV (I,N) = 0
         END DO
      END DO
      DO IF = 1,30000
         WRITE(*,*)' Input file ?'
         READ(*,'(A)')FILE
         IF ( FILE .NE. '            ' ) THEN
            OPEN ( UNIT=51,FILE=FILE,STATUS='OLD',FORM='UNFORMATTED')
            READ (51,ERR=30) RUNTIT,RUNTIM,WEIPRI,NCASE,MCASE
            WRITE(*,*)RUNTIT
            WRITE(*,*)RUNTIM
            WRITE(*,*)WEIPRI
            WRITE(*,*)NCASE
            WRITE(*,*)MCASE
            GO TO 200
 30         CONTINUE
            REWIND 51
            READ (51,ERR=50) RUNTIT,RUNTIM,WEIPRI,NCASE
            WRITE(*,*)RUNTIT
            WRITE(*,*)RUNTIM
            WRITE(*,*)WEIPRI
            WRITE(*,*)NCASE
            MCASE=0
            GO TO 200
 50         CONTINUE
            REWIND 51
            READ (51,ERR=100) RUNTIT,RUNTIM,NCASE
            WRITE(*,*)RUNTIT
            WRITE(*,*)RUNTIM
            WRITE(*,*)' WEIPRI?'
            READ (*,*)WEIPRI
            WRITE(*,*)NCASE
            MCASE=0
            GO TO 200
 100        CONTINUE
            WRITE(*,*)' WEIPRI?'
            READ (*,*)WEIPRI
            WRITE(*,*)' NCASE?'
            READ (*,*)NCASE
            MCASE=0
            REWIND 51
 200        CONTINUE
            WCTOT=WCTOT+DBLE(WEIPRI)
            MCTOT=MCTOT+MCASE
            IF ( DBLE(NCASE) + DBLE(NCTOT) .GE. 0.9999999999D+09 ) THEN
               MCTOT = MCTOT + 1
               NCTOT = NCTOT - 1000000000
            END IF
            NCTOT=NCTOT+NCASE
         ELSE
            GO TO 300
         END IF
         DO JDET=1,1000
            READ(51,END=250) NDET(JDET),CHNAME(JDET),NBIN(JDET),
     &                       EMIN(JDET),EBIN(JDET),ECUT(JDET)
            IF ( IF .EQ. 1 ) WRITE(*,*) NDET(JDET),CHNAME(JDET),
     &              NBIN(JDET),EMIN(JDET),EBIN(JDET),ECUT(JDET),
     &              EMIN(JDET)+NBIN(JDET)*EBIN(JDET)
            READ(51) (IE(I), I=1,NBIN(JDET))
            DO I=1,NBIN(JDET)
               IV(I,JDET)=IV(I,JDET)+IE(I)
            END DO
         END DO
 250     CONTINUE
         CLOSE(UNIT=51)
         MDET=JDET-1
      END DO
 300  CONTINUE
      IF=IF-1
      WRITE(*,*)' Output file ?'
      READ(*,'(A)')FILE
      LQ    = LNNBLN(FILE)
      OPEN ( UNIT=50,FILE=FILE,STATUS='UNKNOWN',FORM='UNFORMATTED')
      INDX  = INDLST ( FILE, '.' )
* Start_VAX_seq
*     IF ( INDX .GT. 0 ) THEN
*        OPEN (UNIT=4,FILE=FILE(1:INDX-1)//'_TAB.LIS',STATUS='NEW',
*    &         CARRIAGECONTROL='LIST')
*     ELSE
*        OPEN (UNIT=4,FILE=FILE(1:LQ)//'_TAB.LIS',STATUS='NEW',
*    &         CARRIAGECONTROL='LIST')
*     END IF
* End_VAX_seq
* Start_UNIX_seq
      IF ( INDX .GT. 0 ) THEN
         OPEN (UNIT=4,FILE=FILE(1:INDX-1)//'_tab.lis',STATUS='UNKNOWN')
      ELSE
         OPEN (UNIT=4,FILE=FILE(1:LQ)//'_tab.lis',STATUS='UNKNOWN')
      END IF
* End_UNIX_seq
      RUNTIM='********    Sum file    ********'
      WRITE (50) RUNTIT,RUNTIM,REAL(WCTOT),NCTOT,MCTOT
      DO JDET=1,MDET
         WRITE(50) NDET(JDET),CHNAME(JDET),NBIN(JDET),EMIN(JDET),
     &             EBIN(JDET),ECUT(JDET)
         WRITE(50) (IV(I,JDET), I=1,NBIN(JDET))
*****   here to write on "table" file
         IF (JDET .GT. 1) THEN
*****       write 2 empty lines so gnuplot can use it with 'index #'
            WRITE (4,*)
            WRITE (4,*)
         END IF
         WRITE (4,*) '# Detector n: ', JDET,'  ',CHNAME(JDET)
         WRITE (4,*) '# N. of energy intervals' ,NBIN(JDET)
         DO 6000 I=1,NBIN(JDET)
            EBNMIN = DBLE (EMIN(JDET)) + DBLE(I-1) * DBLE (EBIN(JDET))
            EBNMAX = DBLE (EMIN(JDET)) + DBLE (I)  * DBLE (EBIN(JDET))
            WEIBIN = DBLE (IV(I,JDET))
     &             / ( DBLE (NCTOT) + 1.D+09 * DBLE (MCTOT) )
            WEIERR = 1.D+00 / SQRT (DBLE(MAX(IV(I,JDET),1)))
            WRITE (4,9501) EBNMIN, EBNMAX,
     &                     WEIBIN, WEIERR * 100.D+00
 6000    CONTINUE
******  end write on tab file
      END DO
 9999 CONTINUE
 9501 FORMAT (4(1P,1X,E10.3))
      STOP
      END

