File indexing completed on 2025-08-03 08:21:00
0001 #include "MyMonDraw.h"
0002
0003 #include <onlmon/OnlMonClient.h>
0004 #include <onlmon/OnlMonDB.h>
0005
0006 #include <TAxis.h> // for TAxis
0007 #include <TCanvas.h>
0008 #include <TDatime.h>
0009 #include <TGraphErrors.h>
0010 #include <TH1.h>
0011 #include <TPad.h>
0012 #include <TROOT.h>
0013 #include <TSystem.h>
0014 #include <TText.h>
0015
0016 #include <cstring> // for memset
0017 #include <ctime>
0018 #include <fstream>
0019 #include <iostream> // for operator<<, basic_ostream, basic_os...
0020 #include <sstream>
0021 #include <vector> // for vector
0022
0023 MyMonDraw::MyMonDraw(const std::string &name)
0024 : OnlMonDraw(name)
0025 {
0026
0027 TDatime T0(2003, 01, 01, 00, 00, 00);
0028 TimeOffsetTicks = T0.Convert();
0029 dbvars = new OnlMonDB("MyMon");
0030 return;
0031 }
0032
0033 int MyMonDraw::Init()
0034 {
0035 return 0;
0036 }
0037
0038 int MyMonDraw::MakeCanvas(const std::string &name)
0039 {
0040 OnlMonClient *cl = OnlMonClient::instance();
0041 int xsize = cl->GetDisplaySizeX();
0042 int ysize = cl->GetDisplaySizeY();
0043 if (name == "MyMon1")
0044 {
0045
0046 TC[0] = new TCanvas(name.c_str(), "MyMon Example Monitor", -1, 0, xsize / 2, ysize);
0047
0048
0049
0050
0051 gSystem->ProcessEvents();
0052 Pad[0] = new TPad("mypad1", "who needs this?", 0.1, 0.5, 0.9, 0.9, 0);
0053 Pad[1] = new TPad("mypad2", "who needs this?", 0.1, 0.05, 0.9, 0.45, 0);
0054 Pad[0]->Draw();
0055 Pad[1]->Draw();
0056
0057 transparent[0] = new TPad("transparent0", "this does not show", 0, 0, 1, 1);
0058 transparent[0]->SetFillStyle(4000);
0059 transparent[0]->Draw();
0060 TC[0]->SetEditable(false);
0061 }
0062 else if (name == "MyMon2")
0063 {
0064
0065 TC[1] = new TCanvas(name.c_str(), "MyMon2 Example Monitor", -xsize / 2, 0, xsize / 2, ysize);
0066 gSystem->ProcessEvents();
0067 Pad[2] = new TPad("mypad3", "who needs this?", 0.1, 0.5, 0.9, 0.9, 0);
0068 Pad[3] = new TPad("mypad4", "who needs this?", 0.1, 0.05, 0.9, 0.45, 0);
0069 Pad[2]->Draw();
0070 Pad[3]->Draw();
0071
0072 transparent[1] = new TPad("transparent1", "this does not show", 0, 0, 1, 1);
0073 transparent[1]->SetFillStyle(4000);
0074 transparent[1]->Draw();
0075 TC[1]->SetEditable(false);
0076 }
0077 else if (name == "MyMon3")
0078 {
0079 TC[2] = new TCanvas(name.c_str(), "MyMon3 Example Monitor", xsize / 2, 0, xsize / 2, ysize);
0080 gSystem->ProcessEvents();
0081 Pad[4] = new TPad("mypad5", "who needs this?", 0.1, 0.5, 0.9, 0.9, 0);
0082 Pad[5] = new TPad("mypad6", "who needs this?", 0.1, 0.05, 0.9, 0.45, 0);
0083 Pad[4]->Draw();
0084 Pad[5]->Draw();
0085
0086
0087
0088
0089
0090 }
0091 return 0;
0092 }
0093
0094 int MyMonDraw::Draw(const std::string &what)
0095 {
0096 int iret = 0;
0097 int idraw = 0;
0098 if (what == "ALL" || what == "FIRST")
0099 {
0100 iret += DrawFirst(what);
0101 idraw++;
0102 }
0103 if (what == "ALL" || what == "SECOND")
0104 {
0105 iret += DrawSecond(what);
0106 idraw++;
0107 }
0108 if (what == "ALL" || what == "HISTORY")
0109 {
0110 iret += DrawHistory(what);
0111 idraw++;
0112 }
0113 if (!idraw)
0114 {
0115 std::cout << __PRETTY_FUNCTION__ << " Unimplemented Drawing option: " << what << std::endl;
0116 iret = -1;
0117 }
0118 return iret;
0119 }
0120
0121 int MyMonDraw::DrawFirst(const std::string & )
0122 {
0123 OnlMonClient *cl = OnlMonClient::instance();
0124 TH1 *mymon_hist1 = cl->getHisto("MYMON_0","mymon_hist1");
0125 TH1 *mymon_hist2 = cl->getHisto("MYMON_1","mymon_hist1");
0126 if (!gROOT->FindObject("MyMon1"))
0127 {
0128 MakeCanvas("MyMon1");
0129 }
0130 TC[0]->SetEditable(true);
0131 TC[0]->Clear("D");
0132 Pad[0]->cd();
0133 if (mymon_hist1)
0134 {
0135 mymon_hist1->DrawCopy();
0136 }
0137 else
0138 {
0139 DrawDeadServer(transparent[0]);
0140 TC[0]->SetEditable(false);
0141 return -1;
0142 }
0143 Pad[1]->cd();
0144 if (mymon_hist2)
0145 {
0146 mymon_hist2->DrawCopy();
0147 }
0148 TText PrintRun;
0149 PrintRun.SetTextFont(62);
0150 PrintRun.SetTextSize(0.04);
0151 PrintRun.SetNDC();
0152 PrintRun.SetTextAlign(23);
0153 std::ostringstream runnostream;
0154 std::string runstring;
0155 std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
0156
0157 runnostream << ThisName << "_1 Run " << cl->RunNumber()
0158 << ", Time: " << ctime(&evttime.first);
0159 runstring = runnostream.str();
0160 transparent[0]->cd();
0161 PrintRun.SetTextColor(evttime.second);
0162 PrintRun.DrawText(0.5, 1., runstring.c_str());
0163 TC[0]->Update();
0164 TC[0]->Show();
0165 TC[0]->SetEditable(false);
0166 return 0;
0167 }
0168
0169 int MyMonDraw::DrawSecond(const std::string & )
0170 {
0171 OnlMonClient *cl = OnlMonClient::instance();
0172 TH1 *mymon_hist1 = cl->getHisto("MYMON_0","mymon_hist2");
0173 TH1 *mymon_hist2 = cl->getHisto("MYMON_1","mymon_hist2");
0174 if (!gROOT->FindObject("MyMon2"))
0175 {
0176 MakeCanvas("MyMon2");
0177 }
0178 TC[1]->SetEditable(true);
0179 TC[1]->Clear("D");
0180 Pad[2]->cd();
0181 if (mymon_hist1)
0182 {
0183 mymon_hist1->DrawCopy();
0184 }
0185 else
0186 {
0187 DrawDeadServer(transparent[1]);
0188 TC[1]->SetEditable(false);
0189 return -1;
0190 }
0191 Pad[3]->cd();
0192 if (mymon_hist2)
0193 {
0194 mymon_hist2->DrawCopy();
0195 }
0196 TText PrintRun;
0197 PrintRun.SetTextFont(62);
0198 PrintRun.SetTextSize(0.04);
0199 PrintRun.SetNDC();
0200 PrintRun.SetTextAlign(23);
0201 std::ostringstream runnostream;
0202 std::string runstring;
0203 std::pair<time_t,int> evttime = cl->EventTime("CURRENT");
0204
0205 runnostream << ThisName << "_2 Run " << cl->RunNumber()
0206 << ", Time: " << ctime(&evttime.first);
0207 runstring = runnostream.str();
0208 transparent[1]->cd();
0209 PrintRun.SetTextColor(evttime.second);
0210 PrintRun.DrawText(0.5, 1., runstring.c_str());
0211 TC[1]->Update();
0212 TC[1]->Show();
0213 TC[1]->SetEditable(false);
0214 return 0;
0215 }
0216
0217 int MyMonDraw::SavePlot(const std::string &what, const std::string &type)
0218 {
0219
0220 OnlMonClient *cl = OnlMonClient::instance();
0221 int iret = Draw(what);
0222 if (iret)
0223 {
0224 return iret;
0225 }
0226 int icnt = 0;
0227 for (TCanvas *canvas : TC)
0228 {
0229 if (canvas == nullptr)
0230 {
0231 continue;
0232 }
0233 icnt++;
0234 std::string filename = ThisName + "_" + std::to_string(icnt) + "_" +
0235 std::to_string(cl->RunNumber()) + "." + type;
0236 cl->CanvasToPng(canvas, filename);
0237 }
0238 return 0;
0239 }
0240
0241 int MyMonDraw::MakeHtml(const std::string &what)
0242 {
0243 int iret = Draw(what);
0244 if (iret)
0245 {
0246 return iret;
0247 }
0248
0249 OnlMonClient *cl = OnlMonClient::instance();
0250
0251 int icnt = 0;
0252 for (TCanvas *canvas : TC)
0253 {
0254 if (canvas == nullptr)
0255 {
0256 continue;
0257 }
0258 icnt++;
0259
0260 std::string pngfile = cl->htmlRegisterPage(*this, canvas->GetTitle(), std::to_string(icnt), "png");
0261 cl->CanvasToPng(canvas, pngfile);
0262 }
0263
0264
0265
0266 std::string logfile = cl->htmlRegisterPage(*this, "EXPERTS/Log", "log", "html");
0267 std::ofstream out(logfile.c_str());
0268 out << "<HTML><HEAD><TITLE>Log file for run " << cl->RunNumber()
0269 << "</TITLE></HEAD>" << std::endl;
0270 out << "<P>Some log file output would go here." << std::endl;
0271 out.close();
0272
0273 std::string status = cl->htmlRegisterPage(*this, "EXPERTS/Status", "status", "html");
0274 std::ofstream out2(status.c_str());
0275 out2 << "<HTML><HEAD><TITLE>Status file for run " << cl->RunNumber()
0276 << "</TITLE></HEAD>" << std::endl;
0277 out2 << "<P>Some status output would go here." << std::endl;
0278 out2.close();
0279 cl->SaveLogFile(*this);
0280 return 0;
0281 }
0282
0283 int MyMonDraw::DrawHistory(const std::string & )
0284 {
0285 int iret = 0;
0286
0287
0288 std::vector<float> var;
0289 std::vector<float> varerr;
0290 std::vector<time_t> timestamp;
0291 std::vector<int> runnumber;
0292 std::string varname = "mymondummy";
0293
0294 time_t begin = 0;
0295 time_t end = time(nullptr);
0296 iret = dbvars->GetVar(begin, end, varname, timestamp, runnumber, var, varerr);
0297 if (iret)
0298 {
0299 std::cout << __PRETTY_FUNCTION__ << " Error in db access" << std::endl;
0300 return iret;
0301 }
0302 if (!gROOT->FindObject("MyMon3"))
0303 {
0304 MakeCanvas("MyMon3");
0305 }
0306
0307 float *x = new float[var.size()];
0308 float *y = new float[var.size()];
0309 float *ex = new float[var.size()];
0310 float *ey = new float[var.size()];
0311 int n = var.size();
0312 for (unsigned int i = 0; i < var.size(); i++)
0313 {
0314
0315
0316
0317
0318
0319 x[i] = timestamp[i] - TimeOffsetTicks;
0320 y[i] = var[i];
0321 ex[i] = 0;
0322 ey[i] = varerr[i];
0323 }
0324 Pad[4]->cd();
0325 if (gr[0])
0326 {
0327 delete gr[0];
0328 }
0329 gr[0] = new TGraphErrors(n, x, y, ex, ey);
0330 gr[0]->SetMarkerColor(4);
0331 gr[0]->SetMarkerStyle(21);
0332 gr[0]->Draw("ALP");
0333 gr[0]->GetXaxis()->SetTimeDisplay(1);
0334 gr[0]->GetXaxis()->SetLabelSize(0.03);
0335
0336
0337
0338 gr[0]->GetXaxis()->SetNdivisions(-1006);
0339 gr[0]->GetXaxis()->SetTimeOffset(TimeOffsetTicks);
0340 gr[0]->GetXaxis()->SetTimeFormat("%Y/%m/%d %H:%M");
0341 delete[] x;
0342 delete[] y;
0343 delete[] ex;
0344 delete[] ey;
0345
0346 varname = "mymoncount";
0347 iret = dbvars->GetVar(begin, end, varname, timestamp, runnumber, var, varerr);
0348 if (iret)
0349 {
0350 std::cout << __PRETTY_FUNCTION__ << " Error in db access" << std::endl;
0351 return iret;
0352 }
0353 x = new float[var.size()];
0354 y = new float[var.size()];
0355 ex = new float[var.size()];
0356 ey = new float[var.size()];
0357 n = var.size();
0358 for (unsigned int i = 0; i < var.size(); i++)
0359 {
0360
0361
0362
0363
0364
0365 x[i] = timestamp[i] - TimeOffsetTicks;
0366 y[i] = var[i];
0367 ex[i] = 0;
0368 ey[i] = varerr[i];
0369 }
0370 Pad[5]->cd();
0371 if (gr[1])
0372 {
0373 delete gr[1];
0374 }
0375 gr[1] = new TGraphErrors(n, x, y, ex, ey);
0376 gr[1]->SetMarkerColor(4);
0377 gr[1]->SetMarkerStyle(21);
0378 gr[1]->Draw("ALP");
0379 gr[1]->GetXaxis()->SetTimeDisplay(1);
0380
0381
0382
0383 gr[1]->GetXaxis()->SetLabelSize(0.03);
0384 gr[1]->GetXaxis()->SetTimeOffset(TimeOffsetTicks);
0385 gr[1]->GetXaxis()->SetTimeFormat("%Y/%m/%d %H:%M");
0386
0387 delete[] x;
0388 delete[] y;
0389 delete[] ex;
0390 delete[] ey;
0391
0392 TC[2]->Update();
0393 return 0;
0394 }