BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
remove_raw_files.C
Go to the documentation of this file.
2
3// Remove raw file metadata from UniConDa for the files listed in the input text file,
4// as well as run metadata if no raw file metadata remains for the run.
5// All files in the list must belong to the same period, otherwise the macro exits with an error.
6// Parameters:
7// list_file_path - path to a text file with raw file path per line (empty lines are skipped)
8// isTest - if true (default), only print what would be removed without changing the Database;
9// set to false to actually delete the raw file and run metadata
10// Returns 0 on success, or a negative code on error (cannot read files / mixed periods).
11int remove_raw_files(TString list_file_path, bool isTest = true)
12{
13 gSystem->ExpandPathName(list_file_path);
14
15 list<string> file_list;
16 ifstream list_file(list_file_path.Data());
17 string s;
18 while (getline(list_file, s)) {
19 // Skip empty lines or lines with only whitespace
20 if (s.find_first_not_of(" \t\r\n") == string::npos)
21 continue;
22 file_list.push_back(s);
23 }
24 if (file_list.empty()) {
25 cout << "WARNING: There are no files in the list of the input text file: " << list_file_path << endl;
26 return 0;
27 }
28 cout << "Total file records in the input text file: " << file_list.size() << endl << endl;
29
30 int period_number = -1, removed_raw_files = 0, removed_runs = 0;
31 unordered_set<int> run_list;
32 // cycle for all files in the list of the input text file
33 for (auto const& cur_file_path : file_list) {
34 string file_name = get_file_name_with_ext(cur_file_path);
35 // cout << "Current file of the list: " << cur_file_path << endl;
36 cout << "Current file of the list: " << file_name << endl;
37
38 TObjArray* raw_files = UniRawFile::GetRawFiles(file_name);
39 if (raw_files == nullptr)
40 return -1;
41
42 // remove raw file metadata for the found files
43 for (int i = 0; i < raw_files->GetEntries(); i++) {
44 UniRawFile* curRawFile = (UniRawFile*)raw_files->At(i);
45
46 if (period_number == -1)
47 period_number = curRawFile->GetPeriodNumber();
48 else {
49 if (period_number != curRawFile->GetPeriodNumber()) {
50 cout << "ERROR: there are different period numbers for the files (" << period_number << "<>"
51 << curRawFile->GetPeriodNumber() << "), exiting...";
52 return -2;
53 }
54 }
55 run_list.insert(curRawFile->GetRunNumber());
56
57 cout << "Removing raw file: " << curRawFile->GetFilePath() << endl;
58 removed_raw_files++;
59 if (!isTest)
61 }
62 }
63
64 // remove run metadata if no raw file metadata remains for the run
65 for (int current_run : run_list) {
66 cout << "Current run: " << current_run << endl;
67 TObjArray* raw_files = UniRawFile::GetRawFiles(period_number, current_run);
68
69 if ((raw_files != nullptr) and (raw_files->GetEntries() == 0)) {
70 cout << "Removing period:run = " << period_number << ":" << current_run << endl;
71 removed_runs++;
72 if (!isTest)
73 UniRun::DeleteRun(period_number, current_run);
74 }
75 }
76
77 cout << endl
78 << (isTest ? "Would remove " : "Removed ") << removed_raw_files << " raw files and " << removed_runs << " runs"
79 << endl;
80
81 return 0;
82}
83
84// Remove run metadata from UniConDa for the runs listed in the input text file,
85// but only for those runs that have no raw file metadata remaining.
86// Parameters:
87// bad_run_file - path to a text file with run number per line
88// period_number - period the runs belong to (default 9)
89// isTest - if true (default), only print what would be removed without changing the Database;
90// set to false to actually delete the run metadata
91// Returns 0 on success, or a negative code on error (line is not a valid number / out of range).
92int remove_runs(TString bad_run_file, int period_number = 9, bool isTest = true)
93{
94 gSystem->ExpandPathName(bad_run_file);
95
96 list<int> run_list;
97 ifstream list_file(bad_run_file.Data());
98 string s;
99 while (getline(list_file, s)) {
100 try {
101 int i_run_number = std::stoi(s);
102 run_list.push_back(i_run_number);
103 } catch (const std::invalid_argument&) {
104 cerr << "ERROR: Line is not a valid number: \"" << s << "\"\n";
105 return -1;
106 } catch (const std::out_of_range&) {
107 cerr << "Warning: Line " << s << ": number is out of range\n";
108 return -2;
109 }
110 }
111 if (run_list.empty()) {
112 cout << "WARNING: There are no runs in the list of the input text file: " << bad_run_file << endl;
113 return 0;
114 }
115 cout << "Total run records in the input text file: " << run_list.size() << endl << endl;
116
117 int removed_runs = 0;
118 // remove run metadata if no raw file metadata remains for the run
119 for (int current_run : run_list) {
120 cout << "Current run: " << current_run << endl;
121 if (UniRun::CheckRunExists(period_number, current_run) < 1)
122 continue;
123
124 TObjArray* raw_files = UniRawFile::GetRawFiles(period_number, current_run);
125 if ((raw_files != nullptr) and (raw_files->GetEntries() == 0)) {
126 cout << "Removing period:run = " << period_number << ":" << current_run << endl;
127 removed_runs++;
128 if (!isTest)
129 UniRun::DeleteRun(period_number, current_run);
130 }
131 }
132
133 cout << endl << (isTest ? "Would remove " : "Removed ") << removed_runs << " runs" << endl;
134
135 return 0;
136}
137
138// Find odd run metadata in UniConDa - runs present in the Database but absent in the input text file -
139// and remove those that have no raw file metadata remaining. Also reports runs from the list that are
140// missing in the Database.
141// Parameters:
142// full_run_path - path to a text file with the full list of valid run numbers, one per line
143// period_number - period to check the runs against (default 9)
144// isTest - if true (default), only print odd and missing runs without changing the Database;
145// set to false to actually delete the odd run metadata
146// Returns 0 on success, or a negative code on error (invalid number / out of range / no runs in the Database).
147int remove_odd_runs(TString good_run_file, int period_number = 9, bool isTest = true)
148{
149 gSystem->ExpandPathName(good_run_file);
150
151 // form full run list
152 unordered_set<int> run_list;
153 ifstream list_file(good_run_file.Data());
154 string s;
155 while (getline(list_file, s)) {
156 try {
157 int i_run_number = std::stoi(s);
158 run_list.insert(i_run_number);
159 } catch (const std::invalid_argument&) {
160 cerr << "ERROR: Line is not a valid number: \"" << s << "\"\n";
161 return -1;
162 } catch (const std::out_of_range&) {
163 cerr << "Warning: Line " << s << ": number is out of range\n";
164 return -2;
165 }
166 }
167 if (run_list.empty()) {
168 cout << "WARNING: There are no runs in the list of the input text file: " << good_run_file << endl;
169 return 0;
170 }
171 cout << "Total run records in the input text file: " << run_list.size() << endl << endl;
172
173 // cycle by runs in UniConDa
174 UniqueRunNumber* run_numbers;
175 int run_count = UniRunPeriod::GetRunNumbers(run_numbers, period_number);
176 if (run_count <= 0) {
177 cout << "ERROR: runs do not exist in the Database for period " << period_number << endl;
178 return -3;
179 }
180
181 int nfound_runs = 0, removed_runs = 0;
182 for (int i = 0; i < run_count; i++) {
183 if (run_list.find(run_numbers[i].run_number) != run_list.end())
184 continue;
185 cout << "Run " << run_numbers[i].run_number << " is not in the list" << endl;
186 nfound_runs++;
187 if (!isTest) {
188 // remove run metadata if no raw file metadata remains for the run
189 TObjArray* raw_files = UniRawFile::GetRawFiles(run_numbers[i].period_number, run_numbers[i].run_number);
190
191 if ((raw_files != nullptr) and (raw_files->GetEntries() == 0)) {
192 cout << "Removing period:run = " << run_numbers[i].period_number << ":" << run_numbers[i].run_number
193 << endl;
194 UniRun::DeleteRun(run_numbers[i].period_number, run_numbers[i].run_number);
195 removed_runs++;
196 }
197 }
198 }
199
200 // find missing runs
201 int missing_runs = 0;
202 for (int current_run : run_list) {
203 if (UniRun::CheckRunExists(period_number, current_run) < 1) {
204 cout << "Run " << current_run << " is missing" << endl;
205 missing_runs++;
206 }
207 }
208
209 if (nfound_runs + missing_runs > 0)
210 cout << endl;
211
212 cout << "Found " << nfound_runs << " odd runs" << endl;
213 if (!isTest)
214 cout << (isTest ? "Would remove " : "Removed ") << removed_runs << " runs" << endl;
215 cout << "Found " << missing_runs << " missing runs" << endl;
216
217 return 0;
218}
int i
Definition P4_F32vec4.h:22
R__EXTERN TSystem * gSystem
Int_t GetEntries() const override
TObject * At(Int_t idx) const override
const char * Data() const
/home/soul/bmnroot/database/uni_db1/UniRawFile.h Class for the table: raw_file
Definition UniRawFile.h:19
TString GetFilePath()
get file path of the current raw file
Definition UniRawFile.h:110
int GetRunNumber()
get run number of the current raw file
Definition UniRawFile.h:100
int GetFileId()
get file id of the current raw file
Definition UniRawFile.h:96
static TObjArray * GetRawFiles(int period_number, int run_number)
get array of raw files (UniRawFile*) corresponding to the period:run number ordered by start_event as...
static int DeleteRawFile(int file_id)
delete raw file from the database
int GetPeriodNumber()
get period number of the current raw file
Definition UniRawFile.h:98
static int GetRunNumbers(UniqueRunNumber *&run_numbers, int period_number)
get numbers of runs existing in the Database for a selected period
static int DeleteRun(int period_number, int run_number)
delete run from the database
Definition UniRun.cxx:281
static int CheckRunExists(int period_number, int run_number)
check run exists in the database: 1- true, 0 - false, <0 - database operation error
Definition UniRun.cxx:241
string get_file_name_with_ext(string path)
int remove_runs(TString bad_run_file, int period_number=9, bool isTest=true)
int remove_odd_runs(TString good_run_file, int period_number=9, bool isTest=true)
int remove_raw_files(TString list_file_path, bool isTest=true)