Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:10:29

0001 from __future__ import print_function
0002 import sys, os, optparse, re, json
0003 import ROOT, xmlrpclib
0004  
0005 SERVER_URL = "http://pccmsdqm04.cern.ch/runregistry/xmlrpc"
0006 
0007 ONLINE_DATASET = '/Global/Online/ALL'
0008 
0009 # FOLDER UNDER EvInfo        NAME    SUMMARY VALUE IN EvInfo
0010 FOLDERS     = { 
0011   'reportSummaryContents': ( 'DQM',  'reportSummary' ),
0012   'CertificationContents': ( 'CERT', 'CertificationSummary' ),
0013   'DAQContents':           ( 'DAQ',  'DAQSummary' ),
0014   'DCSContents':           ( 'DCS',  'DCSSummary' )
0015 }
0016 
0017 SUBSYSTEMS  = {
0018   'CSC' :        'CSC',
0019   'DT' :         'DT',
0020   'ES' :         'ES',
0021   'EcalBarrel' : 'ECAL',
0022   'EcalEndcap' : 'ECAL',
0023   'Hcal' :       'HCAL',
0024   'L1T' :        'L1T',
0025   'L1TEMU' :     'L1T',
0026   'Pixel' :      'PIX',
0027   'RPC' :        'RPC',
0028   'SiStrip' :    'STRIP'
0029 }
0030 
0031 def getDatasetName(file_name):
0032   """ Method to get dataset name from the file name"""
0033   d = None
0034   try:
0035     d = re.search("(__[a-zA-Z0-9-_]+)+", file_name).group(0)
0036     d = re.sub("__", "/", d)
0037   except:
0038     d = None
0039   return d
0040 
0041 def getSummaryValues(file_name, translate, filters = None):
0042   """ Method to extract keys from root file and return dict """
0043   ROOT.gROOT.Reset()
0044 
0045   run_number = None
0046   result = {}
0047 
0048   f = ROOT.TFile(file_name, 'READ')
0049 
0050   root = f.GetDirectory("DQMData")
0051   if root == None: return (run_number, result)
0052   
0053   run = None
0054   for key in root.GetListOfKeys():
0055     if re.match("^Run [0-9]+$", key.ReadObj().GetName()) and key.IsFolder():
0056       run_number = int(re.sub("^Run ", "", key.ReadObj().GetName()))
0057       run = key.ReadObj()
0058       break
0059 
0060   if run == None: return (run_number, result)
0061 
0062   for sub in run.GetListOfKeys():
0063 
0064     sub_name = sub.ReadObj().GetName()
0065     if sub_name not in SUBSYSTEMS: continue
0066 
0067     sub_key = sub_name
0068     if translate:
0069       sub_key = SUBSYSTEMS[sub_name]
0070 
0071     if filters != None:
0072       if not re.match(filters[0], sub_key):
0073         continue
0074     
0075     if sub_key not in result:
0076       result[sub_key] = {}
0077 
0078     evInfo = sub.ReadObj().GetDirectory("Run summary/EventInfo")
0079     if evInfo == None: continue
0080 
0081     for folder_name in FOLDERS.keys():
0082 
0083       folder = evInfo.GetDirectory(folder_name)
0084       if folder == None: continue
0085       
0086       folder_id = folder_name
0087       if translate:
0088         folder_id = FOLDERS[folder_name][0]
0089       
0090       if filters != None:
0091         if not re.match(filters[1], folder_id):
0092           continue
0093     
0094       if folder_id not in result[sub_key]:
0095         result[sub_key][folder_id] = {}
0096 
0097       value_filter = None
0098       if filters != None:
0099         value_filter = filters[2]
0100 
0101       writeValues(folder, result[sub_key][folder_id], None, value_filter)
0102       writeValues(evInfo, result[sub_key][folder_id], {FOLDERS[folder_name][1]: 'Summary'}, value_filter)
0103 
0104   f.Close()
0105 
0106   return (run_number, result)
0107 
0108 def writeValues(folder, map, keymap = None, filter = None):
0109   """ Write values (possibly only for the keys in the keymap and filtered) from folder to map """
0110   for value in folder.GetListOfKeys():
0111     full_name = value.ReadObj().GetName()
0112     if not value.IsFolder() and re.match("^<.+>f=-{,1}[0-9\.]+</.+>$", full_name):
0113       value_name = re.sub("<(?P<n>[^>]+)>.+", "\g<n>", full_name)
0114       value_numb = float(re.sub("<.+>f=(?P<n>-{,1}[0-9\.]+)</.+>", "\g<n>", full_name))
0115       if keymap == None or value_name in keymap:
0116         if not keymap == None:
0117           if not keymap[value_name] == None:
0118             value_name = keymap[value_name]
0119         if filter == None or re.match(filter, value_name):
0120           if value_name not in map:
0121             map[value_name] = value_numb
0122 
0123 def checkFilter(raw_filter):
0124   """ Check if filter is OK """
0125   if raw_filter != None:
0126     try:
0127       filter = eval(raw_filter)
0128       if not isinstance("", type(filter[0])) or not isinstance("", type(filter[1])) or not isinstance("", type(filter[2])):
0129         raise TypeError('')
0130     except:
0131       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.")
0132       sys.exit(2)
0133   else:
0134     filter = ('.*', '.*', '.*')
0135   return filter