BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
check_missing_files.C
Go to the documentation of this file.
2
3#include <algorithm>
4#include <map>
5#include <unordered_map>
6
7// get all raw file records of the period from UniConDa with one query and group them by the file name with extension:
8// file name -> descriptions of its records (path, run number, file id)
9// returns the number of the records, or -1 if the Database query failed
10int get_period_raw_files(int period_number, unordered_map<string, vector<string>>& period_files)
11{
13 if (connDb == nullptr)
14 return -1;
15
16 TString sql =
17 TString::Format("select file_id, run_number, file_path from raw_file where period_number = %d", period_number);
18 TSQLStatement* stmt = connDb->GetSQLServer()->Statement(sql);
19 if (!stmt->Process()) {
20 delete stmt;
21 delete connDb;
22 return -1;
23 }
24 stmt->StoreResult();
25
26 int record_count = 0;
27 while (stmt->NextResultRow()) {
28 string file_path = stmt->GetString(2);
29 period_files[get_file_name_with_ext(file_path)].push_back(
30 TString::Format("%s (run = %d, file_id = %d)", file_path.c_str(), stmt->GetInt(1), stmt->GetInt(0)).Data());
31 record_count++;
32 }
33
34 delete stmt;
35 delete connDb;
36
37 return record_count;
38}
39
40// Check the files listed in the input text file against the raw file metadata of the given period in UniConDa
41// (the 'raw_file' table): whether each file is missing there, present once, or present more than once.
42// All raw file records of the period are read from the Database with one query, and the list is checked in memory.
43// The files are matched by the file name with extension, because the paths stored in the Database
44// can differ from the listed ones (e.g. after postrun_update.C changed them to the real directory).
45// Parameters:
46// list_file_path - path to a text file with a raw file path (or a file name) per line (empty lines are skipped)
47// period_number - period (session) number, only raw file records of this period are taken into account
48// Returns 0 on success, or a negative code on error (cannot open the list / Database error).
49int check_missing_files(TString list_file_path, int period_number = 9)
50{
51 gSystem->ExpandPathName(list_file_path);
52
53 ifstream list_file(list_file_path.Data());
54 if (!list_file.is_open()) {
55 cout << "ERROR: cannot open the input text file: " << list_file_path << endl;
56 return -1;
57 }
58
59 list<string> file_list;
60 string s;
61 while (getline(list_file, s)) {
62 // trim leading and trailing whitespace (including '\r' of Windows line endings), skip empty lines
63 size_t first = s.find_first_not_of(" \t\r\n");
64 if (first == string::npos)
65 continue;
66 size_t last = s.find_last_not_of(" \t\r\n");
67 file_list.push_back(s.substr(first, last - first + 1));
68 }
69 if (file_list.empty()) {
70 cout << "WARNING: There are no files in the list of the input text file: " << list_file_path << endl;
71 return 0;
72 }
73 cout << "Total file records in the input text file: " << file_list.size() << endl;
74
75 // file name -> descriptions of its records in the Database
76 unordered_map<string, vector<string>> period_files;
77 int record_count = get_period_raw_files(period_number, period_files);
78 if (record_count < 0) {
79 cout << "ERROR: getting the raw file records of period " << period_number << " from the Database failed"
80 << endl;
81 return -2;
82 }
83 cout << "Raw file records of period " << period_number << " in the Database: " << record_count << endl;
84
85 vector<string> missing_files, single_files;
86 // listed file -> descriptions of its records in the Database
87 map<string, vector<string>> duplicate_files;
88 for (auto const& cur_file_path : file_list) {
89 auto found = period_files.find(get_file_name_with_ext(cur_file_path));
90 if (found == period_files.end())
91 missing_files.push_back(cur_file_path);
92 else if (found->second.size() == 1)
93 single_files.push_back(cur_file_path);
94 else
95 duplicate_files[cur_file_path] = found->second;
96 }
97
98 // print the resulting lists in alphabetical order (the map is already sorted)
99 sort(missing_files.begin(), missing_files.end());
100 sort(single_files.begin(), single_files.end());
101
102 if (!missing_files.empty()) {
103 cout << endl << "Files missing in the raw_file table for period " << period_number << ":" << endl;
104 for (auto const& cur_file_path : missing_files)
105 cout << cur_file_path << endl;
106 }
107 if (!duplicate_files.empty()) {
108 cout << endl
109 << "Files present in the raw_file table for period " << period_number << " more than once:" << endl;
110 for (auto const& [cur_file_path, records] : duplicate_files) {
111 cout << cur_file_path << endl;
112 for (auto const& record : records)
113 cout << " " << record << endl;
114 }
115 }
116 if (!single_files.empty()) {
117 cout << endl << "Files present in the raw_file table for period " << period_number << " once:" << endl;
118 for (auto const& cur_file_path : single_files)
119 cout << cur_file_path << endl;
120 }
121
122 cout << endl
123 << "Missing: " << missing_files.size() << ", present more than once: " << duplicate_files.size()
124 << ", present once: " << single_files.size() << endl;
125
126 return 0;
127}
R__EXTERN TSystem * gSystem
virtual TSQLStatement * Statement(const char *, Int_t=100)
virtual const char * GetString(Int_t)
virtual Bool_t NextResultRow()=0
virtual Int_t GetInt(Int_t)
virtual Bool_t Process()=0
virtual Bool_t StoreResult()=0
const char * Data() const
static TString Format(const char *fmt,...)
static UniConnection * Open()
TSQLServer * GetSQLServer()
string get_file_name_with_ext(string path)
int get_period_raw_files(int period_number, unordered_map< string, vector< string > > &period_files)
int check_missing_files(TString list_file_path, int period_number=9)