
C **********************************************************************

      SUBROUTINE LMIDAT

C...This is the MINUIT routine MIDATA.
CC        GETS PARAMETERS FROM /LMINUI/  AND /LMINUC/
CC        AND SETS UP THE STARTING PARAMETER LISTS.
CC        CONTROL THEN PASSES TO LMCMND FOR READING THE COMMAND "CARDS".
CC

      COMMON
     1/LMMINE/ ERP(30)  ,ERN(30)
     2/LMPARI/ X(15)    ,XT(15)   ,DIRIN(15) ,MAXINT     ,NPAR
     3/LMPARE/ U(30)              ,WERR(30)  ,MAXEXT     ,NU
     4/LMLIMI/ ALIM(30) ,BLIM(30) ,LCODE(30) ,LCORSP(30) ,LIMSET
     5/LMVARI/ V(15,15)
     7/LMFIX / IPFIX(15),XS(15)   ,XTS(15)   ,DIRINS(15) ,NPFIX
     7/LMFIX2/ GRDS(15) ,G2S(15)  ,GSTEPS(15),ABERFS(15)
     C/LMCASC/ Y(16)    ,JH       ,JL
     F/LMDERI/ GIN(30)  ,GRD(15)  ,G2(15)    ,GSTEP(15)  ,ABERF(15)
     G/LMSIMV/ P(15,16) ,PSTAR(15),PSTST(15) ,PBAR(15)   ,PRHO(15)
     J/LMVART/ VT(15,15)
      COMMON
     6/LMUNIT/ ISYSRD   ,ISYSWR   ,ISYSPU
     8/LMTITL/ TITLE(13),DATE(2)  ,ISW(7)    ,NBLOCK
     9/LMCONV/ EPSI     ,APSI     ,VTEST     ,NSTEPQ     ,NFCN ,NFCNMX
     A/LMCARD/ CWORD    ,CWORD2   ,CWORD3    ,WORD7(7)
     B/LMMINI/ AMIN     ,UP       ,NEWMIN    ,ITAUR      ,SIGMA,EPSMAC
      COMMON /LMINUI/ XKIN(4),UKIN(4),WKIN(4),AIN(4),BIN(4),
     &MAXFIN,RELUP,RELERR,RELER2,FCNMAX
      COMMON /LMINUC/ NAMKIN(4),NAM(30)
      COMMON /LPFLAG/ LST3
      CHARACTER*10 NAMKIN,NAM,NAMK,BLANK
      CHARACTER XTITLE*60
      REAL LMPINT
      DATA BLANK/'          '/
      DATA XTITLE/' FIND MINIMUM OF -(DIFFERENTIAL CROSS SECTION)'/
      DATA MNINIT/0/,IFATAL,NINT/0,0/
C                                        . INITIALIZE NEW DATA BLOCK . .
      IF (MNINIT .EQ. 0)  NBLOCK=0
      MNINIT = 1
      NBLOCK = NBLOCK + 1
      VERSN = 11.79
      IF(LST3.GE.5) THEN
        WRITE (ISYSWR,1004)  MAXINT,MAXEXT,VERSN,NBLOCK
        WRITE (ISYSWR,1005)
      ENDIF
      DO 50 I= 1, 7
   50 ISW(I) = 0
      SIGMA = 0.
      CALL LTIMEX(TIME)
      IF(LST3.GE.5) THEN
        WRITE (ISYSWR,1110)  XTITLE,TIME,EPSMAC
        WRITE (ISYSWR,1005)
      ENDIF
      NPFIX = 0
      NINT = 0
      NU = 0
      NPAR = 0
      IFATAL = 0
      IF(LST3.GE.5) WRITE (ISYSWR,1005)
      DO 100 I= 1, MAXEXT
      U(I) = 0.0
      NAM(I) = BLANK
      ERP(I) = 0.0
      ERN(I) = 0.0
      LCODE(I) = 0
  100 LCORSP (I) = 0
      UP = 1.0
      ISW(5) = 1
      IUNIT = ISYSRD
C                                        . . . READ PARAMETER CARDS . .
      ENTRY LMIDA2
      DO 200 I= 1, 200
      IF(I.GE.5) GOTO 250
      XK=XKIN(I)
      NAMK=NAMKIN(I)
      UK=UKIN(I)
      WK=WKIN(I)
      A=AIN(I)
      B=BIN(I)
      K = XK + 0.1
      NU = MAX0(NU,K)
      IF (K .LE. 0)  GO TO 250
      IF (K .LE. MAXEXT)  GO TO 115
      IFATAL = IFATAL + 1
      IF(LST3.GE.1) THEN
        WRITE (ISYSWR,1009)  K,MAXEXT
        WRITE (ISYSWR,1002)  K,NAMK,UK,WK,A,B
      ENDIF
      GO TO 200
  115 CONTINUE
      IF(NAM(K).EQ.BLANK) GO TO 120
C         PREVIOUSLY DEFINED PARAMETER IS BEING REDEFINED
      IF(LST3.GE.1) WRITE(ISYSWR,1007)
      IF(WERR(K).GT..0) NINT=NINT-1
  120 CONTINUE
      NAM(K) = NAMK
      U(K) = UK
      WERR(K) = WK
      IF (WK .GT. 0.0)  GO TO 122
C                                        . . . FIXED PARAMETER . . . .
      IF(LST3.GE.5) WRITE (ISYSWR, 1002)  K,NAMK,UK
      LCODE(K) = 0
      GO TO 160
C                                        . . . VARIABLE PARAMETER . . .
  122 IF(LST3.GE.5) WRITE (ISYSWR, 1002)  K,NAMK,UK,WK,A,B
      NINT = NINT + 1
      ISW(2) = 0
      IF  (A)  140,130,140
  130 IF  (B)  140,135,140
  135 LCODE(K) = 1
      GO TO 160
  140 IF (B-A)  145,142,150
  142 IFATAL = IFATAL + 1
      IF(LST3.GE.1) WRITE (ISYSWR,1010)
      GO TO 150
  145 SAV = B
      B = A
      A = SAV
      IF(LST3.GE.1) WRITE (ISYSWR,1003)
  150 ALIM(K) = A
      BLIM(K) = B
      LCODE(K) = 4
      IF ((B-U(K))*(U(K)-A))  153,155,160
  153 IFATAL = IFATAL + 1
      IF(LST3.GE.1) WRITE (ISYSWR,1011)
      GO TO 160
  155 IF(LST3.GE.1) WRITE (ISYSWR,1006)
  160 CONTINUE
  200 CONTINUE
      IFATAL = IFATAL + 1
      IF(LST3.GE.1) WRITE (ISYSWR,1012)
C                                       . . . END PARAMETER CARDS
C                                       . .   . STOP IF FATAL ERROR
  250 IF(LST3.GE.5) WRITE (ISYSWR,1005)
      IF (NINT .LE. MAXINT)  GO TO 253
      IF(LST3.GE.1) WRITE (ISYSWR,1008)  NINT,MAXINT
      IFATAL = IFATAL + 1
  253 IF (IFATAL .LE. 0)  GO TO 280
      IF(LST3.GE.1) WRITE (ISYSWR,1013)  IFATAL
      IF(LST3.GE.2) STOP
C                                       CALCULATE STEP SIZES DIRIN
  280 NPAR = 0
      DO 300 K= 1, NU
      IF (LCODE(K) .LE. 0)  GO TO 300
      NPAR = NPAR + 1
      LCORSP(K) = NPAR
      SAV = U(K)
      X(NPAR) = LMPINT(SAV,K)
      XT(NPAR) = X(NPAR)
      SAV2 = SAV + WERR(K)
      VPLU = LMPINT(SAV2,K) - X(NPAR)
      SAV2 = SAV - WERR(K)
      VMINU = LMPINT(SAV2,K) - X(NPAR)
      DIRIN(NPAR) = 0.5 * (ABS(VPLU) +ABS(VMINU))
      G2(NPAR) = 2.0 / DIRIN(NPAR)**2
      GSTEP(NPAR) = DIRIN(NPAR)
      IF (LCODE(K) .GT. 1)  GSTEP(NPAR) = -GSTEP(NPAR)
  300 CONTINUE
      SIGMA = 1.0E10
      IUNIT = ISYSRD
      RETURN
C...     THE FORMAT BELOW IS MACHINE-DEPENDENT. (A10) , (A6,4X) , ETC.
 1002 FORMAT (I10,2X,A10,2X,2G12.6,2X,2G12.6)
 1003 FORMAT(' WARNING           - ABOVE LIMITS HAVE BEEN REVERSED.')
 1004 FORMAT (1H1/42X,21(1H*)/42X,21H*   D506   MINUIT   */42X,
     112H* DIMENSIONS, I3, 1H/, I3, 2H */   42X,
     1'*  MODIFICATION OF  *',/,42X,
     111H*   VERSION ,F6.2,4H   */42X,16H* DATA BLOCK NO. ,I3,2H *)
 1005 FORMAT (4X,96(1H*))
 1006 FORMAT(' WARNING           - ABOVE PARAMETER IS AT LIMIT ')
 1007 FORMAT(' WARNING  *******  - PARAMETER REQUESTED ON FOLLOWING',
     1' CARD HAS ALREADY APPEARED.  PREVIOUS VALUES IGNORED.')
 1008 FORMAT('0   TOO MANY VARIABLE PARAMETERS.  YOU REQUEST',I5/,
     +'   THIS VERSION OF MINUIT IS ONLY DIMENSIONED FOR',I4//)
 1009 FORMAT('0FATAL ERROR. PARAMETER NUMBER',I11,' GREATER THAN ',
     +'ALLOWED MAXIMUM',I4)
 1010 FORMAT(' FATAL ERROR. UPPER AND LOWER LIMITS ARE EQUAL.')
 1011 FORMAT(' FATAL ERROR. PARAMETER OUTSIDE LIMITS',/)
 1012 FORMAT('0FATAL ERROR. MORE THAN 200 PARAMETER CARDS',/)
 1013 FORMAT(/I5,' FATAL ERRORS ON PARAMETER CARDS.  ABORT.',//)
 1110 FORMAT(5X,A60,5X,'TIME',F8.3,' SECONDS',/,70X,'MACH. PREC.=',
     &E10.2)
      END
