|
| 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 |
0 commit comments