00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <sys/time.h>
00012 #include <iostream>
00013 #include <assert.h>
00014 #include <signal.h>
00015
00016 #ifdef HAVE_MIDASSERVER
00017 #include "midasServer.h"
00018 #endif
00019 #ifdef HAVE_LIBNETDIRECTORY
00020 #include "netDirectoryServer.h"
00021 #endif
00022 #ifdef HAVE_XMLSERVER
00023 #include "xmlServer.h"
00024 #endif
00025
00026 #include <TSystem.h>
00027 #include <TROOT.h>
00028 #include <TApplication.h>
00029 #include <TTimer.h>
00030 #include <TFile.h>
00031 #include <TDirectory.h>
00032 #include <TFolder.h>
00033 #include <TH1D.h>
00034
00035
00036 TDirectory* gOnlineHistDir = NULL;
00037 TFile* gOutputFile = NULL;
00038
00039 double GetTimeSec()
00040 {
00041 struct timeval tv;
00042 gettimeofday(&tv,NULL);
00043 return tv.tv_sec + 0.000001*tv.tv_usec;
00044 }
00045
00046 class MyPeriodic : public TTimer
00047 {
00048 public:
00049 typedef void (*TimerHandler)(void);
00050
00051 int fPeriod_msec;
00052 TimerHandler fHandler;
00053 double fLastTime;
00054
00055 MyPeriodic(int period_msec,TimerHandler handler)
00056 {
00057 assert(handler != NULL);
00058 fPeriod_msec = period_msec;
00059 fHandler = handler;
00060 fLastTime = GetTimeSec();
00061 Start(period_msec,kTRUE);
00062 }
00063
00064 Bool_t Notify()
00065 {
00066 double t = GetTimeSec();
00067
00068
00069 if (t - fLastTime >= 0.9*fPeriod_msec*0.001)
00070 {
00071
00072 if (fHandler)
00073 (*fHandler)();
00074
00075 fLastTime = t;
00076 }
00077
00078 Reset();
00079 return kTRUE;
00080 }
00081
00082 ~MyPeriodic()
00083 {
00084 TurnOff();
00085 }
00086 };
00087
00088 static bool gEnableShowMem = false;
00089
00090 int ShowMem(const char* label)
00091 {
00092 if (!gEnableShowMem)
00093 return 0;
00094
00095 FILE* fp = fopen("/proc/self/statm","r");
00096 if (!fp)
00097 return 0;
00098
00099 int mem = 0;
00100 fscanf(fp,"%d",&mem);
00101 fclose(fp);
00102
00103 if (label)
00104 printf("memory at %s is %d\n", label, mem);
00105
00106 return mem;
00107 }
00108
00109 void help()
00110 {
00111 printf("\nUsage:\n");
00112
00113 printf("\n./test_midasServer.exe [-h] [-PtcpPort] [-pTcpPort] [-XtmpPort] [-m]\n");
00114 printf("\n");
00115 printf("\t-h: print this help message\n");
00116 printf("\t-X: Start the XML Server on specified tcp port (for use with roody)\n");
00117 printf("\t-P: Start the TNetDirectory server on specified tcp port (for use with roody)\n");
00118 printf("\t-p: Start the old midas histogram server on specified tcp port (for use with roody)\n");
00119 printf("\t-m: Enable memory leak debugging\n");
00120 printf("\n");
00121
00122 exit(1);
00123 }
00124
00125 void IncrFunc()
00126 {
00127 TH1D *h;
00128 h = (TH1D*)gROOT->FindObjectAny("test1");
00129
00130 if (h)
00131 {
00132 static int x = 0;
00133 h->Fill(x);
00134 x++;
00135 if (x>100)
00136 x = 0;
00137 }
00138
00139 h = (TH1D*)gROOT->FindObjectAny("test3");
00140
00141 if (h)
00142 {
00143 h->Fill(33);
00144 }
00145 }
00146
00147
00148
00149 int main(int argc, char *argv[])
00150 {
00151 setbuf(stdout,NULL);
00152 setbuf(stderr,NULL);
00153
00154 signal(SIGILL, SIG_DFL);
00155 signal(SIGBUS, SIG_DFL);
00156 signal(SIGSEGV, SIG_DFL);
00157 signal(SIGPIPE, SIG_DFL);
00158
00159 std::vector<std::string> args;
00160 for (int i=0; i<argc; i++)
00161 {
00162 if (strcmp(argv[i],"-h")==0)
00163 help();
00164 args.push_back(argv[i]);
00165 }
00166
00167 TApplication *app = new TApplication("rootana", &argc, argv);
00168
00169 if(gROOT->IsBatch()) {
00170 printf("Cannot run in batch mode\n");
00171 return 1;
00172 }
00173
00174 int oldTcpPort = 0;
00175 int tcpPort = 0;
00176 int xmlTcpPort = 0;
00177
00178 for (unsigned int i=1; i<args.size(); i++)
00179 {
00180 const char* arg = args[i].c_str();
00181
00182
00183 if (false)
00184 ;
00185 else if (strncmp(arg,"-m",2)==0)
00186 gEnableShowMem = true;
00187 else if (strncmp(arg,"-p",2)==0)
00188 oldTcpPort = atoi(arg+2);
00189 else if (strncmp(arg,"-P",2)==0)
00190 tcpPort = atoi(arg+2);
00191 else if (strncmp(arg,"-X",2)==0)
00192 xmlTcpPort = atoi(arg+2);
00193 else if (strcmp(arg,"-h")==0)
00194 help();
00195 else if (arg[0] == '-')
00196 help();
00197 }
00198
00199 gROOT->cd();
00200 gOnlineHistDir = new TDirectory("rootana", "rootana online plots");
00201
00202 #ifdef HAVE_MIDASSERVER
00203 if (oldTcpPort)
00204 StartMidasServer(oldTcpPort);
00205 #else
00206 if (oldTcpPort)
00207 fprintf(stderr,"ERROR: No support for the old midas server!\n");
00208 #endif
00209 #ifdef HAVE_LIBNETDIRECTORY
00210 if (tcpPort)
00211 {
00212
00213 StartNetDirectoryServer(tcpPort, gOnlineHistDir);
00214 }
00215 #else
00216 if (tcpPort)
00217 fprintf(stderr,"ERROR: No support for the TNetDirectory server!\n");
00218 #endif
00219 #ifdef HAVE_XMLSERVER
00220 XmlServer* xmlServer = NULL;
00221 if (xmlTcpPort)
00222 {
00223 xmlServer = new XmlServer();
00224 xmlServer->SetVerbose(true);
00225 xmlServer->Start(xmlTcpPort);
00226 xmlServer->Export(gOnlineHistDir, gOnlineHistDir->GetName());
00227 }
00228 #else
00229 if (xmlTcpPort)
00230 fprintf(stderr,"ERROR: No support for the XML Server!\n");
00231 #endif
00232
00233 gOnlineHistDir->cd();
00234
00235
00236 TFile *f = new TFile("output.root", "RECREATE");
00237 f->cd();
00238
00239 NetDirectoryExport(f, "outputFile");
00240
00241 if (xmlServer)
00242 xmlServer->Export(f, "outputFile");
00243
00244 TH1D* hh = new TH1D("test1", "test1", 100, 0, 100);
00245 hh->Fill(1);
00246 hh->Fill(10);
00247 hh->Fill(50);
00248
00249 TDirectory* subdir = gDirectory->mkdir("subdir with space");
00250 subdir->cd();
00251
00252 TH1D* hh2 = new TH1D("test2 with space", "test2", 100, 0, 100);
00253 hh2->Fill(25);
00254
00255 TFolder* subfolder = new TFolder("subfolder", "subfolder");
00256 f->Add(subfolder);
00257
00258 TH1D* hh3 = new TH1D("test3", "test3", 100, 0, 100);
00259 hh3->Fill(33);
00260 subfolder->Add(hh3);
00261
00262
00263
00264
00265 #ifdef HAVE_MIDASSERVER
00266 if (oldTcpPort>0 && gManaHistosFolder)
00267 {
00268 NetDirectoryExport(gManaHistosFolder, "histos");
00269
00270 TFolder *subfolder = gManaHistosFolder->AddFolder("subfolder", "Sub folder");
00271 subfolder->Add(hh2);
00272
00273 gManaHistosFolder->Add(hh);
00274 }
00275 #endif
00276
00277
00278
00279
00280
00281
00282
00283
00284 if (xmlServer) {
00285 xmlServer->Export(gROOT->GetListOfFiles(), "ListOfFiles");
00286
00287 }
00288
00289 new MyPeriodic(100, IncrFunc);
00290
00291
00292 app->Run(kTRUE);
00293 return 0;
00294 }
00295
00296