Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dqm update - using standard ctapipe tools #154

Merged
merged 11 commits into from
Nov 6, 2024
2 changes: 1 addition & 1 deletion src/nectarchain/dqm/camera_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, gaink):
self.ChargeInt_Figures_Dict = {}
self.ChargeInt_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand Down
116 changes: 71 additions & 45 deletions src/nectarchain/dqm/charge_integration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import ctapipe.instrument.camera.readout
import numpy as np
from ctapipe.coordinates import EngineeringCameraFrame
from ctapipe.image import LocalPeakWindowSum
from ctapipe.image.extractor import FixedWindowSum # noqa: F401
from ctapipe.image.extractor import FullWaveformSum # noqa: F401
from ctapipe.image.extractor import GlobalPeakWindowSum # noqa: F401
from ctapipe.image.extractor import LocalPeakWindowSum # noqa: F401
from ctapipe.image.extractor import NeighborPeakWindowSum # noqa: F401
from ctapipe.image.extractor import SlidingWindowMaxSum # noqa: F401
from ctapipe.image.extractor import TwoPassWindowSum # noqa: F401
from ctapipe.visualization import CameraDisplay
from ctapipe_io_nectarcam import constants
from matplotlib import pyplot as plt
from traitlets.config.loader import Config

from ..makers.component import ChargesComponent
from ..makers.component.core import ArrayDataComponent
from ..makers.extractor.utils import CtapipeExtractor
from .dqm_summary_processor import DQMSummary

__all__ = ["ChargeIntegrationHighLowGain"]
Expand Down Expand Up @@ -51,7 +61,7 @@
self.ChargeInt_Figures_Dict = {}
self.ChargeInt_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **charges_kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand All @@ -72,44 +82,60 @@
].camera.readout = ctapipe.instrument.camera.readout.CameraReadout.from_name(
"NectarCam"
)
config = Config({"LocalPeakWindowSum": {"window_shift": 4, "window_width": 12}})

self.integrator = LocalPeakWindowSum(subarray, config=config)
if charges_kwargs:
extractor_kwargs = (
ChargesComponent._get_extractor_kwargs_from_method_and_kwargs(
method=charges_kwargs["method"],
kwargs=charges_kwargs["extractor_kwargs"],
)
)
self.integrator = eval(charges_kwargs["method"])(
subarray, **extractor_kwargs
)
else:
config = Config(

Check warning on line 96 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L96

Added line #L96 was not covered by tests
{"LocalPeakWindowSum": {"window_shift": 4, "window_width": 12}}
)
self.integrator = LocalPeakWindowSum(subarray, config=config)

Check warning on line 99 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L99

Added line #L99 was not covered by tests

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 = list(pixel)
pixels = np.concatenate([pixel_masked_shutter, pixel])
else:
pixels = pixel
self.pixelBADplot = evt.mon.tel[0].pixel_status.hardware_failing_pixels
pixels = pixel
self.pixels = pixels

Check warning on line 105 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L103-L105

Added lines #L103 - L105 were not covered by tests

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

Check warning on line 107 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L107

Added line #L107 was not covered by tests
broken_pixels_hg,
broken_pixels_lg,
) = ArrayDataComponent._compute_broken_pixels_event(evt, pixels)

if self.k == 0:
self.pixelBAD = broken_pixels_hg
channel = constants.HIGH_GAIN
if self.k == 1:
self.pixelBAD = broken_pixels_lg
channel = constants.LOW_GAIN

Check warning on line 117 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L112-L117

Added lines #L112 - L117 were not covered by tests

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

Check warning on line 120 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L119-L120

Added lines #L119 - L120 were not covered by tests
ped = np.mean(waveform[:, 20])

if noped:
w_noped = waveform - ped
output = self.integrator(
w_noped, 0, np.zeros(self.Pix, dtype=int), self.pixelBAD
output = CtapipeExtractor.get_image_peak_time(

Check warning on line 125 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L125

Added line #L125 was not covered by tests
self.integrator(w_noped, 0, channel, self.pixelBAD)
)
image = output.image
peakpos = output.peak_time
image = image[pixels]
peakpos = peakpos[pixels]

image = output[0]
peakpos = output[1]

Check warning on line 130 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L129-L130

Added lines #L129 - L130 were not covered by tests

else:
output = self.integrator(
waveform, 0, np.zeros(self.Pix, dtype=int), self.pixelBAD
output = CtapipeExtractor.get_image_peak_time(

Check warning on line 133 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L133

Added line #L133 was not covered by tests
self.integrator(waveform, 0, channel, self.pixelBAD)
)
image = output.image
peakpos = output.peak_time
image = image[pixels]
peakpos = peakpos[pixels]

image = output[0]
peakpos = output[1]

Check warning on line 138 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L137-L138

Added lines #L137 - L138 were not covered by tests

if evt.trigger.event_type.value == 32: # count peds
self.counter_ped += 1
Expand Down Expand Up @@ -274,9 +300,9 @@
# Charge integration MEAN plot
if self.counter_evt > 0:
fig1, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])

Check warning on line 303 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L303

Added line #L303 was not covered by tests
# disp = CameraDisplay(self.subarray.tels[0].camera)
disp.image = self.image_all_average[~self.pixelBAD[0]]
disp.image = self.image_all_average

Check warning on line 305 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L305

Added line #L305 was not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -302,8 +328,8 @@

if self.counter_ped > 0:
fig2, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_ped_average[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_ped_average

Check warning on line 332 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L331-L332

Added lines #L331 - L332 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -330,8 +356,8 @@
# Charge integration MEDIAN plot
if self.counter_evt > 0:
fig3, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_all_median[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_all_median

Check warning on line 360 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L359-L360

Added lines #L359 - L360 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -357,8 +383,8 @@

if self.counter_ped > 0:
fig4, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_ped_median[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_ped_median

Check warning on line 387 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L386-L387

Added lines #L386 - L387 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -385,8 +411,8 @@
# Charge integration STD plot
if self.counter_evt > 0:
fig5, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_all_std[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_all_std

Check warning on line 415 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L414-L415

Added lines #L414 - L415 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -412,8 +438,8 @@

if self.counter_ped > 0:
fig6, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_ped_std[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_ped_std

Check warning on line 442 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L441-L442

Added lines #L441 - L442 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -440,8 +466,8 @@
# Charge integration RMS plot
if self.counter_evt > 0:
fig7, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_all_rms[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_all_rms

Check warning on line 470 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L469-L470

Added lines #L469 - L470 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -467,8 +493,8 @@

if self.counter_ped > 0:
fig8, disp = plt.subplots()
disp = CameraDisplay(self.camera[~self.pixelBAD[0]])
disp.image = self.image_ped_rms[~self.pixelBAD[0]]
disp = CameraDisplay(self.camera[~self.pixelBADplot[0]])
disp.image = self.image_ped_rms

Check warning on line 497 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L496-L497

Added lines #L496 - L497 were not covered by tests
disp.cmap = plt.cm.coolwarm
disp.axes.text(
2,
Expand All @@ -495,7 +521,7 @@
# Charge integration SPECTRUM
if self.counter_evt > 0:
fig9, disp = plt.subplots()
for i in range(self.Pix):
for i in range(len(self.pixels)):

Check warning on line 524 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L524

Added line #L524 was not covered by tests
plt.hist(
self.image_all[:, i],
100,
Expand Down Expand Up @@ -532,7 +558,7 @@

if self.counter_ped > 0:
fig10, disp = plt.subplots()
for i in range(self.Pix):
for i in range(len(self.pixels)):

Check warning on line 561 in src/nectarchain/dqm/charge_integration.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/charge_integration.py#L561

Added line #L561 was not covered by tests
plt.hist(
self.image_ped[:, i],
100,
Expand Down
2 changes: 1 addition & 1 deletion src/nectarchain/dqm/mean_camera_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, gaink):
self.MeanCameraDisplay_Figures_Dict = {}
self.MeanCameraDisplay_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand Down
2 changes: 1 addition & 1 deletion src/nectarchain/dqm/mean_waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, gaink):
self.MeanWaveForms_Figures_Dict = {}
self.MeanWaveForms_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand Down
2 changes: 1 addition & 1 deletion src/nectarchain/dqm/pixel_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, gaink):
self.PixelParticipation_Figures_Dict = {}
self.PixelParticipation_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand Down
2 changes: 1 addition & 1 deletion src/nectarchain/dqm/pixel_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, gaink):
self.PixelTimeline_Figures_Dict = {}
self.PixelTimeline_Figures_Names_Dict = {}

def ConfigureForRun(self, path, Pix, Samp, Reader1):
def ConfigureForRun(self, path, Pix, Samp, Reader1, **kwargs):
# define number of pixels and samples
self.Pix = Pix
self.Samp = Samp
Expand Down
Empty file.
38 changes: 37 additions & 1 deletion src/nectarchain/dqm/start_dqm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import json

Check warning on line 2 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L2

Added line #L2 was not covered by tests
import os
import sys
import time
Expand All @@ -18,6 +19,7 @@
from nectarchain.dqm.pixel_participation import PixelParticipationHighLowGain
from nectarchain.dqm.pixel_timeline import PixelTimelineHighLowGain
from nectarchain.dqm.trigger_statistics import TriggerStatistics
from nectarchain.makers import ChargesNectarCAMCalibrationTool

Check warning on line 22 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L22

Added line #L22 was not covered by tests


def main():
Expand All @@ -40,6 +42,28 @@
action="store_true",
help="Enables pedestal subtraction in charge integration",
)
# extractor arguments
parser.add_argument(

Check warning on line 46 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L46

Added line #L46 was not covered by tests
"--method",
choices=[
"FullWaveformSum",
"FixedWindowSum",
"GlobalPeakWindowSum",
"LocalPeakWindowSum",
"SlidingWindowMaxSum",
"TwoPassWindowSum",
],
default="LocalPeakWindowSum",
help="charge extractor method",
type=str,
)
parser.add_argument(

Check warning on line 60 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L60

Added line #L60 was not covered by tests
"--extractor_kwargs",
default='{"window_shift": 4, "window_width": 16}',
help="charge extractor kwargs",
type=json.loads,
)

parser.add_argument(
"-r",
"--runnb",
Expand Down Expand Up @@ -94,9 +118,21 @@
# Defining and printing the options
PlotFig = args.plot
noped = args.noped
method = args.method
extractor_kwargs = args.extractor_kwargs

Check warning on line 122 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L121-L122

Added lines #L121 - L122 were not covered by tests

print("Plot:", PlotFig)
print("Noped:", noped)
print("method:", method)
print("extractor_kwargs:", extractor_kwargs)

Check warning on line 127 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L126-L127

Added lines #L126 - L127 were not covered by tests

kwargs = {"method": method, "extractor_kwargs": extractor_kwargs}
charges_kwargs = {}
tool = ChargesNectarCAMCalibrationTool()
for key in tool.traits().keys():
if key in kwargs.keys():
charges_kwargs[key] = kwargs[key]
print(charges_kwargs)

Check warning on line 135 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L129-L135

Added lines #L129 - L135 were not covered by tests

def GetName(RunFile):
name = RunFile.split("/")[-1]
Expand Down Expand Up @@ -189,7 +225,7 @@
break

for p in processors:
p.ConfigureForRun(path, Pix, Samp, reader1)
p.ConfigureForRun(path, Pix, Samp, reader1, **charges_kwargs)

Check warning on line 228 in src/nectarchain/dqm/start_dqm.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/start_dqm.py#L228

Added line #L228 was not covered by tests

for evt in tqdm(
reader, total=args.max_events if args.max_events else len(reader), unit="ev"
Expand Down
1 change: 1 addition & 0 deletions src/nectarchain/dqm/tests/test_camera_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from astropy import time as astropytime
from ctapipe.image.extractor import LocalPeakWindowSum # noqa: F401
from ctapipe.io import EventSource
from ctapipe.utils import get_dataset_path
from ctapipe_io_nectarcam.constants import HIGH_GAIN
Expand Down
Loading