Module unp_mod

module unp_mod

        ! Uses
    use precision_mod
    use det_geom_mod, ONLY: max_planes_d, max_wires_d, max_planes_p, max_wires_p,        max_scints, first_adc_plane, last_adc_plane, first_adc_wire, last_adc_wire,       max_planes_t, max_wires_t, max_wires_sc, ndplanes, npplanes, ntplanes
    use tdc_mod
    use chambers_mod
    use namelist_mod
    use tdcmap_mod
    use calibrations_mod
    use hists_mod

        ! Variables
    integer, private, EXTERNAL :: blocat
    integer, private, EXTERNAL :: bdlen
    integer (kind=i4), public, PARAMETER :: dc_mngw = max_wires_d * max_planes_d * 2
    integer (kind=i4), public, PARAMETER :: pc_mngw = max_wires_p * max_planes_p * 2
    integer (kind=i4), public, PARAMETER :: tc_mngw = max_wires_t * max_planes_t * 8
    integer (kind=i4), public, PARAMETER :: sc_mngw = max_scints * 8
    type (tdc_TYPE), public, TARGET, dimension (dc_mngw) :: dctdc
    type (tdc_TYPE), public, TARGET, dimension (pc_mngw) :: pctdc
    type (tdc_SC_type), public, TARGET, dimension (sc_mngw) :: sctdc
    type (tdc_type), public, TARGET, dimension (tc_mngw) :: tctdc
    type (adc_PC_type), public, TARGET, dimension (pc_mngw) :: pcadc
    type (adc_SC_type), public, TARGET, dimension (sc_mngw) :: scadc
    type (tdcraw_TYPE), public, TARGET, dimension (dc_mngw) :: dctdc_raw
    type (tdcraw_TYPE), public, TARGET, dimension (pc_mngw) :: pctdc_raw
    type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: sctdc_raw
    type (tdcraw_TYPE), public, TARGET, dimension (pc_mngw) :: pcadc_raw
    type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: scadc_raw
    type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: PuTdc_Raw
    type (tdcraw_TYPE), private, TARGET, dimension (tc_mngw) :: tctdc_raw
    integer, public :: dc_ihits
    integer, public :: pc_ihits
    integer, public :: sc_ihits
    integer, public :: pcadc_ihits
    integer, public :: scadc_ihits
    integer, public :: tc_ihits
    integer, public :: nerr_hit_dc
    integer, public :: nerr_hit_pc
    integer, public :: nerr_hit_pcadc
    integer, public :: letime_max
    integer, public :: tetime_min
    integer (kind=i4), public, TARGET :: dctdcraw_ihits
    integer (kind=i4), public, TARGET :: pctdcraw_ihits
    integer (kind=i4), public, TARGET :: sctdcraw_ihits
    integer (kind=i4), public, TARGET :: scadcraw_ihits
    integer (kind=i4), public, TARGET :: pcadcraw_ihits
    integer (kind=i4), public, TARGET :: putdcraw_ihits
    integer (kind=i4), private, TARGET :: tctdcraw_ihits
    integer (kind=i4), public, TARGET, dimension (max_planes_d) :: dc_nphits
    integer (kind=i4), public, TARGET, dimension (max_planes_p) :: pc_nphits
    integer (kind=i4), public, TARGET, dimension (max_scints) :: sc_nphits
    integer (kind=i4), private, TARGET, dimension (max_planes_t) :: tc_nphits
    integer (kind=i4), public, TARGET, dimension (max_planes_p) :: pcadc_nphits
    integer (kind=i4), public, TARGET, dimension (max_scints) :: scadc_nphits
    integer (kind=i4), public, TARGET :: dc_nwhits
    integer (kind=i4), public, TARGET :: pc_nwhits
    integer (kind=i4), public, TARGET :: sc_nwhits
    integer (kind=i4), private, TARGET :: tc_nwhits
    integer (kind=i4), public, TARGET :: pcadc_nwhits
    integer (kind=i4), public, TARGET :: scadc_nwhits
    type (whits_TYPE), public, TARGET, dimension (max_planes_d,max_wires_d) :: dcwhits
    type (whits_TYPE), public, TARGET, dimension (max_planes_p,max_wires_p) :: pcwhits
    type (whits_TYPE), public, TARGET, dimension (max_scints,max_wires_sc) :: scwhits
    type (whits_TYPE), public, TARGET, dimension (max_planes_p,max_wires_p) :: pcadc_whits
    type (whits_TYPE), public, TARGET, dimension (max_scints,max_wires_sc) :: scadc_whits
    type (whits_TYPE), private, TARGET, dimension (max_planes_t,max_wires_t) :: tcwhits
    integer (kind=i4), public, dimension (mbank) :: nWordsFBC
    integer (kind=i4), private :: max_crate_tdc
    logical, public, dimension (mbank) :: PulserHit = .FALSE.
    integer (kind=I4), private :: ib
    integer (kind=I4), public, dimension (mbank) :: NpulserHits = 0
    integer (kind=i4), private :: versionno
    integer (kind=i4), public, dimension (mbank) :: first_tdc_slot
    integer (kind=i4), public, dimension (mbank) :: last_tdc_slot
    integer (kind=i4), public, dimension (nslot,mbank) :: nw_slot
    logical, private, dimension (mbank) :: sparsified
    integer (kind=i4), private, dimension (mbank) :: nstarters
    integer (kind=i4), private, dimension (mbank,5) :: startslots
    real (kind=R4), public :: m1adc
    real (kind=R4), public :: m2adc
    real (kind=R4), public :: m12adc
    logical, private, PARAMETER :: FillFastbusMapHist = .TRUE.
    integer, private, PARAMETER :: IDH_RAWTDCFBU1 = IDH_TDCUNP_RAW + 1
    integer, private, PARAMETER :: IDH_RAWTDCFBU2 = IDH_TDCUNP_RAW + 2
    integer, private, PARAMETER :: IDH_RAWTDCFBU3 = IDH_TDCUNP_RAW + 3
    integer, private, PARAMETER :: IDH_RAWPACT13 = IDH_TDCUNP_RAW + 13
    integer, private, PARAMETER :: IDH_RAWPACT23 = IDH_TDCUNP_RAW + 23
    integer, private, PARAMETER :: IDH_RAWTDCFBU1_ERR = IDH_TDCUNP_RAW + 100
    integer, private, PARAMETER :: IDH_RAWTDCFBU2_ERR = IDH_TDCUNP_RAW + 200
    integer, private, PARAMETER :: IDH_EVENT_LENGTH = IDH_TDCUNP_RAW + 301
    integer, private, PARAMETER :: IDH_EVENT_DCTDC = IDH_TDCUNP_RAW + 310
    integer, private, PARAMETER :: IDH_EVENT_PCTDC = IDH_TDCUNP_RAW + 311
    integer, private, PARAMETER :: IDH_EVENT_PCADC = IDH_TDCUNP_RAW + 312
    logical, private, PARAMETER :: debugPrint = .false.

        ! Subroutines and functions
    public function tdcunp (PRINT)
    private function tdcunp_bank (indat, lendat, PRINT)
    public subroutine UnpTDCP (banknum)
    private subroutine DumpFbcmap ()
    public subroutine tdc_listhits ()

end module unp_mod
 Date 1 Dec 00
 Gets data from ybos banks and creates and fills  data structures for
 tdc's and adc's. History records move to unpmc_mod.
 Modified to handle tdc's in common-start mode
 Modified to fill tec data
--------------------------------------------------------------------------- 

Author: Art Olin

Version: April 03


Description of Variables

blocat

integer, private, EXTERNAL :: blocat

bdlen

integer, private, EXTERNAL :: bdlen

dc_mngw

integer (kind=i4), public, PARAMETER :: dc_mngw = max_wires_d * max_planes_d * 2
Each scintillator is hooked up to a TDC which

pc_mngw

integer (kind=i4), public, PARAMETER :: pc_mngw = max_wires_p * max_planes_p * 2
Each scintillator is hooked up to a TDC which

tc_mngw

integer (kind=i4), public, PARAMETER :: tc_mngw = max_wires_t * max_planes_t * 8
Each scintillator is hooked up to a TDC which

sc_mngw

integer (kind=i4), public, PARAMETER :: sc_mngw = max_scints * 8
Each scintillator is hooked up to a TDC which

dctdc

type (tdc_TYPE), public, TARGET, dimension (dc_mngw) :: dctdc
can handle 8 complete signals.

pctdc

type (tdc_TYPE), public, TARGET, dimension (pc_mngw) :: pctdc
can handle 8 complete signals.

sctdc

type (tdc_SC_type), public, TARGET, dimension (sc_mngw) :: sctdc

tctdc

type (tdc_type), public, TARGET, dimension (tc_mngw) :: tctdc

pcadc

type (adc_PC_type), public, TARGET, dimension (pc_mngw) :: pcadc

scadc

type (adc_SC_type), public, TARGET, dimension (sc_mngw) :: scadc

dctdc_raw

type (tdcraw_TYPE), public, TARGET, dimension (dc_mngw) :: dctdc_raw

pctdc_raw

type (tdcraw_TYPE), public, TARGET, dimension (pc_mngw) :: pctdc_raw

sctdc_raw

type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: sctdc_raw

pcadc_raw

type (tdcraw_TYPE), public, TARGET, dimension (pc_mngw) :: pcadc_raw

scadc_raw

type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: scadc_raw

PuTdc_Raw

type (tdcraw_TYPE), public, TARGET, dimension (sc_mngw) :: PuTdc_Raw

tctdc_raw

type (tdcraw_TYPE), private, TARGET, dimension (tc_mngw) :: tctdc_raw

dc_ihits

integer, public :: dc_ihits

pc_ihits

integer, public :: pc_ihits

sc_ihits

integer, public :: sc_ihits

pcadc_ihits

integer, public :: pcadc_ihits

scadc_ihits

integer, public :: scadc_ihits

tc_ihits

integer, public :: tc_ihits

nerr_hit_dc

integer, public :: nerr_hit_dc

nerr_hit_pc

integer, public :: nerr_hit_pc

nerr_hit_pcadc

integer, public :: nerr_hit_pcadc

letime_max

integer, public :: letime_max

tetime_min

integer, public :: tetime_min

dctdcraw_ihits

integer (kind=i4), public, TARGET :: dctdcraw_ihits

pctdcraw_ihits

integer (kind=i4), public, TARGET :: pctdcraw_ihits

sctdcraw_ihits

integer (kind=i4), public, TARGET :: sctdcraw_ihits

scadcraw_ihits

integer (kind=i4), public, TARGET :: scadcraw_ihits

pcadcraw_ihits

integer (kind=i4), public, TARGET :: pcadcraw_ihits

putdcraw_ihits

integer (kind=i4), public, TARGET :: putdcraw_ihits

tctdcraw_ihits

integer (kind=i4), private, TARGET :: tctdcraw_ihits

dc_nphits

integer (kind=i4), public, TARGET, dimension (max_planes_d) :: dc_nphits

pc_nphits

integer (kind=i4), public, TARGET, dimension (max_planes_p) :: pc_nphits

sc_nphits

integer (kind=i4), public, TARGET, dimension (max_scints) :: sc_nphits

tc_nphits

integer (kind=i4), private, TARGET, dimension (max_planes_t) :: tc_nphits

pcadc_nphits

integer (kind=i4), public, TARGET, dimension (max_planes_p) :: pcadc_nphits

scadc_nphits

integer (kind=i4), public, TARGET, dimension (max_scints) :: scadc_nphits

dc_nwhits

integer (kind=i4), public, TARGET :: dc_nwhits

pc_nwhits

integer (kind=i4), public, TARGET :: pc_nwhits

sc_nwhits

integer (kind=i4), public, TARGET :: sc_nwhits

tc_nwhits

integer (kind=i4), private, TARGET :: tc_nwhits

pcadc_nwhits

integer (kind=i4), public, TARGET :: pcadc_nwhits

scadc_nwhits

integer (kind=i4), public, TARGET :: scadc_nwhits

dcwhits

type (whits_TYPE), public, TARGET, dimension (max_planes_d,max_wires_d) :: dcwhits

pcwhits

type (whits_TYPE), public, TARGET, dimension (max_planes_p,max_wires_p) :: pcwhits

scwhits

type (whits_TYPE), public, TARGET, dimension (max_scints,max_wires_sc) :: scwhits

pcadc_whits

type (whits_TYPE), public, TARGET, dimension (max_planes_p,max_wires_p) :: pcadc_whits

scadc_whits

type (whits_TYPE), public, TARGET, dimension (max_scints,max_wires_sc) :: scadc_whits

tcwhits

type (whits_TYPE), private, TARGET, dimension (max_planes_t,max_wires_t) :: tcwhits

nWordsFBC

integer (kind=i4), public, dimension (mbank) :: nWordsFBC
# TDC words in banks per event

max_crate_tdc

integer (kind=i4), private :: max_crate_tdc
# TDC words in banks per event

PulserHit

logical, public, dimension (mbank) :: PulserHit = .FALSE.

ib

integer (kind=I4), private :: ib

NpulserHits

integer (kind=I4), public, dimension (mbank) :: NpulserHits = 0

versionno

integer (kind=i4), private :: versionno

first_tdc_slot

integer (kind=i4), public, dimension (mbank) :: first_tdc_slot

last_tdc_slot

integer (kind=i4), public, dimension (mbank) :: last_tdc_slot

nw_slot

integer (kind=i4), public, dimension (nslot,mbank) :: nw_slot

sparsified

logical, private, dimension (mbank) :: sparsified

nstarters

integer (kind=i4), private, dimension (mbank) :: nstarters

startslots

integer (kind=i4), private, dimension (mbank,5) :: startslots

m1adc

real (kind=R4), public :: m1adc

m2adc

real (kind=R4), public :: m2adc

m12adc

real (kind=R4), public :: m12adc

FillFastbusMapHist

logical, private, PARAMETER :: FillFastbusMapHist = .TRUE.

IDH_RAWTDCFBU1

integer, private, PARAMETER :: IDH_RAWTDCFBU1 = IDH_TDCUNP_RAW + 1

IDH_RAWTDCFBU2

integer, private, PARAMETER :: IDH_RAWTDCFBU2 = IDH_TDCUNP_RAW + 2

IDH_RAWTDCFBU3

integer, private, PARAMETER :: IDH_RAWTDCFBU3 = IDH_TDCUNP_RAW + 3

IDH_RAWPACT13

integer, private, PARAMETER :: IDH_RAWPACT13 = IDH_TDCUNP_RAW + 13

IDH_RAWPACT23

integer, private, PARAMETER :: IDH_RAWPACT23 = IDH_TDCUNP_RAW + 23

IDH_RAWTDCFBU1_ERR

integer, private, PARAMETER :: IDH_RAWTDCFBU1_ERR = IDH_TDCUNP_RAW + 100

IDH_RAWTDCFBU2_ERR

integer, private, PARAMETER :: IDH_RAWTDCFBU2_ERR = IDH_TDCUNP_RAW + 200

IDH_EVENT_LENGTH

integer, private, PARAMETER :: IDH_EVENT_LENGTH = IDH_TDCUNP_RAW + 301

IDH_EVENT_DCTDC

integer, private, PARAMETER :: IDH_EVENT_DCTDC = IDH_TDCUNP_RAW + 310

IDH_EVENT_PCTDC

integer, private, PARAMETER :: IDH_EVENT_PCTDC = IDH_TDCUNP_RAW + 311

IDH_EVENT_PCADC

integer, private, PARAMETER :: IDH_EVENT_PCADC = IDH_TDCUNP_RAW + 312

debugPrint

logical, private, PARAMETER :: debugPrint = .false.
 set this to TRUE to get "iflag" debug info

Description of Subroutines and Functions

tdcunp

public function tdcunp (PRINT)
    logical :: PRINT
            $     Local variables
    integer (kind=I4) :: tdcunp
    ! Calls: hbook1, hbook2, hf1, kerror, kerror2
end function tdcunp
$---------------------------------------------------------------------
$     Decode TDC  DATA from 1877 TDC
$---------------------------------------------------------------------
$     Input:
$       PRINT-LOG*4  - PRINT flag (TRUE = PRINT on, FALSE = PRINT off)
$     RETURN Status code :
$                      0 = no problems
$                      1 = fatal problem, no DATA available 
$                      2 = DATA FORMAT error, some DATA might be available
$                      3 = DATA size too large, DATA was truncated
$---------------------------------------------------------------------
$  Modified and expanded : 23-oct-1998 Renee Poutissou
$---------------------------------------------------------------------

tdcunp_bank

private function tdcunp_bank (indat, lendat, PRINT)
    integer (kind=I4), INTENT(IN) :: indat
    integer (kind=I4), INTENT(IN) :: lendat
    logical :: PRINT
    integer (kind=I4) :: tdcunp_bank
    ! Calls: hf2, init_tdc_loop, kerror, kerror2
end function tdcunp_bank
---------------------------------------------------------------------  
     Decode TDC  data from any one bank of 1877 TDC data               
---------------------------------------------------------------------  
     Input:                                                            
       indat  - INT*4                                                  
       lendat - INT*4                                                  
       print  -LOG*4  - Print flag (TRUE = print on, FALSE = print off)
     Output :                                                          
       tdcunp_bank  - INT*4 - Return Status code                       
                      0 = no problems                                  
                      1 = fatal problem, no data available             
                      2 = data format error, some data might be availab
                      3 = data size too large, data was truncated
                      4 = strange triggerend gate time
                      5 = no trigger endgate
                     10 = tdc header missing      
---------------------------------------------------------------------  
    1877 and 1877S  TDC data format                                               
                                                                       
 The TDC records edges and has a bit to identify if it is a rising edge or a falling edge. 
 These edges are referred to as TE (trailing edge) and LE (leading edge)

     |                                                                 
     |                                                                 
     |       ________             _________                            
     |      |        |           |         |                           
     |______|        |___________|         |_____________|             
  time goes --------->   earlier time reads larger value               
                                                                       
     1                                                                 
     0      9        8           5         3             0             
     0      0        0           0         0             0             
           LE       TE          LE        TE           Stop            
                                                           
  readout goes the opposite way  <-----------------------
                
 The latest TE value always comes first in the data buffer                    
 The Te has the bit 0x10000 turned on                                  
===========================================================================
Present tdc error functionality:
 iflag > 0 is set for error conditions missing a leading or trailing edge
 which indicates questionable data but  occurs legitimately -
 when a leading or trailing edge occurs outside the tdc range or the difference
 between two pulses is less than the required 10 ns.
 iflag < 0 events are less usual and more likely to indicate tdc
 malfunctions.
 All iflag \=0 hits  are presently eliminated from the regular data structures
 and therefore not analyzed. (We should discuss this.) They are stored in
 the raw tdc structures. 
 An occurance of an iflag < 0 hit is sufficient to kill an event.
 At present, monitoring these tdc errors with qod will require raw_out >0
 instead of raw_out = 0
 As explained above the sequence of edges in a TDC should be TE LE TE LE TE LE .... 
 There should always be a pair of TE LE with the same channel ID 
 iflag kerr do_record description:
    1         Yes     a trailing edge following another trailing edge in same channel
    2         Yes     a trailing edge following another trailing edge from different channel
    3         Yes     Last hit in TDC, but it is a trailing edge. 
    4         Yes     this is the first hit, but it is a leading edge. 
    5         Yes     this is a leading edge following another leading edge.
    6         Yes     this is a leading edge following a trailing edge from a different channel.
                      This error always generates a pair of hits.
    7         Yes     if iwid < 0, probably indicates previous pulse was missing LE and this pulse was
                      missing TE. Impossible to spot this condition. Set w = 0
    8   Yes    No     no valid mapping for this channel           
    9   Yes   Yes     no valid mapping for this channel and last data word was a 
                      trailing edge from a different channel, Register the previous trailing edge
   10   Yes    No     tdc channel number out of range 0..95
 The following only apply to PACT (common START channels) 
   11         Yes     its not the first te on this channel therefore not what we expect for sparse mode
   14         Yes     a trailing edge following another trailing edge,  NOT what we expect in start mode
   17         Yes     this is the first hit, but it is a leading edge.
   18         Yes     a leading edge following another leading edge.
   19         Yes     this is a leading edge following a trailing edge from a different channel.
   20         Yes     if iwid < 0, probably indicates previous pulse was missing LE and this pulse was
                      missing TE.  Impossible to spot this condition if iwid >= 0.
   
=========================================================================

UnpTDCP

public subroutine UnpTDCP (banknum)
    integer (kind=I4), INTENT(OUT) :: banknum
    ! Calls: kerror, rite
end subroutine UnpTDCP
=======================================================================
 Unpacks tdc header data from begin run records.
 These headers are used to check that all tdc's are actually unpacked.
 common-start-mode TDC's are now specified in the header file 
=======================================================================

Author: Art Olin

Version: Nov 2001

DumpFbcmap

private subroutine DumpFbcmap ()
end subroutine DumpFbcmap

tdc_listhits

public subroutine tdc_listhits ()
    ! Calls: rite
end subroutine tdc_listhits
========================================================          
     Date: October 1998
     Revised for new data structures:  Art Olin 1999 
     --------------------------------------------------------          
     Description:                                                      
                                                                       
     Print out all TDC hit information if raw_out = 2. For raw_out =1
     the raw tdc structure is filled but not printed 

     April 25, 2001 - Roy Wilds - Altered printout format.
                      Now accomodates addtional TDC and ADC structures
     ========================================================          

Author: Renee Poutisou