a
    zn„fÀ0  ã                   @  sŠ   d Z ddlmZ ddlZddlZddlmZmZ dZdZ	dZ
dZdd	„ Zd
d„ Zdd„ Zdd„ ZG dd„ dejjjƒZG dd„ deƒZdS )z3
This module defines the behavior of ``TProfile``.
é    )ÚannotationsN)Úboost_axis_metadataÚboost_metadataé   é   é   c                 C  s|   | }t j|t jd�}|}t j|t jd�}t|ƒdks@t|ƒ|krD|S |dk}t jt|ƒt jd�}|| d ||  ||< |S )N©Zdtyper   r   )ÚnumpyÚasarrayÚfloat64ÚlenÚzeros)ÚfBinEntriesÚ	fBinSumw2ÚfNcellsZroot_sumOfWeightsZroot_sumOfWeightSquareZpositiveÚout© r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/behaviors/TProfile.pyÚ_effective_counts_1d   s    r   c                 C  sH   | }t j|t jd�}|dk}t jt|ƒt jd�}|| ||  ||< |S )Nr   r   )r	   r
   r   r   r   )r   Ú	root_contÚroot_sumÚnonzeroÚroot_contsumr   r   r   Ú
_values_1d'   s    r   c                 C  s`   | d u s| t ks| dkrdS | tks,| dkr0dS | tks@| dkrDdS | tksT| dkrXdS dS d S )	NÚ ÚsÚSÚiÚIÚgÚGÚ_)Ú_kERRORMEANÚ_kERRORSPREADÚ_kERRORSPREADIÚ_kERRORSPREADG)Ú
error_moder   r   r   Ú_error_mode_str2   s    r'   c                 C  sb  | d u s| t ks| dkrt } nV| tks.| dkr4t} n@| tksD| dkrJt} n*| tksZ| dkr`t} ntdt| ƒ d ƒ‚|}tj|tjd�}|dk}tj	t
|ƒtjd�}|| ||  ||< | tkròtj	t
|ƒtjd�}	d	t || ¡ |	|< ||	fS |}
|
d u �st
|
ƒ|k�r$tj	t
|ƒtjd�}
ntj|
tjd�}
t|||ƒ}tj	t
|ƒtjd�}t|
| ||  || d
  ƒ||< t |¡}| tk�rtjt
|ƒtjd�}t t
|ƒt d| ¡¡}|dk}|| ||< t || ¡||< tj	t
|ƒtjd�}	|| ||  |	|< ||	fS | tk�r,d|| < ||fS tj	t
|ƒtjd�}	|| t || ¡ |	|< ||	fS )Nr   r   r   r   zòerror_mode must be None/0/'' for error-on-mean,
                        1/'s' for spread (variance),
                        2/'i' for integer spread (using sqrt(12)),
                     or 3/'g' for Gaussian spread
                    not z6see https://root.cern.ch/doc/master/classTProfile.htmlr   r   g      ð?r   é   g        )r"   r#   r$   r%   Ú
ValueErrorÚreprr	   r
   r   r   r   Úsqrtr   ÚabsZonesÚfull)r&   r   r   ÚfSumw2r   r   r   r   r   r   Z	root_err2Z	root_neffZroot_eprim2Z
root_eprimZnumerÚdenomZeprim_nonzeror   r   r   Ú_values_errors_1dA   sb    ûúÿ
ÿ



r0   c                   @  sD   e Zd ZdZedd„ ƒZddd„Zddd„Zdd
d„Zddd„Z	dS )ÚProfilez+
    Abstract class for profile plots.
    c                 C  s   dS )NZMEANr   ©Úselfr   r   r   Úkind‰   s    zProfile.kindFc                 C  s   t t| ƒƒ‚dS )a¥  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

        The effective number of entries, which is a step in the calculation of
        :ref:`uproot.behaviors.TProfile.Profile.values`. The calculation
        of profile errors exactly follows ROOT's "effective entries", but in a
        vectorized NumPy form.
        N©ÚNotImplementedErrorr*   ©r3   Úflowr   r   r   Úcounts�   s    zProfile.countsc                 C  s   t t| ƒƒ‚dS )aX  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

        Mean value of each bin as a 1, 2, or 3 dimensional ``numpy.ndarray`` of
        ``numpy.float64``.

        Setting ``flow=True`` increases the length of each dimension by two.
        Nr5   r7   r   r   r   Úvaluesš   s    zProfile.valuesr   c                 C  s   |   ||¡\}}|S )aj  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.
            error_mode (str): Choose a method for calculating the errors (see below).

        Errors (uncertainties) in the :ref:`uproot.behaviors.TH1.Histogram.values`
        as a 1, 2, or 3 dimensional ``numpy.ndarray`` of ``numpy.float64``.

        The calculation of profile errors exactly follows ROOT's function, but
        in a vectorized NumPy form. The ``error_mode`` may be

        * ``""`` for standard error on the mean
        * ``"s"`` for spread
        * ``"i"`` for integer data
        * ``"g"`` for Gaussian

        following `ROOT's profile documentation <https://root.cern.ch/doc/master/classTProfile.html>`__.

        Setting ``flow=True`` increases the length of each dimension by two.
        )Ú_values_errors©r3   r8   r&   r:   Úerrorsr   r   r   r=   §   s    zProfile.errorsc                 C  s   |   ||¡\}}t |¡S )a¨  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.
            error_mode (str): Choose a method for calculating the errors (see below).

        Variances (uncertainties squared) in the
        :ref:`uproot.behaviors.TH1.Histogram.values` as a 1, 2, or 3
        dimensional ``numpy.ndarray`` of ``numpy.float64``.

        The calculation of profile variances exactly follows ROOT's function, but
        in a vectorized NumPy form. The ``error_mode`` may be

        * ``""`` for standard error on the mean (squared)
        * ``"s"`` for spread (squared)
        * ``"i"`` for integer data (squared)
        * ``"g"`` for Gaussian (squared)

        following `ROOT's profile documentation <https://root.cern.ch/doc/master/classTProfile.html>`__.

        Setting ``flow=True`` increases the length of each dimension by two.
        )r;   r	   Úsquarer<   r   r   r   Ú	variancesÀ   s    zProfile.variancesN)F)F)Fr   )Fr   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr4   r9   r:   r=   r?   r   r   r   r   r1   „   s   



r1   c                   @  sh   e Zd ZdZejjjfZedd„ ƒZ	ddd„Z
edd„ ƒZdd
d„Zddd„Zdd„ Zeefdd„ZdS )ÚTProfilezF
    Behaviors for one-dimensional profiles: ROOT's ``TProfile``.
    c                 C  s   |   d¡fS )NÚfXaxis)Úmemberr2   r   r   r   Úaxesâ   s    zTProfile.axesr   c                 C  s.   |dks|dks|dkr"|   d¡S tdƒ‚d S )Nr   éÿÿÿÿÚxrF   z)axis must be 0 (-1) or 'x' for a TProfile)rG   r)   )r3   Úaxisr   r   r   rK   æ   s    
zTProfile.axisc                 C  s,   | j ddd�}|d u p*t|ƒt|   d¡ƒkS )Nr   T©Znone_if_missingr   )rG   r   )r3   r   r   r   r   Úweightedì   s    zTProfile.weightedTc                 C  s6   t |  d¡|  d¡|  d¡ƒ}|r&|S |dd… S d S )Nr   r   r   r   rI   )r   rG   )r3   r8   r   r   r   r   r9   ñ   s    ýzTProfile.countsFc                 C  sb   t | dƒr| j}n8|  tjjj¡\}tj|tj	d�}t
|  d¡|ƒ}|| _|rR|S |dd… S d S )NÚ_valuesr   r   r   rI   )ÚhasattrrN   ÚbaseÚuprootÚmodelsÚTArrayÚModel_TArrayr	   r
   r   r   rG   )r3   r8   r:   r   r   r   r   r:   ü   s    
þzTProfile.valuesc                 C  sØ   dt |ƒ }t| |ƒr(| j}t| |ƒ}nˆ|  tjjj¡\}t	j
|t	jd�}| jddd�}|d urjt	 
|¡}t|t	 
|  d¡¡|||  d¡t	 
|  d¡¡ƒ\}}|| _t| ||ƒ |r¼||fS |d	d
… |d	d
… fS d S )NÚ_errorsr   r.   TrL   r   r   r   r   rI   )r'   rO   rN   ÚgetattrrP   rQ   rR   rS   rT   r	   r
   r   rG   r0   Úsetattr)r3   r8   r&   Úattrr:   r=   r   r.   r   r   r   r;     s,    

úzTProfile._values_errorsc                 C  sn  t j ¡ }| jdd�}|  d|  d¡¡\}}| jd jd }t |¡}t 	|  d¡¡}	|j
 ¡ }
t jj |  d¡|¡}|j||
d�}| ¡ D ]\}}t|||  |¡ƒ qŒt||jjƒræ|d	d … }|d	d … }|d	d … }|	d	d … }	d
|  d
¡i|_|jdd�}tjddd�� |	d | }W d   ƒ n1 �s40    Y  |	|d< ||d< ||d< ||	||	   |d< |S )NT)r8   Z
fErrorModer   rI   r   rF   )Ústorager   r.   Úignore)ÚdivideÚinvalidr   Zsum_of_weightsZsum_of_weights_squaredÚvalueZ_sum_of_weighted_deltas_squared)rQ   ÚextrasÚboost_histogramr9   r;   rG   Z_basesr	   r>   r
   rY   ZWeightedMeanÚ	behaviorsÚTH1Z_boost_axisÚ	HistogramÚitemsrW   Ú
isinstancerK   ZStrCategoryÚmetadataÚviewZerrstate)r3   re   Zaxis_metadatar_   Zeffective_countsr!   r=   r:   r?   Zsum_of_bin_weightsrY   Zxaxisr   ÚkÚvrf   Zsum_of_bin_weights_squaredr   r   r   Úto_boost(  s6    


,
ÿzTProfile.to_boostN)r   )T)F)r@   rA   rB   rC   rQ   r`   ra   Z
no_inheritrD   rH   rK   rM   r9   r:   r;   r   r   ri   r   r   r   r   rE   Û   s   




rE   )rC   Ú
__future__r   r	   rQ   Zuproot.behaviors.TH1r   r   r"   r#   r$   r%   r   r   r'   r0   r`   ra   rb   r1   rE   r   r   r   r   Ú<module>   s   CW