import argparse import matplotlib.pyplot as plt import os # Input text file file_path = "samples.txt" # Set up the argument parser parser = argparse.ArgumentParser(description="Provide optionally a specific line number.") parser.add_argument('line_number', type=int, nargs='?', default=0, help="Line number (default: 0)") # Parse command-line arguments args = parser.parse_args() # Get the integer argument value (or use the default if not provided) line_num = args.line_number # Output folder to save plots output_folder = "wp/plots1" os.makedirs(output_folder, exist_ok=True) # Counters for different peak categories count_0_peaks = 0 count_1_peak = 0 count_2_peaks = 0 # Limits for each category limit_per_category = 400 # Read and process the file with open(file_path, 'r') as file: i = 0 for line in file: # Skip comment lines if line.strip().startswith("#"): continue i += 1 # count lines from 1 #print(f"Line {i}") # Stop after processing the first N lines #if i >= 200: # break # If a specific line number is provided, plot only that if line_num > 0 and line_num != i: continue # Parse each line starting with "Samples" parts = line.strip().split() if len(parts) > 2 and parts[4] == "Samples": evnum = int(parts[0]) # Detector element detid = int(parts[1]) detn = {0: "SAC", 1: "IRC"}.get(detid, "NONE") chan = int(parts[2]) slot = int(parts[3]) # Get the number of samples specified num_samples = int(parts[5]) # Extract amplitudes based on the number of samples amplitudes = list(map(int, parts[6:6 + num_samples])) mean_val = float(parts[6 + num_samples]) # Mean as calculated in the C++ code min_val = int(parts[7 + num_samples]) # Minimum amplitude value max_val = int(parts[8 + num_samples]) # Maximum amplitude value peaks_found = int(parts[9 + num_samples]) # Number of peaks found # Check if we need more plots for this peaks category if peaks_found == 0 and count_0_peaks < limit_per_category: count_0_peaks += 1 elif peaks_found == 1 and count_1_peak < limit_per_category: count_1_peak += 1 elif peaks_found == 2 and count_2_peaks < limit_per_category: count_2_peaks += 1 else: continue # Generate time intervals in nanoseconds time_ns = [(j - 5) * 25 for j in range(len(amplitudes))] # Print the amplitudes to stdout if limit_per_category < 100 or line_num > 0: print(f"Line {i}, Event {evnum}, {detid}, chan {chan}, slot {slot}, Peaks: {peaks_found}, Amplitudes: {amplitudes}") # Plot amplitude vs. time plt.figure(figsize=(8, 4)) plt.plot(time_ns, amplitudes, marker='o', linestyle='-', color='b') #plt.bar(time_ns, amplitudes, width=25, color='blue', edgecolor='black', alpha=0.7) #plt.ylim(900, 1600) # Set y-axis limits plt.axhline(y=mean_val, color='green', linestyle='-', label=f'quiet mean {mean_val:.0f}') # Add horizontal line plt.axhline(y=min_val, color='blue', linestyle='--', label='min') plt.axhline(y=max_val, color='red', linestyle='--', label='max') plt.plot([], [], ' ', label=f"max - min = {max_val - min_val}") # Extra legend entry plt.title(f"Line #{i}, Event {evnum}, chan {chan}, {detn}, slot {slot} - peaks counted: {peaks_found}") plt.xlabel("Time (ns)") plt.ylabel("Amplitude") plt.legend() plt.grid(True) output_file = os.path.join(output_folder, "samples_line{}_{}_ch{}_slot{}_{}peaks.png".format(i, detn, chan, slot, peaks_found)) plt.savefig(output_file) plt.close() # Print results print(f"Saved {count_0_peaks} plots with no peaks.") print(f"Saved {count_1_peak} plots with 1 peak.") print(f"Saved {count_2_peaks} plots with 2 peaks.") print("Plots saved in the folder: {}".format(output_folder))