Logo ROOT   6.07/09
Reference Guide
treeClient.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_net
3 /// Client program which creates and fills 2 histograms and a TTree.
4 /// Every 1000000 fills the histograms and TTree is send to the server which displays the histogram.
5 ///
6 /// To run this demo do the following:
7 /// - Open at least 2 windows
8 /// - Start ROOT in the first windows
9 /// - Execute in the first window: .x fastMergeServer.C
10 /// - Execute in the other windows: root.exe -b -l -q .x treeClient.C
11 /// (You can put it in the background if wanted).
12 /// If you want to run the hserv.C on a different host, just change
13 /// "localhost" in the TSocket ctor below to the desired hostname.
14 ///
15 /// \macro_code
16 ///
17 /// \authors Fons Rademakers, Philippe Canal
18 
19 #include "TMessage.h"
20 #include "TBenchmark.h"
21 #include "TSocket.h"
22 #include "TH2.h"
23 #include "TTree.h"
24 #include "TMemFile.h"
25 #include "TRandom.h"
26 #include "TError.h"
27 
28 void treeClient(Bool_t evol=kFALSE)
29 {
30  gBenchmark->Start("treeClient");
31 
32  // Open connection to server
33  TSocket *sock = new TSocket("localhost", 9090);
34  if (!sock->IsValid()) {
35  Error("treeClient","Could not establish a connection with the server %s:%d.","localhost",9090);
36  return;
37  }
38 
39  // Wait till we get the start message
40  // server tells us who we are
41  Int_t status, version, kind;
42  sock->Recv(status, kind);
43  if (kind != 0 /* kStartConnection */)
44  {
45  Error("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
46  delete sock;
47  return;
48  }
49  sock->Recv(version, kind);
50  if (kind != 1 /* kStartConnection */)
51  {
52  Fatal("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
53  } else {
54  Info("treeClient","Connected to fastMergeServer version %d\n",version);
55  }
56 
57  int idx = status;
58 
59  Float_t messlen = 0;
60  Float_t cmesslen = 0;
61 
62  TMemFile *file = new TMemFile("mergedClient.root","RECREATE");
63  TH1 *hpx;
64  if (idx == 0) {
65  // Create the histogram
66  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
67  hpx->SetFillColor(48); // set nice fillcolor
68  } else {
69  hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
70  }
71  Float_t px, py;
72  TTree *tree = new TTree("tree","tree");
73  tree->SetAutoFlush(4000000);
74  tree->Branch("px",&px);
75  tree->Branch("py",&py);
76 
78  TMessage mess(kMESS_OBJECT);
79 
80  // Fill histogram randomly
81  gRandom->SetSeed();
82  const int kUPDATE = 1000000;
83  for (int i = 0; i < 25000000; ) {
84  gRandom->Rannor(px,py);
85  if (idx%2 == 0)
86  hpx->Fill(px);
87  else
88  hpx->Fill(px,py);
89  tree->Fill();
90  ++i;
91  if (i && (i%kUPDATE) == 0) {
92  file->Write();
93  mess.Reset(kMESS_ANY); // re-use TMessage object
94  mess.WriteInt(idx);
95  mess.WriteTString(file->GetName());
96  mess.WriteLong64(file->GetEND()); // 'mess << file->GetEND();' is broken in CINT for Long64_t
97  file->CopyTo(mess);
98  sock->Send(mess); // send message
99  messlen += mess.Length();
100  cmesslen += mess.CompLength();
101 
102  file->ResetAfterMerge(0); // This resets only the TTree objects.
103  hpx->Reset();
104  }
105  }
106  sock->Send("Finished"); // tell server we are finished
107 
108  if (cmesslen > 0)
109  printf("Average compression ratio: %g\n", messlen/cmesslen);
110 
111  gBenchmark->Show("hclient");
112 
113  // Close the socket
114  sock->Close();
115 }
void ResetAfterMerge(TFileMergeInfo *)
Wipe all the data from the permanent buffer but keep, the in-memory object alive. ...
Definition: TMemFile.cxx:292
virtual Long64_t CopyTo(void *to, Long64_t maxsize) const
Copy the binary representation of the TMemFile into the memory area starting at &#39;to&#39; and of length at...
Definition: TMemFile.cxx:227
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3127
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:460
void Fatal(const char *location, const char *msgfmt,...)
float Float_t
Definition: RtypesCore.h:53
virtual Bool_t IsValid() const
Definition: TSocket.h:162
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:520
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual Long64_t GetEND() const
Definition: TFile.h:200
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH1.cxx:6375
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void SetAutoFlush(Long64_t autof=-30000000)
This function may be called at the start of a program to change the default value for fAutoFlush...
Definition: TTree.cxx:7591
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:388
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2268
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void EnableSchemaEvolutionForAll(Bool_t enable=kTRUE)
Static function enabling or disabling the automatic schema evolution.
Definition: TMessage.cxx:116
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
The TH1 histogram class.
Definition: TH1.h:80
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1651
Definition: file.py:1
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:308