BmnRoot
Loading...
Searching...
No Matches
show_experiment_files.cxx
Go to the documentation of this file.
1//============================================================================
2// Name : show_experiment_files.cpp
3// Author : Konstantin Gertsenberger
4// Copyright : JINR
5// Description : Get list of experimental files for given runs
6//============================================================================
7
8// own headers
9#include "uni_db_settings.h"
10
11// ROOT includes
12#include "TSQLResult.h"
13#include "TSQLRow.h"
14#include "TSQLServer.h"
15#include "TString.h"
16
17// C++ includes
18#include <algorithm>
19#include <iostream>
20#include <sstream>
21#include <string>
22
23using namespace std;
24
25int main(int argc, char** argv)
26{
27 // help information
28 if (argc > 1) {
29 string first_par = argv[1];
30 if ((first_par == "/?") || (first_par == "--help") || (first_par == "-h")) {
31 cout << "Get list of experimental raw files satisfying the given parameters." << endl
32 << "Parameters separated by comma:" << endl
33 << "period - period number (range is supported by '-' symbol)" << endl
34 << "run - run number (range is supported by '-' symbol)" << endl
35 << "beam - first particle in collision (first beam)" << endl
36 << "target - second particle in collision (second beam) OR target" << endl
37 << "energy - collision energy (range is supported by '-' symbol)" << endl
38 << "events - event count (range is supported by '-' symbol)" << endl
39 << "time - experimental file (or files in case of the time interval) including event at this time "
40 "(range is supported by '-' symbol), datetime format - yyyy-mm-dd 24hh:mm:ss"
41 << endl
42 << "field - field current (range is supported by '-' symbol)" << endl
43 << "size - file size (range is supported by '-' symbol)" << endl
44 << "path - part of the file path" << endl
45 << endl
46 << "Examples:" << endl
47 << "show_experiment_files period=5,energy=3-,beam=d,target=C" << endl
48 << "show_experiment_files period=4-5,field=-800" << endl;
49
50 return 0;
51 }
52 }
53
54 // parse parameter for DB searching
55 bool isPeriod = false, isMinPeriod = false, isMaxPeriod = false, isRun = false, isMinRun = false, isMaxRun = false,
56 isBeam = false, isTarget = false, isEnergy = false, isMinEnergy = false, isMaxEnergy = false, isEvents = false,
57 isMinEvents = false, isMaxEvents = false, isTime = false, isMinTime = false, isMaxTime = false,
58 isField = false, isMinField = false, isMaxField = false, isSize = false, isMinSize = false, isMaxSize = false,
59 isPath = false;
60 string strBeam, strTarget, strTime, strMaxTime, strPath;
61 int iPeriod, iMaxPeriod, iRun, iMaxRun, iEvents, iMaxEvents, iField, iMaxField;
62 double fEnergy, fMaxEnergy, fSize, fMaxSize;
63 for (int i = 1; i < argc; i++) {
64 // TString strParameter(argv[i]);
65 // TObjArray* arrSplit = strParameter.Tokenize(",");
66
67 string input = argv[i];
68 istringstream ss(input);
69 string token;
70
71 // parse tokens by comma separated
72 while (getline(ss, token, ',')) {
73 // to lowercase
74 transform(token.begin(), token.end(), token.begin(), ::tolower);
75
76 // period number parsing
77 if ((token.length() > 7) && (token.substr(0, 7) == "period=")) {
78 token = token.substr(7);
79
80 size_t indDash = token.find_first_of('-');
81 if (indDash != string::npos) {
82 stringstream stream;
83 stream << token.substr(0, indDash);
84 int iVal;
85 if (stream >> iVal) {
86 isPeriod = true;
87 isMinPeriod = true;
88 iPeriod = iVal;
89 }
90 if (token.length() > indDash) {
91 stringstream stream2;
92 stream2 << token.substr(indDash + 1);
93 if (stream2 >> iVal) {
94 isPeriod = true;
95 isMaxPeriod = true;
96 iMaxPeriod = iVal;
97 }
98 }
99 } // if (indDash > -1)
100 // if exact period number
101 else
102 {
103 stringstream stream;
104 stream << token;
105 int iVal;
106 if (stream >> iVal) {
107 isPeriod = true;
108 iPeriod = iVal;
109 }
110 } // else
111 } // if ((token.length() > 7) && (token.substr(0,7) == "period="))
112 else
113 {
114 // run number parsing
115 if ((token.length() > 4) && (token.substr(0, 4) == "run=")) {
116 token = token.substr(4);
117
118 size_t indDash = token.find_first_of('-');
119 if (indDash != string::npos) {
120 stringstream stream;
121 stream << token.substr(0, indDash);
122 int iVal;
123 if (stream >> iVal) {
124 isRun = true;
125 isMinRun = true;
126 iRun = iVal;
127 }
128 if (token.length() > indDash) {
129 stringstream stream2;
130 stream2 << token.substr(indDash + 1);
131 if (stream2 >> iVal) {
132 isRun = true;
133 isMaxRun = true;
134 iMaxRun = iVal;
135 }
136 }
137 } // if (indDash > -1)
138 // if exact run number
139 else
140 {
141 stringstream stream;
142 stream << token;
143 int iVal;
144 if (stream >> iVal) {
145 isRun = true;
146 iRun = iVal;
147 }
148 } // else
149 } // if ((token.length() > 4) && (token.substr(0,4) == "run="))
150 else
151 {
152 // beam particle's name in collision parsing
153 if ((token.length() > 5) && (token.substr(0, 5) == "beam=")) {
154 isBeam = true;
155 strBeam = token.substr(5);
156 } else {
157 // target particle's name in collision parsing
158 if ((token.length() > 7) && (token.substr(0, 7) == "target=")) {
159 isTarget = true;
160 strTarget = token.substr(7);
161 } else {
162 // energy parsing
163 if ((token.length() > 7) && (token.substr(0, 7) == "energy=")) {
164 token = token.substr(7);
165
166 size_t indDash = token.find_first_of('-');
167 if (indDash != string::npos) {
168 stringstream stream;
169 stream << token.substr(0, indDash);
170 double dVal;
171 if (stream >> dVal) {
172 isEnergy = true;
173 isMinEnergy = true;
174 fEnergy = dVal;
175 }
176 if (token.length() > indDash) {
177 stringstream stream2;
178 stream2 << token.substr(indDash + 1);
179 if (stream2 >> dVal) {
180 isEnergy = true;
181 isMaxEnergy = true;
182 fMaxEnergy = dVal;
183 }
184 }
185 } // if (indDash > -1)
186 // if exact energy value
187 else
188 {
189 stringstream stream;
190 stream << token;
191 double dVal;
192 if (stream >> dVal) {
193 isEnergy = true;
194 fEnergy = dVal;
195 }
196 } // else
197 } // if ((token.length() > 7) && (token.substr(0,7) == "energy="))
198 else
199 {
200 // event count parsing
201 if ((token.length() > 6) && (token.substr(0, 6) == "events=")) {
202 token = token.substr(6);
203
204 size_t indDash = token.find_first_of('-');
205 if (indDash != string::npos) {
206 stringstream stream;
207 stream << token.substr(0, indDash);
208 int iVal;
209 if (stream >> iVal) {
210 isEvents = true;
211 isMinEvents = true;
212 iEvents = iVal;
213 }
214 if (token.length() > indDash) {
215 stringstream stream2;
216 stream2 << token.substr(indDash + 1);
217 if (stream2 >> iVal) {
218 isEvents = true;
219 isMaxEvents = true;
220 iMaxEvents = iVal;
221 }
222 }
223 } // if (indDash > -1)
224 // if exact event count
225 else
226 {
227 stringstream stream;
228 stream << token;
229 int iVal;
230 if (stream >> iVal) {
231 isEvents = true;
232 iEvents = iVal;
233 }
234 } // else
235 } // if ((token.length() > 6) && (token.substr(0,6) == "events="))
236 else
237 {
238 // time parsing
239 if ((token.length() > 5) && (token.substr(0, 5) == "time=")) {
240 token = token.substr(5);
241
242 size_t indDash = token.find_first_of('-');
243 if (indDash != string::npos) {
244 if (indDash > 0) {
245 isTime = true;
246 isMinTime = true;
247 strTime = token.substr(0, indDash);
248 }
249 if (token.length() > indDash) {
250 isTime = true;
251 isMaxTime = true;
252 strMaxTime = token.substr(indDash + 1);
253 }
254 } // if (indDash > -1)
255 // if exact time
256 else
257 {
258 isTime = true;
259 strTime = token;
260 } // else
261 } // if ((token.length() > 5) && (token.substr(0,5) == "time="))
262 else
263 {
264 // field current parsing
265 if ((token.length() > 6) && (token.substr(0, 6) == "field=")) {
266 token = token.substr(6);
267
268 size_t indDash = token.find_first_of('-');
269 if (indDash != string::npos) {
270 stringstream stream;
271 stream << token.substr(0, indDash);
272 int iVal;
273 if (stream >> iVal) {
274 isField = true;
275 isMinField = true;
276 iField = iVal;
277 }
278 if (token.length() > indDash) {
279 stringstream stream2;
280 stream2 << token.substr(indDash + 1);
281 if (stream2 >> iVal) {
282 isField = true;
283 isMaxField = true;
284 iMaxField = iVal;
285 }
286 }
287 } // if (indDash > -1)
288 // if exact field current
289 else
290 {
291 stringstream stream;
292 stream << token;
293 int iVal;
294 if (stream >> iVal) {
295 isField = true;
296 iField = iVal;
297 }
298 } // else
299 } // if ((token.length() > 6) && (token.substr(0,6) == "field="))
300 else
301 {
302 // file size parsing
303 if ((token.length() > 5) && (token.substr(0, 5) == "size=")) {
304 token = token.substr(5);
305
306 size_t indDash = token.find_first_of('-');
307 if (indDash != string::npos) {
308 stringstream stream;
309 stream << token.substr(0, indDash);
310 double dVal;
311 if (stream >> dVal) {
312 isSize = true;
313 isMinSize = true;
314 fSize = dVal;
315 }
316 if (token.length() > indDash) {
317 stringstream stream2;
318 stream2 << token.substr(indDash + 1);
319 if (stream2 >> dVal) {
320 isSize = true;
321 isMaxSize = true;
322 fMaxSize = dVal;
323 }
324 }
325 } // if (indDash > -1)
326 // if exact file size
327 else
328 {
329 stringstream stream;
330 stream << token;
331 double dVal;
332 if (stream >> dVal) {
333 isSize = true;
334 fSize = dVal;
335 }
336 } // else
337 } // if ((token.length() > 5) && (token.substr(0,5) == "size="))
338 else
339 {
340 // path parsing
341 if ((token.length() > 5) && (token.substr(0, 5) == "path=")) {
342 isPath = true;
343 strPath = token.substr(5);
344 }
345 } // else SIZE
346 } // else FIELD
347 } // else TIME
348 } // else EVENTS
349 } // else ENERGY
350 } // else TARGER
351 } // else BEAM
352 } // else RUN
353 } // else PERIOD
354 } // while(getline(ss, token, ','))
355 } // for (int i = 1; i < argc; i++)
356
357 // generate query
358 TString strConnection = "pgsql://" + TString(UNI_DB_HOST) + "/" + (TString)UNI_DB_NAME;
359 TSQLServer* pSQLServer = TSQLServer::Connect(strConnection, UNI_DB_USERNAME, UNI_DB_PASSWORD);
360 if (pSQLServer == nullptr) {
361 cout << "Connection to the database was not established" << endl;
362 return -1;
363 }
364
365 TString sql = "select file_path "
366 "from run_";
367
368 bool isWhere = false;
369 // if period selection
370 if (isPeriod == true) {
371 if (isWhere)
372 sql += " AND ";
373 else {
374 isWhere = true;
375 sql += " "
376 "where ";
377 }
378
379 if (isMinPeriod) {
380 sql += TString::Format("period_number >= %d", iPeriod);
381 if (isMaxPeriod)
382 sql += TString::Format(" AND period_number <= %d", iMaxPeriod);
383 } else {
384 if (isMaxPeriod)
385 sql += TString::Format("period_number <= %d", iMaxPeriod);
386 else
387 sql += TString::Format("period_number = %d", iPeriod);
388 }
389 }
390 // if run selection
391 if (isRun == true) {
392 if (isWhere)
393 sql += " AND ";
394 else {
395 isWhere = true;
396 sql += " "
397 "where ";
398 }
399
400 if (isMinRun) {
401 sql += TString::Format("run_number >= %d", iRun);
402 if (isMaxRun)
403 sql += TString::Format(" AND run_number <= %d", iMaxRun);
404 } else {
405 if (isMaxRun)
406 sql += TString::Format("run_number <= %d", iMaxRun);
407 else
408 sql += TString::Format("run_number = %d", iRun);
409 }
410 }
411 // if beam particle was selected
412 if (isBeam == true) {
413 if (isWhere)
414 sql += TString::Format(" AND lower(beam_particle) = '%s'", strBeam.data());
415 else {
416 isWhere = true;
417 sql += TString::Format(" "
418 "where lower(beam_particle) = '%s'",
419 strBeam.data());
420 }
421 }
422 // if target particle was selected
423 if (isTarget == true) {
424 if (isWhere)
425 sql += TString::Format(" AND lower(target_particle) = '%s'", strTarget.data());
426 else {
427 isWhere = true;
428 sql += TString::Format(" "
429 "where lower(target_particle) = '%s'",
430 strTarget.data());
431 }
432 }
433 // if energy selection
434 if (isEnergy == true) {
435 if (isWhere)
436 sql += " AND ";
437 else {
438 isWhere = true;
439 sql += " "
440 "where ";
441 }
442
443 if (isMinEnergy) {
444 sql += TString::Format("energy >= %f", fEnergy);
445 if (isMaxEnergy)
446 sql += TString::Format(" AND energy <= %f", fMaxEnergy);
447 } else {
448 if (isMaxEnergy)
449 sql += TString::Format("energy <= %f", fMaxEnergy);
450 else
451 sql += TString::Format("energy = %f", fEnergy);
452 }
453 }
454 // if event count selection
455 if (isEvents == true) {
456 if (isWhere)
457 sql += " AND ";
458 else {
459 isWhere = true;
460 sql += " "
461 "where ";
462 }
463
464 if (isMinEvents) {
465 sql += TString::Format("event_count >= %d", iEvents);
466 if (isMaxEvents)
467 sql += TString::Format(" AND event_count <= %d", iMaxEvents);
468 } else {
469 if (isMaxEvents)
470 sql += TString::Format("event_count <= %d", iMaxEvents);
471 else
472 sql += TString::Format("event_count = %d", iEvents);
473 }
474 }
475 // if time selection
476 if (isTime == true) {
477 if (isWhere)
478 sql += " AND ";
479 else {
480 isWhere = true;
481 sql += " "
482 "where ";
483 }
484
485 // check correct format
486 struct tm tm;
487 if (!strptime(strMaxTime.c_str(), "%Y-%m-%d %H:%M:%S", &tm)) {
488 isMaxTime = false;
489 cout << "ERROR: " << strMaxTime
490 << " is not correct datetime. DateTime format should be yyyy-mm-dd 24hh:mm:ss." << endl;
491 }
492 if (!strptime(strTime.c_str(), "%Y-%m-%d %H:%M:%S", &tm)) {
493 isMinTime = false;
494 cout << "ERROR: " << strTime << " is not correct datetime. DateTime format should be yyyy-mm-dd 24hh:mm:ss."
495 << endl;
496 if (!isMaxTime)
497 isTime = false;
498 }
499
500 if (isMinTime) {
501 sql += TString::Format("end_datetime >= '%s'", strTime.c_str());
502 if (isMaxTime)
503 sql += TString::Format(" AND start_datetime <= '%s'", strMaxTime.c_str());
504 } else {
505 if (isMaxTime)
506 sql += TString::Format("start_datetime <= '%s'", strMaxTime.c_str());
507 else {
508 if (isTime)
509 sql += TString::Format("start_datetime <= '%s' AND end_datetime >= '%s'", strTime.c_str(),
510 strTime.c_str());
511 }
512 }
513 }
514 // if field current selection
515 if (isField == true) {
516 if (isWhere)
517 sql += " AND ";
518 else {
519 isWhere = true;
520 sql += " "
521 "where ";
522 }
523
524 if (isMinField) {
525 sql += TString::Format("field_voltage >= %d", iField);
526 if (isMaxField)
527 sql += TString::Format(" AND field_voltage <= %d", iMaxField);
528 } else {
529 if (isMaxField)
530 sql += TString::Format("field_voltage <= %d", iMaxField);
531 else
532 sql += TString::Format("field_voltage = %d", iField);
533 }
534 }
535 // if file size selection
536 if (isSize == true) {
537 if (isWhere)
538 sql += " AND ";
539 else {
540 isWhere = true;
541 sql += " "
542 "where ";
543 }
544
545 if (isMinSize) {
546 sql += TString::Format("file_size >= %f", fSize);
547 if (isMaxSize)
548 sql += TString::Format(" AND file_size <= %f", fMaxSize);
549 } else {
550 if (isMaxSize)
551 sql += TString::Format("file_size <= %f", fMaxSize);
552 else
553 sql += TString::Format("file_size = %f", fSize);
554 }
555 }
556 if (isPath == true) {
557 if (isWhere)
558 sql += TString::Format(" AND lower(file_path) like '%%%s%%'", strPath.data());
559 else {
560 isWhere = true;
561 sql += TString::Format(" "
562 "where lower(file_path) like '%%%s%%'",
563 strPath.data());
564 }
565 }
566
567 sql += " order by period_number,run_number";
568 // cout<<sql<<endl;
569
570 TSQLResult* res = pSQLServer->Query(sql);
571
572 int nrows = res->GetRowCount();
573 if (nrows == 0)
574 cout << "There are no experimental files for these parameters" << endl;
575 else {
576 TSQLRow* row;
577 while ((row = res->Next()) != nullptr) {
578 TString path = row->GetField(0);
579 cout << path << endl;
580
581 delete row;
582 }
583
584 cout << endl << "Total count: " << nrows << endl;
585 }
586
587 delete res;
588
589 if (pSQLServer)
590 delete pSQLServer;
591
592 return 0;
593}
int i
Definition P4_F32vec4.h:22
const char *const UNI_DB_HOST
const char *const UNI_DB_NAME
const char *const UNI_DB_PASSWORD
const char *const UNI_DB_USERNAME
STL namespace.
int main()