Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#!/usr/bin/env python3

'''
Script used to find all Strip Tracker PEAK runs

Usage:

 create the list of SiStripLatency::singleReadOutMode() IOVs / values with:

 getPayloadData.py --plugin pluginSiStripLatency_PayloadInspector --plot plot_SiStripIsPeakModeHistory --tag SiStripLatency_GR10_v2_hlt --time_type Run --iovs '{"start_iov": "1", "end_iov": "400000"}' --db Prod --test > & log.json

 create the list of runs with the Strip Tracker in global DAQ

 getPayloadData.py --plugin pluginRunInfo_PayloadInspector --plot plot_RunInfoTrackerHistory --tag runinfo_31X_hlt --time_type Run --iovs '{"start_iov": "1", "end_iov": "400000"}' --db Prod --test > & isTrackerin.json

 followed by:

 python findPeakRuns.py -f log.json -r isTrackerIn.json > & allTrackerRuns.csv &
'''

import json
import ROOT
from pprint import pprint
from optparse import OptionParser
import CondCore.Utilities.conddblib as conddb

##############################################
def findPeakIOVs(values):
##############################################
    listOfIOVs=[]
    listOfModes=[]
    lastMode=1
    lastRun=1
    
    latestRun=-1
    latestMode=-9999

    for value in values:
        if (value['y']!=lastMode):
            listOfIOVs.append((lastRun,value['x']))
            if(lastMode==1):
                listOfModes.append('PEAK')
            else:
                listOfModes.append('DECO')
            lastMode=value['y']
            lastRun=value['x']
        else:
            latestRun=value['x']
            if(value['y']==1):
                latestMode='PEAK'
            else:
                latestMode='DECO'

    ## special case for the last open IOV
    listOfIOVs.append((lastRun,999999))
    listOfModes.append(latestMode)

    return dict(zip(listOfIOVs,listOfModes))

##############################################
def getTkInDict(runvalues):
##############################################
    isTrackerIn = {}
    for entry in runvalues:
        isTrackerIn[entry['x']]=entry['y']
    return isTrackerIn

##############################################
def getAllRuns():
##############################################
    con = conddb.connect(url = conddb.make_url("pro"))
    session = con.session()
    RUNINFO = session.get_dbtype(conddb.RunInfo)
    allRuns = session.query(RUNINFO.run_number, RUNINFO.start_time, RUNINFO.end_time).all()
    return allRuns

##############################################
def convert_timedelta(duration):
##############################################
    days, seconds = duration.days, duration.seconds
    hours = days * 24 + seconds // 3600
    minutes = (seconds % 3600) // 60
    seconds = (seconds % 60)
    return '{0:02}'.format(hours), '{0:02}'.format(minutes), '{0:02}'.format(seconds)

##############################################
def main():
##############################################

    parser = OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                      help="open FILE and extract info", metavar="FILE")
    parser.add_option("-r", "--runfile", dest="runfilename",
                      help="open RUNFILE and extract info", metavar="RUNFILE")
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose", default=False,
                      help="verbose output")
    parser.add_option("-p", "--peakOnly",
                      action="store_true", dest="peakOnly", default=False,
                      help="Print only runs in PEAK mode")
    (options, args) = parser.parse_args()

    with open(options.filename) as data_file:
        data = json.load(data_file)
        values = data["data"]
    
    IOVs = findPeakIOVs(values)
    if(options.verbose):
        pprint(IOVs)

    with open(options.runfilename) as rundata_file:
        data = json.load(rundata_file)
        runvalues = data["data"]

    isTrackerIn = getTkInDict(runvalues)

    if(options.verbose):
        for value in runvalues:
            isTrackerIn = bool(value['y'])
            runnumber = int(value['x'])
            if(not isTrackerIn):
                # there was no Tracker in this run
                continue
            else:
                for key, value in IOVs.items():
                    if(key[0]<runnumber and key[1]>runnumber):
                        print(runnumber,",",key[0],"-",key[1],",",value)

    allRuns = getAllRuns()
    if(options.verbose):
        print(allRuns)

    sorted_runs = sorted(allRuns)

    for run in sorted_runs:
        if run[0] not in isTrackerIn:
            continue
        if(isTrackerIn[run[0]]):
            #print(run[0],"Tracker In")
            for key, value in IOVs.items():
                if(key[0]<run[0] and key[1]>run[0]):
                    hours, minutes, seconds = convert_timedelta(run[2]-run[1])
                    if(options.peakOnly):
                        if(value=='PEAK'):
                            print(run[0],",",run[1].strftime('%Y-%m-%d'),",",'{}:{}:{}'.format(hours,minutes,seconds),",",key[0],"-",key[1],",",value)
                        else:
                            pass
                    else:
                        print(run[0],",",run[1].strftime('%Y-%m-%d'),",",'{}:{}:{}'.format(hours,minutes,seconds),",",key[0],"-",key[1],",",value)
        else:
            pass
            #print(run[0],"Tracker Out")

if __name__ == "__main__":
    main()