a
    ×ýdd  ã                   @   sÞ   d Z ddlmZ G dd„ deddƒƒZG dd„ deddƒƒZG d	d
„ d
ed
dƒƒZG dd„ deddƒƒZG dd„ deddƒƒZG dd„ deddƒƒZG dd„ deddƒƒZ	dd„ Z
dd„ ZdZdZdZdZd d!„ Zd"d#„ Zd$S )%aS  
This module defines an schema of object which describe the
wire-related geometry for the Wire Cell Toolkit.

It covers a hierarchy of object types:

    - Detector

    - Anode (eg, one APA)

    - Face

    - Plane

    - Wire

    - Point

Except for Point, all objects are organizational and describe
parentage.  Points are used to describe the end points of wire
segments (wires) and are MUST be expressed in GLOBAL coordinates.

In the following cases the children are referenced by their parent in
a strictly ordered list:

    - A face orders its child planes following how charge drifts.
      That is, in U/V/W order.

    - A plane orders its child wires in increasing pitch direction
      such that the cross product of the wire direction (see next) and
      this pitch direction gives a direction which is normal to the
      wire plane and anti-parallel to the nominal drift direction for
      that plane.

    - Wire places its head end-point closer to the input of the
      detector electronics.  The wire direction is taken from tail to
      head end-points.  For top DUNE APAs and all other current
      detectors this direction points generally upward and for bottom
      DUNE APAs it points generally downward.

Ordering of all other lists is not defined.

Most objects in the schema have an "ident" attribute.  This number is
made available to user C++ code but is treated as OPAQUE but UNIQUE by
the toolkit.  If an object is referenced in multiple contexts their
ident MUST match.  For example, a wire plane and a field reponse plane
must be correlated by their ident.

Schema objects are held in flat, per type store and a reference to an
object of a particular type is made via its index into the store for
that type.

All values MUST be in the WCT system of units.  Code that generates
these objects may use the Python module wirecell.units to assure this.

Although not specified here, there are places where "front" and "back"
qualifiers are applied to "face".  A "front" face is one for which the
cross product of wire and pitch directions (as described above) is
parallel to the global X-axis.
é    )Ú
namedtuplec                   @   s   e Zd ZdZdZdS )ÚPointzR
    A position.

    :param float x:
    :param float y:
    :param float z:
    © N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   ú</home/yuhw/wc/wire-cell-python/wirecell/util/wires/schema.pyr   C   s   r   zx y zc                   @   s   e Zd ZdZdZdS )ÚWirea*  
    A Wire object holds information about one physical wire segment.

    A wire is a ray which points in the direction that signals flow
    toward the electronics.

    :param int ident:
    :param int channel: numerical identifier unique to this conductor.
        It is made available via IWire::channel().  This number is
        treated as opaque by the toolkit except that it is expected to
        uniquely identify an electronics input channel.
    :param int segment: count the number of other wires between this
        wire and the channel input.
    :param int tail: index referencing the tail end point of the wire
        in the coordinate system for the anode face.
    :param int head: index referencing the head end point of the wire
        in the coordinate system for the anode face.
    r   Nr   r   r   r   r   r   N   s   r   zident channel segment tail headc                   @   s   e Zd ZdZdZdS )ÚPlanea  
    A Plane object collects the coplanar wires.

    :param int ident:
    :param list wires: list of indices referencing the wires that make
        up this plane.  This list MUST be sorted in "increasing wire
        pitch location" as described above.
    r   Nr   r   r   r   r   r   d   s   r   zident wiresc                   @   s   e Zd ZdZdZdS )ÚFacezü
    A Face collects the wire and conductor planes making up one face
    of an anode plane.

    :param int ident:
    :param list planes: list of indices referencing planes.  This list
        MUST be in sorted in U/V/W order as described above.
    r   Nr   r   r   r   r   r   p   s   r   zident planesc                   @   s   e Zd ZdZdZdS )ÚAnodea  
    An Anode object collects together Faces.

    A detector like MicroBooNE has just one face per its single anode.
    protoDUNE/SP has two faces per each of its six anodes (aka APAs).

    :param int ident:
    :param list faces: list indices referencing faces.
    r   Nr   r   r   r   r   r   }   s   	r   zident facesc                   @   s   e Zd ZdZdZdS )ÚDetectorz™
    A detector of anodes.

    This allows one or more sets ot anodes to be defined.  
    :param int ident:
    :param list anodes: list of anodes
    r   Nr   r   r   r   r   r   Š   s   r   zident anodesc                   @   s   e Zd ZdZdZdd„ ZdS )ÚStoreaJ  
    A store of collections of the objects of this schema.

    This somewhat awkward indirection of a reference into a store is
    so that multiple things may refer to something without having to
    invent some kind of "pointer" which must be carried by each
    object.

    :param list detectors: list of the Detector objects.
    :param list anodes: list of the Anode objects.
    :param list faces: list of the Face objects
    :param list planes: list of the Plane objects.
    :param list wires: list of the Wire objects.
    :param list points: list of the Point objects.
    r   c                 C   s8   dt | jƒt | jƒt | jƒt | jƒt | jƒt | jƒf S )NzJ<Store: %d detectors, %d anodes, %d faces, %d planes, %d wires, %d points>)ÚlenÚ	detectorsÚanodesÚfacesÚplanesÚwiresÚpoints©Úselfr   r   r   Ú__repr__§   s    2ÿzStore.__repr__N)r   r   r   r	   r
   r   r   r   r   r   r   •   s   r   z*detectors anodes faces planes wires pointsc                  C   sH   dd l } dd l}tƒ }| | jt ¡D ]\}}| |¡r&| |¡ q&|S )Nr   )ÚsysÚinspectÚlistÚ
getmembersÚmodulesr   ÚisclassÚappend)r   r   ÚretÚnameÚobjr   r   r   Úclasses«   s    
r&   c                  C   s&   ddl } ddl}G dd„ dtƒ}|ƒ S )a2  
    Return a schema instance maker.

    >>> m = maker()
    >>> hid = m.make('Point', x=..., y=..., z=...)
    >>> tid = m.make('Point', x=..., y=..., z=...)
    >>> wid = m.make('Wire', ident=0, channel=0, segment=0 tail=tid, head=hid)

    >>> wire = m.get('Wire', wid)

    >>> store = m.schema()
    r   Nc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )zmaker.<locals>.SchemaMakerc                 S   s<   t ƒ | _tƒ D ](}|j ¡ }tƒ | j|d < || j|< qd S ©NÚs)ÚdictÚ_makersr&   r   Úlowerr   Ú__dict__)r   ÚklassZlnamer   r   r   Ú__init__Ã   s
    

z#maker.<locals>.SchemaMaker.__init__c                 W   s6   | j | }| j|d  }t|ƒ}||Ž }| |¡ |S r'   )r*   r,   r   r"   )r   ÚwhatÚargsr-   Ú
collectionZnthingsÚthingr   r   r   ÚmakeÊ   s    

zmaker.<locals>.SchemaMaker.makec                 S   s   | j |d  }|| S r'   )r,   )r   r/   Úindr1   r   r   r   ÚgetÒ   s    zmaker.<locals>.SchemaMaker.getc                 S   s8   |   d|¡}|   d|j¡}|   d|j¡}d|j|j  S ©NÚwireÚpointg      à?)r5   ÚtailÚheadÚy©r   r4   r7   Úp1Úp2r   r   r   Ú	wire_yposÖ   s    z$maker.<locals>.SchemaMaker.wire_yposc                 S   s8   |   d|¡}|   d|j¡}|   d|j¡}d|j|j  S r6   )r5   r9   r:   Úzr<   r   r   r   Ú	wire_zposÛ   s    z$maker.<locals>.SchemaMaker.wire_zposc                 S   s   t | j| j| j| j| j| jƒS )zReturn self as a schema.Store)r   r   r   r   r   r   r   r   r   r   r   Úschemaá   s    z!maker.<locals>.SchemaMaker.schemaN)	r   r   r   r.   r3   r5   r?   rA   rB   r   r   r   r   ÚSchemaMakerÁ   s   rC   )r   r   Úobject)r   r   rC   r   r   r   Úmaker³   s    #rE   é   é   é   é   c                 C   s   | t @ |t> B |t> B S )zSee WireCellIface/WirePlaneId.h©Ú
layer_maskÚ
face_shiftÚ	apa_shift)ÚplaneÚfaceZapar   r   r   Úwire_plane_idì   s    rP   c                 C   s   | t @ | dt> @ d? | t? fS )NrH   rG   rJ   )Úwpidr   r   r   Úplane_face_apað   s    rR   N)r	   Úcollectionsr   r   r   r   r   r   r   r   r&   rE   rK   rL   Z	face_maskrM   rP   rR   r   r   r   r   Ú<module>   s    >4