Skip to content

Commit d174a6f

Browse files
authored
Add scripts for manipulation with derived data (#518)
1 parent b35ab4f commit d174a6f

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

exec/extract_df.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Extract data frames from AO2D.root files with information about parent files.
5+
"""
6+
7+
import sys
8+
9+
from ROOT import TFile, TObject # pylint: disable=import-error
10+
11+
# DF range
12+
df_first = 0
13+
df_last = 0
14+
15+
for f in sys.argv[1:]:
16+
df_i = 0 # DF counter
17+
file_in = TFile.Open(f)
18+
file_out = TFile.Open(f.replace(".root", "_edit.root"), "recreate")
19+
for key in file_in.GetListOfKeys():
20+
key_name = key.GetName()
21+
obj = file_in.Get(key_name)
22+
if not obj:
23+
continue
24+
if key_name == "parentFiles":
25+
print(f"Writing object {key_name}")
26+
file_out.cd()
27+
res = obj.Write(key_name, TObject.kSingleKey)
28+
if not res:
29+
print(f"Failed to write {key_name}")
30+
# DF directory
31+
elif obj.ClassName() == "TDirectoryFile":
32+
if df_first <= df_i <= df_last:
33+
print(f"Writing object {key_name}")
34+
# Create the DF directory
35+
dir_obj = file_out.mkdir(key_name)
36+
if not dir_obj:
37+
print(f"Failed to create dir {key_name}")
38+
dir_obj.cd()
39+
# Write trees in the DF directory
40+
for key_sub in obj.GetListOfKeys():
41+
key_sub_name = key_sub.GetName()
42+
obj_sub = obj.Get(key_sub_name)
43+
if not obj_sub:
44+
continue
45+
print(f"Writing object {key_name}/{key_sub_name}")
46+
res = obj_sub.CloneTree().Write() if obj_sub.ClassName() == "TTree" else obj_sub.Write()
47+
if not res:
48+
print(f"Failed to write {key_name}/{key_sub_name}")
49+
df_i += 1

exec/get_parents.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Get paths to parent files from derived AO2D.root files.
5+
"""
6+
7+
import sys
8+
9+
from ROOT import TFile # pylint: disable=import-error
10+
11+
for f in sys.argv[1:]:
12+
file = TFile.Open(f)
13+
print(f)
14+
list_parents = []
15+
obj_map = file.Get("parentFiles")
16+
# map.Print()
17+
# Loop over data frames
18+
for key in obj_map:
19+
# Get the parent file path
20+
# print(map.GetValue(key))
21+
list_parents.append(str(obj_map.GetValue(key)))
22+
# Remove duplicities and sort
23+
print(len(list_parents))
24+
list_parents = list(dict.fromkeys(list_parents))
25+
list_parents.sort()
26+
# Print out list of parents
27+
for p in list_parents:
28+
print(p)

0 commit comments

Comments
 (0)