;+
; PROJECT: HESSI
; NAME:   hsi_rm_lld
;
;
; PURPOSE:  Spectral response sub-matrix for LLD thresholds AND
;            gain-crossover (rears only)
;
; CATEGORY: HESSI spectral analysis
;
; CALLING SEQUENCE:
;
;      hsi_rm_lld, ph_edges, vird_index, c, rm
;
; INPUTS:
;
;     ph_edges       If a single number, refers to a particular standard
;                     binning (these TBD).  If a string, will be taken
;                     to refer to an input file. Otherwise will be a
;                     (N+1) vector containing boundaries for
;                     N channels of the counts side of the
;                     matrices (in that order). If any of the use_saved[]s
;                     are defined, the binning of that will override
;                     what is supplied.  FOR NOW, THESE SUBMATRIX
;                     GENERATORS ARE ALL SQUARE.
;     vird_index      Vector of virtual detector IDs
;     c               Response matrix control structure; see
;                     hessi_build_srm for descriptions of the tags
;
; OUTPUTS:
;
;       rm    Partial response matrices per input photon/cm2 in cnts/cm2
;
; PROCEDURE:
;
;    Front segments are cut off by the slow LLD.  Rear segments are
;    cut off by the slow or fast LLD primarily, depending on the
;    settings.  Rear segments have a cutoff at high energies due
;    to the gain crossover (for high-gain = standard energy range).
;
; MODIFICATION HISTORY:
;
; DMSmith 25-May-2000   VERSION 0.0 - Includes no dependence on
;    cooler power, threshold setting, resolution, or time.
;
; DMSmith 20-Nov-2000   VERSION 1.0 - Includes a simplified version
;    of the crossover.
; 29-Jan-03 Kim Prepended $ to HSI_SPEC in hsi_loc_file call
;
;-

pro hsi_rm_lld, ph_edges, vird_index, c, rm

hsi_subrm_init, ph_edges, c, 9, vird_index, nbins, rm, diag, finished, nmats

if finished then return

;set up parameters applicable to all segments:

edge_products, ph_edges, mean=emean, width=ewidth, edges_2=ph_edges2
pi = 3.1415927

;Fits from "ref206" for front&rear slow LLD (and "ref231" for front of G9):
;The shape of the cutoff for energy E in keV is 1./(1.+exp(-(x-c)/w)) where
;w = [R/F]SLLD_WI + [R/F]SLLD_WS*T    and
;c = [R/F]SLLD_CI + [R/F]SLLD_CS*T and T is
;the rear/front slow LLD threshold setting.
;The LLD-setting dependence of "w" is problematic (and also depends
;on the resolution/noise), so right now set it to the average value from
;all the lab data and kill the dependence on T.

RSLLD_WI = fltarr(9)
RSLLD_WS = fltarr(9)
RSLLD_CI = fltarr(9)
RSLLD_CS = fltarr(9)
FSLLD_WI = fltarr(9)
FSLLD_WS = fltarr(9)
FSLLD_CI = fltarr(9)
FSLLD_CS = fltarr(9)

fname = hsi_loc_file(PATH='$HSI_SPEC','hessi_lld_params.1')
openr,lun,/get_lun,fname
readf,lun, RSLLD_WI, RSLLD_WS, RSLLD_CI, RSLLD_CS, $
           FSLLD_WI, FSLLD_WS, FSLLD_CI, FSLLD_CS
free_lun,lun

for i=0,nmats-1 do begin

    if (vird_index[i] LT 9) then begin
        det = vird_index[i]
        front_slow_lld = float(c.lld_values[det,0])
        cen = FSLLD_CI[det] + FSLLD_CS[det]*front_slow_lld
        wid = FSLLD_WI[det] + FSLLD_WS[det]*front_slow_lld
    endif else if (vird_index[i] LT 18) then begin
        det = vird_index[i]-9
        rear_slow_lld = float(c.lld_values[det,2])
        cen = RSLLD_CI[det] + RSLLD_CS[det]*rear_slow_lld
        wid = RSLLD_WI[det] + RSLLD_WS[det]*rear_slow_lld
    endif else begin ;COINCIDENT EVENTS ASSUMED TOO HIGH TO WORRY
        cen = 0.     ;THIS IS A ZERO THRESHOLD
        wid = .01
    end

;Avoid calculating the cutoff when it is nearly 0. or 1.:

    where_below = where( (emean-cen)/wid LE -10.,     ctbelow )
    where_calc = where(  abs((emean-cen)/wid) LT 10., ctcalc )
    where_above = where( (emean-cen)/wid GE 10.,      ctabove )
    correction = fltarr(n_elements(emean))
    if (ctbelow GT 0) then correction[where_below] = 0.
    if (ctcalc GT 0) then  correction[where_calc] = $
                               1./(1.+exp(-(emean[where_calc]-cen)/wid))
    if (ctabove GT 0) then correction[where_above] = 1.

    rm[diag+i*nbins^(2-c.isdiag[9])] = correction

end

hsi_save_subrm, c, 9, rm, vird_index

end