#!/usr/bin/env python # # Shows how to use the ratdb module to access the database and browse for an object # # Author N. Barros #################################################################################################### import ROOT import rat from rat import ratdb import pgdb as psql from sys import exc_info def calculate_run_duration(run): """ Calculate the duration of a run based ont he contents of the respective RUN table Args: run (`int`): Run number """ # By default it connects to the remote database db = ratdb.RATDBConnector(server="postgres://snoplus@pgsql.snopl.us:5400/ratdb") try: result = db.fetch(obj_type="RUN",run=run) if len(result) == 0: # Nothing was found. Just exit print "Didn't find a run table for run {0}".format(run) return # We should now have a table # Grab the start and end times and calculate ## Keep in mind that the return of the query is an array (there could be multiple objects matching ## the query, depending on how it is formulated table = result[0]['data'] ns_in_a_day = 24*3600*1e9 elapsed_time = (table['stop_day']-table['start_day'])*ns_in_a_day elapsed_time += (table['stop_sec']-table['start_sec'])*1e9 elapsed_time += (table['stop_nsc']-table['start_nsc']) print "" print "Run {0} lasted {1} ns ({2} h) ".format(run,elapsed_time,elapsed_time/(3600*1e9)) print "" except psql.Error as e: print "PgSQL error:" print e except: e = exc_info()[1] print "==> Database Exception: {0}".format(e) finally: if db is not None: db.close_ratdb_connection() if __name__ == "__main__": calculate_run_duration(102220)