a
    zn„f£X  ã                   @  sÜ   d Z ddlmZ ddlZddlZdd„ ZG dd„ dejjƒZddd	d	e 	d
¡j
rTdnd	dœZddddddddddddœZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d d!„ d!eƒZG d"d#„ d#eƒZdS )$a]  
This module defines an :doc:`uproot.interpretation.Interpretation` for
several numerical types:

* :doc:`uproot.interpretation.numerical.AsDtype`: numbers, which can simply be
  described as a ``numpy.dtype``.
* :doc:`uproot.interpretation.numerical.AsDtypeInPlace`: a predefined array
  into which data may be overwritten.
* :doc:`uproot.interpretation.numerical.AsDouble32`: ROOT's ``Double32_t``
  packed data type.
* :doc:`uproot.interpretation.numerical.AsFloat16`: ROOT's ``Float16_t``
  packed data type.
* :doc:`uproot.interpretation.numerical.AsSTLBits`: an ``std::bitset<N>``
  for some ``N``.
é    )ÚannotationsNc                 C  s*   d}| j d ur"| j \} }|| }q| |fS )N© )Zsubdtype)ÚdtypeÚshapeÚsr   r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/interpretation/numerical.pyÚ_dtype_shape   s
    


r   c                   @  s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	Numericalzò
    Abstract superclass of numerical interpretations, including

    * :doc:`uproot.interpretation.numerical.AsDtype`
    * :doc:`uproot.interpretation.numerical.AsSTLBits`
    * :doc:`uproot.interpretation.numerical.TruncatedNumerical`
    c                 C  s   |S ©Nr   ©ÚselfÚarrayr   r   r   Ú_wrap_almost_finalized*   s    z Numerical._wrap_almost_finalizedc              	   C  s„  | j ||||||d� ||kr0| j|dd�}�nðd}	|d }
t|dd … ƒD ]’\}}|
|krr||krr|	|| 7 }	nh|
|  kr†|k r˜n n|	|| 7 }	nB|
|  kr¬|kr¾n n|	||
 7 }	n||k rÚ|
|krÚ|	||
 7 }	|}
qL|  ||	¡}|d }
t|dd … ƒD �]\}}|
|k�rP||k�rP||
 }||
 }|| }|||… |d d …< nÈ|
|  k�rh|k �ržn n2||
 }||
 }|| }|||… |d || …< nz|
|  k�r¶|k�rèn n.d}||
 }|| }|||… ||
| d …< n0||k �r|
|k�r|| }|||
| || …< |}
�q| |j d¡¡}| j|||||||d� |  |¡}| ||| |||¡}| j	|||||||d� |S )N)Úbasket_arraysÚentry_startÚ
entry_stopÚentry_offsetsÚlibraryÚbranchr   )Úlengthé   ú=)r   r   r   r   r   r   Úoutput)
Zhook_before_final_arrayÚ_prepare_outputÚ	enumerateÚviewr   ÚnewbyteorderZhook_before_library_finalizer   ÚfinalizeZhook_after_final_array)r   r   r   r   r   r   r   Úoptionsr   r   ÚstartÚ_ÚstopÚ
basket_numZlocal_startZ
local_stopÚbasket_arrayr   r   r   Úfinal_array-   sˆ    
ú	ù

ÿù
zNumerical.final_arrayc                 C  s   |  |f| j¡}|S )a  
        Prepare the output array in which the data is stored.

        In this default implementation, just create an empty array from the library but specializations might re-use an existing array (ex: :doc:`uproot.interpretation.numerical.AsDtypeInPlace`:)
        )ÚemptyÚto_dtype©r   r   r   r   r   r   r   r   Š   s    zNumerical._prepare_outputN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r   r   r   r   r   r	   !   s   ]r	   ÚBÚLz>f8)ú!ú>ú<ú|r   ÚboolZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tÚfloatÚdouble))Úbr   )Úir   )Úur   )r6   é   )r7   r8   )r6   é   )r7   r9   )r6   é   )r7   r:   )Úfr9   )r;   r:   c                   @  s    e Zd ZdZd"dd„Zdd„ Zdd„ Zed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZd#dd„Zedd„ ƒZedd„ ƒZdd„ Zdd„ Zd d!„ ZdS )$ÚAsDtypeaj  
    Args:
        from_dtype (``numpy.dtype`` or its constructor argument): Data type to
            *assume* of the raw but uncompressed bytes in the ``TBasket``.
            Usually big-endian; may include named fields and a shape.
        to_dtype (None, ``numpy.dtype``, or its constructor argument): Data
            type to *convert* the data into. Usually native-endian; may include
            named fields and a shape. If None, ``to_dtype`` will be set to the
            native-endian equivalent of ``from_dtype``.

    Interpretation for any array that can be fully described as a
    ``numpy.dtype``.
    Nc                 C  s4   t  |¡| _|d u r$| j d¡| _nt  |¡| _d S )Nr   )Únumpyr   Ú_from_dtyper   Ú	_to_dtype©r   Ú
from_dtyper&   r   r   r   Ú__init__º   s    zAsDtype.__init__c                 C  sF   | j | j d¡kr$dt| jƒ›d�S dt| jƒ›dt| j ƒ›d�S d S )Nr   zAsDtype(ú)ú, )r?   r>   r   Ústr©r   r   r   r   Ú__repr__Á   s    zAsDtype.__repr__c                 C  s$   t |ƒtu o"| j|jko"| j|jkS r
   )Útyper<   r>   r?   ©r   Úotherr   r   r   Ú__eq__Ç   s
    
ÿ
ýzAsDtype.__eq__c                 C  s   | j S )aS  
        Data type to expect of the raw but uncompressed bytes in the
        ``TBasket`` data. Usually big-endian; may include named fields and a
        shape.

        Named fields (``dtype.names``) can be used to construct a NumPy
        `structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__.

        A shape (``dtype.shape``) can be used to construct a fixed-size array
        for each entry. (Not applicable to variable-length lists! See
        :doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
        ``array.shape[1:] == dtype.shape``.
        )r>   rF   r   r   r   rA   Î   s    zAsDtype.from_dtypec                 C  s   | j S )aw  
        Data type to convert the data into. Usually the native-endian
        equivalent of :ref:`uproot.interpretation.numerical.AsDtype.from_dtype`;
        may include named fields and a shape.

        Named fields (``dtype.names``) can be used to construct a NumPy
        `structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__.

        A shape (``dtype.shape``) can be used to construct a fixed-size array
        for each entry. (Not applicable to variable-length lists! See
        :doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
        ``array.shape[1:] == dtype.shape``.
        ©r?   rF   r   r   r   r&   ß   s    zAsDtype.to_dtypec                 C  s   | j jS )z×
        Number of bytes per item of
        :ref:`uproot.interpretation.numerical.AsDtype.from_dtype`.

        This number of bytes includes the fields and shape, like
        ``dtype.itemsize`` in NumPy.
        )r>   ÚitemsizerF   r   r   r   rM   ð   s    	zAsDtype.itemsizec                 C  s   t | jƒ\}}|S r
   )r   r>   )r   r    r   r   r   r   Úinner_shapeû   s    zAsDtype.inner_shapec                 C  s   | j S r
   rL   rF   r   r   r   Únumpy_dtype   s    zAsDtype.numpy_dtypeÚi64Fr   c                 C  s`   |   |||||¡}tj ¡ }t| jƒ\}}	tj |||¡}
|	d d d… D ]}|j 	|
|¡}
qH|
S )Néÿÿÿÿ)
Ú_make_contextÚuprootÚextrasÚawkwardr   r?   Ú_utilÚawkward_formÚformsÚRegularForm)r   ÚfileÚcontextÚindex_formatÚheaderÚtobject_headerÚbreadcrumbsrU   Údr   ÚoutÚsizer   r   r   rW     s    	
ÿ
zAsDtype.awkward_formc                   s¤   dd„ ‰ ˆj jd u r"ˆ ˆj dƒ}n&dd ‡ ‡fdd„ˆj jD ƒ¡ d }ˆjjd u rbˆ ˆjdƒ}n&dd ‡ ‡fd	d„ˆjjD ƒ¡ d }tˆƒj› d
|› d|› d�S )Nc              	   S  s8   t | ƒ\}}d t|j |j|jd dd„ |D ƒ¡|¡S )Nz{}{}{}({}{})ú,c                 s  s   | ]}t |ƒV  qd S r
   )Úrepr)Ú.0Úxr   r   r   Ú	<genexpr>  ó    z2AsDtype.cache_key.<locals>.form.<locals>.<genexpr>)r   ÚformatÚ_numpy_byteorder_to_cache_keyÚ	byteorderÚkindrM   Újoin)r   Únamer`   r   r   r   r   Úform  s    ûzAsDtype.cache_key.<locals>.formÚ ú[rc   c                 3  s&   | ]}ˆ ˆj | d t|ƒ ƒV  qdS ©rc   N)rA   rd   ©re   Ún©ro   r   r   r   rg   (  s   ÿz$AsDtype.cache_key.<locals>.<genexpr>ú]c                 3  s&   | ]}ˆ ˆj | d t|ƒ ƒV  qdS rr   )r&   rd   rs   ru   r   r   rg   4  s   ú(rC   )rA   Únamesrm   r&   rH   r(   r@   r   ru   r   Ú	cache_key  s,    
þÿûÿ	ÿÿüÿzAsDtype.cache_keyc                   sH   dd„ ‰ ˆj jd u rˆ ˆj ƒS dd ‡ ‡fdd„ˆj jD ƒ¡ d S d S )Nc                 S  s0   t | ƒ\}}t|j|jf d dd„ |D ƒ¡ S )Nrp   c                 s  s   | ]}d t |ƒ d V  qdS ©rq   rv   N©rE   ©re   Údimr   r   r   rg   @  s   z1AsDtype.typename.<locals>.form.<locals>.<genexpr>)r   Ú _dtype_kind_itemsize_to_typenamerl   rM   rm   )r   r`   r   r   r   r   ro   >  s    ÿzAsDtype.typename.<locals>.formzstruct {ú c                 3  s(   | ] }ˆ ˆj | ƒ› d |› d�V  qdS )r   ú;N)rA   rs   ru   r   r   rg   I  s   z#AsDtype.typename.<locals>.<genexpr>Ú})rA   rx   rm   rF   r   ru   r   Útypename<  s    
ÿÿüÿzAsDtype.typenamec	                 C  s¾   | j ||||||||d� t| jƒ\}	}
z| |	¡ dg|
¢R ¡}W nX tyœ } z@td|j› d|j› dt|ƒ› d| › d|j	j
› �
ƒ|‚W Y d }~n
d }~0 0 | j|||||||||d�	 |S )	N©ÚdataÚbyte_offsetsÚbasketr   r[   Úcursor_offsetr   r   rQ   úbasket ú in tree/branch ú  has the wrong number of bytes (ú) for interpretation z	
in file )	r„   r…   r†   r   r[   r   r‡   r   r   )Úhook_before_basket_arrayr   r>   r   ÚreshapeÚ
ValueErrorr"   Úobject_pathÚlenrZ   Ú	file_pathÚhook_after_basket_array)r   r„   r…   r†   r   r[   r‡   r   r   r   r   r   Úerrr   r   r   r#   O  sF    øÿþÿü÷zAsDtype.basket_arrayc                 C  s@   t | jƒ\}}t ||f¡| _t | jƒ\}}t ||f¡| _d S r
   )r   r>   r=   r   r?   )r   r   r`   r   r   r   r   r�   }  s    zAsDtype.reshapec                 C  s   t || jƒS )aM  
        Returns a AsDtypeInPlace version of self in order to fill the given array in place.

        Example usage :
        ```
        var = np.zeros(N, dtype=np.float32)
        b = uproot.openn('afile.root')['treename']['varname']
        b.array(library='np', interpretation=b.interpretation.inplace(var) )
        ```
        )ÚAsDtypeInPlacer>   r   r   r   r   Úinplaceƒ  s    zAsDtype.inplace)N)NrP   FFr   )r(   r)   r*   r+   rB   rG   rK   ÚpropertyrA   r&   rM   rN   rO   rW   ry   r‚   r#   r�   r•   r   r   r   r   r<   «   s6   






     ù

$
.r<   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )r”   z‘
    Like :doc:`uproot.interpretation.numerical.AsDtype`, but a given array is
    filled in-place, rather than creating a new output array.
    c                 C  s   || _ || _t |j¡| _d S r
   )Ú_to_fillr>   r=   r   r?   )r   r   rA   r   r   r   rB   —  s    zAsDtypeInPlace.__init__c              
   C  sl   |j dkrtd|j › d�ƒ‚| j | j¡}|t|ƒkr`tdt|ƒ› d| j› d|› d| j› d�	ƒ‚|d|… S )	zi
        Specialized version of _prepare_output : re-use our target array kept in self._to_fill.
        Únpz8AsDtypeInPlace can only be used with library 'np', not 'ú'z$Requesting to fill an array of size z (type z) with input of size rC   N)	rn   Ú	TypeErrorr—   r   r&   r�   rŽ   r?   r>   r'   r   r   r   r   œ  s    
ÿ$ÿzAsDtypeInPlace._prepare_outputN)r(   r)   r*   r+   rB   r   r   r   r   r   r”   ‘  s   r”   c                   @  s$   e Zd ZdZdd„ Zedd„ ƒZdS )Ú	AsSTLBitsz-
    Interpretation for ``std::bitset``.
    c                 C  s   t ‚d S r
   )ÚNotImplementedErrorrF   r   r   r   rB   ³  s    zAsSTLBits.__init__c                 C  s
   | j d S )Nr9   )Z
_num_bytesrF   r   r   r   rM   ¶  s    zAsSTLBits.itemsizeN)r(   r)   r*   r+   rB   r–   rM   r   r   r   r   r›   ®  s   r›   c                   @  s”   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZdd„ Zdd„ Zedd„ ƒZedd„ ƒZdd„ ZdS )ÚTruncatedNumericala  
    Abstract superclass for interpretations that truncate the range and
    granularity of the real number line to pack data into fewer bits.

    Subclasses are

    * :doc:`uproot.interpretation.numerical.AsDouble32`
    * :doc:`uproot.interpretation.numerical.AsFloat16`
    c                 C  s   | j S )zQ
        Lower bound on the range of real numbers this type can express.
        )Ú_lowrF   r   r   r   ÚlowÆ  s    zTruncatedNumerical.lowc                 C  s   | j S )zQ
        Upper bound on the range of real numbers this type can express.
        )Ú_highrF   r   r   r   ÚhighÍ  s    zTruncatedNumerical.highc                 C  s   | j S )z@
        Number of bytes into which to pack these data.
        )Ú	_num_bitsrF   r   r   r   Únum_bitsÔ  s    zTruncatedNumerical.num_bitsc                 C  s(   | j rt dddœdf¡S t d¡S dS )zæ
        The ``numpy.dtype`` of the raw but uncompressed data.

        May be a
        `structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__
        of ``"exponent"`` and ``"mantissa"`` or an integer.
        )z>u1r   )z>u2r   )ÚexponentÚmantissar   z>u4N)Úis_truncatedr=   r   rF   r   r   r   rA   Û  s    	zTruncatedNumerical.from_dtypec                 C  s   | j jS )zr
        Number of bytes in
        :ref:`uproot.interpretation.numerical.TruncatedNumerical.from_dtype`.
        )rA   rM   rF   r   r   r   rM   é  s    zTruncatedNumerical.itemsizec                 C  s   | j S )z:
        The ``dtype.shape`` of the ``to_dtype``.
        )Ú_to_dimsrF   r   r   r   Úto_dimsñ  s    zTruncatedNumerical.to_dimsc                 C  s   | j | j  kodkS   S )zÚ
        If True (:ref:`uproot.interpretation.numerical.TruncatedNumerical.low`
        and :ref:`uproot.interpretation.numerical.TruncatedNumerical.high` are
        both ``0``), the data are truly truncated.
        g        )rž   r    rF   r   r   r   r¦   ø  s    zTruncatedNumerical.is_truncatedc                 C  sP   t | jƒt | jƒt | jƒg}| jdkr8| d| j›�¡ d t| ƒjd 	|¡¡S )Nr   zto_dims=z{}({})rD   )
rd   rž   r    r¢   r§   Úappendri   rH   r(   rm   )r   Úargsr   r   r   rG     s    
zTruncatedNumerical.__repr__c                 C  s@   t | ƒt |ƒko>| j|jko>| j|jko>| j|jko>| j|jkS r
   )rH   rž   r    r¢   r§   rI   r   r   r   rK     s    
ÿ
þ
ý
ûzTruncatedNumerical.__eq__c                 C  s   | j S r
   )r&   rF   r   r   r   rO     s    zTruncatedNumerical.numpy_dtypec              
   C  s0   t | ƒj› d| j› d| j› d| j› d| j› d�
S )Nrw   rc   rC   )rH   r(   rž   r    r¢   r§   rF   r   r   r   ry     s    zTruncatedNumerical.cache_keyc	                 C  s¶  | j ||||||||d� z| | j¡}	W n` tyŠ }
 zHtd|j› d|j› dt|ƒ› d| › d| j›d|jj	› �ƒ|
‚W Y d }
~
n
d }
~
0 0 | j
�r6|	d  tj¡}|	d	  tj¡}|d
K }||d| jd > d @ d
| j > O }d| jd > |@ dkd d }| tj¡| }t| jƒ\}}| |¡ dg|¢R ¡}n^t| jƒ\}}|	 |¡ dg|¢R ¡}tj|t| j| j ƒd| j>  |d� tj|| j|d� | j||||||||	||d�
 |S )Nrƒ   rˆ   r‰   rŠ   r‹   z (expecting raw array of z
)
in file r¤   r¥   é   r   r   éþÿÿÿrQ   )ra   )
r„   r…   r†   r   r[   r‡   r   Úrawr   r   )rŒ   r   rA   rŽ   r"   r�   r�   r>   rZ   r‘   r¦   Zastyper=   Zint32r£   Úfloat32r   r&   r�   Úmultiplyr3   r    rž   r¢   ÚaddrŸ   r’   )r   r„   r…   r†   r   r[   r‡   r   r   r­   r“   r¤   r¥   Úsignr   r`   r   r   r   r   r#     sp    øÿÿþÿüÿýözTruncatedNumerical.basket_arrayN)r(   r)   r*   r+   r–   rŸ   r¡   r£   rA   rM   r¨   r¦   rG   rK   rO   ry   r#   r   r   r   r   r�   »  s,   







	

r�   c                   @  s<   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZddd„Zd	S )Ú
AsDouble32a~  
    Args:
        low (float): Lower bound on the range of expressible values.
        high (float): Upper bound on the range of expressible values.
        num_bits (int): Number of bits in the representation.
        to_dims (tuple of ints): Shape of
            :ref:`uproot.interpretation.numerical.AsDouble32.to_dtype`.

    Interpretation for ROOT's ``Double32_t`` type.
    r   c                 C  sj   || _ || _|| _|| _tj |¡r:d|  kr8dksBn tdƒ‚||krf| jsft	d|› d|› d�ƒ‚d S ©Nr8   é    z8num_bits must be an integer between 2 and 32 (inclusive)zhigh (z%) must be strictly greater than low (rC   ©
rž   r    r¢   r§   rS   rV   Zisintrš   r¦   rŽ   ©r   rŸ   r¡   r£   r¨   r   r   r   rB   l  s    "zAsDouble32.__init__c                 C  s   t  t j| jf¡S ©aH  
        The ``numpy.dtype`` of the output array.

        A shape (``dtype.shape``) can be used to construct a fixed-size array
        for each entry. (Not applicable to variable-length lists! See
        :doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
        ``array.shape[1:] == dtype.shape``.
        )r=   r   Úfloat64r¨   rF   r   r   r   r&   w  s    
zAsDouble32.to_dtypec                 C  s   dd  dd„ | jD ƒ¡ S )NZ
Double32_trp   c                 s  s   | ]}d t |ƒ d V  qdS rz   r{   r|   r   r   r   rg   …  rh   z&AsDouble32.typename.<locals>.<genexpr>©rm   r§   rF   r   r   r   r‚   ƒ  s    zAsDouble32.typenameNrP   Fc           
      C  sP   |   |||||¡}tj ¡ }|j d¡}| jd d d… D ]}	|j ||	¡}q8|S )Nr¸   rQ   ©rR   rS   rT   rU   rX   Z	NumpyFormr§   rY   ©
r   rZ   r[   r\   r]   r^   r_   rU   ra   rb   r   r   r   rW   ‡  s    	
ÿ
zAsDouble32.awkward_form)r   )NrP   FFr   ©	r(   r)   r*   r+   rB   r–   r&   r‚   rW   r   r   r   r   r²   `  s   


     ùr²   c                   @  s<   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZddd„Zd	S )Ú	AsFloat16a|  
    Args:
        low (float): Lower bound on the range of expressible values.
        high (float): Upper bound on the range of expressible values.
        num_bits (int): Number of bits in the representation.
        to_dims (tuple of ints): Shape of
            :ref:`uproot.interpretation.numerical.AsFloat16.to_dtype`.

    Interpretation for ROOT's ``Float16_t`` type.
    r   c                 C  sj   || _ || _|| _|| _tj |¡r:d|  kr8dksBn tdƒ‚||krf| jsft	d|› d|› d�ƒ‚d S r³   rµ   r¶   r   r   r   rB   ¦  s    "zAsFloat16.__init__c                 C  s   t  t j| jf¡S r·   )r=   r   r®   r¨   rF   r   r   r   r&   ±  s    
zAsFloat16.to_dtypec                 C  s   dd  dd„ | jD ƒ¡ S )NZ	Float16_trp   c                 s  s   | ]}d t |ƒ d V  qdS rz   r{   r|   r   r   r   rg   ¿  rh   z%AsFloat16.typename.<locals>.<genexpr>r¹   rF   r   r   r   r‚   ½  s    zAsFloat16.typenameNrP   Fc           
      C  sP   |   |||||¡}tj ¡ }|j d¡}| jd d d… D ]}	|j ||	¡}q8|S )Nr®   rQ   rº   r»   r   r   r   rW   Á  s    	
ÿ
zAsFloat16.awkward_form)r   )NrP   FFr   r¼   r   r   r   r   r½   š  s   


     ùr½   )r+   Ú
__future__r   r=   rS   r   ZinterpretationZInterpretationr	   r   Zisnativerj   r~   r<   r”   r›   r�   r²   r½   r   r   r   r   Ú<module>   s>   tû	õ g &: