B
    í¼`$e  ã               @   sz  d Z ddlmZ ddlmZ ddlZddlZddlZdej dfdd	„Z	d
ej
 ej dej dfdd„Ze e¡Zdd„ Zdd„ ZG dd„ deƒZdd„ Zd@dd„Zdd„ ZdAdd„Zdd„ Zdd„ Zd d!„ ZdBd"d#„ZG d$d%„ d%eƒZG d&d'„ d'ƒZd(ej fd)d*„Zd+d,„ Zd-d(ej d.d/fd0d1„Zd2d3„ Zd4d5„ Z d6ej! d7ej" ej fd8d9„Z#dCd;d<„Z$d=ej% fd>d?„Z&dS )Dz!
Functions related to responses.
é    )Úunitsé   )ÚschemaNg       @Úcoldc             C   sö  ddt j f}| |d ks&| |d kr*dS | t j } |t j }ddlm}m}m} d}	|dkr¦| | }
d|
 d|
  }d|
 d|
  }d	|d
| ƒ |d
| ƒ }	�nDd|d|  | ƒ d|d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  d|d|  | ƒ |d|  | ƒ |d|  | ƒ  }	|	|9 }	|	S )zí
    This version takes gain parameter already scaled such that the
    gain actually desired is obtained.
    Both the "cold" and "warm" electroinics reponse functions are adapted from
    wire-cell-toolkit/util/src/Response.cxx.  
    r   é
   r   g        )ÚsinÚcosÚexpZwarmgÍÌÌÌÌÌì?g      à?g      ð?g      à¿g<f 2þ=@g/Q½5°•ÀggÕçj+ö@g�]¢zk ÀgbÛ¢Ìó?gbÛ¢Ì@gBÈ—PÁÝ?gÒ5“o¶9ÀgçŒ(í¾@g>?Œ¾@g~r 
fè?g¦	ÛOÆøÔ?)r   ÚusÚmathr   r   r	   )ÚtimeÚgainÚshapingÚ	elec_typeÚdomainÚstr   r   r	   ÚretZreltimeZleftArgZrightArg© r   ú:/wcdo/src/wct/python/wirecell/sigproc/response/__init__.pyÚelectronics_no_gain_scale   s"    

 ÿ ÿ Fr   é   c             C   sj   |}|dkr\|dt j kr$|d }n8|dt j kr<|d }n |dt j krT|d }n|d }t| |||ƒS )	zò
    Electronics response function.

        - gain :: the peak gain value in [voltage]/[charge]

        - shaping :: the shaping time in Wire Cell system of units

        - domain :: outside this pair, the response is identically zero
    r   g      à?g®gÇ,K$@g      ð?g0F$
-K$@g       @gìÂÎ§>$@güTˆ=$@)r   r
   r   )r   Z	peak_gainr   r   r   r   r   r   Úelectronics4   s    


r   c             C   s2   t j | ¡}t j |¡}t j || ¡}t  |¡S )zW
    Return the simple convolution of the two arrays using FFT+mult+invFFT method.
    )ÚnumpyÚfftÚifftÚreal)Úf1Úf2Ús1Ús2Úsigr   r   r   ÚconvolveM   s    r!   c             C   s   ddl m} || |dƒS )zW
    Return the simple convolution of the two arrays using FFT+mult+invFFT method.
    r   )ÚfftconvolveZsame)Zscipy.signalr"   )r   r   r"   r   r   r   Ú	_convolveZ   s    r#   c               @   sv   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
dej ej dej ddfdd„Zdd„ ZdS )ÚResponseFunctionz­
    A response function object holds the response wave function and metadata.

    Note: time is assumed to be in Wire Cell system of units (ns).  This is NOT seconds.
    Nc             C   sN   |  ¡ }|dkst‚|| _|| _t|ƒ| _|| _|| _tj	| jŽ | _
|| _d S )NÚuvw)ÚlowerÚAssertionErrorÚplaneÚregionÚtupleÚposÚdomainlsÚresponser   ÚlinspaceÚtimesÚimpact)Úselfr(   r)   r+   r,   r-   r0   r   r   r   Ú__init__i   s    
zResponseFunction.__init__c             C   s   t  || j| j¡S )N)r   Úinterpr/   r-   )r1   r   r   r   r   Ú__call__u   s    zResponseFunction.__call__c             C   s@   | j d | j d |f}tj|Ž }t || j | j¡}| j||d�S )Nr   éÿÿÿÿ)r,   r-   )r/   r   r.   r3   r-   Údup)r1   ÚnbinsZnewlsZnewtimesZnewrespsr   r   r   Úresamplex   s    
zResponseFunction.resamplec             K   s   t f t| jf|ŽŽS )z{
        Return a new ResponseFunction which is a copy of this one and
        with any values in kwds overriding.
        )r$   ÚdictÚasdict)r1   Úkwdsr   r   r   r6   ~   s    zResponseFunction.dupc             C   s
   | j d S )Né   )r,   )r1   r   r   r   r7   …   s    zResponseFunction.nbinsc             C   s$   t | j| j| j| j| j ¡ | jd�S )z)
        Object as a dictionary.
        )r(   r)   r+   r,   r-   r0   )r9   r(   r)   r+   r,   r-   Útolistr0   )r1   r   r   r   r:   ‰   s    zResponseFunction.asdictr   g       @r   c                sh   |dkr|   ¡ }n
|  |¡}|jd |jd  ‰ ‡ fdd„|jD ƒ|_t|j|||ƒ}t||jƒ|_|S )zj
        Convolve electronics shaping/peaking response, returning a new
        ResponseFunction.
        Nr   r   c                s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úr)Údtr   r   ú
<listcomp>Ÿ   s    z+ResponseFunction.shaped.<locals>.<listcomp>)r6   r8   r/   r-   r   r!   )r1   r   r   r7   r   ZnewfrZelecrr   )r@   r   Úshaped’   s    

zResponseFunction.shapedc             C   s<   d| j | j| j| jf }| jd k	r0|d| j 7 }|d7 }|S )Nz7<ResponseFunction plane=%s region=%d domainls=%s pos=%sz
 impact=%fú>)r(   r)   r,   r+   r0   )r1   Zblahr   r   r   Ú__str__¤   s    
zResponseFunction.__str__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r4   r8   r6   Úpropertyr7   r:   r   ÚmVÚfCr
   rB   rD   r   r   r   r   r$   c   s   
	$r$   c                sL   t ƒ }x@tt‡ fdd„| D ƒƒƒD ]"‰‡ ‡fdd„| D ƒ}| |¡ q"W |S )zF
    Return a list of lists grouping by like values of the field.
    c             3   s   | ]}t |ˆ ƒV  qd S )N)Úgetattr)r>   Úd)Úfieldr   r   ú	<genexpr>²   s    zgroup_by.<locals>.<genexpr>c                s   g | ]}t |ˆ ƒˆkr|‘qS r   )rL   )r>   rM   )rN   Úthingr   r   rA   ³   s    zgroup_by.<locals>.<listcomp>)ÚlistÚsortedÚsetÚappend)ÚrflistrN   r   Zbythingr   )rN   rP   r   Úgroup_by­   s
     rV   c                s&   ‡ fdd„| D ƒ}|j dd„ d� |S )Nc                s   g | ]}|j ˆ kr|‘qS r   )r)   )r>   Úrf)r)   r   r   rA   ¸   s    zby_region.<locals>.<listcomp>c             S   s   | j S )N)r(   )Úxr   r   r   Ú<lambda>¹   s    zby_region.<locals>.<lambda>)Úkey)Úsort)rU   r)   r   r   )r)   r   Ú	by_region·   s    r\   c             C   sD   | j d | j d  }|dkr0td| j| jf ƒ‚t | j¡}|| S )z7
    Integrate total charge in a current response.
    r   r   g        z1Corrupt response function for plane %s, region %d)r/   Ú
ValueErrorr(   r)   r   Úsumr-   )rW   r@   Úitotr   r   r   Útotal_charge½   s
    r`   Úwc                sÆ   ‡‡fdd„| D ƒ}ˆ dk	r@t ˆ tjƒs.ˆ g‰ ‡ fdd„|D ƒ}t|ƒ}d|krldt| ƒˆˆˆ f }t|ƒ‚tdd„ |D ƒƒ}|| }tj | }	tƒ }
x(| D ] }|j	|j
|	 d�}|
 |¡ qžW |
S )	au  
    Return new rflist with all responses normalized to be in Ampere
    and assuming a single electron was drifting.

    The collection signal on the given plane and region is used to
    normalize.  If impact is an impact distance, or a list of them
    then the average collection signal is used.  If not given, all
    impacts for the given plane/region are used.
    c                s$   g | ]}|j ˆ kr|jˆkr|‘qS r   )r(   r)   )r>   rW   )r(   r)   r   r   rA   Ñ   s    znormalize.<locals>.<listcomp>Nc                s   g | ]}|j ˆ kr|‘qS r   )r0   )r>   rW   )r0   r   r   rA   Õ   s    r   z7No fields to average out of %d for nomalize(%s, %d, %s)c             S   s   g | ]}t |ƒ‘qS r   )r`   )r>   rW   r   r   r   rA   Ü   s    )r-   )Ú
isinstanceÚcollectionsÚSequenceÚlenr]   r^   r   ÚeplusrQ   r6   r-   rT   )rU   r(   r)   r0   Z	toaverageÚnumÚmsgÚqtotZqavgÚscaleÚoutrW   Znewrfr   )r0   r(   r)   r   Ú	normalizeÇ   s"    

rl   c             C   sH  t ƒ }�x:t| dƒD �]*}t|dƒ}t |ƒ}| ¡  dd„ |D ƒ}t|ƒ}xòt||d… ||d… ƒD ]Ô\}}|jdd„ d� |jd	d„ d� t |d
 j¡}	d
}
x"|| D ]}|	|j7 }	|
d7 }
q¬W |	d9 }	|
d9 }
|	|d
 j|d
 j 8 }	|
d8 }
|	|d j|d j 8 }	|
d8 }
|	|
 }	|d
 j	|	dd�}| 
|¡ qfW qqW |S )zÏ
    Average fine-grained response functions over multiple impact
    positions in the same plane and wire region.

    Return list of new response.ResponseFunction objects ordered by
    plane, region.
    r(   r)   c             S   s   g | ]}|d  j ‘qS )r   )r)   )r>   rU   r   r   r   rA   õ   s    z_average.<locals>.<listcomp>Nc             S   s   | j S )N)r0   )rX   r   r   r   rY   ú   s    z_average.<locals>.<lambda>)rZ   c             S   s   | j S )N)r0   )rX   r   r   r   rY   û   s    r   r   g       @r<   r5   )r-   r0   )rQ   rV   ÚreverseÚmaxÚzipr[   r   Ú
zeros_liker-   r6   rT   )Úfiner   ÚinplaneÚbyregionÚnoigerybÚregionsÚcenterÚregpÚregmÚtotÚcountÚoneÚdatr   r   r   Ú_averageç   s4    
$
r}   c             C   s
  t ƒ }xþt| dƒD ]ð}t|dƒ}t |ƒ}| ¡  xÎt||ƒD ]À\}}|jdd„ d� |jdd„ d� t |d j¡}t|ƒ}dg| }	d	|	d< d	|	d
< x8t	|ƒD ],}
||
 }||
 }||	|
 |j|j  7 }q W |d|d   }|d j
|dd�}| |¡ q<W qqW |S )aÆ  
    Average fine-grained response functions over multiple impact
    positions in the same plane and wire region.  It assumes an odd
    number of regions and a half-populated impact positions per region
    such that the first impact is exactly on a wire and the impact is
    exactly on a half-way line between neighboring wires.

    Return list of new response.ResponseFunction objects ordered by
    plane, region which cover the same regions.
    r(   r)   c             S   s
   t | jƒS )N)Úabsr0   )rX   r   r   r   rY   %  s    zaverage.<locals>.<lambda>)rZ   c             S   s
   t | jƒS )N)r~   r0   )rX   r   r   r   rY   &  s    r   g      ð?g      à?r5   r<   r   N)r-   r0   )rQ   rV   rm   ro   r[   r   rp   r-   re   Úranger6   rT   )rq   Zcoarserr   rs   rt   rw   rx   ry   ÚnimpactsZbinsizer0   ZrpZrmr|   r   r   r   Úaverage  s,    

r�   c       	      C   s¶   t dd„ | D ƒƒ}t|ƒdkr&t| ƒ} t| ƒ} tƒ }t| dƒ}xn|D ]f}|jdd„ d� dd„ |D ƒ}t|ƒ}| ¡  ||dd	… 7 }t 	|¡}tj
j|d
d�}| |¡ qDW t|ƒS )zÃ
    Return a tuple of response spectra as collection of per-plane
    matrices in channel periodicity vs frequency.  The rflist is both
    averaged over impacts (if needed) and normalized.
    c             S   s   g | ]
}|j ‘qS r   )r0   )r>   rW   r   r   r   rA   D  s    z*field_response_spectra.<locals>.<listcomp>r   r(   c             S   s   | j S )N)r)   )rX   r   r   r   rY   M  s    z(field_response_spectra.<locals>.<lambda>)rZ   c             S   s   g | ]
}|j ‘qS r   )r-   )r>   rW   r   r   r   rA   N  s    N)r   r   )Úaxes)rS   re   r�   rl   rQ   rV   r[   rm   r   Úasarrayr   Úfft2rT   r*   )	rU   Úimpactsr   Úbyplanerr   Ú	responsesÚrowsÚmatZspectr   r   r   Úfield_response_spectra>  s     


rŠ   c             C   sÚ   t ƒ }t| dƒ}xÄ|D ]¼}t|dƒ}dd„ |D ƒ}t|ƒ}t tdd„ |D ƒƒƒ}| ¡  t|ƒ}	t|d jƒ}
||	|
f}t |¡}xD|D ]<}| |d j	¡}x&|D ]}| |j
¡}|j|||f< q¢W qˆW | |¡ qW |S )zÙ
    Return a field responses as a number of matrices blocked by impacts.

    Returns a triple of arrays of shape (Nimpacts, Nregion, Ntbins).

    If eresp is given, it is convolved with each response function.
    r(   r0   c             S   s   g | ]}|d  j ‘qS )r   )r0   )r>   rM   r   r   r   rA   l  s    z'plane_impact_blocks.<locals>.<listcomp>c             S   s   g | ]
}|j ‘qS r   )r)   )r>   rW   r   r   r   rA   n  s    r   )rQ   rV   re   rS   r[   r-   r   ÚzerosÚindexr0   r)   rT   )rU   Zerespr   r†   rr   Úbyimpactr…   r€   ru   ÚnregionsÚntbinsZ	pib_shapeÚpibÚinimpactZimpact_indexÚinregionZregion_indexr   r   r   Úplane_impact_blocksW  s&    






r“   c               @   s2   e Zd ZdZdej fdd„Zdd„ Zdd„ Zd	S )
ÚPlaneImpactBlocksaû  
    Organize responses into per (plane,impact) and make available as
    array blocks of shape (Nregion,Ntbins).

    There are two symmetries for response Resp(w,r,i) on wire w for
    path near wire region r on impact i.

        - Due to reciprocity: Resp(w=0,r=N,i) = R(w=N,r=0,i)

        - Due to geometry: R(w=0,r=N,i=M) = R(w=0,r=-N,i=-M)

    See the functions `plots.plane_impact_blocks_full()` and
    `plots.plane_impact_blocks()` for visualizing this data.  In
    particular check for continuous patterns of responses across
    different impact positions in the first and check that U-plane,
    high-positive impact position puts most response on wires 0 and 1
    and high-negative impact positions puts most response on wires 0
    and -1.
    g        c             C   s”  |d }t |jƒ| _|jd | _|jd |jd  | _| j| j | _| j| j | _|| _t	t
dd„ |D ƒƒƒ| _t	t
dd„ |D ƒƒƒ| _t	t
dd„ |D ƒdd„ |D ƒ ƒƒ| _tƒ }t|dƒ}xÂ|D ]º}|d j}tƒ  ||< }t|d	ƒ}	x’|	D ]Š}
d
|
d j }|dk�st‚tƒ  || < }tƒ  ||< }t|
dƒ}x@|D ]8}t |ƒdk�sTt‚|d }|j}|||< ||| < �q>W qðW qÄW || _tƒ | _d S )Nr   r   c             S   s   g | ]
}|j ‘qS r   )r(   )r>   rW   r   r   r   rA   Ÿ  s    z.PlaneImpactBlocks.__init__.<locals>.<listcomp>c             S   s   g | ]
}|j ‘qS r   )r)   )r>   rW   r   r   r   rA      s    c             S   s   g | ]
}|j ‘qS r   )r0   )r>   rW   r   r   r   rA   ¡  s    c             S   s   g | ]}|j  ‘qS r   )r0   )r>   rW   r   r   r   rA   ¡  s    r(   r0   r5   g        r)   )re   r-   r�   r/   ZtminÚtbinZtrangeZtmaxÚxstartrR   rS   Z
plane_keysÚregion_keysZimpact_keysr9   rV   r(   r0   r'   r)   Ú_treeÚ_by_plane_impact)r1   rU   r–   ZonerfZtreer†   rr   Zplane_letterZ
tree_planer�   r‘   r0   Ztree_impact_posZtree_impact_negrs   r’   rW   r)   r   r   r   r2   “  s<    &






zPlaneImpactBlocks.__init__c                s–   ||f}y
| j | S  tk
r&   Y nX | j| | ‰ ‡ fdd„| jD ƒ}t t| jƒt|d jƒf¡}xt|ƒD ]\}}|j||< qrW || j |< |S )z—
        Return an array shaped (Nregions, Ntbins) for the given plane
        and impact.  Row=0 corresponds to the highest region (wire 10).
        c                s   g | ]}ˆ | ‘qS r   r   )r>   r?   )Úppir   r   rA   Ê  s    z2PlaneImpactBlocks.region_block.<locals>.<listcomp>r   )	r™   ÚKeyErrorr˜   r—   r   r‹   re   r-   Ú	enumerate)r1   r(   r0   rZ   Zrfsr‰   ÚrowrW   r   )rš   r   Úregion_block¿  s    

zPlaneImpactBlocks.region_blockc             C   s   | j | | | jS )N)r˜   r-   )r1   r(   r0   r)   r   r   r   r-   Ñ  s    zPlaneImpactBlocks.responseN)	rE   rF   rG   rH   r   Úcmr2   rž   r-   r   r   r   r   r”     s   ,r”   c               @   sV   e Zd Zdd„ Zddd„Zdd„ Zedd	„ ƒZed
d„ ƒZedd„ ƒZ	edd„ ƒZ
dS )Úfooc             C   s   | j d | _d S )Nr<   )rŽ   Úregion_center)r1   r   r   r   r2   Ö  s    zfoo.__init__Nc             C   sN   | j | }|dkr2|dkr"|| S t ||  ¡S |  ||¡\}}|||f S )aF  
        Return a response block by plane (0,1,2) and by impact
        (-4,...,0,...,5) and optionally by region (-10,...,0,...,10).

        If region is None, return corresponding (Nregions,Ntbins)
        array.  If region is given, return (Ntbins) array.  The impact
        (and region) numbers may be negative.
        Nr   )Úpibsr   ÚflipudÚ impact_region_numbers_to_indices)r1   r(   r0   r)   r�   r   r   r   r4   Û  s    	
zfoo.__call__c             C   s*   |dk r|d9 }|d9 }|| j   ||fS )Nr   r5   )r¡   )r1   r0   r)   r   r   r   r¤   í  s
    
z$foo.impact_region_numbers_to_indicesc             C   s   t | jd ƒd }t| |ƒS )Nr   r   )re   r¢   r   )r1   Z
max_impactr   r   r   Úimpact_rangeõ  s    zfoo.impact_rangec             C   s   | j d }t| |d ƒS )Nr<   r   )rŽ   r   )r1   Znhalf_regionsr   r   r   Úregion_rangeù  s    
zfoo.region_rangec             C   s   | j d d jd S )Nr   )r¢   Úshape)r1   r   r   r   rŽ   þ  s    zfoo.nregionsc             C   s   | j d d jd S )Nr   r   )r¢   r§   )r1   r   r   r   r�     s    z
foo.ntbins)N)rE   rF   rG   r2   r4   r¤   rI   r¥   r¦   rŽ   r�   r   r   r   r   r    Ô  s   
r    g      à?c       
         sš   | d }t | ƒ}t|j||ƒ}tj |¡}|j‰ tt|jd| |jd |jd    ƒƒ‰‡ ‡fdd„|D ƒ}t |t	ˆˆ ˆ ƒ¡‰‡fdd„|D ƒ}	|	S )z£
    Return the a response matrix such as passed to `deconvolve()`.

    Only the frequencies corresponding to a sampling period of `tick`
    are retained.  
    r   g       @r   c                s&   g | ]}t j|tˆˆ ˆ ƒd d�‘qS )r   )Úaxis)r   Údeleter   )r>   Úf)ÚNhaveÚNkeepr   r   rA     s    z*response_spect_nominal.<locals>.<listcomp>c                s   g | ]}ˆ | ‘qS r   r   )r>   rª   )Úele_choppedr   r   rA     s    )
rŠ   r   r/   r   r   r7   ÚintÚroundr©   r   )
rU   r   r   ÚtickÚfirstÚfrmZelectZelespZfrm_choppedÚrespr   )r«   r¬   r­   r   Úresponse_spect_nominal  s    *r´   c                s.   t  d||¡}‡ ‡fdd„}t  |¡}||ƒS )aÿ  
    Return a Fourier space filter function:

    filter = exp(-(freq/sig)^(power))

    The filter function is returned as an array `nbins` long covering
    the low half of the Fourier domain up to the given `nyquist`
    "frequency".

    The `sig` and `nyquist` parameters are in units of the relevant
    "frequency" for the given Fourier domain.  For time-domain Fourier
    this is likely Hz.  For channel-domain this is likely in units of
    per pitch (unitless).  Caller assures this consistency.
    r   c                s   t  d| ˆ ˆ   ¡S )Ng      à¿)r   r	   )rª   )Úpowerr    r   r   Úfilt0  s    zfilter_expower.<locals>.filt)r   r.   Ú	vectorize)r    rµ   r7   ZnyquistZfreqsr¶   r   )rµ   r    r   Úfilter_expower   s    
r¸   i€%  i¸  g      ð?c             C   sr   |t j }dd|  }tdd| |ƒ}tdd| |ƒ}tdd| |ƒ}dd|  }	td	t tj¡ d
||	ƒ}
||||
fS )zP
    Return (fu,fv,fw,fc) filters.

    See `filter_expower()` for details.
    g      ð?r<   g    †Agv7OuÈÍ@g    `þAgòï3.è@g    �ÎAgf1±ù¸@gffffffæ?g       @)r   Úsr¸   r   ÚsqrtÚpi)Únticksr°   ZnpitchesÚpitchZtick_secondsZ
nyquist_hzZfuÚfvÚfwZ
nyquist_ppZfcr   r   r   Úfilters6  s    
	rÀ   c             C   sº   | j \}}|j \}}tjj| dd�}tj|t|d ||d  ƒdd�}tj|t|d ||d  ƒdd�}|| |d|…  d|¡j }	tjj|	dd�}
|
|d|…  }
tjj|
d	d�}|S )
a;  
    Return a matrix like Mct which is deconvolved by Rpf and filtered
    with the Ff and Fp.

    Indices are c=channel, t=time, p=periodicity, f=frequency.

    Mct is the measured ADC in a plane as a matrix of Nchannel rows
    and Nticks columns.

    Rpf is the wire periodicity and frequency space 2D Fourier
    transform of the field response * electronics response.  It is
    assumed to contain only the four corners up the time and wire
    Nyquist frequencies of Mct.

    Ff is a frequency space filter.

    Fp is the channel periodicity space filter.
    )r   r   )r‚   r<   r   )r¨   r   N)r   )r   )	r§   r   r   r„   r©   r   ÚreshapeÚTÚifft2)ZMctZRpfZFfZFpZnchanZntickZnperiZnfreqZMpfZSpfZScfZSctr   r   r   Ú
deconvolveU  s    

""rÄ   c       
   	   C   s–   t ƒ }xŠ| jD ]€}xz|jD ]p}tt|j|j ƒƒ}|j|jf}t|j	ƒ}| j
|| j |f}tj||j  }t|j||||j	|ƒ}	| |	¡ qW qW |S )zv
    Convert response.schema objects to 1D ResponseFunction objects.

    Fixme: this has not yet been validated.
    )rQ   ÚplanesÚpathsr®   r¯   Úpitchposr½   Úwireposre   ÚcurrentÚtstartÚperiodZpathcr$   ÚplaneidrT   )
Úfrr   ÚprÚpathr)   r+   Znsamplesr/   r0   rW   r   r   r   Úschematorf1dw  s    
rÐ   r   gmçû©ñÒñ?c             C   s  d}| d }|j d |j d  }|j d }tƒ }t| dƒ}xÈ|D ]À}	|	d j}
d |
¡}dd„ |	D ƒ}t|d jd |d jd  ƒ}|	d jd }|	jdd	„ d
� tƒ }x:|	D ]2}|j| |j	 }d}t
 |j||¡}| |¡ q°W t
 ||||¡}| |¡ q@W t
 ||||||¡S )zê
    Convert the list of 1D ResponseFunction objects into
    response.schema objects.

    The "1D" refers to the drift paths starting on a line in 2D space.

    Because it is 1D, all the pitch and wire directions are the same.
    )g      ð?g        g        r   r   r(   r%   c             S   s.   g | ]&}|j d kr|jdks&|jdkr|‘qS )g        r   r   )r0   r)   )r>   rW   r   r   r   rA      s    z rf1dtoschema.<locals>.<listcomp>c             S   s   | j d | j S )Ni'  )r)   r0   )rX   r   r   r   rY   £  s    zrf1dtoschema.<locals>.<lambda>)rZ   g        )r/   rQ   rV   r(   rŒ   r~   r+   r[   r)   r0   r   ÚPathResponser-   rT   ÚPlaneResponseÚFieldResponse)rU   ÚoriginÚspeedÚanti_drift_axisr{   rË   rÊ   rÅ   r†   rr   ÚletterrÌ   Zonetwor½   ÚlocationrÆ   rW   rÇ   rÈ   ÚparZplrr   r   r   Úrf1dtoschema‹  s,    




 
rÚ   ú$wire-cell-garfield-response.json.bz2c             C   sš   ddl }| dd„ | D ƒ¡}| d¡r:t|dƒ |¡ dS | d¡rbddl}| |d¡ |¡ dS | d¡rŠddl}| |d	¡ |¡ dS td
| ƒ‚dS )z5
    Write a list of response functions to file.
    r   Nc             S   s   g | ]
}|j ‘qS r   )r:   )r>   rW   r   r   r   rA   º  s    zwrite.<locals>.<listcomp>z.jsonra   z	.json.bz2z.json.gzÚwbzunknown file format: %s)	ÚjsonÚdumpsÚendswithÚopenÚwriteÚbz2ÚBZ2FileÚgzipr]   )rU   Ú
outputfilerÝ   Útextrâ   rä   r   r   r   rá   µ  s    


rá   i„5  c       
      C   s²   t dd„ | D ƒƒ}t|ƒdkr&t| ƒ} t| dƒ}tƒ }xR|D ]J}|d }t |j¡}x|D ]}||j7 }qZW |j|ddd�}	| 	|	¡ q<W |dkr®x|D ]}| j|9  _q˜W |S )	av  
    Assuming an infinite track of `normalization` ionization electrons
    per pitch which runs along the starting points of the response
    function paths, calculate the average response on the central wire
    of each plane.  The returned responses will be normalized such
    that the collection response integrates to the given normalization
    value if nonzero.
    c             S   s   g | ]
}|j ‘qS r   )r0   )r>   rW   r   r   r   rA   Ó  s    zline.<locals>.<listcomp>r   r(   r   N)r-   r0   r)   g        )
rS   re   r�   rV   rQ   r   rp   r-   r6   rT   )
rU   Únormalizationr…   r†   r   rr   r±   ry   rW   r|   r   r   r   ÚlineÉ  s     




rè   )r   )ra   r   N)N)rÛ   )'rH   Úwirecellr   Ú r   r   r   rc   r
   r   rJ   rK   r   r·   r!   r#   Úobjectr$   rV   r\   r`   rl   r}   r�   rŠ   r“   r”   r    r´   r¸   rÀ   rÄ   rÐ   rŸ   ÚmmrÚ   rá   rf   rè   r   r   r   r   Ú<module>   s:   &"
	J



 '0
(U3" *
