CMS 3D CMS Logo

contentValuesLib.py
Go to the documentation of this file.
1 import sys, os, optparse, re, json
2 import ROOT, xmlrpclib
3 
4 SERVER_URL = "http://pccmsdqm04.cern.ch/runregistry/xmlrpc"
5 
6 ONLINE_DATASET = '/Global/Online/ALL'
7 
8 # FOLDER UNDER EvInfo NAME SUMMARY VALUE IN EvInfo
9 FOLDERS = {
10  'reportSummaryContents': ( 'DQM', 'reportSummary' ),
11  'CertificationContents': ( 'CERT', 'CertificationSummary' ),
12  'DAQContents': ( 'DAQ', 'DAQSummary' ),
13  'DCSContents': ( 'DCS', 'DCSSummary' )
14 }
15 
16 SUBSYSTEMS = {
17  'CSC' : 'CSC',
18  'DT' : 'DT',
19  'ES' : 'ES',
20  'EcalBarrel' : 'ECAL',
21  'EcalEndcap' : 'ECAL',
22  'Hcal' : 'HCAL',
23  'L1T' : 'L1T',
24  'L1TEMU' : 'L1T',
25  'Pixel' : 'PIX',
26  'RPC' : 'RPC',
27  'SiStrip' : 'STRIP'
28 }
29 
30 def getDatasetName(file_name):
31  """ Method to get dataset name from the file name"""
32  d = None
33  try:
34  d = re.search("(__[a-zA-Z0-9-_]+)+", file_name).group(0)
35  d = re.sub("__", "/", d)
36  except:
37  d = None
38  return d
39 
40 def getSummaryValues(file_name, translate, filters = None):
41  """ Method to extract keys from root file and return dict """
42  ROOT.gROOT.Reset()
43 
44  run_number = None
45  result = {}
46 
47  f = ROOT.TFile(file_name, 'READ')
48 
49  root = f.GetDirectory("DQMData")
50  if root == None: return (run_number, result)
51 
52  run = None
53  for key in root.GetListOfKeys():
54  if re.match("^Run [0-9]+$", key.ReadObj().GetName()) and key.IsFolder():
55  run_number = int(re.sub("^Run ", "", key.ReadObj().GetName()))
56  run = key.ReadObj()
57  break
58 
59  if run == None: return (run_number, result)
60 
61  for sub in run.GetListOfKeys():
62 
63  sub_name = sub.ReadObj().GetName()
64  if sub_name not in SUBSYSTEMS: continue
65 
66  sub_key = sub_name
67  if translate:
68  sub_key = SUBSYSTEMS[sub_name]
69 
70  if filters != None:
71  if not re.match(filters[0], sub_key):
72  continue
73 
74  if sub_key not in result:
75  result[sub_key] = {}
76 
77  evInfo = sub.ReadObj().GetDirectory("Run summary/EventInfo")
78  if evInfo == None: continue
79 
80  for folder_name in FOLDERS.keys():
81 
82  folder = evInfo.GetDirectory(folder_name)
83  if folder == None: continue
84 
85  folder_id = folder_name
86  if translate:
87  folder_id = FOLDERS[folder_name][0]
88 
89  if filters != None:
90  if not re.match(filters[1], folder_id):
91  continue
92 
93  if folder_id not in result[sub_key]:
94  result[sub_key][folder_id] = {}
95 
96  value_filter = None
97  if filters != None:
98  value_filter = filters[2]
99 
100  writeValues(folder, result[sub_key][folder_id], None, value_filter)
101  writeValues(evInfo, result[sub_key][folder_id], {FOLDERS[folder_name][1]: 'Summary'}, value_filter)
102 
103  f.Close()
104 
105  return (run_number, result)
106 
107 def writeValues(folder, map, keymap = None, filter = None):
108  """ Write values (possibly only for the keys in the keymap and filtered) from folder to map """
109  for value in folder.GetListOfKeys():
110  full_name = value.ReadObj().GetName()
111  if not value.IsFolder() and re.match("^<.+>f=-{,1}[0-9\.]+</.+>$", full_name):
112  value_name = re.sub("<(?P<n>[^>]+)>.+", "\g<n>", full_name)
113  value_numb = float(re.sub("<.+>f=(?P<n>-{,1}[0-9\.]+)</.+>", "\g<n>", full_name))
114  if keymap == None or value_name in keymap:
115  if not keymap == None:
116  if not keymap[value_name] == None:
117  value_name = keymap[value_name]
118  if filter == None or re.match(filter, value_name):
119  if value_name not in map:
120  map[value_name] = value_numb
121 
122 def checkFilter(raw_filter):
123  """ Check if filter is OK """
124  if raw_filter != None:
125  try:
126  filter = eval(raw_filter)
127  if not isinstance("", type(filter[0])) or not isinstance("", type(filter[1])) or not isinstance("", type(filter[2])):
128  raise TypeError('')
129  except:
130  print "Bad filter value ", raw_filter, ".\nFilter should be written in python tupple with 3 elements, i.e. \"('subsystem','folder','value')\". elements are in regexp format."
131  sys.exit(2)
132  else:
133  filter = ('.*', '.*', '.*')
134  return filter
def getDatasetName(file_name)
def writeValues(folder, map, keymap=None, filter=None)
def getSummaryValues(file_name, translate, filters=None)
def checkFilter(raw_filter)