Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 }