a
    zn„f™%  ã                   @  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d„Zde_dd„ Z	e 
¡ e	_de	_de	_G dd	„ d	ƒZG d
d„ dƒZdd„ ZG dd„ dƒZdS )a  
This module provides an interface between Uproot and PyROOT.

PyROOT is a *complete* set of ROOT bindings generated automatically from ROOT's
reflected C++ code. Uproot is a pure Python reimplementation of ROOT I/O, with
incomplete coverage of ROOT's suite of classes, but without an install-time
dependence on ROOT.

The only point of contact between Uproot and PyROOT is that they both recognize
the same *serialized* form of ROOT objects, so conversions in this module proceed
by serializing and deserializing the objects through a TMessage. This makes it
possible to

- convert any PyROOT object into its Uproot :doc:`uproot.model.Model` *if Uproot can read it* (which is possible for most classes)
- convert any Uproot :doc:`uproot.model.Model` into the corresponding PyROOT object *if Uproot can write it* (which is considerably more constrained; mostly just histograms).

This module also makes it possible for PyROOT objects to be added to ROOT files
that Uproot is writing (regardless of whether Uproot could read such objects).
é    )ÚannotationsNc                 C  s¤   ddl }tjdu r&|j d¡ |jt_tj | |¡}t 	t
|ƒd tj¡}|j|dd…  tj¡d< t |tj¡|dd…< t |t
|ƒ¡}| | ¡ ¡}| ¡  |S )a¤  
    Args:
        obj (:doc:`uproot.model.Model`): The Uproot model to convert.
        name (str or None): A name for the new PyROOT object.

    Converts an :doc:`uproot.model.Model` into a PyROOT object *if it is writable by Uproot*.
    A minority of Uproot models are writable, mostly just histograms. Writability
    is necessary for conversion to PyROOT because it is serialized through a
    ROOT TMessage.
    r   NzŠ
class _Uproot_FromTMessage : public TMessage {
public:
    _Uproot_FromTMessage(void* buffer, Int_t size): TMessage(buffer, size) { }
};
é   )ÚROOTÚ	to_pyrootÚ_Uproot_FromTMessageÚgInterpreterÚDeclareÚuprootZserializationZserialize_object_anyÚnumpyÚemptyÚlenÚuint8ÚkMESS_OBJECTÚviewZuint64Z
frombufferZ
ReadObjectZGetClassZDetachBuffer)ÚobjÚnamer   Z
serializedÚbufferÚmessageÚout© r   úP/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/pyroot.pyr       s    
ÿr   c                 C  sŽ   ddl }tjdu rF|j d¡ | ¡ t_t dtj¡t_	dd„ }|t_
| |j¡}| d¡ | |tj	ttj	ƒ¡ | | ¡ tj	d| ¡ … S )aÀ  
    Args:
        obj (PyROOT object inheriting from TObject): PyROOT object to serialize.

    Serializes a PyROOT object into a NumPy array that is owned by this function.

    This function is not thread-safe and the output buffer gets overwritten by
    the next call to this function. It is essential for callers to copy the data
    out of the returned buffer, perhaps by calling :doc:`uproot._util.tobytes` on
    it or by assigning it into another array.

    A lock is provided for safety: callers should always call this function within
    the lock's context:

    .. code-block:: python

        with pyroot_to_buffer.lock:
            return uproot._util.tobytes(pyroot_to_buffer(obj))
    r   Na^  
class _Uproot_buffer_sizer : public TObject {
public:
  size_t buffer;
  size_t newsize;
  size_t oldsize;
};

char* _uproot_TMessage_reallocate(char* buffer, size_t newsize, size_t oldsize) {
    _Uproot_buffer_sizer* ptr = reinterpret_cast<_Uproot_buffer_sizer*>(
        (void*)TPython::Eval("__import__('uproot').pyroot.pyroot_to_buffer.sizer")
    );
    ptr->buffer = reinterpret_cast<size_t>(buffer);
    ptr->newsize = newsize;
    ptr->oldsize = oldsize;

    TPython::Exec("__import__('uproot').pyroot.pyroot_to_buffer.reallocate()");

    TPyReturn out = TPython::Eval("__import__('uproot').pyroot.pyroot_to_buffer.buffer.ctypes.data");
    return reinterpret_cast<char*>((size_t)out);
}

void _uproot_TMessage_SetBuffer(TMessage& message, void* buffer, UInt_t newsize) {
    message.SetBuffer(buffer, newsize, false, _uproot_TMessage_reallocate);
}
i   c                  S  s0   t  tjjt j¡} tj| d ttjƒ…< | t_d S ©N)r
   r   Úpyroot_to_bufferÚsizerZnewsizer   r   r   )Znewbufr   r   r   Ú
reallocate|   s    z$pyroot_to_buffer.<locals>.reallocate)r   r   r   r   r   Z_Uproot_buffer_sizerr
   r   r   r   r   ZTMessager   ÚSetCompressionLevelZ_uproot_TMessage_SetBufferr   ZWriteObjectZLength)r   r   r   r   r   r   r   r   F   s     
ÿ

ÿ
r   c                   @  sl   e Zd Zi Zi Zi Zdd„ Zddd„Zdd„ Zdd	„ Z	e
d
d„ ƒZe
dd„ ƒZG dd„ dƒZe
dd„ ƒZdS )Ú_GetStreamersOncec                 C  s
   || _ d S r   ©Ú_obj©Úselfr   r   r   r   Ú__init__–   s    z_GetStreamersOnce.__init__Nc                 C  s   t jj | ||¡S r   )r	   ÚreadingÚReadOnlyFileÚclass_named©r    Ú	classnameÚversionr   r   r   r$   ™   s    z_GetStreamersOnce.class_namedc                 C  s   t jj | |¡S r   )r	   r"   r#   Ústreamers_named)r    r&   r   r   r   r(   œ   s    z!_GetStreamersOnce.streamers_namedc                 C  s   t jjj| ||d�S )N)r'   )r	   r"   r#   Ústreamer_namedr%   r   r   r   r)   Ÿ   s    ÿz _GetStreamersOnce.streamer_namedc                 C  s   | j S r   )Ú_custom_classes©r    r   r   r   Úcustom_classes¤   s    z _GetStreamersOnce.custom_classesc                 C  s   d S r   r   r+   r   r   r   Ú	file_path¨   s    z_GetStreamersOnce.file_pathc                   @  s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z_GetStreamersOnce.ArrayFilec                 C  s   || _ d| _d S )Nr   )ÚarrayÚcurrent)r    r.   r   r   r   r!   ­   s    z$_GetStreamersOnce.ArrayFile.__init__c                 C  s
   || _ d S r   )r/   )r    Úpositionr   r   r   Úseek±   s    z _GetStreamersOnce.ArrayFile.seekc                 C  s$   | j | }| j| j |… }|| _ |S r   )r/   r.   )r    Ú	num_bytesr0   r   r   r   r   Úread´   s    
z _GetStreamersOnce.ArrayFile.readN)Ú__name__Ú
__module__Ú__qualname__r!   r1   r3   r   r   r   r   Ú	ArrayFile¬   s   r7   c                 C  s  | j  ¡ }| ¡ }| ¡ }| j |i ¡ |d ¡d u �rdd l}| dd¡}| d¡ | 	| j | j  ¡ d¡ | 
¡  | ¡  t | ¡ tj¡}| |t|ƒ¡ t t |¡¡}g  }| j||f< |jj ¡ D ]F\}	}
|	| jvræi | j|	< |
 ¡ D ] \}}|| j|	 |< | |¡ qîqÊ| jS )Nr   znoname.rootÚnewZnoname)r   ÚIsAÚGetNameÚGetClassVersionÚ
_streamersÚgetr   ZTMemFiler   ZWriteObjectAnyZWriteStreamerInfoÚCloser
   r   ZGetENDr   ZCopyTor   r	   Úopenr   r7   Ú_streamer_dependenciesÚfileÚ	streamersÚitemsÚappend)r    ÚtclassZobj_classnameZobj_versionr   Zmemfiler   rA   Údependenciesr&   Úversionsr'   Zstreamerinfor   r   r   rB   º   s*    



z_GetStreamersOnce.streamers)N)r4   r5   r6   r*   r<   r@   r!   r$   r(   r)   Úpropertyr,   r-   r7   rB   r   r   r   r   r   ‘   s   


r   c                   @  s,   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdS )Ú_NoFilec                 C  sr   dd l }d| _i | _|j ¡ | _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _t d¡| _d S )Nr   Ú é   z$00000000-0000-0000-0000-000000000000)r   Z
_file_pathÚ_optionsZgROOTZGetVersionIntZ	_fVersionZ_fBEGINZ_fENDZ
_fSeekFreeZ_fNbytesFreeZ_nfreeZ_fNbytesNameZ_fUnitsZ
_fCompressZ
_fSeekInfoZ_fNbytesInfoZ_fUUID_versionÚuuidÚUUIDZ_fUUID)r    r   r   r   r   r!   Ú   s     z_NoFile.__init__c                 C  s   t jS r   )r   r<   r+   r   r   r   rB   í   s    z_NoFile.streamersc                 C  s   t jS r   )r   r*   r+   r   r   r   r,   ñ   s    z_NoFile.custom_classesN)r4   r5   r6   r!   rH   rB   r,   r   r   r   r   rI   Ù   s
   
rI   c              	   C  sv   t j�\ t | ƒ}tjjj d|¡}tjj d¡}t	| ƒ}t
ƒ }tj ||i ||d¡W  d  ƒ S 1 sh0    Y  dS )aª  
    Args:
        obj (PyROOT object inheriting from TObject): PyROOT object to convert to
            an Uproot :doc:`uproot.model.Model`.

    Converts a PyROOT object into its corresponding Uproot :doc:`uproot.model.Model`
    *if it is readable by Uproot*. Most ROOT classes are readable by Uproot. Readability
    is necessary for conversion from PyROOT because the object is serialized through a
    ROOT TMessage.
    Nr   )r   Úlockr	   ÚsourceÚchunkZChunkÚwrapÚcursorZCursorr   rI   ZdeserializationZread_object_any)r   r   rQ   rS   ZmaybestreamersZ	detatchedr   r   r   Úfrom_pyrootö   s    ÿrT   c                   @  sB   e Zd Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd
d„Zd	S )Ú_PyROOTWritablec                 C  s
   || _ d S r   r   r   r   r   r   r!     s    z_PyROOTWritable.__init__c                 C  s(   | j  ¡ }| ¡ | ¡ f}tj |g ¡S r   )r   r9   r:   r;   r   r@   r=   )r    rE   Úkeyr   r   r   Úclass_rawstreamers  s    
z"_PyROOTWritable.class_rawstreamersc                 C  s   | j  ¡ }| ¡ S r   )r   r9   r:   )r    rE   r   r   r   r&     s    
z_PyROOTWritable.classnamec                 C  s
   | j  ¡ S r   )r   ZGetTitler+   r   r   r   ÚfTitle  s    z_PyROOTWritable.fTitleNc                 C  sv   |d u s|| j  ¡ kr| j }n| j  |¡}tj�2 tj t|ƒt| j	ƒd d … ¡W  d   ƒ S 1 sh0    Y  d S )Né	   )
r   r:   ZCloner   rO   r	   Z_utilÚtobytesr   r&   )r    r   r   r   r   r   Ú	serialize  s    ÿz_PyROOTWritable.serialize)N)	r4   r5   r6   r!   rH   rW   r&   rX   r[   r   r   r   r   rU     s   


rU   )N)Ú__doc__Ú
__future__r   Ú	threadingrM   r
   r	   r   r   r   ÚLockrO   r   r   r   rI   rT   rU   r   r   r   r   Ú<module>   s   
#F
H