a
    ×ýddó  ã                   @   sX   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mZ dd„ ZdS )	zD
This holds routines to load "multitpc" format wire geometry files.
é   )Úschemaé    )ÚunitsN)ÚdefaultdictÚ
namedtuplec              
      sx  t  ¡ ‰ ttƒ}t| ƒ��F}d}d}| ¡ D �]"}| d¡r>q,| ¡ }|sLq,| ¡ }t	t
|dd… ƒ\}}	}
}||kr€|d7 }nd}|}dd„ |dd	… D ƒ}d
d„ |d	d… D ƒ}|d |d krÎ|| }}|dkrä|	}|	}|}nd|	 d }|	d }|}t  |
||¡}ˆ jdg|¢R Ž }ˆ jdg|¢R Ž }ˆ  d|||||¡}||  |¡ q,W d  ƒ n1 �sh0    Y  ‡ fdd„}ttƒ}t| ¡ ƒD ]v\}}t  |¡\}
}}|j|d� |dk�rÂn$|dk�rætd|
||f ƒ | ¡  ˆ  d|
|¡}|||f  |¡ �q’ttƒ}t| ¡ ƒD ]*\\}}}ˆ  d||¡}||  |¡ �qt| ¡ ƒD ]\}}ˆ  d||¡ �qVˆ   ¡ S )aŒ	  
    Load a "multitpc" geometry file.

    Return a list of schema.Wire objects.

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

    protodune-wires-larsoft-v1.txt

    from the wire-cell-data package.  It has columns like:

    # chan tpc plane wire sx sy sz ex ey ez

    And, further the order of rows of identical channel number express
    progressively higher segment count.

        - channel: 0-based ID unique to one conductor in the entire
          detector

        - tpc: 0-based count of the "TPC" which is associated with one
          face of one anode.

        - plane: plane number (0 == U, 1 == V, 2 == W) in the face/tpc

        - 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::

        # chan tpc plane wire sx sy sz ex ey ez
        0 0 0   0 -368.926 606.670  229.881     -368.926 605.569 230.672
        0 1 0 400 -358.463 285.577  5.68434e-14 -358.463 606.509 230.672
        0 0 0 800 -368.926 286.518 -1.56319e-13 -368.926   7.61  200.467
        1 0 0   1 -368.926 606.670  229.306     -368.926 604.769 230.673
        1 1 0 401 -358.463 284.777  5.68434e-14 -358.463 605.709 230.672
        1 0 0 801 -368.926 285.718  1.7053e-13  -368.926   7.61  199.892
        ...
        800 0 1   0 -368.449 605.639  0.335  -368.449 606.335 0.835
        800 1 1 400 -358.939 605.648  0.335  -358.939 285.648 230.337
        800 0 1 800 -368.449   7.61  30.4896 -368.449 285.656 230.337
        801 0 1   1 -368.449 604.839  0.335  -368.449 606.335 1.40999
        801 1 1 401 -358.939 604.848  0.335  -358.939 284.848 230.337
        801 0 1 801 -368.449   7.61  31.0646 -368.449 284.856 230.337
        ...
        1600 0 2 0 -367.973 7.61 0.57535 -367.973 606 0.57535
        1601 0 2 1 -367.973 7.61 1.05455 -367.973 606 1.05455
        1602 0 2 2 -367.973 7.61 1.53375 -367.973 606 1.53375

    As shown in the example, the first and second set of three lines
    are from each the same "chan" and represent subsequent segments of
    one condutor.

    Also as shown, there is no sanity in segment direction.

    TPC 1 are on the "front" face and TPC 0 are "back" face in the
    sense that the normal to a front face pointing into the drift
    region is parallel to the global X axis.
    r   éÿÿÿÿú#Né   r   c                 S   s   g | ]}t |ƒtj ‘qS © ©Úfloatr   Úcm©Ú.0Úxr
   r
   ú>/home/yuhw/wc/wire-cell-python/wirecell/util/wires/multitpc.pyÚ
<listcomp>f   ó    zload.<locals>.<listcomp>é   c                 S   s   g | ]}t |ƒtj ‘qS r
   r   r   r
   r
   r   r   g   r   é
   Zsbndé   ÚpointÚwirec                    sP   ˆ   d| ¡}ˆ   d|j¡}ˆ   d|j¡}|j|j|j|j  |j|j   }|S )z_
        Return a number on which to sort wires.  The z-intercept is
        returned.
        r   r   )ÚgetÚtailÚheadÚzÚy)Úindr   Úp1Úp2Zz_intercept©Ústorer
   r   Úwire_pos|   s
    $zload.<locals>.wire_pos)ÚkeyÚdunevdz$Reversing wire order for p%d f%d a%dÚplaneÚfaceÚanode)r   Zmakerr   ÚlistÚopenÚ	readlinesÚ
startswithÚstripÚsplitÚmapÚintZwire_plane_idÚmakeÚappendÚsortedÚitemsZplane_face_apaÚsortÚprintÚreverse)ÚfilenameÚ
face_styleZwpidsÚfpÚsegZ	last_chanÚlineÚchunksÚchanZtpcr&   r   ZbegÚendr'   ÚapaZwidÚwpidZbegindZendindZwireindr#   Zby_apa_faceZ	wire_listZplane_indexZby_apaZplane_indicesZ
face_indexZface_indicesr
   r!   r   Úload   sh    @


0

rB   )Ú__doc__Ú r   Úwirecellr   ÚnumpyÚmatplotlib.pyplotÚpyplotÚpltÚmatplotlib.patchesÚpatchesÚmpatchesÚcollectionsr   r   rB   r
   r
   r
   r   Ú<module>   s   