Skip to content

Commit

Permalink
Lint DQM
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenain committed Oct 26, 2023
1 parent f92d980 commit 2c3e552
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 176 deletions.
3 changes: 2 additions & 1 deletion src/nectarchain/dqm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .db_utils import DQMDB
from .dqm_summary_processor import DQMSummary

__all__ = ["DQMDB"]
__all__ = ["DQMDB", "DQMSummary"]
118 changes: 65 additions & 53 deletions src/nectarchain/dqm/camera_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from dqm_summary_processor import dqm_summary
from matplotlib import pyplot as plt
from ctapipe.visualization import CameraDisplay
from ctapipe.instrument import CameraGeometry
from ctapipe.coordinates import EngineeringCameraFrame
from astropy import time as astropytime
import numpy as np
import os
import sqlite3

import numpy as np
from astropy import time as astropytime
from ctapipe.coordinates import EngineeringCameraFrame
from ctapipe.visualization import CameraDisplay
from dqm_summary_processor import DQMSummary
from matplotlib import pyplot as plt

class CameraMonitoring(dqm_summary):

class CameraMonitoring(DQMSummary):
def __init__(self, gaink):
self.k = gaink
return None

def ConfigureForRun(self, path, Pix, Samp, Reader1):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp

self.camera = CameraGeometry.from_name("NectarCam-003").transform_to(EngineeringCameraFrame())
self.camera = CameraGeometry.from_name("NectarCam-003").transform_to(
EngineeringCameraFrame()
)
self.cmap = "gnuplot2"

self.subarray = Reader1.subarray
Expand All @@ -29,26 +31,24 @@ def ConfigureForRun(self, path, Pix, Samp, Reader1):
for i, evt1 in enumerate(Reader1):
self.run_start1 = evt1.nectarcam.tel[0].svc.date

SqlFileDate = astropytime.Time(self.run_start1, format="unix").iso.split(" ")[
0
]

SqlFilePath = ""
for i in range(len(path.split("/")) - 1):
SqlFilePath = SqlFilePath + path.split("/")[i] + "/"
SqlFileDate = astropytime.Time(self.run_start1, format="unix").iso.split(" ")[0]

SqlFileName = SqlFilePath + "nectarcam_monitoring_db_" + SqlFileDate + ".sqlite"
SqlFilePath = os.path.split(path)[0]
SqlFileName = (
SqlFilePath + "/nectarcam_monitoring_db_" + SqlFileDate + ".sqlite"
)
print("SqlFileName", SqlFileName)
con = sqlite3.connect(SqlFileName)
cursor = con.cursor()
con = sqlite3.connect(SqlFileName)
cursor = con.cursor()
try:
# print(cursor.fetchall())
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
TempData = cursor.execute('''SELECT * FROM monitoring_drawer_temperatures''')
TempData = cursor.execute(
"""SELECT * FROM monitoring_drawer_temperatures"""
)
# print(TempData.description)
self.DrawerTemp = cursor.fetchall()
cursor.close()

except sqlite3.Error as err:
print("Error Code: ", err)
print("DRAWER TEMPERATURE COULD NOT BE RETRIEVED!")
Expand All @@ -62,49 +62,58 @@ def ProcessEvent(self, evt, noped):

def FinishRun(self):
try:

self.event_id = np.array(self.event_id)
self.event_times = np.array(self.event_times)

self.run_start = self.event_times[self.event_id == np.min(self.event_id)] - 100

self.run_start = (
self.event_times[self.event_id == np.min(self.event_id)] - 100
)
self.run_end = np.max(self.event_times) + 100

self.DrawerTemp = np.array(self.DrawerTemp)
self.DrawerTimes = np.array(self.DrawerTemp[:, 3])

for i in range(len(self.DrawerTimes)):
self.DrawerTimes[i] = astropytime.Time(self.DrawerTimes[i], format='iso').unix
self.DrawerTimes[i] = astropytime.Time(
self.DrawerTimes[i], format="iso"
).unix

self.DrawerTemp11 = self.DrawerTemp[:, 4][self.DrawerTimes > self.run_start]
self.DrawerTemp21 = self.DrawerTemp[:, 5][self.DrawerTimes > self.run_start]
self.DrawerNum1 = self.DrawerTemp[:, 2][self.DrawerTimes > self.run_start]

self.DrawerTimes_new = self.DrawerTimes[self.DrawerTimes > self.run_start]

self.DrawerTemp12 = self.DrawerTemp11[self.DrawerTimes_new < self.run_end]
self.DrawerTemp22 = self.DrawerTemp21[self.DrawerTimes_new < self.run_end]
self.DrawerNum2 = self.DrawerNum1[self.DrawerTimes_new < self.run_end]
self.DrawerNum2 = self.DrawerNum1[self.DrawerTimes_new < self.run_end]

self.DrawerTemp1_mean = []
self.DrawerTemp2_mean = []
TotalDrawers = np.max(self.DrawerNum2)
for i in range(TotalDrawers+1):

for i in range(TotalDrawers + 1):
for j in range(7):
self.DrawerTemp1_mean.append(np.mean(self.DrawerTemp12[self.DrawerNum2 == i]))
self.DrawerTemp2_mean.append(np.mean(self.DrawerTemp22[self.DrawerNum2 == i]))
self.DrawerTemp1_mean.append(
np.mean(self.DrawerTemp12[self.DrawerNum2 == i])
)
self.DrawerTemp2_mean.append(
np.mean(self.DrawerTemp22[self.DrawerNum2 == i])
)
self.DrawerTemp1_mean = np.array(self.DrawerTemp1_mean)
self.DrawerTemp2_mean = np.array(self.DrawerTemp2_mean)
self.DrawerTemp_mean = (self.DrawerTemp1_mean + self.DrawerTemp2_mean)/2

self.DrawerTemp_mean = (self.DrawerTemp1_mean + self.DrawerTemp2_mean) / 2
except Exception as err:
print("Error Code: ", err)
print("DRAWER TEMPERATURE COULD NOT BE RETRIEVED!")

def GetResults(self):
self.CameraMonitoring_Results_Dict = {}
try:
self.CameraMonitoring_Results_Dict["CAMERA-TEMPERATURE-AVERAGE"] = self.DrawerTemp_mean
self.CameraMonitoring_Results_Dict[
"CAMERA-TEMPERATURE-AVERAGE"
] = self.DrawerTemp_mean
except Exception as err:
print("Error Code: ", err)
print("DRAWER TEMPERATURE COULD NOT BE RETRIEVED!")
Expand All @@ -116,48 +125,51 @@ def PlotResults(self, name, FigPath):
self.ChargeInt_Figures_Names_Dict = {}

try:

fig, disp = plt.subplots()
disp = CameraDisplay(self.camera)
disp.image = self.DrawerTemp_mean
disp.cmap = plt.cm.coolwarm
disp.axes.text(1.8, -0.3, 'Temperature', fontsize=12, rotation=90)
disp.axes.text(1.8, -0.3, "Temperature", fontsize=12, rotation=90)
disp.add_colorbar()
plt.title("Camera temperature average")
full_name = name + '_CameraTemperature_Mean.png'
full_name = name + "_CameraTemperature_Mean.png"
FullPath = FigPath + full_name
self.ChargeInt_Figures_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE"] = fig
self.ChargeInt_Figures_Names_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE"] = FullPath

self.ChargeInt_Figures_Names_Dict[
"CAMERA-TEMPERATURE-IMAGE-AVERAGE"
] = FullPath

plt.close()

fig1, disp = plt.subplots()
disp = CameraDisplay(self.camera)
disp.image = self.DrawerTemp1_mean
disp.cmap = plt.cm.coolwarm
disp.axes.text(1.8, -0.3, 'Temperature 1', fontsize=12, rotation=90)
disp.axes.text(1.8, -0.3, "Temperature 1", fontsize=12, rotation=90)
disp.add_colorbar()
plt.title("Camera temperature average 1")
full_name = name + '_CameraTemperature_average1.png'
full_name = name + "_CameraTemperature_average1.png"
FullPath = FigPath + full_name
self.ChargeInt_Figures_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE-1"] = fig1
self.ChargeInt_Figures_Names_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE-1"] = FullPath

self.ChargeInt_Figures_Names_Dict[
"CAMERA-TEMPERATURE-IMAGE-AVERAGE-1"
] = FullPath

plt.close()

fig2, disp = plt.subplots()
disp = CameraDisplay(self.camera)
disp.image = self.DrawerTemp2_mean
disp.cmap = plt.cm.coolwarm
disp.axes.text(1.8, -0.3, 'Temperature 2', fontsize=12, rotation=90)
disp.axes.text(1.8, -0.3, "Temperature 2", fontsize=12, rotation=90)
disp.add_colorbar()
plt.title("Camera temperature average 2")
full_name = name + '_CameraTemperature_average2.png'
full_name = name + "_CameraTemperature_average2.png"
FullPath = FigPath + full_name
self.ChargeInt_Figures_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE-2"] = fig2
self.ChargeInt_Figures_Names_Dict["CAMERA-TEMPERATURE-IMAGE-AVERAGE-2"] = FullPath

plt.close()
self.ChargeInt_Figures_Names_Dict[
"CAMERA-TEMPERATURE-IMAGE-AVERAGE-2"
] = FullPath

except Exception as err:
print("Error Code: ", err)
Expand Down
37 changes: 20 additions & 17 deletions src/nectarchain/dqm/charge_integration.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from dqm_summary_processor import dqm_summary
from matplotlib import pyplot as plt
import ctapipe.instrument.camera.readout
import numpy as np
from ctapipe.visualization import CameraDisplay
from ctapipe.instrument import CameraGeometry
from ctapipe.coordinates import EngineeringCameraFrame
from traitlets.config.loader import Config
import ctapipe.instrument.camera.readout
from ctapipe.image import LocalPeakWindowSum
from ctapipe.visualization import CameraDisplay
from dqm_summary_processor import DQMSummary
from matplotlib import pyplot as plt
from traitlets.config.loader import Config


class ChargeIntegration_HighLowGain(dqm_summary):
class ChargeIntegrationHighLowGain(DQMSummary):
def __init__(self, gaink):
self.k = gaink
return None

def ConfigureForRun(self, path, Pix, Samp, Reader1):
# define number of pixels and samples
Expand All @@ -22,20 +20,19 @@ def ConfigureForRun(self, path, Pix, Samp, Reader1):
self.counter_evt = 0
self.counter_ped = 0

self.camera = CameraGeometry.from_name("NectarCam-003").transform_to(EngineeringCameraFrame())
self.camera = CameraGeometry.from_name("NectarCam-003").transform_to(
EngineeringCameraFrame()
)
self.cmap = "gnuplot2"

# reader1=EventSource(input_url=path, max_events = 1)
self.subarray = Reader1.subarray
subarray = Reader1.subarray
subarray.tel[
0
].camera.readout = ctapipe.instrument.camera.readout.CameraReadout.from_name(
"NectarCam"
)
config = Config(
{"LocalPeakWindowSum": {"window_shift": 4, "window_width": 12}}
)
config = Config({"LocalPeakWindowSum": {"window_shift": 4, "window_width": 12}})

self.integrator = LocalPeakWindowSum(subarray, config=config)

Expand All @@ -49,25 +46,31 @@ def ProcessEvent(self, evt, noped):
self.pixelBAD = evt.mon.tel[0].pixel_status.hardware_failing_pixels
pixel = evt.nectarcam.tel[0].svc.pixel_ids
if len(pixel) < self.Pix:
pixel_masked_shutter = list(np.arange(0, self.Pix - len(pixel), 1, dtype=int))
pixel_masked_shutter = list(
np.arange(0, self.Pix - len(pixel), 1, dtype=int)
)
pixel = list(pixel)
pixels = np.concatenate([pixel_masked_shutter, pixel])
else:
else:
pixels = pixel

waveform = evt.r0.tel[0].waveform[self.k]

if noped:
ped = np.mean(waveform[:, 20])
w_noped = waveform - ped
output = self.integrator(w_noped,0,np.zeros(self.Pix, dtype = int), self.pixelBAD)
output = self.integrator(
w_noped, 0, np.zeros(self.Pix, dtype=int), self.pixelBAD
)
image = output.image
peakpos = output.peak_time
image = image[pixels]
peakpos = peakpos[pixels]

else:
output = self.integrator(waveform,0,np.zeros(self.Pix, dtype = int), self.pixelBAD)
output = self.integrator(
waveform, 0, np.zeros(self.Pix, dtype=int), self.pixelBAD
)
image = output.image
peakpos = output.peak_time
image = image[pixels]
Expand Down
30 changes: 18 additions & 12 deletions src/nectarchain/dqm/dqm_summary_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from astropy.table import Table


class dqm_summary:
__all__ = ["DQMSummary"]


class DQMSummary:
def __init__(self):
print("Processor 0")

Expand All @@ -18,7 +21,7 @@ def ConfigureForRun(self):
print("Processor 1")

def ProcessEvent(self, evt, noped):
print('Processor 2')
print("Processor 2")

def FinishRun(self, M, M_ped, counter_evt, counter_ped):
print("Processor 3")
Expand All @@ -35,6 +38,7 @@ def WriteAllResults(self, path, DICT):
data2 = Table()
data1 = Table()
data = Table()
hdu, hdu1, hdu2 = None, None
hdulist = fits.HDUList()
for i, j in DICT.items():
if i == "Results_TriggerStatistics":
Expand All @@ -43,9 +47,11 @@ def WriteAllResults(self, path, DICT):
hdu2 = fits.BinTableHDU(data2)
hdu2.name = "Trigger"

elif (i == "Results_MeanWaveForms_HighGain") or (i == "Results_MeanWaveForms_LowGain"):
elif (i == "Results_MeanWaveForms_HighGain") or (
i == "Results_MeanWaveForms_LowGain"
):
for n1, m1 in j.items():
data1[n1] = m1
data1[n1] = m1
hdu1 = fits.BinTableHDU(data1)
hdu1.name = "MWF"

Expand All @@ -54,19 +60,19 @@ def WriteAllResults(self, path, DICT):
data[n] = m
hdu = fits.BinTableHDU(data)
hdu.name = "Camera"
try:
if hdu2:
hdulist.append(hdu2)
except:
else:
print("No trigger statistics requests")
try:
hdulist.append(hdu1)
except:
if hdu1:
hdulist.append(hdu1)
else:
print("No MWF studies requests")
try:
if hdu:
hdulist.append(hdu)
except:
else:
print("No Camera studies requests")
FileName = path + '_Results.fits'
FileName = path + "_Results.fits"
print(FileName)
hdulist.writeto(FileName, overwrite=True)
return None
Loading

0 comments on commit 2c3e552

Please sign in to comment.