Logo ROOT   6.14/05
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 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:424
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:481
Random number generator class based on M.
Definition: TRandom3.h:27
float Float_t
Definition: RtypesCore.h:53
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:8231
bool Bool_t
Definition: RtypesCore.h:59
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:445
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
char * Form(const char *fmt,...)
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:610
long Long_t
Definition: RtypesCore.h:50
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:290
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char * cnt
Definition: TXMLSetup.cxx:74
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:291