BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
UniRawFile.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// UniRawFile cxx file
3// Generated 27-09-2022
4// ----------------------------------------------------------------------
5
6#include "UniRawFile.h"
7
8#include "TMD5.h"
9#include "TSQLServer.h"
10#include "TSQLStatement.h"
11
12#ifdef HAVE_ZLIB
13#include <zlib.h>
14#endif
15#include <fstream>
16#include <iomanip>
17#include <iostream>
18#include <sstream>
19#include <string>
20using namespace std;
21
22/* GENERATED CLASS MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */
23// ----- Constructor with database connection -----------------------
25 int file_id,
26 int period_number,
27 int run_number,
28 int start_event,
29 int end_event,
30 TDatime start_datetime,
31 TDatime end_datetime,
32 TString file_path,
33 int event_count,
34 int64_t file_size,
35 TString* file_hash)
36{
37 connectionDB = db_connect;
38
39 i_file_id = file_id;
40 i_period_number = period_number;
41 i_run_number = run_number;
42 i_start_event = start_event;
43 i_end_event = end_event;
44 ts_start_datetime = start_datetime;
45 ts_end_datetime = end_datetime;
46 str_file_path = file_path;
47 i_event_count = event_count;
48 i64_file_size = file_size;
49 str_file_hash = file_hash;
50}
51
52// ----- Destructor -------------------------------------------------
54{
55 if (connectionDB)
56 delete connectionDB;
57 if (str_file_hash)
58 delete str_file_hash;
59}
60
61// ----- Creating new raw file in the database ---------------------------
63 int run_number,
64 int start_event,
65 int end_event,
66 TDatime start_datetime,
67 TDatime end_datetime,
68 TString file_path,
69 int event_count,
70 int64_t file_size,
71 TString* file_hash)
72{
74 if (connDb == nullptr)
75 return nullptr;
76
77 TSQLServer* db_server = connDb->GetSQLServer();
78
79 TString sql = TString::Format("insert into raw_file(period_number, run_number, start_event, end_event, "
80 "start_datetime, end_datetime, file_path, event_count, file_size, file_hash) "
81 "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)");
82 TSQLStatement* stmt = db_server->Statement(sql);
83
84 stmt->NextIteration();
85 stmt->SetInt(0, period_number);
86 stmt->SetInt(1, run_number);
87 stmt->SetInt(2, start_event);
88 stmt->SetInt(3, end_event);
89 stmt->SetDatime(4, start_datetime);
90 stmt->SetDatime(5, end_datetime);
91 stmt->SetString(6, file_path);
92 stmt->SetInt(7, event_count);
93 stmt->SetLong64(8, file_size);
94 if (file_hash == nullptr)
95 stmt->SetNull(9);
96 else
97 stmt->SetString(9, *file_hash);
98
99 // inserting new raw file to the Database
100 if (!stmt->Process()) {
101 cout << "ERROR: inserting new raw file to the Database has been failed" << endl;
102 delete stmt;
103 delete connDb;
104 return nullptr;
105 }
106
107 delete stmt;
108
109 // getting last inserted ID
110 int file_id;
111 TSQLStatement* stmt_last = db_server->Statement("SELECT currval(pg_get_serial_sequence('raw_file','file_id'))");
112
113 // process getting last id
114 if (stmt_last->Process()) {
115 // store result of statement in buffer
116 stmt_last->StoreResult();
117
118 // if there is no last id then exit with error
119 if (!stmt_last->NextResultRow()) {
120 cout << "ERROR: no last ID in DB!" << endl;
121 delete stmt_last;
122 return nullptr;
123 } else {
124 file_id = stmt_last->GetInt(0);
125 delete stmt_last;
126 }
127 } else {
128 cout << "ERROR: getting last ID has been failed!" << endl;
129 delete stmt_last;
130 return nullptr;
131 }
132
133 int tmp_file_id;
134 tmp_file_id = file_id;
135 int tmp_period_number;
136 tmp_period_number = period_number;
137 int tmp_run_number;
138 tmp_run_number = run_number;
139 int tmp_start_event;
140 tmp_start_event = start_event;
141 int tmp_end_event;
142 tmp_end_event = end_event;
143 TDatime tmp_start_datetime;
144 tmp_start_datetime = start_datetime;
145 TDatime tmp_end_datetime;
146 tmp_end_datetime = end_datetime;
147 TString tmp_file_path;
148 tmp_file_path = file_path;
149 int tmp_event_count;
150 tmp_event_count = event_count;
151 int64_t tmp_file_size;
152 tmp_file_size = file_size;
153 TString* tmp_file_hash;
154 if (file_hash == nullptr)
155 tmp_file_hash = nullptr;
156 else
157 tmp_file_hash = new TString(*file_hash);
158
159 return new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event, tmp_end_event,
160 tmp_start_datetime, tmp_end_datetime, tmp_file_path, tmp_event_count, tmp_file_size,
161 tmp_file_hash);
162}
163
164// ----- Get raw file from the database ---------------------------
166{
168 if (connDb == nullptr)
169 return nullptr;
170
171 TSQLServer* db_server = connDb->GetSQLServer();
172
173 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
174 "end_datetime, file_path, event_count, file_size, file_hash "
175 "from raw_file "
176 "where file_id = %d",
177 file_id);
178 TSQLStatement* stmt = db_server->Statement(sql);
179
180 // get raw file from the database
181 if (!stmt->Process()) {
182 cout << "ERROR: getting raw file from the database has been failed" << endl;
183
184 delete stmt;
185 delete connDb;
186 return nullptr;
187 }
188
189 // store result of statement in buffer
190 stmt->StoreResult();
191
192 // extract row
193 if (!stmt->NextResultRow()) {
194 cout << "ERROR: raw file was not found in the database" << endl;
195
196 delete stmt;
197 delete connDb;
198 return nullptr;
199 }
200
201 int tmp_file_id;
202 tmp_file_id = stmt->GetInt(0);
203 int tmp_period_number;
204 tmp_period_number = stmt->GetInt(1);
205 int tmp_run_number;
206 tmp_run_number = stmt->GetInt(2);
207 int tmp_start_event;
208 tmp_start_event = stmt->GetInt(3);
209 int tmp_end_event;
210 tmp_end_event = stmt->GetInt(4);
211 TDatime tmp_start_datetime;
212 tmp_start_datetime = stmt->GetDatime(5);
213 TDatime tmp_end_datetime;
214 tmp_end_datetime = stmt->GetDatime(6);
215 TString tmp_file_path;
216 tmp_file_path = stmt->GetString(7);
217 int tmp_event_count;
218 tmp_event_count = stmt->GetInt(8);
219 int64_t tmp_file_size;
220 tmp_file_size = stmt->GetLong64(9);
221 TString* tmp_file_hash;
222 if (stmt->IsNull(10))
223 tmp_file_hash = nullptr;
224 else
225 tmp_file_hash = new TString(stmt->GetString(10));
226
227 delete stmt;
228
229 return new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event, tmp_end_event,
230 tmp_start_datetime, tmp_end_datetime, tmp_file_path, tmp_event_count, tmp_file_size,
231 tmp_file_hash);
232}
233
234// ----- Get raw file from the database by unique key --------------
236{
238 if (connDb == nullptr)
239 return nullptr;
240
241 TSQLServer* db_server = connDb->GetSQLServer();
242
243 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
244 "end_datetime, file_path, event_count, file_size, file_hash "
245 "from raw_file "
246 "where lower(file_path) = lower('%s')",
247 file_path.Data());
248 TSQLStatement* stmt = db_server->Statement(sql);
249
250 // get raw file from the database
251 if (!stmt->Process()) {
252 cout << "ERROR: getting raw file from the database has been failed" << endl;
253
254 delete stmt;
255 delete connDb;
256 return nullptr;
257 }
258
259 // store result of statement in buffer
260 stmt->StoreResult();
261
262 // extract row
263 if (!stmt->NextResultRow()) {
264 cout << "ERROR: raw file was not found in the database" << endl;
265
266 delete stmt;
267 delete connDb;
268 return nullptr;
269 }
270
271 int tmp_file_id;
272 tmp_file_id = stmt->GetInt(0);
273 int tmp_period_number;
274 tmp_period_number = stmt->GetInt(1);
275 int tmp_run_number;
276 tmp_run_number = stmt->GetInt(2);
277 int tmp_start_event;
278 tmp_start_event = stmt->GetInt(3);
279 int tmp_end_event;
280 tmp_end_event = stmt->GetInt(4);
281 TDatime tmp_start_datetime;
282 tmp_start_datetime = stmt->GetDatime(5);
283 TDatime tmp_end_datetime;
284 tmp_end_datetime = stmt->GetDatime(6);
285 TString tmp_file_path;
286 tmp_file_path = stmt->GetString(7);
287 int tmp_event_count;
288 tmp_event_count = stmt->GetInt(8);
289 int64_t tmp_file_size;
290 tmp_file_size = stmt->GetLong64(9);
291 TString* tmp_file_hash;
292 if (stmt->IsNull(10))
293 tmp_file_hash = nullptr;
294 else
295 tmp_file_hash = new TString(stmt->GetString(10));
296
297 delete stmt;
298
299 return new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event, tmp_end_event,
300 tmp_start_datetime, tmp_end_datetime, tmp_file_path, tmp_event_count, tmp_file_size,
301 tmp_file_hash);
302}
303
304// ----- Check raw file exists in the database ---------------------------
306{
308 if (connDb == nullptr)
309 return -1;
310
311 TSQLServer* db_server = connDb->GetSQLServer();
312
313 TString sql = TString::Format("select 1 "
314 "from raw_file "
315 "where file_id = %d",
316 file_id);
317 TSQLStatement* stmt = db_server->Statement(sql);
318
319 // get raw file from the database
320 if (!stmt->Process()) {
321 cout << "ERROR: getting raw file from the database has been failed" << endl;
322
323 delete stmt;
324 delete connDb;
325 return -2;
326 }
327
328 // store result of statement in buffer
329 stmt->StoreResult();
330
331 // extract row
332 if (!stmt->NextResultRow()) {
333 delete stmt;
334 delete connDb;
335 return 0;
336 }
337
338 delete stmt;
339 delete connDb;
340
341 return 1;
342}
343
344// ----- Check raw file exists in the database by unique key --------------
346{
348 if (connDb == nullptr)
349 return -1;
350
351 TSQLServer* db_server = connDb->GetSQLServer();
352
353 TString sql = TString::Format("select 1 "
354 "from raw_file "
355 "where lower(file_path) = lower('%s')",
356 file_path.Data());
357 TSQLStatement* stmt = db_server->Statement(sql);
358
359 // get raw file from the database
360 if (!stmt->Process()) {
361 cout << "ERROR: getting raw file from the database has been failed" << endl;
362
363 delete stmt;
364 delete connDb;
365 return -2;
366 }
367
368 // store result of statement in buffer
369 stmt->StoreResult();
370
371 // extract row
372 if (!stmt->NextResultRow()) {
373 delete stmt;
374 delete connDb;
375 return 0;
376 }
377
378 delete stmt;
379 delete connDb;
380
381 return 1;
382}
383
384// ----- Delete raw file from the database ---------------------------
386{
388 if (connDb == nullptr)
389 return -1;
390
391 TSQLServer* db_server = connDb->GetSQLServer();
392
393 TString sql = TString::Format("delete from raw_file "
394 "where file_id = $1");
395 TSQLStatement* stmt = db_server->Statement(sql);
396
397 stmt->NextIteration();
398 stmt->SetInt(0, file_id);
399
400 // delete raw file from the dataBase
401 if (!stmt->Process()) {
402 cout << "ERROR: deleting raw file from the dataBase has been failed" << endl;
403
404 delete stmt;
405 delete connDb;
406 return -2;
407 }
408
409 delete stmt;
410 delete connDb;
411 return 0;
412}
413
414// ----- Delete raw file from the database by unique key --------------
416{
418 if (connDb == nullptr)
419 return -1;
420
421 TSQLServer* db_server = connDb->GetSQLServer();
422
423 TString sql = TString::Format("delete from raw_file "
424 "where lower(file_path) = lower($1)");
425 TSQLStatement* stmt = db_server->Statement(sql);
426
427 stmt->NextIteration();
428 stmt->SetString(0, file_path);
429
430 // delete raw file from the dataBase
431 if (!stmt->Process()) {
432 cout << "ERROR: deleting raw file from the DataBase has been failed" << endl;
433
434 delete stmt;
435 delete connDb;
436 return -2;
437 }
438
439 delete stmt;
440 delete connDb;
441 return 0;
442}
443
444// ----- Print all 'raw files' ---------------------------------
446{
448 if (connDb == nullptr)
449 return -1;
450
451 TSQLServer* db_server = connDb->GetSQLServer();
452
453 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
454 "end_datetime, file_path, event_count, file_size, file_hash "
455 "from raw_file");
456 TSQLStatement* stmt = db_server->Statement(sql);
457
458 // get all 'raw files' from the database
459 if (!stmt->Process()) {
460 cout << "ERROR: getting all 'raw files' from the dataBase has been failed" << endl;
461
462 delete stmt;
463 delete connDb;
464 return -2;
465 }
466
467 // store result of statement in buffer
468 stmt->StoreResult();
469
470 // print rows
471 cout << "Table 'raw_file':" << endl;
472 while (stmt->NextResultRow()) {
473 cout << "file_id: ";
474 cout << (stmt->GetInt(0));
475 cout << ", period_number: ";
476 cout << (stmt->GetInt(1));
477 cout << ", run_number: ";
478 cout << (stmt->GetInt(2));
479 cout << ", start_event: ";
480 cout << (stmt->GetInt(3));
481 cout << ", end_event: ";
482 cout << (stmt->GetInt(4));
483 cout << ", start_datetime: ";
484 cout << (stmt->GetDatime(5)).AsSQLString();
485 cout << ", end_datetime: ";
486 cout << (stmt->GetDatime(6)).AsSQLString();
487 cout << ", file_path: ";
488 cout << (stmt->GetString(7));
489 cout << ", event_count: ";
490 cout << (stmt->GetInt(8));
491 cout << ", file_size: ";
492 cout << (stmt->GetLong64(9));
493 cout << ", file_hash: ";
494 if (stmt->IsNull(10))
495 cout << "nullptr";
496 else
497 cout << stmt->GetString(10);
498 cout << "." << endl;
499 }
500
501 delete stmt;
502 delete connDb;
503
504 return 0;
505}
506
507// Setters functions
508int UniRawFile::SetPeriodNumber(int period_number)
509{
510 if (!connectionDB) {
511 cout << "CRITICAL ERROR: Connection object is null" << endl;
512 return -1;
513 }
514
515 TSQLServer* db_server = connectionDB->GetSQLServer();
516
517 TString sql = TString::Format("update raw_file "
518 "set period_number = $1 "
519 "where file_id = $2");
520 TSQLStatement* stmt = db_server->Statement(sql);
521
522 stmt->NextIteration();
523 stmt->SetInt(0, period_number);
524 stmt->SetInt(1, i_file_id);
525
526 // write new value to the database
527 if (!stmt->Process()) {
528 cout << "ERROR: updating information about raw file has been failed" << endl;
529
530 delete stmt;
531 return -2;
532 }
533
534 i_period_number = period_number;
535
536 delete stmt;
537 return 0;
538}
539
540int UniRawFile::SetRunNumber(int run_number)
541{
542 if (!connectionDB) {
543 cout << "CRITICAL ERROR: Connection object is null" << endl;
544 return -1;
545 }
546
547 TSQLServer* db_server = connectionDB->GetSQLServer();
548
549 TString sql = TString::Format("update raw_file "
550 "set run_number = $1 "
551 "where file_id = $2");
552 TSQLStatement* stmt = db_server->Statement(sql);
553
554 stmt->NextIteration();
555 stmt->SetInt(0, run_number);
556 stmt->SetInt(1, i_file_id);
557
558 // write new value to the database
559 if (!stmt->Process()) {
560 cout << "ERROR: updating information about raw file has been failed" << endl;
561
562 delete stmt;
563 return -2;
564 }
565
566 i_run_number = run_number;
567
568 delete stmt;
569 return 0;
570}
571
572int UniRawFile::SetStartEvent(int start_event)
573{
574 if (!connectionDB) {
575 cout << "CRITICAL ERROR: Connection object is null" << endl;
576 return -1;
577 }
578
579 TSQLServer* db_server = connectionDB->GetSQLServer();
580
581 TString sql = TString::Format("update raw_file "
582 "set start_event = $1 "
583 "where file_id = $2");
584 TSQLStatement* stmt = db_server->Statement(sql);
585
586 stmt->NextIteration();
587 stmt->SetInt(0, start_event);
588 stmt->SetInt(1, i_file_id);
589
590 // write new value to the database
591 if (!stmt->Process()) {
592 cout << "ERROR: updating information about raw file has been failed" << endl;
593
594 delete stmt;
595 return -2;
596 }
597
598 i_start_event = start_event;
599
600 delete stmt;
601 return 0;
602}
603
604int UniRawFile::SetEndEvent(int end_event)
605{
606 if (!connectionDB) {
607 cout << "CRITICAL ERROR: Connection object is null" << endl;
608 return -1;
609 }
610
611 TSQLServer* db_server = connectionDB->GetSQLServer();
612
613 TString sql = TString::Format("update raw_file "
614 "set end_event = $1 "
615 "where file_id = $2");
616 TSQLStatement* stmt = db_server->Statement(sql);
617
618 stmt->NextIteration();
619 stmt->SetInt(0, end_event);
620 stmt->SetInt(1, i_file_id);
621
622 // write new value to the database
623 if (!stmt->Process()) {
624 cout << "ERROR: updating information about raw file has been failed" << endl;
625
626 delete stmt;
627 return -2;
628 }
629
630 i_end_event = end_event;
631
632 delete stmt;
633 return 0;
634}
635
637{
638 if (!connectionDB) {
639 cout << "CRITICAL ERROR: Connection object is null" << endl;
640 return -1;
641 }
642
643 TSQLServer* db_server = connectionDB->GetSQLServer();
644
645 TString sql = TString::Format("update raw_file "
646 "set start_datetime = $1 "
647 "where file_id = $2");
648 TSQLStatement* stmt = db_server->Statement(sql);
649
650 stmt->NextIteration();
651 stmt->SetDatime(0, start_datetime);
652 stmt->SetInt(1, i_file_id);
653
654 // write new value to the database
655 if (!stmt->Process()) {
656 cout << "ERROR: updating information about raw file has been failed" << endl;
657
658 delete stmt;
659 return -2;
660 }
661
662 ts_start_datetime = start_datetime;
663
664 delete stmt;
665 return 0;
666}
667
669{
670 if (!connectionDB) {
671 cout << "CRITICAL ERROR: Connection object is null" << endl;
672 return -1;
673 }
674
675 TSQLServer* db_server = connectionDB->GetSQLServer();
676
677 TString sql = TString::Format("update raw_file "
678 "set end_datetime = $1 "
679 "where file_id = $2");
680 TSQLStatement* stmt = db_server->Statement(sql);
681
682 stmt->NextIteration();
683 stmt->SetDatime(0, end_datetime);
684 stmt->SetInt(1, i_file_id);
685
686 // write new value to the database
687 if (!stmt->Process()) {
688 cout << "ERROR: updating information about raw file has been failed" << endl;
689
690 delete stmt;
691 return -2;
692 }
693
694 ts_end_datetime = end_datetime;
695
696 delete stmt;
697 return 0;
698}
699
701{
702 if (!connectionDB) {
703 cout << "CRITICAL ERROR: Connection object is null" << endl;
704 return -1;
705 }
706
707 TSQLServer* db_server = connectionDB->GetSQLServer();
708
709 TString sql = TString::Format("update raw_file "
710 "set file_path = $1 "
711 "where file_id = $2");
712 TSQLStatement* stmt = db_server->Statement(sql);
713
714 stmt->NextIteration();
715 stmt->SetString(0, file_path);
716 stmt->SetInt(1, i_file_id);
717
718 // write new value to the database
719 if (!stmt->Process()) {
720 cout << "ERROR: updating information about raw file has been failed" << endl;
721
722 delete stmt;
723 return -2;
724 }
725
726 str_file_path = file_path;
727
728 delete stmt;
729 return 0;
730}
731
732int UniRawFile::SetEventCount(int event_count)
733{
734 if (!connectionDB) {
735 cout << "CRITICAL ERROR: Connection object is null" << endl;
736 return -1;
737 }
738
739 TSQLServer* db_server = connectionDB->GetSQLServer();
740
741 TString sql = TString::Format("update raw_file "
742 "set event_count = $1 "
743 "where file_id = $2");
744 TSQLStatement* stmt = db_server->Statement(sql);
745
746 stmt->NextIteration();
747 stmt->SetInt(0, event_count);
748 stmt->SetInt(1, i_file_id);
749
750 // write new value to the database
751 if (!stmt->Process()) {
752 cout << "ERROR: updating information about raw file has been failed" << endl;
753
754 delete stmt;
755 return -2;
756 }
757
758 i_event_count = event_count;
759
760 delete stmt;
761 return 0;
762}
763
764int UniRawFile::SetFileSize(int64_t file_size)
765{
766 if (!connectionDB) {
767 cout << "CRITICAL ERROR: Connection object is null" << endl;
768 return -1;
769 }
770
771 TSQLServer* db_server = connectionDB->GetSQLServer();
772
773 TString sql = TString::Format("update raw_file "
774 "set file_size = $1 "
775 "where file_id = $2");
776 TSQLStatement* stmt = db_server->Statement(sql);
777
778 stmt->NextIteration();
779 stmt->SetLong64(0, file_size);
780 stmt->SetInt(1, i_file_id);
781
782 // write new value to the database
783 if (!stmt->Process()) {
784 cout << "ERROR: updating information about raw file has been failed" << endl;
785
786 delete stmt;
787 return -2;
788 }
789
790 i64_file_size = file_size;
791
792 delete stmt;
793 return 0;
794}
795
797{
798 if (!connectionDB) {
799 cout << "CRITICAL ERROR: Connection object is null" << endl;
800 return -1;
801 }
802
803 TSQLServer* db_server = connectionDB->GetSQLServer();
804
805 TString sql = TString::Format("update raw_file "
806 "set file_hash = $1 "
807 "where file_id = $2");
808 TSQLStatement* stmt = db_server->Statement(sql);
809
810 stmt->NextIteration();
811 if (file_hash == nullptr)
812 stmt->SetNull(0);
813 else
814 stmt->SetString(0, *file_hash);
815 stmt->SetInt(1, i_file_id);
816
817 // write new value to the database
818 if (!stmt->Process()) {
819 cout << "ERROR: updating information about raw file has been failed" << endl;
820
821 delete stmt;
822 return -2;
823 }
824
825 if (str_file_hash)
826 delete str_file_hash;
827 if (file_hash == nullptr)
828 str_file_hash = nullptr;
829 else
830 str_file_hash = new TString(*file_hash);
831
832 delete stmt;
833 return 0;
834}
835
836// ----- Print current raw file ---------------------------------------
838{
839 cout << "Table 'raw_file'";
840 cout << ". file_id: " << i_file_id << ". period_number: " << i_period_number << ". run_number: " << i_run_number
841 << ". start_event: " << i_start_event << ". end_event: " << i_end_event
842 << ". start_datetime: " << ts_start_datetime.AsSQLString()
843 << ". end_datetime: " << ts_end_datetime.AsSQLString() << ". file_path: " << str_file_path
844 << ". event_count: " << i_event_count << ". file_size: " << i64_file_size
845 << ". file_hash: " << (str_file_hash == nullptr ? "nullptr" : *str_file_hash) << endl;
846
847 return;
848}
849/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
850
851// check raw file exists in the database
852// for the given period_number:run_number and start_event-end_event values
853// returns 1 - true, 0 - false, <0 - database operation error
854int UniRawFile::CheckRawFileExists(int period_number,
855 int run_number,
856 int start_event,
857 int end_event,
858 bool strict_compare)
859{
861 if (connDb == nullptr)
862 return -1;
863
864 TSQLServer* db_server = connDb->GetSQLServer();
865
866 TString sql;
867 if (!strict_compare)
868 sql = TString::Format("select 1 "
869 "from raw_file "
870 "where period_number = %d AND run_number = %d "
871 "AND %d >= start_event AND %d <= end_event AND %d >= start_event AND %d <= end_event "
872 "AND NOT (((%d - start_event) = 1) AND ((%d - end_event) = -1))",
873 period_number, run_number, start_event, start_event, end_event, end_event, start_event,
874 end_event);
875 else
876 sql = TString::Format("select 1 "
877 "from raw_file "
878 "where period_number = %d AND run_number = %d "
879 "AND start_event = %d AND end_event = %d",
880 period_number, run_number, start_event, end_event);
881 TSQLStatement* stmt = db_server->Statement(sql);
882
883 // get raw file from the database
884 if (!stmt->Process()) {
885 cout << "ERROR: getting raw file from the database has been failed" << endl;
886
887 delete stmt;
888 delete connDb;
889 return -2;
890 }
891
892 // store result of statement in buffer
893 stmt->StoreResult();
894
895 // extract row
896 if (!stmt->NextResultRow()) {
897 delete stmt;
898 delete connDb;
899 return 0;
900 }
901
902 delete stmt;
903 delete connDb;
904
905 return 1;
906}
907
908// check that raw file exists in the database
909// for the given period_number:run_number and start_event value
910// returns maximum 'end_event' value if some exist, 0 - not exists, <0 - database operation error
911int UniRawFile::CheckRawFileExists(int period_number, int run_number, int start_event)
912{
914 if (connDb == nullptr)
915 return -1;
916
917 TSQLServer* db_server = connDb->GetSQLServer();
918
919 TString sql = TString::Format("select end_event "
920 "from raw_file "
921 "where period_number = %d AND run_number = %d "
922 "AND %d = start_event "
923 "order by end_event desc limit 1",
924 period_number, run_number, start_event);
925 TSQLStatement* stmt = db_server->Statement(sql);
926
927 // get raw file from the database
928 if (!stmt->Process()) {
929 cout << "ERROR: getting raw file from the database has been failed" << endl;
930
931 delete stmt;
932 delete connDb;
933 return -2;
934 }
935
936 // store result of statement in buffer
937 stmt->StoreResult();
938
939 // extract row
940 if (!stmt->NextResultRow()) {
941 delete stmt;
942 delete connDb;
943 return 0;
944 }
945
946 int i_end_event = stmt->GetInt(0);
947 if (i_end_event == 0) {
948 cout
949 << "ERROR: while checking existense of raw files in UniConDa one raw file is found with 0 end event value ("
950 << period_number << ":" << run_number << ":" << start_event << ")" << endl
951 << "The function returns 1 end event value to exclude uncertancies" << endl;
952 i_end_event++;
953 }
954
955 delete stmt;
956 delete connDb;
957
958 return i_end_event;
959}
960
961/// get event count for a given run as a sum for all raw files: if <0 - database operation error
962int UniRawFile::GetSumEventCount(int period_number, int run_number)
963{
965 if (connDb == nullptr)
966 return -1;
967
968 TSQLServer* db_server = connDb->GetSQLServer();
969
970 TString sql = TString::Format("select sum(event_count) "
971 "from raw_file "
972 "where period_number = %d AND run_number = %d",
973 period_number, run_number);
974 TSQLStatement* stmt = db_server->Statement(sql);
975
976 // get sum from the database
977 if (!stmt->Process()) {
978 cout << "ERROR: getting summary event count for run " << period_number << ":" << run_number
979 << " from the database has been failed" << endl;
980 delete stmt;
981 delete connDb;
982 return -2;
983 }
984
985 // store result of statement in buffer
986 stmt->StoreResult();
987
988 // extract row
989 if (!stmt->NextResultRow()) {
990 cout << "WARNING: there are no raw files for run " << period_number << ":" << run_number << " in the database"
991 << endl;
992 delete stmt;
993 delete connDb;
994 return 0;
995 }
996
997 int sum_event_count = stmt->GetInt(0);
998
999 delete stmt;
1000 delete connDb;
1001
1002 return sum_event_count;
1003}
1004
1005// get array of raw files (UniRawFile*) corresponding to the period:run number (return nullptr if an error occured)
1006TObjArray* UniRawFile::GetRawFiles(int period_number, int run_number)
1007{
1009 if (connDb == nullptr)
1010 return nullptr;
1011
1012 TSQLServer* db_server = connDb->GetSQLServer();
1013
1014 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1015 "end_datetime, file_path, event_count, file_size, file_hash "
1016 "from raw_file "
1017 "where period_number = %d AND run_number = %d "
1018 "order by start_event asc",
1019 period_number, run_number);
1020 TSQLStatement* stmt = db_server->Statement(sql);
1021
1022 // get raw files for the run from the database
1023 if (!stmt->Process()) {
1024 cout << "ERROR: getting raw files for run " << period_number << ":" << run_number
1025 << " from the database has been failed" << endl;
1026 delete stmt;
1027 delete connDb;
1028 return nullptr;
1029 }
1030
1031 // store result of statement in buffer
1032 stmt->StoreResult();
1033
1034 TObjArray* raw_files = new TObjArray();
1035 while (stmt->NextResultRow()) {
1037 if (connRaw == nullptr) {
1038 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1039 delete stmt;
1040 delete connDb;
1041 return nullptr;
1042 }
1043
1044 int tmp_file_id;
1045 tmp_file_id = stmt->GetInt(0);
1046 int tmp_period_number;
1047 tmp_period_number = stmt->GetInt(1);
1048 int tmp_run_number;
1049 tmp_run_number = stmt->GetInt(2);
1050 int tmp_start_event;
1051 tmp_start_event = stmt->GetInt(3);
1052 int tmp_end_event;
1053 tmp_end_event = stmt->GetInt(4);
1054 TDatime tmp_start_datetime;
1055 tmp_start_datetime = stmt->GetDatime(5);
1056 TDatime tmp_end_datetime;
1057 tmp_end_datetime = stmt->GetDatime(6);
1058 TString tmp_file_path;
1059 tmp_file_path = stmt->GetString(7);
1060 int tmp_event_count;
1061 tmp_event_count = stmt->GetInt(8);
1062 int64_t tmp_file_size;
1063 tmp_file_size = stmt->GetLong64(9);
1064 TString* tmp_file_hash;
1065 if (stmt->IsNull(10))
1066 tmp_file_hash = nullptr;
1067 else
1068 tmp_file_hash = new TString(stmt->GetString(10));
1069
1070 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1071 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1072 tmp_event_count, tmp_file_size, tmp_file_hash));
1073 }
1074
1075 delete stmt;
1076 delete connDb;
1077
1078 return raw_files;
1079}
1080
1081// get array of raw files (UniRawFile*) corresponding to the period_number:run_number and start_event - end_event
1082// ordered by file_id descending
1083// returns nullptr if an error occured
1085 int run_number,
1086 int start_event,
1087 int end_event,
1088 bool strict_compare)
1089{
1091 if (connDb == nullptr)
1092 return nullptr;
1093
1094 TSQLServer* db_server = connDb->GetSQLServer();
1095
1096 TString sql;
1097 if (strict_compare)
1098 sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1099 "end_datetime, file_path, event_count, file_size, file_hash "
1100 "from raw_file "
1101 "where period_number = %d AND run_number = %d AND start_event = %d AND end_event = %d "
1102 "order by file_id desc",
1103 period_number, run_number, start_event, end_event);
1104 else
1105 sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1106 "end_datetime, file_path, event_count, file_size, file_hash "
1107 "from raw_file "
1108 "where period_number = %d AND run_number = %d "
1109 "AND %d >= start_event AND %d <= end_event AND %d >= start_event AND %d <= end_event "
1110 "AND NOT (((%d - start_event) = 1) AND ((%d - end_event) = -1)) "
1111 "order by file_id desc",
1112 period_number, run_number, start_event, start_event, end_event, end_event, start_event,
1113 end_event);
1114
1115 TSQLStatement* stmt = db_server->Statement(sql);
1116
1117 // get raw files for the run and events from the database
1118 if (!stmt->Process()) {
1119 cout << "ERROR: getting raw files for run " << period_number << ":" << run_number << " and events "
1120 << start_event << "-" << end_event << " from the database has been failed" << endl;
1121 delete stmt;
1122 delete connDb;
1123 return nullptr;
1124 }
1125
1126 // store result of statement in buffer
1127 stmt->StoreResult();
1128
1129 TObjArray* raw_files = new TObjArray();
1130 while (stmt->NextResultRow()) {
1132 if (connRaw == nullptr) {
1133 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1134 delete stmt;
1135 delete connDb;
1136 return nullptr;
1137 }
1138
1139 int tmp_file_id;
1140 tmp_file_id = stmt->GetInt(0);
1141 int tmp_period_number;
1142 tmp_period_number = stmt->GetInt(1);
1143 int tmp_run_number;
1144 tmp_run_number = stmt->GetInt(2);
1145 int tmp_start_event;
1146 tmp_start_event = stmt->GetInt(3);
1147 int tmp_end_event;
1148 tmp_end_event = stmt->GetInt(4);
1149 TDatime tmp_start_datetime;
1150 tmp_start_datetime = stmt->GetDatime(5);
1151 TDatime tmp_end_datetime;
1152 tmp_end_datetime = stmt->GetDatime(6);
1153 TString tmp_file_path;
1154 tmp_file_path = stmt->GetString(7);
1155 int tmp_event_count;
1156 tmp_event_count = stmt->GetInt(8);
1157 int64_t tmp_file_size;
1158 tmp_file_size = stmt->GetLong64(9);
1159 TString* tmp_file_hash;
1160 if (stmt->IsNull(10))
1161 tmp_file_hash = nullptr;
1162 else
1163 tmp_file_hash = new TString(stmt->GetString(10));
1164
1165 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1166 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1167 tmp_event_count, tmp_file_size, tmp_file_hash));
1168 }
1169
1170 delete stmt;
1171 delete connDb;
1172
1173 return raw_files;
1174}
1175
1176// get array of raw files (UniRawFile*) corresponding to a part of the file path, e.g. file name
1177// ordered by file_id descending
1178// return nullptr if an error occured)
1180{
1182 if (connDb == nullptr)
1183 return nullptr;
1184
1185 TSQLServer* db_server = connDb->GetSQLServer();
1186
1187 // 'position' is used instead of 'like', because the searched path part is a plain substring and not a pattern:
1188 // the file names of the experiment are full of underscores, which 'like' would treat as "any symbol" wildcards
1189 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1190 "end_datetime, file_path, event_count, file_size, file_hash "
1191 "from raw_file "
1192 "where position('%s' in file_path) > 0 "
1193 "order by file_id desc",
1194 path_part.Data());
1195 TSQLStatement* stmt = db_server->Statement(sql);
1196
1197 // get raw files for the file path part from the database
1198 if (!stmt->Process()) {
1199 cout << "ERROR: getting raw files for file path part " << path_part << " from the database has been failed"
1200 << endl;
1201 delete stmt;
1202 delete connDb;
1203 return nullptr;
1204 }
1205
1206 // store result of statement in buffer
1207 stmt->StoreResult();
1208
1209 TObjArray* raw_files = new TObjArray();
1210 while (stmt->NextResultRow()) {
1212 if (connRaw == nullptr) {
1213 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1214 delete stmt;
1215 delete connDb;
1216 return nullptr;
1217 }
1218
1219 int tmp_file_id;
1220 tmp_file_id = stmt->GetInt(0);
1221 int tmp_period_number;
1222 tmp_period_number = stmt->GetInt(1);
1223 int tmp_run_number;
1224 tmp_run_number = stmt->GetInt(2);
1225 int tmp_start_event;
1226 tmp_start_event = stmt->GetInt(3);
1227 int tmp_end_event;
1228 tmp_end_event = stmt->GetInt(4);
1229 TDatime tmp_start_datetime;
1230 tmp_start_datetime = stmt->GetDatime(5);
1231 TDatime tmp_end_datetime;
1232 tmp_end_datetime = stmt->GetDatime(6);
1233 TString tmp_file_path;
1234 tmp_file_path = stmt->GetString(7);
1235 int tmp_event_count;
1236 tmp_event_count = stmt->GetInt(8);
1237 int64_t tmp_file_size;
1238 tmp_file_size = stmt->GetLong64(9);
1239 TString* tmp_file_hash;
1240 if (stmt->IsNull(10))
1241 tmp_file_hash = nullptr;
1242 else
1243 tmp_file_hash = new TString(stmt->GetString(10));
1244
1245 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1246 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1247 tmp_event_count, tmp_file_size, tmp_file_hash));
1248 }
1249
1250 delete stmt;
1251 delete connDb;
1252
1253 return raw_files;
1254}
1255
1256/// calculate and return (in hex string format) cheсksum for a given file
1257/// use Adler32 algorithm if zlib library is found
1258/// or use MD5 algorithm if zlib library is not found
1260{
1261#ifdef HAVE_ZLIB
1262 ifstream raw_file(raw_file_path, ios::binary);
1263 if (!raw_file) {
1264 cout << "ERROR: raw file could not be opened to calculate Adler32 checksum: " << raw_file_path << endl;
1265 return "";
1266 }
1267
1268 uLong adler = adler32(0L, Z_NULL, 0); // initialize Adler32 checksum
1269
1270 const size_t bufferSize = 65536;
1271 char buffer[bufferSize];
1272 while (raw_file.read(buffer, bufferSize) || raw_file.gcount() > 0)
1273 adler = adler32(adler, (const Bytef*)buffer, raw_file.gcount());
1274
1275 raw_file.close();
1276
1277 // cout << "Adler32 for " << raw_file_path << ": 0x" << std::hex << adler << std::dec << endl;
1278 std::ostringstream hexStream;
1279 hexStream << std::hex << std::setw(8) << std::setfill('0') << (adler & 0xFFFFFFFF);
1280
1281 return hexStream.str();
1282#else
1283 TMD5* pMD5 = TMD5::FileChecksum(raw_file_path.Data());
1284 if (!pMD5) {
1285 cout << "ERROR: raw file could not be opened to calculate MD5 checksum: " << raw_file_path << endl;
1286 return "";
1287 }
1288
1289 // cout << "MD5 for " << raw_file_path << ": 0x" << pMD5->AsString() << endl;
1290 TString strMD5(pMD5->AsString());
1291
1292 delete pMD5 return strMD5;
1293#endif
1294}
const char * AsString() const
static TMD5 * FileChecksum(const char *file)
void Add(TObject *obj) override
virtual TSQLStatement * Statement(const char *, Int_t=100)
virtual const char * GetString(Int_t)
virtual Bool_t NextIteration()=0
virtual Bool_t NextResultRow()=0
virtual Bool_t SetString(Int_t, const char *, Int_t=256)
Bool_t SetDatime(Int_t, const TDatime &)
virtual Int_t GetInt(Int_t)
virtual Bool_t IsNull(Int_t)
virtual Long64_t GetLong64(Int_t)
virtual Bool_t Process()=0
virtual Bool_t SetInt(Int_t, Int_t)
virtual Bool_t SetNull(Int_t)
virtual Bool_t StoreResult()=0
virtual Bool_t SetLong64(Int_t, Long64_t)
TDatime GetDatime(Int_t)
const char * Data() const
static TString Format(const char *fmt,...)
static UniConnection * Open()
TSQLServer * GetSQLServer()
TString str_file_path
file path
Definition UniRawFile.h:40
static int PrintAll()
print all raw files
TString * str_file_hash
file hash
Definition UniRawFile.h:46
int i_start_event
start event
Definition UniRawFile.h:32
int SetFileSize(int64_t file_size)
set file size of the current raw file
void Print()
print information about current raw file
int SetEndDatetime(TDatime end_datetime)
set end datetime of the current raw file
UniConnection * connectionDB
connection to the database
Definition UniRawFile.h:23
int SetFileHash(TString *file_hash)
set file hash of the current raw file
int i_end_event
end event
Definition UniRawFile.h:34
static int GetSumEventCount(int period_number, int run_number)
get event count for a given run as a sum for all raw files: if <0 - database operation error
static UniRawFile * GetRawFile(int file_id)
get raw file from the database
static int CheckRawFileExists(int file_id)
check raw file exists in the database: 1- true, 0 - false, <0 - database operation error
int SetRunNumber(int run_number)
set run number of the current raw file
int SetEventCount(int event_count)
set event count of the current raw file
int SetStartDatetime(TDatime start_datetime)
set start datetime of the current raw file
int SetStartEvent(int start_event)
set start event of the current raw file
UniRawFile(UniConnection *db_connect, int file_id, int period_number, int run_number, int start_event, int end_event, TDatime start_datetime, TDatime end_datetime, TString file_path, int event_count, int64_t file_size, TString *file_hash)
static UniRawFile * CreateRawFile(int period_number, int run_number, int start_event, int end_event, TDatime start_datetime, TDatime end_datetime, TString file_path, int event_count, int64_t file_size, TString *file_hash)
add new raw file to the database
virtual ~UniRawFile()
int SetEndEvent(int end_event)
set end event of the current raw file
int i_period_number
period number
Definition UniRawFile.h:28
int SetFilePath(TString file_path)
set file path of the current raw file
int i_file_id
file id
Definition UniRawFile.h:26
int i_event_count
event count
Definition UniRawFile.h:42
TDatime ts_end_datetime
end datetime
Definition UniRawFile.h:38
TDatime ts_start_datetime
start datetime
Definition UniRawFile.h:36
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 i_run_number
run number
Definition UniRawFile.h:30
static TString CalculateFileHash(TString file_path)
calculate and return (in hex string format) cheсksum for a given file use Adler32 algorithm if zlib l...
int64_t i64_file_size
file size
Definition UniRawFile.h:44
int SetPeriodNumber(int period_number)
set period number of the current raw file