#!/usr/bin/env python #pylint: disable=C0103 #pylint: disable=R0201 """ Read Tracker calibrations from the CDB Includes ADC calibrations and channel mapping to be included in MAUS. """ import cdb import json import os from Configuration import Configuration from cdb._exceptions import CdbPermanentError class GetTrackerCalib: """ Connects to the CDB and downloads tracker calibration, mapping, and bad channel lists """ def __init__(self): print "Init running" self.config = json.loads(\ Configuration().getConfigJSON(command_line_args = True)) calib_cdb_url = self.config['cdb_download_url'] + 'calibration?wsdl' self._calib = cdb.Calibration() self._calib.set_url(calib_cdb_url) print "Connected to calibration" cable_cdb_url = self.config['cdb_download_url'] + 'cabling?wsdl' self._cable = cdb.Cabling() self._cable.set_url(cable_cdb_url) self._method = None self._rnum = 0 print "Connected to cabling" def Process(self): """ Needs datafile set to Run/Date/Current; date formate: "1984-09-14 00:10:00.0" """ print "Starting process" self._method = self.config["SciFiCalibMethod"] _input = self.config["SciFiCalibSrc"] self._rnum = _input if self._method == "Date": self.Date(_input) if self._method == "Run": try: self.Run(_input) except CdbPermanentError: self.Date(self.Convert_Run(_input)) if self._method == "Current": self.Current() def Current(self): """ Collects current calibration info """ calib = self._calib.get_current_calibration("Trackers","trackers") badchan = self._calib.get_current_calibration("Trackers","bad_chan") cable = self._cable.get_current_cabling("Trackers") self.Output(calib, badchan, cable) def Run(self, run_num): """ Collects calibration info by run """ try: print "Getting SciFi calibrations and maps for Run ", run_num calib = self._calib.get_calibration_for_run\ ("Trackers", run_num, "trackers") badchan = self._calib.get_calibration_for_run\ ("Trackers", run_num, "bad_chan") cable = self._cable.get_cabling_for_run\ ("Trackers", run_num) self.Output(calib, badchan, cable) except CdbPermanentError: raise CdbPermanentError\ ("CDB error getting scifi calibration by run") def Date(self, date): """ Collects calibration info by date """ calib = self._calib.get_calibration_for_date\ ("Trackers", date, "trackers") badchan = self._calib.get_calibration_for_date\ ("Trackers", date, "bad_chan") cable = self._cable.get_cabling_for_date\ ("Trackers", date) self.Output(calib, badchan, cable) def Convert_Run(self, _input): """ If new calibration files are uploaded this function needs updated """ run = int(_input) if run < 6664: print "No tracker configuration data for this run" if run <= 6664 and run > 7273: date = "2015-06-02 00:10:00.0" if run <= 7273 and run > 7294: date = "2015-07-22 00:10:00.0" if run <= 7294: date = "2015-08-19 00:10:00.0" return date def Output(self, CAL, BC, CAB): """ Outputs calibration files The calibration and mapping files go into files/calibration and files/cabling The path is relative to SciFiConfigDir SciFiConfigDir default is MAUS_ROOT_DIR """ path = self.config['SciFiConfigDir'] path_calib = path + "/files/calibration/" path_cable = path + "/files/cabling/" calib_out = None badchan_out = None cable_out = None if self._method == "Run": calib_rpath = path_calib + str(self._rnum) cable_rpath = path_cable + str(self._rnum) if not os.path.exists(calib_rpath): os.makedirs(calib_rpath, 0755) if not os.path.exists(cable_rpath): os.makedirs(cable_rpath, 0755) calib_out = open(calib_rpath+"/scifi_calibration.txt","w") badchan_out = open(calib_rpath+"/scifi_bad_channels.txt","w") cable_out = open(cable_rpath+"/scifi_mapping.txt","w") else: calib_out = open(path_calib+"scifi_calibration.txt","w") badchan_out = open(path_calib+"scifi_bad_channels.txt","w") cable_out = open(path_cable+"scifi_mapping.txt","w") calib_out.write(CAL) badchan_out.write(BC) cable_out.write(CAB) calib_out.close() badchan_out.close() cable_out.close() if __name__ == "__main__": GetTrackerCalib().Process()