      REAL FUNCTION nt951()
*     
      LOGICAL         CHAIN
      CHARACTER*128   CFILE
*     
      COMMON /PAWCHN/ CHAIN, NCHEVT, ICHEVT
      COMMON /PAWCHC/ CFILE
*     
      COMMON/PAWIDN/IDNEVT,OBS(13),
     +RUN     ,EVENT   ,X0START ,PHISTART,X0FIT   ,PHIFIT  ,
     +SIGMAFIT,Z       ,THETA   ,DXWOUT3 ,DXWOUT4 ,DXWOUT5 ,
     $DXWITH2 ,DXWITH3 ,  DXWITH4 ,DXWITH5 ,DXWITH6 
     $,AMPLROW
      DIMENSION AMPLROW(3),DXROW(3)
*      DATA AMPLROW /3*0./
*
      CHARACTER*20 CHTEXT
      PARAMETER (NZBIN=15)
*     
      IF(ICHEVT.EQ.1 )THEN
            CALL HBOOK1(10000,'events',30,80.5,110.5,0.)
            CALL HBOOK1(10001,'events phi',30,80.5,110.5,0.)
            CALL HBPROF(10002,'DX row 2',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10003,'DX row 3',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10004,'DX row 4',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10005,'DX row 5',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10006,'DX row 6',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10007,'Amplitude 1',30,97.5,127.5,1.,500.,' ')
            CALL HBPROF(10008,'Amplitude 2',30,97.5,127.5,1.,500.,' ')
            CALL HBPROF(10009,'Amplitude 3',30,97.5,127.5,1.,500.,' ')
            CALL HBPROF(10030,'DX row 2',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10031,'DX row 3',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10032,'DX row 4',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10033,'DX row 5',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10034,'DX row 6',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10035,'DX row 2',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10036,'DX row 3',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10037,'DX row 4',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10038,'DX row 5',32,-6.4,6.4,-2.,2.,' ')
            CALL HBPROF(10039,'DX row 6',32,-6.4,6.4,-2.,2.,' ')

            CALL HBOOK1(10010,'Amplitude',100.,0.,300.,0.)
            CALL HBOOK1(10011,'1/sq(Ampl)',100.,0.,0.5,0.)
            CALL HBOOK1(10012,'1/Ampl',100.,0.,0.25,0.)
            CALL HBOOK1(10013,'DX3,4,5',100,-1.,1.,0.)
            CALL HBOOK1(10014,'row 4',40,-8.,8.,0.)
            CALL HBOOK1(10015,'Amplitude z.lt.6',100.,0.,300.,0.)
            CALL HBOOK1(10016,'Amplitude 6-z-9',100.,0.,300.,0.)
            CALL HBOOK1(10017,'Amplitude 9.lt.z',100.,0.,300.,0.)
            CALL HBPROF(10018,'Mean Amplitude',15,0.,150.,1.,500.,' ')
            CALL HBOOK1(10019,'DX3,4,5, z.lt.2',100,-1.,1.,0.)


            CALL HBPROF(10020,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10021,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10022,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10023,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10024,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10025,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10026,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10027,'DX mod',20,0.,1.,-2.,2.,' ')
            CALL HBPROF(10028,'DX mod',20,0.,1.,-2.,2.,' ')

         DO I=1,NZBIN
* residual
            WRITE(CHTEXT,'(A11,I3)') 'Residual, Z',I
            CALL HBOOK1(11010+I,CHTEXT,31,-1.,1.,0.)
* amplitude
            WRITE(CHTEXT,'(A6,I3,A11)') '1/A, Z',I,'           '
            CALL HBOOK1(11030+I,CHTEXT,100.,0.,0.5,0.)
            WRITE(CHTEXT,'(A10,I3)') 'Amplit, Z',I
            CALL HBOOK1(11050+I,CHTEXT,100.,1.,300.,0.)

          DO J=1,6
            WRITE(CHTEXT,'(A11,I3,A2,I3)') 'Residual, A',J,' Z',I
            CALL HBOOK1(11010+I+J*100,CHTEXT,51,-1.5,1.5,0.)
          ENDDO
        ENDDO
        DO J=1,6
            WRITE(CHTEXT,'(A6,I3,A11)') '1/A, A',J,'           '
            CALL HBPROF(11030+J*100,CHTEXT,15,0.,150.,0.,1.,0.)
        ENDDO
         
      ENDIF

*      IF( nint(RUN).GT.100  )RETURN

      DX2 = DXWITH2
      DX6 = DXWITH6
      
*      DX3 = DXWOUT3
*      DX4 = DXWOUT4
*      DX5 = DXWOUT5
      DX3 = DX(DXWITH3,DXWOUT3)
      DX4 = DX(DXWITH4,DXWOUT4)
      DX5 = DX(DXWITH5,DXWOUT5)
      
      DXROW(1) = DX3
      DXROW(2) = DX4
      DXROW(3) = DX5
     
      CALL HF1(10000,run,1.)

*      IF( ABS(PHIFIT).GT.0.15 ) RETURN
      IF( ABS(PHIFIT).GT.0.08727 ) RETURN

      CALL HF1(10001,run,1.)
      
      IF( z.lt.60. )THEN
         IH = 10007
      ELSEIF( z.lt.90. )THEN
         IH = 10008
      ELSE
         IH = 10009
      ENDIF
      DO I=1,3
        CALL HFILL(IH,RUN,AMPLROW(I)/ amplcor(z),1.)
        CALL HFILL(10018,Z,AMPLROW(I),1.)
      ENDDO
      
      DO I=1,3
         AMPL = AMPLROW(I) / amplcor(z)
         IF( AMPL.GT.1. )THEN
         CALL HF1(10010,AMPL,1.)
         CALL HF1(10011,SQRT(1./AMPL),1.)
         CALL HF1(10012,1./AMPL,1.)
         IF( z.lt.52. )then
            CALL HF1(10015,AMPL,1.)
         elseIF( z.lt.95. )then
            CALL HF1(10016,AMPL,1.)
         else
            CALL HF1(10017,AMPL,1.)
         endif
         ENDIF
      ENDDO
      
      CALL HF1(10013,dx3,1.)
      CALL HF1(10013,dx4,1.)
      CALL HF1(10013,dx5,1.)
      IF( z.lt.10. )then
      CALL HF1(10019,dx3,1.)
      CALL HF1(10019,dx4,1.)
      CALL HF1(10019,dx5,1.)
      endif
      
      DO I=1,NZBIN
         IF( Z.LT. REAL(I)/REAL(NZBIN)*150.)THEN
            IZ = I
            GOTO 100
         ENDIF
      ENDDO
 100  CONTINUE
 
      CALL HF1(11010+IZ,DX3,1.)
*      CALL HF1(11010+IZ,DX4,1.)
      CALL HF1(11010+IZ,DX5,1.)
      
      DO i=1,3
         AMPL = AMPLROW(I) / amplcor(z)
         IF( AMPL.GT.1. )THEN
            A = 1./AMPL
         ELSE
            A = -1.
         ENDIF
         CALL HF1(11030+IZ,A,1.)
         CALL HF1(11050+IZ,AMPL,1.)
      ENDDO
      
      DO i=1,3
         AMPL = AMPLROW(I)/amplcor(z)
         DXX  = DXROW(I)
         
         IF( AMPL.GT.1. )THEN
            A = 1./AMPL
         ELSE
            A = -1.
         ENDIF
         IF(AMPL.LT.1. )THEN
            IA = 0
         ELSEIF(AMPL.LT.31. )THEN
            IA = 1
         ELSEIF(AMPL.LT.41. )THEN
            IA = 2
         ELSEIF(AMPL.LT.51. )THEN
            IA = 3
         ELSEIF(AMPL.LT.63. )THEN
            IA = 4
         ELSEIF(AMPL.LT.83. )THEN
            IA = 5
         ELSEIF(AMPL.LT.999. )THEN
            IA = 6
         ELSE
            IA = 0
         ENDIF
         
         IF( IA.GT.0 )THEN
         CALL HF1(11010+IZ+IA*100,DXX,1.)
         CALL HFILL(11030+IA*100,Z,A,1.)
         ENDIF
      ENDDO
      
      xtrack = X0FIT + TAN(PHIFIT)*12.
      CALL HFILL(10002,xtrack,dx2,1.)
      IF( z.lt.70. )then
         CALL HFILL(10030,xtrack,dx2,1.)
      else
         CALL HFILL(10035,xtrack,dx2,1.)
      endif
      
      xtrack = X0FIT + TAN(PHIFIT)*6.
      CALL HFILL(10003,xtrack,dx3,1.)
      IF( z.lt.70. )then
         CALL HFILL(10031,xtrack,dx3,1.)
      else
         CALL HFILL(10036,xtrack,dx3,1.)
      endif
      xtrack = X0FIT 
      CALL HFILL(10004,xtrack,dx4,1.)
      IF( z.lt.70. )then
         CALL HFILL(10032,xtrack,dx4,1.)
      else
         CALL HFILL(10037,xtrack,dx4,1.)
      endif
      CALL HF1(10014,xtrack,1.)
* modulo padwidth
      xtrack = mod(xtrack,2.)/2.
      IF( xtrack.lt.0. ) xtrack = xtrack+1.
      CALL HFILL(10020,xtrack,dx4,1.)
      IF( Z.LT.20. )THEN
          IH = 10021
      ELSEIF( Z.LT.30. )THEN
          IH = 10022
      ELSEIF( Z.LT.40. )THEN
          IH = 10023
      ELSEIF( Z.LT.50. )THEN
          IH = 10024
      ELSEIF( Z.LT.60. )THEN
          IH = 10025
      ELSEIF( Z.LT.80. )THEN
          IH = 10026
      ELSEIF( Z.LT.110. )THEN
          IH = 10027
      ELSE
          IH = 10028
      ENDIF
      CALL HFILL(IH,xtrack,dx4,1.)
       
      
      
      xtrack = X0FIT - TAN(PHIFIT)*6.
      CALL HFILL(10005,xtrack,dx5,1.)
      IF( z.lt.70. )then
         CALL HFILL(10033,xtrack,dx5,1.)
      else
         CALL HFILL(10038,xtrack,dx5,1.)
      endif
      xtrack = X0FIT - TAN(PHIFIT)*12.
      CALL HFILL(10006,xtrack,dx6,1.)
      IF( z.lt.70. )then
         CALL HFILL(10034,xtrack,dx6,1.)
      else
         CALL HFILL(10039,xtrack,dx6,1.)
      endif

      END
      FUNCTION DX(DXWITH,DXWOUT)
      DX = DXWITH
      DX = SIGN(SQRT(ABS(DXWITH*DXWOUT)),DXWITH)
      END
      function amplcor(z)
* fit histo 10018 to optain values
* --------------------------------      
*      amplcor = 1.
*      amplcor = gauint(z)

* arco2
*      amplcor = (72.99-0.16259*z )/57.
* with new signal reconstruction
*      amplcor = (57.3-0.11688*z )/57.

* p10
      amplcor = (44.44-0.11825*z )/55.
      END
      function gauint(zin)
      parameter (x=0.,pwidth=2.)
* cm -> mm      
      
      z = zin/10.
      sigma = sqrt(0.2328+0.0594*z-0.00127*z**2)
      
      x1 = (x-pwidth*0.5)/sigma
      x2 = (x+pwidth*0.5)/sigma
      
      gauint = freq(x2)-freq(x1)
 
      end
