BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
UniRun.cxx
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2// UniRun cxx file
3// Generated 05-11-2015
4// ----------------------------------------------------------------------
5
6#include "UniRun.h"
7
8#include "TSQLServer.h"
9#include "TSQLStatement.h"
10#include "TSystem.h"
11#include "UniRawFile.h"
12
13#include <iostream>
14using namespace std;
15
16/* GENERATED CLASS MEMBERS (SHOULD NOT BE CHANGED MANUALLY) */
17// ----- Constructor with database connection -----------------------
19 int period_number,
20 int run_number,
21 int run_type,
22 TDatime start_datetime,
23 TDatime* end_datetime,
24 TString beam_particle,
25 TString target_particle,
26 double* energy,
27 double* field_voltage,
28 int* event_count,
29 int run_quality)
30{
31 connectionDB = db_connect;
32
33 i_period_number = period_number;
34 i_run_number = run_number;
35 i_run_type = run_type;
36 ts_start_datetime = start_datetime;
37 ts_end_datetime = end_datetime;
38 str_beam_particle = beam_particle;
39 str_target_particle = target_particle;
40 d_energy = energy;
41 d_field_voltage = field_voltage;
42 i_event_count = event_count;
43 i_run_quality = run_quality;
44}
45
46// ----- Destructor -------------------------------------------------
48{
49 if (connectionDB)
50 delete connectionDB;
52 delete ts_end_datetime;
53 if (d_energy)
54 delete d_energy;
56 delete d_field_voltage;
57 if (i_event_count)
58 delete i_event_count;
59}
60
61// ----- Creating new run in the database ---------------------------
62UniRun* UniRun::CreateRun(int period_number,
63 int run_number,
64 int run_type,
65 TDatime start_datetime,
66 TDatime* end_datetime,
67 TString beam_particle,
68 TString target_particle,
69 double* energy,
70 double* field_voltage,
71 int* event_count,
72 int run_quality)
73{
75 if (connDb == nullptr)
76 return nullptr;
77
78 TSQLServer* db_server = connDb->GetSQLServer();
79
80 TString sql = TString::Format("insert into run_(period_number, run_number, run_type, start_datetime, end_datetime, "
81 "beam_particle, target_particle, energy, field_voltage, event_count, run_quality) "
82 "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)");
83 TSQLStatement* stmt = db_server->Statement(sql);
84
85 stmt->NextIteration();
86 stmt->SetInt(0, period_number);
87 stmt->SetInt(1, run_number);
88 stmt->SetInt(2, run_type);
89 stmt->SetDatime(3, start_datetime);
90 if (end_datetime == nullptr)
91 stmt->SetNull(4);
92 else
93 stmt->SetDatime(4, *end_datetime);
94 stmt->SetString(5, beam_particle);
95 stmt->SetString(6, target_particle);
96 if (energy == nullptr)
97 stmt->SetNull(7);
98 else
99 stmt->SetDouble(7, *energy);
100 if (field_voltage == nullptr)
101 stmt->SetNull(8);
102 else
103 stmt->SetDouble(8, *field_voltage);
104 if (event_count == nullptr)
105 stmt->SetNull(9);
106 else
107 stmt->SetInt(9, *event_count);
108 stmt->SetInt(10, run_quality);
109
110 // inserting new run to the Database
111 if (!stmt->Process()) {
112 cout << "ERROR: inserting new run to the Database has been failed" << endl;
113 delete stmt;
114 delete connDb;
115 return nullptr;
116 }
117
118 delete stmt;
119
120 int tmp_period_number;
121 tmp_period_number = period_number;
122 int tmp_run_number;
123 tmp_run_number = run_number;
124 int tmp_run_type;
125 tmp_run_type = run_type;
126 TDatime tmp_start_datetime;
127 tmp_start_datetime = start_datetime;
128 TDatime* tmp_end_datetime;
129 if (end_datetime == nullptr)
130 tmp_end_datetime = nullptr;
131 else
132 tmp_end_datetime = new TDatime(*end_datetime);
133 TString tmp_beam_particle;
134 tmp_beam_particle = beam_particle;
135 TString tmp_target_particle;
136 tmp_target_particle = target_particle;
137 double* tmp_energy;
138 if (energy == nullptr)
139 tmp_energy = nullptr;
140 else
141 tmp_energy = new double(*energy);
142 double* tmp_field_voltage;
143 if (field_voltage == nullptr)
144 tmp_field_voltage = nullptr;
145 else
146 tmp_field_voltage = new double(*field_voltage);
147 int* tmp_event_count;
148 if (event_count == nullptr)
149 tmp_event_count = nullptr;
150 else
151 tmp_event_count = new int(*event_count);
152 int tmp_run_quality;
153 tmp_run_quality = run_quality;
154
155 return new UniRun(connDb, tmp_period_number, tmp_run_number, tmp_run_type, tmp_start_datetime, tmp_end_datetime,
156 tmp_beam_particle, tmp_target_particle, tmp_energy, tmp_field_voltage, tmp_event_count,
157 tmp_run_quality);
158}
159
160// ----- Get run from the database ---------------------------
161UniRun* UniRun::GetRun(int period_number, int run_number)
162{
164 if (connDb == nullptr)
165 return nullptr;
166
167 TSQLServer* db_server = connDb->GetSQLServer();
168
169 TString sql =
170 TString::Format("select period_number, run_number, run_type, start_datetime, end_datetime, beam_particle, "
171 "target_particle, energy, field_voltage, event_count, run_quality "
172 "from run_ "
173 "where period_number = %d and run_number = %d",
174 period_number, run_number);
175 TSQLStatement* stmt = db_server->Statement(sql);
176
177 // get run from the database
178 if (!stmt->Process()) {
179 cout << "ERROR: getting run from the database has been failed" << endl;
180
181 delete stmt;
182 delete connDb;
183 return nullptr;
184 }
185
186 // store result of statement in buffer
187 stmt->StoreResult();
188
189 // extract row
190 if (!stmt->NextResultRow()) {
191 cout << "ERROR: run was not found in the database" << endl;
192
193 delete stmt;
194 delete connDb;
195 return nullptr;
196 }
197
198 int tmp_period_number;
199 tmp_period_number = stmt->GetInt(0);
200 int tmp_run_number;
201 tmp_run_number = stmt->GetInt(1);
202 int tmp_run_type;
203 tmp_run_type = stmt->GetInt(2);
204 TDatime tmp_start_datetime;
205 tmp_start_datetime = stmt->GetDatime(3);
206 TDatime* tmp_end_datetime;
207 if (stmt->IsNull(4))
208 tmp_end_datetime = nullptr;
209 else
210 tmp_end_datetime = new TDatime(stmt->GetDatime(4));
211 TString tmp_beam_particle;
212 tmp_beam_particle = stmt->GetString(5);
213 TString tmp_target_particle;
214 tmp_target_particle = stmt->GetString(6);
215 double* tmp_energy;
216 if (stmt->IsNull(7))
217 tmp_energy = nullptr;
218 else
219 tmp_energy = new double(stmt->GetDouble(7));
220 double* tmp_field_voltage;
221 if (stmt->IsNull(8))
222 tmp_field_voltage = nullptr;
223 else
224 tmp_field_voltage = new double(stmt->GetDouble(8));
225 int* tmp_event_count;
226 if (stmt->IsNull(9))
227 tmp_event_count = nullptr;
228 else
229 tmp_event_count = new int(stmt->GetInt(9));
230 int tmp_run_quality;
231 tmp_run_quality = stmt->GetInt(10);
232
233 delete stmt;
234
235 return new UniRun(connDb, tmp_period_number, tmp_run_number, tmp_run_type, tmp_start_datetime, tmp_end_datetime,
236 tmp_beam_particle, tmp_target_particle, tmp_energy, tmp_field_voltage, tmp_event_count,
237 tmp_run_quality);
238}
239
240// ----- Check run exists in the database ---------------------------
241int UniRun::CheckRunExists(int period_number, int run_number)
242{
244 if (connDb == nullptr)
245 return -1;
246
247 TSQLServer* db_server = connDb->GetSQLServer();
248
249 TString sql = TString::Format("select 1 "
250 "from run_ "
251 "where period_number = %d and run_number = %d",
252 period_number, run_number);
253 TSQLStatement* stmt = db_server->Statement(sql);
254
255 // get run from the database
256 if (!stmt->Process()) {
257 cout << "ERROR: getting run from the database has been failed" << endl;
258
259 delete stmt;
260 delete connDb;
261 return -2;
262 }
263
264 // store result of statement in buffer
265 stmt->StoreResult();
266
267 // extract row
268 if (!stmt->NextResultRow()) {
269 delete stmt;
270 delete connDb;
271 return 0;
272 }
273
274 delete stmt;
275 delete connDb;
276
277 return 1;
278}
279
280// ----- Delete run from the database ---------------------------
281int UniRun::DeleteRun(int period_number, int run_number)
282{
284 if (connDb == nullptr)
285 return -1;
286
287 TSQLServer* db_server = connDb->GetSQLServer();
288
289 TString sql = TString::Format("delete from run_ "
290 "where period_number = $1 and run_number = $2");
291 TSQLStatement* stmt = db_server->Statement(sql);
292
293 stmt->NextIteration();
294 stmt->SetInt(0, period_number);
295 stmt->SetInt(1, run_number);
296
297 // delete run from the dataBase
298 if (!stmt->Process()) {
299 cout << "ERROR: deleting run from the dataBase has been failed" << endl;
300
301 delete stmt;
302 delete connDb;
303 return -2;
304 }
305
306 delete stmt;
307 delete connDb;
308 return 0;
309}
310
311// ----- Print all 'runs' ---------------------------------
313{
315 if (connDb == nullptr)
316 return -1;
317
318 TSQLServer* db_server = connDb->GetSQLServer();
319
320 TString sql =
321 TString::Format("select period_number, run_number, run_type, start_datetime, end_datetime, beam_particle, "
322 "target_particle, energy, field_voltage, event_count, run_quality "
323 "from run_");
324 TSQLStatement* stmt = db_server->Statement(sql);
325
326 // get all 'runs' from the database
327 if (!stmt->Process()) {
328 cout << "ERROR: getting all 'runs' from the dataBase has been failed" << endl;
329
330 delete stmt;
331 delete connDb;
332 return -2;
333 }
334
335 // store result of statement in buffer
336 stmt->StoreResult();
337
338 // print rows
339 cout << "Table 'run_':" << endl;
340 while (stmt->NextResultRow()) {
341 cout << "period_number: ";
342 cout << (stmt->GetInt(0));
343 cout << ", run_number: ";
344 cout << (stmt->GetInt(1));
345 cout << ", run_type: ";
346 cout << (stmt->GetInt(2));
347 cout << ", start_datetime: ";
348 cout << (stmt->GetDatime(3)).AsSQLString();
349 cout << ", end_datetime: ";
350 if (stmt->IsNull(4))
351 cout << "nullptr";
352 else
353 cout << stmt->GetDatime(4).AsSQLString();
354 cout << ", beam_particle: ";
355 cout << (stmt->GetString(5));
356 cout << ", target_particle: ";
357 cout << (stmt->GetString(6));
358 cout << ", energy: ";
359 if (stmt->IsNull(7))
360 cout << "nullptr";
361 else
362 cout << stmt->GetDouble(7);
363 cout << ", field_voltage: ";
364 if (stmt->IsNull(8))
365 cout << "nullptr";
366 else
367 cout << stmt->GetDouble(8);
368 cout << ", event_count: ";
369 if (stmt->IsNull(9))
370 cout << "nullptr";
371 else
372 cout << stmt->GetInt(9);
373 cout << ", run_quality: ";
374 cout << (stmt->GetInt(10));
375 cout << "." << endl;
376 }
377
378 delete stmt;
379 delete connDb;
380
381 return 0;
382}
383
384// Setters functions
385int UniRun::SetPeriodNumber(int period_number)
386{
387 if (!connectionDB) {
388 cout << "CRITICAL ERROR: Connection object is null" << endl;
389 return -1;
390 }
391
392 TSQLServer* db_server = connectionDB->GetSQLServer();
393
394 TString sql = TString::Format("update run_ "
395 "set period_number = $1 "
396 "where period_number = $2 and run_number = $3");
397 TSQLStatement* stmt = db_server->Statement(sql);
398
399 stmt->NextIteration();
400 stmt->SetInt(0, period_number);
401 stmt->SetInt(1, i_period_number);
402 stmt->SetInt(2, i_run_number);
403
404 // write new value to the database
405 if (!stmt->Process()) {
406 cout << "ERROR: updating information about run has been failed" << endl;
407
408 delete stmt;
409 return -2;
410 }
411
412 i_period_number = period_number;
413
414 delete stmt;
415 return 0;
416}
417
418int UniRun::SetRunNumber(int run_number)
419{
420 if (!connectionDB) {
421 cout << "CRITICAL ERROR: Connection object is null" << endl;
422 return -1;
423 }
424
425 TSQLServer* db_server = connectionDB->GetSQLServer();
426
427 TString sql = TString::Format("update run_ "
428 "set run_number = $1 "
429 "where period_number = $2 and run_number = $3");
430 TSQLStatement* stmt = db_server->Statement(sql);
431
432 stmt->NextIteration();
433 stmt->SetInt(0, run_number);
434 stmt->SetInt(1, i_period_number);
435 stmt->SetInt(2, i_run_number);
436
437 // write new value to the database
438 if (!stmt->Process()) {
439 cout << "ERROR: updating information about run has been failed" << endl;
440
441 delete stmt;
442 return -2;
443 }
444
445 i_run_number = run_number;
446
447 delete stmt;
448 return 0;
449}
450
451int UniRun::SetRunType(int run_type)
452{
453 if (!connectionDB) {
454 cout << "CRITICAL ERROR: Connection object is null" << endl;
455 return -1;
456 }
457
458 TSQLServer* db_server = connectionDB->GetSQLServer();
459
460 TString sql = TString::Format("update run_ "
461 "set run_type = $1 "
462 "where period_number = $2 and run_number = $3");
463 TSQLStatement* stmt = db_server->Statement(sql);
464
465 stmt->NextIteration();
466 stmt->SetInt(0, run_type);
467 stmt->SetInt(1, i_period_number);
468 stmt->SetInt(2, i_run_number);
469
470 // write new value to the database
471 if (!stmt->Process()) {
472 cout << "ERROR: updating information about run has been failed" << endl;
473
474 delete stmt;
475 return -2;
476 }
477
478 i_run_type = run_type;
479
480 delete stmt;
481 return 0;
482}
483
485{
486 if (!connectionDB) {
487 cout << "CRITICAL ERROR: Connection object is null" << endl;
488 return -1;
489 }
490
491 TSQLServer* db_server = connectionDB->GetSQLServer();
492
493 TString sql = TString::Format("update run_ "
494 "set start_datetime = $1 "
495 "where period_number = $2 and run_number = $3");
496 TSQLStatement* stmt = db_server->Statement(sql);
497
498 stmt->NextIteration();
499 stmt->SetDatime(0, start_datetime);
500 stmt->SetInt(1, i_period_number);
501 stmt->SetInt(2, i_run_number);
502
503 // write new value to the database
504 if (!stmt->Process()) {
505 cout << "ERROR: updating information about run has been failed" << endl;
506
507 delete stmt;
508 return -2;
509 }
510
511 ts_start_datetime = start_datetime;
512
513 delete stmt;
514 return 0;
515}
516
518{
519 if (!connectionDB) {
520 cout << "CRITICAL ERROR: Connection object is null" << endl;
521 return -1;
522 }
523
524 TSQLServer* db_server = connectionDB->GetSQLServer();
525
526 TString sql = TString::Format("update run_ "
527 "set end_datetime = $1 "
528 "where period_number = $2 and run_number = $3");
529 TSQLStatement* stmt = db_server->Statement(sql);
530
531 stmt->NextIteration();
532 if (end_datetime == nullptr)
533 stmt->SetNull(0);
534 else
535 stmt->SetDatime(0, *end_datetime);
536 stmt->SetInt(1, i_period_number);
537 stmt->SetInt(2, i_run_number);
538
539 // write new value to the database
540 if (!stmt->Process()) {
541 cout << "ERROR: updating information about run has been failed" << endl;
542
543 delete stmt;
544 return -2;
545 }
546
547 if (ts_end_datetime)
548 delete ts_end_datetime;
549 if (end_datetime == nullptr)
550 ts_end_datetime = nullptr;
551 else
552 ts_end_datetime = new TDatime(*end_datetime);
553
554 delete stmt;
555 return 0;
556}
557
559{
560 if (!connectionDB) {
561 cout << "CRITICAL ERROR: Connection object is null" << endl;
562 return -1;
563 }
564
565 TSQLServer* db_server = connectionDB->GetSQLServer();
566
567 TString sql = TString::Format("update run_ "
568 "set beam_particle = $1 "
569 "where period_number = $2 and run_number = $3");
570 TSQLStatement* stmt = db_server->Statement(sql);
571
572 stmt->NextIteration();
573 stmt->SetString(0, beam_particle);
574 stmt->SetInt(1, i_period_number);
575 stmt->SetInt(2, i_run_number);
576
577 // write new value to the database
578 if (!stmt->Process()) {
579 cout << "ERROR: updating information about run has been failed" << endl;
580
581 delete stmt;
582 return -2;
583 }
584
585 str_beam_particle = beam_particle;
586
587 delete stmt;
588 return 0;
589}
590
592{
593 if (!connectionDB) {
594 cout << "CRITICAL ERROR: Connection object is null" << endl;
595 return -1;
596 }
597
598 TSQLServer* db_server = connectionDB->GetSQLServer();
599
600 TString sql = TString::Format("update run_ "
601 "set target_particle = $1 "
602 "where period_number = $2 and run_number = $3");
603 TSQLStatement* stmt = db_server->Statement(sql);
604
605 stmt->NextIteration();
606 stmt->SetString(0, target_particle);
607 stmt->SetInt(1, i_period_number);
608 stmt->SetInt(2, i_run_number);
609
610 // write new value to the database
611 if (!stmt->Process()) {
612 cout << "ERROR: updating information about run has been failed" << endl;
613
614 delete stmt;
615 return -2;
616 }
617
618 str_target_particle = target_particle;
619
620 delete stmt;
621 return 0;
622}
623
624int UniRun::SetEnergy(double* energy)
625{
626 if (!connectionDB) {
627 cout << "CRITICAL ERROR: Connection object is null" << endl;
628 return -1;
629 }
630
631 TSQLServer* db_server = connectionDB->GetSQLServer();
632
633 TString sql = TString::Format("update run_ "
634 "set energy = $1 "
635 "where period_number = $2 and run_number = $3");
636 TSQLStatement* stmt = db_server->Statement(sql);
637
638 stmt->NextIteration();
639 if (energy == nullptr)
640 stmt->SetNull(0);
641 else
642 stmt->SetDouble(0, *energy);
643 stmt->SetInt(1, i_period_number);
644 stmt->SetInt(2, i_run_number);
645
646 // write new value to the database
647 if (!stmt->Process()) {
648 cout << "ERROR: updating information about run has been failed" << endl;
649
650 delete stmt;
651 return -2;
652 }
653
654 if (d_energy)
655 delete d_energy;
656 if (energy == nullptr)
657 d_energy = nullptr;
658 else
659 d_energy = new double(*energy);
660
661 delete stmt;
662 return 0;
663}
664
665int UniRun::SetFieldVoltage(double* field_voltage)
666{
667 if (!connectionDB) {
668 cout << "CRITICAL ERROR: Connection object is null" << endl;
669 return -1;
670 }
671
672 TSQLServer* db_server = connectionDB->GetSQLServer();
673
674 TString sql = TString::Format("update run_ "
675 "set field_voltage = $1 "
676 "where period_number = $2 and run_number = $3");
677 TSQLStatement* stmt = db_server->Statement(sql);
678
679 stmt->NextIteration();
680 if (field_voltage == nullptr)
681 stmt->SetNull(0);
682 else
683 stmt->SetDouble(0, *field_voltage);
684 stmt->SetInt(1, i_period_number);
685 stmt->SetInt(2, i_run_number);
686
687 // write new value to the database
688 if (!stmt->Process()) {
689 cout << "ERROR: updating information about run has been failed" << endl;
690
691 delete stmt;
692 return -2;
693 }
694
695 if (d_field_voltage)
696 delete d_field_voltage;
697 if (field_voltage == nullptr)
698 d_field_voltage = nullptr;
699 else
700 d_field_voltage = new double(*field_voltage);
701
702 delete stmt;
703 return 0;
704}
705
706int UniRun::SetEventCount(int* event_count)
707{
708 if (!connectionDB) {
709 cout << "CRITICAL ERROR: Connection object is null" << endl;
710 return -1;
711 }
712
713 TSQLServer* db_server = connectionDB->GetSQLServer();
714
715 TString sql = TString::Format("update run_ "
716 "set event_count = $1 "
717 "where period_number = $2 and run_number = $3");
718 TSQLStatement* stmt = db_server->Statement(sql);
719
720 stmt->NextIteration();
721 if (event_count == nullptr)
722 stmt->SetNull(0);
723 else
724 stmt->SetInt(0, *event_count);
725 stmt->SetInt(1, i_period_number);
726 stmt->SetInt(2, i_run_number);
727
728 // write new value to the database
729 if (!stmt->Process()) {
730 cout << "ERROR: updating information about run has been failed" << endl;
731
732 delete stmt;
733 return -2;
734 }
735
736 if (i_event_count)
737 delete i_event_count;
738 if (event_count == nullptr)
739 i_event_count = nullptr;
740 else
741 i_event_count = new int(*event_count);
742
743 delete stmt;
744 return 0;
745}
746
747int UniRun::SetRunQuality(int run_quality)
748{
749 if (!connectionDB) {
750 cout << "CRITICAL ERROR: Connection object is null" << endl;
751 return -1;
752 }
753
754 TSQLServer* db_server = connectionDB->GetSQLServer();
755
756 TString sql = TString::Format("update run_ "
757 "set run_quality = $1 "
758 "where period_number = $2 and run_number = $3");
759 TSQLStatement* stmt = db_server->Statement(sql);
760
761 stmt->NextIteration();
762 stmt->SetInt(0, run_quality);
763 stmt->SetInt(1, i_period_number);
764 stmt->SetInt(2, i_run_number);
765
766 // write new value to the database
767 if (!stmt->Process()) {
768 cout << "ERROR: updating information about run has been failed" << endl;
769
770 delete stmt;
771 return -2;
772 }
773
774 i_run_quality = run_quality;
775
776 delete stmt;
777 return 0;
778}
779
780// ----- Print current run ---------------------------------------
782{
783 cout << "Table 'run_'";
784 cout << ". period_number: " << i_period_number << ". run_number: " << i_run_number << ". run_type: " << i_run_type
785 << ". start_datetime: " << ts_start_datetime.AsSQLString()
786 << ". end_datetime: " << (ts_end_datetime == nullptr ? "nullptr" : (*ts_end_datetime).AsSQLString())
787 << ". beam_particle: " << str_beam_particle << ". target_particle: " << str_target_particle
788 << ". energy: " << (d_energy == nullptr ? "nullptr" : TString::Format("%f", *d_energy))
789 << ". field_voltage: " << (d_field_voltage == nullptr ? "nullptr" : TString::Format("%f", *d_field_voltage))
790 << ". event_count: " << (i_event_count == nullptr ? "nullptr" : TString::Format("%d", *i_event_count))
791 << ". run_quality: " << i_run_quality << endl;
792
793 return;
794}
795/* END OF GENERATED CLASS PART (SHOULD NOT BE CHANGED MANUALLY) */
796
797// get array of raw files (UniRawFile*) corresponding to the run (return nullptr if an error occured)
802
803// get numbers of runs existing in the Database for a selected range
804int UniRun::GetRunNumbers(UniqueRunNumber*& run_numbers, int start_period, int start_run, int end_period, int end_run)
805{
807 if (connDb == nullptr) {
808 cout << "ERROR: connection to the database was failed" << endl;
809 return -1;
810 }
811
812 TSQLServer* db_server = connDb->GetSQLServer();
813
814 if (end_period == -1)
815 end_period = start_period;
817 "select period_number, run_number "
818 "from run_ "
819 "where (not (((%d < period_number) or ((%d = period_number) and (%d <> -1) and (%d < run_number))) "
820 "or ((%d > period_number) or ((%d = period_number) and (%d > run_number))))) "
821 "order by period_number, run_number",
822 end_period, end_period, end_run, end_run, start_period, start_period, start_run);
823 TSQLStatement* stmt = db_server->Statement(sql);
824
825 // get table record from DB
826 if (!stmt->Process()) {
827 cout << "ERROR: getting run numbers from the database has been failed" << endl;
828 delete stmt;
829 delete connDb;
830 return -2;
831 }
832
833 // store result of statement in buffer
834 stmt->StoreResult();
835
836 vector<int> vecPeriods;
837 vector<int> vecRuns;
838 while (stmt->NextResultRow()) {
839 vecPeriods.push_back(stmt->GetInt(0));
840 vecRuns.push_back(stmt->GetInt(1));
841 }
842
843 delete stmt;
844 delete connDb;
845
846 int run_count = vecPeriods.size();
847 run_numbers = new UniqueRunNumber[run_count];
848 for (int i = 0; i < run_count; i++) {
849 run_numbers[i].period_number = vecPeriods[i];
850 run_numbers[i].run_number = vecRuns[i];
851 }
852
853 return run_count;
854}
855
856// get numbers of existing runs in the Database
858{
860 if (connDb == nullptr) {
861 cout << "ERROR: connection to the database was failed" << endl;
862 return -1;
863 }
864
865 TSQLServer* db_server = connDb->GetSQLServer();
866
867 TString sql = TString::Format("select period_number, run_number "
868 "from run_ "
869 "order by period_number, run_number");
870 TSQLStatement* stmt = db_server->Statement(sql);
871
872 // get table record from DB
873 if (!stmt->Process()) {
874 cout << "ERROR: getting run numbers from the database has been failed" << endl;
875 delete stmt;
876 delete connDb;
877 return -2;
878 }
879
880 // store result of statement in buffer
881 stmt->StoreResult();
882
883 vector<int> vecPeriods;
884 vector<int> vecRuns;
885 while (stmt->NextResultRow()) {
886 vecPeriods.push_back(stmt->GetInt(0));
887 vecRuns.push_back(stmt->GetInt(1));
888 }
889
890 delete stmt;
891 delete connDb;
892
893 int run_count = vecPeriods.size();
894 run_numbers = new UniqueRunNumber[run_count];
895 for (int i = 0; i < run_count; i++) {
896 run_numbers[i].period_number = vecPeriods[i];
897 run_numbers[i].run_number = vecRuns[i];
898 }
899
900 return run_count;
901}
902
903// get field voltage value
904// if period_number > 0 and run_number > 0: from the closest previous run, below (period:run)
905// in another case: for the latest run in the database
906// ATTENTION: changes the input period and run numbers to the correspoding ones of the previous run with the field
907// voltage
908double UniRun::FindPreviousFieldVoltage(int& period_number, int& run_number)
909{
911 if (connDb == nullptr)
912 return 0;
913
914 TSQLServer* db_server = connDb->GetSQLServer();
915
916 TString sql("select period_number, run_number, field_voltage "
917 "from run_ ");
918 if ((period_number > 0) and (run_number > 0))
919 sql += TString::Format(
920 "where period_number < %d OR (period_number = %d and run_number < %d) and field_voltage is not NULL ",
921 period_number, period_number, run_number);
922 sql += "order by period_number desc, run_number desc "
923 "limit 1";
924 TSQLStatement* stmt = db_server->Statement(sql);
925
926 // get field_voltage from the database
927 if (!stmt->Process()) {
928 cout << "ERROR: getting previous field_voltage from the database has been failed" << endl;
929
930 delete stmt;
931 delete connDb;
932 return 0;
933 }
934
935 // store result of statement in buffer
936 stmt->StoreResult();
937
938 // extract row
939 if (!stmt->NextResultRow()) {
940 delete stmt;
941 delete connDb;
942 return 0;
943 }
944
945 period_number = stmt->GetInt(0);
946 run_number = stmt->GetInt(1);
947 double field_voltage = stmt->GetDouble(2);
948
949 delete stmt;
950 delete connDb;
951
952 return field_voltage;
953}
954
956{
957 TObjArray* arrayResult = nullptr;
958 search_conditions.SetOwner(kTRUE);
959
961 if (connDb == nullptr) {
962 cout << "ERROR: connection to the Unified Condition Database was failed" << endl;
963 return arrayResult;
964 }
965
966 TSQLServer* db_server = connDb->GetSQLServer();
967
968 TString sql = TString::Format("select period_number, run_number, run_type, start_datetime, end_datetime, "
969 "beam_particle, target_particle, energy, field_voltage, event_count, run_quality "
970 "from run_");
971
972 TString strCondition;
973 bool isFirst = true;
974 TIter next(&search_conditions);
975 UniSearchCondition* curCondition;
976 while ((curCondition = (UniSearchCondition*)next()) != nullptr) {
977 strCondition = "";
978
979 switch (curCondition->GetColumn()) {
981 strCondition += "run_number ";
982 break;
984 strCondition += "period_number ";
985 break;
986 // case UniSearchCondition::columnFilePath: strCondition += "lower(file_path) "; break;
988 strCondition += "lower(beam_particle) ";
989 break;
991 strCondition += "lower(target_particle) ";
992 break;
994 strCondition += "energy ";
995 break;
997 strCondition += "start_datetime ";
998 break;
1000 strCondition += "end_datetime ";
1001 break;
1003 strCondition += "event_count ";
1004 break;
1006 strCondition += "field_voltage ";
1007 break;
1008 // case UniSearchCondition::columnFileSize: strCondition += "file_size "; break;
1009 default:
1010 cout << "ERROR: the column in the search condition was not defined, the condition is skipped" << endl;
1011 continue;
1012 }
1013
1014 switch (curCondition->GetCondition()) {
1016 strCondition += "< ";
1017 break;
1019 strCondition += "<= ";
1020 break;
1022 strCondition += "= ";
1023 break;
1025 strCondition += "<> ";
1026 break;
1028 strCondition += "> ";
1029 break;
1031 strCondition += ">= ";
1032 break;
1034 strCondition += "like ";
1035 break;
1037 strCondition += "is null ";
1038 break;
1040 strCondition += "is not null ";
1041 break;
1042 default:
1043 cout << "ERROR: the comparison operator in the search condition was not defined, the condition is "
1044 "skipped"
1045 << endl;
1046 continue;
1047 }
1048
1049 switch (curCondition->GetValueType()) {
1050 case 0:
1051 if ((curCondition->GetCondition() != UniSearchCondition::conditionNull)
1052 && (curCondition->GetCondition() != UniSearchCondition::conditionNotNull))
1053 continue;
1054 break;
1055 case 1:
1056 strCondition += Form("%d", curCondition->GetIntValue());
1057 break;
1058 case 2:
1059 strCondition += Form("%u", curCondition->GetUIntValue());
1060 break;
1061 case 3:
1062 strCondition += Form("%f", curCondition->GetDoubleValue());
1063 break;
1064 case 4:
1065 strCondition += Form("lower('%s')", curCondition->GetStringValue().Data());
1066 break;
1067 case 5:
1068 strCondition += Form("'%s'", curCondition->GetDatimeValue().AsSQLString());
1069 break;
1070 default:
1071 cout << "ERROR: the value type in the search condition was not found, the condition is skipped" << endl;
1072 continue;
1073 }
1074
1075 if (isFirst) {
1076 sql += " where ";
1077 isFirst = false;
1078 } else
1079 sql += " and ";
1080
1081 sql += strCondition;
1082 }
1083 sql += " order by period_number,run_number";
1084
1085 TSQLStatement* stmt = db_server->Statement(sql);
1086 // cout<<"SQL code: "<<sql<<endl;
1087
1088 // get table record from DB
1089 if (!stmt->Process()) {
1090 cout << "ERROR: getting runs from the Unified Condition Database has been failed" << endl;
1091 delete stmt;
1092 delete connDb;
1093
1094 return arrayResult;
1095 }
1096
1097 // store result of statement in buffer
1098 stmt->StoreResult();
1099
1100 // extract rows one after another
1101 arrayResult = new TObjArray();
1102 arrayResult->SetOwner(kTRUE);
1103 while (stmt->NextResultRow()) {
1105 if (connRun == nullptr) {
1106 cout << "ERROR: the connection to the Unified Condition Database for the selected run was failed" << endl;
1107 return arrayResult;
1108 }
1109
1110 int tmp_period_number;
1111 tmp_period_number = stmt->GetInt(0);
1112 int tmp_run_number;
1113 tmp_run_number = stmt->GetInt(1);
1114 int tmp_run_type;
1115 tmp_run_type = stmt->GetInt(2);
1116 TDatime tmp_start_datetime;
1117 tmp_start_datetime = stmt->GetDatime(3);
1118 TDatime* tmp_end_datetime;
1119 if (stmt->IsNull(4))
1120 tmp_end_datetime = nullptr;
1121 else
1122 tmp_end_datetime = new TDatime(stmt->GetDatime(4));
1123 TString tmp_beam_particle;
1124 tmp_beam_particle = stmt->GetString(5);
1125 TString tmp_target_particle;
1126 tmp_target_particle = stmt->GetString(6);
1127 double* tmp_energy;
1128 if (stmt->IsNull(7))
1129 tmp_energy = nullptr;
1130 else
1131 tmp_energy = new double(stmt->GetDouble(7));
1132 double* tmp_field_voltage;
1133 if (stmt->IsNull(8))
1134 tmp_field_voltage = nullptr;
1135 else
1136 tmp_field_voltage = new double(stmt->GetDouble(8));
1137 int* tmp_event_count;
1138 if (stmt->IsNull(9))
1139 tmp_event_count = nullptr;
1140 else
1141 tmp_event_count = new int(stmt->GetInt(9));
1142 int tmp_run_quality;
1143 tmp_run_quality = stmt->GetInt(10);
1144
1145 arrayResult->Add((TObject*)new UniRun(
1146 connRun, tmp_period_number, tmp_run_number, tmp_run_type, tmp_start_datetime, tmp_end_datetime,
1147 tmp_beam_particle, tmp_target_particle, tmp_energy, tmp_field_voltage, tmp_event_count, tmp_run_quality));
1148 }
1149
1150 delete stmt;
1151
1152 return arrayResult;
1153}
1154
1156{
1157 TObjArray search_conditions;
1158 search_conditions.Add((TObject*)&search_condition);
1159
1160 return Search(search_conditions);
1161}
1162
1163// get run from the database with the given period and run numbers
1164// if usePrevRunIfMissing is true then find and get the previous (existing) run in case the given run is absent
1165// ATTENTION: changes the input period and run numbers to ones of the found run (if usePrevRunIfMissing is true)
1166UniRun* UniRun::GetRun(int& period_number, int& run_number, bool usePrevRunIfMissing)
1167{
1168 if (!usePrevRunIfMissing)
1169 return UniRun::GetRun(period_number, run_number);
1170
1171 UniRun* pRun = UniRun::GetRun(period_number, run_number);
1172 if (pRun == nullptr) {
1173 cout << "INFO: Trying to get the previous run number before " << period_number << ":" << run_number << " run"
1174 << endl;
1175 int res_prev_code = UniRun::FindPreviousRun(period_number, run_number);
1176 if (res_prev_code != 0)
1177 return nullptr;
1178 cout << "INFO: The previous run number was defined: " << period_number << ":" << run_number << endl;
1179 pRun = UniRun::GetRun(period_number, run_number);
1180 }
1181
1182 return pRun;
1183}
1184
1185// get number of the closest run below the given one
1186// or return the latest run in the database if period_number < 1 or run_number < 1
1187int UniRun::FindPreviousRun(int& period_number, int& run_number)
1188{
1190 if (connDb == nullptr) {
1191 cout << "ERROR: connection to the Database was failed" << endl;
1192 return -1;
1193 }
1194
1195 TSQLServer* db_server = connDb->GetSQLServer();
1196
1197 TString sql = "select period_number, run_number "
1198 "from run_ ";
1199 if ((period_number > 0) and (run_number > 0))
1200 sql += TString::Format("where (period_number < %d) OR ((period_number = %d) AND (run_number < %d)) ",
1201 period_number, period_number, run_number);
1202 sql += "order by period_number desc, run_number desc "
1203 "limit 1";
1204 TSQLStatement* stmt = db_server->Statement(sql);
1205
1206 // get table record from DB
1207 if (!stmt->Process()) {
1208 cout << "ERROR: getting previous run number from DB has been failed" << endl;
1209 delete stmt;
1210 delete connDb;
1211 return -2;
1212 }
1213
1214 // store result of statement in buffer
1215 stmt->StoreResult();
1216
1217 // extract row
1218 if (!stmt->NextResultRow()) {
1219 cout << "ERROR: previous run was not found in the database" << endl;
1220
1221 delete stmt;
1222 delete connDb;
1223 return -3;
1224 }
1225
1226 period_number = stmt->GetInt(0);
1227 run_number = stmt->GetInt(1);
1228
1229 delete stmt;
1230 delete connDb;
1231
1232 return 0;
1233}
int i
Definition P4_F32vec4.h:22
constexpr Bool_t kTRUE
char * Form(const char *fmt,...)
virtual void SetOwner(Bool_t enable=kTRUE)
const char * AsSQLString() const
void Add(TObject *obj) override
virtual TSQLStatement * Statement(const char *, Int_t=100)
virtual const char * GetString(Int_t)
virtual Bool_t NextIteration()=0
virtual Bool_t NextResultRow()=0
virtual Bool_t SetString(Int_t, const char *, Int_t=256)
Bool_t SetDatime(Int_t, const TDatime &)
virtual Int_t GetInt(Int_t)
virtual Bool_t IsNull(Int_t)
virtual Bool_t SetDouble(Int_t, Double_t)
virtual Bool_t Process()=0
virtual Bool_t SetInt(Int_t, Int_t)
virtual Bool_t SetNull(Int_t)
virtual Double_t GetDouble(Int_t)
virtual Bool_t StoreResult()=0
TDatime GetDatime(Int_t)
const char * Data() const
static TString Format(const char *fmt,...)
static UniConnection * Open()
TSQLServer * GetSQLServer()
static TObjArray * GetRawFiles(int period_number, int run_number)
get array of raw files (UniRawFile*) corresponding to the period:run number ordered by start_event as...
static int PrintAll()
print all runs
Definition UniRun.cxx:312
void Print()
print information about current run
Definition UniRun.cxx:781
int i_run_number
run number
Definition UniRun.h:30
static UniRun * GetRun(int period_number, int run_number)
get run from the database
Definition UniRun.cxx:161
int SetEndDatetime(TDatime *end_datetime)
set end datetime of the current run
Definition UniRun.cxx:517
static TObjArray * Search(UniSearchCondition &search_condition)
get runs corresponding to the specified single condition and set owner for search_condition to kTRUE
Definition UniRun.cxx:1155
double * d_energy
energy
Definition UniRun.h:42
int SetTargetParticle(TString target_particle)
set target particle of the current run
Definition UniRun.cxx:591
TObjArray * GetRawFiles()
get array of raw files (UniRawFile*) corresponding to the run (return nullptr if an error occured)
Definition UniRun.cxx:798
TString str_target_particle
target particle
Definition UniRun.h:40
TDatime ts_start_datetime
start datetime
Definition UniRun.h:34
int SetRunType(int run_type)
set run type of the current run
Definition UniRun.cxx:451
static int DeleteRun(int period_number, int run_number)
delete run from the database
Definition UniRun.cxx:281
double * d_field_voltage
field voltage
Definition UniRun.h:44
static int GetRunNumbers(UniqueRunNumber *&run_numbers, int start_period, int start_run=0, int end_period=-1, int end_run=-1)
get numbers of runs stored in the Database for a selected range
Definition UniRun.cxx:804
int SetPeriodNumber(int period_number)
set period number of the current run
Definition UniRun.cxx:385
int i_run_quality
run quality
Definition UniRun.h:48
TDatime * ts_end_datetime
end datetime
Definition UniRun.h:36
int i_run_type
run type
Definition UniRun.h:32
UniRun(UniConnection *db_connect, int period_number, int run_number, int run_type, TDatime start_datetime, TDatime *end_datetime, TString beam_particle, TString target_particle, double *energy, double *field_voltage, int *event_count, int run_quality)
Definition UniRun.cxx:18
static UniRun * CreateRun(int period_number, int run_number, int run_type, TDatime start_datetime, TDatime *end_datetime, TString beam_particle, TString target_particle, double *energy, double *field_voltage, int *event_count, int run_quality)
add new run to the database
Definition UniRun.cxx:62
int SetEventCount(int *event_count)
set event count of the current run
Definition UniRun.cxx:706
int SetRunNumber(int run_number)
set run number of the current run
Definition UniRun.cxx:418
static double FindPreviousFieldVoltage(int &period_number, int &run_number)
get field voltage value if period_number > 0 and run_number > 0: from the closest previous run,...
Definition UniRun.cxx:908
static int FindPreviousRun(int &period_number, int &run_number)
get number of the closest run below the given one or return the latest run in the database if period_...
Definition UniRun.cxx:1187
int * i_event_count
event count
Definition UniRun.h:46
int SetFieldVoltage(double *field_voltage)
set field voltage of the current run
Definition UniRun.cxx:665
int i_period_number
period number
Definition UniRun.h:28
int SetEnergy(double *energy)
set energy of the current run
Definition UniRun.cxx:624
int SetRunQuality(int run_quality)
set run quality of the current run
Definition UniRun.cxx:747
TString str_beam_particle
beam particle
Definition UniRun.h:38
virtual ~UniRun()
Definition UniRun.cxx:47
UniConnection * connectionDB
connection to the database
Definition UniRun.h:25
int SetBeamParticle(TString beam_particle)
set beam particle of the current run
Definition UniRun.cxx:558
int SetStartDatetime(TDatime start_datetime)
set start datetime of the current run
Definition UniRun.cxx:484
static int CheckRunExists(int period_number, int run_number)
check run exists in the database: 1- true, 0 - false, <0 - database operation error
Definition UniRun.cxx:241
enumConditions GetCondition()
unsigned int GetUIntValue()
Definition json.hpp:4446