14#include <TMultiGraph.h>
15#include <TSQLServer.h>
16#include <TSQLStatement.h>
39 , parameter_time(ttp.parameter_time)
40 , parameter_type(ttp.parameter_type)
72 const char* system_name,
73 const char* parameter_name,
74 const char* member_name,
76 : strSystemName(system_name)
77 , strParameterName(parameter_name)
78 , strMemberName(member_name)
80 , strStartDate(start_date)
81 , strEndDate(end_date)
84 if (strcmp(start_date,
"2020-03-25") < 0) {
87 cout <<
"Old MySQL Database is used to get Tango data" << endl;
89 cout <<
"PostgreSQL Database is used to get Tango data" << endl;
96 if (arrTangoData !=
nullptr) {
97 arrTangoData->Delete();
104 TString strFullName = strSystemName +
":" + strParameterName;
105 if (strMemberName !=
"")
106 strFullName +=
" (" + strMemberName +
")";
115int TangoData::GetTangoParameter()
118 cout <<
"Input Start Date = " << strStartDate.Data() << endl;
120 cout <<
"Input End Date = " << strEndDate.Data() << endl;
121 if ((strStartDate ==
"") || (strEndDate ==
"")) {
122 cout <<
"ERROR: start datetime and end datetime should not be empty!" << endl;
125 if (strStartDate <
"2000-01-01") {
126 cout <<
"ERROR: start date cannot be less than 2000 year" << endl;
131 TString start_date_new(strStartDate), end_date_new(strEndDate);
133 start_date_new +=
" Europe/Moscow", end_date_new +=
" Europe/Moscow";
142 if (connDb ==
nullptr)
145 if (db_server ==
nullptr)
148 if (db_server->GetTables(db_server->GetDB()) ==
nullptr)
153 TString strStatement;
155 strStatement = TString::Format(
156 "SELECT att_conf_id, data_type "
157 "FROM att_conf ac join att_conf_data_type acdt on ac.att_conf_data_type_id = acdt.att_conf_data_type_id "
158 "WHERE family='%s' and name='%s' ",
159 strSystemName.Data(), strParameterName.Data());
161 strStatement = TString::Format(
"SELECT att_conf_id, table_name "
163 "WHERE domain='%s' and name='%s' ",
164 strSystemName.Data(), strParameterName.Data());
165 if (strMemberName !=
"")
166 strStatement += TString::Format(
"and member = '%s' ", strMemberName.Data());
170 cout <<
"Processing the following SQL request to get info on the Tango parameter:" << endl
171 << strStatement << endl;
172 TSQLStatement* stmt_select = db_server->Statement(strStatement);
173 if (!stmt_select->Process()) {
174 cout <<
"ERROR: getting info on parameter from Tango has been failed: " <<
GetFullName() << endl;
179 stmt_select->StoreResult();
180 if (!stmt_select->NextResultRow()) {
181 cout <<
"ERROR: There is no Tango parameter '" <<
GetFullName() << endl;
186 if ((IsOldMySQL) && (stmt_select->GetNumAffectedRows() > 1) && (strcmp(strEndDate.Data(),
"2018-01-01") > 0)) {
187 if (!stmt_select->NextResultRow()) {
188 cout <<
"ERROR: There is no second row for parameter '" << strParameterName <<
"' for " << strSystemName
189 <<
" detector" << endl;
195 int parameter_id = stmt_select->GetInt(0);
196 TString table_name(stmt_select->GetString(1));
198 table_name = TString::Format(
"att_%s", table_name.Data());
200 cout <<
"Found Data Table name = " << table_name <<
". Parameter ID = " << parameter_id << endl;
205 if (table_name.Contains(
"array_devdouble"))
208 if (table_name.Contains(
"array_devboolean"))
211 if (table_name.Contains(
"scalar_devdouble"))
214 cout <<
"ERROR: This Tango type is not supported: '" << table_name <<
"'" << endl;
223 query_data =
"SELECT date_format(data_time, '%Y-%m-%d %H:%i:%S'), value_r ";
225 query_data =
"SELECT to_char(data_time, 'YYYY-MM-DD HH24:MI:SS.MS'), value_r ";
228 query_data +=
", dim_x_r, dim_x_w, idx ";
231 query_data += TString::Format(
"FROM %s "
232 "WHERE att_conf_id='%d' and ((data_time > '%s' and data_time <= '%s') "
233 "or (data_time = (SELECT max(data_time) FROM %s WHERE att_conf_id='%d' and data_time "
234 "<= '%s' and value_r is not null))) "
235 "and value_r is not null ",
236 table_name.Data(), parameter_id, start_date_new.Data(), end_date_new.Data(),
237 table_name.Data(), parameter_id, start_date_new.Data());
240 query_data +=
"ORDER BY data_time,idx";
242 query_data +=
"ORDER BY data_time";
245 cout <<
"Query data with the following SQL request:" << endl << query_data << endl;
246 stmt_select = db_server->Statement(query_data);
247 if (!stmt_select->Process()) {
248 cout <<
"ERROR: getting parameter values from Tango has been failed" << endl;
253 stmt_select->StoreResult();
258 arrTangoData =
new TObjArray();
259 arrTangoData->SetName(
260 TString::Format(
"'%s' parameter for %s detector", strParameterName.Data(), strSystemName.Data()));
261 arrTangoData->SetOwner(kTRUE);
262 while (stmt_select->NextResultRow()) {
263 const char* pc_datetime = stmt_select->GetString(0);
264 TDatime datetime(pc_datetime);
266 if ((cur_num == 0) && (strcmp(pc_datetime, strStartDate) < 0))
267 datetime.Set(strStartDate);
271 i_par_len = stmt_select->GetInt(2);
272 if (i_par_len == 0) {
273 cout <<
"ERROR: Parameter length cannot be equal 0" << endl;
275 DeleteTangoParameter();
279 int i_real_par_len = 1;
281 i_real_par_len = stmt_select->GetInt(3);
282 if (i_real_par_len == 0) {
283 cout <<
"ERROR: Real parameter length cannot be equal 0" << endl;
285 DeleteTangoParameter();
291 for (
int ind = 0; ind < i_par_len; ind++) {
293 stmt_select->NextResultRow();
297 i_idx = stmt_select->GetInt(4);
300 cout <<
"ERROR: idx should be equal index of the parameter array" << endl;
302 DeleteTangoParameter();
306 if (ind >= i_real_par_len)
310 bool b_val = (bool)stmt_select->GetInt(1);
315 double d_val = stmt_select->GetDouble(1);
325 arrTangoData->Add(par);
331 if ((par !=
nullptr) && (strcmp(par->
parameter_time.AsSQLString(), strEndDate.Data()) < 0)) {
334 arrTangoData->Add(par_last);
343 if (arrTangoData ==
nullptr) {
344 cout <<
"ERROR: Tango parameter has not been obtained" << endl;
348 int def_precision = cout.precision();
352 for (
int i = 0;
i < arrTangoData->GetEntriesFast();
i++) {
358 for (
int j = 0; j < par_length; j++)
360 cout <<
"" << endl << endl;
363 cout.precision(def_precision);
370 for (
const auto& tango_data : vecTangoData) {
371 cout <<
"The current parameter: " << tango_data->GetFullName() << endl;
372 tango_data->PrintTangoDataConsole();
380 if (arrTangoData ==
nullptr) {
381 cout <<
"ERROR: Tango parameter has not been obtained" << endl;
385 TCanvas* c1 =
new TCanvas(
"c1",
"Tango Data", 800, 600);
386 TGraph2D* tango_graph =
new TGraph2D();
387 tango_graph->SetTitle(arrTangoData->GetName());
390 for (
int i = 0;
i < arrTangoData->GetEntriesFast();
i++) {
395 for (
int j = 0; j < par_length; j++) {
399 tango_graph->SetPoint(
i * par_length + j, cur_time, j + 1, value);
403 tango_graph->Draw(
"SURF1");
406 tango_graph->GetXaxis()->SetTitle(
"time");
407 tango_graph->GetXaxis()->CenterTitle();
408 tango_graph->GetXaxis()->SetTitleOffset(1.9);
409 tango_graph->GetXaxis()->SetLabelSize(0.025);
410 tango_graph->GetXaxis()->SetLabelOffset(0.009);
411 tango_graph->GetXaxis()->SetNdivisions(-503);
413 tango_graph->GetYaxis()->SetTitle(y_axis);
414 tango_graph->GetYaxis()->CenterTitle();
415 tango_graph->GetYaxis()->SetTitleOffset(1.9);
416 tango_graph->GetYaxis()->SetLabelSize(0.025);
417 tango_graph->GetYaxis()->SetLabelOffset(0.001);
418 tango_graph->GetYaxis()->CenterLabels();
419 tango_graph->GetYaxis()->SetNdivisions(par_length + 1, 0, 0);
421 tango_graph->GetZaxis()->SetLabelSize(0.025);
424 tango_graph->GetXaxis()->SetTimeDisplay(1);
425 tango_graph->GetXaxis()->SetTimeFormat(
"%Y.%m.%d %H:%M");
426 tango_graph->GetXaxis()->SetTimeOffset(0,
"local");
438 if (arrTangoData ==
nullptr) {
439 cout <<
"ERROR: Tango parameter has not been obtained" << endl;
443 if (arrTangoData->GetEntries() < 1) {
444 cout <<
"WARNING: There are no elements in the Tango array" << endl;
448 TCanvas* c1 =
new TCanvas(
"c1",
"Tango Data", 800, 600);
451 int par_length = ((
TangoTimeParameter*)arrTangoData->At(0))->double_parameter_value.size();
453 TGraph* tango_graphs =
new TGraph[par_length]();
455 for (
int j = 0; j < par_length; j++) {
456 Int_t color = gRandom->Integer(50);
457 if ((color == 0) || (color == 10) || ((color > 16) && (color < 20)))
459 tango_graphs[j].SetLineColor(color);
462 for (
int i = 0;
i < arrTangoData->GetEntriesFast();
i++) {
465 for (
int j = 0; j < par_length; j++) {
468 tango_graphs[j].SetPoint(
i, cur_time, value);
472 TMultiGraph* tango_multi =
new TMultiGraph();
473 tango_multi->SetTitle(arrTangoData->GetName());
474 for (
int j = 0; j < par_length; j++) {
475 tango_multi->Add(&tango_graphs[j]);
476 tango_graphs[j].SetTitle(TString::Format(
"%d", j));
477 tango_graphs[j].SetLineWidth(3);
480 TString draw_par =
"AL";
483 tango_multi->Draw(draw_par.Data());
487 tango_multi->GetXaxis()->SetTitle(
"time");
488 tango_multi->GetXaxis()->CenterTitle();
489 tango_multi->GetXaxis()->SetTitleOffset(1.3);
490 tango_multi->GetXaxis()->SetLabelSize(0.025);
491 tango_multi->GetXaxis()->SetLabelOffset(0.009);
492 tango_multi->GetXaxis()->SetNdivisions(-503);
494 tango_multi->GetXaxis()->SetTimeDisplay(1);
495 tango_multi->GetXaxis()->SetTimeFormat(
"%Y.%m.%d %H:%M");
496 tango_multi->GetXaxis()->SetTimeOffset(0,
"local");
498 tango_multi->GetYaxis()->SetTitle(y_axis);
499 tango_multi->GetYaxis()->CenterTitle();
500 tango_multi->GetYaxis()->SetTitleOffset(1.3);
501 tango_multi->GetYaxis()->SetLabelSize(0.025);
502 tango_multi->GetYaxis()->SetLabelOffset(0.001);
503 tango_multi->GetYaxis()->CenterLabels();
507 TLegend* pLegend = c1->BuildLegend(0.92, 0.77, 0.99, 0.98,
"");
508 pLegend->SetMargin(0.80);
518 if (vecTangoData.size() < 1) {
519 cout <<
"WARNING: There are no parameters in the specified array" << endl;
524 int par_length = vecTangoData.size();
527 TCanvas* c1 =
new TCanvas(
"c1",
"Tango Data", 800, 600);
528 TGraph* tango_graphs =
new TGraph[par_length]();
531 for (j = 0; j < par_length; j++) {
532 Int_t color = gRandom->Integer(50);
533 if ((color == 0) || (color == 10) || ((color > 16) && (color < 20)))
535 tango_graphs[j].SetLineColor(color);
539 for (
const auto& current_parameter : vecTangoData) {
540 if (current_parameter->GetTangoData()->IsEmpty()) {
541 cout <<
"ERROR: There are no elements in the parameter (" << current_parameter->GetFullName() <<
")"
546 if ((((
TangoTimeParameter*)current_parameter->GetTangoData()->At(0))->double_parameter_value.size()) != 1) {
547 cout <<
"ERROR: the parameter " << current_parameter->GetFullName() <<
" must have single value" << endl;
551 TObjArray* arrTangoData = current_parameter->GetTangoData();
552 for (
int i = 0;
i < arrTangoData->GetEntriesFast();
i++) {
555 tango_graphs[j].SetPoint(
i, cur_time, value);
560 TMultiGraph* tango_multi =
new TMultiGraph();
561 tango_multi->SetTitle(vecTangoData[0]->
GetFullName());
562 for (j = 0; j < par_length; j++) {
563 tango_multi->Add(&tango_graphs[j]);
564 tango_graphs[j].SetTitle(TString::Format(
"%d", j));
565 tango_graphs[j].SetLineWidth(3);
568 TString draw_par =
"AL";
571 tango_multi->Draw(draw_par.Data());
575 tango_multi->GetXaxis()->SetTitle(
"time");
576 tango_multi->GetXaxis()->CenterTitle();
577 tango_multi->GetXaxis()->SetTitleOffset(1.3);
578 tango_multi->GetXaxis()->SetLabelSize(0.025);
579 tango_multi->GetXaxis()->SetLabelOffset(0.009);
580 tango_multi->GetXaxis()->SetNdivisions(-503);
582 tango_multi->GetXaxis()->SetTimeDisplay(1);
583 tango_multi->GetXaxis()->SetTimeFormat(
"%Y.%m.%d %H:%M");
584 tango_multi->GetXaxis()->SetTimeOffset(0,
"local");
586 tango_multi->GetYaxis()->SetTitle(y_axis);
587 tango_multi->GetYaxis()->CenterTitle();
588 tango_multi->GetYaxis()->SetTitleOffset(1.3);
589 tango_multi->GetYaxis()->SetLabelSize(0.025);
590 tango_multi->GetYaxis()->SetLabelOffset(0.001);
591 tango_multi->GetYaxis()->CenterLabels();
595 TLegend* pLegend = c1->BuildLegend(0.92, 0.77, 0.99, 0.98,
"");
596 pLegend->SetMargin(0.80);
606 vector<double> result;
607 if (arrTangoData ==
nullptr) {
608 cout <<
"ERROR: Tango parameter has not been obtained" << endl;
613 cout <<
"strStartDateAvg = " << strStartDate.Data() << endl;
615 cout <<
"strEndDateAvg = " << strEndDate.Data() << endl;
616 int time_count = arrTangoData->GetEntriesFast();
617 TDatime start_date(strStartDate), end_date(strEndDate);
620 if ((IsOldMySQL) || (time_count < 2)) {
621 for (
int i = 0;
i < time_count;
i++) {
632 for (
size_t j = 0; j < result.size(); j++)
633 result[j] /= time_count;
638 TDatime previousTime;
640 for (
int i = 0;
i < time_count; previousTime = pParameter->
parameter_time,
i++) {
644 if (start_date < pParameter->parameter_time) {
645 UInt_t sec = pParameter->
parameter_time.Convert() - start_date.Convert();
653 UInt_t sec = pParameter->
parameter_time.Convert() - previousTime.Convert();
665 UInt_t sec = end_date.Convert() - pParameter->
parameter_time.Convert();
671 UInt_t sec = end_date.Convert() - start_date.Convert();
672 for (
size_t j = 0; j < result.size(); j++)
691 if (arrTangoData ==
nullptr) {
692 cout <<
"ERROR: Tango parameter has not been obtained" << endl;
696 TObjArray* pTimeIntervals =
new TObjArray();
697 pTimeIntervals->SetOwner(kTRUE);
700 if (arrTangoData->GetEntriesFast() == 0) {
702 return pTimeIntervals;
708 vector<bool> vecCondition;
712 for (
int i = 0;
i < arrTangoData->GetEntriesFast();
i++) {
717 vecCondition.push_back(
false);
718 vecStart.push_back(0);
720 TObjArray* pChannel =
new TObjArray();
721 pChannel->SetOwner(kTRUE);
722 pTimeIntervals->Add(pChannel);
749 cout <<
"ERROR: comparison operator in the searching of intervals is not appropriable for boolean "
753 delete pTimeIntervals;
761 if (!vecCondition[j]) {
762 vecCondition[j] =
true;
771 if (vecCondition[j]) {
772 vecCondition[j] =
false;
774 int start_i = vecStart[j];
775 TDatime startInterval = ((
TangoTimeParameter*)arrTangoData->At(start_i))->parameter_time;
776 if ((start_i > 0) && (IsOldMySQL)) {
777 TDatime previousTime = ((
TangoTimeParameter*)arrTangoData->At(start_i - 1))->parameter_time;
778 TDatime middleTime((startInterval.Convert() + previousTime.Convert()) / 2);
779 startInterval = middleTime;
782 TDatime endInterval(uiEndTime - 1);
785 (uiEndTime + ((
TangoTimeParameter*)arrTangoData->At(
i - 1))->parameter_time.Convert()) / 2);
789 int real_channel = j;
790 if (mapChannel !=
nullptr) {
791 if (mapChannel->size() > j) {
792 real_channel = mapChannel->at(j);
793 if (pTimeIntervals->GetEntriesFast() <= real_channel) {
794 cout <<
"CRITICAL WARNING: Tango array size is less than the corrected channel: "
795 << real_channel << endl;
799 cout <<
"CRITICAL WARNING: map channel size is less than the current channel: " << j
805 ((TObjArray*)pTimeIntervals->At(real_channel))->
Add(pTimeInterval);
814 if (vecCondition[j]) {
815 vecCondition[j] =
false;
817 int start_i = vecStart[j];
818 TDatime startInterval = ((
TangoTimeParameter*)arrTangoData->At(start_i))->parameter_time;
820 TDatime previousTime = ((
TangoTimeParameter*)arrTangoData->At(start_i - 1))->parameter_time;
821 TDatime middleTime((startInterval.Convert() + previousTime.Convert()) / 2);
822 startInterval = middleTime;
824 TDatime endInterval =
825 ((
TangoTimeParameter*)arrTangoData->At(arrTangoData->GetEntriesFast() - 1))->parameter_time;
828 int real_channel = j;
829 if (mapChannel !=
nullptr) {
830 if (mapChannel->size() > j) {
831 real_channel = mapChannel->at(j);
832 if (pTimeIntervals->GetEntriesFast() <= real_channel) {
833 cout <<
"CRITICAL WARNING: Tango array size is less than the corrected channel: "
834 << real_channel << endl;
838 cout <<
"CRITICAL WARNING: map channel size is less than the current channel: " << j << endl;
842 ((TObjArray*)pTimeIntervals->At(real_channel))->
Add(pTimeInterval);
847 return pTimeIntervals;
858 for (
int i = 0;
i < tango_intervals->GetEntriesFast();
i++) {
859 TObjArray* pChannel = (TObjArray*)tango_intervals->At(
i);
860 if (pChannel->GetEntriesFast() == 0) {
861 if (!isShowOnlyExists)
862 cout << channel_name.Data() <<
" " <<
i <<
":" << endl
863 <<
" No intervals correspond to the specified conditions" << endl;
866 cout << channel_name.Data() <<
" " <<
i <<
":" << endl;
867 for (
int j = 0; j < pChannel->GetEntriesFast(); j++) {
869 cout <<
" " << pInterval->
start_time.AsSQLString();
871 cout << pInterval->
end_time.AsSQLString();
880void TangoData::DeleteTangoParameter()
883 strSystemName =
"", strParameterName =
"", strStartDate =
"", strEndDate =
"";
884 if (arrTangoData !=
nullptr) {
885 arrTangoData->Delete();
887 arrTangoData =
nullptr;
Bool_t Add(const vector< Double_t > &a, const vector< Double_t > &b, vector< Double_t > &c)
const char *const TANGO_DB_USERNAME_b2020
const char *const TANGO_DB_NAME_b2020
const char *const TANGO_DB_PASSWORD_b2020
const char *const TANGO_DB_HOST_b2020
TSQLServer * GetSQLServer()
static TangoConnection * Open()
std::vector< double > GetAverageTangoData()
static void PrintTangoArrayConsole(std::vector< TangoData * > vecTangoData)
TObjArray * SearchTangoIntervals(enumConditions condition=conditionEqual, bool value=true, std::vector< int > *mapChannel=nullptr)
void PrintTangoDataConsole()
@ conditionGreaterOrEqual
void PrintTangoDataSurface(const char *y_axis="tango parameter")
static int PrintTangoArrayMultiGraph(std::vector< TangoData * > vecTangoData, const char *y_axis="parameter", bool is3D=false)
int PrintTangoDataMultiGraph(const char *y_axis="parameter", bool is3D=false)
TangoData(const char *start_date, const char *end_date, const char *system_name, const char *parameter_name, const char *member_name="", int verbose=0)
void PrintTangoIntervalConsole(TObjArray *tango_intervals, TString channel_name="Channel", bool isShowOnlyExists=false)
virtual ~TangoTimeInterval()
std::vector< bool > bool_parameter_value
Tango_Parameter_Type parameter_type
std::vector< double > double_parameter_value
virtual ~TangoTimeParameter()