a
    zn„fØ  ã                   @  sˆ   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	Z	G dd„ dƒZ
dddd	d
dd„ ddd„ dddfddœdd„Zdd„ ZdS )é    )ÚannotationsN)ÚAny)ÚAbstractFileSystem)Ú	url_to_fsc                   @  s.   e Zd Zddddddddœd	d
„Zdd„ ZdS )Ú	_ToROOTFnNr   ÚstrÚintú
str | Nonezdict | Noner   )ÚfsÚpathÚnpartitionsÚprefixÚstorage_optionsÚkwargsc                 K  sl   || _ || _|| _t t |d¡¡| _|| _| j j| jdd� t	| j j
tƒrT| j j
n
| j j
d | _
|| _d S )Né
   T)Úexist_okr   )r
   r   r   ÚmathÚceilÚlogÚzfillr   ZmkdirsÚ
isinstanceÚprotocolr   r   )Úselfr
   r   r   r   r   r   © r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/writing/_dask_write.pyÚ__init__   s    	ÿ

ýz_ToROOTFn.__init__c                 C  sr   dt |d ƒ | j¡› d�}| jd ur6| j› d|› �}| j | j› | jj› |› �¡}t||fi | j¤d| j	i¤ŽS )NÚpartr   z.rootú-r   )
r   r   r   r
   Zunstrip_protocolr   ÚsepÚ
ak_to_rootr   r   )r   ÚdataZblock_indexÚfilenamer   r   r   Ú__call__#   s    
ÿÿÿz_ToROOTFn.__call__)NN)Ú__name__Ú
__module__Ú__qualname__r   r"   r   r   r   r   r      s     úr   TÚtreeÚ c                 C  s   | dkr|S | d | S )Nr'   Ú_r   )ÚouterÚinnerr   r   r   Ú<lambda>5   ó    r+   r   c                 C  s   d|  S )NÚnr   )Zcountedr   r   r   r+   7   r,   g      $@Úzlibé   r	   )r   c                 C  s"  ddl m} ddlm} ddlm} ddlm} ddlm	}m
} t|fi |pNi ¤Ž\}}d||| |ƒ› �}|t||| j|||||||	|
|d�| || jfƒd	| jd
�}ddi|jj|j _i }|d }dd„ | ¡ f||df< |j||||jgd�|gd�}|||dd�}|�r| ¡  dS |S dS )u_  
    Args:
        array (`dask_awkward.Array`): The :obj:`dask_awkward.Array` collection to write to disk.
        destination (path-like): Where to store the output; this can be a local filesystem path
            or a remote filesystem path.
        compute (bool): If ``True``, immediately compute the result (write data to disk). If ``False`` a Scalar collection will be returned such that ``compute`` can be explicitly called.
        prefix (str): An addition prefix for output files. If ``None`` all parts
            inside the destination directory will be named ``?``; if
            defined, the names will be ``f"{prefix}-partN.root"``.
        tree_name (str): Name of ttree to be written to. Default is "tree".
        title (str): Title of ttree to be written to. Default is "".
        field_name (callable of str â†’ str): Function to generate TBranch
            names for columns of an Awkward record array or a Pandas DataFrame.
        initial_basket_capacity (int): Number of TBaskets that can be written to the
            TTree without rewriting the TTree metadata to make room.
        resize_factor (float): When the TTree metadata needs to be rewritten,
            this specifies how many more TBasket slots to allocate as a multiplicative
            factor.
        compression (:doc:`uproot.compression.Compression` or None): Compression algorithm
            and level for new objects added to the file. Can be updated after creating
            the :doc:`uproot.writing.writable.WritableFile`. Default is ``uproot.ZLIB(1)``.

    Writes a dask-awkward array to a set of ROOT files. Data is written to a TTree

    .. code-block:: python
        import awkward as ak
        import dask_awkward as dak
        a = ak.Array([{"a": [1,2,3]}, {"a": [4, 5]}])
        d = dask_write(a, npartitions=2)
        d.nparatitions
        uproot.dask_write(d)

    r   )Útokenize)Ú
BlockIndex)ÚHighLevelGraph)ÚAwkwardMaterializedLayer)Úmap_partitionsÚnew_scalar_objectzwrite-root-)r
   r   r   r   Ú	tree_nameÚcompressionÚcompression_levelÚtitleÚ
field_nameÚcounter_nameÚresize_factorÚinitial_basket_capacityzto-root)ÚlabelÚmetaZ	ak_outputTz	-finalizec                  W  s   d S )Nr   )r(   r   r   r   r+   }   r,   zdask_write.<locals>.<lambda>)Zprevious_layer_names)ÚdependenciesZf8)ZdtypeN)Z	dask.baser0   Zdask.blockwiser1   Zdask.highlevelgraphr2   Zdask_awkward.layers.layersr3   Zdask_awkward.lib.corer4   r5   r   r   r   Ú_metaZdaskZlayersÚnamer   Z__dask_keys__Zfrom_collectionsÚcompute)ÚarrayÚdestinationrC   r   r   r6   r9   r:   r=   r;   r<   r7   r8   r0   r1   r2   r3   r4   r5   r
   r   rB   Zmap_resZdskÚ
final_nameÚgraphÚoutr   r   r   Ú
dask_write-   sR    0ô
îýrI   c              	     sÐ   |dv rt jj}nJ|dv r$t jj}n8|dv r6t jj}n&|dv rHt jj}nd|› d�}t|ƒ‚t j| fdt jj	 
||¡i|
p|i ¤Ž}‡ fdd	„ˆ jD ƒ}|j|||||||	d
� ||  ‡ fdd	„ˆ jD ƒ¡ d S )N)ZLZMAÚlzma)ZZLIBr.   )ZLZ4Zlz4)ZZSTDZzstdz$unrecognized compression algorithm: z.. Only ZLIB, LZMA, LZ4, and ZSTD are accepted.r7   c                   s   i | ]}|ˆ | j “qS r   )Útype©Ú.0rB   ©rD   r   r   Ú
<dictcomp>­   r,   zak_to_root.<locals>.<dictcomp>)r9   r;   r:   r=   r<   c                   s   i | ]}|ˆ | “qS r   r   rL   rN   r   r   rO   ¹   r,   )ÚuprootÚconstZkLZMAZkZLIBZkLZ4ZkZSTDÚ
ValueErrorZrecreater7   ZCompressionZfrom_code_pairÚfieldsZmktreeÚextend)rE   rD   r6   r7   r8   r9   r;   r:   r=   r<   r   Zcompression_codeÚmsgÚout_fileZbranch_typesr   rN   r   r   Œ   s<    



ÿÿþûù
r   )Ú
__future__r   r   Útypingr   Zfsspecr   Zfsspec.corer   rP   r   rI   r   r   r   r   r   Ú<module>   s&   $ó_