PyLamarr
Pythonizations for the ultra-fast simulation option for the LHCb experiment
 
Loading...
Searching...
No Matches
TTreeLoader.py
1"""
2TTreeLoader.
3"""
4import sys
5import pandas as pd
6import numpy as np
7
9 def __init__(self, database_connection):
10 self._db = database_connection
11
12 def load(self, filename, batch_id=0, tables=None, if_exists='replace'):
13
14 try:
15 import uproot
16 except ImportError as e:
17 print ("The uproot package is required for loading ROOT files "
18 "with TTreeLoader. \nInstall it with `pip install uproot`",
19 file=sys.stderr)
20 raise e
21
22
23 if isinstance(tables, str):
24 tables = [tables]
25
26
27 input_file = uproot.open(filename)
28
29
30 with self._db.connect() as db:
31 # Loop on the objects in the ROOT file
32 for key in input_file.keys():
33
34 obj = input_file[key]
35
36
37 if not isinstance(obj, uproot.behaviors.TTree.TTree):
38 continue
39
40
41 if tables is not None and all([key not in t for t in tables]):
42 continue
43
44 df = pd.DataFrame(
45 obj.arrays(library='np')
46 )
47 df['input_file'] = batch_id
48 df.to_sql(obj.name, db, if_exists=if_exists)
49
50
51
52
53
54
55
56
57
58
59
60
61
62