FairShip
Loading...
Searching...
No Matches
ShipGeoConfig Namespace Reference

Classes

class  AttrDict
class  Config

Functions

Config load_from_root_file (root_file, str key="ShipGeo")

Function Documentation

◆ load_from_root_file()

Config ShipGeoConfig.load_from_root_file ( root_file,
str key = "ShipGeo" )
Load configuration from ROOT file.

Automatically detects and handles both formats:
- New format: JSON string (stored as std::string or TObjString)
- Old format: Pickled Python object

Args:
    root_file: Either a ROOT.TFile object or a string path to ROOT file
    key: The key name for the stored config (default: 'ShipGeo')

Returns:
    Config object with the loaded configuration

Definition at line 103 of file ShipGeoConfig.py.

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 # Get the object (could be std::string or TObjString)
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 # Convert to Python string
132 content_str = str(config_obj)
133
134 # Auto-detect format by checking first character
135 if content_str.startswith("{"):
136 # JSON format - parse it
137 config = Config()
138 config.loads_json(content_str)
139 else:
140 # Assume pickle format - unpickle it
141 # Convert to bytes for pickle (using latin-1 encoding)
142 pickle_bytes = content_str.encode("latin-1")
143 config = pickle.loads(pickle_bytes)
144
145 # Ensure it's a Config object (might be if it was pickled as Config)
146 if not isinstance(config, Config):
147 # Wrap in Config if needed
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()