CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions | Variables
root2sqlite Namespace Reference

Functions

def columnescape
 
def read_objects_root
 
def save_keyvalue
 
def tosqlite
 
def treetotable
 

Variables

tuple args = parser.parse_args()
 
dictionary basic_objects = {}
 
tuple db = sqlite3.connect(args.output)
 
tuple f = ROOT.TFile.Open(args.inputfile)
 
tuple inf = re.compile("([- \[])inf([,}\]])")
 
tuple nan = re.compile("([- \[])nan([,}\]])")
 
tuple parser = argparse.ArgumentParser(description="Convert arbitrary ROOT file to SQLite database, mapping TTrees to tables and converting TObjects to JSON.")
 

Function Documentation

def root2sqlite.columnescape (   s)

Definition at line 46 of file root2sqlite.py.

Referenced by treetotable().

46 
47 def columnescape(s):
48  # add whatever is not a valid column name here
49  SQLKWDs = ["index"]
50  if s.lower() in SQLKWDs:
51  return s + "_"
52  else:
53  return s
def columnescape
Definition: root2sqlite.py:46
def root2sqlite.read_objects_root (   rootfile)

Definition at line 70 of file root2sqlite.py.

Referenced by save_keyvalue().

70 
71 def read_objects_root(rootfile):
72  xml_re = re.compile(r"^<(.+)>(.+)=(.+)<\/\1>$")
73  def parse_directory(di):
74  directory = rootfile.GetDirectory(di)
75  for key in directory.GetListOfKeys():
76  entry = key.GetName()
77  rtype = key.GetClassName()
78  fullpath = "%s/%s" % (di, entry) if di != "" else entry
79  if (rtype == "TDirectoryFile"):
80  for k, v, t in parse_directory(fullpath):
81  yield (k, v, t)
82  else:
83  obj = rootfile.Get(fullpath)
84  if obj:
85  yield (fullpath, obj, rtype)
86  else:
87  # special case to parse the xml abomination
88  m = xml_re.search(entry)
89  if m:
90  name = m.group(1)
91  typecode = m.group(2)
92  value = m.group(3)
93  fp = "%s/%s" % (di, name)
94  yield (fp, value, rtype)
95  else:
96  raise Exception("Invalid xml:" + entry)
97  path_fix = re.compile(r"^\/Run \d+")
98  for fullname, obj, rtype in parse_directory(""):
99  yield fullname, obj, rtype
def read_objects_root
Definition: root2sqlite.py:70
def root2sqlite.save_keyvalue (   dictionary,
  name 
)

Definition at line 100 of file root2sqlite.py.

References print(), read_objects_root(), tosqlite(), treetotable(), and betterConfigParser.unicode.

101 def save_keyvalue(dictionary, name):
102  name = name.replace("/", "_")
103  create = "CREATE TABLE %s(key, value);" % name
104  print(create)
105  db.execute(create)
106  data = []
107  for k, v in dictionary.iteritems():
108  vals = (unicode(k), tosqlite(v))
109  data.append(vals)
110  insert = "INSERT INTO %s(key, value) VALUES (?,?);" % name
111  print(insert)
112  db.executemany(insert, data)
113  db.commit()
114 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def save_keyvalue
Definition: root2sqlite.py:100
def root2sqlite.tosqlite (   x)

Definition at line 23 of file root2sqlite.py.

References edmScanValgrind.buffer, and betterConfigParser.unicode.

Referenced by save_keyvalue(), and treetotable().

23 
24 def tosqlite(x):
25  if isinstance(x, ROOT.string):
26  try:
27  return unicode(x.data())
28  except:
29  return buffer(x.data())
30  if isinstance(x, int):
31  return x
32  if isinstance(x, float):
33  return x
34  if isinstance(x, int):
35  return x
36  else:
37  try:
38  rootobj = unicode(ROOT.TBufferJSON.ConvertToJSON(x))
39  # turns out ROOT does not generate valid JSON for NaN/inf
40  clean = nan.sub('\\g<1>0\\g<2>', inf.sub('\\g<1>1e38\\g<2>', rootobj))
41  obj = json.loads(clean)
42  jsonobj = json.dumps(obj, allow_nan=False)
43  return jsonobj
44  except Exception as e:
45  return json.dumps({"root2sqlite_error": e.__repr__(), "root2sqlite_object": x.__repr__()})
def root2sqlite.treetotable (   ttree,
  name 
)

Definition at line 54 of file root2sqlite.py.

References columnescape(), join(), print(), sistrip::SpyUtilities.range(), and tosqlite().

Referenced by save_keyvalue().

54 
55 def treetotable(ttree, name):
56  name = name.replace("/", "_")
57  branches = [b.GetName() for b in ttree.GetListOfBranches()]
58  colnames = ", ".join(columnescape(b) for b in branches)
59  create = "CREATE TABLE %s(%s);" % (name, colnames)
60  print(create)
61  db.execute(create)
62  data = []
63  for i in range(ttree.GetEntries()):
64  ttree.GetEntry(i)
65  vals = tuple([tosqlite(getattr(ttree, b)) for b in branches])
66  data.append(vals)
67  insert = "INSERT INTO %s(%s) VALUES (%s);" % (name, colnames, ",".join(["?"] * len(branches)))
68  print(insert)
69  db.executemany(insert, data)
const uint16_t range(const Frame &aFrame)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def columnescape
Definition: root2sqlite.py:46
def treetotable
Definition: root2sqlite.py:54

Variable Documentation

tuple root2sqlite.args = parser.parse_args()

Definition at line 13 of file root2sqlite.py.

dictionary root2sqlite.basic_objects = {}

Definition at line 18 of file root2sqlite.py.

tuple root2sqlite.db = sqlite3.connect(args.output)

Definition at line 16 of file root2sqlite.py.

tuple root2sqlite.f = ROOT.TFile.Open(args.inputfile)

Definition at line 15 of file root2sqlite.py.

tuple root2sqlite.inf = re.compile("([- \[])inf([,}\]])")

Definition at line 20 of file root2sqlite.py.

tuple root2sqlite.nan = re.compile("([- \[])nan([,}\]])")

Definition at line 21 of file root2sqlite.py.

tuple root2sqlite.parser = argparse.ArgumentParser(description="Convert arbitrary ROOT file to SQLite database, mapping TTrees to tables and converting TObjects to JSON.")

Definition at line 9 of file root2sqlite.py.