File indexing completed on 2023-03-17 11:15:49
0001 from PhysicsTools.HeppyCore.framework.services.service import Service
0002 from ROOT import TFile
0003
0004 class TFileService(Service):
0005 """TFile service.
0006 The file attribute is a TFile that can be used in several analyzers.
0007 The file is closed when the service stops.
0008
0009 Example configuration:
0010
0011 output_rootfile = cfg.Service(
0012 TFileService,
0013 'myhists',
0014 fname='histograms.root',
0015 option='recreate'
0016 )
0017 """
0018 def __init__(self, cfg, comp, outdir):
0019 """
0020 cfg must contain:
0021 - fname: file name
0022 - option: TFile options, e.g. recreate
0023
0024 outdir is the output directory for the TFile
0025
0026 comp is a dummy parameter here.
0027 It is needed because the looper creates services and analyzers
0028 in the same way, providing the configuration (cfg),
0029 the component currently processed (comp),
0030 and the output directory.
0031
0032 Other implementations of the TFileService could
0033 make use of the component information, eg. the component name.
0034 """
0035 fname = '/'.join([outdir, cfg.fname])
0036 self.file = TFile(fname, cfg.option)
0037
0038 def stop(self):
0039 self.file.Write()
0040 self.file.Close()
0041