ATLAS Offline Software
Loading...
Searching...
No Matches
PyROOTTFilePythonize.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
10
11// stdlib includes
12#include <stdlib.h>
13#include <stdio.h>
14
15#include "TFile.h"
16
17// local includes
19
20namespace RootUtils {
21
22PyBytes::PyBytes(size_t buf_sz) :
23 sz(buf_sz>0?buf_sz:0),
24 buf(sz)
25{
26}
27
31
33 sz(rhs.sz),
34 buf(rhs.buf)
35{
36}
37
40{
41 if (this != &rhs) {
42 this->sz = rhs.sz;
43 this->buf = rhs.buf;
44 }
45 return *this;
46}
47
48Long64_t
50{
51 if (f) {
52 return f->GetRelOffset();
53 }
54 fprintf (stderr,
55 "::RootUtils::_pythonize_tell_root_file got a NULL TFile ptr!\n");
56 return 0;
57}
58
59PyBytes
60_pythonize_read_root_file(TFile* f, Int_t len)
61{
62 if (!f) {
63 //err
64 return PyBytes(0);
65 }
66
67 Long64_t totsz = f->GetSize();
68 Long64_t curpos = _pythonize_tell_root_file(f);
69 Long64_t remain = totsz - curpos;
70 remain = (remain>0) ? remain : 0;
71
72 len = (len>remain) ? remain : len;
73 PyBytes buf(len);
74 if (f->ReadBuffer((char*)buf.buf.data(), buf.sz)) {
75 //err
76 return PyBytes(0);
77 }
78
79 f->Seek(buf.sz + curpos);
80 return buf;
81}
82
83} //> namespace RootUtils
Pythonize the access to TFile so they can be used as file-like python objects.
PyBytes & operator=(const PyBytes &rhs)
std::vector< char > buf
the buffer of bytes
size_t sz
the size of the buffer of bytes
PyBytes(size_t buf_sz=0)
wrapper around char* to explicitly mean bytes.
PyBytes _pythonize_read_root_file(TFile *f, Int_t len)
read len bytes from file f
Long64_t _pythonize_tell_root_file(TFile *f)
tell the current position in file f