a
    àˆc¤  ã                   @   sT   d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z ddlmZ dd„ ZdS )	zC
This holds MicroBooNE specific routines related to wire geometry.
é   )Úschemaé    )ÚunitsN)Údefaultdictc              
      s
  t  ¡ ‰ d}d}d}ttƒ}t| ƒ��6}| ¡ D �]}| d¡rBq0| ¡ }|sPq0| ¡ }dd„ |dd… D ƒ\}}	}
dd„ |dd… D ƒ}d	d„ |dd
… D ƒ}t	dƒD ]4}t
|| ƒdk rÄd||< t
|| ƒdk r¨d||< q¨|d |d k rø|| }}ˆ jdg|¢R Ž }ˆ jdg|¢R Ž }t  |	||¡}ˆ  d|
||||¡}||  |¡ q0W d  ƒ n1 �sd0    Y  ‡ fdd„}tƒ }t| ¡ ƒD ].\}	}|j|d� ˆ  d|	|¡}| |¡ �qŒ|tt	dƒƒk�rätd| ¡ ƒ tdƒ ˆ  dd|¡}ˆ  dd|g¡ ˆ   ¡ S )ai  Load a "celltree wire geometry file".

    Return a list of schema.Store.


    Somewhere, there exists code to dump wires from larsoft in a text
    format such as what made the files found:

    https://raw.githubusercontent.com/BNLIF/wire-cell-celltree/master/geometry/ChannelWireGeometry_v2.txt

    The file is line oriented.  Comment lines may begin with "#" and then have columns of:

    - channel: some channel ID

    - plane: plane number (0 == U, 1 == V, 2 == W)

    - wip: wire index in its plane

    - tail: triplet (sx,sy,sz) starting position of the wire in cm

    - head: triplet (ex,ey,ez) ending position of the wire in cm

    Example lines:

        # channel plane wind sx sy sz ex ey ex
        0 0 0 -6.34915e-14 117.153 0.0352608 -6.34287e-14 117.45 0.548658
        1 0 1 -6.34915e-14 116.807 0.0352608 -6.33552e-14 117.45 1.14866
        ...

    Some assumptions made by wire cell in using these files:

    - There is no wire wrapping, all wires have segment=0.

    - The wire index in plane (wip) counts from 0 for each plane, has no holes and
      increases with increasing Z coordinate.

    r   ú#c                 S   s   g | ]}t |ƒ‘qS © )Úint©Ú.0Úxr   r   ú>/home/yuhw/wc/wire-cell-python/wirecell/util/wires/onesided.pyÚ
<listcomp>I   ó    zload.<locals>.<listcomp>Né   c                 S   s   g | ]}t |ƒtj ‘qS r   ©Úfloatr   Úcmr	   r   r   r   r   J   r   é   c                 S   s   g | ]}t |ƒtj ‘qS r   r   r	   r   r   r   r   K   r   é	   g‚vIhÂ%<=g        r   ÚpointÚwirec                    s8   ˆ   d| ¡}ˆ   d|j¡}ˆ   d|j¡}d|j|j  S )Nr   r   g      à?)ÚgetÚtailÚheadÚz)Úindr   Úp1Úp2©Ústorer   r   Úwire_posZ   s    zload.<locals>.wire_pos)ÚkeyÚplanez-caution: did not get 3 planes, got plane IDs:z.continue, assuming you know what you are doingÚfaceÚanode)r   Zmakerr   ÚlistÚopenÚ	readlinesÚ
startswithÚstripÚsplitÚrangeÚabsÚmakeZwire_plane_idÚappendÚsortedÚitemsÚsortÚprintÚkeys)ÚfilenameZsegmentr#   ÚapaÚplanesÚfpÚlineÚchunksÚchr"   ZwipZbegÚendr   ZbegindZendindÚwpidZwireindr    Zwire_plane_indicesZ	wire_listÚindexZ
face_indexr   r   r   Úload   sP    '


0r>   )Ú__doc__Ú r   Úwirecellr   ÚnumpyÚmatplotlib.pyplotÚpyplotÚpltÚmatplotlib.patchesZpatchesZmpatchesÚcollectionsr   r>   r   r   r   r   Ú<module>   s   