Re: converting cint macros to c++

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Thu May 14 1998 - 21:17:42 MEST


Hi Hans,

   several problems. Like Valery Fine mentioned in his previous mail. In
hclient.cxx
add declaration:

   TH1F  *hpx;

See attached hclient.cxx.

In hserv.cxx to get the graphics you've to initialize TROOT with the graphics
system (see initfuncs argument). Then you've to create an TApplication object
and at the end of the program you've to call app.Run() otherwise you will exit
without having a change to look at the histogras.

See attached hserv.cxx.

Cheers, Fons.
-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7677910

  // Client program which creates and fills a histogram. Every 1000 fills
   // the histogram is send to the server which displays the histogram.
   //
   // To run this demo do the following:
   //   - Open three windows
   //   - Start ROOT in all three windows
   //   - Execute in the first window: .x hserv.C
   //   - Execute in the second and third windows: .x hclient.C
   // If you want to run the hserv.C on a different host, just change
   // "localhost" in the TSocket ctor below to the desried hostname.



#include "TROOT.h"
//#include "TApplication.h"
//#include "TCanvas.h"
//#include "TLine.h"
//#include "TPaveLabel.h"
//#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
//#include "TProfile.h"
//#include "TNtuple.h"
#include "TRandom.h"
#include "TSocket.h"
//#include "TServerSocket.h"
//#include "TInetAddress.h"
//#include "TPad.h"
#include "TMessage.h"
//#include "TMonitor.h"
#include "TBenchmark.h"

int main()
{ 
  TROOT hclient("hclient","Test of simple histogram client");

   TH1F *hpx; 

   // Open connection to server
   TSocket *sock = new TSocket("localhost", 9090);

   // Wait till we get the start message
   char str[32];
   sock->Recv(str, 32);

   // server tells us who we are
   int idx = !strcmp(str, "go 0") ? 0 : 1;

   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);
   }

   TMessage mess(kMESS_OBJECT);
   //TMessage mess(kMESS_OBJECT | kMESS_ACK);

   // Fill histogram randomly
   gRandom->SetSeed();
   Float_t px, py;
   const int kUPDATE = 1000;
   for (int i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      if (idx == 0)
         hpx->Fill(px);
      else
         ((TH2F*)hpx)->Fill(px,py);
      if (i && (i%kUPDATE) == 0) {
         mess.Reset();              // re-use TMessage object
         mess.WriteObject(hpx);     // write object in message buffer
         sock->Send(mess);          // send message
      }
   }
   sock->Send("Finished");          // tell server we are finished

   // Close the socket
   sock->Close();

   return 0;
}



// Server program which waits for two clients to connect. It then monitors
// the sockets and displays the objects it receives.
//
// To run this demo do the following:
//   - Open three windows
//   - Start ROOT in all three windows
//   - Execute in the first window: .x hserv.C
//   - Execute in the second and third windows: .x hclient.C
// If you want to run the hserv.C on a different host, just change
// "localhost" in the TSocket ctor below to the desried hostname.

// Open a server socket looking for connections on a named service or
// on a specified port.

#include "TROOT.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TLine.h"
#include "TPaveLabel.h"
#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TProfile.h"
#include "TNtuple.h"
#include "TRandom.h"
#include "TSocket.h"
#include "TServerSocket.h"
#include "TInetAddress.h"
#include "TPad.h"
#include "TMessage.h"
#include "TMonitor.h"

extern void InitGui();
VoidFuncPtr_t initfuncs[] = { InitGui, 0 };

int main(int argc, char **argv)
{

   TROOT hserv("hserv","Test of simple histogram server",initfuncs);

   TApplication theApp("App", &argc, argv);
   
   //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
   TServerSocket *ss = new TServerSocket(9090, kTRUE);

   // Accept a connection and return a full-duplex communication socket.
   TSocket *s0 = ss->Accept();
   TSocket *s1 = ss->Accept();

   // tell the clients to start
   s0->Send("go 0");
   s1->Send("go 1");

   // Close the server socket (unless we will use it later to wait for
   // another connection).
   ss->Close();

   // Check some options of socket 0.
   int val;
   s0->GetOption(kSendBuffer, val);
   printf("sendbuffer size: %d\n", val);
   s0->GetOption(kRecvBuffer, val);
   printf("recvbuffer size: %d\n", val);

   // Get the remote addresses (informational only).
   TInetAddress adr = s0->GetInetAddress();
   adr.Print();
   adr = s1->GetInetAddress();
   adr.Print();

   // Create canvas and pads to display the histograms
   TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
   TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
   TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
   pad1->Draw();
   pad2->Draw();

   TMonitor *mon = new TMonitor;

   mon->Add(s0);
   mon->Add(s1);

   while (1) {
      TMessage *mess;
      TSocket  *s;

      s = mon->Select();

      s->Recv(mess);

      if (mess->What() == kMESS_STRING) {
         char str[64];
         mess->ReadString(str, 64);
         //printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
         printf("Client %d\n", s==s0 ? 0 : 1);
         printf("Client %s\n", str);
         mon->Remove(s);
         if (mon->GetActive() == 0) {
            printf("No more active clients... stopping\n");
            break;
         }
      } else if (mess->What() == kMESS_OBJECT) {
         //printf("got object of class: %s\n", mess->GetClass()->GetName());
         TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
         if (s == s0)
            pad1->cd();
         else
            pad2->cd();
         h->Print();
         h->DrawCopy();  // draw a copy of the histogram, not the histo itself
         c1->Modified();
         c1->Update();
         delete h;       // delete histogram
      } else {
         printf("*** Unexpected message ***\n");
      }

      delete mess;
   }

   // Close the socket.
   s0->Close();
   s1->Close();

   // Here we don't return from the eventloop. "Exit ROOT" will quit the app.
   theApp.Run();

   return 0;
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:32 MET