95def load_from_root_file(root_file, key: str = "ShipGeo") -> Config:
96 """
97 Load configuration from ROOT file.
98
99 Automatically detects and handles both formats:
100 - New format: JSON string (stored as std::string or TObjString)
101 - Old format: Pickled Python object
102
103 Args:
104 root_file: Either a ROOT.TFile object or a string path to ROOT file
105 key: The key name for the stored config (default: 'ShipGeo')
106
107 Returns:
108 Config object with the loaded configuration
109 """
110 import ROOT
111
112 own_file = False
113 if isinstance(root_file, str):
114 root_file = ROOT.TFile.Open(root_file)
115 own_file = True
116
117 try:
118
119 config_obj = root_file.Get(key)
120 if not config_obj:
121 raise ValueError(f"No object with key '{key}' found in ROOT file")
122
123
124 content_str = str(config_obj)
125
126
127 if content_str.startswith("{"):
128
129 config = Config()
130 config.loads_json(content_str)
131 else:
132
133
134 pickle_bytes = content_str.encode("latin-1")
135 config = pickle.loads(pickle_bytes)
136
137
138 if not isinstance(config, Config):
139
140 c = Config()
141 c.update(config)
142 config = c
143
144 return config
145
146 finally:
147 if own_file:
148 root_file.Close()