BmnRoot
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 -----------------------
24UniRawFile::UniRawFile(UniConnection* db_connect,
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 --------------
345int UniRawFile::CheckRawFileExists(TString file_path)
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 --------------
415int UniRawFile::DeleteRawFile(TString file_path)
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
636int UniRawFile::SetStartDatetime(TDatime start_datetime)
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
668int UniRawFile::SetEndDatetime(TDatime end_datetime)
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
700int UniRawFile::SetFilePath(TString file_path)
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
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
796int UniRawFile::SetFileHash(TString* file_hash)
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, int run_number, int start_event, int end_event, bool strict_compare)
855{
857 if (connDb == nullptr)
858 return -1;
859
860 TSQLServer* db_server = connDb->GetSQLServer();
861
862 TString sql;
863 if (!strict_compare)
864 sql = TString::Format("select 1 "
865 "from raw_file "
866 "where period_number = %d AND run_number = %d "
867 "AND %d >= start_event AND %d <= end_event AND %d >= start_event AND %d <= end_event "
868 "AND NOT (((%d - start_event) = 1) AND ((%d - end_event) = -1))",
869 period_number, run_number, start_event, start_event, end_event, end_event,
870 start_event, end_event);
871 else
872 sql = TString::Format("select 1 "
873 "from raw_file "
874 "where period_number = %d AND run_number = %d "
875 "AND start_event = %d AND end_event = %d",
876 period_number, run_number, start_event, end_event);
877 TSQLStatement* stmt = db_server->Statement(sql);
878
879 // get raw file from the database
880 if (!stmt->Process()) {
881 cout << "ERROR: getting raw file from the database has been failed" << endl;
882
883 delete stmt;
884 delete connDb;
885 return -2;
886 }
887
888 // store result of statement in buffer
889 stmt->StoreResult();
890
891 // extract row
892 if (!stmt->NextResultRow()) {
893 delete stmt;
894 delete connDb;
895 return 0;
896 }
897
898 delete stmt;
899 delete connDb;
900
901 return 1;
902}
903
904// check that raw file exists in the database
905// for the given period_number:run_number and start_event value
906// returns maximum 'end_event' value if some exist, 0 - not exists, <0 - database operation error
907int UniRawFile::CheckRawFileExists(int period_number, int run_number, int start_event)
908{
910 if (connDb == nullptr)
911 return -1;
912
913 TSQLServer* db_server = connDb->GetSQLServer();
914
915 TString sql = TString::Format("select end_event "
916 "from raw_file "
917 "where period_number = %d AND run_number = %d "
918 "AND %d = start_event "
919 "order by end_event desc limit 1",
920 period_number, run_number, start_event);
921 TSQLStatement* stmt = db_server->Statement(sql);
922
923 // get raw file from the database
924 if (!stmt->Process()) {
925 cout << "ERROR: getting raw file from the database has been failed" << endl;
926
927 delete stmt;
928 delete connDb;
929 return -2;
930 }
931
932 // store result of statement in buffer
933 stmt->StoreResult();
934
935 // extract row
936 if (!stmt->NextResultRow()) {
937 delete stmt;
938 delete connDb;
939 return 0;
940 }
941
942 int i_end_event = stmt->GetInt(0);
943 if (i_end_event == 0) {
944 cout
945 << "ERROR: while checking existense of raw files in UniConDa one raw file is found with 0 end event value ("
946 << period_number << ":" << run_number << ":" << start_event << ")" << endl
947 << "The function returns 1 end event value to exclude uncertancies" << endl;
948 i_end_event++;
949 }
950
951 delete stmt;
952 delete connDb;
953
954 return i_end_event;
955}
956
958int UniRawFile::GetSumEventCount(int period_number, int run_number)
959{
961 if (connDb == nullptr)
962 return -1;
963
964 TSQLServer* db_server = connDb->GetSQLServer();
965
966 TString sql = TString::Format("select sum(event_count) "
967 "from raw_file "
968 "where period_number = %d AND run_number = %d",
969 period_number, run_number);
970 TSQLStatement* stmt = db_server->Statement(sql);
971
972 // get sum from the database
973 if (!stmt->Process()) {
974 cout << "ERROR: getting summary event count for run " << period_number << ":" << run_number
975 << " from the database has been failed" << endl;
976 delete stmt;
977 delete connDb;
978 return -2;
979 }
980
981 // store result of statement in buffer
982 stmt->StoreResult();
983
984 // extract row
985 if (!stmt->NextResultRow()) {
986 cout << "WARNING: there are no raw files for run " << period_number << ":" << run_number << " in the database"
987 << endl;
988 delete stmt;
989 delete connDb;
990 return 0;
991 }
992
993 int sum_event_count = stmt->GetInt(0);
994
995 delete stmt;
996 delete connDb;
997
998 return sum_event_count;
999}
1000
1001// get array of raw files (UniRawFile*) corresponding to the period:run number (return nullptr if an error occured)
1002TObjArray* UniRawFile::GetRawFiles(int period_number, int run_number)
1003{
1005 if (connDb == nullptr)
1006 return nullptr;
1007
1008 TSQLServer* db_server = connDb->GetSQLServer();
1009
1010 TString sql = TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1011 "end_datetime, file_path, event_count, file_size, file_hash "
1012 "from raw_file "
1013 "where period_number = %d AND run_number = %d "
1014 "order by start_event asc",
1015 period_number, run_number);
1016 TSQLStatement* stmt = db_server->Statement(sql);
1017
1018 // get raw files for the run from the database
1019 if (!stmt->Process()) {
1020 cout << "ERROR: getting raw files for run " << period_number << ":" << run_number
1021 << " from the database has been failed" << endl;
1022 delete stmt;
1023 delete connDb;
1024 return nullptr;
1025 }
1026
1027 // store result of statement in buffer
1028 stmt->StoreResult();
1029
1030 TObjArray* raw_files = new TObjArray();
1031 while (stmt->NextResultRow()) {
1033 if (connRaw == nullptr) {
1034 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1035 delete stmt;
1036 delete connDb;
1037 return nullptr;
1038 }
1039
1040 int tmp_file_id;
1041 tmp_file_id = stmt->GetInt(0);
1042 int tmp_period_number;
1043 tmp_period_number = stmt->GetInt(1);
1044 int tmp_run_number;
1045 tmp_run_number = stmt->GetInt(2);
1046 int tmp_start_event;
1047 tmp_start_event = stmt->GetInt(3);
1048 int tmp_end_event;
1049 tmp_end_event = stmt->GetInt(4);
1050 TDatime tmp_start_datetime;
1051 tmp_start_datetime = stmt->GetDatime(5);
1052 TDatime tmp_end_datetime;
1053 tmp_end_datetime = stmt->GetDatime(6);
1054 TString tmp_file_path;
1055 tmp_file_path = stmt->GetString(7);
1056 int tmp_event_count;
1057 tmp_event_count = stmt->GetInt(8);
1058 int64_t tmp_file_size;
1059 tmp_file_size = stmt->GetLong64(9);
1060 TString* tmp_file_hash;
1061 if (stmt->IsNull(10))
1062 tmp_file_hash = nullptr;
1063 else
1064 tmp_file_hash = new TString(stmt->GetString(10));
1065
1066 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1067 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1068 tmp_event_count, tmp_file_size, tmp_file_hash));
1069 }
1070
1071 delete stmt;
1072 delete connDb;
1073
1074 return raw_files;
1075}
1076
1077// get array of raw files (UniRawFile*) corresponding to the period_number:run_number and start_event - end_event
1078// ordered by file_id descending
1079// returns nullptr if an error occured
1080TObjArray* UniRawFile::GetRawFiles(int period_number, int run_number, int start_event, int end_event, bool strict_compare)
1081{
1083 if (connDb == nullptr)
1084 return nullptr;
1085
1086 TSQLServer* db_server = connDb->GetSQLServer();
1087
1088 TString sql;
1089 if (strict_compare)
1090 sql =
1091 TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1092 "end_datetime, file_path, event_count, file_size, file_hash "
1093 "from raw_file "
1094 "where period_number = %d AND run_number = %d AND start_event = %d AND end_event = %d "
1095 "order by file_id desc",
1096 period_number, run_number, start_event, end_event);
1097 else
1098 sql =
1099 TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1100 "end_datetime, file_path, event_count, file_size, file_hash "
1101 "from raw_file "
1102 "where period_number = %d AND run_number = %d "
1103 "AND %d >= start_event AND %d <= end_event AND %d >= start_event AND %d <= end_event "
1104 "AND NOT (((%d - start_event) = 1) AND ((%d - end_event) = -1)) "
1105 "order by file_id desc",
1106 period_number, run_number, start_event, start_event, end_event, end_event,
1107 start_event, end_event);
1108
1109 TSQLStatement* stmt = db_server->Statement(sql);
1110
1111 // get raw files for the run and events from the database
1112 if (!stmt->Process()) {
1113 cout << "ERROR: getting raw files for run " << period_number << ":" << run_number << " and events "
1114 << start_event << "-" << end_event << " from the database has been failed" << endl;
1115 delete stmt;
1116 delete connDb;
1117 return nullptr;
1118 }
1119
1120 // store result of statement in buffer
1121 stmt->StoreResult();
1122
1123 TObjArray* raw_files = new TObjArray();
1124 while (stmt->NextResultRow()) {
1126 if (connRaw == nullptr) {
1127 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1128 delete stmt;
1129 delete connDb;
1130 return nullptr;
1131 }
1132
1133 int tmp_file_id;
1134 tmp_file_id = stmt->GetInt(0);
1135 int tmp_period_number;
1136 tmp_period_number = stmt->GetInt(1);
1137 int tmp_run_number;
1138 tmp_run_number = stmt->GetInt(2);
1139 int tmp_start_event;
1140 tmp_start_event = stmt->GetInt(3);
1141 int tmp_end_event;
1142 tmp_end_event = stmt->GetInt(4);
1143 TDatime tmp_start_datetime;
1144 tmp_start_datetime = stmt->GetDatime(5);
1145 TDatime tmp_end_datetime;
1146 tmp_end_datetime = stmt->GetDatime(6);
1147 TString tmp_file_path;
1148 tmp_file_path = stmt->GetString(7);
1149 int tmp_event_count;
1150 tmp_event_count = stmt->GetInt(8);
1151 int64_t tmp_file_size;
1152 tmp_file_size = stmt->GetLong64(9);
1153 TString* tmp_file_hash;
1154 if (stmt->IsNull(10))
1155 tmp_file_hash = nullptr;
1156 else
1157 tmp_file_hash = new TString(stmt->GetString(10));
1158
1159 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1160 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1161 tmp_event_count, tmp_file_size, tmp_file_hash));
1162 }
1163
1164 delete stmt;
1165 delete connDb;
1166
1167 return raw_files;
1168}
1169
1170// get array of raw files (UniRawFile*) corresponding to a part of the file path, e.g. file name
1171// ordered by file_id descending
1172// return nullptr if an error occured)
1173TObjArray* UniRawFile::GetRawFiles(TString path_part)
1174{
1176 if (connDb == nullptr)
1177 return nullptr;
1178
1179 TSQLServer* db_server = connDb->GetSQLServer();
1180
1181 TString sql =
1182 TString::Format("select file_id, period_number, run_number, start_event, end_event, start_datetime, "
1183 "end_datetime, file_path, event_count, file_size, file_hash "
1184 "from raw_file "
1185 "where file_path like '%%%s%%' "
1186 "order by file_id desc",
1187 path_part.Data());
1188 TSQLStatement* stmt = db_server->Statement(sql);
1189
1190 // get raw files for the file path part from the database
1191 if (!stmt->Process()) {
1192 cout << "ERROR: getting raw files for file path part " << path_part << " from the database has been failed" << endl;
1193 delete stmt;
1194 delete connDb;
1195 return nullptr;
1196 }
1197
1198 // store result of statement in buffer
1199 stmt->StoreResult();
1200
1201 TObjArray* raw_files = new TObjArray();
1202 while (stmt->NextResultRow()) {
1204 if (connRaw == nullptr) {
1205 cout << "ERROR: the connection to the UniConDa Database for the selected raw file info failed" << endl;
1206 delete stmt;
1207 delete connDb;
1208 return nullptr;
1209 }
1210
1211 int tmp_file_id;
1212 tmp_file_id = stmt->GetInt(0);
1213 int tmp_period_number;
1214 tmp_period_number = stmt->GetInt(1);
1215 int tmp_run_number;
1216 tmp_run_number = stmt->GetInt(2);
1217 int tmp_start_event;
1218 tmp_start_event = stmt->GetInt(3);
1219 int tmp_end_event;
1220 tmp_end_event = stmt->GetInt(4);
1221 TDatime tmp_start_datetime;
1222 tmp_start_datetime = stmt->GetDatime(5);
1223 TDatime tmp_end_datetime;
1224 tmp_end_datetime = stmt->GetDatime(6);
1225 TString tmp_file_path;
1226 tmp_file_path = stmt->GetString(7);
1227 int tmp_event_count;
1228 tmp_event_count = stmt->GetInt(8);
1229 int64_t tmp_file_size;
1230 tmp_file_size = stmt->GetLong64(9);
1231 TString* tmp_file_hash;
1232 if (stmt->IsNull(10))
1233 tmp_file_hash = nullptr;
1234 else
1235 tmp_file_hash = new TString(stmt->GetString(10));
1236
1237 raw_files->Add((TObject*)new UniRawFile(connDb, tmp_file_id, tmp_period_number, tmp_run_number, tmp_start_event,
1238 tmp_end_event, tmp_start_datetime, tmp_end_datetime, tmp_file_path,
1239 tmp_event_count, tmp_file_size, tmp_file_hash));
1240 }
1241
1242 delete stmt;
1243 delete connDb;
1244
1245 return raw_files;
1246}
1247
1251TString UniRawFile::CalculateFileHash(TString raw_file_path)
1252{
1253#ifdef HAVE_ZLIB
1254 ifstream raw_file(raw_file_path, ios::binary);
1255 if (!raw_file) {
1256 cout << "ERROR: raw file could not be opened to calculate Adler32 checksum: " << raw_file_path << endl;
1257 return "";
1258 }
1259
1260 uLong adler = adler32(0L, Z_NULL, 0); // initialize Adler32 checksum
1261
1262 const size_t bufferSize = 65536;
1263 char buffer[bufferSize];
1264 while (raw_file.read(buffer, bufferSize) || raw_file.gcount() > 0)
1265 adler = adler32(adler, (const Bytef*)buffer, raw_file.gcount());
1266
1267 raw_file.close();
1268
1269 // cout << "Adler32 for " << raw_file_path << ": 0x" << std::hex << adler << std::dec << endl;
1270 std::ostringstream hexStream;
1271 hexStream << std::hex << std::setw(8) << std::setfill('0') << (adler & 0xFFFFFFFF);
1272
1273 return hexStream.str();
1274#else
1275 TMD5* pMD5 = TMD5::FileChecksum(raw_file_path.Data());
1276 if (!pMD5) {
1277 cout << "ERROR: raw file could not be opened to calculate MD5 checksum: " << raw_file_path << endl;
1278 return "";
1279 }
1280
1281 // cout << "MD5 for " << raw_file_path << ": 0x" << pMD5->AsString() << endl;
1282 TString strMD5(pMD5->AsString());
1283
1284 delete pMD5 return strMD5;
1285#endif
1286}
static UniConnection * Open()
TSQLServer * GetSQLServer()
static int PrintAll()
print all raw files
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
int SetFileHash(TString *file_hash)
set file hash of the current raw file
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
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 SetFilePath(TString file_path)
set file path of the current raw file
static TObjArray * GetRawFiles(int period_number, int run_number)
static int DeleteRawFile(int file_id)
delete raw file from the database
static TString CalculateFileHash(TString file_path)
int SetPeriodNumber(int period_number)
set period number of the current raw file
STL namespace.