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 95 of file ShipGeoConfig.py.

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