CbmRoot
Loading...
Searching...
No Matches
Server.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer] */
4#include "Server.h"
5
6#include <TH1.h>
7#include <THttpServer.h>
8#include <TSystem.h>
9
10#include <log.hpp>
11
12#include <fmt/format.h>
13
14using namespace cbm::explore;
15
17
18Server::Server(Settings settings) : TNamed("server", "server"), fS(settings)
19{
20 L_(info) << "Starting Server with port " << fS.port;
21
22 if (fS.sensor) L_(info) << "Filtering on sensor " << *fS.sensor;
23 else {
24 L_(info) << "Not filtering on sensor";
25 }
26
27 fServer = new THttpServer(fmt::format("http:{}", fS.port).c_str());
28 fServer->SetTimer(0, true);
29
30 // FIXME: Why doesn't root use the local jsroot by default???
31 fServer->SetJSROOT("https://jsroot.gsi.de/7.1.0/");
32
33 // register this object in http server
34 fServer->Register("/", this);
35 fServer->Hide("/server");
36
37 // enable monitoring and
38 // specify items to draw when page is opened
39 // fServer->SetItemField("/", "_monitoring", "5000");
40
41 // Specify layout. TODO: does not work?
42 fServer->SetItemField("/", "_layout", "grid3x3");
43
44 fServer->RegisterCommand("/NextTS", "/server/->RequestNextTS()");
45 fServer->SetItemField("/NextTS", "_title", "Next Timeslice");
46
47 if (fS.archive == nullptr) throw std::runtime_error("Archive must not be null");
48 if (fS.histograms == nullptr) throw std::runtime_error("Histograms must not be null");
49
50 if (fS.archive2 != nullptr || fS.histograms2 != nullptr) {
51 if (fS.archive2 == nullptr) throw std::runtime_error("Archive2 must not be null");
52 if (fS.histograms2 == nullptr) throw std::runtime_error("Histograms2 must not be null");
53 }
54
55 // Setup folders
56 for (auto& folder : fS.histograms->GetFolders()) {
57 fServer->CreateItem(folder.path.c_str(), folder.name.c_str());
58 fServer->SetItemField(folder.path.c_str(), "_kind", "Folder");
59 }
60
61 // Setup histograms
62 for (auto& [path, histo] : fS.histograms->GetHistos()) {
63 fServer->Register(path.c_str(), histo);
64 }
65}
66
68
70{
71 NextTS();
72
73 L_(info) << "Starting event loop";
74 while (true) {
75
76 int nRequests = 0;
77 do {
78 nRequests = fServer->ProcessRequests();
79 if (nRequests > 0) L_(info) << "Processed " << nRequests << " requests";
80
81 // sleep minimal time
82 // TODO: This is terrible. And the sleep should not be in this loop.
83 // But ROOTJS sometimes sends commands multiple times,
84 // And this is the only way, i was able to catch duplicate commands
85 gSystem->Sleep(SleepPerTick_ms);
86 } while (nRequests > 0);
87
88 if (fRequestNextTS) {
89 NextTS();
90 fRequestNextTS = false;
91 }
92 }
93
94 return 0;
95}
96
98{
99 L_(info) << "Fetching next Timeslice...";
100
101 auto recoResults = fS.archive->get();
102 if (fS.archive->eos()) {
103 L_(info) << "End of archive reached";
104 return;
105 }
106
107 HistoData fill {.data = recoResults.get(), .sensor = fS.sensor};
108
109 fS.histograms->Reset();
110 fS.histograms->FillHistos(fill);
111
112 bool divide = fS.archive2 != nullptr;
113 if (divide) {
114 recoResults = fS.archive2->get();
115 if (fS.archive2->eos()) throw std::runtime_error("End of archive2 reached too early");
116 fS.histograms2->Reset();
117 fill = {.data = recoResults.get(), .sensor = fS.sensor};
118 fS.histograms2->FillHistos(fill);
119 L_(info) << "Dividing histograms";
121 }
122
123 L_(info) << "Finished Timeslice";
124}
#define L_(level)
ClassImp(Server)
THttpServer * fServer
Definition Server.h:50
Server(Settings settings)
Definition Server.cxx:18
virtual ~Server()
Definition Server.cxx:67
static constexpr int SleepPerTick_ms
Definition Server.h:47
std::shared_ptr< HistogramCollection > histograms2
Definition Server.h:34
std::shared_ptr< HistogramCollection > histograms
Definition Server.h:29
std::shared_ptr< algo::RecoResultsInputArchive > archive
Definition Server.h:28
std::optional< uint32_t > sensor
Definition Server.h:27
std::shared_ptr< algo::RecoResultsInputArchive > archive2
Definition Server.h:33