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