Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

example2a.cxx

Go to the documentation of this file.
00001 #include <TSystem.h>
00002 #include <TApplication.h>
00003 #include <TGClient.h>
00004 #include <TCanvas.h>
00005 #include <TF1.h>
00006 #include <TH1D.h>
00007 #include <TRandom.h>
00008 #include <TGButton.h>
00009 #include <TGTextEntry.h>
00010 #include <TGMenu.h> 
00011 #include <TGLabel.h>
00012 #include <TRootEmbeddedCanvas.h>
00013 #include <TGFSContainer.h>
00014 #include <iostream>
00015 #include <TLatex.h>
00016 #include "example2a.h"
00017 
00018 #include <unistd.h>
00019 #include <TRandom.h>
00020 #include <iostream>
00021 #include <string>
00022 
00023 //#define HOMEDIR "/home/Linux/meguro/study/ROOT/jikken3/root_org/"
00024 
00025 
00026 //#include "SMcommon.h"
00027 
00028 
00029 MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h){
00030   ShMemory();
00031 
00032 //Create a main frame
00033   fMain = new TGMainFrame(p,w,h);
00034 
00035 // Create frames
00036   TGHorizontalFrame *hframe = new TGHorizontalFrame(fMain,200,40);
00037   TGHorizontalFrame *hframe2 = new TGHorizontalFrame(fMain,200,40);
00038   TGVerticalFrame *canvasFrame = new TGVerticalFrame(hframe,200,40);
00039   TGVerticalFrame *buttonFrame = new TGVerticalFrame(hframe,200,40);
00040   TGVerticalFrame *timeFrame = new TGVerticalFrame(hframe2,200,40);
00041   TGVerticalFrame *fileFrame = new TGVerticalFrame(hframe2,200,40);
00042   
00043   //  TGHorizontalFrame *saveFrame = new TGHorizontalFrame(fMain,200,40);
00044 
00045 //  Create Layouts
00046   TGLayoutHints *hframeLayout = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY);
00047   TGLayoutHints *hframe2Layout = new TGLayoutHints(kLHintsExpandX);
00048   TGLayoutHints *canvasLayout = new TGLayoutHints(
00049                       (kLHintsExpandX | kLHintsExpandY),25,10,40,10);
00050   TGLayoutHints *buttonLayout = new TGLayoutHints(kLHintsNoHints,15,15,10,10);
00051   TGLayoutHints *top_buttonLayout = new TGLayoutHints(kLHintsNoHints,15,15,40,10);
00052   TGLayoutHints *timeLayout = new TGLayoutHints(kLHintsExpandX,10,10,10,10);
00053   TGLayoutHints *logLayout = new TGLayoutHints((kLHintsRight),10,10,10,10);
00054   TGLayoutHints *saveasLayout = new TGLayoutHints(kLHintsExpandX,10,10,10,10);
00055   TGLayoutHints *saveLayout = new TGLayoutHints(kLHintsNoHints,40,10,10,10);
00056   TGLayoutHints *NoLayout = new TGLayoutHints(kLHintsNoHints);
00057 // Create widgets
00058   resume = new TGTextButton(buttonFrame,"&Resume");
00059   suspend = new TGTextButton(buttonFrame,"&Suspend");
00060   terminate = new TGTextButton(buttonFrame,"&Terminate");
00061   TGCheckButton * logswc = new TGCheckButton(canvasFrame,"liner <-> log");
00062   fEcanvas = new TRootEmbeddedCanvas("Ecanvas",canvasFrame,200,200);
00063   char timelbl[100];
00064   sprintf(timelbl,"StartTime::%s ",param_ptr->StartTime);
00065   //  std::cout<<timelbl<<std::endl;
00066   TGLabel *time_label  = new TGLabel (fMain,timelbl);
00067 
00069 //  TGLabel *label_saveas = new TGLabel (fMain,"save as:");
00070 //  tbuf = new TGTextBuffer(100);
00071 //  TGTextEntry  *tent = new TGTextEntry(saveFrame, tbuf);
00072 //  TGTextButton *save = new TGTextButton(saveFrame,"&Save");
00073 //  tent->Resize(200, tent->GetDefaultHeight());
00074 //  tent->SetFont("-adobe-courier-bold-r-*-*-24-*-*-*-*-*-iso8859-1");
00075 //  char test[30];
00076 //  int year,month,day,hour,min,sec;
00077 //  char *wday;
00078 //  sscanf(param_ptr->StartTime,"%d/%d/%d(%s)%02d:%02d:%02d",&year,&month,&day,&wday,&hour,&min,&sec);
00079 //  sprintf(test,"%02d_%02d_%02d",hour,min,sec);
00080 //  tbuf->AddText(0, test);
00081 
00082 
00083 
00084   
00086   resume->Connect("Clicked()","MyMainFrame",this,"DoResume()");
00087   suspend->Connect("Clicked()","MyMainFrame",this,"DoSuspend()");
00088   terminate->Connect("Clicked()","MyMainFrame",this,"DoTerminate()");
00089   logswc->Connect("Clicked()","MyMainFrame",this,"DoSwich()");
00090   // save->Connect("Clicked()","MyMainFrame",this,"DoSave()");
00091   logswc->SetState(kButtonUp);
00092 
00093 
00094 // Layout widgets
00095   buttonFrame->AddFrame(resume,top_buttonLayout);
00096   buttonFrame->AddFrame(suspend,buttonLayout);
00097   buttonFrame->AddFrame(terminate,buttonLayout);
00098   canvasFrame->AddFrame(fEcanvas,canvasLayout);
00099   canvasFrame->AddFrame(logswc,logLayout);
00100   //  saveFrame->AddFrame(tent,NoLayout);
00101   //  saveFrame->AddFrame(save,NoLayout);
00102   buttonFrame->Resize();
00103   hframe->Resize();
00104   hframe->AddFrame(canvasFrame,hframeLayout);
00105   hframe->AddFrame(buttonFrame,NoLayout);
00106   hframe2->AddFrame(timeFrame,timeLayout);
00107   //  hframe2->AddFrame(fileFrame,fileLayout);
00108 
00109   fMain->AddFrame(hframe,hframeLayout);
00110   fMain->AddFrame(hframe2,hframe2Layout);
00111   fMain->AddFrame(time_label,timeLayout);
00112   //  fMain->AddFrame(label_saveas,NoLayout);
00113   //  fMain->AddFrame(saveFrame,saveLayout);
00114 
00116   terminate->SetBackgroundColor(400);
00117   hframe->SetBackgroundColor(400);
00118   canvasFrame->SetBackgroundColor(400);
00119   buttonFrame->SetBackgroundColor(400);
00120   //        estat->SetBackgroundColor(400);
00121 
00122 
00123 
00124 // Set a name to the main frame
00125   fMain->SetWindowName("mesurement of muon lifetime!!");
00126   std::cout<<"**************"<<std::endl;
00127   std::cout<<fMain->GetWindowName()<<std::endl;
00128   std::cout<<"**************"<<std::endl;
00129 // Map all subwindows of main frame
00130   fMain->MapSubwindows();
00131 // Initialize the layout algorithm
00132   fMain->Resize(fMain->GetDefaultSize());
00133 // Map main frame
00134   fMain->MapWindow();
00135 
00136   logSW=false;
00137   DrawHist();
00138 }
00139 
00140 void MyMainFrame::DrawHist() {
00141   delete hi;
00142   //hi = new TH1D("h1","h1",250,-0.5,249.5);
00143   hi = new TH1D("h1","decay time distribution",200,-0.05,19.95);
00144   //      for(int ii=0;ii<250;ii++){
00145   for(int ii=0;ii<200;ii++){
00146     hi->SetBinContent(ii,double(param_ptr->Hist[ii]));
00147   }
00148   hi->GetXaxis()->SetTitle("decay time [us]");
00149   hi->SetEntries(param_ptr->nEvent);
00150   if(logSW)gPad->SetLogy(1); 
00151   else if(!logSW)gPad->SetLogy(0); 
00152   hi->Draw();
00153   
00154   SetLastEventTime();
00155   TLatex *t1 = new TLatex();
00156   TLatex *t2 = new TLatex();
00157   char txt[50];
00158   char txt2[50];
00159   sprintf(txt,"last event : %s",LastEvent);
00160   //      std::cout<<"lastEve"<<LastEvent<<std::endl;
00161       //      std::cout<<txt<<std::endl;
00162   sprintf(txt2,"# of Over Flow : %d",param_ptr->nOverFlow);
00163   t1->SetNDC(kTRUE);
00164   t2->SetNDC(kTRUE);
00165   t1->DrawLatex(0.3,0.8,txt);
00166   t2->DrawLatex(0.3,0.7,txt2);
00167 
00168   
00169   TCanvas *fCanvas = fEcanvas->GetCanvas();
00170   fCanvas->cd();
00171   fCanvas->Update();
00172 
00173   //std::cout << "Output file: " << gifOutputDir << std::endl;
00174   if (gifOutputDir[0]) fCanvas->SaveAs(gifOutputDir);
00175   //fCanvas->SaveAs("/daq/1_1st/lifetime/result.gif");
00176 }
00177 
00178 void MyMainFrame::SetLastEventTime() {
00179   time_t t;
00180   time(&t);
00181   ltm = localtime(&t);
00182   sprintf(LastEvent,"%02d:%02d:%02d",ltm->tm_hour,ltm->tm_min,ltm->tm_sec);
00183   //  std::cout<<"lastevent time  "<<LastEvent<<std::endl;
00184 } 
00185 void MyMainFrame::Loop() { 
00186   //  sprintf(param_ptr->StartTime,"%d %d",22,21);
00187   EventNumber_old=param_ptr->nEvent;
00188   int ii=1;
00189   while(ii==1){
00190     if(EventNumber_old<param_ptr->nEvent){
00191       EventNumber_old=param_ptr->nEvent;
00192 //      delete hi;
00193 //      hi = new TH1D("h1","h1",250,-0.5,249.5);
00194 //      for(int ii=0;ii<250;ii++){
00195 //      hi->SetBinContent(ii,double(param_ptr->Hist[ii]));
00196 //      }
00197 //      hi->SetEntries(param_ptr->nEvent);
00198 //      if(logSW)gPad->SetLogy(1); 
00199 //      else if(!logSW)gPad->SetLogy(0); 
00200 //      hi->Draw();
00201 //      TCanvas *fCanvas = fEcanvas->GetCanvas();
00202 //      fCanvas->cd();
00203 //      fCanvas->Update();
00204 //      fCanvas->SaveAs("/daq/DAQ/lifetime/gui/interface/result.gif");
00205       DrawHist();
00206     }
00207     gSystem->ProcessEvents();
00208     usleep(2000);
00209     //    std::cout<<"start time "<<param_ptr->StartTime<<std::endl;
00210   }
00211 }
00212 void MyMainFrame::DoSave(){
00213   //  std::cout<<"save"<<std::endl;
00214   
00215 //  ofstream out(tbuf->GetString());
00216 //  if(!out){
00217 //    std::cout<<"***error can't open the file"<<std::endl;
00218 //  }
00219 //  out << "test";
00220 //  out.close();
00221 //  std::cout<<tbuf->GetString()<<std::endl;
00222 //  char comment[100];
00223 //  sprintf(comment,"/daq/DAQ/lifetime/gui/save/example2a %s &",tbuf->GetString());
00224   //  tbuf->Terminate();
00225   //  std::cout<<"comment  "<<comment<<std::endl;
00226   //  std::cout<<"system"<<system(comment)<<std::endl;
00227 
00228   //  std::cout<<"check"<<std::endl;
00229 }
00230 
00231 void example(char* gifOutputDir) {
00232   //Popup the GUI...
00233   MyMainFrame *mmf = new MyMainFrame(gClient->GetRoot(),200,200);
00234   //  mmf->ShMemory();
00235   strncpy(mmf->gifOutputDir, gifOutputDir, 1024);
00236   mmf->Loop();
00237 }
00238 
00239 int main(int argc, char **argv) {
00240   if (argc <= 1) {
00241     std::cerr << "You need to spcify the out location of GIF" << std::endl
00242               << "e.g. /home/daq/monitor_group2.gif"
00243               << std::endl;
00244     exit(-1);
00245   }
00246   std::cout << "Hello output GIF will be created at "
00247             << argv[1] << std::endl;
00248 
00249   //  SharedMemory sm;
00250   //TApplication theApp("App",&argc,argv);
00251   int AppArgc = 0;
00252   char** AppArgv;
00253   
00254   TApplication theApp("App", &AppArgc, AppArgv);
00255   example(argv[1]);
00256   //theApp.Run();
00257   return 0;
00258 }
00259 
00260 
00261 int  MyMainFrame::ShMemory() {
00262   //  key = ftok("../DataTaking/viewer",PROJ_CHAR);
00263   //  key = ftok("../../CounterTest2/CounterDAQ",PROJ_CHAR);
00264   key = ftok(KEYFILE_PATH,PROJ_CHAR);
00265   if( key == -1 ) {
00266     perror("ftok()");
00267     return 1;
00268   }
00269   sm.shm_access(key,sizeof(struct SHM_PARAM),&shd_param_id,(int *)&param_ptr);    
00270   return 0;
00271 }
00272 MyMainFrame::~MyMainFrame() { 
00273   //Clean up used widgets: frames , buttons , layouthints
00274   fMain->Cleanup();
00275   delete fMain;
00276 }
00277 void MyMainFrame::DoDraw() { 
00278   DrawHist();
00279 }
00280 
00281 void MyMainFrame::DoResume(){
00282   //  std::cout<<"click Resume"<<std::endl;
00283   param_ptr->status = RESTART;
00284   //  param_ptr->status = 0;
00285 }
00286 void MyMainFrame::DoSuspend(){
00287   //  std::cout<<"click Suspend (in DoSuspend)"<<std::endl;
00288   param_ptr->status = PAUSE;
00289 }
00290 void MyMainFrame::DoTerminate(){
00291   //  std::cout<<"click Terminate"<<std::endl;
00292   param_ptr->status = FIN;
00293   gApplication->Terminate(0);    
00294 }
00295 
00296 void MyMainFrame::DoSwich(){
00297   if(logSW==false)logSW=true;
00298   else if(logSW==true)logSW=false;
00299   //  std::cout<<logSW<<std::endl;
00300   DrawHist();
00301 }

Generated on Sun Dec 14 23:32:46 2008 by doxygen 1.3.5