BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
UniDetectorParameter.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// UniDetectorParameter cxx file
3// ----------------------------------------------------------------------
4
5#include "TObjString.h"
6#include "TSQLServer.h"
7#include "TSQLStatement.h"
8
9// #define ONLY_DECLARATIONS
10// #include "function_set.h"
11#include "TBufferFile.h"
12#include "TBufferJSON.h"
13#include "TSystem.h"
15#include "UniRunPeriod.h"
16#include "uni_db_structures.h"
17
18#include <fstream>
19
20/* GENERATED CLASS MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */
21// ----- Constructor with database connection -----------------------
23 int value_id,
24 TString detector_name,
25 int parameter_id,
26 int start_period,
27 int start_run,
28 int end_period,
29 int end_run,
30 int value_key,
31 unsigned char* parameter_value,
32 Long_t size_parameter_value,
33 TDatime* expiry_date)
34{
35 connectionDB = db_connect;
36
37 i_value_id = value_id;
38 str_detector_name = detector_name;
39 i_parameter_id = parameter_id;
40 i_start_period = start_period;
41 i_start_run = start_run;
42 i_end_period = end_period;
43 i_end_run = end_run;
44 i_value_key = value_key;
45 blob_parameter_value = parameter_value;
46 sz_parameter_value = size_parameter_value;
47 ts_expiry_date = expiry_date;
48}
49
50// ----- Destructor -------------------------------------------------
60
61// ----- Creating new detector parameter in the database ---------------------------
63 int parameter_id,
64 int start_period,
65 int start_run,
66 int end_period,
67 int end_run,
68 int value_key,
69 unsigned char* parameter_value,
70 Long_t size_parameter_value,
71 TDatime* expiry_date)
72{
74 if (connDb == nullptr)
75 return nullptr;
76
77 TSQLServer* db_server = connDb->GetSQLServer();
78
79 TString sql = TString::Format("insert into detector_parameter(detector_name, parameter_id, start_period, "
80 "start_run, end_period, end_run, value_key, parameter_value, expiry_date) "
81 "values ($1, $2, $3, $4, $5, $6, $7, $8, $9)");
82 TSQLStatement* stmt = db_server->Statement(sql);
83
84 stmt->NextIteration();
85 stmt->SetString(0, detector_name);
86 stmt->SetInt(1, parameter_id);
87 stmt->SetInt(2, start_period);
88 stmt->SetInt(3, start_run);
89 stmt->SetInt(4, end_period);
90 stmt->SetInt(5, end_run);
91 stmt->SetInt(6, value_key);
92 stmt->SetBinary(7, parameter_value, size_parameter_value, 0x4000000);
93 if (expiry_date == nullptr)
94 stmt->SetNull(8);
95 else
96 stmt->SetDatime(8, *expiry_date);
97
98 // inserting new detector parameter to the Database
99 if (!stmt->Process()) {
100 cout << "ERROR: inserting new detector parameter to the Database has been failed" << endl;
101 delete stmt;
102 delete connDb;
103 return nullptr;
104 }
105
106 delete stmt;
107
108 // getting last inserted ID
109 int value_id;
110 TSQLStatement* stmt_last =
111 db_server->Statement("SELECT currval(pg_get_serial_sequence('detector_parameter','value_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 value_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_value_id;
134 tmp_value_id = value_id;
135 TString tmp_detector_name;
136 tmp_detector_name = detector_name;
137 int tmp_parameter_id;
138 tmp_parameter_id = parameter_id;
139 int tmp_start_period;
140 tmp_start_period = start_period;
141 int tmp_start_run;
142 tmp_start_run = start_run;
143 int tmp_end_period;
144 tmp_end_period = end_period;
145 int tmp_end_run;
146 tmp_end_run = end_run;
147 int tmp_value_key;
148 tmp_value_key = value_key;
149 unsigned char* tmp_parameter_value;
150 Long_t tmp_sz_parameter_value = size_parameter_value;
151 tmp_parameter_value = new unsigned char[tmp_sz_parameter_value];
152 memcpy(tmp_parameter_value, parameter_value, tmp_sz_parameter_value);
153 TDatime* tmp_expiry_date;
154 if (expiry_date == nullptr)
155 tmp_expiry_date = nullptr;
156 else
157 tmp_expiry_date = new TDatime(*expiry_date);
158
159 return new UniDetectorParameter(connDb, tmp_value_id, tmp_detector_name, tmp_parameter_id, tmp_start_period,
160 tmp_start_run, tmp_end_period, tmp_end_run, tmp_value_key, tmp_parameter_value,
161 tmp_sz_parameter_value, tmp_expiry_date);
162}
163
164// ----- Get detector parameter 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 value_id, detector_name, parameter_id, start_period, start_run, end_period, "
174 "end_run, value_key, parameter_value, expiry_date "
175 "from detector_parameter "
176 "where value_id = %d",
177 value_id);
178 TSQLStatement* stmt = db_server->Statement(sql);
179
180 // get detector parameter from the database
181 if (!stmt->Process()) {
182 cout << "ERROR: getting detector parameter 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: detector parameter was not found in the database" << endl;
195
196 delete stmt;
197 delete connDb;
198 return nullptr;
199 }
200
201 int tmp_value_id;
202 tmp_value_id = stmt->GetInt(0);
203 TString tmp_detector_name;
204 tmp_detector_name = stmt->GetString(1);
205 int tmp_parameter_id;
206 tmp_parameter_id = stmt->GetInt(2);
207 int tmp_start_period;
208 tmp_start_period = stmt->GetInt(3);
209 int tmp_start_run;
210 tmp_start_run = stmt->GetInt(4);
211 int tmp_end_period;
212 tmp_end_period = stmt->GetInt(5);
213 int tmp_end_run;
214 tmp_end_run = stmt->GetInt(6);
215 int tmp_value_key;
216 tmp_value_key = stmt->GetInt(7);
217 unsigned char* tmp_parameter_value;
218 tmp_parameter_value = nullptr;
219 Long_t tmp_sz_parameter_value = 0;
220 stmt->GetBinary(8, (void*&)tmp_parameter_value, tmp_sz_parameter_value);
221 TDatime* tmp_expiry_date;
222 if (stmt->IsNull(9))
223 tmp_expiry_date = nullptr;
224 else
225 tmp_expiry_date = new TDatime(stmt->GetDatime(9));
226
227 delete stmt;
228
229 return new UniDetectorParameter(connDb, tmp_value_id, tmp_detector_name, tmp_parameter_id, tmp_start_period,
230 tmp_start_run, tmp_end_period, tmp_end_run, tmp_value_key, tmp_parameter_value,
231 tmp_sz_parameter_value, tmp_expiry_date);
232}
233
234// ----- Check detector parameter exists in the database ---------------------------
236{
238 if (connDb == nullptr)
239 return -1;
240
241 TSQLServer* db_server = connDb->GetSQLServer();
242
243 TString sql = TString::Format("select 1 "
244 "from detector_parameter "
245 "where value_id = %d",
246 value_id);
247 TSQLStatement* stmt = db_server->Statement(sql);
248
249 // get detector parameter from the database
250 if (!stmt->Process()) {
251 cout << "ERROR: getting detector parameter from the database has been failed" << endl;
252
253 delete stmt;
254 delete connDb;
255 return -2;
256 }
257
258 // store result of statement in buffer
259 stmt->StoreResult();
260
261 // extract row
262 if (!stmt->NextResultRow()) {
263 delete stmt;
264 delete connDb;
265 return 0;
266 }
267
268 delete stmt;
269 delete connDb;
270
271 return 1;
272}
273
274// ----- Delete detector parameter from the database ---------------------------
276{
278 if (connDb == nullptr)
279 return -1;
280
281 TSQLServer* db_server = connDb->GetSQLServer();
282
283 TString sql = TString::Format("delete from detector_parameter "
284 "where value_id = $1");
285 TSQLStatement* stmt = db_server->Statement(sql);
286
287 stmt->NextIteration();
288 stmt->SetInt(0, value_id);
289
290 // delete detector parameter from the dataBase
291 if (!stmt->Process()) {
292 cout << "ERROR: deleting detector parameter from the dataBase has been failed" << endl;
293
294 delete stmt;
295 delete connDb;
296 return -2;
297 }
298
299 delete stmt;
300 delete connDb;
301 return 0;
302}
303
304// ----- Print all 'detector parameters' ---------------------------------
306{
308 if (connDb == nullptr)
309 return -1;
310
311 TSQLServer* db_server = connDb->GetSQLServer();
312
313 TString sql = TString::Format("select value_id, detector_name, parameter_id, start_period, start_run, end_period, "
314 "end_run, value_key, parameter_value, expiry_date "
315 "from detector_parameter");
316 TSQLStatement* stmt = db_server->Statement(sql);
317
318 // get all 'detector parameters' from the database
319 if (!stmt->Process()) {
320 cout << "ERROR: getting all 'detector parameters' from the database has been failed" << endl;
321
322 delete stmt;
323 delete connDb;
324 return -2;
325 }
326
327 // store result of statement in buffer
328 stmt->StoreResult();
329
330 // print rows
331 cout << "Table 'detector_parameter':" << endl;
332 while (stmt->NextResultRow()) {
333 cout << "value_id: ";
334 cout << (stmt->GetInt(0));
335 cout << ", detector_name: ";
336 cout << (stmt->GetString(1));
337 cout << ", parameter_id: ";
338 cout << (stmt->GetInt(2));
339 cout << ", start_period: ";
340 cout << (stmt->GetInt(3));
341 cout << ", start_run: ";
342 cout << (stmt->GetInt(4));
343 cout << ", end_period: ";
344 cout << (stmt->GetInt(5));
345 cout << ", end_run: ";
346 cout << (stmt->GetInt(6));
347 cout << ", value_key: ";
348 cout << (stmt->GetInt(7));
349 cout << ", parameter_value: ";
350 unsigned char* tmp_parameter_value = nullptr;
351 Long_t tmp_sz_parameter_value = 0;
352 stmt->GetBinary(8, (void*&)tmp_parameter_value, tmp_sz_parameter_value);
353 cout << (void*)tmp_parameter_value << ", binary size: " << tmp_sz_parameter_value;
354 cout << ", expiry_date: ";
355 if (stmt->IsNull(9))
356 cout << "nullptr";
357 else
358 cout << stmt->GetDatime(9).AsSQLString();
359 cout << "." << endl;
360 }
361
362 delete stmt;
363 delete connDb;
364
365 return 0;
366}
367
368// Setters functions
370{
371 if (!connectionDB) {
372 cout << "CRITICAL ERROR: Connection object is null" << endl;
373 return -1;
374 }
375
376 TSQLServer* db_server = connectionDB->GetSQLServer();
377
378 TString sql = TString::Format("update detector_parameter "
379 "set detector_name = $1 "
380 "where value_id = $2");
381 TSQLStatement* stmt = db_server->Statement(sql);
382
383 stmt->NextIteration();
384 stmt->SetString(0, detector_name);
385 stmt->SetInt(1, i_value_id);
386
387 // write new value to the database
388 if (!stmt->Process()) {
389 cout << "ERROR: updating information about detector parameter has been failed" << endl;
390
391 delete stmt;
392 return -2;
393 }
394
395 str_detector_name = detector_name;
396
397 delete stmt;
398 return 0;
399}
400
402{
403 if (!connectionDB) {
404 cout << "CRITICAL ERROR: Connection object is null" << endl;
405 return -1;
406 }
407
408 TSQLServer* db_server = connectionDB->GetSQLServer();
409
410 TString sql = TString::Format("update detector_parameter "
411 "set parameter_id = $1 "
412 "where value_id = $2");
413 TSQLStatement* stmt = db_server->Statement(sql);
414
415 stmt->NextIteration();
416 stmt->SetInt(0, parameter_id);
417 stmt->SetInt(1, i_value_id);
418
419 // write new value to the database
420 if (!stmt->Process()) {
421 cout << "ERROR: updating information about detector parameter has been failed" << endl;
422
423 delete stmt;
424 return -2;
425 }
426
427 i_parameter_id = parameter_id;
428
429 delete stmt;
430 return 0;
431}
432
434{
435 if (!connectionDB) {
436 cout << "CRITICAL ERROR: Connection object is null" << endl;
437 return -1;
438 }
439
440 TSQLServer* db_server = connectionDB->GetSQLServer();
441
442 TString sql = TString::Format("update detector_parameter "
443 "set start_period = $1 "
444 "where value_id = $2");
445 TSQLStatement* stmt = db_server->Statement(sql);
446
447 stmt->NextIteration();
448 stmt->SetInt(0, start_period);
449 stmt->SetInt(1, i_value_id);
450
451 // write new value to the database
452 if (!stmt->Process()) {
453 cout << "ERROR: updating information about detector parameter has been failed" << endl;
454
455 delete stmt;
456 return -2;
457 }
458
459 i_start_period = start_period;
460
461 delete stmt;
462 return 0;
463}
464
466{
467 if (!connectionDB) {
468 cout << "CRITICAL ERROR: Connection object is null" << endl;
469 return -1;
470 }
471
472 TSQLServer* db_server = connectionDB->GetSQLServer();
473
474 TString sql = TString::Format("update detector_parameter "
475 "set start_run = $1 "
476 "where value_id = $2");
477 TSQLStatement* stmt = db_server->Statement(sql);
478
479 stmt->NextIteration();
480 stmt->SetInt(0, start_run);
481 stmt->SetInt(1, i_value_id);
482
483 // write new value to the database
484 if (!stmt->Process()) {
485 cout << "ERROR: updating information about detector parameter has been failed" << endl;
486
487 delete stmt;
488 return -2;
489 }
490
491 i_start_run = start_run;
492
493 delete stmt;
494 return 0;
495}
496
498{
499 if (!connectionDB) {
500 cout << "CRITICAL ERROR: Connection object is null" << endl;
501 return -1;
502 }
503
504 TSQLServer* db_server = connectionDB->GetSQLServer();
505
506 TString sql = TString::Format("update detector_parameter "
507 "set end_period = $1 "
508 "where value_id = $2");
509 TSQLStatement* stmt = db_server->Statement(sql);
510
511 stmt->NextIteration();
512 stmt->SetInt(0, end_period);
513 stmt->SetInt(1, i_value_id);
514
515 // write new value to the database
516 if (!stmt->Process()) {
517 cout << "ERROR: updating information about detector parameter has been failed" << endl;
518
519 delete stmt;
520 return -2;
521 }
522
523 i_end_period = end_period;
524
525 delete stmt;
526 return 0;
527}
528
530{
531 if (!connectionDB) {
532 cout << "CRITICAL ERROR: Connection object is null" << endl;
533 return -1;
534 }
535
536 TSQLServer* db_server = connectionDB->GetSQLServer();
537
538 TString sql = TString::Format("update detector_parameter "
539 "set end_run = $1 "
540 "where value_id = $2");
541 TSQLStatement* stmt = db_server->Statement(sql);
542
543 stmt->NextIteration();
544 stmt->SetInt(0, end_run);
545 stmt->SetInt(1, i_value_id);
546
547 // write new value to the database
548 if (!stmt->Process()) {
549 cout << "ERROR: updating information about detector parameter has been failed" << endl;
550
551 delete stmt;
552 return -2;
553 }
554
555 i_end_run = end_run;
556
557 delete stmt;
558 return 0;
559}
560
562{
563 if (!connectionDB) {
564 cout << "CRITICAL ERROR: Connection object is null" << endl;
565 return -1;
566 }
567
568 TSQLServer* db_server = connectionDB->GetSQLServer();
569
570 TString sql = TString::Format("update detector_parameter "
571 "set value_key = $1 "
572 "where value_id = $2");
573 TSQLStatement* stmt = db_server->Statement(sql);
574
575 stmt->NextIteration();
576 stmt->SetInt(0, value_key);
577 stmt->SetInt(1, i_value_id);
578
579 // write new value to the database
580 if (!stmt->Process()) {
581 cout << "ERROR: updating information about detector parameter has been failed" << endl;
582
583 delete stmt;
584 return -2;
585 }
586
587 i_value_key = value_key;
588
589 delete stmt;
590 return 0;
591}
592
593int UniDetectorParameter::SetParameterValue(unsigned char* parameter_value, Long_t size_parameter_value)
594{
595 if (!connectionDB) {
596 cout << "CRITICAL ERROR: Connection object is null" << endl;
597 return -1;
598 }
599
600 TSQLServer* db_server = connectionDB->GetSQLServer();
601
602 TString sql = TString::Format("update detector_parameter "
603 "set parameter_value = $1 "
604 "where value_id = $2");
605 TSQLStatement* stmt = db_server->Statement(sql);
606
607 stmt->NextIteration();
608 stmt->SetBinary(0, parameter_value, size_parameter_value, 0x4000000);
609 stmt->SetInt(1, i_value_id);
610
611 // write new value to the database
612 if (!stmt->Process()) {
613 cout << "ERROR: updating information about detector parameter has been failed" << endl;
614
615 delete stmt;
616 return -2;
617 }
618
620 delete[] blob_parameter_value;
621 sz_parameter_value = size_parameter_value;
622 blob_parameter_value = new unsigned char[sz_parameter_value];
623 memcpy(blob_parameter_value, parameter_value, sz_parameter_value);
624
625 delete stmt;
626 return 0;
627}
628
630{
631 if (!connectionDB) {
632 cout << "CRITICAL ERROR: Connection object is null" << endl;
633 return -1;
634 }
635
636 TSQLServer* db_server = connectionDB->GetSQLServer();
637
638 TString sql = TString::Format("update detector_parameter "
639 "set expiry_date = $1 "
640 "where value_id = $2");
641 TSQLStatement* stmt = db_server->Statement(sql);
642
643 stmt->NextIteration();
644 if (expiry_date == nullptr)
645 stmt->SetNull(0);
646 else
647 stmt->SetDatime(0, *expiry_date);
648 stmt->SetInt(1, i_value_id);
649
650 // write new value to the database
651 if (!stmt->Process()) {
652 cout << "ERROR: updating information about detector parameter has been failed" << endl;
653
654 delete stmt;
655 return -2;
656 }
657
658 if (ts_expiry_date)
659 delete ts_expiry_date;
660 if (expiry_date == nullptr)
661 ts_expiry_date = nullptr;
662 else
663 ts_expiry_date = new TDatime(*expiry_date);
664
665 delete stmt;
666 return 0;
667}
668
669// ----- Print current detector parameter ---------------------------------------
671{
672 cout << "Table 'detector_parameter'";
673 cout << ". value_id: " << i_value_id << ". detector_name: " << str_detector_name
674 << ". parameter_id: " << i_parameter_id << ". start_period: " << i_start_period
675 << ". start_run: " << i_start_run << ". end_period: " << i_end_period << ". end_run: " << i_end_run
676 << ". value_key: " << i_value_key << ". parameter_value: " << (void*)blob_parameter_value
677 << ", binary size: " << sz_parameter_value
678 << ". expiry_date: " << (ts_expiry_date == nullptr ? "nullptr" : (*ts_expiry_date).AsSQLString()) << endl;
679
680 return;
681}
682/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
683
684// non-user function for getting parameter value as a binary (char) array
685unsigned char* UniDetectorParameter::GetUNC(enumValueType enum_parameter_type)
686{
687 if (enum_parameter_type > -1) {
688 if (!connectionDB) {
689 cout << "CRITICAL ERROR: Connection object is null" << endl;
690 return nullptr;
691 }
692
693 TSQLServer* db_server = connectionDB->GetSQLServer();
694
695 // get parameter object from 'parameter_' table
696 TString sql = TString::Format("select parameter_name, parameter_type "
697 "from parameter_ "
698 "where parameter_id = %d",
700 TSQLStatement* stmt = db_server->Statement(sql);
701
702 // get table record from DB
703 if (!stmt->Process()) {
704 cout << "CRITICAL ERROR: getting record with parameter from 'parameter_' table has been failed" << endl;
705 delete stmt;
706 return nullptr;
707 }
708
709 stmt->StoreResult();
710
711 // extract row with parameter
712 if (!stmt->NextResultRow()) {
713 cout << "CRITICAL ERROR: the parameter with id '" << i_parameter_id << "' was not found" << endl;
714 delete stmt;
715 return nullptr;
716 }
717
718 TString parameter_name = stmt->GetString(0);
719 int parameter_type = stmt->GetInt(1);
720 delete stmt;
721
722 if (parameter_type != enum_parameter_type) {
723 cout << "CRITICAL ERROR: the parameter with name '" << parameter_name
724 << "' is not corresponding the given type: "
725 "Database Type - "
726 << parameter_type << ", but user type - " << enum_parameter_type << endl;
727 return nullptr;
728 }
729 }
730
732}
733
734// non-user function for setting parameter value as a binary (char) array
735int UniDetectorParameter::SetUNC(unsigned char* p_parameter_value, Long_t size_parameter_value)
736{
737 if (!connectionDB) {
738 cout << "Connection object is null" << endl;
739 return -1;
740 }
741
742 TSQLServer* db_server = connectionDB->GetSQLServer();
743
744 TString sql = TString::Format("update detector_parameter "
745 "set parameter_value = $1 "
746 "where detector_name = $2 and parameter_id = $3 and start_period = $4 and start_run "
747 "= $5 and end_period = $6 and end_run = $7 and value_key = $8");
748
749 TSQLStatement* stmt = db_server->Statement(sql);
750
751 stmt->NextIteration();
752 stmt->SetBinary(0, (void*)p_parameter_value, size_parameter_value);
754 stmt->SetInt(2, i_parameter_id);
755 stmt->SetInt(3, i_start_period);
756 stmt->SetInt(4, i_start_run);
757 stmt->SetInt(5, i_end_period);
758 stmt->SetInt(6, i_end_run);
759 stmt->SetInt(7, i_value_key);
760
761 // write new value to database
762 if (!stmt->Process()) {
763 cout << "ERROR: updating the detector parameter has been failed" << endl;
764 delete stmt;
765 return -2;
766 }
767
769 delete[] blob_parameter_value;
770 blob_parameter_value = p_parameter_value;
771 sz_parameter_value = size_parameter_value;
772
773 delete stmt;
774 return 0;
775}
776
777// non-user function for writing new parameter value (integer value key is optional, default, 0)
779 TString parameter_name,
780 int start_period,
781 int start_run,
782 int end_period,
783 int end_run,
784 unsigned char* p_parameter_value,
785 Long_t size_parameter_value,
786 enumValueType enum_parameter_type,
787 int value_key)
788{
789 if (((end_period < start_period) or ((end_period == start_period) and (end_run < start_run)))
790 or ((start_period > end_period) or ((start_period == end_period) and (start_run > end_run))))
791 {
792 cout << "ERROR: end run should be after or the same as start run" << endl;
793 return nullptr;
794 }
795
797 if (connDb == nullptr)
798 return nullptr;
799
800 TSQLServer* db_server = connDb->GetSQLServer();
801
802 int parameter_id = -1;
803 bool res_code = UniParameter::CheckAndGetParameterID(db_server, parameter_name, enum_parameter_type, parameter_id);
804 if (!res_code) {
805 delete connDb;
806 return nullptr;
807 }
808
809 TString sql = TString::Format("insert into detector_parameter(detector_name, parameter_id, start_period, "
810 "start_run, end_period, end_run, value_key, parameter_value) "
811 "values ($1, $2, $3, $4, $5, $6, $7, $8)");
812 TSQLStatement* stmt = db_server->Statement(sql);
813
814 // cout<<"DEBUG: detector_name = "<<detector_name<<". parameter_id = "<<parameter_id<<". start_period =
815 // "<<start_period<<". start_run = "<<start_run<<". end_period = "<<end_period<<". end_run = "<<end_run<<".
816 // value_key = "<<value_key<<". p_parameter_value[0] = "<<p_parameter_value[0]<<". size_parameter_value =
817 // "<<size_parameter_value<<endl;
818 stmt->NextIteration();
819 stmt->SetString(0, detector_name);
820 stmt->SetInt(1, parameter_id);
821 stmt->SetInt(2, start_period);
822 stmt->SetInt(3, start_run);
823 stmt->SetInt(4, end_period);
824 stmt->SetInt(5, end_run);
825 stmt->SetInt(6, value_key);
826 stmt->SetBinary(7, (void*)p_parameter_value, size_parameter_value);
827
828 // inserting new record to DB
829 if (!stmt->Process()) {
830 cout << "ERROR: inserting new parameter value to the database has been failed" << endl;
831 delete stmt;
832 delete connDb;
833 return nullptr;
834 }
835
836 delete stmt;
837
838 // getting last inserted node ID
839 int last_id = -1;
840 // TSQLStatement* stmt_last = db_server->Statement("SELECT LAST_INSERT_ID()"); //MySQL
841 TSQLStatement* stmt_last =
842 db_server->Statement("SELECT currval(pg_get_serial_sequence('detector_parameter','value_id'))");
843
844 if (stmt_last->Process()) {
845 // store result of statement in buffer
846 stmt_last->StoreResult();
847
848 // if there is no last id then exit with error
849 if (!stmt_last->NextResultRow()) {
850 cout << "ERROR: no last ID in the Database for the parameter value has been found!" << endl;
851 delete stmt_last;
852 return nullptr;
853 } else {
854 last_id = stmt_last->GetInt(0);
855 delete stmt_last;
856 }
857 } else {
858 cout << "ERROR: getting last ID for new parameter value has been failed!" << endl;
859 delete stmt_last;
860 return nullptr;
861 }
862
863 // unsigned char* tmp_parameter_value = new unsigned char[size_parameter_value];
864 // memcpy(tmp_parameter_value, p_parameter_value, size_parameter_value);
865 return new UniDetectorParameter(connDb, last_id, detector_name, parameter_id, start_period, start_run, end_period,
866 end_run, value_key, p_parameter_value, size_parameter_value, nullptr);
867}
868
869// write detector parameter value presented by an array (integer value key is optional, default, 0)
871 TString parameter_name,
872 int start_period,
873 int start_run,
874 int end_period,
875 int end_run,
876 vector<UniValue*> parameter_value,
877 int value_key)
878{
879 if (parameter_value.size() < 1) {
880 cout << "ERROR: The length of the parameter array should be greater zero" << endl;
881 return nullptr;
882 }
883 enumValueType parameter_type = parameter_value[0]->GetType();
884
885 // calculate total size
886 size_t size_parameter_value = 0;
887 for (size_t i = 0; i < parameter_value.size(); i++) {
888 if (parameter_value[i]->GetType() != parameter_type) {
889 cout << "ERROR: the type of the parameters in the array must be the same" << endl;
890 return nullptr;
891 }
892 size_parameter_value += parameter_value[i]->GetStorageSize();
893 }
894
895 // write parameters of the vector to the buffer
896 unsigned char* p_parameter_value = new unsigned char[size_parameter_value];
897 unsigned char* tmp_pointer = p_parameter_value;
898 for (size_t i = 0; i < parameter_value.size(); i++) {
899 parameter_value[i]->WriteValue(tmp_pointer);
900 tmp_pointer += parameter_value[i]->GetStorageSize();
901 }
902
903 if (start_run < 1) {
904 int first_run = UniRunPeriod::GetFirstRunNumber(start_period);
905 if (first_run >= 0)
906 start_run = first_run;
907 }
908 if (end_period < 1)
909 end_period = start_period;
910 if (end_run < 1) {
911 int last_run = UniRunPeriod::GetLastRunNumber(end_period);
912 if (last_run >= 0)
913 end_run = last_run;
914 }
915
917 detector_name, parameter_name, start_period, start_run, end_period, end_run, p_parameter_value,
918 size_parameter_value, parameter_type, value_key);
919 if (pDetectorParameter == nullptr)
920 delete[] p_parameter_value;
921
922 return pDetectorParameter;
923}
924
925// write detector parameter value presented by a single value (integer value key is optional, default, 0)
927 TString parameter_name,
928 int start_period,
929 int start_run,
930 int end_period,
931 int end_run,
932 UniValue* parameter_value,
933 int value_key)
934{
935 if (parameter_value == nullptr) {
936 cout << "ERROR: the parameter value must not be nullptr" << endl;
937 return nullptr;
938 }
939 enumValueType parameter_type = parameter_value->GetType();
940
941 // write the parameter to the buffer
942 size_t size_parameter_value = parameter_value->GetStorageSize();
943 unsigned char* p_parameter_value = new unsigned char[size_parameter_value];
944 parameter_value->WriteValue(p_parameter_value);
945
946 if (start_run < 1) {
947 int first_run = UniRunPeriod::GetFirstRunNumber(start_period);
948 if (first_run >= 0)
949 start_run = first_run;
950 }
951 if (end_period < 1)
952 end_period = start_period;
953 if (end_run < 1) {
954 int last_run = UniRunPeriod::GetLastRunNumber(end_period);
955 if (last_run >= 0)
956 end_run = last_run;
957 }
958
960 detector_name, parameter_name, start_period, start_run, end_period, end_run, p_parameter_value,
961 size_parameter_value, parameter_type, value_key);
962 if (pDetectorParameter == nullptr)
963 delete[] p_parameter_value;
964
965 return pDetectorParameter;
966}
967
968// get detector parameter value (integer value key is optional, default, 0)
970 TString parameter_name,
971 int period_number,
972 int run_number,
973 TDatime* usage_time,
974 int value_key)
975{
977 if (connDb == nullptr)
978 return nullptr;
979
980 TSQLServer* db_server = connDb->GetSQLServer();
981
982 TString sql = TString::Format("select value_id, detector_name, p.parameter_id, start_period, start_run, "
983 "end_period, end_run, parameter_value, expiry_date "
984 "from detector_parameter dp join parameter_ p on dp.parameter_id = p.parameter_id "
985 "where lower(detector_name) = lower('%s') and lower(parameter_name) = lower('%s') "
986 "and (not (((%d < start_period) or ((%d = start_period) and (%d < start_run))) or "
987 "((%d > end_period) or ((%d = end_period) and (%d > end_run))))) "
988 "and value_key = %d",
989 detector_name.Data(), parameter_name.Data(), period_number, period_number, run_number,
990 period_number, period_number, run_number, value_key);
991 if (usage_time != nullptr)
992 sql += TString::Format(" and ((expiry_date is null) or ('%s' < expiry_date)) order by expiry_date",
993 (*usage_time).AsSQLString());
994 else
995 sql += " and expiry_date is null";
996 TSQLStatement* stmt = db_server->Statement(sql);
997
998 // get table record from DB
999 if (!stmt->Process()) {
1000 cout << "ERROR: getting record from the Database has been failed" << endl;
1001 delete stmt;
1002 delete connDb;
1003 return nullptr;
1004 }
1005
1006 // store result of statement in buffer
1007 stmt->StoreResult();
1008
1009 // extract row
1010 if (!stmt->NextResultRow()) {
1011 cout << "ERROR: parameter value was not found in the Condition Database" << endl;
1012 delete stmt;
1013 delete connDb;
1014 return nullptr;
1015 }
1016
1017 int tmp_value_id;
1018 tmp_value_id = stmt->GetInt(0);
1019 TString tmp_detector_name;
1020 tmp_detector_name = stmt->GetString(1);
1021 int tmp_parameter_id;
1022 tmp_parameter_id = stmt->GetInt(2);
1023 int tmp_start_period;
1024 tmp_start_period = stmt->GetInt(3);
1025 int tmp_start_run;
1026 tmp_start_run = stmt->GetInt(4);
1027 int tmp_end_period;
1028 tmp_end_period = stmt->GetInt(5);
1029 int tmp_end_run;
1030 tmp_end_run = stmt->GetInt(6);
1031 unsigned char* tmp_parameter_value = nullptr;
1032 Long_t tmp_sz_parameter_value = 0;
1033 stmt->GetBinary(7, (void*&)tmp_parameter_value, tmp_sz_parameter_value);
1034 TDatime* tmp_expiry_date;
1035 if (stmt->IsNull(8))
1036 tmp_expiry_date = nullptr;
1037 else
1038 tmp_expiry_date = new TDatime(stmt->GetDatime(8));
1039
1040 delete stmt;
1041
1042 return new UniDetectorParameter(connDb, tmp_value_id, tmp_detector_name, tmp_parameter_id, tmp_start_period,
1043 tmp_start_run, tmp_end_period, tmp_end_run, value_key, tmp_parameter_value,
1044 tmp_sz_parameter_value, tmp_expiry_date);
1045}
1046
1047// delete detector parameter value (integer value key is optional, default, 0)
1049 TString parameter_name,
1050 int start_period,
1051 int start_run,
1052 int end_period,
1053 int end_run,
1054 TDatime* usage_time,
1055 int value_key)
1056{
1058 if (connDb == nullptr)
1059 return -1;
1060
1061 TSQLServer* db_server = connDb->GetSQLServer();
1062
1063 TString sql = "delete from detector_parameter "
1064 "where lower(detector_name) = lower($1) and parameter_id IN (select parameter_id from parameter_ "
1065 "where lower(parameter_name) = lower($2)) and "
1066 "start_period = $3 and start_run = $4 and end_period = $5 and end_run = $6 and value_key = $7";
1067 if (usage_time == nullptr)
1068 sql += " and expiry_date is null";
1069 else
1070 sql += " and expiry_date >= $8 and expiry_date < $9";
1071 TSQLStatement* stmt = db_server->Statement(sql);
1072
1073 stmt->NextIteration();
1074 stmt->SetString(0, detector_name);
1075 stmt->SetString(1, parameter_name);
1076 stmt->SetInt(2, start_period);
1077 stmt->SetInt(3, start_run);
1078 stmt->SetInt(4, end_period);
1079 stmt->SetInt(5, end_run);
1080 stmt->SetInt(6, value_key);
1081 if (usage_time != nullptr) {
1082 stmt->SetDatime(7, *usage_time);
1083 TDatime dt_plus_1(usage_time->Convert() + 1);
1084 stmt->SetDatime(8, dt_plus_1);
1085 }
1086
1087 // delete table record from DB
1088 if (!stmt->Process()) {
1089 cout << "ERROR: deleting parameter value from the Database has been failed" << endl;
1090
1091 delete stmt;
1092 delete connDb;
1093 return -2;
1094 }
1095
1096 if (stmt->GetNumAffectedRows() == 0) {
1097 cout << "WARNING: parameter value was not found in the Condition Database for deletion" << endl;
1098 delete stmt;
1099 delete connDb;
1100 return -2;
1101 }
1102
1103 delete stmt;
1104 delete connDb;
1105 return 0;
1106}
1107
1108// get value of detector parameter presented by an array
1109int UniDetectorParameter::GetValue(vector<UniValue*>& parameter_value)
1110{
1111 unsigned char* p_parameter_value = GetUNC();
1112 if (p_parameter_value == nullptr)
1113 return -1;
1114
1115 Long_t full_size = sz_parameter_value;
1116 while (full_size > 0) {
1117 // cout<<"full_size = "<<full_size<<". GetParameterType() = "<<GetParameterType()<<endl;
1118 UniValue* pParameterValue = CreateParameterValue(GetParameterType());
1119 pParameterValue->ReadValue(p_parameter_value);
1120 full_size -= pParameterValue->GetStorageSize();
1121 p_parameter_value += pParameterValue->GetStorageSize();
1122 parameter_value.push_back(pParameterValue);
1123 }
1124
1125 return 0;
1126}
1127
1128// get value of detector parameter presented by a single value
1130{
1131 unsigned char* p_parameter_value = GetUNC();
1132 if (p_parameter_value == nullptr)
1133 return nullptr;
1134
1135 UniValue* parameter_value = CreateParameterValue(GetParameterType());
1136 parameter_value->ReadValue(p_parameter_value);
1137
1138 return parameter_value;
1139}
1140
1141// set value to detector parameter presented by an array
1142int UniDetectorParameter::SetValue(vector<UniValue*> parameter_value)
1143{
1144 if (parameter_value.size() < 1) {
1145 cout << "ERROR: The length of the parameter array should be greater zero" << endl;
1146 return -1;
1147 }
1148
1149 // calculate full size to create a 'char' buffer
1150 size_t size_parameter_value = 0;
1151 for (size_t i = 0; i < parameter_value.size(); i++)
1152 size_parameter_value += parameter_value[i]->GetStorageSize();
1153 unsigned char* p_parameter_value = new unsigned char[size_parameter_value];
1154
1155 // write value by value to the 'char' buffer
1156 unsigned char* tmp_pointer = p_parameter_value;
1157 for (size_t i = 0; i < parameter_value.size(); i++) {
1158 parameter_value[i]->WriteValue(tmp_pointer);
1159 tmp_pointer += parameter_value[i]->GetStorageSize();
1160 }
1161
1162 int res_code = SetUNC(p_parameter_value, size_parameter_value);
1163 if (res_code != 0) {
1164 delete[] p_parameter_value;
1165 return res_code;
1166 }
1167
1168 return 0;
1169}
1170
1171// set value to detector parameter presented by a single value
1173{
1174 if (parameter_value == nullptr) {
1175 cout << "ERROR: the parameter value must not be nullptr" << endl;
1176 return -1;
1177 }
1178
1179 // write value to the 'char' buffer
1180 size_t size_parameter_value = parameter_value->GetStorageSize();
1181 unsigned char* p_parameter_value = new unsigned char[size_parameter_value];
1182 parameter_value->WriteValue(p_parameter_value);
1183
1184 int res_code = SetUNC(p_parameter_value, size_parameter_value);
1185 if (res_code != 0) {
1186 delete[] p_parameter_value;
1187 return res_code;
1188 }
1189
1190 return 0;
1191}
1192
1193// write ROOT object as a binary parameter to the Database from start run to end one
1194int UniDetectorParameter::WriteObjectBinary(const char* detector_name,
1195 const char* parameter_name,
1196 int start_period,
1197 int start_run,
1198 int end_period,
1199 int end_run,
1200 TObject* ptr_object)
1201{
1202 if (!ptr_object) {
1203 cout << "ERROR: object pointer is nullptr" << endl;
1204 return -1;
1205 }
1206
1207 // Create buffer to write ROOT object
1209
1210 // Serialize ROOT object to the buffer
1211 buf.WriteObject(ptr_object);
1212
1213 if (!buf.Buffer()) {
1214 cout << "ERROR: buffer for writing the given object is nullptr" << endl;
1215 return -2;
1216 }
1217
1218 // create binary parameter with the buffer bytes
1219 BinaryValue* pValue = new BinaryValue;
1220 pValue->size = buf.Length();
1221 pValue->value = (unsigned char*)buf.Buffer();
1223 detector_name, parameter_name, start_period, start_run, end_period, end_run, pValue);
1224 if (pParameterValue == nullptr) {
1225 delete pValue;
1226 return -3;
1227 }
1228
1229 delete pValue;
1230 delete pParameterValue;
1231
1232 return 0;
1233}
1234
1235// read ROOT object as a binary parameter from from the Database for the selected run
1236int UniDetectorParameter::ReadObjectBinary(const char* detector_name,
1237 const char* parameter_name,
1238 int period_number,
1239 int run_number,
1240 TObject*& ptr_object)
1241{
1242 UniDetectorParameter* pDetectorParameter =
1243 UniDetectorParameter::GetDetectorParameter(detector_name, parameter_name, period_number, run_number);
1244 if (!pDetectorParameter)
1245 return -1;
1246 BinaryValue* pValue = (BinaryValue*)pDetectorParameter->GetValue();
1247 if (!pValue)
1248 return -2;
1249
1250 // Create buffer to read ROOT object
1251 TBufferFile buf(TBuffer::kRead, pValue->size, pValue->value);
1252
1253 // Deserialize the object
1254 ptr_object = buf.ReadObject(TObject::Class());
1255
1256 delete pValue;
1257
1258 return 0;
1259}
1260
1261// write ROOT object as a JSON parameter to the Database from start run to end one
1262int UniDetectorParameter::WriteObjectJSON(const char* detector_name,
1263 const char* parameter_name,
1264 int start_period,
1265 int start_run,
1266 int end_period,
1267 int end_run,
1268 TObject* ptr_object)
1269{
1270 if (!ptr_object) {
1271 cout << "ERROR: object pointer is nullptr" << endl;
1272 return -1;
1273 }
1274
1275 // Create JSON string to write the ROOT object
1276 TString json_str = TBufferJSON::ConvertToJSON(ptr_object);
1277
1278 // create JSON parameter with the object
1279 JsonValue json_value;
1280 json_value.value = json::parse(json_str.Data());
1282 detector_name, parameter_name, start_period, start_run, end_period, end_run, &json_value);
1283 if (pParameterValue == nullptr) {
1284 return -2;
1285 }
1286
1287 // clean memory after work
1288 delete pParameterValue;
1289
1290 return 0;
1291}
1292
1293// read ROOT object as a JSON parameter from from the Database for the selected run
1294int UniDetectorParameter::ReadObjectJSON(const char* detector_name,
1295 const char* parameter_name,
1296 int period_number,
1297 int run_number,
1298 TObject*& ptr_object)
1299{
1300 // get parameter value (JSON value)
1301 UniDetectorParameter* pDetectorParameter =
1302 UniDetectorParameter::GetDetectorParameter(detector_name, parameter_name, period_number, run_number);
1303 if (!pDetectorParameter)
1304 return -1;
1305 JsonValue* pValue = (JsonValue*)pDetectorParameter->GetValue();
1306 if (!pValue)
1307 return -2;
1308
1309 // Read ROOT object from the JSON value
1310 ptr_object = TBufferJSON::ConvertFromJSON(pValue->value.dump().c_str());
1311
1312 // clean memory after work
1313 delete pValue;
1314
1315 return 0;
1316}
1317
1318// write a whole file with parameter value (with given detector and parameter names)
1319// to the Database from start run to end one
1320int UniDetectorParameter::WriteFile(const char* detector_name,
1321 const char* parameter_name,
1322 int start_period,
1323 int start_run,
1324 int end_period,
1325 int end_run,
1326 const char* file_path)
1327{
1328 TString strFilePath(file_path);
1329 gSystem->ExpandPathName(strFilePath);
1330
1331 FILE* cur_file = fopen(strFilePath.Data(), "rb");
1332 if (cur_file == nullptr) {
1333 cout << "ERROR: opening file: " << strFilePath << " was failed" << endl;
1334 return -1;
1335 }
1336
1337 fseek(cur_file, 0, SEEK_END);
1338 size_t file_size = ftell(cur_file);
1339 rewind(cur_file);
1340 if (file_size <= 0) {
1341 cout << "ERROR: getting file size: " << strFilePath << " was failed" << endl;
1342 fclose(cur_file);
1343 return -2;
1344 }
1345
1346 unsigned char* buffer = new unsigned char[file_size];
1347 if (buffer == nullptr) {
1348 cout << "ERROR: getting memory from heap for file was failed" << endl;
1349 fclose(cur_file);
1350 return -3;
1351 }
1352
1353 size_t bytes_read = fread(buffer, 1, file_size, cur_file);
1354 if (bytes_read != file_size) {
1355 cout << "ERROR: reading file: " << strFilePath << ", got " << bytes_read << " bytes of " << file_size << endl;
1356 delete[] buffer;
1357 fclose(cur_file);
1358 return -4;
1359 }
1360
1361 fclose(cur_file);
1362
1363 // create binary parameter with file's bytes
1364 BinaryValue* pValue = new BinaryValue;
1365 pValue->size = file_size;
1366 pValue->value = buffer;
1368 detector_name, parameter_name, start_period, start_run, end_period, end_run, pValue);
1369 if (pParameterValue == nullptr) {
1370 delete[] buffer;
1371 return -5;
1372 }
1373 delete pValue;
1374
1375 delete[] buffer;
1376 delete pParameterValue;
1377
1378 return 0;
1379}
1380
1381// read a whole file with parameter value (with given detector and parameter names)
1382// from the Database for the selected run
1383int UniDetectorParameter::ReadFile(const char* detector_name,
1384 const char* parameter_name,
1385 int period_number,
1386 int run_number,
1387 const char* file_path)
1388{
1389 UniDetectorParameter* pDetectorParameter =
1390 UniDetectorParameter::GetDetectorParameter(detector_name, parameter_name, period_number, run_number);
1391 if (pDetectorParameter == nullptr)
1392 return -1;
1393 BinaryValue* pValue = (BinaryValue*)pDetectorParameter->GetValue();
1394
1395 FILE* cur_file = fopen(file_path, "wb");
1396 if (cur_file == nullptr) {
1397 cout << "ERROR: creating file: " << file_path << " has been failed!" << endl;
1398 delete pValue;
1399 delete pDetectorParameter;
1400 return -2;
1401 }
1402
1403 unsigned char* buffer = pValue->value;
1404 if (buffer == nullptr) {
1405 cout << "ERROR: getting binary parameter array" << endl;
1406 delete pValue;
1407 delete pDetectorParameter;
1408 fclose(cur_file);
1409 return -3;
1410 }
1411 size_t parameter_size = pValue->size;
1412
1413 size_t bytes_write = fwrite(buffer, 1, parameter_size, cur_file);
1414 delete pDetectorParameter;
1415 if (bytes_write != parameter_size) {
1416 cout << "ERROR: writing file: " << file_path << ", put " << bytes_write << " bytes of " << parameter_size
1417 << endl;
1418 fclose(cur_file);
1419 delete pValue;
1420 return -4;
1421 }
1422
1423 fclose(cur_file);
1424 delete[] buffer;
1425 delete pValue;
1426
1427 return 0;
1428}
1429
1431{
1432 TObjArray* arrayResult = nullptr;
1433 search_conditions.SetOwner(kTRUE);
1434
1436 if (connDb == nullptr) {
1437 cout << "ERROR: the connection to the Unified Condition Database was failed" << endl;
1438 return arrayResult;
1439 }
1440
1441 TSQLServer* db_server = connDb->GetSQLServer();
1442
1443 TString sql = TString::Format("select value_id, detector_name, p.parameter_id, start_period, start_run, "
1444 "end_period, end_run, value_key, parameter_value, expiry_date "
1445 "from detector_parameter dp join parameter_ p on dp.parameter_id = p.parameter_id");
1446
1447 TString strCondition;
1448 bool isFirst = true;
1449 TIter next(&search_conditions);
1450 UniSearchCondition* curCondition;
1451 while ((curCondition = (UniSearchCondition*)next()) != nullptr) {
1452 strCondition = "";
1453
1454 switch (curCondition->GetColumn()) {
1456 strCondition += "lower(detector_name) ";
1457 break;
1459 strCondition += "lower(parameter_name) ";
1460 break;
1462 strCondition += "start_period ";
1463 break;
1465 strCondition += "start_run ";
1466 break;
1468 strCondition += "end_period ";
1469 break;
1471 strCondition += "end_run ";
1472 break;
1473 default:
1474 cout << "ERROR: the column in the search condition was not defined, the condition is skipped" << endl;
1475 continue;
1476 }
1477
1478 switch (curCondition->GetCondition()) {
1480 strCondition += "< ";
1481 break;
1483 strCondition += "<= ";
1484 break;
1486 strCondition += "= ";
1487 break;
1489 strCondition += "<> ";
1490 break;
1492 strCondition += "> ";
1493 break;
1495 strCondition += ">= ";
1496 break;
1498 strCondition += "like ";
1499 break;
1501 strCondition += "is null ";
1502 break;
1504 strCondition += "is not null ";
1505 break;
1506 default:
1507 cout << "ERROR: the comparison operator in the search condition was not defined, the condition is "
1508 "skipped"
1509 << endl;
1510 continue;
1511 }
1512
1513 switch (curCondition->GetValueType()) {
1514 case 0:
1515 if ((curCondition->GetCondition() != UniSearchCondition::conditionNull)
1516 && (curCondition->GetCondition() != UniSearchCondition::conditionNotNull))
1517 continue;
1518 break;
1519 case 1:
1520 strCondition += Form("%d", curCondition->GetIntValue());
1521 break;
1522 case 2:
1523 strCondition += Form("%u", curCondition->GetUIntValue());
1524 break;
1525 case 3:
1526 strCondition += Form("%f", curCondition->GetDoubleValue());
1527 break;
1528 case 4:
1529 strCondition += Form("lower('%s')", curCondition->GetStringValue().Data());
1530 break;
1531 case 5:
1532 strCondition += Form("'%s'", curCondition->GetDatimeValue().AsSQLString());
1533 break;
1534 default:
1535 cout << "ERROR: the value type in the search condition was not found, the condition is skipped" << endl;
1536 continue;
1537 }
1538
1539 if (isFirst) {
1540 sql += " where ";
1541 isFirst = false;
1542 } else
1543 sql += " and ";
1544
1545 sql += strCondition;
1546 }
1547
1548 TSQLStatement* stmt = db_server->Statement(sql);
1549
1550 // get table record from DB
1551 if (!stmt->Process()) {
1552 cout << "ERROR: getting runs from the Unified Condition Database has been failed" << endl;
1553 delete stmt;
1554 delete connDb;
1555
1556 return arrayResult;
1557 }
1558
1559 // store result of statement in buffer
1560 stmt->StoreResult();
1561
1562 // extract rows one after another
1563 arrayResult = new TObjArray();
1564 arrayResult->SetOwner(kTRUE);
1565 while (stmt->NextResultRow()) {
1567 if (connPar == nullptr) {
1568 cout << "ERROR: the connection to the Unified Condition Database for the selected run was failed" << endl;
1569 delete stmt;
1570 delete connDb;
1571 return arrayResult;
1572 }
1573
1574 int tmp_value_id;
1575 tmp_value_id = stmt->GetInt(0);
1576 TString tmp_detector_name;
1577 tmp_detector_name = stmt->GetString(1);
1578 int tmp_parameter_id;
1579 tmp_parameter_id = stmt->GetInt(2);
1580 int tmp_start_period;
1581 tmp_start_period = stmt->GetInt(3);
1582 int tmp_start_run;
1583 tmp_start_run = stmt->GetInt(4);
1584 int tmp_end_period;
1585 tmp_end_period = stmt->GetInt(5);
1586 int tmp_end_run;
1587 tmp_end_run = stmt->GetInt(6);
1588 int tmp_value_key;
1589 tmp_value_key = stmt->GetInt(7);
1590 unsigned char* tmp_parameter_value;
1591 tmp_parameter_value = nullptr;
1592 Long_t tmp_sz_parameter_value = 0;
1593 stmt->GetBinary(8, (void*&)tmp_parameter_value, tmp_sz_parameter_value);
1594 TDatime* tmp_expiry_date;
1595 if (stmt->IsNull(9))
1596 tmp_expiry_date = nullptr;
1597 else
1598 tmp_expiry_date = new TDatime(stmt->GetDatime(9));
1599
1600 arrayResult->Add((TObject*)new UniDetectorParameter(
1601 connPar, tmp_value_id, tmp_detector_name, tmp_parameter_id, tmp_start_period, tmp_start_run, tmp_end_period,
1602 tmp_end_run, tmp_value_key, tmp_parameter_value, tmp_sz_parameter_value, tmp_expiry_date));
1603 }
1604
1605 delete stmt;
1606 delete connDb;
1607
1608 arrayResult->SetOwner(kTRUE);
1609 return arrayResult;
1610}
1611
1613{
1614 TObjArray search_conditions;
1615 search_conditions.Add((TObject*)&search_condition);
1616
1617 TObjArray* arrayResult = Search(search_conditions);
1618 // the condition belongs to the caller, so the local array (made owner by Search above) must not delete it
1619 search_conditions.SetOwner(kFALSE);
1620
1621 return arrayResult;
1622}
1623
1624// get parameter name of the current detector parameter
1626{
1628 if (pCurParameter == nullptr) {
1629 cout << "ERROR: parameter with current ID was not found" << endl;
1630 return "";
1631 }
1632
1633 TString par_name = pCurParameter->GetParameterName();
1634
1635 delete pCurParameter;
1636 return par_name;
1637}
1638
1639// get parameter type of the current detector parameter
1641{
1643 if (pCurParameter == nullptr) {
1644 cout << "ERROR: parameter with current ID was not found" << endl;
1645 return UndefinedType;
1646 }
1647
1648 enumValueType par_type = (enumValueType)pCurParameter->GetParameterType();
1649
1650 delete pCurParameter;
1651 return par_type;
1652}
1653
1654// get start period and run of the current detector parameter
1655void UniDetectorParameter::GetStart(int& start_period, int& start_run)
1656{
1657 start_period = i_start_period;
1658 start_run = i_start_run;
1659
1660 return;
1661}
1662
1663// get end period and run of the current detector parameter
1664void UniDetectorParameter::GetEnd(int& end_period, int& end_run)
1665{
1666 end_period = i_end_period;
1667 end_run = i_end_run;
1668
1669 return;
1670}
1671
1672// set start period and run of the current detector parameter
1673int UniDetectorParameter::SetStart(int start_period, int start_run)
1674{
1675 if (!connectionDB) {
1676 cout << "ERROR: Connection object is null" << endl;
1677 return -1;
1678 }
1679
1680 TSQLServer* db_server = connectionDB->GetSQLServer();
1681
1682 TString sql = TString::Format("update detector_parameter "
1683 "set start_period = $1, start_run = $2"
1684 "where value_id = $3");
1685 TSQLStatement* stmt = db_server->Statement(sql);
1686
1687 stmt->NextIteration();
1688 stmt->SetInt(0, start_period);
1689 stmt->SetInt(1, start_run);
1690 stmt->SetInt(2, i_value_id);
1691
1692 // write new value to database
1693 if (!stmt->Process()) {
1694 cout << "ERROR: updating the record has been failed" << endl;
1695
1696 delete stmt;
1697 return -2;
1698 }
1699
1700 i_start_period = start_period;
1701 i_start_run = start_run;
1702
1703 delete stmt;
1704 return 0;
1705}
1706
1707// set end period and run of the current detector parameter
1708int UniDetectorParameter::SetEnd(int end_period, int end_run)
1709{
1710 if (!connectionDB) {
1711 cout << "ERROR: Connection object is null" << endl;
1712 return -1;
1713 }
1714
1715 TSQLServer* db_server = connectionDB->GetSQLServer();
1716
1717 TString sql = TString::Format("update detector_parameter "
1718 "set end_period = $1, end_run = $2"
1719 "where value_id = $3");
1720 TSQLStatement* stmt = db_server->Statement(sql);
1721
1722 stmt->NextIteration();
1723 stmt->SetInt(0, end_period);
1724 stmt->SetInt(1, end_run);
1725 stmt->SetInt(2, i_value_id);
1726
1727 // write new value to database
1728 if (!stmt->Process()) {
1729 cout << "ERROR: updating the record has been failed" << endl;
1730
1731 delete stmt;
1732 return -2;
1733 }
1734
1735 i_end_period = end_period;
1736 i_end_run = end_run;
1737
1738 delete stmt;
1739 return 0;
1740}
1741
1742// ----- Print all 'detector parameters', except expired if the flag is true ---------------------------------
1743int UniDetectorParameter::PrintAll(bool isNonExpiredOnly)
1744{
1746 if (connDb == nullptr)
1747 return -1;
1748
1749 TSQLServer* db_server = connDb->GetSQLServer();
1750
1751 TString sql = TString::Format("select value_id, detector_name, parameter_id, start_period, start_run, end_period, "
1752 "end_run, value_key, parameter_value, expiry_date "
1753 "from detector_parameter%s",
1754 isNonExpiredOnly ? " where expiry_date is null" : "");
1755 TSQLStatement* stmt = db_server->Statement(sql);
1756
1757 // get all 'detector parameters' from the database
1758 if (!stmt->Process()) {
1759 cout << "ERROR: getting all 'detector parameters' from the database has been failed" << endl;
1760
1761 delete stmt;
1762 delete connDb;
1763 return -2;
1764 }
1765
1766 // store result of statement in buffer
1767 stmt->StoreResult();
1768
1769 // print rows
1770 cout << "Table 'detector_parameter':" << endl;
1771 while (stmt->NextResultRow()) {
1772 cout << "value_id: ";
1773 cout << (stmt->GetInt(0));
1774 cout << ", detector_name: ";
1775 cout << (stmt->GetString(1));
1776 cout << ", parameter_id: ";
1777 cout << (stmt->GetInt(2));
1778 cout << ", start_period: ";
1779 cout << (stmt->GetInt(3));
1780 cout << ", start_run: ";
1781 cout << (stmt->GetInt(4));
1782 cout << ", end_period: ";
1783 cout << (stmt->GetInt(5));
1784 cout << ", end_run: ";
1785 cout << (stmt->GetInt(6));
1786 cout << ", value_key: ";
1787 cout << (stmt->GetInt(7));
1788 cout << ", parameter_value: ";
1789 unsigned char* tmp_parameter_value = nullptr;
1790 Long_t tmp_sz_parameter_value = 0;
1791 stmt->GetBinary(8, (void*&)tmp_parameter_value, tmp_sz_parameter_value);
1792 cout << (void*)tmp_parameter_value << ", binary size: " << tmp_sz_parameter_value;
1793 if (!isNonExpiredOnly) {
1794 cout << ", expiry_date: ";
1795 if (stmt->IsNull(9))
1796 cout << "nullptr";
1797 else
1798 cout << stmt->GetDatime(9).AsSQLString();
1799 }
1800 cout << "." << endl;
1801 }
1802
1803 delete stmt;
1804 delete connDb;
1805
1806 return 0;
1807}
1808
1809/*
1810/// parse detector parameter's values, function returns row count added to the database
1811int UniDetectorParameter::ParseTxt(TString text_file, TString detector_name, TString parameter_name, int start_period,
1812int start_run, int end_period, int end_run, bool isSerialChannel)
1813{
1814 ifstream txtFile;
1815 txtFile.open(text_file, ios::in);
1816 if (!txtFile.is_open())
1817 {
1818 cout<<"Error: opening TXT file '"<<text_file<<"' was failed"<<endl;
1819 return -1;
1820 }
1821
1822 // open connection to database
1823 UniConnection* connDb = UniConnection::Open();
1824 if (connDb == nullptr)
1825 return -3;
1826
1827 TSQLServer* db_server = connDb->GetSQLServer();
1828
1829 string cur_line;
1830 while (getline(txtFile, cur_line))
1831 {
1832 //remove duplicates of whitespaces and tabs
1833 TString reduce_line = reduce(cur_line);
1834
1835 if (reduce_line[0] == '#')
1836 continue;
1837
1838 //stringstream ss;
1839 //ss.str(reduce_line);
1840
1841 // get detector object from 'detector_' table
1842 TString sql = TString::Format(
1843 "select detector_name "
1844 "from detector_ "
1845 "where lower(detector_name) = lower('%s'')", detector_name.Data());
1846 TSQLStatement* stmt = db_server->Statement(sql);
1847
1848 // get table record from DB
1849 if (!stmt->Process())
1850 {
1851 cout<<"Error: getting record with detector from 'detector_' table has been failed"<<endl;
1852 delete stmt;
1853 return -4;
1854 }
1855 stmt->StoreResult();
1856
1857 // check detector with the given name is exist
1858 if (!stmt->NextResultRow())
1859 {
1860 cout<<"Error: the detector with name '"<<detector_name<<"' was not found"<<endl;
1861 delete stmt;
1862 return -5;
1863 }
1864
1865 detector_name = stmt->GetString(0);
1866
1867 delete stmt;
1868
1869 // get parameter object from 'parameter_' table
1870 sql = TString::Format(
1871 "select parameter_id, parameter_name, parameter_type "
1872 "from parameter_ "
1873 "where lower(parameter_name) = lower('%s')", parameter_name.Data());
1874 stmt = db_server->Statement(sql);
1875
1876 // get table record from DB
1877 if (!stmt->Process())
1878 {
1879 cout<<"Error: getting record with parameter from 'parameter_' table has been failed"<<endl;
1880 delete stmt;
1881 return -6;
1882 }
1883
1884 stmt->StoreResult();
1885
1886 // extract row with parameter
1887 if (!stmt->NextResultRow())
1888 {
1889 cout<<"Error: the parameter with name '"<<parameter_name<<"' was not found"<<endl;
1890 delete stmt;
1891 return -7;
1892 }
1893
1894 //int parameter_id = stmt->GetInt(0);
1895 parameter_name = stmt->GetString(1);
1896 int parameter_type = stmt->GetInt(2);
1897
1898 delete stmt;
1899
1900 TObjArray* tokens = reduce_line.Tokenize("\t ");
1901 TObjString* item;
1902 int value_count = tokens->GetEntriesFast();
1903
1904 UniDetectorParameter* pDetectorParameter = nullptr;
1905 // choose parsing method for parameter's value according it's type
1906 int serial_number = -1, channel_number = -1, cur_num_item = 0;
1907 switch (parameter_type)
1908 {
1909 case BoolType:
1910 {
1911 int count_required = 1;
1912 if (isSerialChannel) count_required++, count_required++;
1913
1914 if (value_count != count_required)
1915 {
1916 cout<<"Warning: the count of parameters is not equal 1 or 3 ([serial] [channel]
1917value)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
1918 }
1919 if (isSerialChannel)
1920 {
1921 // get serial number
1922 item = (TObjString*) tokens->At(cur_num_item++);
1923 serial_number = item->GetString().Atoi();
1924 // get channel number
1925 item = (TObjString*) tokens->At(cur_num_item++);
1926 channel_number = item->GetString().Atoi();
1927 }
1928 item = (TObjString*) tokens->At(cur_num_item++);
1929 tokens->Delete();
1930
1931 bool value = (bool) item->GetString().Atoi();
1932 if (isSerialChannel)
1933 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
1934parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, value); else
1935 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
1936parameter_name,start_period, start_run, end_period, end_run, value);
1937
1938 break;
1939 }
1940 case IntType:
1941 {
1942 int count_required = 1;
1943 if (isSerialChannel) count_required++, count_required++;
1944
1945 if (value_count != count_required)
1946 {
1947 cout<<"Warning: the count of parameters is not equal 1 or 3 ([serial] [channel]
1948value)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
1949 }
1950 if (isSerialChannel)
1951 {
1952 // get serial number
1953 item = (TObjString*) tokens->At(cur_num_item++);
1954 serial_number = item->GetString().Atoi();
1955 // get channel number
1956 item = (TObjString*) tokens->At(cur_num_item++);
1957 channel_number = item->GetString().Atoi();
1958 }
1959 item = (TObjString*) tokens->At(cur_num_item++);
1960 tokens->Delete();
1961
1962 int value = item->GetString().Atoi();
1963 if (value == 0)
1964 {
1965 if (!item->GetString().IsDigit())
1966 {
1967 cout<<"Error: the parameter value is not integer: '"<<item<<"'"<<endl;
1968 continue;
1969 }
1970 }
1971
1972 if (isSerialChannel)
1973 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
1974parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, value); else
1975 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
1976parameter_name,start_period, start_run, end_period, end_run, value);
1977
1978 break;
1979 }
1980 case DoubleType:
1981 {
1982 int count_required = 1;
1983 if (isSerialChannel) count_required++, count_required++;
1984
1985 if (value_count != count_required)
1986 {
1987 cout<<"Warning: the count of parameters is not equal 1 or 3 ([serial] [channel]
1988value)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
1989 }
1990 if (isSerialChannel)
1991 {
1992 // get serial number
1993 item = (TObjString*) tokens->At(cur_num_item++);
1994 serial_number = item->GetString().Atoi();
1995 // get channel number
1996 item = (TObjString*) tokens->At(cur_num_item++);
1997 channel_number = item->GetString().Atoi();
1998 }
1999 item = (TObjString*) tokens->At(cur_num_item++);
2000 tokens->Delete();
2001
2002 double value = item->GetString().Atof();
2003 if (isSerialChannel)
2004 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2005parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, value); else
2006 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2007parameter_name,start_period, start_run, end_period, end_run, value);
2008
2009 break;
2010 }
2011 case IIArrayType:
2012 {
2013 int count_required = 2, size_arr = value_count/2;
2014 if (isSerialChannel)
2015 {
2016 count_required++, count_required++;
2017 size_arr--;
2018 }
2019
2020 if ((value_count < count_required) || ((value_count % 2) != 0))
2021 {
2022 cout<<"Warning: the count of parameters should be greater 2 or 4 ([serial] [channel] values@2)
2023and should be even"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2024 }
2025 if (isSerialChannel)
2026 {
2027 // get serial number
2028 item = (TObjString*) tokens->At(cur_num_item++);
2029 serial_number = item->GetString().Atoi();
2030 // get channel number
2031 item = (TObjString*) tokens->At(cur_num_item++);
2032 channel_number = item->GetString().Atoi();
2033 }
2034
2035 int iter = 0, num = count_required-2;
2036 IIStructure* pValues = new IIStructure[size_arr];
2037 for (; iter < size_arr; iter++)
2038 {
2039 pValues[iter].int_1 = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2040 pValues[iter].int_2 = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2041 }
2042 tokens->Delete();
2043
2044 if (isSerialChannel)
2045 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2046parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2047 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2048parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2049
2050 delete [] pValues;
2051 break;
2052 }
2053 case IntArrayType:
2054 {
2055 int count_required = 1, size_arr = value_count;
2056 if (isSerialChannel)
2057 {
2058 count_required++, count_required++;
2059 size_arr--, size_arr--;
2060 }
2061
2062 if (value_count < count_required)
2063 {
2064 cout<<"Warning: the count of parameters should be greater 1 or 3 ([serial] [channel] value1
2065value2...)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2066 }
2067 if (isSerialChannel)
2068 {
2069 // get serial number
2070 item = (TObjString*) tokens->At(cur_num_item++);
2071 serial_number = item->GetString().Atoi();
2072 // get channel number
2073 item = (TObjString*) tokens->At(cur_num_item++);
2074 channel_number = item->GetString().Atoi();
2075 }
2076
2077 int iter = 0;
2078 int* pValues = new int[size_arr];
2079 for (int num = count_required-1; num < value_count; num++,iter++)
2080 pValues[iter] = ((TObjString*) tokens->At(num))->GetString().Atoi();
2081 tokens->Delete();
2082
2083 if (isSerialChannel)
2084 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2085parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2086 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2087parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2088
2089 delete [] pValues;
2090 break;
2091 }
2092 case DoubleArrayType:
2093 {
2094 int count_required = 1, size_arr = value_count;
2095 if (isSerialChannel)
2096 {
2097 count_required++, count_required++;
2098 size_arr--, size_arr--;
2099 }
2100
2101 if (value_count < count_required)
2102 {
2103 cout<<"Warning: the count of parameters should be greater 1 or 3 ([serial] [channel] value1
2104value2...)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2105 }
2106 if (isSerialChannel)
2107 {
2108 // get serial number
2109 item = (TObjString*) tokens->At(cur_num_item++);
2110 serial_number = item->GetString().Atoi();
2111 // get channel number
2112 item = (TObjString*) tokens->At(cur_num_item++);
2113 channel_number = item->GetString().Atoi();
2114 }
2115
2116 int iter = 0;
2117 double* pValues = new double[size_arr];
2118 for (int num = count_required-1; num < value_count; num++, iter++)
2119 pValues[iter] = ((TObjString*) tokens->At(num))->GetString().Atof();
2120 tokens->Delete();
2121
2122 if (isSerialChannel)
2123 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2124parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2125 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2126parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2127
2128 delete [] pValues;
2129 break;
2130 }
2131 case UIntArrayType:
2132 {
2133 int count_required = 1, size_arr = value_count;
2134 if (isSerialChannel)
2135 {
2136 count_required++, count_required++;
2137 size_arr--, size_arr--;
2138 }
2139
2140 if (value_count < count_required)
2141 {
2142 cout<<"Warning: the count of parameters should be greater 1 or 3 ([serial] [channel] value1
2143value2...)"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2144 }
2145 if (isSerialChannel)
2146 {
2147 // get serial number
2148 item = (TObjString*) tokens->At(cur_num_item++);
2149 serial_number = item->GetString().Atoi();
2150 // get channel number
2151 item = (TObjString*) tokens->At(cur_num_item++);
2152 channel_number = item->GetString().Atoi();
2153 }
2154
2155 int iter = 0;
2156 unsigned int* pValues = new unsigned int[size_arr];
2157 for (int num = count_required-1; num < value_count; num++, iter++)
2158 pValues[iter] = (unsigned int) ((TObjString*) tokens->At(num))->GetString().Atoll();
2159 tokens->Delete();
2160
2161 if (isSerialChannel)
2162 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2163parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2164 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2165parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2166
2167 delete [] pValues;
2168 break;
2169 }
2170 case DchMapArrayType:
2171 {
2172 int count_required = 6, size_arr = value_count/6;
2173 if (isSerialChannel)
2174 count_required++, count_required++;
2175
2176 if ((value_count < count_required) || (((value_count-count_required+6) % 6) != 0))
2177 {
2178 cout<<"Warning: the count of parameters should be greater 6 or 8 ([serial] [channel] values@6)
2179and must be divisible by 6"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2180 }
2181 if (isSerialChannel)
2182 {
2183 // get serial number
2184 item = (TObjString*) tokens->At(cur_num_item++);
2185 serial_number = item->GetString().Atoi();
2186 // get channel number
2187 item = (TObjString*) tokens->At(cur_num_item++);
2188 channel_number = item->GetString().Atoi();
2189 }
2190
2191 int iter = 0, num = count_required-6;
2192 DchMapStructure* pValues = new DchMapStructure[size_arr];
2193 for (; iter < size_arr; iter++)
2194 {
2195 pValues[iter].plane = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2196 pValues[iter].group = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2197 pValues[iter].crate = (unsigned int) ((TObjString*) tokens->At(num++))->GetString().Atoll();
2198 pValues[iter].slot = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2199 pValues[iter].channel_low = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2200 pValues[iter].channel_high = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2201 }
2202 tokens->Delete();
2203
2204 if (isSerialChannel)
2205 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2206parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2207 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2208parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2209
2210 delete [] pValues;
2211 break;
2212 }
2213 case GemMapArrayType:
2214 {
2215 int count_required = 6, size_arr = value_count/6;
2216 if (isSerialChannel)
2217 count_required++, count_required++;
2218
2219 if ((value_count < count_required) || (((value_count-count_required+6) % 6) != 0))
2220 {
2221 cout<<"Warning: the count of parameters should be greater 6 or 8 ([serial] [channel] values@6)
2222and must be divisible by 6"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2223 }
2224 if (isSerialChannel)
2225 {
2226 // get serial number
2227 item = (TObjString*) tokens->At(cur_num_item++);
2228 serial_number = item->GetString().Atoi();
2229 // get channel number
2230 item = (TObjString*) tokens->At(cur_num_item++);
2231 channel_number = item->GetString().Atoi();
2232 }
2233
2234 int iter = 0, num = count_required-6;
2235 GemMapStructure* pValues = new GemMapStructure[size_arr];
2236 for (; iter < size_arr; iter++)
2237 {
2238 pValues[iter].serial = (unsigned int) ((TObjString*) tokens->At(num++))->GetString().Atoll();
2239 pValues[iter].id = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2240 pValues[iter].station = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2241 pValues[iter].channel_low = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2242 pValues[iter].channel_high = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2243 pValues[iter].hotZone = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2244 }
2245 tokens->Delete();
2246
2247 if (isSerialChannel)
2248 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2249parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2250 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2251parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2252
2253 delete [] pValues;
2254 break;
2255 }
2256 case GemPedestalArrayType:
2257 {
2258 int count_required = 4, size_arr = value_count/4;
2259 if (isSerialChannel)
2260 count_required++, count_required++;
2261
2262 if ((value_count < count_required) || (((value_count-count_required+4) % 4) != 0))
2263 {
2264 cout<<"Warning: the count of parameters should be greater 4 or 6 ([serial] [channel] values@6)
2265and must be divisible by 4"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2266 }
2267 if (isSerialChannel)
2268 {
2269 // get serial number
2270 item = (TObjString*) tokens->At(cur_num_item++);
2271 serial_number = item->GetString().Atoi();
2272 // get channel number
2273 item = (TObjString*) tokens->At(cur_num_item++);
2274 channel_number = item->GetString().Atoi();
2275 }
2276
2277 int iter = 0, num = count_required-4;
2278 GemPedestalStructure* pValues = new GemPedestalStructure[size_arr];
2279 for (; iter < size_arr; iter++)
2280 {
2281 pValues[iter].serial = (unsigned int) ((TObjString*) tokens->At(num++))->GetString().Atoll();
2282 pValues[iter].channel = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2283 pValues[iter].pedestal = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2284 pValues[iter].noise = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2285 }
2286 tokens->Delete();
2287
2288 if (isSerialChannel)
2289 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2290parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2291 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2292parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2293
2294 delete [] pValues;
2295 break;
2296 }
2297 case TriggerMapArrayType:
2298 {
2299 int count_required = 3, size_arr = value_count/3;
2300 if (isSerialChannel)
2301 count_required++, count_required++;
2302
2303 if ((value_count < count_required) || (((value_count-count_required+3) % 3) != 0))
2304 {
2305 cout<<"Warning: the count of parameters should be greater 3 or 5 ([serial] [channel] values@3)
2306and must be divisible by 3"<<endl<<"The current line will be skipped!"<<endl; tokens->Delete(); continue;
2307 }
2308 if (isSerialChannel)
2309 {
2310 // get serial number
2311 item = (TObjString*) tokens->At(cur_num_item++);
2312 serial_number = item->GetString().Atoi();
2313 // get channel number
2314 item = (TObjString*) tokens->At(cur_num_item++);
2315 channel_number = item->GetString().Atoi();
2316 }
2317
2318 int iter = 0, num = count_required-3;
2319 TriggerMapStructure* pValues = new TriggerMapStructure[size_arr];
2320 for (; iter < size_arr; iter++)
2321 {
2322 pValues[iter].serial = (unsigned int) ((TObjString*) tokens->At(num++))->GetString().Atoll();
2323 pValues[iter].slot = (unsigned int) ((TObjString*) tokens->At(num++))->GetString().Atoll();
2324 pValues[iter].channel = ((TObjString*) tokens->At(num++))->GetString().Atoi();
2325 }
2326 tokens->Delete();
2327
2328 if (isSerialChannel)
2329 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2330parameter_name,start_period, start_run, end_period, end_run, serial_number, channel_number, pValues, size_arr); else
2331 pDetectorParameter = UniDetectorParameter::CreateDetectorParameter(detector_name,
2332parameter_name,start_period, start_run, end_period, end_run, pValues, size_arr);
2333
2334 delete [] pValues;
2335 break;
2336 }
2337 default:
2338 {
2339 cout<<"Error: the type of parameter ("<<parameter_name<<") is not supported for parsing (txt)
2340now"<<endl; continue;
2341 }
2342 }// switch (parameter_type)
2343
2344 // clean memory
2345 if (pDetectorParameter)
2346 delete pDetectorParameter;
2347 }
2348
2349 txtFile.close();
2350 delete connDb;
2351
2352 return 0;
2353}*/
int i
Definition P4_F32vec4.h:22
long Long_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
TObject * ReadObject(const TClass *cl) override
Int_t WriteObject(const T *objptr, Bool_t cacheReuse=kTRUE)
static TObject * ConvertFromJSON(const char *str)
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=nullptr)
Int_t Length() const
char * Buffer() const
virtual void SetOwner(Bool_t enable=kTRUE)
const char * AsSQLString() const
UInt_t Convert(Bool_t toGMT=kFALSE) const
void Add(TObject *obj) override
static TClass * Class()
virtual TSQLStatement * Statement(const char *, Int_t=100)
virtual const char * GetString(Int_t)
virtual Bool_t SetBinary(Int_t, void *, Long_t, Long_t=0x1000)
virtual Bool_t NextIteration()=0
virtual Bool_t NextResultRow()=0
virtual Int_t GetNumAffectedRows()
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 Bool_t Process()=0
virtual Bool_t SetInt(Int_t, Int_t)
virtual Bool_t SetNull(Int_t)
virtual Bool_t StoreResult()=0
TDatime GetDatime(Int_t)
virtual Bool_t GetBinary(Int_t, void *&, Long_t &)
const char * Data() const
static TString Format(const char *fmt,...)
static UniConnection * Open()
TSQLServer * GetSQLServer()
int SetStart(int start_period, int start_run)
set start period and run of the current parameter value
int SetValue(vector< UniValue * > parameter_value)
set value to detector parameter presented by an array
static int WriteObjectBinary(const char *detector_name, const char *parameter_name, int start_period, int start_run, int end_period, int end_run, TObject *ptr_object)
write ROOT object as a binary parameter to the Database from start run to end one
static int WriteObjectJSON(const char *detector_name, const char *parameter_name, int start_period, int start_run, int end_period, int end_run, TObject *ptr_object)
write ROOT object as a JSON parameter to the Database from start run to end one
enumValueType GetParameterType()
get parameter type of the current parameter value
int SetValueKey(int value_key)
set value key of the current detector parameter
int SetDetectorName(TString detector_name)
set detector name of the current detector parameter
void GetStart(int &start_period, int &start_run)
get start period and run of the current parameter value
UniConnection * connectionDB
connection to the database
static int ReadObjectBinary(const char *detector_name, const char *parameter_name, int period_number, int run_number, TObject *&ptr_object)
read ROOT object as a binary parameter from from the Database for the selected run
Long_t sz_parameter_value
size of parameter value
int SetExpiryDate(TDatime *expiry_date)
set expiry date of the current detector parameter
TString GetParameterName()
get parameter name of the current parameter value
void GetEnd(int &end_period, int &end_run)
get end period and run of the current parameter value
UniValue * GetValue()
get value of detector parameter presented by a single value
int SetEnd(int end_period, int end_run)
set end period and run of the current parameter value
static int DeleteDetectorParameter(int value_id)
delete detector parameter from the database
unsigned char * blob_parameter_value
parameter value
int SetParameterId(int parameter_id)
set parameter id of the current detector parameter
static TObjArray * Search(UniSearchCondition &search_condition)
get parameters' values corresponding to the specified single condition (the condition stays owned by ...
int GetValue(vector< UniValue * > &parameter_value)
get value of detector parameter presented by an array
int SetEndPeriod(int end_period)
set end period of the current detector parameter
static int WriteFile(const char *detector_name, const char *parameter_name, int start_period, int start_run, int end_period, int end_run, const char *file_path)
write a whole file with parameter value (with given detector and parameter names) to the Database fro...
TString str_detector_name
detector name
int SetEndRun(int end_run)
set end run of the current detector parameter
int SetStartRun(int start_run)
set start run of the current detector parameter
void Print()
print information about current detector parameter
int SetUNC(unsigned char *p_parameter_value, Long_t size_parameter_value)
int i_parameter_id
parameter id
static int PrintAll()
print all detector parameters
UniDetectorParameter(UniConnection *db_connect, int value_id, TString detector_name, int parameter_id, int start_period, int start_run, int end_period, int end_run, int value_key, unsigned char *parameter_value, Long_t size_parameter_value, TDatime *expiry_date)
int SetStartPeriod(int start_period)
set start period of the current detector parameter
static UniDetectorParameter * GetDetectorParameter(int value_id)
get detector parameter from the database
static int CheckDetectorParameterExists(int value_id)
check detector parameter exists in the database: 1- true, 0 - false, <0 - database operation error
static int ReadFile(const char *detector_name, const char *parameter_name, int period_number, int run_number, const char *file_path)
read a whole file with parameter value (with given detector and parameter names) from the Database fo...
TDatime * ts_expiry_date
expiry date
static UniDetectorParameter * CreateDetectorParameter(TString detector_name, int parameter_id, int start_period, int start_run, int end_period, int end_run, int value_key, unsigned char *parameter_value, Long_t size_parameter_value, TDatime *expiry_date)
add new detector parameter to the database
int SetParameterValue(unsigned char *parameter_value, Long_t size_parameter_value)
set parameter value of the current detector parameter
unsigned char * GetUNC(enumValueType enum_parameter_type=UndefinedType)
Parse detector parameter's values and write them to the database: txtFile - path to the text file wit...
int i_start_period
start period
static int ReadObjectJSON(const char *detector_name, const char *parameter_name, int period_number, int run_number, TObject *&ptr_object)
read ROOT object as a JSON parameter from from the Database for the selected run
TString GetParameterName()
get parameter name of the current parameter
static bool CheckAndGetParameterID(TSQLServer *db_server, TString parameter_name, enumValueType enum_parameter_type, int &parameter_id)
static UniParameter * GetParameter(int parameter_id)
get parameter from the database
int GetParameterType()
get parameter type of the current parameter
static int GetFirstRunNumber(int period_number)
get first run number for a selected period
static int GetLastRunNumber(int period_number)
get last run number for a selected period
enumConditions GetCondition()
unsigned int GetUIntValue()
unsigned char * value
virtual enumValueType GetType()=0
virtual void ReadValue(unsigned char *source)=0
virtual void WriteValue(unsigned char *destination)=0
virtual size_t GetStorageSize()=0
enumValueType
@ UndefinedType
UniValue * CreateParameterValue(enumValueType parameter_type)