a
    zn„fH/  ã                   @  s^   d Z ddlmZ ddlZddlZdddœZdddœZdd„ ZG d	d
„ d
ƒZG dd„ deƒZ	dS )zt
This module defines the behaviors of ``TH1`` and its subclasses (not including ``TH2``,
``TH3``, or ``TProfile``).
é    )ÚannotationsNÚfNameÚfTitle)ÚnameÚlabelc                 C  sü   t j ¡ }|  d¡}| jddd�}| jddd�}|d ur†zdd„ |D ƒ}|jj}W n( tyz   dd„ |D ƒ}|jj}Y n0 ||ƒ}nN|d u sžt|ƒ|d	 krÂ|jj	||  d
¡|  d¡ddd�}n|jj
|ddd�}| ¡ D ]\}	}
t||	|  |
¡ƒ qÜ|S )NÚfNbinsÚfXbinsT©Znone_if_missingÚfLabelsc                 S  s   g | ]}t |ƒ‘qS © )Úint©Ú.0Úxr   r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/behaviors/TH1.pyÚ
<listcomp>   ó    z_boost_axis.<locals>.<listcomp>c                 S  s   g | ]}t |ƒ‘qS r   )Ústrr   r   r   r   r      r   é   ZfXminZfXmax)Z	underflowÚoverflow)ÚuprootÚextrasÚboost_histogramÚmemberÚaxisÚIntCategoryÚ
ValueErrorÚStrCategoryÚlenZRegularÚVariableÚitemsÚsetattr)r   Úmetadatar   r   r   r
   ÚlabelsZcategory_clsÚoutÚkÚvr   r   r   Ú_boost_axis   s0    


û	r'   c                   @  s¨   e Zd ZdZedd„ ƒZedd„ ƒZdd„ Zdd	„ Zed
d„ ƒZ	dd„ Z
edd„ ƒZedd„ ƒZd"dd„Zd#dd„Zd$dd„Zd%dd„Zd&dd„Zd'dd„Zd d!„ ZdS )(Ú	Histogramz(
    Abstract class for histograms.
    c                 C  s
   |   d¡S )z,
        The name of the histogram.
        r   ©r   ©Úselfr   r   r   r   7   s    zHistogram.namec                 C  s
   |   d¡S )z-
        The title of the histogram.
        r   r)   r*   r   r   r   Útitle>   s    zHistogram.titlec                 C  s`   t | ƒt |ƒkrdS | j|jkr$dS |  d¡\}}| d¡\}}t ||¡}t ||¡}|o^|S )z‚
        Two histograms are equal if their axes are equal, their values are equal,
        and their variances are equal.
        FT)ÚtypeÚaxesÚ_values_variancesÚnumpyZarray_equal)r+   ÚotherZself_valuesZself_variancesZother_valuesZother_variancesZvalues_equalZvariances_equalr   r   r   Ú__eq__E   s    zHistogram.__eq__c                 C  s   |   |¡ S )zL
        Some versions of Python don't automatically negate __eq__.
        )r2   )r+   r1   r   r   r   Ú__ne__T   s    zHistogram.__ne__c                 C  s   t t| ƒƒ‚dS )zM
        A tuple of all :doc:`uproot.behaviors.TAxis.TAxis` objects.
        N©ÚNotImplementedErrorÚreprr*   r   r   r   r.   Z   s    zHistogram.axesc                 C  s   t t| ƒƒ‚dS )a#  
        Returns a specified :doc:`uproot.behaviors.TAxis.TAxis` object.

        The ``axis`` can be specified as

        * a non-negative integer: ``0`` is the first axis, ``1`` is the second,
          and ``2`` is the third.
        * a negative integer: ``-1`` is the last axis, ``-2`` is the
          second-to-last, and ``-3`` is the third-to-last.
        * a string: ``"x"`` is the first axis, ``"y"`` is the second, and ``"z"``
          is the third

        (assuming that the histogram dimension supports a given ``axis``).
        Nr4   ©r+   r   r   r   r   r   a   s    zHistogram.axisc                 C  s0   |   d¡}|duo.t|ƒdko.t|ƒ|   d¡kS )zR
        True if the histogram has weights (``fSumw2``); False otherwise.
        ÚfSumw2Nr   ÚfNcells)r   r   )r+   Úsumw2r   r   r   Úweightedr   s    

ÿýzHistogram.weightedc                 C  s   t t| ƒƒ‚dS )z‰
        The meaning of this object: ``"COUNT"`` for true histograms (TH*) and
        ``"MEAN"`` for profile plots (TProfile*).
        Nr4   r*   r   r   r   Úkind~   s    zHistogram.kindFc                 C  s   t t| ƒƒ‚dS )az  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

        Bin contents as a 1, 2, or 3 dimensional ``numpy.ndarray``. The
        ``numpy.dtype`` of this array depends on the histogram type.

        Setting ``flow=True`` increases the length of each dimension by two.
        Nr4   ©r+   Úflowr   r   r   Úvalues†   s    zHistogram.valuesc                 C  s   |   |¡\}}t |¡S )a@  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

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

        If ``fSumw2`` (weights) are available, they will be used in the
        calculation of the errors. If not, errors are assumed to be the square
        root of the values.

        Setting ``flow=True`` increases the length of each dimension by two.
        )r/   r0   Úsqrt©r+   r>   r?   Ú	variancesr   r   r   Úerrors“   s    zHistogram.errorsc                 C  s   |   |¡\}}|S )aO  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

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

        If ``fSumw2`` (weights) are available, they will be used in the
        calculation of the variances. If not, variances are assumed to be equal
        to the values.

        Setting ``flow=True`` increases the length of each dimension by two.
        )r/   rA   r   r   r   rB   ¥   s    zHistogram.variancesc                 C  s   | j |d�S )a=  
        Args:
            flow (bool): If True, include underflow and overflow bins before and
                after the normal (finite-width) bins.

        Returns the (possibly weighted) number of entries in each bin. For
        histograms, this is equal to :ref:`uproot.behaviors.TH1.Histogram.values`.
        ©r>   )r?   r=   r   r   r   Úcounts¸   s    	zHistogram.countsNc                   s°  t ˆjƒdksJ dƒ‚ˆ du r"t‰ |du r.t}tj ¡ }d}ˆjr^ˆjdd�\}}|j	 
¡ }n2ˆjdd�}t|jjtjƒr†|j	 ¡ }n
|j	 ¡ }‡ ‡fdd„g d¢d	t ˆjƒ… D ƒ}|j|d
|iŽ}| ¡ D ]\}	}
t||	ˆ |
¡ƒ qÎt |jƒt |ƒk�sJ dƒ‚t|ƒD ]Z\}}t||jj|jjfƒ�s2�qtdƒf| tddƒf }|| }|du�r|| }�q|jdd�}|du�r¤|j|jk�s–J dƒ‚||_||_ n||d< |S )uA  
        Args:
            metadata (dict of str â†’ str): Metadata to collect (keys) and
                their C++ class member names (values).
            axis_metadata (dict of str â†’ str): Metadata to collect from
                each axis.

        Converts the histogram into a ``boost-histogram`` object.
        é   z,Only 1D, 2D, and 3D histograms are supportedNTrD   c                   s   g | ]}t ˆ |¡ˆ ƒ‘qS r   )r'   r   )r   r   ©Úaxis_metadatar+   r   r   r   á   s   ÿz&Histogram.to_boost.<locals>.<listcomp>)ÚfXaxisZfYaxisZfZaxisr   Ústoragez.Number of dimensions must match number of axesr   z2weights (fSumw2) and values should have same shape.)!r   r.   Úboost_axis_metadataÚboost_metadatar   r   r   r;   r/   rJ   ZWeightr?   Ú
issubclassÚdtyper-   r0   ÚintegerZInt64ÚDoubler(   r    r!   r   ÚshapeÚ	enumerateÚ
isinstancer   r   r   ÚsliceÚviewÚvalueZvariance)r+   r"   rH   r   r:   r?   rJ   r.   r$   r%   r&   Úir   ZslicerrU   r   rG   r   Úto_boostÃ   sZ    


þ
ÿ
þþ


ÿþzHistogram.to_boostc                 C  s2   |du rt }|du rt}tj ¡  | j||d�¡S )u6  
        Args:
            metadata (dict of str â†’ str): Metadata to collect (keys) and
                their C++ class member names (values).
            axis_metadata (dict of str â†’ str): Metadata to collect from
                each axis.

        Converts the histogram into a ``hist`` object.
        N)r"   rH   )rK   rL   r   r   ÚhistZHistrX   )r+   r"   rH   r   r   r   Úto_hist  s    

ÿzHistogram.to_histc                 C  s   |   ¡ S )N)rX   r*   r   r   r   Ú_to_boost_histogram_  s    zHistogram._to_boost_histogram_)F)F)F)F)NN)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr   r,   r2   r3   r.   r   r;   r<   r?   rC   rB   rE   rX   rZ   r[   r   r   r   r   r(   2   s*   









@
r(   c                   @  sN   e Zd ZdZedd„ ƒZddd„Zedd„ ƒZdd
d„Zdd„ Z	ddd„Z
dS )ÚTH1zš
    Behaviors for one-dimensional histograms: descendants of ROOT's
    ``TH1``, not including ``TProfile``, ``TH2``, ``TH3``, or their descendants.
    c                 C  s   |   d¡fS )NrI   r)   r*   r   r   r   r.   !  s    zTH1.axesr   c                 C  s.   |dks|dks|dkr"|   d¡S tdƒ‚d S )Nr   éÿÿÿÿr   rI   z$axis must be 0 (-1) or 'x' for a TH1)r   r   r7   r   r   r   r   %  s    
zTH1.axisc                 C  s   dS )NZCOUNTr   r*   r   r   r   r<   +  s    zTH1.kindFc                 C  sX   t | dƒr| j}n.|  tjjj¡\}tj||j	 
d¡d�}|| _|rH|S |dd… S d S )NÚ_valuesú=©rN   r   rb   )Úhasattrrc   Úbaser   ÚmodelsZTArrayZModel_TArrayr0   ÚasarrayrN   Únewbyteorder)r+   r>   r?   r   r   r   r?   /  s    
z
TH1.valuesc                 C  sÔ   | j dd�}t| dƒr| j}nŽtj|jtjd�}| jddd�}|d ur’t|ƒ|  d¡kr’tj	||j
 d¡d�}t ||j¡}|d	k}|| ||< n|d	k}|| ||< || _|r¸||fS |d
d… |d
d… fS d S )NTrD   Ú
_variancesre   r8   r	   r9   rd   r   r   rb   )r?   rf   rk   r0   ZzerosrQ   Zfloat64r   r   ri   rN   rj   Zreshape)r+   r>   r?   rB   r:   Zpositiver   r   r   r/   <  s     
zTH1._values_variancesc                 C  s8   | j |d�}|  d¡j|d�}|r,||ffS ||fS dS )a´  
        Args:
            flow (bool): If True, include underflow and overflow bins; otherwise,
                only normal (finite-width) bins are included.
            dd (bool): If True, the return type follows
                `numpy.histogramdd <https://numpy.org/doc/stable/reference/generated/numpy.histogramdd.html>`__;
                otherwise, it follows `numpy.histogram <https://numpy.org/doc/stable/reference/generated/numpy.histogram.html>`__
                and `numpy.histogram2d <https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html>`__.

        Converts the histogram into a form like the ones produced by the NumPy
        histogram functions.
        rD   r   N)r?   r   Úedges)r+   r>   Úddr?   Zxedgesr   r   r   Úto_numpyS  s
    
zTH1.to_numpyN)r   )F)FF)r\   r]   r^   r_   r`   r.   r   r<   r?   r/   rn   r   r   r   r   ra     s   



ra   )
r_   Ú
__future__r   r0   r   rL   rK   r'   r(   ra   r   r   r   r   Ú<module>   s   

! j