CMS 3D CMS Logo

TkMap_script_phase1.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 import sys
5 import os
6 from subprocess import call
7 import os.path
8 import shutil
9 import subprocess
10 import codecs
11 import re
12 import errno
13 from getGTfromDQMFile_V2 import getGTfromDQMFile
14 
15 def setRunDirectory(runNumber):
16  # Don't forget to add an entry when there is a new era
17  dirDict = { 325799:['Data2018', 'HIRun2018'],\
18  315252:['Data2018', 'Run2018'],\
19  308336:['Data2018', 'Commissioning2018'],\
20  294644:['Data2017', 'Run2017'],\
21  290123:['Data2017', 'Commissioning2017'],\
22  284500:['Data2016', 'PARun2016'],\
23  271024:['Data2016', 'Run2016'],\
24  264200:['Data2016', 'Commissioning2016'],\
25  246907:['Data2015', 'Run2015'],\
26  232881:['Data2015', 'Commissioning2015'],\
27  211658:['Data2013', 'Run2013'],\
28  209634:['Data2013', 'HIRun2013'],\
29  190450:['Data2012', 'Run2012']}
30  runKey=0
31  for key in sorted(dirDict):
32  if runNumber > key:
33  runKey=key
34  return dirDict[runKey]
35 
36 def downloadOfflineDQMhisto(run, Run_type,rereco):
37  runDirval=setRunDirectory(run)
38  DataLocalDir=runDirval[0]
39  DataOfflineDir=runDirval[1]
40  nnn=run/100
41  print('Processing '+ Run_type + ' in '+DataOfflineDir+"...")
42  File_Name = ''
43  print('Directory to fetch the DQM file from: https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/')
44  url = 'https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/'
45  os.popen("curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET "+url+" > index.html")
46  f=codecs.open("index.html", 'r')
47  index = f.readlines()
48  if any(str(Run_Number[i]) in s for s in index):
49  for s in index:
50  if rereco:
51  if (str(Run_Number[i]) in s) and ("__DQMIO.root" in s) and ("17Sep2018" in s):
52  File_Name = str(str(s).split("xx/")[1].split("'>DQM")[0])
53  else:
54  if (str(Run_Number[i]) in s) and ("__DQMIO.root" in s):
55  File_Name = str(str(s).split("xx/")[1].split("'>DQM")[0])
56 
57  else:
58  print('No DQM file available. Please check the Offline server')
59  sys.exit(0)
60 
61  print('Downloading DQM file:'+File_Name)
62  os.system('curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/'+File_Name+' > /tmp/'+File_Name)
63 
64  return File_Name
65 
66 def downloadOfflinePCLhisto(run, Run_type):
67  runDirval=setRunDirectory(run)
68  DataLocalDir=runDirval[0]
69  DataOfflineDir=runDirval[1]
70  nnn=run/100
71  print('Processing '+ Run_type + ' in '+DataOfflineDir+"...")
72  File_Name = 'Temp'
73  print('Directory to fetch the DQM file from: https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/')
74  url = 'https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/'
75  os.popen("curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET "+url+" > index.html")
76  f=codecs.open("index.html", 'r')
77  index = f.readlines()
78  if any(str(Run_Number[i]) in s for s in index):
79  for s in index:
80  if (str(Run_Number[i]) in s) and ("PromptCalibProdSiPixel-Express" in s) and ("__ALCAPROMPT.root" in s):
81  File_Name = str(str(s).split("xx/")[1].split("'>DQM")[0])
82  else:
83  print('No DQM file available. Please check the Offline server')
84  sys.exit(0)
85  if File_Name!='Temp':
86  print('Downloading DQM file:'+File_Name)
87  os.system('curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OfflineData/'+DataOfflineDir+'/'+Run_type+'/000'+str(nnn)+'xx/'+File_Name+' > /tmp/'+File_Name)
88 
89  return File_Name
90 
91 
92 def downloadnlineDQMhisto(run, Run_type):
93  runDirval=setRunDirectory(run)
94  DataLocalDir=runDirval[0]
95  DataOfflineDir=runDirval[1]
96  nnn=run/100
97  nnnOnline = run/10000
98  File_Name_online=''
99  deadRocMap = False
100 
101  url1 = 'https://cmsweb.cern.ch/dqm/online/data/browse/Original/000'+str(nnnOnline)+'xxxx/000'+str(nnn)+'xx/'
102  os.popen("curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET "+url1+" > index_online.html")
103 
104  url2 = 'https://cmsweb.cern.ch/dqm/offline/data/browse/ROOT/OnlineData/original/000'+str(nnnOnline)+'xxxx/000'+str(nnn)+'xx/'
105  os.popen("curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET "+url2+" > index_online_backup.html")
106  f_online_backup=codecs.open("index_online_backup.html", 'r') index_online_backup = f_online_backup.readlines()
107 
108  f_online=codecs.open("index_online.html", 'r')
109  index_online = f_online.readlines()
110  if any(str(run) in x for x in index_online):
111  for x in index_online:
112  if (str(run) in x) and ("_PixelPhase1_" in x):
113  File_Name_online=str(str(x).split(".root'>")[1].split("</a></td><td>")[0])
114  deadRocMap = True
115  else:
116  print("Can't find any file in offline server, trying the online server")
117  if any(str(run) in y for y in index_online_backup):
118  for y in index_online:
119  if (str(run) in y) and ("_PixelPhase1_" in y):
120  File_Name_online=str(str(y).split(".root'>")[1].split("</a></td><td>")[0])
121  deadRocMap = True
122  else:
123  print('No Online DQM file available. Skip dead roc map')
124  deadRocMap = False
125 
126  print('Downloading DQM file:'+File_Name_online)
127 
128 
129  os.system('curl -k --cert /data/users/cctrkdata/current/auth/proxy/proxy.cert --key /data/users/cctrkdata/current/auth/proxy/proxy.cert -X GET https://cmsweb.cern.ch/dqm/online/data/browse/Original/000'+str(nnnOnline)+'xxxx/000'+str(nnn)+'xx/'+File_Name_online+' > /tmp/'+File_Name_online)
130 
131  os.remove('index_online.html')
132  os.remove('index_online_backup.html')
133  return deadRocMap, File_Name_online;
134 
135 
136 def getGT(DQMfile, RunNumber, globalTagVar):
137  globalTag_v0 = getGTfromDQMFile(DQMfile, RunNumber, globalTagVar)
138  print("Global Tag: " + globalTag_v0)
139  globalTag = globalTag_v0
140 
141  for z in range(len(globalTag_v0)-2):#clean up the garbage string in the GT
142  if (globalTag_v0[z].isdigit()) and (globalTag_v0[z+1].isdigit()) and (globalTag_v0[z+2].isdigit()) and(globalTag_v0[z+3].isupper()):
143  globalTag = globalTag_v0[z:]
144  break
145  if globalTag == "":
146  print(" No GlobalTag found: trying from DAS.... ")
147  globalTag = str(os.popen('getGTscript.sh '+filepath+ File_Name+' ' +str(Run_Number[i])));
148  if globalTag == "":
149  print(" No GlobalTag found for run: "+str(Run_Number[i]))
150 
151  return globalTag
152 
153 
154 Run_type = sys.argv[1]
155 Run_Number = [int(x) for x in sys.argv[2:]]
156 CMSSW_BASE = str(os.popen('echo ${CMSSW_BASE}').read().strip())
157 rereco=False
159 
160 if Run_type == 'Cosmics' or Run_type == 'StreamExpress' or Run_type == 'StreamExpressCosmics' or Run_type == 'ZeroBias' or Run_type == 'StreamHIExpress' or Run_type == 'HIMinimumBias1' or re.match('ZeroBias([0-9]+?)',Run_type) or re.match('HIMinimumBias([0-9]+?)',Run_type):
161  print(Run_type)
162 elif Run_type == 'ReReco':
163  rereco=True
164  Run_type='ZeroBias'
165 else:
166  print("please enter a valid run type: Cosmics | ZeroBias | StreamExpress | StreamExpressCosmics ")
167  sys.exit(0)
168 
169 for i in range(len(Run_Number)):
170 
171  print("Downloading File!!")
172  nnnOut = Run_Number[i]/1000
174  filepath = '/tmp/'
175  File_Name = downloadOfflineDQMhisto(Run_Number[i], Run_type, rereco)
176  if Run_type=="StreamExpress" or Run_type=="StreamHIExpress":
177  File_Name_PCL = downloadOfflinePCLhisto(Run_Number[i], Run_type)
178  deadRocMap, File_Name_online = downloadnlineDQMhisto(Run_Number[i], Run_type)
180  runDirval=setRunDirectory(Run_Number[i])
181  DataLocalDir=runDirval[0]
182  #DataOfflineDir=runDirval[1]
183 
184 
185 
186  print("get the run status from DQMFile")
187 
188 
189  check_command = 'check_runcomplete '+filepath+File_Name
190  Check_output = subprocess.call(check_command, shell=True)
192 
193  if Check_output == 0:
194  print('Using DQM file: '+File_Name)
195  else:
196  print('*****************Warning: DQM file is not ready************************')
197  input_var = input("DQM file is incompleted, do you want to continue? (y/n): ")
198  if (input_var == 'y') or (input_var == 'Y'):
199  print('Using DQM file: '+File_Name)
200  else:
201  sys.exit(0)
202  if Run_type=="StreamExpress" or Run_type=="StreamHIExpress":
203  if File_Name_PCL=='Temp':
204  print(' ')
205  print(' ')
206  print('*****************Warning: PCL file is not ready************************')
207  input_var = input("PCL file is not ready, you will need to re-run the script later for PCL plots, do you want to continue? (y/n): ")
208  if (input_var == 'y') or (input_var == 'Y'):
209  print('--------> Remember to re-run the script later!!!!!')
210  else:
211  sys.exit(0)
212 
213 
214 
215  checkfolder = os.path.exists(str(Run_Number[i]))
216  if checkfolder == True:
217  shutil.rmtree(str(Run_Number[i]))
218  os.makedirs(str(Run_Number[i])+'/'+Run_type)
219  else:
220  os.makedirs(str(Run_Number[i])+'/'+Run_type)
221 
222 
224  globalTag = getGT(filepath+File_Name, str(Run_Number[i]), 'globalTag_Step1')
225 
226 
227 
228 
229  print(" Creating the TrackerMap.... ")
230 
231  detIdInfoFileName = 'TkDetIdInfo_Run'+str(Run_Number[i])+'_'+Run_type+'.root'
232  workPath = os.popen('pwd').readline().strip()
234  os.chdir(str(Run_Number[i])+'/'+Run_type)
235 
236 
237  os.system('cmsRun ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/test/SiStripDQM_OfflineTkMap_Template_cfg_DB.py globalTag='+globalTag+' runNumber='+str(Run_Number[i])+' dqmFile='+filepath+'/'+File_Name+' detIdInfoFile='+detIdInfoFileName)
238  os.system('rm -f *svg')
239 
240  sefile = 'QualityTest_run'+str(Run_Number[i])+'.txt'
241  shutil.move('QTBadModules.log',sefile)
242 
243 
244 
245 # PLEASE UNCOMMENT THE LINES BELOW TO GET THE LEGEND ON THE QT TkMAP (it will work only on vocms061)
246 # os.system('/usr/bin/python ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/LegendToQT.py QTestAlarm.png /data/users/cctrack/FinalLegendTrans.png')
247 # shutil.move('result.png', 'QTestAlarm.png')
248 
249 
250 
251  if Run_type == "Cosmics" or Run_type == "StreamExpressCosmics":
252  os.system('cat ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/data/index_template_TKMap_cosmics.html | sed -e "s@RunNumber@'+str(Run_Number[i])+'@g" > index.html')
253  elif Run_type == "StreamExpress":
254  os.system('cat ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/data/index_template_Express_TKMap.html | sed -e "s@RunNumber@'+str(Run_Number[i])+'@g" > index.html')
255  else:
256  os.system('cat ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/data/index_template_TKMap.html | sed -e "s@RunNumber@'+str(Run_Number[i])+'@g" > index.html')
257 
258  shutil.copyfile(CMSSW_BASE+'/src/DQM/SiStripMonitorClient/data/fedmap.html','fedmap.html')
259  shutil.copyfile(CMSSW_BASE+'/src/DQM/SiStripMonitorClient/data/psumap.html','psumap.html')
260 
261  print(" Check TrackerMap on "+str(Run_Number[i])+'/'+Run_type+" folder")
262 
263  output =[]
264  output.append(os.popen("/bin/ls ").readline().strip())
265  print(output)
266 
267 
268  print(" Creating the list of bad modules ")
269 
270  os.system('listbadmodule '+filepath+'/'+File_Name+' PCLBadComponents.log')
271 
272 
275 
276 
277 
278 
279 # print(" Creating the lumisection certification:")
280 
281 # if (Run_type.startswith("ZeroBias")) or (Run_type == "StreamExpress"):
282 # os.system('ls_cert 0.95 0.95 '+filepath+'/'+File_Name)
283 
284 
288 
289 
290  os.system('bs_bad_ls_harvester . '+str(Run_Number[i]))
291 
292 
293 
294 
295  dest='Beam'
297  if (Run_type == "Cosmics") or (Run_type == "StreamExpressCosmics"):
298  dest="Cosmics"
299 
300 
301 
302  os.system('cmsRun ${CMSSW_BASE}/src/DQM/SiStripMonitorClient/test/mergeBadChannel_Template_cfg.py globalTag='+globalTag+' runNumber='+str(Run_Number[i])+' dqmFile='+filepath+'/'+File_Name)
303  shutil.move('MergedBadComponents.log','MergedBadComponents_run'+str(Run_Number[i])+'.txt')
304 
305  os.system("mkdir -p /data/users/event_display/TkCommissioner_runs/"+DataLocalDir+"/"+dest+" 2> /dev/null")
306 
307 
308  shutil.copyfile(detIdInfoFileName,'/data/users/event_display/TkCommissioner_runs/'+DataLocalDir+'/'+dest+'/'+detIdInfoFileName)
309 
310  os.remove(detIdInfoFileName)
311  os.remove('MergedBadComponentsTkMap_Canvas.root')
312  os.remove('MergedBadComponentsTkMap.root')
313 
314  print("countig dead pixel ROCs" )
315  if (Run_Number[i] < 290124) :
316 
317  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/DeadROCCounter.py '+filepath+'/'+File_Name)
318  else:
319  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/DeadROCCounter_Phase1.py '+filepath+'/'+File_Name)
320 
321  if rereco:
322  os.system('mkdir -p /data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/ReReco 2> /dev/null')
323  else:
324  os.system('mkdir -p /data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/'+Run_type+' 2> /dev/null')
325 
326  shutil.move('PixZeroOccROCs_run'+str(Run_Number[i])+'.txt',workPath+'/PixZeroOccROCs_run'+str(Run_Number[i])+'.txt')
327 
328 
329 
330 
331 
332 
333  if deadRocMap == True:
334 
335  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/DeadROC_duringRun.py '+filepath+File_Name_online+' '+filepath+File_Name)
336  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/change_name.py')
337  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/PixelMapPlotter.py MaskedROC_sum.txt -c')
338  os.remove('MaskedROC_sum.txt')
339  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/InefficientDoubleROC.py '+filepath+File_Name_online)
340  else:
341  print('No Online DQM file available, Dead ROC maps will not be produced')
342  print('No Online DQM file available, inefficient DC list will also not be produced')
343 
344 
345 
346  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/MergeOccDeadROC.py '+filepath+File_Name)
347 
348 
349  if Run_type=="StreamExpress" or Run_type=="StreamHIExpress":
350  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/MergePCLDeadROC.py '+filepath+File_Name+' '+filepath+File_Name_PCL)
351  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/MergePCLFedErr.py '+filepath+File_Name+' '+filepath+File_Name_PCL)
352  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/PCLOthers.py '+filepath+File_Name+' '+filepath+File_Name_PCL)
353 
354 
355  strip_files = os.listdir('.')
356  for file_name in strip_files:
357  full_stripfile_name = os.path.join('.', file_name)
358  if (os.path.isfile(full_stripfile_name)):
359  if rereco:
360  shutil.copy(full_stripfile_name, '/data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/ReReco')
361  else:
362  shutil.copy(full_stripfile_name, '/data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/'+Run_type)
363 
364 
365 
366 
367 
368  os.chdir(workPath)
369  shutil.rmtree(str(Run_Number[i]))
370  os.remove('index.html')
371 
372  # produce pixel phase1 TH2Poly maps
373 # os.chdir(CMSSW_BASE+'/src/DQM/SiStripMonitorClient/scripts/PhaseIMaps/')
374  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/TH2PolyOfflineMaps.py ' + filepath+'/'+File_Name+' 3000 2000')
375  shutil.move(workPath+'/PixZeroOccROCs_run'+str(Run_Number[i])+'.txt', 'OUT/PixZeroOccROCs_run'+str(Run_Number[i])+'.txt')
376 
377 
378 
379  pixel_files = os.listdir('./OUT')
380  for file_name in pixel_files:
381  full_pixelfile_name = os.path.join('./OUT/', file_name)
382  if (os.path.isfile(full_pixelfile_name)):
383  if rereco:
384  shutil.copy(full_pixelfile_name, '/data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/ReReco')
385  else:
386  shutil.copy(full_pixelfile_name, '/data/users/event_display/'+DataLocalDir+'/'+dest+'/'+str(nnnOut)+'/'+str(Run_Number[i])+'/'+Run_type)
387 
388 
389  shutil.rmtree('OUT')
390 
391 
392  # produce pixel phase1 tree for Offline TkCommissioner
393  pixelTreeFileName = 'PixelPhase1Tree_Run'+str(Run_Number[i])+'_'+Run_type+'.root'
394  os.system('${CMSSW_BASE}/src/DQM/SiStripMonitorClient/scripts/PhaseITreeProducer.py ' + filepath+'/'+File_Name + ' ' + pixelTreeFileName)
395 
396  shutil.copyfile(pixelTreeFileName,'/data/users/event_display/TkCommissioner_runs/'+DataLocalDir+'/'+dest+'/'+pixelTreeFileName)
397  os.remove(pixelTreeFileName)
398  if File_Name:
399  os.remove(filepath+File_Name)
400  if File_Name_online:
401  os.remove(filepath+File_Name_online)
402  os.chdir(workPath)
403 
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
static std::string const input
Definition: EdmProvDump.cc:50
def downloadnlineDQMhisto(run, Run_type)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def downloadOfflinePCLhisto(run, Run_type)
def setRunDirectory(runNumber)
def getGT(DQMfile, RunNumber, globalTagVar)
#define str(s)
def downloadOfflineDQMhisto(run, Run_type, rereco)