Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnlMonClient.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnlMonClient.cc
1 #include "OnlMonClient.h"
2 #include "ClientHistoList.h"
3 #include "OnlMonDraw.h"
4 #include "OnlMonHtml.h"
5 
6 #include <onlmon/HistoBinDefs.h>
7 #include <onlmon/OnlMonBase.h> // for OnlMonBase
8 #include <onlmon/OnlMonDefs.h>
9 
10 #include <MessageTypes.h> // for kMESS_STRING, kMESS_OBJECT
11 #include <TCanvas.h>
12 #include <TDirectory.h>
13 #include <TFile.h>
14 #include <TGClient.h> // for gClient, TGClient
15 #include <TGFrame.h>
16 #include <TH1.h>
17 #include <TImage.h>
18 #include <TIterator.h>
19 #include <TList.h> // for TList
20 #include <TMessage.h>
21 #include <TROOT.h>
22 #include <TSeqCollection.h>
23 #include <TSocket.h>
24 #include <TStyle.h>
25 #include <TSystem.h>
26 
27 #include <odbc++/connection.h>
28 #include <odbc++/drivermanager.h>
29 #include <odbc++/resultset.h>
30 #include <odbc++/statement.h> // for Statement
31 #include <odbc++/types.h> // for SQLException
32 
33 #include <sys/stat.h>
34 #include <sys/utsname.h>
35 #include <unistd.h>
36 #include <uuid/uuid.h>
37 #include <algorithm>
38 #include <cstdio> // for printf, remove
39 #include <cstdlib> // for getenv, exit
40 #include <cstring> // for strcmp
41 #include <filesystem>
42 #include <fstream>
43 #include <iostream>
44 #include <list>
45 #include <sstream>
46 #include <utility> // for pair
47 
49 
50 int pinit()
51 {
52  return 0;
53 }
54 
56 {
57  if (__instance)
58  {
59  return __instance;
60  }
61  __instance = new OnlMonClient("ONLMONCLIENT");
62  return __instance;
63 }
64 
66  : OnlMonBase(name)
67 {
68  defaultStyle = new TStyle();
70  InitAll();
71 }
72 
74 {
75  if (gROOT->FindObject("ServerRunning"))
76  {
77  std::cout << "Don't run Server and Client in same session, exiting" << std::endl;
78  exit(1);
79  }
80  if (!gClient) // cannot draw on display, warn and bail out
81  {
82  const char *env_display = getenv("DISPLAY");
83  if (env_display)
84  {
85  std::string displaystring = env_display;
86  std::cout << "Cannot open Display, Display Env Var is set to "
87  << displaystring << std::endl;
88  if (displaystring.find("unix") != std::string::npos)
89  {
90  utsname ThisNode;
91  uname(&ThisNode);
92  std::cout << "presumably the virtual framebuffer is not running on " << ThisNode.nodename
93  << ", check if process /usr/X11R6/bin/Xvfb is alive" << std::endl;
94  }
95  else if (displaystring.find("localhost") != std::string::npos)
96  {
97  std::cout << "Check your ssh forwarding" << std::endl;
98  std::cout << "your $HOME/.ssh/config has to contain the line" << std::endl;
99  std::cout << "ForwardX11 yes" << std::endl;
100  }
101  }
102  else
103  {
104  std::cout << "Display not set, cannot continue" << std::endl;
105  }
106  exit(1);
107  }
108  // we have a working display
109  clientrunning = new TH1F("ClientRunning", "ClientRunning", 1, 0, 1);
110  if (getenv("ONLMON_HTMLDIR") == nullptr)
111  {
112  std::cout << "ONLMON_HTMLDIR not set, exiting" << std::endl;
113  exit(1);
114  }
115  fHtml = new OnlMonHtml(getenv("ONLMON_HTMLDIR"));
116 
117  TGFrame *rootWin = (TGFrame *) gClient->GetRoot();
118  display_sizex = rootWin->GetDefaultWidth();
119  display_sizey = rootWin->GetDefaultHeight();
120  for (int i = 0; i < kMAXSIGNALS; i++)
121  {
122  gSystem->IgnoreSignal((ESignals) i);
123  }
124  return;
125 }
126 
128 {
129  while (DrawerList.begin() != DrawerList.end())
130  {
131  if (verbosity > 0)
132  {
133  std::cout << "deleting " << DrawerList.begin()->first << std::endl;
134  }
135  delete DrawerList.begin()->second;
136  DrawerList.erase(DrawerList.begin());
137  }
138  while (Histo.begin() != Histo.end())
139  {
140  delete Histo.begin()->second;
141  Histo.erase(Histo.begin());
142  }
143  delete clientrunning;
144  delete fHtml;
145  delete defaultStyle;
146  // this deletes all open canvases
147  TSeqCollection *allCanvases = gROOT->GetListOfCanvases();
148  TCanvas *canvas = nullptr;
149  while ((canvas = static_cast<TCanvas *>(allCanvases->First())))
150  {
151  if (verbosity > 0)
152  {
153  std::cout << "Deleting Canvas " << canvas->GetName() << std::endl;
154  }
155  delete canvas;
156  }
157  __instance = nullptr;
158  return;
159 }
160 
161 void OnlMonClient::registerHisto(const std::string &hname, const std::string &subsys)
162 {
163  auto subsysiter = SubsysHisto.find(subsys);
164  if (subsysiter == SubsysHisto.end())
165  {
166  std::map<const std::string, ClientHistoList *> entry;
167  // c++11 map.insert returns pair<iterator,bool>
168  subsysiter = SubsysHisto.insert(std::make_pair(subsys, entry)).first;
169  if (Verbosity() > 2)
170  {
171  std::cout << "inserting " << subsys << " into SubsysHisto, readback: " << subsysiter->first << std::endl;
172  }
173  }
174  auto hiter = subsysiter->second.find(hname);
175  if (hiter == subsysiter->second.end())
176  {
177  ClientHistoList *newhisto = new ClientHistoList();
178  newhisto->SubSystem(subsys);
179  subsysiter->second.insert(std::make_pair(hname, newhisto));
180  if (Verbosity() > 0)
181  {
182  std::cout << "new histogram " << hname << " of subsystem " << subsys << std::endl;
183  }
184  }
185  else
186  {
187  hiter->second->SubSystem(subsys);
188  }
189  // register FrameWorkVars histo if we don't have it already
190  std::string frameworkhistoname = "FrameWorkVars";
191  hiter = subsysiter->second.find(frameworkhistoname);
192  if (hiter == subsysiter->second.end())
193  {
194  ClientHistoList *newhisto = new ClientHistoList();
195  newhisto->SubSystem(subsys);
196  subsysiter->second.insert(std::make_pair(frameworkhistoname, newhisto));
197  if (Verbosity() > 0)
198  {
199  std::cout << "new histogram " << frameworkhistoname << " of subsystem " << subsys << std::endl;
200  }
201  }
202  else
203  {
204  hiter->second->SubSystem(subsys);
205  }
206 
207  return;
208 }
209 
211 {
212  std::string mysubsys = subsys.substr(0,subsys.find('_'));
213  for (auto frwrkiter : m_MonitorFetchedSet)
214  {
215  if (frwrkiter.find(mysubsys) == std::string::npos)
216  {
217  m_MonitorFetchedSet.clear();
218  break;
219  }
220  }
221  int iret = 0;
222  std::map<const std::string, ClientHistoList *>::const_iterator histoiter;
223  std::map<const std::string, ClientHistoList *>::const_iterator histonewiter;
224  if (!IsMonitorRunning(subsys)) // test if saved monitor server is still running
225  {
226  // monitor is not running - remove its entry if it exists
227  auto moniiter = MonitorHostPorts.find(subsys);
228  if (moniiter != MonitorHostPorts.end())
229  {
230  MonitorHostPorts.erase(moniiter);
231  }
232  // Find monitor, if not found reset ClientHistoList entries for host and port
233  if (FindMonitor(subsys) == 0)
234  {
235  auto subsyshistos = SubsysHisto.find(subsys);
236  if (subsyshistos != SubsysHisto.end())
237  {
238  for (auto &hiter : subsyshistos->second)
239  {
240  hiter.second->ServerHost("UNKNOWN");
241  hiter.second->ServerPort(0);
242  if (hiter.second->Histo() != nullptr)
243  {
244  hiter.second->Histo()->Delete();
245  }
246  hiter.second->Histo(nullptr);
247  }
248  }
249  return iret;
250  }
251  }
252 
253  if (getall == 0)
254  {
255  std::map<std::string, std::map<const std::string, ClientHistoList *>>::const_iterator histos = SubsysHisto.find(subsys);
256  for (auto &hiter : histos->second)
257  {
258  if (requestHistoByName(subsys, hiter.first))
259  {
260  if (Verbosity() > 2)
261  {
262  std::cout << "Request for " << hiter.first << " on " << subsys << " failed " << std::endl;
263  }
264  if (hiter.second->Histo())
265  {
266  hiter.second->Histo()->Delete();
267  hiter.second->Histo(nullptr);
268  hiter.second->ServerHost("UNKNOWN");
269  hiter.second->ServerPort(0);
270  }
271  iret = -1;
272  }
273  }
274  }
275  else if (getall == 1)
276  {
277  std::map<std::string, std::list<std::string>> transferlist;
278  std::ostringstream host_port;
279 
280  int failed_to_locate = 0;
281  auto subs = SubsysHisto.find(subsys);
282  {
283  for (auto &histos : subs->second)
284  {
285  if (Verbosity() > 2)
286  {
287  std::cout << "checking for subsystem " << subs->first << ", histo " << histos.first << std::endl;
288  }
289  if (histos.second->SubSystem() == subsys)
290  {
291  int unknown_histo = 0;
292  std::string hname = histos.first;
293  if (histos.second->ServerHost() == "UNKNOWN")
294  {
295  if (!failed_to_locate)
296  {
297  if (LocateHistogram(hname, subsys) < 1)
298  {
299  if (Verbosity() > 2)
300  {
301  std::cout << "Subsystem " << subsys << " Histogram " << hname << " cannot be located" << std::endl;
302  }
303  failed_to_locate = 1;
304  unknown_histo = 1;
305  iret = -1;
306  }
307  }
308  else
309  {
310  unknown_histo = 1;
311  }
312  }
313  // reset histogram in case they don't exist on the server anymore
314  if (histos.second->Histo())
315  {
316  histos.second->Histo()->Reset();
317  }
318  if (!unknown_histo)
319  {
320  std::string fullhname = subsys + std::string(" ") + hname;
321  (transferlist[subsys]).push_back(fullhname);
322  }
323  }
324  }
325  }
326  std::list<std::string>::const_iterator liter;
327  for (auto listiter = transferlist.begin(); listiter != transferlist.end(); ++listiter)
328  {
329  std::list<std::string> hlist = listiter->second;
330  auto hostportiter = MonitorHostPorts.find(listiter->first);
331  if (hostportiter == MonitorHostPorts.end())
332  {
333  std::cout << __PRETTY_FUNCTION__ << "Cannot find MonitorHostPorts entry for " << listiter->first << std::endl;
334  std::cout << "existing hosts: " << std::endl;
335  for (auto &hport : MonitorHostPorts)
336  {
337  std::cout << "subsystem " << hport.first << " on host " << hport.second.first
338  << " listening on port " << hport.second.second << std::endl;
339  }
340  continue;
341  }
342  if (requestHistoList(listiter->first, hostportiter->second.first, hostportiter->second.second, hlist) != 0)
343  {
344  for (liter = hlist.begin(); liter != hlist.end(); ++liter)
345  {
346  if (requestHistoByName(*liter))
347  {
348  std::cout << "Request for " << *liter << " failed " << std::endl;
349  histoiter = Histo.find(*liter);
350  if (histoiter->second && histoiter->second->Histo())
351  {
352  histoiter->second->Histo()->Delete();
353  histoiter->second->Histo(nullptr);
354  histoiter->second->ServerHost("UNKNOWN");
355  histoiter->second->ServerPort(0);
356  }
357  }
358  }
359  }
360  }
361  }
362  else if (getall == 2)
363  {
364  const char *hname = "none";
365  for (histoiter = Histo.begin(); histoiter != Histo.end(); ++histoiter)
366  {
367  if (histoiter->second->SubSystem() == subsys)
368  {
369  hname = histoiter->first.c_str();
370  break;
371  }
372  }
373  // histoiter points here to first histogram of subsystem
374  if (histoiter != Histo.end())
375  {
376  std::string hostname = histoiter->second->ServerHost();
377  int moniport = histoiter->second->ServerPort();
378  {
379  if (hostname == "UNKNOWN")
380  {
381  if (LocateHistogram(hname, "") < 1)
382  {
383  std::cout << "Histogram " << hname << " cannot be located" << std::endl;
384  return -1;
385  }
386  histonewiter = Histo.find(hname);
387  hostname = histonewiter->second->ServerHost();
388  moniport = histonewiter->second->ServerPort();
389  }
390 
391  // Reset the histograms in case they don't exhist on the host.
392  for (histonewiter = Histo.begin(); histonewiter != Histo.end(); ++histonewiter)
393  {
394  if (histonewiter->second->SubSystem() == subsys)
395  {
396  if (histonewiter->second->Histo())
397  {
398  histonewiter->second->Histo()->Reset();
399  }
400  }
401  }
402 
403  if (requestHisto("ALL", hostname, moniport))
404  {
405  std::cout << "Request for all histograms from "
406  << hostname << " failed, trying single " << std::endl;
407  iret = requestHistoBySubSystem(subsys, 0);
408  }
409  }
410  }
411  else
412  {
413  std::cout << "No Histogram of subsystem "
414  << subsys << " registered" << std::endl;
415  }
416  }
417  m_MonitorFetchedSet.insert(subsys);
418  return iret;
419 }
420 
422 {
423  std::map<const std::string, OnlMonDraw *>::iterator iter = DrawerList.find(Drawer->Name());
424  if (iter != DrawerList.end())
425  {
426  std::cout << "Drawer " << Drawer->Name() << " already registered, I won't overwrite it" << std::endl;
427  std::cout << "Use a different name and try again" << std::endl;
428  }
429  else
430  {
431  DrawerList[Drawer->Name()] = Drawer;
432  Drawer->Init();
434  }
435  return;
436 }
437 
438 int OnlMonClient::Draw(const char *who, const char *what)
439 {
440  int iret = DoSomething(who, what, "DRAW");
441  // gSystem->ProcessEvents();
442  return iret;
443 }
444 
445 int OnlMonClient::SavePlot(const std::string &who, const std::string &what)
446 {
447  int iret = DoSomething(who, what, "SAVEPLOT");
448  TSeqCollection *allCanvases = gROOT->GetListOfCanvases();
449  TCanvas *canvas = nullptr;
450  while ((canvas = static_cast<TCanvas *>(allCanvases->First())))
451  {
452  if (verbosity > 0)
453  {
454  std::cout << "Deleting Canvas " << canvas->GetName() << std::endl;
455  }
456  delete canvas;
457  }
458  // gSystem->ProcessEvents();
459  return iret;
460 }
461 
462 int OnlMonClient::MakePS(const char *who, const char *what)
463 {
464  int iret = DoSomething(who, what, "PS");
465  // gSystem->ProcessEvents();
466  return iret;
467 }
468 
469 int OnlMonClient::MakeHtml(const char *who, const char *what)
470 {
471  mode_t old_umask;
472  int runno = RunNumber();
473  if (runno <= 0)
474  {
475  std::cout << "Run Number too small: " << runno
476  << " not creating html output" << std::endl;
477  return 0;
478  }
479  char *onlmon_real_html = getenv("ONLMON_REAL_HTML");
480  if (!onlmon_real_html)
481  {
482  old_umask = umask(S_IWOTH);
483  std::cout << "Making html output group writable so others can run tests as well" << std::endl;
484  }
485  fHtml->runNumber(runno); // do not forget this !
486  int iret = DoSomething(who, what, "HTML");
487 
488  if (!onlmon_real_html)
489  {
490  umask(old_umask);
491  }
492  // gSystem->ProcessEvents();
493  return iret;
494 }
495 
496 int OnlMonClient::DoSomething(const std::string &who, const std::string &what, const std::string &opt)
497 {
498  std::map<const std::string, OnlMonDraw *>::iterator iter;
499  if (who != "ALL") // is drawer was specified (!All)
500  {
501  iter = DrawerList.find(who);
502  if (iter != DrawerList.end())
503  {
504  if (opt == "DRAW")
505  {
506  iter->second->Draw(what);
507  }
508  else if (opt == "PS")
509  {
510  iter->second->MakePS(what);
511  }
512  else if (opt == "SAVEPLOT")
513  {
514  iter->second->SavePlot(what, "png");
515  }
516  else if (opt == "HTML")
517  {
518  if (verbosity > 0)
519  {
520  std::cout << __PRETTY_FUNCTION__ << " creating html output for "
521  << iter->second->Name() << std::endl;
522  }
523  if (iter->second->MakeHtml(what))
524  {
525  std::cout << "subsystem " << iter->second->Name()
526  << " not in root file, skipping" << std::endl;
527  }
528  }
529  else
530  {
531  std::cout << "option " << opt << " not implemented" << std::endl;
532  return 0;
533  }
535  return 0;
536  }
537  else
538  {
539  std::cout << "Drawer " << who << " not in list" << std::endl;
540  Print("DRAWER");
541  return -1;
542  }
543  }
544  else
545  {
546  for (iter = DrawerList.begin(); iter != DrawerList.end(); ++iter)
547  {
548  if (opt == "DRAW")
549  {
550  iter->second->Draw(what);
551  }
552  else if (opt == "PS")
553  {
554  iter->second->MakePS(what);
555  }
556  else if (opt == "HTML")
557  {
558  if (verbosity > 0)
559  {
560  std::cout << __PRETTY_FUNCTION__ << " creating html output for "
561  << iter->second->Name() << std::endl;
562  }
563  gROOT->Reset();
564  int iret = iter->second->MakeHtml(what);
565  if (iret)
566  {
567  std::cout << "subsystem " << iter->second->Name()
568  << " not in root file, skipping" << std::endl;
569  // delete all canvases (no more piling up of 50 canvases)
570  // if run for a single subsystem this leaves the canvas intact
571  // for debugging
572  TSeqCollection *allCanvases = gROOT->GetListOfCanvases();
573  TCanvas *canvas = nullptr;
574  while ((canvas = static_cast<TCanvas *>(allCanvases->First())))
575  {
576  std::cout << "Deleting Canvas " << canvas->GetName() << std::endl;
577  delete canvas;
578  }
579  }
580  }
582  }
583  }
584  return 0;
585 }
586 
588 {
589  std::string hostname = "UNKNOWN";
590  int moniport = OnlMonDefs::MONIPORT;
591  std::map<std::string, std::map<const std::string, ClientHistoList *>>::const_iterator histos = SubsysHisto.find(subsys);
592  auto histoiter = histos->second.find(what);
593  if (Verbosity() > 2)
594  {
595  std::cout << __PRETTY_FUNCTION__ << "checking for " << what << " on monitor " << subsys << std::endl;
596  }
597  if (histoiter != histos->second.end())
598  {
599  hostname = histoiter->second->ServerHost();
600  moniport = histoiter->second->ServerPort();
601  if (hostname == "UNKNOWN")
602  {
603  if (LocateHistogram(what, subsys) < 1)
604  {
605  std::cout << "Histogram " << what << " cannot be located" << std::endl;
606  return -1;
607  }
608  // search again since LocateHistogram can change the map
609  histoiter = histos->second.find(what);
610  hostname = histoiter->second->ServerHost();
611  moniport = histoiter->second->ServerPort();
612  if (hostname == "UNKNOWN")
613  {
614  std::cout << __PRETTY_FUNCTION__ << "host UNKNOWN for whatever reason" << std::endl;
615  return -3;
616  }
617  }
618  }
619  else
620  {
621  if (LocateHistogram(what, subsys) < 1)
622  {
623  std::cout << "Histogram " << what << " cannot be located" << std::endl;
624  return -2;
625  }
626  histoiter = histos->second.find(what);
627  if (histoiter != histos->second.end())
628  {
629  hostname = histoiter->second->ServerHost();
630  moniport = histoiter->second->ServerPort();
631  if (hostname == "UNKNOWN")
632  {
633  std::cout << __PRETTY_FUNCTION__ << "host UNKNOWN for whatever reason" << std::endl;
634  return -3;
635  }
636  }
637  else
638  {
639  std::cout << __PRETTY_FUNCTION__ << "Problem determining host" << std::endl;
640  }
641  }
642  // Open connection to server
643  TSocket sock(hostname.c_str(), moniport);
644  TMessage *mess;
645  std::string fullhistoname = subsys + std::string(" ") + what;
646  if (Verbosity() > 2)
647  {
648  std::cout << __PRETTY_FUNCTION__ << " sending " << fullhistoname << " to " << hostname << " port " << moniport << std::endl;
649  }
650  sock.Send(fullhistoname.c_str());
651  while (true)
652  {
653  if (verbosity > 1)
654  {
655  std::cout << __PRETTY_FUNCTION__ << "Waiting for Message from : " << hostname
656  << " on port " << moniport << std::endl;
657  }
658  sock.Recv(mess);
659  if (!mess) // if server is not up mess is NULL
660  {
661  std::cout << __PRETTY_FUNCTION__ << "Server not running on " << hostname << std::endl;
662  sock.Close();
663  return 1;
664  }
665  if (mess->What() == kMESS_STRING)
666  {
667  char str[OnlMonDefs::MSGLEN];
668  mess->ReadString(str, OnlMonDefs::MSGLEN);
669  delete mess;
670  if (verbosity > 1)
671  {
672  std::cout << __PRETTY_FUNCTION__ << "Message: " << str << std::endl;
673  }
674  if (!strcmp(str, "Finished"))
675  {
676  break;
677  }
678  else if (!strcmp(str, "UnknownHisto"))
679  {
680  break;
681  }
682  else
683  {
684  std::cout << __PRETTY_FUNCTION__ << "Unknown Text Message: " << str << std::endl;
685  sock.Send("Ack");
686  }
687  }
688  else if (mess->What() == kMESS_OBJECT)
689  {
690  TH1 *histo = static_cast<TH1 *>(mess->ReadObjectAny(mess->GetClass()));
691  delete mess;
692  TH1 *maphist = static_cast<TH1 *>(histo->Clone(histo->GetName()));
693  if (verbosity > 1)
694  {
695  std::cout << __PRETTY_FUNCTION__ << "histoname: " << histo->GetName() << " at "
696  << histo << std::endl;
697  }
698 
699  updateHistoMap(subsys, histo->GetName(), maphist);
700  delete histo;
701  sock.Send("Ack");
702  }
703  }
704  sock.Send("Finished"); // tell server we are finished
705  // Close the socket
706  sock.Close();
707  return 0;
708 }
709 
710 int OnlMonClient::requestHisto(const std::string &what, const std::string &hostname, const int moniport)
711 {
712  // Open connection to server
713  TSocket sock(hostname.c_str(), moniport);
714  TMessage *mess;
715  sock.Send(what.c_str());
716  while (true)
717  {
718  sock.Recv(mess);
719  if (!mess) // if server is not up mess is NULL
720  {
721  std::cout << __PRETTY_FUNCTION__ << "Server not running on " << hostname << std::endl;
722  sock.Close();
723  return 1;
724  }
725  if (mess->What() == kMESS_STRING)
726  {
727  char str[OnlMonDefs::MSGLEN];
728  mess->ReadString(str, OnlMonDefs::MSGLEN);
729  delete mess;
730  if (verbosity > 1)
731  {
732  std::cout << __PRETTY_FUNCTION__ << "Message: " << str << std::endl;
733  }
734 
735  if (!strcmp(str, "Finished"))
736  {
737  break;
738  }
739  else if (!strcmp(str, "UnknownHisto"))
740  {
741  break;
742  }
743  else
744  {
745  std::cout << __PRETTY_FUNCTION__ << "Unknown Text Message: " << str << std::endl;
746  sock.Send("Ack");
747  }
748  }
749  else if (mess->What() == kMESS_OBJECT)
750  {
751  // this reads the message and allocate space for new histogram
752  TH1 *histo = static_cast<TH1 *>(mess->ReadObjectAny(mess->GetClass()));
753  delete mess;
754  if (verbosity > 1)
755  {
756  std::cout << __PRETTY_FUNCTION__ << "histoname: " << histo->GetName() << " at "
757  << histo << std::endl;
758  }
759 
760  updateHistoMap(what, histo->GetName(), histo);
761  sock.Send("Ack");
762  }
763  }
764  sock.Send("Finished"); // tell server we are finished
765 
766  // Close the socket
767  sock.Close();
768  return 0;
769 }
770 
771 int OnlMonClient::requestMonitorList(const std::string &hostname, const int moniport)
772 {
773  TSocket sock(hostname.c_str(), moniport);
774  TMessage *mess;
775  sock.Send("LISTMONITORS");
776  sock.Recv(mess);
777  if (!mess) // if server is not up mess is NULL
778  {
779  std::cout << __PRETTY_FUNCTION__ << "Server not running on " << hostname << std::endl;
780  sock.Close();
781  return 1;
782  }
783  delete mess;
784  while (true)
785  {
786  sock.Recv(mess);
787  if (mess->What() == kMESS_STRING)
788  {
789  char strmess[OnlMonDefs::MSGLEN];
790  mess->ReadString(strmess, OnlMonDefs::MSGLEN);
791  delete mess;
792  if (Verbosity() > 1)
793  {
794  std::cout << "received " << strmess << std::endl;
795  }
796  std::string str(strmess);
797  if (str == "Finished")
798  {
799  break;
800  }
801  if (Verbosity() > 2)
802  {
803  std::cout << "inserting " << str << " on host " << hostname
804  << " listening to " << moniport << std::endl;
805  }
806  MonitorHostPorts.insert(std::make_pair(str, std::make_pair(hostname, moniport)));
807  }
808  else
809  {
810  std::cout << "requestMonitorList: received unexpected message type: " << mess->What() << std::endl;
811  break;
812  }
813  }
814  sock.Send("Finished"); // tell server we are finished
815 
816  // Close the socket
817  sock.Close();
818  return 0;
819 }
820 
821 int OnlMonClient::requestHistoList(const std::string &subsys, const std::string &hostname, const int moniport, std::list<std::string> &histolist)
822 {
823  // Open connection to server
824  TSocket sock(hostname.c_str(), moniport);
825  TMessage *mess;
826  sock.Send("LIST");
827  std::list<std::string>::const_iterator listiter;
828  sock.Recv(mess);
829  if (!mess) // if server is not up mess is NULL
830  {
831  std::cout << __PRETTY_FUNCTION__ << "Server not running on " << hostname << std::endl;
832  sock.Close();
833  return 1;
834  }
835 
836  delete mess;
837  for (listiter = histolist.begin(); listiter != histolist.end(); ++listiter)
838  {
839  if (Verbosity() > 2)
840  {
841  std::cout << __PRETTY_FUNCTION__ << "asking for " << *listiter << std::endl;
842  }
843  sock.Send((*listiter).c_str());
844  sock.Recv(mess);
845  if (!mess)
846  {
847  std::cout << __PRETTY_FUNCTION__ << "Server shut down during getting histo list" << std::endl;
848  sock.Close();
849  return 1;
850  }
851  if (mess->What() == kMESS_STRING)
852  {
853  char str[OnlMonDefs::MSGLEN];
854  mess->ReadString(str, OnlMonDefs::MSGLEN);
855  delete mess;
856  if (verbosity > 1)
857  {
858  std::cout << __PRETTY_FUNCTION__ << "Message: " << str << std::endl;
859  }
860 
861  if (!strcmp(str, "Ack"))
862  {
863  break;
864  }
865  else if (!strcmp(str, "UnknownHisto"))
866  {
867  break;
868  }
869  else
870  {
871  std::cout << __PRETTY_FUNCTION__ << "Unknown Text Message: " << str << std::endl;
872  }
873  }
874  else if (mess->What() == kMESS_OBJECT)
875  {
876  // this reads the message and allocate space for new histogram
877  TH1 *histo = static_cast<TH1 *>(mess->ReadObjectAny(mess->GetClass()));
878  delete mess;
879  if (verbosity > 1)
880  {
881  std::cout << __PRETTY_FUNCTION__ << "histoname: " << histo->GetName() << " at "
882  << histo << std::endl;
883  }
884 
885  updateHistoMap(subsys, histo->GetName(), histo);
886  }
887  }
888  sock.Send("alldone");
889  sock.Recv(mess);
890  delete mess;
891  sock.Send("Finished"); // tell server we are finished
892 
893  // Close the socket
894  sock.Close();
895  return 0;
896 }
897 
898 void OnlMonClient::updateHistoMap(const std::string &subsys, const std::string &hname, TH1 *h1d)
899 {
900  auto subsysiter = SubsysHisto.find(subsys);
901  if (subsysiter == SubsysHisto.end())
902  {
903  std::map<const std::string, ClientHistoList *> newmap;
904  ClientHistoList *entry = new ClientHistoList(subsys);
905  entry->Histo(h1d);
906  newmap.insert(std::make_pair(hname, entry));
907  SubsysHisto.insert(std::make_pair(subsys, newmap));
908  return;
909  }
910  auto histoiter = subsysiter->second.find(hname);
911  // std::map<const std::string, ClientHistoList *>::const_iterator histoiter = Histo.find(hname);
912  if (histoiter != subsysiter->second.end())
913  {
914  if (Verbosity() > 2)
915  {
916  std::cout << "deleting histogram " << hname << " at " << Histo[hname] << std::endl;
917  }
918  delete histoiter->second->Histo(); // delete old histogram
919  histoiter->second->Histo(h1d);
920  }
921  else
922  {
923  ClientHistoList *newhisto = new ClientHistoList(subsys);
924  newhisto->Histo(h1d);
925  subsysiter->second.insert(std::make_pair(hname, newhisto));
926  if (Verbosity() > 2)
927  {
928  std::cout << "new histogram " << hname << " at " << newhisto->Histo() << std::endl;
929  }
930  }
931  return;
932 }
933 
934 OnlMonDraw *
936 {
937  std::map<const std::string, OnlMonDraw *>::iterator iter = DrawerList.find(name);
938  if (iter != DrawerList.end())
939  {
940  return iter->second;
941  }
942  std::cout << "Could not locate drawer" << name << std::endl;
943  return nullptr;
944 }
945 
946 TH1 *OnlMonClient::getHisto(const std::string &monitor, const std::string &hname)
947 {
948  auto subsysiter = SubsysHisto.find(monitor);
949  if (subsysiter == SubsysHisto.end())
950  {
951  return nullptr;
952  }
953  auto hiter = subsysiter->second.find(hname);
954  if (hiter == subsysiter->second.end())
955  {
956  return nullptr;
957  }
958  return hiter->second->Histo();
959 }
960 
961 void OnlMonClient::Print(const char *what)
962 {
963  if (!strcmp(what, "ALL") || !strcmp(what, "DRAWER"))
964  {
965  // loop over the map and print out the content (name and location in memory)
966  std::cout << "--------------------------------------" << std::endl
967  << std::endl;
968  std::cout << "List of Drawers in OnlMonClient:" << std::endl;
969 
970  std::map<const std::string, OnlMonDraw *>::const_iterator hiter;
971  for (hiter = DrawerList.begin(); hiter != DrawerList.end(); ++hiter)
972  {
973  std::cout << hiter->first << " is at " << hiter->second << std::endl;
974  }
975  std::cout << std::endl;
976  }
977  if (!strcmp(what, "ALL") || !strcmp(what, "SERVERS"))
978  {
979  // loop over the map and print out the content (name and location in memory)
980  std::cout << "--------------------------------------" << std::endl
981  << std::endl;
982  std::cout << "List of Servers in OnlMonClient:" << std::endl;
983 
984  std::vector<std::string>::iterator hostiter;
985  for (hostiter = MonitorHosts.begin(); hostiter != MonitorHosts.end(); ++hostiter)
986  {
987  std::cout << "ServerHost: " << *hostiter << std::endl;
988  }
989  }
990  if (!strcmp(what, "ALL") || !strcmp(what, "MONITORS"))
991  {
992  // loop over the map and print out the content (name and location in memory)
993  std::cout << "--------------------------------------" << std::endl
994  << std::endl;
995  std::cout << "List of Monitors in OnlMonClient:" << std::endl;
996 
997  for (auto &moniiter : MonitorHostPorts)
998  {
999  std::cout << "Monitor " << moniiter.first << " runs on " << moniiter.second.first
1000  << " listening to port " << moniiter.second.second << std::endl;
1001  }
1002  }
1003  if (!strcmp(what, "ALL") || !strcmp(what, "HISTOS"))
1004  {
1005  // loop over the map and print out the content (name and location in memory)
1006  std::cout << "--------------------------------------" << std::endl
1007  << std::endl;
1008  std::cout << "List of Histograms in OnlMonClient:" << std::endl;
1009 
1010  std::map<const std::string, ClientHistoList *>::const_iterator hiter;
1011  for (hiter = Histo.begin(); hiter != Histo.end(); ++hiter)
1012  {
1013  std::cout << hiter->first << " Address " << hiter->second->Histo()
1014  << " on host " << hiter->second->ServerHost()
1015  << " port " << hiter->second->ServerPort()
1016  << ", subsystem " << hiter->second->SubSystem() << std::endl;
1017  }
1018  std::cout << std::endl;
1019  for (auto &subs : SubsysHisto)
1020  {
1021  auto subiter = MonitorHostPorts.find(subs.first);
1022  if (subiter != MonitorHostPorts.end())
1023  {
1024  std::cout << "Subsystem " << subs.first << " runs on " << subiter->second.first;
1025  std::cout << " on port " << subiter->second.second << std::endl;
1026  }
1027  for (auto &histos : subs.second)
1028  std::cout << histos.first << " @ " << subs.first
1029  << " Address " << histos.second->Histo()
1030  << " on host " << histos.second->ServerHost()
1031  << " port " << histos.second->ServerPort()
1032  << ", subsystem " << histos.second->SubSystem() << std::endl;
1033  }
1034  std::cout << std::endl;
1035  }
1036  if (!strcmp(what, "ALL") || !strcmp(what, "UNKNOWN"))
1037  {
1038  // loop over the map and print out the content (name and location in memory)
1039  std::cout << "--------------------------------------" << std::endl
1040  << std::endl;
1041  std::cout << "List of Unknown Histograms in OnlMonClient:" << std::endl;
1042 
1043  std::map<const std::string, ClientHistoList *>::const_iterator hiter;
1044  for (hiter = Histo.begin(); hiter != Histo.end(); ++hiter)
1045  {
1046  if (hiter->second->ServerHost() == "UNKNOWN" ||
1047  hiter->second->SubSystem() == "UNKNOWN")
1048  {
1049  std::cout << hiter->first << " Address " << hiter->second->Histo()
1050  << " on host " << hiter->second->ServerHost()
1051  << " port " << hiter->second->ServerPort()
1052  << ", subsystem " << hiter->second->SubSystem() << std::endl;
1053  }
1054  }
1055  std::cout << std::endl;
1056  }
1057  return;
1058 }
1059 
1060 int OnlMonClient::UpdateServerHistoMap(const std::string &hname, const std::string &subsys, const std::string &hostname)
1061 {
1062  // Open connection to server
1063  std::string searchstring = subsys + ' ' + hname;
1064  int MoniPort = OnlMonDefs::MONIPORT;
1065  int foundit = 0;
1066  do
1067  {
1068  std::cout << "Connecting to " << hostname << ", if it is frozen here - this is the one you need to restart" << std::endl;
1069  TSocket sock(hostname.c_str(), MoniPort);
1070  TMessage *mess;
1071  if (verbosity > 0)
1072  {
1073  std::cout << "UpdateServerHistoMap: sending cmd HistoList to "
1074  << hostname << " on port "
1075  << MoniPort
1076  << std::endl;
1077  }
1078  if (!sock.Send("HistoList"))
1079  {
1080  std::cout << "Server not running on " << hostname
1081  << " port " << MoniPort << std::endl;
1082  goto noserver;
1083  }
1084  while (true)
1085  {
1086  if (verbosity > 0)
1087  {
1088  std::cout << "UpdateServerHistoMap: waiting for response on "
1089  << hostname << " on port "
1090  << MoniPort
1091  << std::endl;
1092  }
1093  sock.Recv(mess);
1094  if (!mess) // if server is not up mess is NULL
1095  {
1096  std::cout << "UpdateServerHistoMap: No Recv, Server not running on "
1097  << hostname
1098  << " on port " << MoniPort << std::endl;
1099  goto noserver;
1100  }
1101  if (mess->What() == kMESS_STRING)
1102  {
1103  char strchr[OnlMonDefs::MSGLEN];
1104  mess->ReadString(strchr, OnlMonDefs::MSGLEN);
1105  delete mess;
1106  std::string str = strchr;
1107  if (verbosity > 0)
1108  {
1109  std::cout << "UpdateServerHistoMap Response: " << str << std::endl;
1110  }
1111  if (str == "Finished")
1112  {
1113  break;
1114  }
1115 
1116  if (str == searchstring)
1117  {
1118  std::cout << "found subsystem " << subsys << " histo " << hname << std::endl;
1119  foundit = 1;
1120  }
1121  unsigned int pos_space = str.find(' ');
1122  PutHistoInMap(str.substr(pos_space + 1, str.size()), str.substr(0, pos_space), hostname, MoniPort);
1123  sock.Send("Ack");
1124  }
1125  }
1126  sock.Send("Finished"); // tell server we are finished
1127 
1128  // Close the socket
1129  noserver:
1130  sock.Close();
1131  if (foundit)
1132  {
1133  return foundit;
1134  }
1135  MoniPort++;
1136  } while ((MoniPort - OnlMonDefs::MONIPORT) < OnlMonDefs::NUMMONIPORT); // no more than NUMMONIPORT parallel servers
1137  return foundit;
1138 }
1139 
1140 void OnlMonClient::PutHistoInMap(const std::string &hname, const std::string &subsys, const std::string &hostname, const int port)
1141 {
1142  auto hiter = SubsysHisto.find(subsys);
1143  if (hiter == SubsysHisto.end())
1144  {
1145  std::map<const std::string, ClientHistoList *> entry;
1146  // c++11 map.insert returns pair<iterator,bool>
1147  hiter = SubsysHisto.insert(std::make_pair(subsys, entry)).first;
1148  }
1149  auto histoiter = hiter->second.find(hname);
1150  if (histoiter != hiter->second.end())
1151  {
1152  histoiter->second->ServerHost(hostname);
1153  histoiter->second->ServerPort(port);
1154  }
1155  else
1156  {
1157  ClientHistoList *newhisto = new ClientHistoList();
1158  newhisto->ServerHost(hostname);
1159  newhisto->ServerPort(port);
1160  hiter->second.insert(std::make_pair(hname, newhisto));
1161  }
1162  return;
1163 }
1164 
1166 {
1167  if (find(MonitorHosts.begin(), MonitorHosts.end(), hostname) != MonitorHosts.end())
1168  {
1169  std::cout << "Host " << hostname << " already in list" << std::endl;
1170  }
1171  else
1172  {
1173  MonitorHosts.emplace_back(hostname);
1174  }
1175  return;
1176 }
1177 
1179 {
1180  for (auto &hostiter : MonitorHosts)
1181  {
1182  if (UpdateServerHistoMap(hname, subsys, hostiter) > 0)
1183  {
1184  return 1;
1185  }
1186  }
1187  return 0;
1188 }
1189 
1191 {
1192  int runno = -9999;
1193  for (auto frwrkiter : m_MonitorFetchedSet)
1194  {
1195  TH1 *frameworkvars = getHisto(frwrkiter, "FrameWorkVars");
1196  if (frameworkvars)
1197  {
1198  runno = std::max(runno, (int) frameworkvars->GetBinContent(RUNNUMBERBIN));
1199  }
1200  }
1201  return (runno);
1202 }
1203 
1205 {
1206  time_t tret = 0;
1207  for (auto frwrkiter : m_MonitorFetchedSet)
1208  {
1209  tret = std::max(tret, EventTime(frwrkiter,which));
1210  }
1211 
1212  if (verbosity > 0)
1213  {
1214  std::cout << "Time is " << ctime(&tret) << std::endl;
1215  }
1216  return (tret);
1217 }
1218 
1219 time_t OnlMonClient::EventTime(const std::string &servername, const std::string &which)
1220 {
1221  time_t tret = 0;
1222  int ibin = 0;
1223  if (which == "BOR")
1224  {
1225  ibin = BORTIMEBIN;
1226  }
1227  else if (which == "CURRENT")
1228  {
1229  ibin = CURRENTTIMEBIN;
1230  }
1231  else if (which == "EOR")
1232  {
1233  ibin = EORTIMEBIN;
1234  }
1235  else
1236  {
1237  std::cout << "Bad Option for Time: " << which
1238  << ", implemented are BOR EOR CURRENT" << std::endl;
1239  ibin = CURRENTTIMEBIN;
1240  }
1241  TH1 *frameworkvars = getHisto(servername, "FrameWorkVars");
1242  if (frameworkvars == nullptr)
1243  {
1244  tret = 0;
1245  }
1246  else
1247  {
1248  tret = (time_t) frameworkvars->GetBinContent(ibin);
1249  }
1250  if (verbosity > 0)
1251  {
1252  std::cout << "Time is " << ctime(&tret) << std::endl;
1253  }
1254  return (tret);
1255 }
1256 
1258 {
1259  std::string subsys = ExtractSubsystem(filename);
1260  TDirectory *save = gDirectory; // save current dir (which will be overwritten by the following fileopen)
1261  TFile *histofile = new TFile(filename.c_str(), "READ");
1262  if (!histofile)
1263  {
1264  std::cout << "Can't open " << filename << std::endl;
1265  return -1;
1266  }
1267  save->cd();
1268  TIterator *titer = histofile->GetListOfKeys()->MakeIterator();
1269  TObject *obj;
1270  TH1 *histo, *histoptr;
1271  while ((obj = titer->Next()))
1272  {
1273  if (verbosity > 0)
1274  {
1275  std::cout << "TObject at " << obj << std::endl;
1276  std::cout << obj->GetName() << std::endl;
1277  std::cout << obj->ClassName() << std::endl;
1278  }
1279  histofile->GetObject(obj->GetName(), histoptr);
1280  if (histoptr)
1281  {
1282  histo = static_cast<TH1 *>(histoptr->Clone());
1283  updateHistoMap(subsys, histo->GetName(), histo);
1284  if (verbosity > 0)
1285  {
1286  std::cout << "HistoName: " << histo->GetName() << std::endl;
1287  std::cout << "HistoClass: " << histo->ClassName() << std::endl;
1288  }
1289  }
1290  }
1291  delete histofile;
1292  delete titer;
1293  return 0;
1294 }
1295 
1296 int OnlMonClient::SendCommand(const char *hostname, const int port, const char *cmd)
1297 {
1298  // Open connection to server
1299  TSocket sock(hostname, port);
1300  TMessage *mess;
1301  if (!sock.Send(cmd))
1302  {
1303  std::cout << "Server not running on " << hostname
1304  << " port " << port << std::endl;
1305  sock.Close();
1306  return -1;
1307  }
1308  while (true)
1309  {
1310  sock.Recv(mess);
1311  if (!mess) // if server is not up mess is NULL
1312  {
1313  std::cout << __PRETTY_FUNCTION__ << "No Recv, Server not running on " << hostname
1314  << " on port " << port << std::endl;
1315  sock.Close();
1316  return -1;
1317  }
1318  if (mess->What() == kMESS_STRING)
1319  {
1320  char str[OnlMonDefs::MSGLEN];
1321  mess->ReadString(str, OnlMonDefs::MSGLEN);
1322  delete mess;
1323  if (verbosity > 1)
1324  {
1325  std::cout << __PRETTY_FUNCTION__ << " Message: " << str << std::endl;
1326  }
1327 
1328  if (!strcmp(str, "Finished"))
1329  {
1330  break;
1331  }
1332  }
1333  }
1334  sock.Send("Finished"); // tell server we are finished
1335 
1336  // Close the socket
1337  sock.Close();
1338  return 0;
1339 }
1340 
1341 //_____________________________________________________________________________
1343 {
1344  verbosity = v;
1345  if (fHtml)
1346  {
1347  fHtml->verbosity(v);
1348  }
1349 }
1350 
1351 //_____________________________________________________________________________
1353  const std::string &path,
1354  const std::string &relfilename)
1355 {
1356  fHtml->addMenu(drawer.Name(), path, relfilename);
1357 }
1358 
1359 //_____________________________________________________________________________
1361  const std::string &basefilename,
1362  const std::string &ext,
1363  std::string &fullfilename,
1365 {
1366  fHtml->namer(drawer.Name(), basefilename, ext, fullfilename, filename);
1367 }
1368 
1369 //_____________________________________________________________________________
1372  const std::string &path,
1373  const std::string &basefilename,
1374  const std::string &ext)
1375 {
1376  return fHtml->registerPage(drawer.Name(), path, basefilename, ext);
1377 }
1378 
1379 int OnlMonClient::CanvasToPng(TCanvas *canvas, std::string const &pngfilename)
1380 {
1381  // in order not to collide when running multiple html generators
1382  // create a unique filename (okay tempnam is not totally safe against
1383  // multiple procs getting the same name but the local /tmp filesystem should
1384  // prevent at least multiple machines colliding)
1385  // returned char array needs to be free'd after use
1386  if (!canvas)
1387  {
1388  std::cout << __PRETTY_FUNCTION__ << " TCanvas is Null Pointer" << std::endl;
1389  return -2;
1390  }
1391  if (pngfilename.empty())
1392  {
1393  std::cout << __PRETTY_FUNCTION__ << " emtpy png filename, not saving TCanvas "
1394  << canvas->GetName() << std::endl;
1395  return -1;
1396  }
1397  uuid_t uu;
1398  uuid_generate(uu);
1399  char uuid[50];
1400  uuid_unparse(uu, uuid);
1401  std::string tmpname = "/tmp/TC" + std::string(uuid);
1402  canvas->Print(tmpname.c_str(), "gif"); // write gif format
1403  TImage *img = TImage::Open(tmpname.c_str());
1404  img->WriteImage(pngfilename.c_str());
1405  delete img;
1406  if (remove(tmpname.c_str()))
1407  {
1408  std::cout << "Error removing " << tmpname << std::endl;
1409  }
1410  return 0;
1411 }
1412 
1413 int OnlMonClient::HistoToPng(TH1 *histo, std::string const &pngfilename, const char *drawopt, const int statopt)
1414 {
1415  TCanvas *cgiCanv = new TCanvas("cgiCanv", "cgiCanv", 200, 200, 650, 500);
1416  gStyle->SetOptStat(statopt);
1417  cgiCanv->SetFillColor(0);
1418  cgiCanv->SetBorderMode(0);
1419  cgiCanv->SetBorderSize(2);
1420  cgiCanv->SetFrameFillColor(0);
1421  cgiCanv->SetFrameBorderMode(0);
1422  cgiCanv->SetTickx();
1423  cgiCanv->SetTicky();
1424  cgiCanv->cd();
1425  histo->SetMarkerStyle(8);
1426  histo->SetMarkerSize(0.15);
1427  histo->Draw(drawopt);
1428  uuid_t uu;
1429  uuid_generate(uu);
1430  char uuid[50];
1431  uuid_unparse(uu, uuid);
1432  std::string tmpname = "/tmp/TC" + std::string(uuid);
1433  cgiCanv->Print(tmpname.c_str(), "gif");
1434  TImage *img = TImage::Open(tmpname.c_str());
1435  img->WriteImage(pngfilename.c_str());
1436  if (remove(tmpname.c_str()))
1437  {
1438  std::cout << "Error removing " << tmpname << std::endl;
1439  }
1440  delete cgiCanv;
1441  return 0;
1442 }
1443 
1445 {
1446  // sendfile example shamelessly copied from
1447  // http://www.linuxgazette.com/issue91/tranter.html
1448  std::ostringstream logfilename;
1449  const char *logdir = getenv("ONLMON_LOGDIR");
1450  if (logdir)
1451  {
1452  logfilename << logdir << "/";
1453  }
1454  int irun = RunNumber();
1455  logfilename << drawer.Name() << "_" << irun << ".log.gz";
1456  std::ifstream infile(logfilename.str().c_str(), std::ios_base::binary);
1457  if (infile.good())
1458  {
1459  std::string outfilename = htmlRegisterPage(drawer, "Logfile", "log", "txt.gz");
1460  std::ofstream outfile(outfilename.c_str(), std::ios_base::binary);
1461  outfile << infile.rdbuf();
1462  infile.close();
1463  outfile.close();
1464  }
1465  return 0;
1466 }
1467 
1469 {
1470  defaultStyle->cd();
1471  defaultStyle->Reset();
1472  defaultStyle->SetFrameBorderMode(0);
1473  defaultStyle->SetCanvasColor(0);
1474  defaultStyle->SetPadBorderMode(0);
1475  defaultStyle->SetCanvasBorderMode(0);
1476  defaultStyle->SetPalette(1, nullptr);
1477  return 0;
1478 }
1479 
1480 void OnlMonClient::CacheRunDB(const int runnoinput)
1481 {
1482  int runno = 221;
1483  if (runnoinput == 221)
1484  {
1485  runno = runnoinput;
1486  }
1487  if (runno == cachedrun)
1488  {
1489  return;
1490  }
1491  std::string TriggerConfig = "UNKNOWN";
1492  standalone = 0;
1493  cosmicrun = 0;
1494  runtype = "UNKNOWN";
1495 
1496  if (runno == 0xFEE2DCB) // dcm2 standalone runs have this runnumber
1497  {
1498  standalone = 1;
1499  return;
1500  }
1501  odbc::Connection *con = nullptr;
1502  odbc::Statement *query = nullptr;
1503  std::ostringstream cmd;
1504  try
1505  {
1506  con = odbc::DriverManager::getConnection("daq", "phnxrc", "");
1507  }
1508  catch (odbc::SQLException &e)
1509  {
1510  printf(" Exception caught during DriverManager::getConnection, Message: %s\n", e.getMessage().c_str());
1511  return;
1512  }
1513 
1514  query = con->createStatement();
1515  cmd << "select runnumber from run where runnumber = " << runno;
1516  odbc::ResultSet *rs = nullptr;
1517  int ncount = 10;
1518  while (ncount > 0)
1519  {
1520  try
1521  {
1522  rs = query->executeQuery(cmd.str());
1523  }
1524  catch (odbc::SQLException &e)
1525  {
1526  printf("Exception caught for query %s\nMessage: %s", cmd.str().c_str(), e.getMessage().c_str());
1527  }
1528  if (!rs->next())
1529  {
1530  printf("run table query did not give any result, run %d not in DB yet countdown %d\n", runno, ncount);
1531  ncount--;
1532  sleep(10);
1533  delete rs;
1534  }
1535  else
1536  {
1537  delete rs;
1538  break;
1539  }
1540  }
1541  cmd.str("");
1542  cmd << "SELECT triggerconfig,brunixtime,runtype FROM RUN WHERE RUNNUMBER = "
1543  << runno;
1544  if (verbosity > 0)
1545  {
1546  printf("command: %s\n", cmd.str().c_str());
1547  }
1548  try
1549  {
1550  rs = query->executeQuery(cmd.str());
1551  }
1552  catch (odbc::SQLException &e)
1553  {
1554  printf("Exception caught for query %s\nMessage: %s", cmd.str().c_str(), e.getMessage().c_str());
1555  }
1556  if (rs->next())
1557  {
1558  runtype = rs->getString("runtype");
1559  TriggerConfig = rs->getString("triggerconfig");
1560  if (TriggerConfig == "StandAloneMode")
1561  {
1562  standalone = 1;
1563  }
1564  else
1565  {
1566  standalone = 0;
1567  }
1568  if (TriggerConfig.find("Cosmic") != std::string::npos)
1569  {
1570  cosmicrun = 1;
1571  }
1572  else
1573  {
1574  cosmicrun = 0;
1575  }
1576  }
1577  delete con;
1578  // printf("CacheRunDB: runno: %d\n",runno);
1579  return;
1580 }
1581 
1583 {
1584  CacheRunDB(RunNumber());
1585  return cosmicrun;
1586 }
1587 
1589 {
1590  CacheRunDB(RunNumber());
1591  return standalone;
1592 }
1593 
1596 {
1597  CacheRunDB(RunNumber());
1598  return runtype;
1599 }
1600 
1602 {
1603  for (auto &hostiter : MonitorHosts)
1604  {
1605  if (Verbosity() > 2)
1606  {
1607  std::cout << "checking " << hostiter << std::endl;
1608  }
1609  for (unsigned int moniport = OnlMonDefs::MONIPORT; moniport < OnlMonDefs::MONIPORT + OnlMonDefs::NUMMONIPORT; ++moniport)
1610  {
1611  requestMonitorList(hostiter, moniport);
1612  }
1613  }
1614  return;
1615 }
1616 
1618 {
1619  // loop over all hosts/ports until we find ours
1620  int iret = 0;
1621  for (auto &hostiter : MonitorHosts)
1622  {
1623  if (Verbosity() > 2)
1624  {
1625  std::cout << "checking " << hostiter << std::endl;
1626  }
1627  for (unsigned int moniport = OnlMonDefs::MONIPORT; moniport < OnlMonDefs::MONIPORT + OnlMonDefs::NUMMONIPORT; ++moniport)
1628  {
1629  requestMonitorList(hostiter, moniport);
1630  if (Verbosity() > 2)
1631  {
1632  std::cout << "looking for " << name << std::endl;
1633  }
1634  auto moniter = MonitorHostPorts.find(name);
1635  if (moniter != MonitorHostPorts.end())
1636  {
1637  if (Verbosity() > 2)
1638  {
1639  std::cout << "found " << name << " running on " << moniter->second.first
1640  << " listening to port " << moniter->second.second << std::endl;
1641  }
1642  return 1;
1643  }
1644  }
1645  }
1646  return iret;
1647 }
1648 
1650 {
1651  int iret = 0;
1652  std::string command = std::string("ISRUNNING") + ' ' + name;
1653  auto moniter = MonitorHostPorts.find(name);
1654  if (moniter == MonitorHostPorts.end())
1655  {
1656  return iret;
1657  }
1658  TSocket sock(moniter->second.first.c_str(), moniter->second.second);
1659  TMessage *mess;
1660  sock.Send(command.c_str());
1661  sock.Recv(mess);
1662  if (!mess) // if server is not up mess is NULL
1663  {
1664  std::cout << __PRETTY_FUNCTION__ << "Server not running on " << moniter->second.first << std::endl;
1665  sock.Close();
1666  return iret;
1667  }
1668  if (mess->What() == kMESS_STRING)
1669  {
1670  char str[OnlMonDefs::MSGLEN];
1671  mess->ReadString(str, OnlMonDefs::MSGLEN);
1672  delete mess;
1673  if (verbosity > 1)
1674  {
1675  std::cout << __PRETTY_FUNCTION__ << "Message: " << str << std::endl;
1676  }
1677  if (!strcmp(str, "Yes"))
1678  {
1679  iret = 1;
1680  }
1681  }
1682  sock.Send("Finished");
1683  sock.Close();
1684  return iret;
1685 }
1686 
1688 {
1689  std::string subsys = std::filesystem::path(fullfilename).filename();
1690  subsys = subsys.substr(subsys.find('-') + 1);
1691  subsys = subsys.substr(0, subsys.find(".root"));
1692  m_MonitorFetchedSet.insert(subsys);
1693  return subsys;
1694 }