a
    `DÌfc+  ã                   @   s¢   d Z ddlmZ ddlZddlZdd„ Zdd„ ZG dd	„ d	eƒZG d
d„ deƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddej dej dfdd„ZdS )z.
Functions to provide information about wires
é    )ÚunitsNc                 C   s   t | j| j| jd�S )N)ÚxÚyÚz)Údictr   r   r   )Úp© r   ú:/home/yuhw/wc/wire-cell-python/wirecell/util/wires/info.pyÚp2p	   s    r
   c                 C   sö   t ƒ }| jD ]Ú}t|jt ƒ d�}|jD ]´}| j| }t|jt ƒ d�}|jD ]€}| j| }t|jt ƒ d�}	|jD ]L}
| j|
 }t|j|j|jt	| j
|j ƒt	| j
|j ƒd�}|	d  |¡ qn|d  |	¡ qJ|d  |¡ q&| |¡ qtd|d	�gS )
zk
    Convert the store to a dict-based representation.  This will
    inflate any duplicate references
    )ÚidentÚfaces)r   Úplanes)r   Úwires)r   ÚchannelÚsegmentÚheadÚtailr   r   r   r   )r   Úanodes)Úlistr   r   r   r   r   r   r   r   r
   Úpointsr   r   Úappend)ÚstoreZd_anodesÚanodeZd_anodeÚifaceÚfaceZd_faceÚiplaneÚplaneZd_planeZwindÚwireZd_wirer   r   r	   Útodict   s,    






ür   c                   @   s0   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	S )
ÚBoundingBoxc                 C   s   d | _ d | _d S ©N©ÚminpÚmaxp©Úselfr   r   r	   Ú__init__.   s    zBoundingBox.__init__c                 C   s   t | j| jd�S )Nr!   )r   r"   r#   r$   r   r   r	   Úasdict2   s    zBoundingBox.asdictc                 C   s®   t |dƒr,t |dƒr,| |jƒ | |jƒ d S | jd u rNt|ƒ| _t|ƒ| _d S | j ¡ D ]"\}}|| |k rX|| | j|< qX| j ¡ D ]"\}}|| |kr†|| | j|< q†d S )Nr"   r#   )Úhasattrr"   r#   r   Úitems)r%   r   ÚcÚvr   r   r	   Ú__call__6   s    




zBoundingBox.__call__c                    s   ‡ fdd„dD ƒS )Nc                    s&   i | ]}|d ˆ j | ˆ j|   “qS )ç      à?r!   ©Ú.0r*   r$   r   r	   Ú
<dictcomp>K   ó    z&BoundingBox.center.<locals>.<dictcomp>Úxyzr   r$   r   r$   r	   ÚcenterJ   s    zBoundingBox.centerN)Ú__name__Ú
__module__Ú__qualname__r&   Úpropertyr'   r,   r3   r   r   r   r	   r   -   s
   
r   c                   @   s,   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdS )ÚRayc                    s8   t  ‡ fdd„dD ƒ¡| _t  ‡ fdd„dD ƒ¡| _d S )Nc                    s   g | ]}ˆ d  | ‘qS )r   r   r.   ©r   r   r	   Ú
<listcomp>O   r1   z Ray.__init__.<locals>.<listcomp>r2   c                    s   g | ]}ˆ d  | ‘qS )r   r   r.   r9   r   r	   r:   P   r1   )ÚnumpyÚasarrayr   r   )r%   r   r   r9   r	   r&   N   s    zRay.__init__c                 C   s   | j | j S r    ©r   r   r$   r   r   r	   ÚvectorQ   s    z
Ray.vectorc                 C   s   d| j | j  S )Nr-   r=   r$   r   r   r	   r3   T   s    z
Ray.centerN)r4   r5   r6   r&   r7   r>   r3   r   r   r   r	   r8   M   s
   
r8   c                 C   s|  t  g d¢¡}t  g d¢¡}t| d ƒ}t  ||j¡}|t j |¡ }d }}tƒ }| D ](}t|ƒ}	t  ||	j	¡}
| 
|
|	f¡ qX| ¡  dd„ |D ƒ}d }}tt|dd… |d	d… ƒƒD ]j\}\}}t  ||j	|j	 ¡}
|dkrê|
}|du rü|
 }}nt||
ƒ}t||
ƒ}|t|
ƒ7 }||
|
 7 }q¾t| ƒd	 }|| }|dk�sLJ ‚|| ||  |d	  }|t t|ƒ¡|||fS )
z<
    Return pitch summary values [mean,rms,min,max,p0] 
    ©ç      ð?ç        rA   )rA   rA   rA   r   Nc                 S   s   g | ]}|d  ‘qS )é   r   )r/   Úprr   r   r	   r:   m   r1   z!pitch_summary.<locals>.<listcomp>rA   éÿÿÿÿrB   )r;   r<   r8   Úcrossr>   ÚlinalgÚnormr   Údotr3   r   ÚsortÚ	enumerateÚzipÚmaxÚminÚabsÚlenÚmathÚsqrt)r   ÚeksÚzeroÚr0ÚpdirÚpmaxÚpminZpraysÚwÚrr   ZraysZpsumZpsum2ÚcountÚr1Úr2Úp0ÚnÚpmeanZpvarr   r   r	   Úpitch_summaryZ   s:    *


r`   c                 C   s   t | ƒd d… S )NrD   )r`   )r   r   r   r	   Úpitch_mean_rms�   s    ra   c                 C   s   t dd„ | D ƒƒ}d| S )Nc                 S   s   g | ]}|t j ‘qS r   )r   Úmm)r/   Úppr   r   r	   r:   …   r1   z format_pitch.<locals>.<listcomp>z%(%.4f +/- %.6f [%.4f<%.4f], p0=%.4f) )Útuple)r   Zpmmr   r   r	   Úformat_pitch„   s    re   c                    s  t jt  dd„ | D ƒ¡dd�}t j |¡}|| }t  g d¢¡}t  ||¡}|t j |¡ }t| ƒ}td| ƒ}td| ƒ}t	| | ƒ}	t	| | ƒ}
|
j
|	j
 }t  ||¡|| d  }t	| d ƒ}|j
‰ ˆ ||  ‰t‡ fd	d
„tdƒD ƒ‡fdd
„tdƒD ƒd�S )z+
    Return a rays for wire and pitch.
    c                 S   s   g | ]}t |ƒj‘qS r   )r8   r>   )r/   rX   r   r   r	   r:   Œ   r1   z#wire_pitch_rays.<locals>.<listcomp>r   )Úaxisr?   g      Ð?g      è?rB   c                    s   i | ]\}}|ˆ | “qS r   r   ©r/   ÚiÚa)Úc0r   r	   r0   ¡   r1   z#wire_pitch_rays.<locals>.<dictcomp>r2   c                    s   i | ]\}}|ˆ | “qS r   r   rg   )Úc1r   r	   r0   ¢   r1   )r   r   )r;   ÚsumÚvstackrF   rG   r<   rE   rO   Úintr8   r3   rH   r   rJ   )r   ZwtotZwmagZwdirrR   rU   ÚnwiresÚind1Úind2Úw1Úw2Zc2cr_   Úw0r   )rj   rk   r	   Úwire_pitch_raysˆ   s&    ÿru   c                 C   sª  t ƒ }t| ƒD �]”}tƒ }|d |d< t ƒ |d< tƒ }|d D �]N}tƒ }|d |d< t ƒ |d< tƒ }|d }|D ]ú}	tƒ }
|	d |
d< t ƒ |
d< tƒ }|	d }|D ]¦}tƒ }|d |d< |d }t|ƒ|d< t|ƒ\|d< |d	< |d
< |d< |d< t|ƒ|d< tƒ }|D ]}||d ƒ ||d ƒ �q
||ƒ |j|d< |
d  |¡ q¤||ƒ |j|
d< |d  |
¡ qr||ƒ |j|d< |d  |¡ q>|j|d< | |¡ q|S )a^  
    Summarize the wires in store returning a dict

    Dict is a tree

    det -> anode -> face -> plane

    All three have key ["ident"] giving index number and "bb" giving
    bounding box with minp/maxp points

    Each plane has these keys:

        - pitch :: the wire pitch, also pmin, pmax and prms

        - nwires :: number of wires

    r   Úidr   r   r   r   ro   ZpitchZprmsrW   rV   r]   Zprayr   r   Úbb)	r   r   r   r   rO   r`   ru   r'   r   )r   ÚretÚdetZddictZdbbr   ÚadictZabbr   r   ZfdictZfbbr   r   Zpdictr   Zpbbr   r   r   r	   Úsummary_dict¥   sR    


&



r{   c           
      C   s†  t ƒ }t| ƒD �]p}|d D �]`}|d D �]P}tƒ }t ƒ }|d D ]@}| tt|d ƒƒ¡ |d D ]}||d ƒ ||d ƒ qfqD| d|d |d |jd	 tj |j	d	 tj |jd
 tj |j	d
 tj |jd tj |j	d tj f ¡ t
|d ƒD ]z\}	}| d|d |d d d d	 tj |d d d d	 |d d d d d d	  tj t|d ƒ||	 f ¡ �q q*qq|S )z?
    Return a summary data structure about the wire store.
    r   r   r   r   r   r   z@anode:%d face:%d X=[%.2f,%.2f]mm Y=[%.2f,%.2f]mm Z=[%.2f,%.2f]mmr   r   r   r   z%	%d: x=%.2fmm dx=%.4fmm n=%d pitch=%sr   é   )r   r   r   r   re   r`   r"   r   rb   r#   rJ   rO   )
r   Úlinesry   r   r   rw   Zpitchesr   r   Zpindr   r   r	   Úsummaryî   s6    	ýÿ4ýÿr~   rA   é
   rB   c              
   C   sš  t ƒ }|pd}d}tt| ƒƒD �]>\}}|d D �](}	|	d }
t|
ƒdksNJ ‚ddg}|
D ]Î}t ƒ }|d D ]<}tƒ }|d D ]}||d	 ƒ ||d
 ƒ q~| |¡ qlqldd„ |D ƒ}d}d}|d |d k rØd}d}|d }d|j|||  tj |||  tj |||  tj d� d ||< qZqZd 	|¡}d|j|	d |d� d }| |¡ q0q0qqd|tj |tj |tj f }dd 	|¡ d }|| S )a}  
    Return a Jsonnet string suitable for copying to set
    params.det.volumes found in the pgrapher configuration.

    The "namepat" should be a string with format markers "{variable}"
    and will be formated with "detector", "anode" set to their
    numbers.

    The "d" arguments give the distance measured from the collection
    plane to each of these logical planes.
    zQ
        wires: {anode},
        name: anode{anode},
        faces: [ {faces} ],
zJanode:{anodex}*wc.cm, cathode:{cathodex}*wc.cm, response:{responsex}*wc.cmr   r   r|   Únullr   r   r   r   c                 S   s   g | ]}|j d  ‘qS )r   )r"   )r/   rw   r   r   r	   r:   6  r1   z#jsonnet_volumes.<locals>.<listcomp>r@   r   g      ð¿rB   z
            {)ZanodexZ	responsexZcathodexÚ}ú,z    {r   )r   r   z    }zS    local volumeargs = { danode:%f*wc.cm, dresponse:%f*wc.cm, dcathode:%f*wc.cm },
z    volumes: [
z,
z
    ],
)
r   rJ   r   rO   r   r   Úformatr   ÚcmÚjoin)r   ZdanodeZ	dresponseZdcathodeZvolpatZvoltextsZfacepatZidetry   r   r   Z	facetextsr   Zface_bbsr   rw   r   Zuvw_xÚsignÚfindZxoriginZfacetextZvoltextZargstextZ
volumetextr   r   r	   Újsonnet_volumes  sT    
ýý

ÿrˆ   )Ú__doc__Úwirecellr   r;   rP   r
   r   Úobjectr   r8   r`   ra   re   ru   r{   r~   r„   Úmrˆ   r   r   r   r	   Ú<module>   s   ! 'I%ÿ