Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:44:28

0001 #!/usr/bin/env python
0002 #GBenelli Added the /env above to use the python version of CMSSW and run without having to do python <SCRIPT NAME>
0003 
0004 """ This script does the following:
0005 1- reads the list of iovs (by timestamp) in a sqlite file or in the Offline DB
0006 2- creates a cfg for each iov and runs them
0007 3- creates 2 log files per IOV (Summary/Debug) with all the SiStripDetVOff information in ASCII format
0008 It is recommended to redirect the output to a file.
0009 """
0010 from __future__ import print_function
0011 #3- takes the output of each job and builds a single output with the content of each iov
0012 
0013 import os
0014 import re
0015 import sys
0016 import time
0017 
0018 """ Helper functions for time conversions """
0019 
0020 def pack(high,low):
0021     """pack high,low 32bit unsigned int to one unsigned 64bit long long
0022        Note:the print value of result number may appear signed, if the sign bit is used.
0023     """
0024     h=high<<32
0025     return (h|low)
0026 
0027 def secondsFromString(i):
0028     """convert from a string in the format output from timeStamptoDate to a 32bit seconds from the epoch.
0029     The format accepted is \"DD/MM/YYYY HH:MM:SS\". The year must be the full number.
0030     """
0031     return int(time.mktime(time.strptime(i, "%d/%m/%Y %H:%M:%S")))
0032 
0033 def packFromString(i):
0034     """pack from a string in the format output from timeStamptoUTC to a 64bit timestamp
0035     the format accepted is \"DD/MM/YYYY HH:MM:SS\" . The year must be the full number.
0036     """
0037     return pack(secondsFromString(i), 0)
0038 
0039 def intervalSinceEpoch(i):
0040     """ compute the interval of time is seconds since the Epoch and return the packed 64bit value.
0041     """
0042     return( packFromString(i) - packFromString("01/01/1970 00:00:00") )
0043 
0044 def unpack(i):
0045     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
0046     """
0047     high=i>>32
0048     low=i&0xFFFFFFFF
0049     return(high,low)
0050 
0051 def timeStamptoDate(i):
0052     """convert 64bit timestamp to local date in string format
0053     """
0054     #GBenelli Add a try: except: to handle the stop time of the last IOV "end of time"
0055     try:
0056         date=time.ctime(unpack(i)[0])
0057     except:
0058         #Handle the case of last IOV (or any IOV) timestamp being "out of range" by returning -1 instead of the date...
0059         print("Could not unpack time stamp %s, unpacked to %s!"%(i,unpack(i)[0]))
0060         date=-1
0061     return date
0062 
0063 
0064 
0065 # The first parameter is the name of the script
0066 if len(sys.argv) < 3:
0067     print("Please provide the name of the sqlite file and the tag as in: ")
0068     print("./CheckAllIOVs.py dbfile.db SiStripDetVOff_Fake_31X")
0069     print("OR to access directly the Offline DB with a time bracket:")
0070     print("./CheckAllIOVs.py CMS_COND_31X_STRIP SiStripDetVOff_v1_offline DD/MM/YYYY HH:MM:SS DD/MM/YYYY HH:MM:SS")
0071     sys.exit(1)
0072 
0073 print("Reading all IOVs")
0074 
0075 database= sys.argv[1]
0076 #Offline DB case (e.g. user would write ./CheckAllIOVs.py CMS_COND_31X_STRIP SiStripDetVOff_v1_offline):
0077 if "COND" in database and "STRIP" in database:
0078     DBConnection="frontier://PromptProd/"+database
0079 #SQLite DB case (e.g. user would write ./CheckAllIOVs.py dbfile.db SiStripDetVOff_Fake_31X):
0080 else:
0081     DBConnection="sqlite_file:"+database
0082 
0083 tag=sys.argv[2]
0084     
0085 #GBenelli commit code from Marco to run check on a time interval:
0086 startFrom = 0
0087 if len(sys.argv) > 3:
0088     startFrom = packFromString(sys.argv[3])
0089 endAt = 0
0090 if len(sys.argv) > 4:
0091     endAt = packFromString(sys.argv[4])
0092 #TODO:
0093 #Should use subprocess.Popen...
0094 #Use cmscond_list_iov command to get the full list of IOVs available in the DB
0095 iovs = os.popen("cmscond_list_iov -c "+DBConnection+" -t "+tag)
0096 cmscond_list_iov_output = iovs.readlines()
0097 for line in cmscond_list_iov_output:
0098     print(line)
0099     if "[DB=" in line:
0100         (start,end)=line.split()[0:2]
0101         if long(startFrom) > long(start):
0102             print("Skipping IOV =", start, " before requested =", startFrom)
0103             continue
0104         if (endAt != 0) and (long(endAt) < long(end)):
0105             print("Skipping IOV =", end, " after requested =", endAt)
0106             continue
0107         # print "start =", start,
0108         # print ", end =", end
0109 
0110         if long(startFrom) > long(start):
0111             print("Skipping IOV =", start, " before requested =", startFrom)
0112             continue
0113         if (endAt != 0) and (long(endAt) < long(end)):
0114             print("Skipping IOV =", end, " after requested =", endAt)
0115             continue
0116         
0117         ##TODO:Should we investigate this issue? Is it going to be an issue in the DB?
0118         if end == "18446744073709551615":
0119             end = str(int(start) + 1)
0120 
0121         startDate = timeStamptoDate(int(start))
0122         endDate = timeStamptoDate(int(end))
0123         #GBenelli Handle here the case of "end of time" IOV end time stamp 
0124         if endDate==-1:
0125             endDate=timeStamptoDate(int(start)+1) 
0126             
0127         print("start date = ", startDate, end=' ')
0128         print(", end date = ", endDate)
0129         fullDates="_FROM_"+startDate.replace(" ", "_").replace(":", "_")+"_TO_"+endDate.replace(" ", "_").replace(":", "_")
0130         fileName="DetVOffPrint"+fullDates+"_cfg.py"
0131         CfgFile=open(fileName,"w")
0132         #Until the tag is in the release, CMSSW_RELEASE_BASE should be replaced by CMSSW_BASE...
0133         for cfgline in open(os.path.join(os.getenv("CMSSW_RELEASE_BASE"),"src/CalibTracker/SiStripDCS/test","templateCheckAllIOVs_cfg.py"),"r"):
0134             #Do the template replacements for the relevant variables:
0135             if "STARTTIME" in cfgline:
0136                 cfgline=cfgline.replace("STARTTIME",start)
0137             elif "ENDTIME" in cfgline:
0138                 cfgline=cfgline.replace("ENDTIME",end)
0139             elif "DATE" in cfgline:
0140                 cfgline=cfgline.replace("DATE",fullDates)
0141             elif "DATABASECONNECTION" in cfgline:
0142                 cfgline=cfgline.replace("DATABASECONNECTION",DBConnection)
0143             elif "TAG" in cfgline:
0144                 cfgline=cfgline.replace("TAG",tag)
0145             #Write the line from the template into the cloned cfg.py file:
0146             CfgFile.write(cfgline)
0147         CfgFile.close()
0148 
0149         #Comment this line if you are debuggin and don't want cmsRun to run!
0150         os.system("cmsRun "+fileName+" > /dev/null")
0151 
0152         for logline in open("DetVOffReaderDebug_"+fullDates+".log", "r"):
0153             if "IOV" in logline or "OFF" in logline or "ON" in logline:
0154                 print(logline.strip("\n"))
0155     else:
0156         print(line)