ATLAS Offline Software
split_histdefs.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 import argparse
6 
7 from hist_bookkeep_utils import get_val, get_comm_def, weed, get_hbuff
8 
9 parser = argparse.ArgumentParser(
10  description='''
11  take two files and find overlapping histogram definitions.
12  Write out overlapping, different (in 1st file and 2nd file),
13  and exclusive (only 1st file, only 2nd file'''
14 )
15 
16 parser.add_argument('--xml1',
17  dest='in_xml1',
18  type=open,
19  required=True,
20  help='xml 1 file with histogram definitions')
21 
22 parser.add_argument('--xml2',
23  dest='in_xml2',
24  type=open,
25  required=True,
26  help='xml 2 file with histogram definitions')
27 
28 args = parser.parse_args()
29 
30 
33 def compare_idline(_l1,_l2):
34  _t1=[ get_val("id",_l1), get_val("type",_l1)]
35  _t2=[ get_val("id",_l2), get_val("type",_l2)]
36  check=compare_textrange(_t1,_t2,[],[])
37  if ("common"!=check):
38  return "error"
39  return compare_textrange(_t1,_t2,[],[])
40 
41 
42 
43 def compare_textrange(_t1,_t2,_r1,_r2):
44  if (_r1!=_r2):
45  return "rangediff"
46  elif (_t1!=_t2):
47  return "textdiff"
48  else:
49  return "common"
50 
51 
52 
53 def compare_xyline(_l1,_l2):
54  _t1=[get_val("title",_l1)]
55  _t2=[get_val("title",_l2)]
56  _r1=[get_val("lo",_l1),get_val("hi",_l1),get_val("n",_l1)]
57  _r2=[get_val("lo",_l2),get_val("hi",_l2),get_val("n",_l2)]
58  return compare_textrange(_t1,_t2,_r1,_r2)
59 
60 
61 
62 def compare_hbuffs(blist):
63  # strip definitions of comments
64  _buff1=(get_comm_def(blist[0])[1])
65  _buff2=(get_comm_def(blist[1])[1])
66 
67  textdiff=False
68  rangediff=False
69 
70  if not _buff1:
71  return [-1,2]
72  elif not _buff2:
73  return [1,-1]
74 
75  test=compare_idline(_buff1[0],_buff2[0])
76  if ("error"==test):
77  return [-1,-1]
78  elif ("textdiff"==test):
79  textdiff=True
80 
81  get_val("type",_buff1[0])
82  get_val("type",_buff2[0])
83 
84  testx=compare_xyline(_buff1[1],_buff2[1])
85  testy=compare_xyline(_buff1[2],_buff2[2])
86  if ("error"==testx or "error"==testy):
87  return [-1,-1]
88  elif ("textdiff"==testx or "textdiff"==testy):
89  textdiff=True
90  elif ("rangediff"==testx or "rangediff"==testy):
91  rangediff=True
92  if rangediff:
93  return [3,4]
94  elif textdiff:
95  return [5,6]
96  else:
97  return [0,-1]
98 
99 
100 
102 infname1=args.in_xml1.name
103 infname2=args.in_xml2.name
104 
105 outfname_common=infname1.split(".")[0]+"_"+infname2.split(".")[0]+".xml"
106 outfname_1only=infname1.split(".")[0]+"_only.xml"
107 outfname_2only=infname2.split(".")[0]+"_only.xml"
108 outfname_1diffrange=infname1.split(".")[0]+"_diffrange.xml"
109 outfname_2diffrange=infname2.split(".")[0]+"_diffrange.xml"
110 outfname_1difftext=infname1.split(".")[0]+"_difftext.xml"
111 outfname_2difftext=infname2.split(".")[0]+"_difftext.xml"
112 
113 print('')
114 print(' reading histogram xml definitions in ', infname1, infname2)
115 print(' writing histograms only in ', infname1, 'to', outfname_1only)
116 print(' writing histograms only in ', infname2, 'to', outfname_2only)
117 print(' writing histograms range diff in ', infname1, 'to', outfname_1diffrange)
118 print(' writing histograms range diff in ', infname2, 'to', outfname_2diffrange)
119 print(' writing histograms text diff in ', infname1, 'to', outfname_1difftext)
120 print(' writing histograms text diff in ', infname2, 'to', outfname_2difftext)
121 print(' writing histograms common to both to', outfname_common)
122 print('')
123 
124 outf_common=open(outfname_common, 'w')
125 outf_1only=open(outfname_1only, 'w')
126 outf_2only=open(outfname_2only, 'w')
127 outf_1diffrange=open(outfname_1diffrange, 'w')
128 outf_2diffrange=open(outfname_2diffrange, 'w')
129 outf_1difftext=open(outfname_1difftext, 'w')
130 outf_2difftext=open(outfname_2difftext, 'w')
131 
132 
134 buff_comp_dict={0:outf_common,
135  1:outf_1only,2:outf_2only,
136  3:outf_1diffrange,4:outf_2diffrange,
137  5:outf_1difftext,6:outf_2difftext}
138 
139 
140 buff1=[]
141 buff2=[]
142 
143 for line in args.in_xml1:
144  if line.strip().startswith("<h"):
145  line=weed(line)
146  id=get_val("id",line)
147  buff1=get_hbuff(id,infname1,True)
148  buff2=get_hbuff(id,infname2,True)
149  # compare buffers
150  buffs=[buff1,buff2]
151  compvals=compare_hbuffs(buffs)
152  # write contents acc to the outcome of buffer-buffer comparison
153  for _val,_buff in zip(compvals,buffs):
154  for dkey,dval in buff_comp_dict.iteritems():
155  if (_val==dkey):
156  for _bitem in _buff:
157  dval.write(_bitem)
158  # clear all the buffers for the next comparison
159  buff1=[]
160  buff2=[]
161 
162 
163 
165 buff1=[]
166 buff2=[]
167 for line in args.in_xml2:
168  line=weed(line)
169  if line.strip().startswith("<h"):
170  id=get_val("id",line)
171  buff1=get_hbuff(id,infname1,True)
172  buff2=get_hbuff(id,infname2,True)
173  if not buff1:
174  for _bitem in buff2:
175  buff_comp_dict[2].write(_bitem)
176  # clear all the buffers for the next comparison
177  buff1=[]
178  buff2=[]
179 
180 
181 
183 for key,of in buff_comp_dict.iteritems():
184  of.close()
185 
186 print('')
187 print('all done')
188 print('')
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
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
split_histdefs.compare_hbuffs
def compare_hbuffs(blist)
Definition: split_histdefs.py:62
python.ByteStreamConfig.write
def write
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:248
split_histdefs.compare_xyline
def compare_xyline(_l1, _l2)
Definition: split_histdefs.py:53
split_histdefs.compare_textrange
def compare_textrange(_t1, _t2, _r1, _r2)
Definition: split_histdefs.py:43
Trk::open
@ open
Definition: BinningType.h:40
split_histdefs.compare_idline
def compare_idline(_l1, _l2)
========================================================
Definition: split_histdefs.py:33
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70