ATLAS Offline Software
trigbs_updateBSMetadata.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
4 #
5 
6 '''
7 Write a copy of a ByteStream file with updated metadata
8 '''
9 
10 import sys
11 import argparse
12 import logging
13 import eformat
14 import libpyevent_storage as EventStorage
15 import six
16 
17 
18 # This mapping was found in a comment in PyUtils.MetaReader
19 beam_type_dict = {
20  'none': 0,
21  'protons': 1,
22  'ions': 2
23 }
24 
25 
26 def get_parser():
27  parser = argparse.ArgumentParser(usage='%(prog)s [options] FILE',
28  description=__doc__,
29  formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=36, width=100))
30  parser.add_argument('file',
31  metavar='FILE',
32  help='input ByteStream file')
33  parser.add_argument('--outputName', '-o',
34  metavar='NAME', action='store',
35  help='Output file name base. If not specified, created automatically from metadata')
36  parser.add_argument('--copyFrom', '-c',
37  metavar='FILE', action='store',
38  help='Copy metadata from other ByteStream file')
39  parser.add_argument('--numEvents', '-n',
40  metavar='NUM', action='store', type=int,
41  help='Copy only first NUM events from input to output')
42  parser.add_argument('--runNumber', '-r',
43  metavar='NUM', action='store', type=int,
44  help='Change run number')
45  parser.add_argument('--stream', '-s',
46  metavar='NAME', action='store',
47  help='Change stream name')
48  parser.add_argument('--projectTag', '-p',
49  metavar='TAG', action='store',
50  help='Change project tag')
51  parser.add_argument('--beamType', '-b',
52  metavar='TYPE', action='store',
53  choices=beam_type_dict.keys(),
54  help='Change beam type, possible values are: %(choices)s')
55  parser.add_argument('--beamEnergy', '-e',
56  metavar='ENERGY', action='store', type=int,
57  help='Change beam energy')
58  parser.add_argument('--lumiBlock', '-l',
59  metavar='LB', action='store', type=int,
60  help='Change lumi block number')
61  parser.add_argument('--triggerType', '-t',
62  metavar='TT', action='store', type=int,
63  help='Change trigger type')
64  parser.add_argument('--detectorMask', '-m',
65  metavar='MASK', action='store', type=int,
66  help='Change detector mask')
67  parser.add_argument('--verbose', '-v',
68  action='store_true',
69  help='Increase output verbosity')
70  return parser
71 
72 
73 def main():
74  args = get_parser().parse_args()
75  logging.basicConfig(stream=sys.stdout,
76  format='%(levelname)-8s %(message)s',
77  level=logging.DEBUG if args.verbose else logging.INFO)
78 
79  if args.copyFrom:
80  logging.info('Reading events from %s and metadata from %s', args.file, args.copyFrom)
81  else:
82  logging.info('Reading events and metadata from %s', args.file)
83  meta_input = args.copyFrom if args.copyFrom else args.file
84  reader = EventStorage.pickDataReader(meta_input)
85  input_stream = eformat.istream(args.file)
86 
87  # Read metadata from input file
88  metadata_basic = {} # arguments for eformat.ostream
89  metadata_extra = {} # metadata passed as dictionary
90  metadata_basic['runNumber'] = reader.runNumber()
91  metadata_basic['triggerType'] = reader.triggerType()
92  metadata_basic['detectorMask'] = reader.detectorMask()
93  metadata_basic['beamType'] = reader.beamType()
94  metadata_basic['beamEnergy'] = reader.beamEnergy()
95  metadata_extra['Stream'] = reader.stream()
96  metadata_extra['Project'] = reader.projectTag()
97  metadata_extra['LumiBlock'] = reader.lumiblockNumber()
98 
99  logging.debug('Input metadata_basic = %s', metadata_basic)
100  logging.debug('Input metadata_extra = %s', metadata_extra)
101 
102  # Change metadata
103  if args.runNumber:
104  metadata_basic['runNumber'] = args.runNumber
105  if args.triggerType:
106  metadata_basic['triggerType'] = args.triggerType
107  if args.detectorMask:
108  metadata_basic['detectorMask'] = args.detectorMask
109  if args.beamType:
110  metadata_basic['beamType'] = beam_type_dict[args.beamType]
111  if args.beamEnergy:
112  metadata_basic['beamEnergy'] = args.beamEnergy
113  if args.stream:
114  metadata_extra['Stream'] = args.stream
115  if args.projectTag:
116  metadata_extra['Project'] = args.projectTag
117  if args.lumiBlock:
118  metadata_extra['LumiBlock'] = args.lumiBlock
119 
120  logging.debug('Updated metadata_basic = %s', metadata_basic)
121  logging.debug('Updated metadata_extra = %s', metadata_extra)
122 
123  # Create new file name
124  file_name_base = args.outputName
125  if not file_name_base:
126  # Get the name elements
127  ptag = metadata_extra['Project']
128  runno = metadata_basic['runNumber']
129  stream = metadata_extra['Stream']
130  lbn = metadata_extra['LumiBlock']
131  # Build the name
132  file_name_list = []
133  file_name_list.append(ptag if ptag else 'data')
134  file_name_list.append('{:08d}'.format(runno if runno else 0))
135  file_name_list.append(stream if stream else 'unknown_stream')
136  file_name_list.append('lb{:04d}'.format(lbn if lbn else 0))
137  file_name_base = '.'.join(file_name_list)
138 
139  # Write the new file
140  metadata_extra_strings = ['{:s}={:s}'.format(k, str(v)) for k, v in six.iteritems(metadata_extra)]
141  output_stream = eformat.ostream(
142  core_name = file_name_base,
143  run_number = metadata_basic['runNumber'],
144  trigger_type = metadata_basic['triggerType'],
145  detector_mask = metadata_basic['detectorMask'],
146  beam_type = metadata_basic['beamType'],
147  beam_energy = metadata_basic['beamEnergy'],
148  meta_data_strings = metadata_extra_strings)
149 
150  logging.info('Writing file %s', output_stream.current_filename().replace('.writing', '.data'))
151 
152  ievt = 0
153  nmax = args.numEvents or -1
154  for event in input_stream:
155  ievt+=1
156  if nmax >= 0 and ievt > nmax:
157  break
158  logging.debug('Writing event %d', ievt)
159  output_stream.write(event)
160 
161 
162 if '__main__' in __name__:
163  sys.exit(main())
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
trigbs_updateBSMetadata.main
def main()
Definition: trigbs_updateBSMetadata.py:73
vtune_athena.format
format
Definition: vtune_athena.py:14
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
confTool.parse_args
def parse_args()
Definition: confTool.py:35
str
Definition: BTagTrackIpAccessor.cxx:11
trigbs_updateBSMetadata.get_parser
def get_parser()
Definition: trigbs_updateBSMetadata.py:26