Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-27 07:41:40

0001 import matplotlib.pyplot as plt
0002 import os
0003 import numpy as np
0004 
0005 def read_data(file_name):
0006     """
0007     Reads a file with numbers at the end of each line and returns a list of integers.
0008     """
0009     base_path = '/sphenix/u/sphnxpro/mainkolja/'
0010     file_path = os.path.join(base_path, file_name)
0011     
0012     data_list = []
0013     try:
0014         with open(file_path, 'r') as f:
0015             for line in f:
0016                 line = line.strip()
0017                 if not line:
0018                     continue
0019                 try:
0020                     parts = line.split()
0021                     num = int(parts[-1])
0022                     if num > 0:
0023                         data_list.append(num)
0024                 except (ValueError, IndexError):
0025                     pass
0026     except FileNotFoundError:
0027         print(f"Warning: File not found at {file_path}")
0028         # Try relative path
0029         alt_path = f'../{file_name}'
0030         try:
0031             with open(alt_path, 'r') as f:
0032                 for line in f:
0033                     line = line.strip()
0034                     if not line:
0035                         continue
0036                     try:
0037                         parts = line.split()
0038                         num = int(parts[-1])
0039                         if num > 0:
0040                             data_list.append(num)
0041                     except (ValueError, IndexError):
0042                         pass
0043         except FileNotFoundError:
0044              print(f"Error: File also not found at {alt_path}")
0045     return data_list
0046 
0047 
0048 def plot_events_distribution():
0049     """
0050     Reads data from 'nums' and 'seednums' and plots their distributions,
0051     including mean and RMS in the legend. The y-axis is logarithmic.
0052     """
0053     tracks_list = read_data('nums')
0054     seeds_list = read_data('seednums')
0055 
0056     if not tracks_list and not seeds_list:
0057         print("No valid data found to plot.")
0058         return
0059 
0060     # Create histogram
0061     plt.figure(figsize=(10, 6))
0062     
0063     # Plot tracks
0064     if tracks_list:
0065         mean_tracks = np.mean(tracks_list)
0066         rms_tracks = np.sqrt(np.mean(np.square(tracks_list)))
0067         label_tracks = f'tracks (mean={mean_tracks:.2f}, rms={rms_tracks:.2f})'
0068         plt.hist(tracks_list, bins=100, range=(0, 1000), edgecolor='black', label=label_tracks)
0069         
0070     # Plot seeds on top
0071     if seeds_list:
0072         mean_seeds = np.mean(seeds_list)
0073         rms_seeds = np.sqrt(np.mean(np.square(seeds_list)))
0074         label_seeds = f'seeds (mean={mean_seeds:.2f}, rms={rms_seeds:.2f})'
0075         plt.hist(seeds_list, bins=100, range=(0, 1000), histtype='step', color='red', linewidth=2, label=label_seeds)
0076 
0077     plt.title('Distribution of Tracks and Seeds')
0078     plt.xlabel('Number of Events')
0079     plt.ylabel('Frequency (log scale)')
0080     plt.yscale('log')
0081     plt.grid(axis='y', alpha=0.75)
0082     plt.legend()
0083 
0084     # Save the plot
0085     output_filename = 'nevents_distribution.png'
0086     plt.savefig(output_filename)
0087     print(f"Histogram saved to {output_filename}")
0088 
0089 if __name__ == '__main__':
0090     plot_events_distribution()