a
    ×ýdd!  ã                   @   sö   d dl mZ d dlmZ d dlZd dlmZ d dlZd dl	Z	dZ
dd„ Zdd„ Zd2d
d„Zd3dd„Zd4dd„Zd5dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd6d(d)„Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ ZdS )7é    )Úunits)ÚarioNZtqxyzLTc                 C   s0   t ƒ }ttƒD ]\}}| dd…|f ||< q|S )z4Return a dictionary of arrays instead of a 2D array.N)ÚdictÚ	enumerateÚcolumns)ÚdeposÚretÚindÚletter© r   ú4/home/yuhw/wc/wire-cell-python/wirecell/gen/depos.pyÚtodict   s    r   c                 C   s*   | d dk}| D ]}| | | | |< q| S )zL
    For some reason sometimes zero steps are taken.  This removes them
    Úsç        r   )r   ÚkeepÚkeyr   r   r   Úremove_zero_steps   s    r   ú1.0c                 C   s¤   t  | ¡} t|tjƒ}t|tjƒ}t|tjƒ}|du r<|}	nt|tjƒ}	t|tjƒ}
|gd |g |g |	g |
g }t|ƒD ] \}}| dd…|f  |9  < q~| S )z3Apply units to a deposition array, return a new oneNé   )ÚnumpyÚcopyÚevalr   Ú__dict__r   )r   Zdistance_unitÚ	time_unitZenergy_unitZ	step_unitZelectrons_unitZdunitZtunitZeunitZsunitZnunitZtheunitsr	   Úunitr   r   r   Úapply_units(   s    
"r   c                 c   sD   t  | ¡}d}zt|||ƒV  W n ty4   Y dS 0 |d7 }qdS )z-
    Like load() but yield over indices.
    r   Né   )r   ÚloadÚKeyError)ÚdepofileÚ
generationÚfpÚindexr   r   r   Ústream<   s    
r#   c                 C   s¦   t | tƒrt | ¡}n| }|d|› � }|d|› � }|jd dkr^|jd dkr^|j}|j}|dd…df |k}||dd…f }|jd dkržtd|› �ƒ‚t|ƒS )	a   Return depo info from file.

    The integer specifies the generation of depos with 0 being the
    "youngest" and its "prior" depo being 1 higher.  

    A dict holding seven items of array value is returned.  The keys
    are t=time, c=charge, x, y, z, L=sigma_L, T=sigma_T.  The .columns
    string of this module provides these keys as a string.

    If index or generation do not exist, KeyError is raised.
    Ú
depo_data_Ú
depo_info_r   é   é   Né   zno generation )Ú
isinstanceÚstrr   r   ÚshapeÚTr   r   )r   r"   r    r!   ÚdatÚnfoÚindicesZthegenr   r   r   r   M   s    
r   r   c           
      C   sª   t ƒ }|D ]"}dd„ tt|ƒD ƒ}| |¡ q
||i}tj|dd�}|  d¡rTt}n|  d¡rftj	}nt
dt ƒ‚|| dƒ�}	|	 |¡ W d	  ƒ n1 sœ0    Y  d	S )
z/
    Save a deposition array to JSON file 
    c                 S   s   i | ]\}}||“qS r   r   )Ú.0ÚcÚvr   r   r   Ú
<dictcomp>q   ó    zdump.<locals>.<dictcomp>r'   )Úindentz.jsonz	.json.bz2zUnknown file extension: "%s"ÚwN)ÚlistÚzipr   ÚappendÚjsonÚdumpsÚendswithÚopenÚbz2ÚBZ2FileÚIOErrorÚfilenameÚwrite)
Zoutput_filer   ÚjpathZjlistÚdepoZjdepoÚoutÚtextZfopenr!   r   r   r   Údumpk   s    

(rG   c                 C   s4   t  |¡}t  | ¡} | dd…dd…f  |7  < | S )zC
    Return new set of depos all moved by given vector offset.
    Nr   r   )r   Úasarrayr   )r   Úoffsetr   r   r   Úmove‡   s    

rJ   c                 C   sP   t  |¡}t  g d¢¡}| D ]}||dd… 7 }qt| ƒ}|||  }t| |ƒS )zH
    Shift depositions so that they are centered on the given point
    )r   r   r   Nr   )r   rH   ÚlenrJ   )r   ÚpointÚtotrD   ÚnrI   r   r   r   Úcenter‘   s    
rO   c                 K   sd  | | } || }t  | ¡t  | ¡f}t  |¡t  |¡f}t|d |d  ƒ}t|d |d  ƒ}	td|› d|	› d|› d|› d�	ƒ |dks’|	dkr¢tdƒ tdƒ‚t  |d |d |¡}
t  |d |d |	¡}t ¡ }| 	d¡}t j
| ||
|f|d	�\}}}|jt j |dk|¡d
d|d |d |d |d f| dd¡| dd¡| dd¡d�}tj||d� |S )zA
    Plot b vs a weighted by c in da x db bins.  Return axis
    r   r   zbounds: ú[z] vs ú]zno range on an axiséo   )ÚbinsÚweightsÚnoneÚautoÚcmapÚviridisÚvminÚvmaxi † )ÚinterpolationÚaspectÚextentrW   rY   rZ   )Úax)r   ÚminÚmaxÚintÚprintÚ
ValueErrorÚlinspaceÚpltÚfigureÚadd_subplotÚhistogram2dÚimshowÚmaÚmasked_whereÚgetÚcolorbar)ÚaÚbr1   ÚdaÚdbÚkwdsZammZbmmÚnaÚnbZaedgesZbedgesÚfigr^   ÚhistZabinsZbbinsÚimr   r   r   Ú	_abc_histž   s.    "



ürx   c                 K   s\   t  | d ¡}| d }| d }t|||tjtjfi |¤Ž}| d¡ | d¡ | d¡ dS )zPlot colz q as X vs ZÚqÚxÚzúdepo electronsúX [cm]úZ [cm]N©r   Úabsrx   r   ÚcmÚ	set_titleÚ
set_xlabelÚ
set_ylabel)r   rr   ry   rz   r{   r^   r   r   r   Úplot_qxz½   s    

r…   c                 K   s\   t  | d ¡}| d }| d }t|||tjtjfi |¤Ž}| d¡ | d¡ | d¡ dS )zPlot colz q as X vs Yry   rz   Úyr|   r}   úY [cm]Nr   )r   rr   ry   rz   r†   r^   r   r   r   Úplot_qxyÉ   s    

rˆ   c                 K   s\   t  | d ¡}| d }| d }t|||tjtjfi |¤Ž}| d¡ | d¡ | d¡ dS )zPlot colz q as Z vs Yry   r{   r†   r|   r~   r‡   Nr   )r   rr   ry   r{   r†   r^   r   r   r   Úplot_qzyÕ   s    

r‰   c                 K   s\   t  | d ¡}| d }| d }t|||tjtjfi |¤Ž}| d¡ | d¡ | d¡ dS )zPlot colz q as X vs Try   rz   Útr|   r}   úT [us]N©	r   r€   rx   r   r�   Úusr‚   rƒ   r„   )r   rr   ry   rz   rŠ   r^   r   r   r   Úplot_qxtá   s    

rŽ   c                 K   s\   t  | d ¡}| d }| d }t|||tjtjfi |¤Ž}| d¡ | d¡ | d¡ dS )zPlot colz q as Z vs Try   r{   rŠ   r|   r~   r‹   NrŒ   )r   rr   ry   r{   rŠ   r^   r   r   r   Úplot_qztí   s    

r�   c                 K   sL   t  | d ¡}| d }tjdddd�\}}|j|tj dd� | d¡ d	S )
zPlot t histogram weighted by qry   rŠ   r   T©Útight_layoutéè  ©rS   r‹   N)r   r€   re   Úsubplotsrv   r   r�   rƒ   )r   rr   ry   rŠ   ru   r^   r   r   r   Úplot_tù   s
    r•   c                 K   sL   t  | d ¡}| d }tjdddd�\}}|j|tj dd� | d¡ d	S )
zPlot x histogram weighted by qry   rz   r   Tr�   r’   r“   r}   N)r   r€   re   r”   rv   r   r�   rƒ   )r   rr   ry   rz   ru   r^   r   r   r   Úplot_x  s
    r–   rX   c                 K   s\   t j |¡}t  ¡ }	|	 d¡}
|
 | ¡ |
 |¡ |
 |¡ |
j||||d�}t  	|¡ dS )z#
    Plot b vs a colored by c.
    rR   )r1   rW   N)
re   r�   Úget_cmaprf   rg   r‚   rƒ   r„   Úscatterrm   )Útitlern   ro   r1   ZatitZbtitZctitrW   rr   ru   r^   Úscr   r   r   Ú	_plot_abc  s    



r›   c                 K   s>   t d| d tj | d tj t | d ¡dddfi |¤Ž dS )	úPlot charge as scatter plotÚChargerz   r{   ry   úx [mm]úz [mm]úq [ele]N©r›   r   Úmmr   r€   ©r   rr   r   r   r   Úplot_xzqscat!  s    üür¤   c                 K   s>   t d| d tj | d tj t | d ¡dddfi |¤Ž dS )	rœ   r�   rz   r†   ry   rž   úy [mm]r    Nr¡   r£   r   r   r   Úplot_xyqscat*  s    üür¦   c                 K   s>   t d| d tj | d tj t | d ¡dddfi |¤Ž dS )	rœ   r�   rŠ   r{   ry   út [mus]rŸ   r    N©r›   r   r�   r¢   r   r€   r£   r   r   r   Úplot_tzqscat3  s    üür©   c                 K   s>   t d| d tj | d tj t | d ¡dddfi |¤Ž dS )	rœ   r�   rŠ   r†   ry   r§   r¥   r    Nr¨   r£   r   r   r   Úplot_tyqscat<  s    üürª   )Nr   )r   )r   r   )r   )rX   ) Úwirecellr   Úwirecell.utilr   r   Úmatplotlib.pyplotÚpyplotre   r:   r>   r   r   r   r   r#   r   rG   rJ   rO   rx   r…   rˆ   r‰   rŽ   r�   r•   r–   r›   r¤   r¦   r©   rª   r   r   r   r   Ú<module>   s6   








			