ATLAS Offline Software
test_athena_variable_shape_ntuple.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 import os
4 from AthenaCommon.Utils import unixtools
5 
6 if 'FNAMES' not in dir():
7  paths = os.getenv('DATAPATH').split(os.pathsep) + [os.getenv('ATLAS_REFERENCE_DATA','')]
8  testdir = unixtools.find_datafile(os.getenv('ATLAS_REFERENCE_TAG'), paths)
9  FNAMES = [os.path.join(testdir, f) for f in ('ntuple.0.root', 'ntuple.1.root')]
10 
11 if not isinstance(FNAMES, (list,tuple)):
12  FNAMES = [FNAMES]
13  pass
14 
15 import AthenaRootComps.ReadAthenaRoot
16 svcMgr.EventSelector.InputCollections = FNAMES
17 svcMgr.EventSelector.TupleName = "egamma"
18 
19 from AthenaCommon.AlgSequence import AlgSequence
20 job = AlgSequence()
21 
22 from AthenaPython import PyAthena
23 StatusCode = PyAthena.StatusCode
24 
26 
27  def __init__(self, name='MyAlg', **kw):
28  kw['name'] = name
29  self.activeBranches = kw.get(
30  'activeBranches',
31  ['RunNumber',
32  'EventNumber',
33  'el_n',
34  'el_eta',
35  'el_jetcone_dr',
36  ])
37  self.fname = kw.get('fname', 'data.var.txt')
38  super(MyAlg, self).__init__(**kw)
39  return
40 
41  def initialize(self):
42  self.evtstore = PyAthena.py_svc('StoreGateSvc')
43  self.fd = open(self.fname, 'w')
44  return StatusCode.Success
45 
46  def execute(self):
47  self.msg.info('running execute...')
48  keys = []
49  for p in self.evtstore.proxies():
50  if not p.isValid():
51  continue
52  keys.append(p.name())
53  for br in self.activeBranches:
54  try:
55  if br not in keys:
56  raise KeyError("no such object [%s] in store" % br)
57  o = self.evtstore[br]
58  if hasattr(o, 'at'):
59  o = list(o)
60  for i,v in enumerate(o):
61  if hasattr(v, 'at') and not isinstance(v, str):
62  o[i] = list(v)
63  self.msg.info('%s: %r', br, o)
64  print ("%s: %r" % (br, o), file=self.fd)
65  except Exception as err:
66  self.msg.info(' --> err for [%s]: %s' % (br, err))
67  pass
68  return StatusCode.Success
69 
70  def finalize(self):
71  if hasattr(self, 'fd') and self.fd:
72  self.fd.flush()
73  self.fd.close()
74  return StatusCode.Success
75 
76  pass # MyAlg
77 
78 from AthenaCommon.AlgSequence import AlgSequence
79 job = AlgSequence()
80 job += MyAlg('py_alg')
81 
82 if 'BRANCHES' in dir():
83  job.py_alg.activeBranches = BRANCHES
84  pass
85 
86 if not 'EVTMAX' in dir():
87  EVTMAX=-1
88 theApp.EvtMax = EVTMAX
89 
90 if not 'DOWRITE' in dir():
91  DOWRITE=1
92 if DOWRITE:
93  svcMgr += CfgMgr.DecisionSvc()
94  import AthenaRootComps.WriteAthenaRoot as arcw
95  out = arcw.createNtupleOutputStream("StreamD3PD", "d3pd.root", "egamma")
96  if 'OUTBRANCHES' not in dir():
97  OUTBRANCHES=[
98  "el_n",
99  "el_eta",
100  "el_jetcone_dr",
101  ]
102  out.ItemList += OUTBRANCHES
103 
grepfile.info
info
Definition: grepfile.py:38
FullCPAlgorithmsTest_eljob.flush
flush
Definition: FullCPAlgorithmsTest_eljob.py:186
test_athena_variable_shape_ntuple.MyAlg
Definition: test_athena_variable_shape_ntuple.py:25
test_athena_variable_shape_ntuple.MyAlg.evtstore
evtstore
Definition: test_athena_variable_shape_ntuple.py:42
python.AlgSequence.AlgSequence
AlgSequence
Definition: PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py:7
test_athena_variable_shape_ntuple.MyAlg.fname
fname
Definition: test_athena_variable_shape_ntuple.py:37
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
test_athena_variable_shape_ntuple.MyAlg.activeBranches
activeBranches
Definition: test_athena_variable_shape_ntuple.py:29
test_athena_variable_shape_ntuple.MyAlg.fd
fd
Definition: test_athena_variable_shape_ntuple.py:43
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
test_athena_variable_shape_ntuple.MyAlg.__init__
def __init__(self, name='MyAlg', **kw)
Definition: test_athena_variable_shape_ntuple.py:27
beamspotman.dir
string dir
Definition: beamspotman.py:623
Trk::open
@ open
Definition: BinningType.h:40
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
PyAthena::Alg
Definition: PyAthenaAlg.h:33
Trk::split
@ split
Definition: LayerMaterialProperties.h:38