ROOTANA
test_midasServer.cxx
Go to the documentation of this file.
1 //
2 // ROOT analyzer
3 //
4 // K.Olchanski
5 //
6 // $Id$
7 //
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/time.h>
12 #include <iostream>
13 #include <assert.h>
14 #include <signal.h>
15 
16 #ifdef HAVE_MIDASSERVER
17 #include "midasServer.h"
18 #endif
19 #ifdef HAVE_LIBNETDIRECTORY
20 #include "netDirectoryServer.h"
21 #endif
22 #ifdef HAVE_XMLSERVER
23 #include "xmlServer.h"
24 #endif
25 
26 #include <TSystem.h>
27 #include <TROOT.h>
28 #include <TApplication.h>
29 #include <TTimer.h>
30 #include <TFile.h>
31 #include <TDirectory.h>
32 #include <TFolder.h>
33 #include <TH1D.h>
34 //#include <TBrowser.h>
35 
37 TFile* gOutputFile = NULL;
38 
39 double GetTimeSec()
40 {
41  struct timeval tv;
42  gettimeofday(&tv,NULL);
43  return tv.tv_sec + 0.000001*tv.tv_usec;
44 }
45 
46 class MyPeriodic : public TTimer
47 {
48 public:
49  typedef void (*TimerHandler)(void);
50 
53  double fLastTime;
54 
55  MyPeriodic(int period_msec,TimerHandler handler)
56  {
57  assert(handler != NULL);
58  fPeriod_msec = period_msec;
59  fHandler = handler;
61  Start(period_msec,kTRUE);
62  }
63 
64  Bool_t Notify()
65  {
66  double t = GetTimeSec();
67  //printf("timer notify, period %f should be %f!\n",t-fLastTime,fPeriod_msec*0.001);
68 
69  if (t - fLastTime >= 0.9*fPeriod_msec*0.001)
70  {
71  //printf("timer: call handler %p\n",fHandler);
72  if (fHandler)
73  (*fHandler)();
74 
75  fLastTime = t;
76  }
77 
78  Reset();
79  return kTRUE;
80  }
81 
83  {
84  TurnOff();
85  }
86 };
87 
88 static bool gEnableShowMem = false;
89 
90 int ShowMem(const char* label)
91 {
92  if (!gEnableShowMem)
93  return 0;
94 
95  FILE* fp = fopen("/proc/self/statm","r");
96  if (!fp)
97  return 0;
98 
99  int mem = 0;
100  fscanf(fp,"%d",&mem);
101  fclose(fp);
102 
103  if (label)
104  printf("memory at %s is %d\n", label, mem);
105 
106  return mem;
107 }
108 
109 void help()
110 {
111  printf("\nUsage:\n");
112 
113  printf("\n./test_midasServer.exe [-h] [-PtcpPort] [-pTcpPort] [-XtmpPort] [-m]\n");
114  printf("\n");
115  printf("\t-h: print this help message\n");
116  printf("\t-X: Start the XML Server on specified tcp port (for use with roody)\n");
117  printf("\t-P: Start the TNetDirectory server on specified tcp port (for use with roody)\n");
118  printf("\t-p: Start the old midas histogram server on specified tcp port (for use with roody)\n");
119  printf("\t-m: Enable memory leak debugging\n");
120  printf("\n");
121 
122  exit(1);
123 }
124 
125 void IncrFunc()
126 {
127  TH1D *h;
128  h = (TH1D*)gROOT->FindObjectAny("test1");
129  //printf("Histogram %p\n", h);
130  if (h)
131  {
132  static int x = 0;
133  h->Fill(x);
134  x++;
135  if (x>100)
136  x = 0;
137  }
138 
139  h = (TH1D*)gROOT->FindObjectAny("test3");
140  //printf("Histogram %p\n", h);
141  if (h)
142  {
143  h->Fill(33);
144  }
145 }
146 
147 // Main function call
148 
149 int main(int argc, char *argv[])
150 {
151  setbuf(stdout,NULL);
152  setbuf(stderr,NULL);
153 
154  signal(SIGILL, SIG_DFL);
155  signal(SIGBUS, SIG_DFL);
156  signal(SIGSEGV, SIG_DFL);
157  signal(SIGPIPE, SIG_DFL);
158 
159  std::vector<std::string> args;
160  for (int i=0; i<argc; i++)
161  {
162  if (strcmp(argv[i],"-h")==0)
163  help(); // does not return
164  args.push_back(argv[i]);
165  }
166 
167  TApplication *app = new TApplication("rootana", &argc, argv);
168 
169  if(gROOT->IsBatch()) {
170  printf("Cannot run in batch mode\n");
171  return 1;
172  }
173 
174  int oldTcpPort = 0;
175  int tcpPort = 0;
176  int xmlTcpPort = 0;
177 
178  for (unsigned int i=1; i<args.size(); i++) // loop over the commandline options
179  {
180  const char* arg = args[i].c_str();
181  //printf("argv[%d] is %s\n",i,arg);
182 
183  if (false)
184  ;
185  else if (strncmp(arg,"-m",2)==0) // Enable memory debugging
186  gEnableShowMem = true;
187  else if (strncmp(arg,"-p",2)==0) // Set the histogram server port
188  oldTcpPort = atoi(arg+2);
189  else if (strncmp(arg,"-P",2)==0) // Set the histogram server port
190  tcpPort = atoi(arg+2);
191  else if (strncmp(arg,"-X",2)==0) // Set the histogram server port
192  xmlTcpPort = atoi(arg+2);
193  else if (strcmp(arg,"-h")==0)
194  help(); // does not return
195  else if (arg[0] == '-')
196  help(); // does not return
197  }
198 
199  gROOT->cd();
200  gOnlineHistDir = new TDirectory("rootana", "rootana online plots");
201 
202 #ifdef HAVE_MIDASSERVER
203  if (oldTcpPort)
204  StartMidasServer(oldTcpPort);
205 #else
206  if (oldTcpPort)
207  fprintf(stderr,"ERROR: No support for the old midas server!\n");
208 #endif
209 #ifdef HAVE_LIBNETDIRECTORY
210  if (tcpPort)
211  {
212  //VerboseNetDirectoryServer(true);
214  }
215 #else
216  if (tcpPort)
217  fprintf(stderr,"ERROR: No support for the TNetDirectory server!\n");
218 #endif
219 #ifdef HAVE_XMLSERVER
220  XmlServer* xmlServer = NULL;
221  if (xmlTcpPort)
222  {
223  xmlServer = new XmlServer();
224  xmlServer->SetVerbose(true);
225  xmlServer->Start(xmlTcpPort);
226  xmlServer->Export(gOnlineHistDir, gOnlineHistDir->GetName());
227  }
228 #else
229  if (xmlTcpPort)
230  fprintf(stderr,"ERROR: No support for the XML Server!\n");
231 #endif
232 
233  gOnlineHistDir->cd();
234 
235  //TFile *f = new TFile("/Users/olchansk/output.root", "RECREATE");
236  TFile *f = new TFile("output.root", "RECREATE");
237  f->cd();
238 
239  NetDirectoryExport(f, "outputFile");
240 
241 #ifdef HAVE_XMLSERVER
242  if (xmlServer)
243  xmlServer->Export(f, "outputFile");
244 #endif
245 
246  TH1D* hh = new TH1D("test1", "test1", 100, 0, 100);
247  hh->Fill(1);
248  hh->Fill(10);
249  hh->Fill(50);
250 
251  TDirectory* subdir = gDirectory->mkdir("subdir with space");
252  subdir->cd();
253 
254  TH1D* hh2 = new TH1D("test2 with space", "test2", 100, 0, 100);
255  hh2->Fill(25);
256 
257  TFolder* subfolder = new TFolder("subfolder", "subfolder");
258  f->Add(subfolder);
259 
260  TH1D* hh3 = new TH1D("test3", "test3", 100, 0, 100);
261  hh3->Fill(33);
262  subfolder->Add(hh3);
263 
264  //f->Write();
265  //f->Close();
266 
267 #ifdef HAVE_MIDASSERVER
268  if (oldTcpPort>0 && gManaHistosFolder)
269  {
271 
272  TFolder *subfolder = gManaHistosFolder->AddFolder("subfolder", "Sub folder");
273  subfolder->Add(hh2);
274 
275  gManaHistosFolder->Add(hh);
276  }
277 #endif
278 
279  //gDirectory->cd("/");
280  //gDirectory->pwd();
281  //gROOT->cd();
282 
283  //NetDirectoryExport(gROOT->GetListOfFiles(), "ListOfFiles");
284  //NetDirectoryExport(gROOT->GetListOfGlobals(), "ListOfGlobals");
285 
286 #ifdef HAVE_XML_SERVER
287  if (xmlServer) {
288  xmlServer->Export(gROOT->GetListOfFiles(), "ListOfFiles");
289  //xmlServer->Export(gROOT->GetListOfGlobals(), "ListOfGlobals");
290  }
291 #endif
292 
293  new MyPeriodic(100, IncrFunc);
294 
295  //new TBrowser();
296  app->Run(kTRUE);
297  return 0;
298 }
299 
300 //end
R__EXTERN TDirectory * gDirectory
TimerHandler fHandler
void(* TimerHandler)(void)
MyPeriodic(int period_msec, TimerHandler handler)
virtual TDirectory * mkdir(const char *name, const char *title="")
virtual Bool_t cd(const char *path=0)
void Export(TDirectory *dir, const char *exportName)
Definition: xmlServer.cxx:990
void SetVerbose(bool verbose)
Definition: xmlServer.cxx:983
void Start(int port)
Definition: xmlServer.cxx:1044
TFolder * gManaHistosFolder
void StartMidasServer(int port)
void NetDirectoryExport(TDirectory *dir, const char *exportName)
void StartNetDirectoryServer(int port, TDirectory *dir)
int main(int argc, char *argv[])
TDirectory * gOnlineHistDir
static bool gEnableShowMem
void help()
int ShowMem(const char *label)
void IncrFunc()
TFile * gOutputFile
double GetTimeSec()