ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
treeClient.C
Go to the documentation of this file.
1 #include "TMessage.h"
2 #include "TBenchmark.h"
3 #include "TSocket.h"
4 #include "TH2.h"
5 #include "TTree.h"
6 #include "TMemFile.h"
7 #include "TRandom.h"
8 #include "TError.h"
9 
11 {
12  // Client program which creates and fills 2 histograms and a TTree.
13  // Every 1000000 fills the histograms and TTree is send to the server which displays the histogram.
14  //
15  // To run this demo do the following:
16  // - Open at least 2 windows
17  // - Start ROOT in the first windows
18  // - Execute in the first window: .x fastMergeServer.C
19  // - Execute in the other windows: root.exe -b -l -q .x treeClient.C
20  // (You can put it in the background if wanted).
21  // If you want to run the hserv.C on a different host, just change
22  // "localhost" in the TSocket ctor below to the desired hostname.
23  //
24  //Author: Fons Rademakers, Philippe Canal
25 
26  gBenchmark->Start("treeClient");
27 
28  // Open connection to server
29  TSocket *sock = new TSocket("localhost", 9090);
30  if (!sock->IsValid()) {
31  Error("treeClient","Could not establish a connection with the server %s:%d.","localhost",9090);
32  return;
33  }
34 
35  // Wait till we get the start message
36  // server tells us who we are
37  Int_t status, version, kind;
38  sock->Recv(status, kind);
39  if (kind != 0 /* kStartConnection */)
40  {
41  Error("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
42  delete sock;
43  return;
44  }
45  sock->Recv(version, kind);
46  if (kind != 1 /* kStartConnection */)
47  {
48  Fatal("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
49  } else {
50  Info("treeClient","Connected to fastMergeServer version %d\n",version);
51  }
52 
53  int idx = status;
54 
55  Float_t messlen = 0;
56  Float_t cmesslen = 0;
57 
58  TMemFile *file = new TMemFile("mergedClient.root","RECREATE");
59  TH1 *hpx;
60  if (idx == 0) {
61  // Create the histogram
62  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
63  hpx->SetFillColor(48); // set nice fillcolor
64  } else {
65  hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
66  }
67  Float_t px, py;
68  TTree *tree = new TTree("tree","tree");
69  tree->SetAutoFlush(4000000);
70  tree->Branch("px",&px);
71  tree->Branch("py",&py);
72 
74  TMessage mess(kMESS_OBJECT);
75 
76  // Fill histogram randomly
77  gRandom->SetSeed();
78  const int kUPDATE = 1000000;
79  for (int i = 0; i < 25000000; ) {
80  gRandom->Rannor(px,py);
81  if (idx%2 == 0)
82  hpx->Fill(px);
83  else
84  hpx->Fill(px,py);
85  tree->Fill();
86  ++i;
87  if (i && (i%kUPDATE) == 0) {
88  file->Write();
89  mess.Reset(kMESS_ANY); // re-use TMessage object
90  mess.WriteInt(idx);
91  mess.WriteTString(file->GetName());
92  mess.WriteLong64(file->GetEND()); // 'mess << file->GetEND();' is broken in CINT for Long64_t
93  file->CopyTo(mess);
94  sock->Send(mess); // send message
95  messlen += mess.Length();
96  cmesslen += mess.CompLength();
97 
98  file->ResetAfterMerge(0); // This resets only the TTree objects.
99  hpx->Reset();
100  }
101  }
102  sock->Send("Finished"); // tell server we are finished
103 
104  if (cmesslen > 0)
105  printf("Average compression ratio: %g\n", messlen/cmesslen);
106 
107  gBenchmark->Show("hclient");
108 
109  // Close the socket
110  sock->Close();
111 }
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 'to' 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:3159
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:4306
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Int_t CompLength() const
Definition: TMessage.h:95
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
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:191
Float_t py
Definition: hprod.C:33
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void treeClient(Bool_t evol=kFALSE)
Definition: treeClient.C:10
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH1.cxx:6669
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:172
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:7382
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
virtual void WriteTString(const TString &s)
Write TString to TBuffer.
void Reset()
Reset the message buffer so we can use (i.e. fill) it again.
Definition: TMessage.cxx:171
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:388
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
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:2248
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void WriteInt(Int_t i)
Definition: TBufferFile.h:364
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:62
tuple tree
Definition: tree.py:24
tuple file
Definition: fildir.py:20
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
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:1623
Float_t px
Definition: hprod.C:33
virtual void WriteLong64(Long64_t l)
Definition: TBufferFile.h:392
Int_t Length() const
Definition: TBuffer.h:96
A TTree object has a header with a name and a title.
Definition: TTree.h:98
TH1F * hpx
Definition: hcons.C:32