GUI/TFndMonFrame.cxx

00001 // @(#)fROOT/GUI:$Name:  $:$Id: TFndMonFrame.cxx,v 1.19 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Revision Author: Diego Faso <mailto:faso@to.infn.it>, 2006/05/16
00003 // Original Author: Piergiorgio Cerello <mailto:cerello@to.infn.it>, 2002
00004 
00006 //     GUI Monitor base class for the FINUDA online monitoring       //
00007 //                                                                   //
00008 //      - Mouse actions are considered                               //
00009 //                                                                   //
00010 //      - The CloseCanvas mouse-action is redirected to the          //
00011 //        local method CloseMainCanvas in order to guarantee an      //
00012 //        appropriated "close-delete-reinitialize" sequence          //
00013 //                                                                   //
00014 //      - Automatic update is available                              //
00015 //        by means of the virtual method UpdateDaughters             //
00016 //                                                                   //
00018 
00019 #include <TROOT.h>
00020 #include <TSystem.h>
00021 #include <TCanvas.h>
00022 #include <TH1F.h>
00023 #include <TH2F.h>
00024 #include <TH1D.h>
00025 #include <TList.h>
00026 #include <TText.h>
00027 #include <TPaveText.h>
00028 #include <TPaveStats.h>
00029 #include <TTimer.h>
00030 #include <TThread.h>
00031 
00032 //#include "TROOT.h"
00033 
00034 #include "FROOT.h"
00035 #include "TFndRun.h"
00036 #include "TFndMonFrame.h" 
00037 
00038 ClassImp(TFndMonFrame)
00039 
00040 //_______________________________________________
00041 TFndMonFrame::TFndMonFrame(TString filename, TString filename2):
00042   fRF(),fRF2(),fIsOnline(),fCanStartGUI(),fClosed(kTRUE),
00043   fMainCanvCoord(),fMainCanvas(),fMainRootCanvas(),
00044   fDisplayPad(),fUpdateFlashCol(),fHighlightCol(),
00045   fBigCanvas(),fBigRootCanvas(),
00046   fBigHis1F(),fBigHis2F(),fBigHis3F(),fBigHis1D(),
00047   fTimer(),fUpdateInterval(),fAutoUpdate(),fWhichPop()
00048 {
00049   // The default constructor is overloaded by this constructor
00050   // if files are specified, they are opened and kept in memory
00051   //   (data-members: fRF and fRF2)
00052   // NOTE: "root-files" only are accepted
00053   // Constructor for online monitoring: both arguments equal to zero
00054 
00055   if(filename.IsNull() && filename2.IsNull()){
00056     fIsOnline = kTRUE;  
00057     fCanStartGUI = kTRUE;
00058   }
00059   fAutoUpdate = fIsOnline;
00060 
00061   //  Printf("DEBUG!!!!!!!!! \"%s\" --- \"%s\"",filename.Data(),filename2.Data());
00062   // FROOT::ExpandPathName(...) is used for backward compatibility
00063   if(!filename.IsNull()){
00064     if(gSystem->AccessPathName(FROOT::ExpandPathName(filename.Data()))){
00065       Printf("file \"%s\" not found: can't open the monitor GUI.",filename.Data());
00066       fCanStartGUI = kFALSE;
00067     }
00068     else{
00069       fRF = new TFile(filename);
00070       fCanStartGUI = kTRUE;
00071     }
00072   }
00073   // ---
00074   if(!filename2.IsNull()){
00075     if(gSystem->AccessPathName(FROOT::ExpandPathName(filename2))){
00076       Printf("file \"%s\" not found: can't open the monitor GUI.",filename2.Data());
00077       fCanStartGUI = kFALSE;
00078     }
00079     else{
00080       fRF2 = new TFile(filename2);
00081       fCanStartGUI = kTRUE;
00082     }  
00083   }  
00084 
00085   InitAll();
00086 }
00087 
00088 //____________________________________
00089 TFndMonFrame::~TFndMonFrame() {
00090 
00091   delete fBigHis1F;
00092   delete fBigHis2F;
00093   delete fBigHis3F;
00094   delete fBigHis1D;
00095 
00096   if(fRF) { fRF->Close(); fRF->Delete(); }
00097   delete fRF;
00098   if(fRF2) { fRF2->Close(); fRF2->Delete(); }
00099   delete fRF2;
00100   for(Int_t i=0;i<2;i++){  
00101     if(!fClosed) delete fDisplayPad[i];
00102     delete fMainCanvas[i];
00103   }
00104 
00105   delete fBigCanvas;
00106 
00107   delete fTimer;
00108 }
00109 
00110 //____________________________________
00111 TPaveStats *TFndMonFrame::GetPadStats(TVirtualPad *pad){
00112   // default (pad=0) uses gPad
00113 
00114   if(!gPad){
00115     gROOT->Warning("TFndMonFrame::GetPadStats","gPad not available");
00116     return 0;
00117   }
00118   return pad ? 
00119     (TPaveStats *)(pad->GetPrimitive("stats")):
00120     (TPaveStats *)(gPad->GetPrimitive("stats"));
00121 
00122 }
00123 
00124 //____________________________________
00125 void TFndMonFrame::SetMonitorTitle(Int_t cnum,Char_t *detector_name){
00126   // cnum: MainCanvas array index [0-1]
00127 
00128   TString mainctit = TString(detector_name);
00129   mainctit+=" Display monitor (";
00130   if (!fRF) mainctit+="ONLINE";
00131   else{
00132     mainctit+=fndrun->GetRunType();
00133     mainctit+="-";
00134     mainctit+=fndrun->GetRunNumber();
00135   }
00136   mainctit+=")";
00137   fMainCanvas[cnum]->SetTitle(mainctit.Data());
00138 }
00139 
00140 //____________________________________
00141 void TFndMonFrame::InitAll(){
00142   // All GUI is dependent on the
00143   // screen resolution (from froot-8.00.04)
00144 
00145   /*
00146   fMainCanvCoord[0] = 305;
00147   fMainCanvCoord[1] = 130;
00148   fMainCanvCoord[2] = 970;
00149   fMainCanvCoord[3] = 680;
00150   */
00151 
00152   UInt_t wstart = (UInt_t) (gClient->GetDisplayWidth() / 5 + gClient->GetDisplayWidth() / 150 );
00153   UInt_t hstart = (UInt_t) (gClient->GetDisplayHeight() / 9 + gClient->GetDisplayHeight() /  30 );
00154 
00155   fMainCanvCoord[0] = wstart;
00156   fMainCanvCoord[1] = hstart;
00157   fMainCanvCoord[2] = (UInt_t) (gClient->GetDisplayWidth())  - (UInt_t)(1.05 * wstart);  // window borders considered
00158   fMainCanvCoord[3] = (UInt_t) (gClient->GetDisplayHeight()) - (UInt_t)(1.95 * hstart); // bottom linux taskbar considered
00159 
00160   fUpdateFlashCol = 3;
00161 
00162   fHighlightCol[0]=5;
00163   fHighlightCol[1]=5;
00164 
00165   fUpdateInterval = 30; // seconds
00166   fTimer = new TTimer();  
00167   fTimer->Connect("Timeout()","TFndMonFrame",this,"RefreshSingleMonView()");
00168   //  Connect(timer, "Timeout()", "myObjectClassName",myObject, "TimerDone()");                         
00169   fTimer->Start(fUpdateInterval*1000, kFALSE);   // kTRUE:single-shot ; kFALSE:repetitive          
00170   if(fAutoUpdate) fTimer->TurnOn();
00171   else fTimer->TurnOff();
00172 }
00173 
00174 //____________________________________
00175 void TFndMonFrame::SetStyle(Int_t style_id){
00176   // You can select the style used by all
00177   // frames of the froot-online monitor GUI
00178 
00179   switch(style_id){
00180   case 0:
00181     gStyle->SetTitleW(0.75);
00182     gStyle->SetTitleH(0.08);
00183     gStyle->SetPalette(1,0);
00184     break;
00185   case 1:
00186     gStyle->SetTitleW(0.7);
00187     gStyle->SetTitleH(0.06);
00188     gStyle->SetPalette(1,0);
00189     break;  
00190   default: break;
00191   }
00192 }
00193 
00194 
00195 //____________________________________
00196 void TFndMonFrame::BuildConnections(){
00197 
00198   for(Int_t i=0;i<2;i++) if(fMainCanvas[i]){
00199     //    fMainCanvas[i]->ToggleEventStatus();
00200     fMainCanvas[i]->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","TFndMonFrame",this,"HandleMouseActions(Int_t,Int_t,Int_t,TObject*)");
00201     
00202     fMainRootCanvas[i] = (TRootCanvas*)fMainCanvas[i]->GetCanvasImp();
00203     fMainRootCanvas[i]->DontCallClose(); // disable manual close
00204     //    fMainRootCanvas[i]->Connect("CloseWindow()", "TFndMonFrame", this,"CloseMainCanvas()");
00205   }
00206   
00207 }
00208 
00209 //____________________________________
00210 void TFndMonFrame::FlashDisplayPad(Int_t padnum){
00211   // Flash on fDisplayCanvas and refresh histograms (implemented in each daughter)
00212   
00213   //  gROOT->Info("Monitor consumer","Refreshing Histograms view");
00214   
00215   if(fDisplayPad[padnum]){
00216     Int_t ColOri = fDisplayPad[padnum]->GetFillColor();
00217     fDisplayPad[padnum]->SetFillColor(fUpdateFlashCol);
00218     fDisplayPad[padnum]->Modified();
00219     fDisplayPad[padnum]->Update();
00220     gSystem->Sleep(300);
00221     fDisplayPad[padnum]->SetFillColor(ColOri);
00222     fDisplayPad[padnum]->Modified();
00223     fDisplayPad[padnum]->Update();
00224     gSystem->Sleep(300);
00225   }
00226   //  gSystem->ProcessEvents();
00227   
00228 }
00229 //____________________________________
00230 void TFndMonFrame::PopMainCanvas(){
00231   // Show the selected Main Canvas
00232   //
00233   //  ___________________________________________________________
00234   // |  Development NOTE (Diego):                                |
00235   // |   TCanvas::Pop() doesn't work with TG* classes            |
00236   // |   under Fedora/Gentoo Linux: that's why all               |
00237   // |   canvases are iconifyied before showing the selected one |
00238   // |   (it is simply a work-around..non needed under SLC)      |
00239   //  -----------------------------------------------------------
00240 
00241 
00242   // Search for active canvas and iconify them
00243 
00244   TString CanvNames[6];
00245   CanvNames[0] = "Stb_MainCanvas";
00246   CanvNames[1] = "Lmd_MainCanvas";
00247   CanvNames[2] = "tof_MainCanvas";
00248   CanvNames[3] = "TOF_MainCanvas";
00249   CanvNames[4] = "Gts_MainCanvas";
00250   CanvNames[5] = "SIL_MainCanvas";
00251 
00252   
00253   TCanvas *curcan = 0;
00254   for(Int_t i=0;i<7;i++){
00255     curcan = ((TCanvas *)gROOT->FindObjectAny(CanvNames[i].Data()));
00256     if(!curcan) continue;
00257     cout << " Iconifying ============> " << curcan->GetName() << endl;
00258     curcan->GetCanvasImp()->Iconify();
00259     curcan->Modified();
00260     curcan->Update();
00261     
00262   }
00263   curcan = 0;
00264   usleep(300000);
00265   
00266   
00267   fWhichPop=!fWhichPop;
00268   if(!fMainRootCanvas[1])fWhichPop = kFALSE;
00269   
00270   if(!fWhichPop){
00271     cout << " Now Showing ===> " << fMainCanvas[0]->GetName() << endl;
00272     fMainCanvas[0]->Show();
00273     fMainCanvas[0]->Modified();
00274     fMainCanvas[0]->Update();
00275   }
00276   else{
00277     cout << " Now Showing ===> " << fMainCanvas[1]->GetName() << endl;
00278     fMainCanvas[1]->Show();
00279     fMainCanvas[1]->Modified();
00280     fMainCanvas[1]->Update();
00281   }
00282   
00283 }
00284 
00285 //____________________________________
00286 Bool_t TFndMonFrame::SwitchUpdateMethod(){
00287   
00288   fAutoUpdate=!fAutoUpdate;
00289   cout << "Autoupdate option is now: " << fAutoUpdate << endl;
00290 
00291   if(fAutoUpdate) fTimer->TurnOn();
00292   else fTimer->TurnOff();
00293   return fAutoUpdate;
00294 
00295 }
00296 
00297 //____________________________________
00298 Int_t TFndMonFrame::CheckProducerMessages(){
00299   // Simply wait if producer is denying any consumer action
00300   // (shared text file check: see geb2hdt.C)
00301   //
00302   // return value:
00303   //              0: ok
00304   //             -1: message file not found in $FND_SHR
00305   //             -2: shared memories existent, but busy
00306 
00307 #if !defined _FND_USE_HANDLE_CONSUMER_ACTIONS
00308   return 0;
00309 #endif
00310 
00311   TString sh_msg_file = TString(gSystem->Getenv("FND_SHR"));
00312   sh_msg_file += "/.shared_msg.txt";
00313 
00314   //  Int_t interval = 10;
00315 
00316   //  while(1){
00317   FILE *f_sh = fopen(sh_msg_file.Data(),"r");
00318   if(!f_sh){
00319     cout << "Messages shared file not found." << endl;
00320     return -1;
00321   }
00322   Char_t msg[10];
00323   Int_t scan = fscanf(f_sh,"%s",msg);
00324   fclose(f_sh);
00325   
00326   if(!strcmp(msg,"go")) return 0;
00327   
00328   // --- if you are here it means that shared memories exist, but they are busy
00329   gROOT->Info("TFndMonFrame::CheckProducerMessages","Shared memories busy...waiting for producer");
00330   //  cout << "TFndMonFrame::CheckProducerMessages(): Shared memories busy...waiting for producer" << endl;
00331   //    usleep(500000);
00332   //    gSystem->Sleep(1000);
00333   //  }
00334   return -2;
00335 }
00336 
00337 //____________________________________
00338 void TFndMonFrame::HandleMouseActions(Int_t event, Int_t x, Int_t y, TObject *selected){
00339 
00340   //cout << "event: " << event << " ===> (" << x << ";" << y << "): " << selected->ClassName() << " -> " << selected->GetName() << endl;
00341   // 51: move
00342   // 53: leave pad
00343   // 52: enter subpad
00344   //
00345   //  1: left  click (press left mouse button)
00346   // 11: left  release (release left mouse button)
00347   // 61: left  double-click (after "1-11-1" combo)
00348   // 21: left  drag (keep left mouse button clicked and move mouse)
00349   //
00350   //  2: right click (press left mouse button)
00351   // 12: right release (release left mouse button)
00352   // 62: right double-click (after "1-11-1" combo)
00353   //
00354   //  3: right click (press left mouse button)
00355   // 13: right release (release left mouse button)
00356   // 63: right double-click (after "1-11-1" combo)
00357 
00358   //  if(event == 11 && !strcmp(selected->ClassName(),"TH1F"))
00359   //  cout << "Clicked on " << selected->ClassName() << " " << selected->GetName() << endl;
00360 
00361 
00362   switch(event){
00363     // ------------
00364     // --- move ---
00365   case 51: // move inside the canvas (or sub-pads)
00366     //cout << "Moving on " << selected->ClassName() << " " << selected->GetName() << endl << endl;
00367     break;
00368   case 53: // leave the main pad
00369     break;
00370   case 52: // enter sub-pad
00371     break;
00372     // -------------
00373     // --- click ---
00374   case  1: // left click (before releasing mouse button)
00375     
00376     break;
00377     //
00378   case 11: // left click (after having releasing mouse button)
00379     
00380     break;
00381     //
00382   case 61: // left double-click
00383     cout << "Double-click received on object: \"" << selected->ClassName() 
00384          << "\"; name: \"" << selected->GetName() 
00385          << "\"; title: \"" << selected->GetTitle() << "\"." << endl;
00386     
00387     if(!strncmp(selected->ClassName(),"TH",2)) EnlargeHisto(selected);
00388     if(!strncmp(selected->ClassName(),"TPave",5)) PrintPaveInfo(selected);
00389     break;
00390     //
00391   default: break;    
00392   }
00393 
00394 }
00395 
00396 //____________________________________
00397 void TFndMonFrame::EnlargeHisto(TObject *selected){
00398 
00399   //  if(fMainCanvas[0]) cout << "first  main canvas address: " << fMainCanvas[0] << endl;
00400   //  if(fMainCanvas[1]) cout << "second main canvas address: " << fMainCanvas[1] << endl;
00401   delete fBigHis1F; fBigHis1F = 0;
00402   delete fBigHis2F; fBigHis2F = 0;
00403   delete fBigHis3F; fBigHis3F = 0;
00404   delete fBigHis1D; fBigHis1D = 0;
00405 
00406   TString CurDrawOpt = "";
00407   //  if(!fBigCanvas){
00408   fBigCanvas = new TCanvas("EnlargedViewCanv1","Big Canvas 1",300,100,1000,750);
00409   fBigCanvas->ToggleEventStatus();
00410   // --- canvas main mouse actions management
00411   fBigRootCanvas = (TRootCanvas*)fBigCanvas->GetCanvasImp();
00412   fBigRootCanvas->DontCallClose(); // disable manual close
00413   fBigRootCanvas->Connect("CloseWindow()", "TFndMonFrame", this,"CloseBigCanvas()");
00414   fBigCanvas->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","TFndMonFrame",this,"FollowBigCanvas(Int_t,Int_t,Int_t,TObject*)");
00415   //CurDrawOpt = "";
00416   //  }
00417   //  fBigCanvas->cd();
00418   
00419   const Char_t *clnam = (Char_t *) selected->ClassName();
00420   if(!strcmp(clnam,"TH1F")){
00421     fBigHis1F = (TH1F *)selected->Clone("ZoomedHisto");
00422     fBigHis1F->SetLineWidth(2);
00423     fBigHis1F->SetLineColor(2);
00424     fBigHis1F->SetFillColor(5);
00425     fBigHis1F->Draw(CurDrawOpt.Data());
00426   }
00427   else if(!strcmp(clnam,"TH2F")){
00428     fBigHis2F = (TH2F *)selected->Clone("ZoomedHisto");
00429     fBigHis2F->Draw(CurDrawOpt.Data());
00430   }
00431   else if(!strcmp(clnam,"TH3F")){
00432     fBigHis3F = (TH3F *)selected->Clone("ZoomedHisto");
00433     fBigHis3F->Draw(CurDrawOpt.Data());
00434   }
00435   else if(!strcmp(clnam,"TH1D")){
00436     fBigHis1D = (TH1D *)selected->Clone("ZoomedHisto");
00437     fBigHis1D->SetLineWidth(2);
00438     fBigHis1D->SetLineColor(2);
00439     fBigHis1D->SetFillColor(5);
00440     fBigHis1D->Draw(CurDrawOpt.Data());
00441   }
00442   else{
00443     fBigCanvas->Close(); delete fBigCanvas; fBigCanvas = 0;
00444   }
00445 
00446 }
00447 
00448 //____________________________________
00449 void TFndMonFrame::PrintPaveInfo(TObject *selected){
00450   
00451   const Char_t *clnam = (Char_t *) selected->ClassName();
00452   
00453   Printf("");
00454   Printf(" ----------------------------------------");
00455   Printf(" Selected object general properties:");
00456   Printf("   Object type: \"%s\"",clnam);
00457   
00458   //  if(!strcmp(clnam,"TPaveText")){
00459   
00460   if(!strcmp(clnam,"TPaveText")){
00461     Printf("   Histogram title ====> \"%s\".",((TPaveText *)selected)->GetLine(0)->GetTitle());
00462   }
00463   else if(!strcmp(clnam,"TPaveStats")){
00464     for(Int_t i=0;i<50;i++){
00465       TText *curtxt = (TText*)(((TPaveText *)selected)->GetListOfLines())->At(i);   
00466       if(!curtxt) break;
00467       Printf("   - %s",curtxt->GetTitle());
00468     }
00469   }
00470   //  cout << "   Text Align =======> " << ((TPaveText *)selected)->GetTextAlign() << endl;
00471   //  cout << "   Text Color =======> " << ((TPaveText *)selected)->GetTextColor() << endl;
00472   //  cout << "   Text Font ========> " << ((TPaveText *)selected)->GetTextFont() << endl;  
00473   cout << " ----------------------------------------" << endl;
00474 }
00475 
00476 //____________________________________
00477 void TFndMonFrame::CloseBigCanvas(){
00478   
00479   fBigCanvas->Close();
00480   delete fBigCanvas; fBigCanvas = 0;
00481   //  cout << "The big canvas has just been closed!" << endl;
00482 }
00483 
00484 //____________________________________
00485 void TFndMonFrame::CloseMainCanvas(){
00486   for(Int_t i=0;i<2;i++){
00487     if(!fMainCanvas[i]) continue;
00488     delete fDisplayPad[i]; fDisplayPad[i] = 0;
00489     fMainCanvas[i]->Close();
00490     delete fMainCanvas[i]; fMainCanvas[i] = 0;
00491     cout << "Main canvas has just been closed! i = " << i << endl;
00492   } 
00493   fClosed = kTRUE;
00494 }
00495 
00496 //____________________________________
00497 void TFndMonFrame::FollowBigCanvas(Int_t event, Int_t x, Int_t y, TObject *selected){
00498   // You can implement this method as you need
00499   // in order to follow mouse actions in the Canvas with the enlarged histogram.
00500 
00501   //  cout << "event: " << event << " ===> (" << x << ";" << y << "): " << selected->ClassName() << " -> " << selected->GetName() << endl;
00502 
00503 }

Generated on Tue Oct 16 15:40:46 2007 by  doxygen 1.5.2