Logo ROOT  
Reference Guide
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 
26 Bool_t bFillHist = kTRUE;
27 
28 void 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(0);
34  TH2D *hpxpy = new TH2D("hpxpy","py vs px",40,-4,4,40,-4,4);
35  hpxpy->SetDirectory(0);
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.ch/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 }
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
THttpServer::SetItemField
Bool_t SetItemField(const char *fullname, const char *name, const char *value)
Definition: THttpServer.cxx:1250
THttpServer::CreateItem
Bool_t CreateItem(const char *fullname, const char *title)
Definition: THttpServer.cxx:1243
Form
char * Form(const char *fmt,...)
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
Float_t
float Float_t
Definition: RtypesCore.h:57
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TRandom::Rannor
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:500
THttpServer
Definition: THttpServer.h:31
TSystem.h
TRandom3
Random number generator class based on M.
Definition: TRandom3.h:27
THttpServer::Register
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Definition: THttpServer.cxx:1046
Long_t
long Long_t
Definition: RtypesCore.h:54
TH2D
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:292
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3350
TRandom3.h
TH1::SetDirectory
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8777
TSystem::Sleep
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:438
TH2.h
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:294
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
THttpServer.h
THttpServer::RegisterCommand
Bool_t RegisterCommand(const char *cmdname, const char *method, const char *icon=nullptr)
Register command which can be executed from web interface.
Definition: THttpServer.cxx:1217
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
kUPDATE
@ kUPDATE
Definition: TFitParametersDialog.cxx:45
THttpServer::Hide
Bool_t Hide(const char *fullname, Bool_t hide=kTRUE)
hides folder or element from web gui
Definition: THttpServer.cxx:1225
xmlio::cnt
const char * cnt
Definition: TXMLSetup.cxx:75
TH1.h
TSystem::ProcessEvents
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:417