ROOT logo

From $ROOTSYS/tutorials/net/treeClient.C

#include "TMessage.h"
#include "TBenchmark.h"
#include "TSocket.h"
#include "TH2.h"
#include "TTree.h"
#include "TMemFile.h"
#include "TRandom.h"
#include "TError.h"

void treeClient(Bool_t evol=kFALSE) 
{
   // Client program which creates and fills 2 histograms and a TTree. 
   // Every 1000000 fills the histograms and TTree is send to the server which displays the histogram.
   //
   // To run this demo do the following:
   //   - Open at least 2 windows
   //   - Start ROOT in the first windows
   //   - Execute in the first window: .x fastMergeServer.C
   //   - Execute in the other windows: root.exe -b -l -q .x treeClient.C
   //     (You can put it in the background if wanted).
   // If you want to run the hserv.C on a different host, just change
   // "localhost" in the TSocket ctor below to the desired hostname.
   //
   //Author: Fons Rademakers, Philippe Canal
   
   gBenchmark->Start("treeClient");

   // Open connection to server
   TSocket *sock = new TSocket("localhost", 9090);
   if (!sock->IsValid()) {
      Error("treeClient","Could not establish a connection with the server %s:%d.","localhost",9090);
      return;
   }

   // Wait till we get the start message
   // server tells us who we are
   Int_t status, kind;
   sock->Recv(status, kind);
   
   if (kind != 0 /* kStartConnection */) 
   {
      Error("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
      delete sock;
      return;
   }
   
   int idx = status;
   
   Float_t messlen  = 0;
   Float_t cmesslen = 0;

   TMemFile *file = new TMemFile("mergedClient.root","RECREATE");
   TH1 *hpx;
   if (idx == 0) {
      // Create the histogram
      hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
      hpx->SetFillColor(48);  // set nice fillcolor
   } else {
      hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
   }
   Float_t px, py;
   TTree *tree = new TTree("tree","tree");
   tree->SetAutoFlush(4000000);
   tree->Branch("px",&px);
   tree->Branch("py",&py);
 
   TMessage::EnableSchemaEvolutionForAll(evol);
   TMessage mess(kMESS_OBJECT);

   // Fill histogram randomly
   gRandom->SetSeed();
   const int kUPDATE = 1000000;
   for (int i = 0; i < 25000000; ) {
      gRandom->Rannor(px,py);
      if (idx%2 == 0)
         hpx->Fill(px);
      else
         hpx->Fill(px,py);
      tree->Fill();
      ++i;
      if (i && (i%kUPDATE) == 0) {
         file->Write();
         mess.Reset(kMESS_ANY);              // re-use TMessage object
         mess.WriteInt(idx);
         mess.WriteTString(file->GetName());
         mess.WriteLong64(file->GetEND());   // 'mess << file->GetEND();' is broken in CINT for Long64_t
         file->CopyTo(mess);
         sock->Send(mess);          // send message
         messlen  += mess.Length();
         cmesslen += mess.CompLength();
         
         file->ResetAfterMerge(0);  // This resets only the TTree objects.
         hpx->Reset();
      }
   }
   sock->Send("Finished");          // tell server we are finished

   if (cmesslen > 0)
      printf("Average compression ratio: %g\n", messlen/cmesslen);

   gBenchmark->Show("hclient");

   // Close the socket
   sock->Close();
}
 treeClient.C:1
 treeClient.C:2
 treeClient.C:3
 treeClient.C:4
 treeClient.C:5
 treeClient.C:6
 treeClient.C:7
 treeClient.C:8
 treeClient.C:9
 treeClient.C:10
 treeClient.C:11
 treeClient.C:12
 treeClient.C:13
 treeClient.C:14
 treeClient.C:15
 treeClient.C:16
 treeClient.C:17
 treeClient.C:18
 treeClient.C:19
 treeClient.C:20
 treeClient.C:21
 treeClient.C:22
 treeClient.C:23
 treeClient.C:24
 treeClient.C:25
 treeClient.C:26
 treeClient.C:27
 treeClient.C:28
 treeClient.C:29
 treeClient.C:30
 treeClient.C:31
 treeClient.C:32
 treeClient.C:33
 treeClient.C:34
 treeClient.C:35
 treeClient.C:36
 treeClient.C:37
 treeClient.C:38
 treeClient.C:39
 treeClient.C:40
 treeClient.C:41
 treeClient.C:42
 treeClient.C:43
 treeClient.C:44
 treeClient.C:45
 treeClient.C:46
 treeClient.C:47
 treeClient.C:48
 treeClient.C:49
 treeClient.C:50
 treeClient.C:51
 treeClient.C:52
 treeClient.C:53
 treeClient.C:54
 treeClient.C:55
 treeClient.C:56
 treeClient.C:57
 treeClient.C:58
 treeClient.C:59
 treeClient.C:60
 treeClient.C:61
 treeClient.C:62
 treeClient.C:63
 treeClient.C:64
 treeClient.C:65
 treeClient.C:66
 treeClient.C:67
 treeClient.C:68
 treeClient.C:69
 treeClient.C:70
 treeClient.C:71
 treeClient.C:72
 treeClient.C:73
 treeClient.C:74
 treeClient.C:75
 treeClient.C:76
 treeClient.C:77
 treeClient.C:78
 treeClient.C:79
 treeClient.C:80
 treeClient.C:81
 treeClient.C:82
 treeClient.C:83
 treeClient.C:84
 treeClient.C:85
 treeClient.C:86
 treeClient.C:87
 treeClient.C:88
 treeClient.C:89
 treeClient.C:90
 treeClient.C:91
 treeClient.C:92
 treeClient.C:93
 treeClient.C:94
 treeClient.C:95
 treeClient.C:96
 treeClient.C:97
 treeClient.C:98
 treeClient.C:99
 treeClient.C:100
 treeClient.C:101
 treeClient.C:102
 treeClient.C:103
 treeClient.C:104
 treeClient.C:105
 treeClient.C:106