;+
;
;  Name:
;       BATSE_DEADTM
;
;
; PURPOSE: compute the livetime fraction as a function of count rate for
;	the BATSE PHA and discriminator data for a single detector.
;
; CATEGORY: Data Reduction
;       BATSE
;
; CALLING SEQUENCE: live_fraction = $
;	batse_deadtm(counts, channel_edges=channel_edges, delta_t=delta_t, $
;			pha_dead=pha_dead, amp_dead=amp_dead, $
;			high_rate=high_rate, gate=gate)
;
; INPUTS:
;       Counts - Counts in each of N energy channels, (n x m), m samples
;
; OPTIONAL INPUTS:
;	If the data type is not LAD CONT 16 channel or if the linear
;	edges are not the standard ones after 31-May-1991 then the
;	detector/type must be characterized by the optional inputs:
;	Channel_edges - Linear channel edges for k channels, (2 x k)
;	Delta_t - Accumulation interval in seconds
;	Pha_dead - Dead time in seconds for each linear PHA channel
;	Amp_dead - Dead time in seconds for each event.
;	high_rate - indices where high_rate branch is used in
;		    relationship of observed counts vs true rate
;	gate    - gate recovery time, nominally .68e-6 sec
; Output:
;	Function returns the live-time fraction, between 0 and 1.
;
; PROCEDURE: Assumes COUNTS vector is from CONT data type from LAD.  Dead time
;	is computed by adding number of counts_channel* dead_time/linear_channel
;	+ dead_time/event.  Other data types can be used if the linear 
;	channel edges, integration duration, and pha_deadtime units are
;	provided.
;
;
;*******************************************************************************
;       The deadtime equation is composed of two terms, one paralyzable and
;	one not.  The paralyzable term comes from the analog gate relaxation time
;	of about .68 microseconds, and the non-paralyzable term comes from the
;	signal level processing time:
;		N = R *(1.- N* DPT) * exp(-R tau)
;      		N is the measure rate in counts/s
;		R is the true rate in counts/s
;		DPT is the average event signal processing time
;		tau is the gate relaxation time
;	The livetime fraction is defined as N/R.
;*******************************************************************************
;
; MODIFICATION HISTORY:
;	4 Dec 1993 - Improved deadtime calculation, two terms
;-



function batse_deadtm, counts, channel_edges=channel_edges, delta_t=delta_t, $
	pha_dead=pha_dead, amp_dead=amp_dead, high_rate=high_rate,gate=gate

checkvar, delta_t, 2.048 ;assume CONT data type

checkvar, channel_edges, $  
	transpose( reform( $
	[5,7,9,11,14,18,23,28,36,49,65,85,115,143,207,335, $
	 7,9,11,14,18,23,28,36,49,65,85,115,143,207,335,725], 16,2))

checkvar, pha_dead, 0.0625e-6
checkvar, amp_dead, 3.75e-6
if n_elements(gate) ge 1 then paralyzable = gate(0) else $
	paralyzable = 0.68e-6 ;paralyzable deadtime for BATSE LAD and SPEC

;compute the average dead time in seconds for an event in each channel
dead_channel = (channel_edges(0,*) + channel_edges(1,*))/2 * pha_dead + amp_dead

;some over the counts in all of the channels
count_sec = f_div( total(counts,1) , delta_t )
count_sec_prm = f_div( count_sec , 1.- f_div(dead_channel # counts,delta_t) ) 
;Get the paralyzable term in the live time equation

;Don't implement high-rate branch for normal processing
;answer = f_div (para_livetime( count_sec_prm, paralyzable, $
;		high_rate=high_rate)*count_sec, count_sec_prm)
answer = f_div (para_livetime( count_sec_prm, paralyzable	)*count_sec, count_sec_prm)


return, answer
end

