BmnRoot
Loading...
Searching...
No Matches
move_bad_runs.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3import os
4import shutil
5import re
6from collections import defaultdict
7
8# Configuration
9THRESHOLD = 5 * 1024 * 1024 * 1024 # bytes
10BAD_DIR = "beam-bad"
11
12# Create the bad directory if it does not exist
13if not os.path.exists(BAD_DIR):
14 os.makedirs(BAD_DIR)
15 print(f"Created directory: {BAD_DIR}")
16
17# Regex pattern for the filename format:
18# run_<label>_<run_number>_ev<digits>_p<digits>.data
19# The label can contain underscores or other characters.
20pattern = r'^run_(.*)_(\d+)_ev\d+_p\d+\.data$'
21
22# Collect all potential data files
23data_files = [
24 f for f in os.listdir('.')
25 if os.path.isfile(f) and f.startswith('run_') and f.endswith('.data')
26]
27
28# Group files by run_number
29run_groups = defaultdict(list)
30for f in data_files:
31 match = re.match(pattern, f)
32 if match:
33 label, run_num_str = match.groups()
34 run_num = int(run_num_str)
35 run_groups[run_num].append(f)
36 else:
37 print(f"Skipping file: {f} (does not match expected pattern)")
38
39# Process each run
40moved_count = 0
41for run_num, files in sorted(run_groups.items()):
42 if not files:
43 continue
44
45 # Calculate total size in bytes
46 total_size = sum(os.path.getsize(f) for f in files)
47 num_files = len(files)
48
49 if total_size < THRESHOLD:
50 print(f"Bad run {run_num}: {num_files} files, {total_size:,} bytes (< {THRESHOLD:,} bytes) → moving to {BAD_DIR}/")
51 for f in files:
52 shutil.move(f, os.path.join(BAD_DIR, f))
53 moved_count += 1
54 else:
55 print(f"Good run {run_num}: {num_files} files, {total_size:,} bytes")
56
57print(f"\nProcessing complete. Moved {moved_count} files to '{BAD_DIR}'.")