a
    zn„f.B  ã                   @  sŽ   d Z ddlmZ ddlZddlZddlZddlZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ ZedƒZG dd„ dejjƒZeƒ ZdS )zø
This module defines a :doc:`uproot.language.Language` for expressions passed to
:ref:`uproot.behaviors.TBranch.HasBranches.arrays` (and similar).

The :doc:`uproot.language.python.PythonLanguage` evaluates Python code. It is
the default language.
é    )ÚannotationsNc              
   C  s”   zt  | ¡}W nL tyZ } z4t|jd d|› d|› � |jd ƒ|‚W Y d }~n
d }~0 0 t|jƒdks|t|jd t jƒs�td|› d|› �ƒ‚|S )Nr   z	
in file z
in object é   z%expected a single expression
in file )ÚastÚparseÚSyntaxErrorÚargsÚlenÚbodyÚ
isinstanceÚExpr)Ú
expressionÚ	file_pathÚobject_pathÚnodeÚerr© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/language/python.pyÚ_expression_to_node   s    þý ÿr   c                 C  sL   t | tjƒr2t| jƒ}|d u r"d S |d | j S nt | tjƒrD| jS d S d S )NÚ.)r
   r   Ú	AttributeÚ_attribute_to_dotted_nameÚvalueÚattrÚNameÚid)r   Útmpr   r   r   r   %   s    
r   c                 c  s¦  t | tjƒrˆt | jtjƒrˆ| jj|krˆt| jƒdkrht | jd tjƒrht | jd j	t
ƒrh| jd j	V  ntd|›dt | j¡› �ƒ‚�nt | tjƒrÔ| j|v s¨| j|v r²| jV  n | j|v sÒ| j|krÈn
t| jƒ‚nÎt | tjƒ�r4t| ƒ}|d u �rt| j	||||ƒE d H  n$||v �s"||v �r*|V  nt|ƒ‚nnt | tjƒ�rr| jD ]&}t| |ƒ}t|||||ƒE d H  �qHn0t | tƒ�r¢| D ]}t|||||ƒE d H  �q‚n d S )Nr   r   z3expected a constant string as the only argument of z; found )r
   r   ÚCallÚfuncr   r   r   r   ÚConstantr   ÚstrÚ	TypeErrorÚdumpÚKeyErrorr   r   Ú_walk_ast_yield_symbolsÚASTÚ_fieldsÚgetattrÚlist)r   ÚkeysÚaliasesÚ	functionsÚgetterÚnameÚ
field_nameÚxr   r   r   r#   4   sR    
ÿþ
ýÿþý
ÿÿ

ÿ


r#   c              	     s   t | tjƒr\t | jtjƒr\| jjˆkr\t| jƒdkr\t | jd tjƒr\t | jd j	t
ƒr\| S t | tjƒrÌ| jˆv s|| jˆ v r˜t d| j›d�¡jd j	S | jˆv r¾t d| j›d�¡jd j	S t| jƒ‚�n0t | tjƒ�rht| ƒ}|d u �r0t| j	ˆˆ ˆˆƒ}t || j| j¡}t| ddƒ|_t| ddƒ|_|S |ˆv �sD|ˆ v �r^t d|›d�¡jd j	S t|ƒ‚n”t | tjƒ�rÔg }| jD ]&}	t| |	ƒ}
| t|
ˆˆ ˆˆƒ¡ �q€t| ƒ|Ž }t| ddƒ|_t| ddƒ|_|S t | tƒ�rø‡ ‡‡‡fd	d
„| D ƒS | S d S )Nr   r   zget(ú)z	function[ú]ÚlinenoÚ
col_offsetc                   s   g | ]}t |ˆˆ ˆˆƒ‘qS r   )Ú_ast_as_branch_expression)Ú.0r.   ©r)   r*   r+   r(   r   r   Ú
<listcomp>—   s   z-_ast_as_branch_expression.<locals>.<listcomp>)r
   r   r   r   r   r   r   r   r   r   r   r   r	   r"   r   r   r3   r   Úctxr&   r1   r2   r$   r%   ÚappendÚtyper'   )r   r(   r)   r*   r+   r,   r   Únew_noder   r-   Zfield_valuer   r5   r   r3   g   s`    
ÿþ
ýüûú

ÿ


ÿÿr3   c              
     sà   ˆ |v r‡ ‡‡fdd„S t ˆ ||ƒ}zt|jd j|||ˆƒ}	W nJ tyŠ }
 z2tj|
jd t|ƒt	|ƒ ||d�|
‚W Y d }
~
n
d }
~
0 0 t
 d¡jd j}|	|_t
 |¡‰ t|ddƒˆ _t|ddƒˆ _ttˆ d	d
ƒˆƒS d S )Nc                     s   ˆˆ ˆ ƒS ©Nr   r   ©r   r+   Úscoper   r   Ú<lambda>£   ó    z)_expression_to_function.<locals>.<lambda>r   )r(   r   r   zlambda: Noner1   r   r2   z	<dynamic>Úeval)r   r3   r	   r   r"   ÚuprootÚKeyInFileErrorr   Úsortedr'   r   r   Ú
Expressionr&   r1   r2   r@   Úcompile)r   r(   r)   r*   r+   r=   r   r   r   Úexprr   Úfunctionr   r<   r   Ú_expression_to_functionŸ   s,    ÿüû
rH   c                   s4   d‰ d‰d‰d‰d‰d‰‡ ‡‡‡‡‡‡fdd„}|S )	Ng~ZìÆ OÐ?gi<Ì15Ò¿gWáºU¾ö?g9LW@÷¿g-¡UB„ûð?gŒ{=©@÷Ô?c              	     s|   dt  | ¡ˆ d  }dˆ| ˆ | ˆ | ˆ | ˆ  | t  t  t  | ¡¡¡  }ˆrldt  || ¡ S t  || ¡S d S )Nç      ð?r   )ÚnumpyÚabsoluteÚexpÚnegativeZsquareÚcopysign)ÚvaluesÚtÚy©Za1Za2Úa3Za4Za5Ú
complementÚpr   r   ÚerfÃ   s    ,ÿz_vectorized_erf.<locals>.erfr   )rT   rV   r   rR   r   Ú_vectorized_erf»   s    
rW   c                   s.   d‰ d‰‡ ‡fdd„‰| rˆS ‡fdd„S d S )N)gS¨vŸ†S@g§Ö+W UÀgòÔáð›8@gë’¦~4µó¿g±Mí&ÍS?g1!e�[Ö¾g%Îö“@c                   sâ   | d }|d }t jdd��& |d t  |¡ | }W d   ƒ n1 sH0    Y  d}t jdd��. ˆ D ]}|d }|||  }qhW d   ƒ n1 s–0    Y  t jdd��" |t  ˆ| ¡ W  d   ƒ S 1 sÔ0    Y  d S )NrI   g      @Úignore)Úinvalidg      à?)Údivide)rJ   ZerrstateÚlog)rO   r.   r   ZserZcof)ÚcofsÚstpr   r   ÚlgammaÛ   s    4,z!_vectorized_gamma.<locals>.lgammac                   s   t  ˆ | ƒ¡S r;   )rJ   rL   )rO   )r^   r   r   r>   ë   r?   z#_vectorized_gamma.<locals>.<lambda>r   )Z	logarithmr   )r\   r^   r]   r   Ú_vectorized_gammaÐ   s    r_   Tc                =   @  sX  e Zd ZdZejejejejejejejejej	ej	ej
ejej
ejejejejejejejejejejedƒedƒejejejejdd„ ejejejedƒejejejejej e!ej"ej#ej$ej%ej&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2dd„ ej3ej4dœ<Z5dd
d„Z6dd„ Z7dd„ Z8e9dd„ ƒZ:e9dd„ ƒZ;dd„ Z<dd„ Z=dd„ Z>dS )ÚPythonLanguageaW  
    Args:
        functions (None or dict): Mapping from function name to function, or
            None for ``default_functions``.
        getter (str): Name of the function that extracts branches by name;
            needed for branches whose names are not valid Python symbols.
            Default is "get".

    PythonLanguage is the default :doc:`uproot.language.Language` for
    interpreting expressions passed to
    :ref:`uproot.behaviors.TBranch.HasBranches.arrays` (and similar). This
    interpretation assumes that the expressions have Python syntax and
    semantics, with math functions loaded into the namespace.

    Unlike standard Python, an expression with attributes, such as
    ``some.thing``, can be a single identifier, so that a ``TBranch`` whose
    name contains dots does not need to be loaded with ``get("some.thing")``.
    FTc                 C  s   t  t  tt  | ¡d ƒ¡¡S )Nr   )rJ   ÚroundrL   Ú_lgamma©r.   r   r   r   r>   #  r?   zPythonLanguage.<lambda>c                 C  s   t  t| ƒ¡S r;   )rJ   rL   rb   rc   r   r   r   r>   ?  r?   )<ÚabsrK   ÚacosÚarccosÚacoshÚarccoshÚasinÚarcsinÚasinhÚarcsinhÚatanÚatan2ÚarctanÚarctan2ÚatanhÚarctanhÚcbrtÚceilZconjÚ	conjugaterN   ÚcosÚcoshrV   ÚerfcrL   Úexp2Úexpm1ÚfabsÚ	factorialÚfloorÚfmaxÚfminÚgammaÚhypotÚimagÚisfiniteÚisinfÚisnanr^   r[   Úlog10Úlog1pÚlog2Úlogical_andÚ
logical_orÚnegÚ	nextafterÚrealÚrintra   ÚsignbitÚsinÚsinhÚsqrtÚtanÚtanhZtgammaÚtruncÚwhereNÚgetc                 C  s&   |d u r| j | _n
t|ƒ| _|| _d S r;   )Údefault_functionsÚ
_functionsÚdictÚ_getter)Úselfr*   r+   r   r   r   Ú__init__D  s    

zPythonLanguage.__init__c                 C  s   dS )Nz'uproot.language.python.PythonLanguage()r   ©rœ   r   r   r   Ú__repr__K  s    zPythonLanguage.__repr__c                 C  s
   t |tƒS r;   )r
   r`   )rœ   Úotherr   r   r   Ú__eq__N  s    zPythonLanguage.__eq__c                 C  s   | j S )z@
        Mapping from function name to function (dict).
        )r™   rž   r   r   r   r*   Q  s    zPythonLanguage.functionsc                 C  s   | j S )zŒ
        Name of the function that extracts branches by name; needed for
        branches whose names are not valid Python symbols.
        ©r›   rž   r   r   r   r+   X  s    zPythonLanguage.getterc                 C  s   | j › d|›d�S )z§
        Returns a string, an expression in which the ``getter`` is getting
        ``name`` as a quoted string.

        For example, ``"get('something')"``.
        ú(r/   r¢   )rœ   r,   r   r   r   Ú	getter_of`  s    zPythonLanguage.getter_ofc              
   C  sv   ||v r|gS t |||ƒ}ztt|||| j| jƒƒW S  typ } z$tj|jd ||d�|‚W Y d}~n
d}~0 0 dS )as  
        Args:
            expression (str): The expression to analyze.
            keys (list of str): Names of branches or aliases (for aliases that
                refer to aliases).
            aliases (list of str): Names of aliases.
            file_path (str): File path for error messages.
            object_path (str): Object path for error messages.

        Finds the symbols in the expression that are in ``keys`` or ``aliases``,
        in other words, ``TBranch`` names or alias names. These expressions may
        include dots (attributes). Known ``functions`` and the ``getter`` are
        excluded.
        r   )r   r   N)	r   r'   r#   r™   r›   r"   rA   rB   r   )rœ   r   r(   r)   r   r   r   r   r   r   r   Úfree_symbolsi  s    ÿÿÿþzPythonLanguage.free_symbolsc              
     sv  i ‰t ˆ ƒt ˆƒk r ˆ ˆ }}	n
ˆˆ  }}	|D ]&}
|
|	v r.tj|
›d�tjjdd� q.‡ ‡‡‡‡‡‡fdd„}ˆj|dˆji‰|D ]t\}}|d D ]b}||j }|j	}||urÜ|ˆv r¾|ˆ|< |j
}||ur¦|j	d | }q¦d| }|ˆv r’|ˆ|< q’q‚i }d	}|D ]V\}}|d
 �r|d �st|ˆˆ ˆjˆjˆˆˆƒƒ ||< tj || d¡�rd}�qd}|D ]T\}}|d
 �rd|d �rdt|ˆˆ ˆjˆjˆˆˆƒƒ }tj |d¡�r°d} �qº�qd|du�rr|dk}|�rÜtj ¡ }|D ]�}|�r^t|j|jƒ�r^t|| j|jƒ�s^|| }|j|j	|ji|jj|jgdgd�d� |j¡}|| }||j	 ||< n|| | ||< �qà|S )uÞ  
        Args:
            hasbranches (:doc:`uproot.behaviors.TBranch.HasBranches`): The
                ``TTree`` or ``TBranch`` that is requesting a computation.
            arrays (dict of arrays): Inputs to the computation.
            expression_context (list of (str, dict) tuples): Expression strings
                and a dict of metadata about each.
            keys (set of str): Names of branches or aliases (for aliases that
                refer to aliases).
            aliases (dict of str â†’ str): Names of aliases and their definitions.
            file_path (str): File path for error messages.
            object_path (str): Object path for error messages.

        Computes an array for each expression.
        z# is both an alias and a branch namer   )Ú
stacklevelc              	     s4   | ˆvr,t ˆ |  ˆˆ ˆjˆjˆˆˆƒƒ ˆ| < ˆ|  S r;   )rH   r™   r›   )r,   ©r)   r   r(   r   r=   rœ   rO   r   r   r+   ¯  s    ø

z2PythonLanguage.compute_expressions.<locals>.getterrG   Zbranchesú/FZ
is_primaryZis_cutÚpandasTNr   Úentry)Únames)Úindex)r   ÚwarningsÚwarnrA   Ú
exceptionsZNameConflictWarningr›   r™   Ú	cache_keyr,   ÚparentrH   Z_utilZfrom_moduleÚextrasr©   r
   r¬   Z
MultiIndexZ	DataFramerO   Zfrom_arraysZreindex)rœ   ZhasbranchesZarraysZexpression_contextr(   r)   r   r   ZshorterZlongerr.   r+   Ú_ÚcontextÚbranchÚarrayr,   ÚoutputZ	is_pandasr   Úcutr©   ÚoriginalÚmodifiedÚselectedr   r§   r   Úcompute_expressionsˆ  s¤    
ý
ø

ø



ÿþý

ÿþûz"PythonLanguage.compute_expressions)Nr—   )?Ú__name__Ú
__module__Ú__qualname__Ú__doc__rJ   rK   rf   rh   rj   rl   ro   rp   rr   rs   rt   ru   rN   rv   rw   rW   rL   ry   rz   r{   r}   r~   r   r_   r�   r‚   rƒ   r„   r…   rb   r[   r†   r‡   rˆ   r‰   rŠ   rM   rŒ   r�   rŽ   ra   r�   r�   r‘   r’   r“   r”   r•   r–   r˜   r�   rŸ   r¡   Úpropertyr*   r+   r¤   r¥   r¼   r   r   r   r   r`   ñ   s�   Ä?


	r`   )rÀ   Ú
__future__r   r   r­   rJ   rA   r   r   r#   r3   rH   rW   r_   rb   ÚlanguageZLanguager`   Zpython_languager   r   r   r   Ú<module>   s"   38  