RDSS/test_debug_format.py

107 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
测试debug文件输出格式验证组内逆序排列
"""
import sys
import tempfile
import os
sys.path.insert(0, 'd:\\DESIGN\\Git\\RDSS')
from core.data_writer import DataWriterV4
from core.encoder import BitFieldEncoder
# 创建编码器
encoder = BitFieldEncoder()
# 创建测试事件数据模拟2个事件每个事件16个信号
# 事件1: 信号1-16事件2: 信号17-32
all_events = []
# 事件1: 16个信号2组
event1_signals = []
for i in range(16):
# 创建简单的测试信号,时间戳递增
timestamp = (i + 1) * 10 # 10, 20, 30, ... 160 us
energies = [100 + i * 10, 0, 0] # 只有探测器1有信号
encoded = encoder.encode(timestamp, energies, event_end=(i == 15))
event1_signals.append(encoded)
all_events.append(event1_signals)
# 事件2: 16个信号2组
event2_signals = []
for i in range(16):
timestamp = (i + 1) * 10 # 10, 20, 30, ... 160 us
energies = [0, 200 + i * 10, 0] # 只有探测器2有信号
encoded = encoder.encode(timestamp, energies, event_end=(i == 15))
event2_signals.append(encoded)
all_events.append(event2_signals)
# 创建数据写入器
config = {
'simulation': {
'packing_mode': 'separate',
'energy_conversion': {'K': 1.0, 'B': 0.0},
'output_file': 'test_output'
}
}
data_writer = DataWriterV4(config)
# 创建临时debug文件
with tempfile.NamedTemporaryFile(mode='w', suffix='_debug.txt', delete=False, encoding='utf-8') as f:
temp_debug_file = f.name
# 写入debug文件
data_writer.write_events_debug_text(all_events, temp_debug_file)
# 读取并显示debug文件内容
print("生成的debug文件内容")
print("=" * 80)
with open(temp_debug_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines[:30]: # 只显示前30行
print(line.rstrip())
print("=" * 80)
# 验证组内逆序排列
print("\n验证组内逆序排列:")
print("期望的第1组信号序号: 8, 7, 6, 5, 4, 3, 2, 1")
print("期望的第2组信号序号: 16, 15, 14, 13, 12, 11, 10, 9")
print()
# 解析debug文件中的信号序号
signal_numbers = []
for line in lines:
if line.strip() and not line.startswith('#') and not line.startswith('-'):
parts = line.split('|')
if len(parts) >= 2:
try:
sig_num = int(parts[1].strip())
signal_numbers.append(sig_num)
except:
pass
# 检查前16个信号事件1
event1_numbers = signal_numbers[:16]
print(f"实际的事件1信号序号: {event1_numbers}")
# 验证第1组应该是8,7,6,5,4,3,2,1
group1 = event1_numbers[:8]
expected_group1 = [8, 7, 6, 5, 4, 3, 2, 1]
print(f"第1组信号序号: {group1}")
print(f"期望的第1组: {expected_group1}")
# 验证第2组应该是16,15,14,13,12,11,10,9
group2 = event1_numbers[8:16]
expected_group2 = [16, 15, 14, 13, 12, 11, 10, 9]
print(f"第2组信号序号: {group2}")
print(f"期望的第2组: {expected_group2}")
if group1 == expected_group1 and group2 == expected_group2:
print("\n✅ 测试通过!组内逆序排列正确")
else:
print("\n❌ 测试失败!组内逆序排列不正确")
# 清理临时文件
os.unlink(temp_debug_file)