Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
httpcontrol.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_http
3/// This program demonstrates simple application control via THttpServer
4/// Two histogram are filled within endless loop.
5/// Via published commands one can enable/disable histograms filling
6/// There are also command to clear histograms content
7///
8/// After macro started, open in browser with url
9/// ~~~
10/// http://localhost:8080
11/// ~~~
12///
13/// Histograms will be automatically displayed and
14/// monitoring with interval 2000 ms started
15///
16/// \macro_code
17///
18/// \author Sergey Linev
19
20#include "TH1.h"
21#include "TH2.h"
22#include "TRandom3.h"
23#include "TSystem.h"
24#include "THttpServer.h"
25
26Bool_t bFillHist = kTRUE;
27
28void httpcontrol()
29{
30 // create histograms
31 TH1D *hpx = new TH1D("hpx","This is the px distribution",100,-4,4);
32 hpx->SetFillColor(48);
33 hpx->SetDirectory(nullptr);
34 TH2D *hpxpy = new TH2D("hpxpy","py vs px",40,-4,4,40,-4,4);
35 hpxpy->SetDirectory(nullptr);
36
37 // start http server
38 THttpServer* serv = new THttpServer("http:8080");
39
40 // One could specify location of newer version of JSROOT
41 // serv->SetJSROOT("https://root.cern/js/latest/");
42 // serv->SetJSROOT("http://jsroot.gsi.de/latest/");
43
44 // register histograms
45 serv->Register("/", hpx);
46 serv->Register("/", hpxpy);
47
48 // enable monitoring and
49 // specify items to draw when page is opened
50 serv->SetItemField("/","_monitoring","5000");
51 serv->SetItemField("/","_layout","grid2x2");
52 serv->SetItemField("/","_drawitem","[hpxpy,hpx,Debug]");
53 serv->SetItemField("/","_drawopt","col");
54
55 // register simple start/stop commands
56 serv->RegisterCommand("/Start", "bFillHist=kTRUE;", "button;rootsys/icons/ed_execute.png");
57 serv->RegisterCommand("/Stop", "bFillHist=kFALSE;", "button;rootsys/icons/ed_interrupt.png");
58
59 // one could hide commands and let them appear only as buttons
60 serv->Hide("/Start");
61 serv->Hide("/Stop");
62
63 // register commands, invoking object methods
64 serv->RegisterCommand("/ResetHPX","/hpx/->Reset()", "button;rootsys/icons/ed_delete.png");
65 serv->RegisterCommand("/ResetHPXPY","/hpxpy/->Reset()", "button;rootsys/icons/bld_delete.png");
66
67
68 // create debug text element, use MathJax - works only on Firefox
69 serv->CreateItem("/Debug","debug output");
70 serv->SetItemField("/Debug", "_kind", "Text");
71 serv->SetItemField("/Debug", "value","\\(\\displaystyle{x+1\\over y-1}\\)");
72 serv->SetItemField("/Debug", "mathjax", "true");
73
74 // Fill histograms randomly
75 TRandom3 random;
76 Float_t px, py;
77 const Long_t kUPDATE = 1000;
78 Long_t cnt = 0;
79 while (kTRUE) {
80 if (bFillHist) {
81 random.Rannor(px,py);
82 hpx->Fill(px);
83 hpxpy->Fill(px,py);
84 cnt++;
85 } else {
86 gSystem->Sleep(10); // sleep minimal time
87 }
88
89 if ((cnt % kUPDATE==0) || !bFillHist) {
90 // IMPORTANT: one should regularly call ProcessEvents
91 // to let http server process requests
92
93 serv->SetItemField("/Debug", "value", Form("\\(\\displaystyle{x+1\\over y-1}\\) Loop:%ld", cnt/kUPDATE));
94
95 if (gSystem->ProcessEvents()) break;
96 }
97 }
98}
bool Bool_t
Definition RtypesCore.h:63
long Long_t
Definition RtypesCore.h:54
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:669
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8905
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3344
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:357
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:393
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=nullptr)
Register command which can be executed from web interface.
Bool_t CreateItem(const char *fullname, const char *title)
Create item in sniffer.
Bool_t Hide(const char *fullname, Bool_t hide=kTRUE)
Hides folder or element from web gui.
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Set item field in sniffer.
Random number generator class based on M.
Definition TRandom3.h:27
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:507
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:437
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:416
const char * cnt
Definition TXMLSetup.cxx:75