a
    zn„fÀÞ  ã                   @  sB  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZ d dlmZ z(d dlmZmZ d dlmZmZmZ W n. ey®   d dlmZmZmZmZmZ Y n0 d dlZd dlZd d	lmZmZ d d
lmZmZmZ e�rd dlm Z  d dl!m"Z" d dl#m$Z% eeeddeedddddddddddœdd„Z&G dd„ dƒZ'G dd„ de	ƒZ(ddddœdd„Z)G dd„ dƒZ*G dd„ dƒZ+dd „ Z,d!d"„ Z-G d#d$„ d$eƒZ.G d%d&„ d&eƒZ/G d'd(„ d(e.ƒZ0G d)d*„ d*e/ƒZ1ed+ƒZ2G d,d-„ d-ƒZ3d.d/„ Z4d0d1„ Z5d2d3„ Z6G d4d5„ d5e3ƒZ7G d6d7„ d7e3ƒZ8d8d9„ Z9d:d;„ Z:d<d=„ Z;dS )>é    )ÚannotationsN)ÚCallableÚIterableÚMapping)ÚExecutor)ÚSourcePerformanceCounters)ÚTYPE_CHECKINGÚFinal)ÚAnyÚProtocolÚTypeVar)r   r
   r	   r   r   )Ú	no_filterÚunset)ÚHasBranchesÚTBranchÚ_regularize_step_size)ÚTypeTracerReport)ÚForm)ÚArrayTFÚak)Úfilter_nameÚfilter_typenameÚfilter_branchÚ	recursiveÚ
full_pathsÚ	step_sizeÚsteps_per_fileÚlibraryÚ
ak_add_docÚcustom_classesÚallow_missingÚ
open_filesÚform_mappingÚallow_read_errors_with_reportÚknown_base_formÚdecompression_executorÚinterpretation_executorc                K  sú  t jj| fddi|¤Ž} dd„ | D ƒ}t|ƒr^t|ƒs>tdƒ‚|turNtdƒ‚|tur^tdƒ‚t jj 	|¡}|tur’|tur’td|›d	|›�ƒ‚n,|tur°|s¾td
|›�ƒ‚q¾n|turºnd}|durÒ|rÒtdƒ‚|j
dkrâtƒ ‚|j
dkrú|durútƒ ‚| ¡ }d|v�rd|d< d|v�r&d|d< t j |¡}d|	i}|j
dk�r�|�rnt| |||||||
||||||ƒS t| ||||||
||||||ƒS nf|j
dk�rð|�rÈt| |||||||
||||||||ƒS t| ||||||
|||||||||ƒS ntƒ ‚dS )u,  
    Args:
        files: See below.
        filter_name (None, glob string, regex string in ``"/pattern/i"`` syntax, function of str â†’ bool, or iterable of the above): A
            filter to select ``TBranches`` by name.
        filter_typename (None, glob string, regex string in ``"/pattern/i"`` syntax, function of str â†’ bool, or iterable of the above): A
            filter to select ``TBranches`` by type.
        filter_branch (None or function of :doc:`uproot.behaviors.TBranch.TBranch` â†’ bool, :doc:`uproot.interpretation.Interpretation`, or None): A
            filter to select ``TBranches`` using the full
            :doc:`uproot.behaviors.TBranch.TBranch` object. If the function
            returns False or None, the ``TBranch`` is excluded; if the function
            returns True, it is included with its standard
            :ref:`uproot.behaviors.TBranch.TBranch.interpretation`; if an
            :doc:`uproot.interpretation.Interpretation`, this interpretation
            overrules the standard one.
        recursive (bool): If True, include all subbranches of branches as
            separate fields; otherwise, only search one level deep.
        full_paths (bool): If True, include the full path to each subbranch
            with slashes (``/``); otherwise, use the descendant's name as
            the field name.
        step_size (int or str): If an integer, the maximum number of entries to
            include in each chunk/partition; if a string, the maximum memory_size to include
            in each chunk/partition. The string must be a number followed by a memory unit,
            such as "100 MB". Mutually incompatible with steps_per_file: only set
            step_size or steps_per_file, not both. Cannot be used with
            ``open_files=False``.
        steps_per_file (int, default 1):
            Subdivide files into the specified number of chunks/partitions. Mutually incompatible
            with step_size: only set step_size or steps_per_file, not both.
            If both ``step_size`` and ``steps_per_file`` are unset,
            ``steps_per_file``'s default value of 1 (whole file per chunk/partition) is used,
            regardless of ``open_files``.
        library (str or :doc:`uproot.interpretation.library.Library`): The library
            that is used to represent arrays. If ``library='np'`` it returns a dict
            of dask arrays and if ``library='ak'`` it returns a single dask-awkward
            array. ``library='pd'`` has not been implemented yet and will raise a
            ``NotImplementedError``.
        ak_add_doc (bool): If True and ``library="ak"``, add the TBranch ``title``
            to the Awkward ``__doc__`` parameter of the array.
        custom_classes (None or dict): If a dict, override the classes from
            the :doc:`uproot.reading.ReadOnlyFile` or ``uproot.classes``.
        allow_missing (bool): If True, skip over any files that do not contain
            the specified ``TTree``.
        open_files (bool): If True (default), the function will open the files to read file
            metadata, i.e. only the main data read is delayed till the compute call on
            the dask collections. If False, the opening of the files and reading the
            metadata is also delayed till the compute call. In this case, branch-names
            are inferred by opening only the first file.
        form_mapping (Callable[awkward.forms.Form] -> awkward.forms.Form | None): If not none
            and library="ak" then apply this remapping function to the awkward form of the input
            data. The form keys of the desired form should be available data in the input form.
        allow_read_errors_with_report (bool or tuple of exceptions): If True, catch OSError
            exceptions and return an empty array for these nodes in the task graph. If a tuple,
            catch any of those exceptions and return empty arrays for those nodes. In either of
            those cases, The return of this function becomes a two element tuple, where the
            first return is the dask-awkward collection of interest and the second return is a
            report dask-awkward collection.
        known_base_form (awkward.forms.Form | None): If not none use this form instead of opening
            one file to determine the dataset's form. Only available with open_files=False.
        decompression_executor (None or Executor with a ``submit`` method): The
            executor that is used to decompress ``TBaskets``; if None, a
            :doc:`uproot.source.futures.TrivialExecutor` is created.
            Executors attached to a file are ``shutdown`` when the file is closed.
        interpretation_executor (None or Executor with a ``submit`` method): The
            executor that is used to interpret uncompressed ``TBasket`` data as
            arrays; if None, a :doc:`uproot.source.futures.TrivialExecutor`
            is created.
            Executors attached to a file are ``shutdown`` when the file is closed.
        options: See below.

    Returns dask equivalents of the backends supported by uproot. If ``library='np'``,
    the function returns a Python dict of dask arrays. If ``library='ak'``, the
    function returns a single dask-awkward array.

    For example:

    .. code-block:: python

        >>> uproot.dask(root_file)
        dask.awkward<from-uproot, npartitions=1>
        >>> uproot.dask(root_file,library='np')
        {'Type': dask.array<Type-from-uproot, shape=(2304,), dtype=object, chunksize=(2304,), chunktype=numpy.ndarray>, ...}

    Allowed types for the ``files`` parameter:

    * str/bytes: relative or absolute filesystem path or URL, without any colons
      other than Windows drive letter or URL schema.
      Examples: ``"rel/file.root"``, ``"C:\abs\file.root"``, ``"http://where/what.root"``
    * str/bytes: same with an object-within-ROOT path, separated by a colon.
      Example: ``"rel/file.root:tdirectory/ttree"``
    * pathlib.Path: always interpreted as a filesystem path or URL only (no
      object-within-ROOT path), regardless of whether there are any colons.
      Examples: ``Path("rel:/file.root")``, ``Path("/abs/path:stuff.root")``
    * glob syntax in str/bytes and pathlib.Path.
      Examples: ``Path("rel/*.root")``, ``"/abs/*.root:tdirectory/ttree"``
    * dict: keys are filesystem paths, values are objects-within-ROOT paths.
      Example: ``{"/data_v1/*.root": "ttree_v1", "/data_v2/*.root": "ttree_v2"}``
    * dict: keys are filesystem paths, values are dicts containing objects-within-ROOT and
      steps (chunks/partitions) as a list of starts and stops or steps as a list of offsets
      Example:

          {{"/data_v1/tree1.root": {"object_path": "ttree_v1", "steps": [[0, 10000], [15000, 20000], ...]},
            "/data_v1/tree2.root": {"object_path": "ttree_v1", "steps": [0, 10000, 20000, ...]}}}

      (This ``files`` pattern is incompatible with ``step_size`` and ``steps_per_file``.)
    * already-open TTree objects.
    * iterables of the above.

    Options (type; default):

    * handler (:doc:`uproot.source.chunk.Source` class; None)
    * timeout (float for HTTP, int for XRootD; 30)
    * max_num_elements (None or int; None)
    * num_workers (int; 1)
    * use_threads (bool; False on the emscripten platform (i.e. in a web browser), else True)
    * num_fallback_workers (int; 10)
    * begin_chunk_size (memory_size; 403, the smallest a ROOT file can be)
    * minimal_ttree_metadata (bool; True)

    Other file entry points:

    * :doc:`uproot.reading.open`: opens one file to read any of its objects.
    * :doc:`uproot.behaviors.TBranch.iterate`: iterates through chunks of
      contiguous entries in ``TTrees``.
    * :doc:`uproot.behaviors.TBranch.concatenate`: returns a single
      concatenated array from ``TTrees``.
    * :doc:`uproot._dask.dask` (this function): returns an unevaluated Dask
      array from ``TTrees``.
    Zsteps_allowedTc                 S  s   g | ]}t |ƒd k‘qS )é   )Úlen)Ú.0Úx© r+   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/_dask.pyÚ
<listcomp>¹   ó    zdask.<locals>.<listcomp>z?partition sizes for some but not all 'files' have been assignedz<partition sizes for 'files' is incompatible with 'step_size'zApartition sizes for 'files' is incompatible with 'steps_per_file'zLonly 'step_size' or 'steps_per_file' should be set, not both; got step_size=z and steps_per_file=z>'step_size' should not be set when 'open_files' is False; got é   Nz2known_base_form must be None if open_files is TrueÚpdr   Znum_workersZnum_fallback_workersr   Únp)ÚuprootÚ_utilZregularize_filesÚanyÚallÚ	TypeErrorr   Úinterpretationr   Z_regularize_libraryÚnameÚNotImplementedErrorÚcopyZregularize_filterÚ_get_dask_arrayÚ_get_dask_array_delay_openÚ_get_dak_arrayÚ_get_dak_array_delay_open)Úfilesr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   ÚoptionsZis_3argÚreal_optionsÚinterp_optionsr+   r+   r,   Údask   sæ     ÿÿÿÿÿ


òóððrC   c                   @  s*   e Zd ZdZddddœdd„Zd	d
„ ZdS )Ú_PackedArgCallablezyWrap a callable such that packed arguments can be unrolled.
    Inspired by dask.dataframe.io.io._PackedArgCallable.
    NFr   Úbool)ÚfuncÚpackedc                 C  s   || _ || _|| _|| _d S ©N)rF   ÚargsÚkwargsrG   )ÚselfrF   rI   rJ   rG   r+   r+   r,   Ú__init__C  s    z_PackedArgCallable.__init__c                 C  s2   | j s|f}| jg |¢| jpg ¢R i | jp,i ¤ŽS rH   )rG   rF   rI   rJ   )rK   Z
packed_argr+   r+   r,   Ú__call__O  s    ÿþýz_PackedArgCallable.__call__)NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rL   rM   r+   r+   r+   r,   rD   >  s      ûrD   c                   @  sF   e Zd ZdZddœdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )Ú_LazyInputsDictz„Dictionary with lazy key value pairs
    Parameters
    ----------
    inputs : list[Any]
        The list of dicionary values.
    ÚNone©Úreturnc                 K  s   || _ || _d S rH   )ÚinputsrJ   )rK   rV   rJ   r+   r+   r,   rL   a  s    z_LazyInputsDict.__init__c                 C  s
   t | jƒS rH   )r(   rV   ©rK   r+   r+   r,   Ú__len__e  s    z_LazyInputsDict.__len__c                   s   ‡ fdd„ˆ   ¡ D ƒS )Nc                 3  s   | ]}ˆ | V  qd S rH   r+   ©r)   ÚkrW   r+   r,   Ú	<genexpr>i  r.   z+_LazyInputsDict.__iter__.<locals>.<genexpr>)ÚkeysrW   r+   rW   r,   Ú__iter__h  s    z_LazyInputsDict.__iter__c                 C  s   | j |d  S ©Nr   )rV   )rK   Úir+   r+   r,   Ú__getitem__k  s    z_LazyInputsDict.__getitem__c                 C  s8   t |tƒr4t |d tƒr4|d dko2|d t| ƒk S dS )Nr   F)Ú
isinstanceÚtupleÚintr(   )rK   rZ   r+   r+   r,   Ú__contains__n  s    z_LazyInputsDict.__contains__c                 C  s   dd„ t t| jƒƒD ƒS )Nc                 s  s   | ]}|fV  qd S rH   r+   )r)   r_   r+   r+   r,   r[   t  r.   z'_LazyInputsDict.keys.<locals>.<genexpr>)Úranger(   rV   rW   r+   r+   r,   r\   s  s    z_LazyInputsDict.keysN)
rN   rO   rP   rQ   rL   rX   r]   r`   rd   r\   r+   r+   r+   r,   rR   Y  s   rR   )rI   ÚlabelÚtokenc             
   O  s  t j ¡ }t j ¡ }	t| ƒs$tdƒ‚tƒ }
t|ƒ}t|ƒD ]l\}}t	|t
ƒs^tdt|ƒ› �ƒ‚z|
 t|ƒ¡ W q: ttfy¤   t|ƒ||< |
 t|| ƒ¡ Y q:0 q:t|
ƒdkr¾tdƒ‚nt|
ƒdkrÒtdƒ‚|
dhkrätdƒ‚| dd	¡}|�st|ƒdk�r,t|ƒdk�rtd
ƒ‚t|d ƒ}d	}ntt|Ž ƒ}d}|�pL|j | ¡}|�ph|jj| |fi |¤Ž}|› d|› �}|�sŠ|�sŠ|�rœt| |||d�}n| }|jjt|ƒ|d�}|jj|d|||j d¡fi|dfgi d d�}|jj ||¡}|	jj ||||d�S )Nz"`func` argument must be `callable`z2All elements of `iterables` must be Iterable, got r   z/`from_map` requires at least one Iterable inputr/   z)All `iterables` must have the same lengthz+All `iterables` must have a non-zero lengthÚproduces_tasksFz9Multiple iterables not supported when produces_tasks=TrueTú-)rI   rJ   rG   )Úmappingrh   r_   )ÚoutputZoutput_indicesÚdskÚindicesZ	numblocksr   )Údtype)!r2   ÚextrasrC   Z
dask_arrayÚcallableÚ
ValueErrorÚsetÚlistÚ	enumeratera   r   ÚtypeÚaddr(   ÚAttributeErrorr6   ÚpopÚzipÚutilsÚfuncnameÚbaseÚtokenizerD   Z	blockwiseZBlockwiseDepDictrR   Z	BlockwiseZblockwise_tokenZhighlevelgraphZHighLevelGraphZfrom_collectionsÚcorer   )rF   Úchunksrn   rI   rf   rg   Ú	iterablesrJ   rC   ÚdaÚlengthsZitersr_   Úiterablerh   rV   rG   r8   Zio_funcZ
io_arg_maprl   Zhlgr+   r+   r,   Ú_dask_array_from_mapw  sr    



ÿ

ÿüþú	r„   c                   @  s$   e Zd Zdddœdd„Zdd„ ZdS )	Ú_UprootReadNumpyNrS   rT   c                 C  s"   || _ || _|| _|| _|| _d S rH   )ÚttreesÚkeyrB   r%   r&   )rK   r†   r‡   rB   r%   r&   r+   r+   r,   rL   Ï  s
    z_UprootReadNumpy.__init__c                 C  s6   |\}}}| j | | j j||d| jd | j| jd�S )Nr1   r   )Úentry_startÚ
entry_stopr   r   r%   r&   )r†   r‡   ÚarrayrB   r%   r&   )rK   Úi_start_stopr_   ÚstartÚstopr+   r+   r,   rM   Ý  s    
úz_UprootReadNumpy.__call__)NN©rN   rO   rP   rL   rM   r+   r+   r+   r,   r…   Î  s     úr…   c                   @  s   e Zd Zddd„Zdd„ ZdS )Ú_UprootOpenAndReadNumpyNc                 C  s.   || _ || _|| _|| _|| _|| _|| _d S rH   )r   r    rA   r‡   rB   r%   r&   )rK   r   r    rA   r‡   rB   r%   r&   r+   r+   r,   rL   ê  s    
z _UprootOpenAndReadNumpy.__init__c                 C  sê   |\}}}}}t j ||| j| j| j¡}|j}|| }	}
|sht || ¡}|| t	|d | |ƒ }	}
n\d|	  kr||k r–n nd|
  kr”|ksÄn t
d|	› d|
› d|jj› d|j› d|› d�ƒ‚|| j jd	|	|
| jd
 | j| jd�S )Nr/   r   úexplicit entry start (ú) or stop (úC) from uproot.dask 'files' argument is out of bounds for file

    ú

TTree in path

    ú

which has ú entriesr1   r   )r   rˆ   r‰   r   r%   r&   )r2   r3   Úregularize_object_pathr   r    rA   Únum_entriesÚmathÚceilÚminrq   ÚfileÚ	file_pathÚobject_pathr‡   rŠ   rB   r%   r&   )rK   Z*file_path_object_path_istep_nsteps_ischunkrœ   r�   Zistep_or_startZnsteps_or_stopZischunkÚttreer—   rŒ   r�   Zevents_per_stepsr+   r+   r,   rM   ü  sN    úû
ÿ
.þúøÿ
úz _UprootOpenAndReadNumpy.__call__)NNrŽ   r+   r+   r+   r,   r�   é  s     ø
r�   c           ,        sô  g }g }d }g }d}| D ]ð}|dd… \}}t j |||||	¡}|d ur|d7 }t|tƒr’t|jdd�ƒdkr’|‰|j}| d¡ ‡ ‡fdd„}n| d¡ ˆ }| |¡ t|ƒd	krÆ| |d ¡ nd }|j|||||dd
�‰|d u rîˆ}qt	ˆƒ‰‡fdd„|D ƒ}q|t
u �rX|t
u�s"J ‚t|ƒ}tdd„ |D ƒƒ}tdtt |||  ¡ƒƒ}|dk�r€td d dd„ | D ƒ¡¡ƒ‚t|ƒdk�s¢t|ƒ�sÀt|ƒ�rÀtd d dd„ | D ƒ¡¡ƒ‚i }d}|D ]P}d}|j}i }|D ]} ||  }!|!j||!j< �qât|||||ƒ}"|t|"ƒ7 }�qÌtt|t|ƒ ƒƒ}#|#dk�s@J ‚|D �]¨} |d |  jj}$|$jd u �rld}%n
|$j\}$}%g }&g }'t|ƒD �]\}(}d}|j}|d u �rôt|||#ƒD ]@})t|)|# |ƒ}*|*|) }+|+dk�r°|& |+¡ |' |(|)|*f¡ �q°n¤||( D ]š\})}*d|)  k�r|k �r:n nd|*  k�r8|k�shn td|)› d|*› d|jj › d|j!› d|› d�ƒ‚|*|) }+|+dk�rü|& |+¡ |' |(|)|*f¡ �qü�q†t|'ƒdk�r¾|& d¡ |' d¡ t"t#|| |
||ƒ|'t$|&ƒf|$| › d�d�|| < �qD|S )Nr   é   r/   T©r   c                   s   | ˆu oˆ | ƒS rH   r+   ©Úbranch©r   Úoriginalr+   r,   Úreal_filter_branchN  s    z+_get_dask_array.<locals>.real_filter_branchFr'   ©r   r   r   r   r   Zignore_duplicatesc                   s   g | ]}|ˆ v r|‘qS r+   r+   ©r)   r‡   ©Únew_keysr+   r,   r-   h  r.   z#_get_dask_array.<locals>.<listcomp>c                 s  s   | ]}|j V  qd S rH   ©r—   ©r)   rž   r+   r+   r,   r[   n  r.   z"_get_dask_array.<locals>.<genexpr>ú1allow_missing=True and no TTrees found in

    {}ú
    c                 s  sD   | ]<\}}d t |tƒr|jn|›dt |tƒr2|jn|›d�V  qdS ©Ú{z: Ú}N©ra   r   rœ   r�   ©r)   ÚfÚor+   r+   r,   r[   v  s   ÿú.TTrees in

    {}

have no TBranches in commonc                 s  sD   | ]<\}}d t |tƒr|jn|›dt |tƒr2|jn|›d�V  qdS r®   r±   r²   r+   r+   r,   r[   €  s   ÿr+   r�   r‘   r’   r“   r”   r•   ©r   r   r   ú-from-uproot©r   rn   rf   )%r2   r3   r–   ra   r   r(   r\   ÚparentÚappendrr   r   ÚsumÚmaxrc   r˜   r™   rq   ÚformatÚjoinr5   r4   r—   r7   Ú	cache_keyr   ÚroundÚnumpy_dtypeÚsubdtypert   re   rš   r›   rœ   r�   r„   r…   rb   ),r?   r   r   r   r   r   r   r   r    rA   rB   r   r%   r&   r†   Úexplicit_chunksÚcommon_keysÚis_selfÚcountÚfile_object_maybechunksrœ   r�   Úobjr¥   Útotal_filesÚtotal_entriesÚ	dask_dictÚstep_sumrž   rˆ   r‰   Úbranchid_interpretationr‡   r¢   Ú
ttree_stepÚ
entry_stepÚdtÚinner_shaper   Z
chunk_argsr_   rŒ   r�   Úlengthr+   ©r   r©   r¤   r,   r;   )  sô    
ÿ


ú	
ÿ

þÿÿ	"
þÿÿ	
ÿ




6þúøÿ



ûõr;   c              
   C  sZ  | d dd… \}}t j |||||¡}|j|||||dd�}i }|D �]}|| jj}|jd u rhd}n
|j\}}g }g }| D ]¢}|dd… \}}d }t|ƒdkrª|d }|d urê| dd„ |D ƒ¡ |D ]\}}| 	||||df¡ qÊq~| t
jg|
 ¡ t|
ƒD ]}| 	||||
d	f¡ �qq~tt|||||	||ƒ|t|ƒf||› d
�d�||< qF|S )Nr   rŸ   Tr¦   r+   r'   c                 S  s   g | ]\}}|| ‘qS r+   r+   )r)   rŒ   r�   r+   r+   r,   r-     r.   z._get_dask_array_delay_open.<locals>.<listcomp>Fr·   r¸   )r2   r3   r–   r\   r7   rÁ   rÂ   r(   Úextendrº   ÚnumpyÚnanre   r„   r�   rb   )r?   r   r   r   r   r   r   r    rA   rB   r   r%   r&   Ú
ffile_pathÚfobject_pathrÈ   rÄ   rË   r‡   rÐ   rÑ   Z
partitionsÚpartition_argsÚifile_iobject_maybeichunksÚ
ifile_pathÚiobject_pathr   rŒ   r�   Úistepr+   r+   r,   r<   Ö  s|    
ÿú	


ûÿ
ûÿ

ù	ór<   c                	   @  sd   e Zd ZU eddœdd„ƒZded< ddd	œd
d„Zdddœdd„Zdddddddddœdd„ZdS )ÚImplementsFormMappingInfozdict | NonerT   c                 C  s   d S rH   r+   rW   r+   r+   r,   Úbehavior0  s    z"ImplementsFormMappingInfo.behaviorzstr | CallableÚ
buffer_keyÚstrútuple[str, str]©rà   rU   c                 C  s   d S rH   r+   )rK   rà   r+   r+   r,   Úparse_buffer_key5  r.   z*ImplementsFormMappingInfo.parse_buffer_keyúfrozenset[str]©Úbuffer_keysrU   c                 C  s   d S rH   r+   )rK   rç   r+   r+   r,   Úkeys_for_buffer_keys7  r.   z.ImplementsFormMappingInfo.keys_for_buffer_keysr   rc   r   r
   úMapping[str, AwkArray])Útreer\   rŒ   r�   r%   r&   r@   rU   c                 C  s   d S rH   r+   )rK   rê   r\   rŒ   r�   r%   r&   r@   r+   r+   r,   Úload_buffers9  s    	z&ImplementsFormMappingInfo.load_buffersN)	rN   rO   rP   Úpropertyrß   Ú__annotations__rä   rè   rë   r+   r+   r+   r,   rÞ   /  s   
rÞ   c                   @  s   e Zd Zdddœdd„ZdS )ÚImplementsFormMappingr   z&tuple[Form, ImplementsFormMappingInfo]©ÚformrU   c                 C  s   d S rH   r+   )rK   rð   r+   r+   r,   rM   F  r.   zImplementsFormMapping.__call__N©rN   rO   rP   rM   r+   r+   r+   r,   rî   E  s   rî   c                   @  s€   e Zd ZU dd„ Zeddœdd„ƒZeddd	œd
d„ƒZdZde	d< dddœdd„Z
dddœdd„Zdddddddœdd„ZdS ) ÚTrivialFormMappingInfoc                 C  s2   t j ¡ }t||jjƒsJ ‚|| _|  |¡| _d S rH   )	r2   ro   Úawkwardra   ÚformsÚ
RecordFormÚ_formÚbuild_form_key_to_keyÚ_form_key_to_key)rK   rð   ró   r+   r+   r,   rL   J  s    
zTrivialFormMappingInfo.__init__rS   rT   c                 C  s   d S rH   r+   rW   r+   r+   r,   rß   Q  s    zTrivialFormMappingInfo.behaviorr   zdict[str, str | None]rï   c                   s    i ‰ ‡ ‡fdd„‰ˆ| dƒ ˆ S )Nc                   sÀ   |r|d nd ˆ | j < | jr<t| jƒD ]\}}ˆ||ƒ q&n€| jrPˆ| j|ƒ nl| jrdˆ| j|ƒ nX| jrxˆ| j|ƒ nD| jr¦| j	D ]}ˆ|  |¡g |¢|‘R ƒ q„n| j
s¼| jr´nt| ƒ‚d S r^   )Úform_keyZis_unionrt   ÚcontentsZ
is_indexedÚcontentZis_listZ	is_optionZ	is_recordÚfieldsZ
is_unknownZis_numpyÚAssertionError)rð   Zcolumn_pathZ_iÚentryÚfield©Zform_key_to_pathÚimplr+   r,   r  Y  s     
z:TrivialFormMappingInfo.build_form_key_to_key.<locals>.implr+   r+   )rð   r+   r   r,   r÷   U  s    
z,TrivialFormMappingInfo.build_form_key_to_keyz{form_key}-{attribute}z
Final[str]rà   rá   râ   rã   c                 C  s   |j ddd�\}}||fS ©Nri   r/   )Úmaxsplit)Úrsplit)rK   rà   rù   Ú	attributer+   r+   r,   rä   t  s    z'TrivialFormMappingInfo.parse_buffer_keyrå   ræ   c                 C  s:   t ƒ }|D ]&}|jddd�\}}| | j| ¡ q
t|ƒS r  )rr   r  rv   rø   Ú	frozenset)rK   rç   r\   rà   rù   r  r+   r+   r,   rè   x  s
    z+TrivialFormMappingInfo.keys_for_buffer_keysr   rc   r
   ré   )rê   r\   rŒ   r�   r@   rU   c              	   C  s¦   |j ||||d ||td�}tj ¡ }	i }
t||ƒD ]l\}}|	 |¡\}}}| j |¡}t| 	¡  
¡ | 	| j¡ 
¡ ƒD ](\\}}\}}||ks’J ‚|| |
|< qvq4|
S )Nr   )rˆ   r‰   r   r%   r&   Úhow)Úarraysrb   r2   ro   ró   ry   Z
to_buffersrö   rû   Úexpected_from_buffersÚitemsrà   )rK   rê   r\   rŒ   r�   r%   r&   r@   r  ró   Ú	containerr‡   rŠ   Zttree_subformrÒ   Zttree_containerZprojection_subformÚsrcZ	src_dtypeÚdstZ	dst_dtyper+   r+   r,   rë   �  s*    ù


þz#TrivialFormMappingInfo.load_buffersN)rN   rO   rP   rL   rì   rß   Ústaticmethodr÷   rà   rí   rä   rè   rë   r+   r+   r+   r,   rò   I  s   
	rò   c                   @  s   e Zd Zdddœdd„ZdS )ÚTrivialFormMappingr   z#tuple[Form, TrivialFormMappingInfo]rï   c                 C  s&   t j ¡ }|jj |d¡}|t|ƒfS )Nú<root>)r2   ro   Údask_awkwardÚlibrz   Úform_with_unique_keysrò   )rK   rð   r  Znew_formr+   r+   r,   rM   ±  s    
zTrivialFormMapping.__call__Nrñ   r+   r+   r+   r,   r  °  s   r  ÚTc                   @  sÒ   e Zd ZU ded< ded< ded< ded< ded	< d
ed< edd„ ƒZeddœdd„ƒZdddddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
d d!d"d d#œd$d%„Zd!d"dd&œd'd(„Zd dd d)œd*d+„Zd,S )-ÚUprootReadMixinr   Ú	base_formÚexpected_formrÞ   Úform_mapping_inforå   rÄ   údict[str, Any]rB   ú&bool | tuple[type[BaseException], ...]r#   c                 C  s   t | jtƒr| jS tfS rH   )ra   r#   rb   ÚOSErrorrW   r+   r+   r,   Úallowed_exceptionsÂ  s    z"UprootReadMixin.allowed_exceptionsrE   rT   c                 C  s
   t | jƒS rH   )rE   r#   rW   r+   r+   r,   Úreturn_reportÈ  s    zUprootReadMixin.return_reportr   rc   z*tuple[AwkArray, SourcePerformanceCounters])rê   rŒ   r�   rU   c              	     sø   ||ksJ ‚ddl m} tj ¡ }| ¡ }ˆ j |ˆ j||ˆ j	ˆ j
ˆ j¡}i }ˆ jjˆ jjd� ¡ D ]Z\}	}
ˆ j t|	hƒ¡}t‡ fdd„|D ƒƒr¢||	 ||	< qd|jj||jjf|
d�||	< qd|jˆ j|| |ˆ jjˆ jjd�}|jsìJ ‚||jjfS )Nr   )ÚNumpy©rà   c                 3  s   | ]}|ˆ j v V  qd S rH   )rÄ   rY   rW   r+   r,   r[   ñ  r.   z,UprootReadMixin.read_tree.<locals>.<genexpr>)ÚnplikeÚshapern   )rß   rà   )Zawkward._nplikes.numpyr  r2   ro   ró   Úinstancer  rë   rÄ   r%   r&   rB   r  r	  rà   r
  rè   r  r5   Ú
typetracerZPlaceholderArrayZunknown_lengthZfrom_buffersrß   ÚsourceÚperformance_counters)rK   rê   rŒ   r�   r  ró   r   rj   r  rà   rn   Zkeys_for_bufferÚoutr+   rW   r,   Ú	read_treeÌ  sH    
ùÿÿýû
zUprootReadMixin.read_treeÚAwkArrayc                 C  s"   t j ¡ }|jj| jd| jjd�S )NT)Ú	highlevelrß   )r2   ro   ró   r#  Ztypetracer_from_formr  r  rß   )rK   ró   r+   r+   r,   Úmock  s    
ýzUprootReadMixin.mockc                 C  s*   t j ¡ }|j| jjdd�|d| jjd�S )NF)r)  T)Úbackendr)  rß   )r2   ro   ró   Z
to_backendr  Zlength_zero_arrayr  rß   )rK   r+  ró   r+   r+   r,   Ú
mock_empty  s    
üzUprootReadMixin.mock_emptyz'tuple[AwkArray, TypeTracerReport, dict]c                 C  s\   t j ¡ }t j ¡ }|jj| jd| jj| jj	d�\}}|||j
jj| j| jj	d�| jdœfS )NT)r)  rß   rà   r  )ÚtraceÚ	form_info)r2   ro   ró   r  r#  Ztypetracer_with_reportr  r  rß   rà   r  rz   Ztrace_form_structure)rK   ró   r  ÚmetaÚreportr+   r+   r,   Úprepare_for_projection  s"    

ü
þûýz&UprootReadMixin.prepare_for_projectionr  r   Údict)rK   r0  ÚstaterU   c                C  s   | j ||d�}|  |¡S )N)r0  r3  )Únecessary_columnsÚproject_keys)rK   r0  r3  r\   r+   r+   r,   Úproject1  s    zUprootReadMixin.project)r0  r3  rU   c                C  s`   |d d }|d d }|d }t j ¡ }h |j£|jj |j|j||¡£}| 	|¡t
| jƒ@ S )Nr-  Úform_key_to_parent_form_keyÚform_key_to_buffer_keysr.  )r2   ro   r  Zdata_touchedr  rz   Z&buffer_keys_required_to_compute_shapesrä   Zshape_touchedrè   r  rÄ   )rK   r0  r3  r7  r8  r.  r  Zdata_buffersr+   r+   r,   r4  5  s&    ÿ
ÿüþ
ÿz!UprootReadMixin.necessary_columns©rK   r\   rU   c                 C  s   t ‚d S rH   )r9   ©rK   r\   r+   r+   r,   r5  S  s    zUprootReadMixin.project_keysN)rN   rO   rP   rí   rì   r  r  r'  r*  r,  r1  r6  r4  r5  r+   r+   r+   r,   r  º  s"   

9	r  c                 O  sT   t j ¡ }| |d d dd„ |D ƒdd„ | ¡ D ƒt| ƒjt| ƒt 	¡ t 
¡ dœ	g¡S )Nc                 S  s   g | ]}t |ƒ‘qS r+   ©Úrepr©r)   Úar+   r+   r,   r-   _  r.   z#_report_failure.<locals>.<listcomp>c                 S  s   g | ]\}}|t |ƒg‘qS r+   r;  ©r)   rZ   Úvr+   r+   r,   r-   `  r.   ©	Ú	call_timeÚdurationr%  rI   rJ   Ú	exceptionÚmessageZfqdnÚhostname)r2   ro   ró   r   r
  ru   rN   rá   ÚsocketÚgetfqdnÚgethostname)rD  rB  rI   rJ   ró   r+   r+   r,   Ú_report_failureW  s    
÷ÿÿrJ  c                 O  sP   t j ¡ }| d¡}| d | | ¡ dd„ |D ƒdd„ | ¡ D ƒd d d d dœ	g¡S )NÚcountersc                 S  s   g | ]}t |ƒ‘qS r+   r;  r=  r+   r+   r,   r-   s  r.   z#_report_success.<locals>.<listcomp>c                 S  s   g | ]\}}|t |ƒg‘qS r+   r;  r?  r+   r+   r,   r-   t  r.   rA  )r2   ro   ró   rx   r   Úasdictr
  )rC  rI   rJ   ró   rK  r+   r+   r,   Ú_report_successj  s    

÷ÿÿrM  c                   s   t  ˆ ¡‡ fdd„ƒ}|S )Nc                    s*   t  ¡ }ˆ | i |¤Ž}t  ¡ }||| fS rH   )ÚtimeÚ	monotonic)rI   rJ   rŒ   Úresultr�   ©r³   r+   r,   Úwrapper  s    zwith_duration.<locals>.wrapper)Ú	functoolsÚwraps)r³   rR  r+   rQ  r,   Úwith_duration~  s    rU  c                   @  sH   e Zd Zddddddddœdd	„Zd
dd
dœdd„Zdd„ Zdd„ ZdS )Ú_UprootReadrå   r  r   rÞ   r  rS   )rÄ   rB   r  r  r  r#   rU   c
           
      C  s>   || _ t|ƒ| _|| _|| _|| _|| _|| _|| _|	| _	d S rH   )
r†   r  rÄ   rB   r  r  r  r#   r%   r&   )
rK   r†   rÄ   rB   r  r  r  r#   r%   r&   r+   r+   r,   rL   Š  s    
z_UprootRead.__init__r  r9  c              
   C  s(   t | j|| j| j| j| j| j| j| jƒ	S rH   )	rV  r†   rB   r  r  r  r#   r%   r&   r:  r+   r+   r,   r5     s    ÷z_UprootRead.project_keysc              
   C  s°   |\}}}| j ršt ¡ }z8t| jƒ|||ƒ\\}}}|t|| j| |||d�fW S  | jy˜ }	 z.| jdd�t	|	|| j| ||ƒfW  Y d }	~	S d }	~	0 0 |  |||¡\}}
|S ©N)rK  Úcpu)r+  )
r  rN  Útime_nsrU  Ú
_call_implrM  r†   r  r,  rJ  )rK   r‹   r_   rŒ   r�   rB  rP  rK  rC  ÚerrÚ_r+   r+   r,   rM   ­  s8    
ÿûþ

ûþz_UprootRead.__call__c                 C  s   |   | j| ||¡S rH   )r'  r†   )rK   r_   rŒ   r�   r+   r+   r,   rZ  Î  s
    ýz_UprootRead._call_implN)rN   rO   rP   rL   r5  rM   rZ  r+   r+   r+   r,   rV  ‰  s   !rV  c                   @  sD   e Zd Zddddddœdd„Zdd	„ Zd
d„ Zddddœdd„ZdS )Ú_UprootOpenAndReadr   rÞ   r  rS   )r  r  r  r#   rU   c                 C  sJ   || _ || _|| _t|ƒ| _|| _|| _|| _|| _|	| _	|
| _
|| _d S rH   )r   r    rA   r  rÄ   rB   r  r  r  r#   r%   r&   )rK   r   r    rA   rÄ   rB   r  r  r  r#   r%   r&   r+   r+   r,   rL   ×  s    
z_UprootOpenAndRead.__init__c                 C  sÖ   t j ||| j| j| j¡}|j}|rŒ|| }}	d|  krB|k r\n nd|	  krZ|ks¼n td|› d|	› d|jj	› d|j
› d|› d�ƒ‚n0t || ¡}
t||
 |ƒt|d |
 |ƒ }}	||	ksÈJ ‚|  |||	¡S )	Nr   r�   r‘   r’   r“   r”   r•   r/   )r2   r3   r–   r   r    rA   r—   rq   r›   rœ   r�   r˜   r™   rš   r'  )rK   rœ   r�   Úi_step_or_startÚn_steps_or_stopÚis_chunkrž   r—   rŒ   r�   Zevents_per_stepr+   r+   r,   rZ  ñ  s<    û
.þúøÿÿýz_UprootOpenAndRead._call_implc                 C  s¸   |\}}}}}| j ržt ¡ }z:t| jƒ|||||ƒ\\}}	}
|t|
||||||	d�fW S  | jyœ } z,| jdd�t|||||||ƒfW  Y d }~S d }~0 0 |  |||||¡\}}|S rW  )	r  rN  rY  rU  rZ  rM  r  r,  rJ  )rK   Zblockwise_argsrœ   r�   r^  r_  r`  rB  rP  rK  rC  r[  r\  r+   r+   r,   rM     sP    ú
ÿùþ
ùþ
ÿz_UprootOpenAndRead.__call__r  rå   r9  c                 C  s0   t | j| j| j|| j| j| j| j| j| j	| j
ƒS rH   )r]  r   r    rA   rB   r  r  r  r#   r%   r&   r:  r+   r+   r,   r5  F  s    õz_UprootOpenAndRead.project_keysN)rN   rO   rP   rL   rZ  rM   r5  r+   r+   r+   r,   r]  Ö  s   '.r]  c           	      C  sh   g }|D ]:}|| }|j  |j¡}|r8|jd|jid�}| |¡ q|rRd|jind }| jj|||d�S )NrQ   )Ú
parameters)r7   Zawkward_formr›   r:   Útitlerº   rô   rõ   )	ró   rž   rÄ   r   rú   r‡   r¢   Zcontent_formra  r+   r+   r,   Ú_get_ttree_formV  s    rc  c           1        s  t j ¡ }t j ¡ }g }g }d }g }d}| D ]ò}|dd… \}}t j |||||	¡}|d ur,|d7 }t|tƒr¦t|j	dd�ƒdkr¦|‰|j
}| d¡ ‡ ‡fdd„}n| d¡ ˆ }| |¡ t|ƒd	krÚ| |d ¡ nd }|j	|||||dd
�‰|d u �rˆ}q,tˆƒ‰‡fdd„|D ƒ}q,|tu �rn|tu�s8J ‚t|ƒ}tdd„ |D ƒƒ}tdtt |||  ¡ƒƒ}|dk�r–td d dd„ | D ƒ¡¡ƒ‚t|ƒdk�s¸t|ƒ�sÖt|ƒ�rÖtd d dd„ | D ƒ¡¡ƒ‚d}|D ]P}d} |j}!i }"|D ]}#||# }$|$j|"|$j< �qôt||| |!|"ƒ}%|t|%ƒ7 }�qÞtt|t|ƒ ƒƒ}&dg}'g }(t|ƒD �]"\})}d} |j}!|d u �rÌt| |!|&ƒD ]H}*t|*|& |!ƒ}+|+|* },|,dk�r€|' |'d |, ¡ |( |)|*|+f¡ �q€n¬||) D ]¢\}*}+d|*  k�rô|!k �rn nd|+  k�r|!k�s@n td|*› d|+› d|j j!› d|j"› d|!› d�ƒ‚|+|* },|,dk�rÔ|' |'d |, ¡ |( |)|*|+f¡ �qÔ�qVt#||d ||
 $d¡ƒ}-t|(ƒdk�r¶|' d¡ |( d¡ |d u �rÚ|j%j& '|-d¡}.t(|.ƒ}/n||-ƒ\}.}/t)|||
|-|.|/|||d�	}0|j*|0|(t+|'ƒdd �S )!Nr   rŸ   r/   Tr    c                   s   | ˆu oˆ | ƒS rH   r+   r¡   r£   r+   r,   r¥   “  s    z*_get_dak_array.<locals>.real_filter_branchFr'   r¦   c                   s   g | ]}|ˆ v r|‘qS r+   r+   r§   r¨   r+   r,   r-   ­  r.   z"_get_dak_array.<locals>.<listcomp>c                 s  s   | ]}|j V  qd S rH   rª   r«   r+   r+   r,   r[   ³  r.   z!_get_dak_array.<locals>.<genexpr>r¬   r­   c                 s  sD   | ]<\}}d t |tƒr|jn|›dt |tƒr2|jn|›d�V  qdS r®   r±   r²   r+   r+   r,   r[   »  s   ÿrµ   c                 s  sD   | ]<\}}d t |tƒr|jn|›dt |tƒr2|jn|›d�V  qdS r®   r±   r²   r+   r+   r,   r[   Å  s   ÿéÿÿÿÿr�   r‘   r’   r“   r”   r•   r   r¶   r  ©r  r  r  r#   r%   r&   úfrom-uproot©Ú	divisionsrf   ),r2   ro   r  ró   r3   r–   ra   r   r(   r\   r¹   rº   rr   r   r»   r¼   rc   r˜   r™   rq   r½   r¾   r5   r4   r—   r7   r¿   r   rÀ   rt   re   rš   r›   rœ   r�   rc  Úgetr  rz   r  rò   rV  Úfrom_maprb   )1r?   r   r   r   r   r   r   r   r    rA   rB   r"   r   r#   r%   r&   r  ró   r†   rÃ   rÄ   rÅ   rÆ   rÇ   rœ   r�   rÈ   r¥   rÉ   rÊ   rÌ   rž   rˆ   r‰   rÍ   r‡   r¢   rÎ   rÏ   rh  rÙ   r_   rŒ   r�   rÒ   r  r  r  Úfnr+   rÓ   r,   r=   i  s   


ÿ


ú	

ÿ

þÿÿ	"
þÿÿ	
ÿ

6þúøÿ
ÿ


ÿ
÷ür=   c           #      C  sž  t j ¡ }t j ¡ }| d dd… \}}|d urFtt |j¡ƒ}|}n>t j 	|||||¡}|j
|||||dd�}t||||	 d¡ƒ}dg}g }| D ]˜}d }|dd… \}}t|ƒdkr¾|d }|d u�r|D ]2\}}| |d ||  ¡ | ||||df¡ qÌq’d }t|ƒD ]}| ||||df¡ �qq’|
d u �rP|jj |d	¡} t| ƒ}!n|
|ƒ\} }!t|||||	|| |!|||d
�}"|j|"||d u �r�d nt|ƒdd�S )Nr   rŸ   Tr¦   r   r'   rd  Fr  re  rf  rg  )r2   ro   r  ró   rs   r2  Úfromkeysrü   r3   r–   r\   rc  ri  r(   rº   re   r  rz   r  rò   r]  rj  rb   )#r?   r   r   r   r   r   r   r    rA   rB   r"   r   r#   r$   r%   r&   r  ró   r×   rØ   rÄ   r  rÈ   rh  rÙ   rÚ   r   rÛ   rÜ   rŒ   r�   rÝ   r  r  rk  r+   r+   r,   r>      s’    


ÿúÿ
ûÿ
ûÿ


ÿ
õür>   )<Ú
__future__r   rS  r˜   rG  rN  Úcollections.abcr   r   r   Úconcurrent.futuresr   Zuproot.source.chunkr   Útypingr   r	   Ztyping_extensionsr
   r   r   ÚImportErrorrÕ   r2   Zuproot._utilr   r   Zuproot.behaviors.TBranchr   r   r   Zawkward._nplikes.typetracerr   Zawkward.formsr   Zawkward.highlevelr   r(  rC   rD   rR   r„   r…   r�   r;   r<   rÞ   rî   rò   r  r  r  rJ  rM  rU  rV  r]  rc  r=   r>   r+   r+   r+   r,   Ú<module>   sˆ   "í  !#ùW@ .Yg M  8