Logo ROOT  
Reference Guide
hclient.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_net
3/// Client program which creates and fills a histogram. Every 1000 fills
4/// the histogram is send to the server which displays the histogram.
5///
6/// To run this demo do the following:
7/// - Open three windows
8/// - Start ROOT in all three windows
9/// - Execute in the first window: .x hserv.C (or hserv2.C)
10/// - Execute in the second and third windows: .x hclient.C
11/// If you want to run the hserv.C on a different host, just change
12/// "localhost" in the TSocket ctor below to the desired hostname.
13///
14/// The script argument "evol" can be used when using a modified version
15/// of the script where the clients and server are on systems with
16/// different versions of ROOT. When evol is set to kTRUE the socket will
17/// support automatic schema evolution between the client and the server.
18///
19/// \macro_code
20///
21/// \author Fons Rademakers
22
23void hclient(Bool_t evol=kFALSE)
24{
25 gBenchmark->Start("hclient");
26
27 // Open connection to server
28 TSocket *sock = new TSocket("localhost", 9090);
29
30 // Wait till we get the start message
31 char str[32];
32 sock->Recv(str, 32);
33
34 // server tells us who we are
35 int idx = !strcmp(str, "go 0") ? 0 : 1;
36
37 Float_t messlen = 0;
38 Float_t cmesslen = 0;
39 if (idx == 1)
40 sock->SetCompressionLevel(1);
41
42 TH1 *hpx;
43 if (idx == 0) {
44 // Create the histogram
45 hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
46 hpx->SetFillColor(48); // set nice fill-color
47 } else {
48 hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
49 }
50
53 //TMessage mess(kMESS_OBJECT | kMESS_ACK);
54
55 // Fill histogram randomly
57 Float_t px, py;
58 const int kUPDATE = 1000;
59 for (int i = 0; i < 25000; i++) {
60 gRandom->Rannor(px,py);
61 if (idx == 0)
62 hpx->Fill(px);
63 else
64 hpx->Fill(px,py);
65 if (i && (i%kUPDATE) == 0) {
66 mess.Reset(); // re-use TMessage object
67 mess.WriteObject(hpx); // write object in message buffer
68 sock->Send(mess); // send message
69 messlen += mess.Length();
70 cmesslen += mess.CompLength();
71 }
72 }
73 sock->Send("Finished"); // tell server we are finished
74
75 if (cmesslen > 0)
76 printf("Average compression ratio: %g\n", messlen/cmesslen);
77
78 gBenchmark->Show("hclient");
79
80 // Close the socket
81 sock->Close();
82}
@ kMESS_OBJECT
Definition: MessageTypes.h:35
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:53
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
The TH1 histogram class.
Definition: TH1.h:56
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3275
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
static void EnableSchemaEvolutionForAll(Bool_t enable=kTRUE)
Static function enabling or disabling the automatic schema evolution.
Definition: TMessage.cxx:116
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:597
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:489
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1058
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:816
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:388
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:521