CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
checkRuns.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys,os,commands,re
3 import xmlrpclib
4 from CommonMethods import *
5 try: # FUTURE: Python 2.6, prior to 2.6 requires simplejson
6  import json
7 except:
8  try:
9  import simplejson as json
10  except:
11  print "Please set a crab environment in order to get the proper JSON lib"
12  sys.exit(1)
13 
14 #####################################################################################
15 def getListOfRunsAndLumiFromFile(firstRun=-1,fileName=""):
16  file = open(fileName);
17  jsonFile = file.read();
18  file.close()
19  jsonList=json.loads(jsonFile);
20 
21  selected_dcs = {};
22  for element in jsonList:
23  selected_dcs[long(element)]=jsonList[element]
24  return selected_dcs
25 
26 #####################################################################################
27 def getListOfRunsAndLumiFromRR(firstRun=-1,error=""):
28  RunReg ="http://pccmsdqm04.cern.ch/runregistry"
29  #RunReg = "http://localhost:40010/runregistry"
30  #Dataset=%Online%
31  Group = "Collisions10"
32 
33  # get handler to RR XML-RPC server
34  FULLADDRESS=RunReg + "/xmlrpc"
35  #print "RunRegistry from: ",FULLADDRESS
36  #firstRun = 153000
37  server = xmlrpclib.ServerProxy(FULLADDRESS)
38  #sel_runtable="{groupName} ='" + Group + "' and {runNumber} > " + str(firstRun) + " and {datasetName} LIKE '" + Dataset + "'"
39  sel_runtable="{groupName} ='" + Group + "' and {runNumber} > " + str(firstRun)
40 
41  tries = 0;
42  maxAttempts = 3
43  while tries<maxAttempts:
44  try:
45  run_data = server.DataExporter.export('RUN', 'GLOBAL', 'csv_runs', sel_runtable)
46  break
47  except:
48  tries += 1
49  print "Trying to get run data. This fails only 2-3 times so don't panic yet...", tries, "/", maxAttempts
50  time.sleep(1)
51  print "Exception type: ", sys.exc_info()[0]
52  if tries==maxAttempts:
53  error = "Ok, now panic...run registry unaccessible...I'll get the runs from a json file!"
54  print error;
55  return {};
56 
57  listOfRuns=[]
58  runErrors = {}
59  for line in run_data.split("\n"):
60  run=line.split(',')[0]
61  if run.isdigit():
62  listOfRuns.append(run)
63 
64  tries = 0
65  maxAttempts = 3
66  firstRun = listOfRuns[len(listOfRuns)-1];
67  lastRun = listOfRuns[0];
68  sel_dcstable="{groupName} ='" + Group + "' and {runNumber} >= " + str(firstRun) + " and {runNumber} <= " + str(lastRun) + " and {parDcsBpix} = 1 and {parDcsFpix} = 1 and {parDcsTibtid} = 1 and {parDcsTecM} = 1 and {parDcsTecP} = 1 and {parDcsTob} = 1 and {parDcsEbminus} = 1 and {parDcsEbplus} = 1 and {parDcsEeMinus} = 1 and {parDcsEePlus} = 1 and {parDcsEsMinus} = 1 and {parDcsEsPlus} = 1 and {parDcsHbheA} = 1 and {parDcsHbheB} = 1 and {parDcsHbheC} = 1 and {parDcsH0} = 1 and {parDcsHf} = 1"
69  while tries<maxAttempts:
70  try:
71  dcs_data = server.DataExporter.export('RUNLUMISECTION', 'GLOBAL', 'json' , sel_dcstable)
72  break
73  except:
74  tries += 1
75  print "I was able to get the list of runs and now I am trying to access the detector status", tries, "/", maxAttempts
76  time.sleep(1)
77  print "Exception type: ", sys.exc_info()[0]
78 
79  if tries==maxAttempts:
80  error = "Ok, now panic...run registry unaccessible...I'll get the runs from a json file!"
81  print error;
82  return {};
83 
84  selected_dcs={}
85  jsonList=json.loads(dcs_data)
86 
87  #for element in jsonList:
88  for element in listOfRuns:
89  #if element in listOfRuns:
90  if element in jsonList:
91  selected_dcs[long(element)]=jsonList[element]
92  else:
93  #print "WARNING: Run " + element + " is a collision10 run with 0 lumis in Run Registry!"
94  selected_dcs[long(element)]= [[]]
95  return selected_dcs
96 
97 #####################################################################################
98 def main():
99  filesDir = "LatestRuns/Results/";
100  fileList = ls(filesDir)
101  listOfRunsAndLumi = {};
102  #listOfRunsAndLumi = getListOfRunsAndLumiFromRR(-1);
103  if(not listOfRunsAndLumi):
104  listOfRunsAndLumi = getListOfRunsAndLumiFromFile(-1,"/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions10/7TeV/StreamExpress/Cert_132440-149442_7TeV_StreamExpress_Collisions10_JSON_v3.txt");
105 
106  runKeys = listOfRunsAndLumi.keys();
107  runKeys.sort();
108  runFiles = [];
109  for fileName in fileList:
110  regExp = re.search('(\D+)(\d+)_(\d+)_(\d+).txt',fileName);
111  if(not regExp):
112  error = "Can't find reg exp";
113  exit(error);
114  runFiles.append(long(regExp.group(3)));
115 
116  #for run in runKeys:
117  # if(run not in runFiles):
118  # print "Can't find run", run, "in the files!"
119 
120  runsAndLumisInRR = {};
121  for run in runKeys:
122  RRList = [];
123  for lumiRange in listOfRunsAndLumi[run]:
124  if lumiRange != []:
125  for l in range(lumiRange[0],lumiRange[1]+1):
126  RRList.append(long(l));
127  #print run, "->", RRList;
128  runsAndLumisInRR[run] = RRList;
129 
130  runsAndLumisProcessed = {}
131  for fileName in fileList:
132  file = open(filesDir+fileName)
133  for line in file:
134  if line.find("Runnumber") != -1:
135  run = long(line.replace('\n','').split(' ')[1])
136  elif line.find("LumiRange") != -1:
137  lumiLine = line.replace('\n','').split(' ')
138  begLumi = long(lumiLine[1])
139  endLumi = long(lumiLine[3])
140  if begLumi != endLumi:
141  error = "The lumi range is greater than 1 for run " + str(run) + " " + line + " in file: " + runListDir + fileName
142  exit(error)
143  else:
144  if not run in runsAndLumisProcessed:
145  runsAndLumisProcessed[run] = []
146  if begLumi in runsAndLumisProcessed[run]:
147  print "Lumi " + str(begLumi) + " in event " + str(run) + " already exist. This MUST not happen but right now I will ignore this lumi!"
148  else:
149  runsAndLumisProcessed[run].append(begLumi)
150  file.close()
151  #print run, "->", runsAndLumisProcessed[run];
152 
153  for run in runKeys:
154  missingLumis = [];
155  for lumi in runsAndLumisInRR[run]:
156  #print str(counter) + "->" + str(lumi)
157  #counter += 1
158  if(run not in runFiles):
159  print "Can't find run", run, "in the files!"
160  break ;
161  elif( not lumi in runsAndLumisProcessed[run]):
162  missingLumis.append(lumi)
163  if(len(missingLumis) != 0):
164  print "In run", run, "these lumis are missing ->", missingLumis
165 
166 
167 if __name__ == "__main__":
168  main()
169 
def ls
Definition: eostools.py:348
def getListOfRunsAndLumiFromRR
Definition: checkRuns.py:27
if(c.getParameter< edm::InputTag >("puppiValueMap").label().size()!=0)
def main
Definition: checkRuns.py:98
Definition: main.py:1
def getListOfRunsAndLumiFromFile
Definition: checkRuns.py:15
double split
Definition: MVATrainer.cc:139