File indexing completed on 2023-03-17 10:44:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 from __future__ import print_function
0015 import os, subprocess
0016
0017 def ProduceTkMapVoltageInputFiles(workdir=os.getcwd()):
0018 """
0019 Function that processes the indicated workdir directory (defaults to current working directory) looking for CheckAllIOVs.py
0020 DetVOffReaderDebug*.log files.
0021 For each log file (that correspond to 1 IOV) 2 input files (one for LV one for HV status) are created for the TkVoltageMapCreator plugin.
0022 The function returns the 2 lists of HV and LV files.
0023 """
0024
0025 print("Analysing %s directory"%workdir)
0026 logfilenames=[x for x in os.listdir(workdir) if x.startswith("DetVOffReaderDebug")]
0027 if logfilenames:
0028 print("Processing %s logfiles..."%len(logfilenames))
0029 else:
0030 print("No DetVIfReaderDebug files found!\nPlease run this script in the same dir you have run CheckAllIOVS.py, or in the dir where you store the output logfiles.")
0031
0032
0033
0034
0035 import pickle
0036
0037
0038 StripDetIDAliasPickle=os.path.join(os.getenv("CMSSW_RELEASE_BASE"),"src/CalibTracker/SiStripDCS/data","StripDetIDAlias.pkl")
0039
0040
0041
0042 DetIDdict=open(StripDetIDAliasPickle,"r")
0043 StripDetIDAlias=pickle.load(DetIDdict)
0044
0045
0046
0047 LVFilenames=[]
0048 HVFilenames=[]
0049 for logfilename in logfilenames:
0050 print(logfilename)
0051
0052
0053
0054
0055 LVfilename=os.path.join(workdir,logfilename.replace("DetVOffReaderDebug_","LV"))
0056 HVfilename=os.path.join(workdir,logfilename.replace("DetVOffReaderDebug_","HV"))
0057
0058
0059 LVFilenames.append(LVfilename)
0060 HVFilenames.append(HVfilename)
0061
0062
0063 logfile=open(logfilename,"r")
0064 LVfile=open(LVfilename,"w")
0065 HVfile=open(HVfilename,"w")
0066
0067
0068
0069 LVDict={}
0070 HVDict={}
0071 for line in logfile:
0072 if "OFF" in line:
0073 (detid,HV,LV)=line.split()
0074 LVDict.update({detid:LV})
0075 HVDict.update({detid:HV})
0076
0077
0078 for detid in StripDetIDAlias.keys():
0079 detid=str(detid)
0080 if detid in LVDict.keys():
0081 LVfile.write(detid+"\t"+LVDict[detid]+"\n")
0082 else:
0083 LVfile.write(detid+"\tON\n")
0084 if detid in HVDict.keys():
0085 HVfile.write(detid+"\t"+HVDict[detid]+"\n")
0086 else:
0087 HVfile.write(detid+"\tON\n")
0088
0089
0090
0091 logfile.close()
0092 LVfile.close()
0093 HVfile.close()
0094
0095
0096 return LVFilenames,HVFilenames
0097
0098
0099 def runcmd(command):
0100 """
0101 Function that uses subprocess.Popen to run commands, it returns the exit code of the command.
0102 """
0103 try:
0104 process = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
0105 pid=process.pid
0106 exitstat= process.wait()
0107 cmdout = process.stdout.read()
0108 exitstat = process.returncode
0109 except OSError as detail:
0110 print("Race condition in subprocess.Popen has robbed us of the exit code of the %s process (PID %s).Assume it failed!\n %s\n"%(command,pid,detail))
0111 exitstat=999
0112 if exitstat == None:
0113 print("Something strange is going on! Exit code was None for command %s: check if it really ran!"%command)
0114 exitstat=0
0115 return exitstat
0116
0117 def CreateTkVoltageMapsCfgs(workdir=os.getcwd()):
0118 """
0119 Function that looks for TkVoltageMapCreator input files, creates 1 cfg.py for each IOV.
0120 It returns the list of cfgs ready to be cmsRun to produce the maps
0121 """
0122
0123 HVLogs=[x for x in os.listdir(workdir) if x.startswith("HV") and "FROM" in x and x.endswith(".log")]
0124
0125
0126 TkMapCreatorTemplateFile=open(os.path.join(os.getenv("CMSSW_BASE"),"src/CalibTracker/SiStripDCS/test","TkVoltageMapCreator_cfg.py"),"r")
0127 TkMapCreatorTemplateContent=TkMapCreatorTemplateFile.readlines()
0128
0129 TkMapCfgFilenames=[]
0130 for HVlog in HVLogs:
0131
0132 HVlog=os.path.join(workdir,HVlog)
0133
0134 LVlog=os.path.join(workdir,HVlog.replace("HV","LV"))
0135 if not os.path.exists(LVlog):
0136 print("ARGH! Missing LV file for file %s"%HVlog)
0137 print("Will not process the HV file either!")
0138 TkMapCfgFilename=os.path.join(workdir,HVlog.replace("HV","TkVoltageMap").replace(".log","_cfg.py"))
0139 TkMapCfgFilenames.append(TkMapCfgFilename)
0140 TkMapCfg=open(TkMapCfgFilename,"w")
0141 for line in TkMapCreatorTemplateContent:
0142 if "LVStatusFile" in line and "#" not in line:
0143 line='\tLVStatusFile = cms.string("%s"),\n'%LVlog
0144 if "LVTkMapName" in line and "#" not in line:
0145 line='\tLVTkMapName = cms.string("%s"),\n'%LVlog.replace(".log",".png")
0146 if "HVStatusFile" in line and "#" not in line:
0147 line='\tHVStatusFile = cms.string("%s"),\n'%HVlog
0148 if "HVTkMapName" in line and "#" not in line:
0149 line='\tHVTkMapName = cms.string("%s")\n'%HVlog.replace(".log",".png")
0150 TkMapCfg.write(line)
0151 TkMapCfg.close()
0152
0153 TkMapCreatorTemplateFile.close()
0154 return TkMapCfgFilenames
0155
0156 def CreateTkVoltageMaps(workdir=os.getcwd()):
0157 """
0158 Function that looks for TkVoltageMap*cfg.py in the workdir directory and launches each of them
0159 creating 2 TkVoltageMaps per IOV, one for LV and one of HV status (each as a png file).
0160 """
0161 TkMapCfgs=[x for x in os.listdir(workdir) if x.startswith("TkVoltageMap") and "FROM" in x and x.endswith("cfg.py")]
0162 for TkMapCfg in TkMapCfgs:
0163
0164 TkMapCfg=os.path.join(workdir,TkMapCfg)
0165 cmsRunCmd="cmsRun %s >& %s"%(TkMapCfg,TkMapCfg.replace(".py",".log"))
0166 print(cmsRunCmd)
0167 exitstat=runcmd(cmsRunCmd)
0168 if exitstat != 0:
0169 print("Uh-Oh!")
0170 print("Command %s FAILED!"%cmsRunCmd)
0171
0172
0173
0174
0175 (LVfiles,HVfiles)=ProduceTkMapVoltageInputFiles()
0176
0177
0178
0179
0180 TkMapCfgFilenames=CreateTkVoltageMapsCfgs()
0181 print(TkMapCfgFilenames)
0182 CreateTkVoltageMaps()
0183
0184
0185
0186
0187
0188