Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:11

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