ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Database
CoolRunQuery
python
output
AtlRunQuerySave.py
Go to the documentation of this file.
1
#!/usr/env python
2
3
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4
#
5
# ----------------------------------------------------------------
6
# Script : AtlRunQuerySave.py
7
# Project: AtlRunQuery
8
# Purpose: Utility to save pickled dictionary
9
# Authors: Andreas Hoecker (CERN), Joerg Stelzer (DESY)
10
# Created: Dec 2, 2008
11
# ----------------------------------------------------------------
12
#
13
# ---------------------------------------------------------------------------------------------------
14
# Creation of Pickled dictionary for output
15
# ---------------------------------------------------------------------------------------------------
16
from
CoolRunQuery.AtlRunQueryQueryConfig
import
QC
17
from
CoolRunQuery.selector.AtlRunQuerySelectorBase
import
DataKey
18
19
import
pickle,sys
20
from
CoolRunQuery.AtlRunQueryRun
import
DataEntry, DataEntryList, Run
21
22
def
CreateResultDict
( runlist ):
23
24
if
len(runlist)==0:
25
return
{}, {}
26
27
print
(
"Creating results in path '%s'"
% QC.datapath )
28
29
# define the header
30
header = []
31
excludelist = []
32
if
Run.showrunnr:
33
header += [DataKey(x)
for
x
in
[
'Run'
,
'Links'
,
'#LB'
]]
34
if
Run.showtime:
35
header += [DataKey(
'Start and endtime'
)]
36
if
Run.showduration:
37
header += [DataKey(
'Duration'
)]
38
header += [k
for
k
in
Run.ShowOrder
if
k
not
in
excludelist]
39
40
AddUpEvents
(runlist)
41
42
SaveResultTxt
(runlist, header)
43
44
dic =
CreateDic
(runlist, header)
45
46
SaveResultAsJson
(dic)
47
48
summary =
CreateSummary
(dic)
49
50
dic_basic =
CreateDictForPickling
(dic)
51
52
SaveTypelessPickleResult
(dic_basic)
53
54
return
dic, summary
55
56
57
def
AddUpEvents
(runlist):
58
# Sum up total event numbers
59
for
run
in
runlist:
60
try
:
61
Run.totevents[0] += int(run.result[
"#Events"
])
62
except
ValueError:
63
Run.totevents[1] += 1
64
65
66
67
def
SaveResultTxt
(runlist, header):
68
# write header to text file
69
f = open(
'%s/QueryResult.txt'
% QC.datapath,
'w'
)
70
print
(
"data keys:"
,
', '
.join([h.ResultKey
for
h
in
header]), file=f)
71
print
(
'number of runs: %i'
% len(runlist), file=f)
72
73
# now get the values for each run and write to file
74
for
r
in
runlist:
75
line = []
76
if
Run.showrunnr:
77
line += [
"%i"
% r.runNr,
""
,
"%i"
% r.lastlb]
78
if
Run.showtime:
79
line += [
"%s"
% r.timestr(
'seconds'
)]
80
if
Run.showduration:
81
line += [
"%s"
% r.durationstr()]
82
for
k
in
Run.ShowOrder:
83
line += [r.data[k.ResultKey]]
84
for
head,item
in
zip(header,line):
85
if
isinstance(item,tuple):
86
item =
'|'
.join([str(x)
for
x
in
item])
87
print
(
'%40s: %s'
% (head.ResultKey, item), file=f)
88
print
(
'\n'
, file=f)
89
f.close()
90
91
92
93
94
def
SaveTypelessPickleResult
(pdic, filename = 'atlrunquery.pickle'):
95
# write pickle output
96
pf = open(
'%s/atlrunquery.pickle'
% QC.datapath,
'wb'
)
97
try
:
98
pickle.dump(pdic, pf)
99
except
Exception
as
e:
100
print
(
'ERROR: could not pickle results dictionary: "%r"'
% e)
101
sys.exit(1)
102
pf.close()
103
104
def
SaveResultAsJson
( result, filename = 'atlrunquery.json'):
105
# write json output
106
107
# ignoring a few large ones for now and one that is not working
108
# large ones should be stored better
109
ignoreForNow = [
110
# "lhc:fillnumber",
111
# "lhc:stablebeams",
112
"lhc:beamenergy"
,
# large
113
"olc:lumi:0"
,
# large
114
"olc:beam1intensity"
,
# large
115
"olc:beam2intensity"
,
# large
116
# "olc:beam1bunches",
117
# "olc:beam2bunches",
118
# "olc:collbunches",
119
"olc:bcidmask"
# broken
120
]
121
122
runs = [r[
"runNr"
]
for
r
in
result[DataKey(
"Run"
)]]
123
store = { runNr:{}
for
runNr
in
runs}
124
125
for
datakey
in
result:
126
key = datakey.pickled()
127
if
key
in
ignoreForNow:
128
print
(
"Not storing in json file: "
, key)
129
continue
130
131
for
(runNr, x)
in
zip(runs, result[datakey]):
132
if
isinstance(x, (DataEntry,DataEntryList)):
133
store[runNr][key] = x.json()
134
else
:
135
store[runNr][key] = x
136
137
with
open(
'%s/atlrunquery.json'
% QC.datapath,
'w'
)
as
pf:
138
try
:
139
import
json
140
json.dump(store, pf)
141
except
Exception
as
e:
142
print
(
'ERROR: could not create json file with results: "%r"'
% e)
143
144
145
def
CreateDic
(runlist, header):
146
# create keys for pickle dictionary
147
dic = {}
148
for
r
in
runlist:
149
for
k
in
header:
150
if
k ==
'Run'
:
151
scontent = {
"runNr"
: r.runNr,
"lastLB"
: r.lastlb,
"dataPeriod"
:
"tbd"
,
"lhcRun"
: r.lhcRun}
152
elif
k ==
'Links'
:
153
scontent =
""
154
elif
k ==
'#LB'
:
155
scontent = (r.lastlb, [(lbtime[0]-r.lbtimes[0][0])*1e-9
for
lbtime
in
r.lbtimes] + [ (r.lbtimes[-1][1]-r.lbtimes[0][0])*1e-9 ] )
156
elif
k ==
'Start and endtime'
:
157
scontent = r.timestr(
'seconds'
)
158
elif
k ==
'Duration'
:
159
scontent = r.durationstr()
160
else
:
161
scontent = r.data[k.ResultKey]
162
dic.setdefault(k,[]).append(scontent)
163
return
dic
164
165
166
def
basic
(v):
167
if
hasattr(v,
'pickled'
):
168
return
v.pickled()
169
return
v
170
171
def
CreateDictForPickling
(dic):
172
dic_basic = {
'Run'
: [ r[
"runNr"
]
for
r
in
dic[DataKey(
'Run'
)]]}
173
174
for
i,r
in
enumerate(dic_basic[
'Run'
]):
175
dic_basic[r] = dict([ ( k.pickled(),
basic
(v[i]) )
for
k,v
in
dic.items()])
176
177
return
dic_basic
178
179
180
181
182
183
def
CreateSummary
(dic):
184
# create summary
185
dicsum = {}
186
for
key, results
in
dic.items():
187
if
key.ResultKey
in
[
'SMK'
,
'HLT PSK'
,
'L1 PSK'
,
'TorCurrent'
,
'SolCurrent'
,
'BGS Key'
,
'#LB'
]:
188
continue
189
if
key.Type==DataKey.DETECTOR:
190
continue
191
for
r
in
results:
192
if
key==
'Run'
:
193
dicsum.setdefault(key,0)
194
dicsum[key] += 1
195
elif
key.Type == DataKey.STREAM:
196
entry = r[0]
197
if
entry
is
None
or
entry.value ==
'n.a.'
:
198
continue
199
dicsum.setdefault(key,[0,0])
200
dicsum[key][0] += entry.value[0]
201
dicsum[key][1] += entry.value[1]
202
else
:
203
try
:
204
ir = int(r)
205
if
key
not
in
dicsum:
206
dicsum[key] = 0
207
dicsum[key] += ir
208
except
(ValueError, TypeError):
209
pass
210
return
dicsum
print
void print(char *figname, TCanvas *c1)
Definition
TRTCalib_StrawStatusPlots.cxx:26
python.output.AtlRunQuerySave.SaveTypelessPickleResult
SaveTypelessPickleResult(pdic, filename='atlrunquery.pickle')
Definition
AtlRunQuerySave.py:94
python.output.AtlRunQuerySave.basic
basic(v)
Definition
AtlRunQuerySave.py:166
python.output.AtlRunQuerySave.AddUpEvents
AddUpEvents(runlist)
Definition
AtlRunQuerySave.py:57
python.output.AtlRunQuerySave.SaveResultAsJson
SaveResultAsJson(result, filename='atlrunquery.json')
Definition
AtlRunQuerySave.py:104
python.output.AtlRunQuerySave.CreateSummary
CreateSummary(dic)
Definition
AtlRunQuerySave.py:183
python.output.AtlRunQuerySave.CreateDic
CreateDic(runlist, header)
Definition
AtlRunQuerySave.py:145
python.output.AtlRunQuerySave.CreateResultDict
CreateResultDict(runlist)
Definition
AtlRunQuerySave.py:22
python.output.AtlRunQuerySave.CreateDictForPickling
CreateDictForPickling(dic)
Definition
AtlRunQuerySave.py:171
python.output.AtlRunQuerySave.SaveResultTxt
SaveResultTxt(runlist, header)
Definition
AtlRunQuerySave.py:67
Generated on
for ATLAS Offline Software by
1.17.0