CMS 3D CMS Logo

Functions | Variables
root2sqlite Namespace Reference

Functions

def columnescape (s)
 
def read_objects_root (rootfile)
 
def save_keyvalue (dictionary, name)
 
def tosqlite (x)
 
def treetotable (ttree, name)
 

Variables

 args
 
 basic_objects
 
 db
 
 default
 
 description
 
 f
 
 help
 
 inf
 
 nan
 
 parser
 

Function Documentation

◆ columnescape()

def root2sqlite.columnescape (   s)

Definition at line 46 of file root2sqlite.py.

Referenced by treetotable().

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

◆ read_objects_root()

def root2sqlite.read_objects_root (   rootfile)

Definition at line 70 of file root2sqlite.py.

Referenced by save_keyvalue().

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

◆ save_keyvalue()

def root2sqlite.save_keyvalue (   dictionary,
  name 
)

Definition at line 100 of file root2sqlite.py.

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

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

◆ tosqlite()

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

◆ treetotable()

def root2sqlite.treetotable (   ttree,
  name 
)

Definition at line 54 of file root2sqlite.py.

References columnescape(), join(), print(), FastTimerService_cff.range, tosqlite(), and mkLumiAveragedPlots.tuple.

Referenced by save_keyvalue().

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

Variable Documentation

◆ args

root2sqlite.args

Definition at line 13 of file root2sqlite.py.

◆ basic_objects

root2sqlite.basic_objects

Definition at line 18 of file root2sqlite.py.

◆ db

root2sqlite.db

Definition at line 16 of file root2sqlite.py.

◆ default

root2sqlite.default

Definition at line 12 of file root2sqlite.py.

◆ description

root2sqlite.description

Definition at line 9 of file root2sqlite.py.

◆ f

root2sqlite.f

Definition at line 15 of file root2sqlite.py.

◆ help

root2sqlite.help

Definition at line 11 of file root2sqlite.py.

◆ inf

root2sqlite.inf

Definition at line 20 of file root2sqlite.py.

◆ nan

root2sqlite.nan

Definition at line 21 of file root2sqlite.py.

◆ parser

root2sqlite.parser

Definition at line 9 of file root2sqlite.py.