本文整理匯總了Python中ROOT.TFile方法的典型用法代碼示例。如果您正苦於以下問題:Python ROOT.TFile方法的具體用法?Python ROOT.TFile怎麽用?Python ROOT.TFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ROOT
的用法示例。
在下文中一共展示了ROOT.TFile方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: open_ROOT_file
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def open_ROOT_file(filename):
"""
Open a ROOT file in a context. Will close it no matter what, even if there are exceptions
:param filename:
:return:
"""
f = ROOT.TFile(filename)
try:
yield f
finally:
f.Close()
del f
示例2: test_read_write
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def test_read_write():
df = pd.DataFrame({'x': [1, 2, 3]})
df.to_root('tmp.root')
df_ = read_root('tmp.root')
os.remove('tmp.root')
df.to_root('tmp.root', key='mykey')
df_ = read_root('tmp.root', key='mykey')
assert_frame_equal(df, df_)
os.remove('tmp.root')
tf = ROOT.TFile('tmp.root', 'recreate')
tt = ROOT.TTree("a", "a")
x = np.array([1])
x[0] = 42
tt.Branch('x', x, 'x/D')
tt.Fill()
x[0] = 1
tt.Fill()
tt.Write()
tf.Close()
# Read when no index is present
df = read_root('tmp.root', columns=['x'])
os.remove('tmp.root')
示例3: startup
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def startup(self):
self.config.setdefault('buffer_size', 16000)
self.config.setdefault('output_class_code', True)
output_file = self.config['output_name'] + '.root'
if os.path.exists(output_file):
print("\n\nOutput file %s already exists, overwriting." % output_file)
self.f = ROOT.TFile(output_file, "RECREATE")
self.f.cd()
self.tree_created = False
# Write the metadata to the file as JSON
ROOT.TNamed('pax_metadata', json.dumps(self.processor.get_metadata())).Write()
示例4: load_pax_event_class_from_root
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def load_pax_event_class_from_root(rootfilename):
"""Load the pax event class from the pax root file rootfilename"""
# Open the ROOT file just to get the pax class code out
# We want to suppress the "help me I don't have the right dictionaries" warnings,
# since we want to load the class code to solve this very problem!
with ShutUpROOT():
f = ROOT.TFile(rootfilename)
if 'pax_event_class' not in [x.GetName() for x in list(f.GetListOfKeys())]:
raise exceptions.MaybeOldFormatException("Root file %s does not contain pax event class code.\n "
"Maybe it was made before March 2016? See #323." % rootfilename)
load_event_class_code(f.Get('pax_event_class').GetTitle())
f.Close()
示例5: startup
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def startup(self):
if not have_root:
raise RuntimeError("Can't read MC ROOT files if you do not have root!")
self.config.setdefault('add_to_z', 0)
self.log.warning('This plugin is completely untested and will probably crash!')
filename = self.config['input_name']
self.f = ROOT.TFile(utils.data_file_name(filename))
self.t = self.f.Get("events/events") # new MC structure, 160622
WaveformSimulator.startup(self)
self.number_of_events = self.t.GetEntries() * self.config['event_repetitions']
示例6: get_list_of_keys
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def get_list_of_keys(root_file, dir=""):
"""
Given a ROOT file, it returns the list of object names contained in the file in the provided directory.
:param root_file: a ROOT.TFile instance
:param dir: the directory (default: "", i.e., the root of the file)
:return: a list of object names
"""
root_file.cd(dir)
return [key.GetName() for key in ROOT.gDirectory.GetListOfKeys()]
示例7: test_root_output
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def test_root_output(self):
# Get an event
mypax = core.Processor(config_names='XENON100', config_dict={'pax': {
'events_to_process': [0],
'output': 'Dummy.DummyOutput',
'encoder_plugin': None}})
mypax.run()
event = mypax.get_plugin_by_name('DummyOutput').last_event
del mypax
# Write same event to ROOT
mypax = core.Processor(config_names='XENON100', config_dict={'pax': {
'events_to_process': [0],
'output_name': 'test_root_output'}})
mypax.run()
del mypax
self.assertTrue(os.path.exists('test_root_output.root'))
self.assertTrue(hasattr(ROOT, 'Peak'))
# Can't test event class loading, event class already loaded during writing
# ROOTClass.load_pax_event_class_from_root('test_root_output.root')
f = ROOT.TFile('test_root_output.root')
t = f.Get('tree')
t.GetEntry(0)
root_event = t.events
self.assertEqual(len(root_event.peaks), len(event.peaks))
for i in range(len(event.peaks)):
peak = event.peaks[i]
root_peak = root_event.peaks[i]
self.assertEqual(peak.type, root_peak.type)
# 5th or 6th significant figure appears to be different.. float precision difference?
self.assertAlmostEqual(peak.area, root_peak.area,
delta=0.0001 * max(1, peak.area))
# Check area per channel
self.assertAlmostEqual(peak.area, peak.area_per_channel.sum())
self.assertAlmostEqual(peak.area, sum(root_peak.area_per_channel),
delta=0.0001 * max(1, peak.area))
np.testing.assert_array_almost_equal(peak.area_per_channel,
np.array(list(root_peak.area_per_channel)),
decimal=4)
示例8: __init__
# 需要導入模塊: import ROOT [as 別名]
# 或者: from ROOT import TFile [as 別名]
def __init__(self, name, veritas_root_data):
# Open file
f = ROOT.TFile(veritas_root_data)
try:
# Loop over the runs
keys = get_list_of_keys(f)
finally:
f.Close()
# Get the names of all runs included
run_names = [x for x in keys if x.find("run") == 0]
self._runs_like = collections.OrderedDict()
for run_name in run_names:
# Build the VERITASRun class
this_run = VERITASRun(veritas_root_data, run_name)
this_run.display()
if this_run.total_counts == 0 or this_run.total_background_counts == 0:
custom_warnings.warn(
"%s has 0 source or bkg counts, cannot use it." % run_name
)
continue
else:
# Get background spectrum and observation spectrum (with response)
# this_observation = this_run.get_spectrum()
# this_background = this_run.get_background_spectrum()
#
# self._runs_like[run_name] = DispersionSpectrumLike(run_name,
# this_observation,
# this_background)
#
# self._runs_like[run_name].set_active_measurements("c50-c130")
self._runs_like[run_name] = this_run
super(VERITASLike, self).__init__(name, {})