SUBROUTINE FTDEOR
*-- Author :    John V. Morris   23/06/93
      SUBROUTINE FTDEOR(JRUN,IERR)
                                                                        
                                                                        
      COMMON/FHLUN/ LUNH, LUNS, LMES, IL4L5                             
      COMMON/FCOUNT/ NDATE0,NTIME0,ISTATP(20),ISTATR(20),IFRHV,IFPHV,   
     &               NEVENT,NFIEL0,NPRES0,NECUR0,NPCUR0,IRTE0,IRTP0,    
     &               IEVIN,NFPEVT,NHITSP,NFREVT,NHITSR,NMIN92,          
     &               TOTL,H1L,RTIME,REFF,ILRET                          
      LOGICAL PLANAR,RADIAL                                             
      COMMON /FTQRUN/  NRUN,NRUN0,NRUN1,MAXEV,PLANAR,RADIAL             
                                                                        
      CHARACTER*8  NAMES(19),NAMEB(24),NAMEL(14),NAMEH(9),NAMEC(9)      
      CHARACTER*8  NAMEV(18),NAMET(18),NAMEP(26),NAMEE(39)              
      CHARACTER*8  NAMIT(4)                                             
      CHARACTER*8  FNAME1(6),FNAMET(13),FNAMEE(6)                       
      CHARACTER*8  FNAMEA(8),FNAMEP(8),FNAMER(13),FNAMEB(5),FNAMEC(10)  
      CHARACTER*8  NAMES1(8),NAMES2(8),NAMES3(8),NAMES4(8)              
      DIMENSION AVEC(14),IOUT(6),FOUT(17)                               
                                                                        
      LOGICAL LEXIST                                                    
      CHARACTER*8 ARNAME                                                
      LOGICAL FIRST /.TRUE./                                            
      DATA NAMIT/'Run_numb','CompCODE','RadialHV','PlanarHV'/           
      DATA NAMEL/'Run_numb','Date    ','Time    ','Min92   ','Pressure',
     +           'Totevs  ','Run_lumi','H1R_lumi','Run_time','Run_eff ',
     +           'ILRET   ','e_curr  ','p_curr  ','Bfield  '/           
      DATA NAMES/'Events  ','Htspev  ','Clupev  ','Cpev_err','PERHts  ',
     +           'PHt_err ','V2pk    ','V2pk_err','Vdt     ','Vdt_err ',
     +           'Qmph    ','Qmph_err','Resol   ','Reso_err','Tzero   ',
     +           'Tzer_err','Weff    ','Weff_err','HVflag  '/           
      DATA NAMEH/'HitspL0 ','HitspL1 ','Hitspl2 ','HitspL3 ','HitspL4 ',
     +           'HitspL5 ','HitspL6 ','Hitspl7 ','HitspL8 '/           
      DATA NAMEC/'CluspL0 ','CluspL1 ','CluspL2 ','Cluspl3 ','CluspL4 ',
     +           'CluspL5 ','CluspL6 ','CluspL7 ','Cluspl8 '/           
      DATA NAMET/'V2pkL0  ','V2pkL1  ','V2pkL2  ','V2pkL3  ','V2pkL4  ',
     +           'V2pkL5  ','V2pkL6  ','V2pkL7  ','V2pkL8  ',           
     +           'V2L0_err','V2L1_err','V2L2_err','V2L3_err','V2L4_err',
     +           'V2L5_err','V2L6_err','V2L7_err','V2L8_err'/           
      DATA NAMEV/'V2p0    ','V2p0_err','V2p1    ','V2p1_err','V2p2    ',
     +           'V2p2_err','Res0    ','Res0_err','Res1    ','Res1_err',
     +           'Res2    ','Res2_err','Qmt0    ','Qmt0_err','Qmt1    ',
     +           'Qmt1_err','Qmt2    ','Qmt2_err'/                      
      DATA NAMEB/'Weff_0  ','Qmph_0  ','Weff_1  ','Qmph_1  ','Weff_2  ',
     +           'Qmph_2  ','Weff_3  ','Qmph_3  ','Weff_4  ','Qmph_4  ',
     +           'Weff_5  ','Qmph_5  ','Weff_6  ','Qmph_6  ','Weff_7  ',
     +           'Qmph_7  ','Weff_8  ','Qmph_8  ','Weff_9  ','Qmph_9  ',
     +           'Weff_10 ','Qmph_10 ','Weff_11 ','Qmph_11 '/           
      DATA NAMEP/'Vdtall  ','VdtM0   ','VdtM1   ','VdtM2   ',           
     +           'VdtL0   ','VdtL1   ','VdtL2   ','VdtL3   ','VdtL4   ',
     +           'VdtL5   ','VdtL6   ','VdtL7   ','VdtL8   ',           
     +           'Vdal_err','VdM0_err','VdM1_err','VdM2_err',           
     +           'VdL0_err','VdL1_err','VdL2_err','VdL3_err','VdL4_err',
     +           'VdL5_err','VdL6_err','VdL7_err','VdL8_err'/           
      DATA NAMEE/'Winall  ','WinM0   ','WinM1   ','Winm2   ','WinL0   ',
     +           'WinL1   ','WinL2   ','Winl3   ','Winl4   ','Winl5   ',
     +           'WinL6   ','Winl7   ','WinL8   ',                      
     +           'Winall_n','WinM0_n ','WinM1_n ','Winm2_n ','WinL0_n ',
     +           'WinL1_n ','WinL2_n ','Winl3_n ','Winl4_n ','Winl5_n ',
     +           'WinL6_n ','Winl7_n ','WinL8_n ',                      
     +           'Winall_f','WinM0_f ','WinM1_f ','Winm2_f ','WinL0_f ',
     +           'WinL1_f ','WinL2_f ','Winl3_f ','Winl4_f ','Winl5_f ',
     +           'WinL6_f ','Winl7_f ','WinL8_f '/                      
      DATA FNAME1/'Run_numb','Trks_evt','Phit_trk','Rhit_trk',          
     +            'Phit_use','Rhit_use'/                                
      DATA FNAMEA/'Npeak   ','Peak_pos','Sigma   ','Integral',          
     +            'Peak_hgt','bkg_sig ','Mean    ','RMSSigma' /         
      DATA FNAMET/'Npeak   ','Peakpos1','Sigma1  ','Integrl1',          
     +            'Peakhgt1','bkgsig1 ','Peakpos2','Sigma2  ',          
     +            'Integrl2','Peakhgt2','bkgsig2 ','Mean    ',          
     +            'RMSSigma'/                                           
      DATA FNAMEE/'CheffP0 ','CheffR0 ','CheffP1 ','CheffR1 ',          
     +            'CheffP2 ','CheffR2 '/                                
      DATA FNAMEP/'Tot_seg ','Hit_seg0',                                
     +            'Hit_seg1','Hit_seg2','9ht_seg ','10ht_seg',          
     +            '11ht_seg','12ht_seg'/                                
      DATA FNAMER/'Tot_seg ','Hit_seg0',                                
     +            'Hit_seg1','Hit_seg2','4ht_seg ','5ht_seg ',          
     +            '6ht_seg ','7ht_seg ','8ht_seg ','9ht_seg ',          
     +            '10ht_seg','11ht_seg','12ht_seg'/                     
      DATA FNAMEB/'Hits_seg','9ht_seg ','10ht_seg','11ht_seg',          
     +            '12ht_seg'/                                           
      DATA FNAMEC/'Hits_seg','4ht_seg ','5ht_seg ','6ht_seg ',          
     +            '7ht_seg ','8ht_seg ','9ht_seg ','10ht_seg',          
     +            '11ht_seg','12ht_seg'/                                
                                                                        
      DATA NAMES1/                                                      
     +    'Run_numb','Events  ',                                        
     +    'Vmean   ','dVmean  ',                                        
     +    'Vf0r8   ','dVf0r8  ',                                        
     +    'intercep','dinterc '/                                        
      DATA NAMES2/                                                      
     +    'Vplus   ','dVplus  ',                                        
     +    'Splus   ','dSplus  ',                                        
     +    'Intplus ','dIplus  ',                                        
     +    'Nopplus ','Chiplus '/                                        
      DATA NAMES3/                                                      
     +    'Vminus  ','dVminus ',                                        
     +    'Sminus  ','dSminus ',                                        
     +    'Intminus','dIminus',                                         
     +    'Nopminus','Chiminus'/                                        
      DATA NAMES4/                                                      
     +    'TanLor  ','dTanLor ',                                        
     +    'IntLor  ','dIntLor ',                                        
     +    'AngLor  ','dAngLor',                                         
     +    'NopLor  ','ChLor   '/                                        
      NRUN0  = JRUN                                                     
      IF(FIRST) THEN                                                    
        FIRST = .FALSE.                                                 
        CALL H1ENVI(IFLAG,IPROC,NPROC)                                                                 
        IL4L5 = IFLAG                                                   
        IF(IL4L5.NE.1 .AND. IL4L5.NE.2) THEN                            
          WRITE(6,*) ' ** WARNING **  FTDEOR called                     
     & but H1ENVI flag wrong',IL4L5                                     
          RETURN                                                        
        ENDIF                                                           
                                                                        
      ELSE                                                              
        CALL HDELET(0)                                                                                 
      ENDIF                                                             
      IERR = 0                                                          
      CALL FTQINT
                                                                        
      CALL FBOOKQ
                                                                        
      IF(IL4L5.EQ.1) CALL SAREA('L4',NRUN0)                                                            
      IF(IL4L5.EQ.2) CALL SAREA('FTDSGI',0)                                                            
      CALL FLK2HB(NHIST,IER1)
                                                                        
      CALL SAREA('FTDMON',0)                                                                           
                                                                        
      IF(.NOT.LEXIST('VEC',2,0)) THEN                                   
        CALL BNT( 2, 0,     4, NAMIT)                                                                  
      ENDIF                                                             
      IF(.NOT.LEXIST('VEC',1,0)) THEN                                   
        CALL BNT( 1, 0,    14, NAMEL)                                                                  
        CALL BNT( 7, 0,     8,FNAMEA)                                                                  
        CALL BNT( 8, 0,     8,FNAMEA)                                                                  
        CALL BNT( 9, 0,    26, NAMEP)                                                                  
        CALL BNT(10, 0,    39, NAMEE)                                                                  
        CALL BNT(11, 0,    19, NAMES)                                                                  
        CALL BNT(12, 0,     8, NAMEB)                                                                  
        CALL BNT(13, 0,    18, NAMEV)                                                                  
        CALL BNT(14, 0,     9, NAMEH)                                                                  
        CALL BNT(15, 0,     9, NAMEC)                                                                  
        CALL BNT(16, 0,    18, NAMET)                                                                  
        CALL BHD  (17,0,32,-0.5,31.5,36,0.5,36.5)                                                      
        CALL SHD  (17,0,40.,40.)                                                                       
        CALL STEXT(17 ,4,'FT Planar')                                                                  
        CALL STEXT(17 ,1,'Dead Cell No vs Wire Plane')                                                 
        CALL BHDW (18,0,32,-0.5,31.5,36,0.5,36.5)                                                      
        CALL SHDW (18,0,40.,40.,1.)                                                                    
        CALL STEXT(18 ,4,'FT Planar')                                                                  
        CALL STEXT(18 ,1,'Cell No vs Wire Plane')                                                      
        CALL BHS  (19,0,101,0.0,1.01)                                                                  
        CALL SHS  (19,0,-1.)                                                                           
        CALL STEXT(19 ,4,'FT Planar')                                                                  
        CALL STEXT(19 ,1,'Relative Efficiency')                                                        
                                                                        
        CALL BNT(20, 0,    39, NAMEE)                                                                  
        CALL BNT(21, 0,    19, NAMES)                                                                  
        CALL BNT(22, 0,    24, NAMEB)                                                                  
        CALL BNT(23, 0,    18, NAMEV)                                                                  
        CALL BNT(24, 0,     9, NAMEH)                                                                  
        CALL BNT(25, 0,     9, NAMEC)                                                                  
        CALL BNT(26, 0,    18, NAMET)                                                                  
        CALL BHD  (27,0,48,-0.5,47.5,36,0.5,36.5)                                                      
        CALL SHD  (27,0,40.,40.)                                                                       
        CALL STEXT(27 ,4,'FT Radial')                                                                  
        CALL STEXT(27 ,1,'Dead Wedge Pair No vs Wire Plane')                                           
        CALL BHDW (28,0,48,-0.5,47.5,36,0.5,36.5)                                                      
        CALL SHDW (28,0,40.,40.,1.)                                                                    
        CALL STEXT(28 ,4,'FT Radial')                                                                  
        CALL STEXT(28 ,1,'Wedge Pair No vs Wire Plane')                                                
        CALL BHS  (29,0,101,0.0,1.01)                                                                  
        CALL SHS  (29,0,-1.)                                                                           
        CALL STEXT(29 ,4,'FT Radial')                                                                  
        CALL STEXT(29 ,1,'Relative Efficiency')                                                        
                                                                        
        IF(IL4L5.EQ.2) THEN                                             
                                                                        
          CALL BHDW (40,0,32,0.0,32.0,36,0.0,36.0)                                                     
          CALL SHDW (40,0,40.,40.,1.)                                                                  
          CALL STEXT(40 ,4,'FT Planar')                                                                
          CALL STEXT(40 ,1,'No. of hits        vs Wire Plane')                                         
          CALL BHDW (41,0,32,0.0,32.0,36,0.0,36.0)                                                     
          CALL SHDW (41,0,40.,40.,1.)                                                                  
          CALL STEXT(41 ,4,'FT Planar')                                                                
          CALL STEXT(41 ,1,'No. of used hits   vs Wire Plane')                                         
          CALL BHDW (42,0,32,0.0,32.0,36,0.0,36.0)                                                     
          CALL SHDW (42,0,40.,40.,1.)                                                                  
          CALL STEXT(42 ,4,'FT Planar')                                                                
          CALL STEXT(42 ,1,'No. of unused hits vs Wire Plane')                                         
                                                                        
          CALL BHDW (50,0,24,0.0,24.0,36,0.0,36.0)                                                     
          CALL SHDW (50,0,40.,40.,1.)                                                                  
          CALL STEXT(50 ,4,'FT Radial')                                                                
          CALL STEXT(50 ,1,'No. of hits        vs Wire Plane')                                         
          CALL BHDW (51,0,24,0.0,24.0,36,0.0,36.0)                                                     
          CALL SHDW (51,0,40.,40.,1.)                                                                  
          CALL STEXT(51 ,4,'FT Radial')                                                                
          CALL STEXT(51 ,1,'No. of used hits   vs Wire Plane')                                         
          CALL BHDW (52,0,24,0.0,24.0,36,0.0,36.0)                                                     
          CALL SHDW (52,0,40.,40.,1.)                                                                  
          CALL STEXT(52 ,4,'FT Radial')                                                                
          CALL STEXT(52 ,1,'No. of unused hits vs Wire Plane')                                         
                                                                        
        ENDIF                                                           
      ENDIF                                                             
                                                                        
      IF(IL4L5.EQ.2) THEN                                               
        CALL SAREA('FTRMON',0)                                                                         
        IF(.NOT.LEXIST('VEC',1,0)) THEN                                 
          CALL BNT( 1, 0,     6,FNAME1)                                                                
          CALL BNT( 4, 0,     8,FNAMEA)                                                                
          CALL BNT( 5, 0,     8,FNAMEA)                                                                
          CALL BNT( 6, 0,     8,FNAMEA)                                                                
          CALL BNT(75, 0,     8,FNAMEA)                                                                
          CALL BNT(76, 0,     8,FNAMEA)                                                                
          CALL BNT(79, 0,     8,FNAMEA)                                                                
          CALL BNT(81, 0,     8,FNAMEA)                                                                
          CALL BNT(80, 0,    13,FNAMET)                                                                
          CALL BNT(82, 0,    13,FNAMET)                                                                
                                                                        
          CALL BNT(87, 0,     8,FNAMEA)                                                                
          CALL BNT(88, 0,     8,FNAMEA)                                                                
          CALL BNT(89, 0,     8,FNAMEA)                                                                
          CALL BNT(90, 0,     8,FNAMEA)                                                                
                                                                        
          CALL BNT(19, 0,     6,FNAMEE)                                                                
          CALL BNT(100,0,     8,FNAMEP)                                                                
          CALL BNT(101,0,    13,FNAMER)                                                                
          CALL BNT(200,0,    10,FNAMEC)                                                                
          CALL BNT(201,0,    10,FNAMEC)                                                                
          CALL BNT(202,0,    10,FNAMEC)                                                                
          CALL BNT(203,0,    10,FNAMEC)                                                                
          CALL BNT(320,0,     5,FNAMEB)                                                                
          CALL BNT(321,0,     5,FNAMEB)                                                                
          CALL BNT(322,0,     5,FNAMEB)                                                                
          CALL BNT(323,0,     5,FNAMEB)                                                                
          CALL BNT(2016, 0,     8,FNAMEA)                                                              
          CALL BNT(2017, 0,     8,FNAMEA)                                                              
          CALL BNT(907, 0,     8,FNAMEA)                                                               
          CALL BNT(908, 0,     8,FNAMEA)                                                               
          CALL BNT(650, 0,     8,FNAMEA)                                                               
          CALL BNT(651, 0,     8,FNAMEA)                                                               
          CALL BNT(652, 0,     8,FNAMEA)                                                               
          CALL BNT(653, 0,     8,FNAMEA)                                                               
          CALL BNT(654, 0,     8,FNAMEA)                                                               
          CALL BNT(655, 0,     8,FNAMEA)                                                               
          CALL BNT(656, 0,     8,FNAMEA)                                                               
          CALL BNT(657, 0,     8,FNAMEA)                                                               
        ENDIF                                                           
        CALL SAREA('FPOKER',0)                                                                         
        IF(.NOT.LEXIST('VEC',1,0)) THEN                                 
          CALL BNT(1, 0, 8, NAMES1)                                                                    
          CALL BNT(2, 0, 8, NAMES2)                                                                    
          CALL BNT(3, 0, 8, NAMES3)                                                                    
          CALL BNT(4, 0, 8, NAMES4)                                                                    
        ENDIF                                                           
      ENDIF                                                             
                                                                        
      CALL SAREA('FTDMON',0)                                                                           
                                                                        
      IF( IER1.NE.0 )THEN   ! no histograms copied so abort             
         IERR = IER1                                                    
         CALL FMOUT(NRUN0,IERR)
         GOTO 9000                                                      
      ELSE IF( NHIST.EQ.0 )THEN                                         
         IERR = -10                                                     
         CALL FMOUT(NRUN0,IERR)
         GOTO 9000                                                      
      ENDIF                                                             
                                                                        
      AVEC(1) = FLOAT(NRUN0)                                            
      AVEC(2) = FLOAT(NDATE0)                                           
      AVEC(3) = FLOAT(NTIME0)                                           
      AVEC(4) = FLOAT(NMIN92)                                           
      AVEC(5) = FLOAT(NPRES0)                                           
      AVEC(6) = FLOAT(IEVIN)                                            
      AVEC(7) = TOTL                                                    
      AVEC(8) = H1L                                                     
      AVEC(9) = RTIME                                                   
      AVEC(10) = REFF                                                   
      AVEC(11) = FLOAT(ILRET)                                           
      AVEC(12) = -1.                                                    
      AVEC(13) = -1.                                                    
      IF(IRTE0.EQ.0) AVEC(12) = FLOAT(NECUR0)                           
      IF(IRTP0.EQ.0) AVEC(13) = FLOAT(NPCUR0)                           
      AVEC(14) = FLOAT(NFIEL0)                                          
      CALL SVEC(1,0,AVEC)                                                                              
                                                                        
      CALL FQENDP
      CALL FQENDR
      IF(ISTATP(1).GE.1000) CALL FDEADP
      IF(ISTATR(1).GE.1000) CALL FDEADR
                                                                        
      IF(IL4L5.EQ.1) CALL SAREA('L4',NRUN0)                                                            
      IF(IL4L5.EQ.2) CALL SAREA('FTDSGI',0)                                                            
      CALL FMEANT
      IERR = 0                                                          
      CALL FMOUT(NRUN0,IERR)
                                                                        
 9000 CONTINUE                                                          
                                                                        
      IF(IL4L5.EQ.2) THEN                                               
        CALL FHTMAP
        CALL FTRMON(NRUN0)
        CALL FPOKEM(NRUN0)
      ENDIF                                                             
                                                                        
      CALL SAREA('FTDMON',0)                                                                           
                                                                        
      RETURN                                                            
      END