ATLAS Offline Software
hist_bookkeep_utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 #
5 # utilities for book-keeping xml histograms
6 
7 import re
8 
9 
10 def weed(line):
11  line=line.strip()
12  line=re.sub(' +',' ',line)
13  line=re.sub(' =','=',line)
14  line=re.sub('= ','=',line)
15  return line
16 
17 
18 
19 def get_val(_key,line):
20  _val=""
21  line=weed(line)
22  pieces=line.split(" ")
23  for piece in pieces:
24  if piece.startswith(_key+"="):
25  _val=piece.split("\"")[1]
26  return _val
27 
28 
29 
30 def get_hbuff(_id,_infname,_withcomment=True):
31  # buffering the xml definition lines:
32  _buff=[]
33  _dobuff=False
34  # buffering the comment lines
35  _commbuff=[]
36  _docommbuff=False
37 
38  # loop over file to find histo-s with id xml and comment:
39  with open(_infname, 'r') as _f:
40  for _line in _f:
41  _wline=weed(_line)
42  if _wline.startswith("<h id=\""+_id+"\""):
43  _dobuff=True
44  _buff=_commbuff
45  elif _wline.startswith("</h>"):
46  if (_dobuff):
47  _buff.append(_line)
48  # prepend comment
49  _dobuff=False
50  _commbuff=[]
51  _docommbuff=False
52  elif _withcomment and _wline.startswith("<!--"):
53  if not _dobuff:
54  _commbuff.append(_line)
55  if ( not re.search("-->",_wline) ):
56  # multi-line comment
57  _docommbuff=True
58  else:
59  _docommbuff=False
60  else:
61  if (_docommbuff):
62  _commbuff.append(_line)
63  if ( re.search("-->",_wline) ):
64  _docommbuff=False
65  elif not is_xml_form(_wline):
66  print('Warning', _infname, 'non-xml formatted line :', _line)
67 
68  # buffer histo lines here:
69  if (_dobuff):
70  _buff.append(_line)
71 
72  return _buff
73 
74 
75 
77 def get_comm_def(_buff):
78  _comm_def=[[],[]]
79  _iscomm=False
80  for _bitem in _buff:
81  if _bitem.strip().startswith("<!--"):
82  _iscomm=True
83  _comm_def[0].append(_bitem)
84  if ( re.search("-->",_bitem) ):
85  _iscomm=False
86  elif ( _iscomm):
87  _comm_def[0].append(_bitem)
88  if re.search("-->",_bitem):
89  _iscomm=False
90  else:
91  _comm_def[1].append(_bitem)
92 
93  return _comm_def
94 
95 
97 def is_xml_form(_line):
98  _line=_line.strip()
99  if _line.startswith("<") or _line.endswith(">"):
100  # assume these are .xml-s
101  return True
102  if not _line:
103  # empty line, that is '' after strip
104  return True
105  # any other exceptions?
106  if _line.startswith("&"):
107  # including daughters
108  return True
109  else:
110  return False
hist_bookkeep_utils.get_hbuff
def get_hbuff(_id, _infname, _withcomment=True)
Definition: hist_bookkeep_utils.py:30
hist_bookkeep_utils.get_comm_def
def get_comm_def(_buff)
Definition: hist_bookkeep_utils.py:77
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
hist_bookkeep_utils.weed
def weed(line)
Definition: hist_bookkeep_utils.py:10
hist_bookkeep_utils.get_val
def get_val(_key, line)
Definition: hist_bookkeep_utils.py:19
hist_bookkeep_utils.is_xml_form
def is_xml_form(_line)
Definition: hist_bookkeep_utils.py:97
Trk::open
@ open
Definition: BinningType.h:40
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70