CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/Fireworks/Core/src/CmsShowMain.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     CmsShowMain
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Mon Dec  3 08:38:38 PST 2007
00011 // $Id: CmsShowMain.cc,v 1.193 2011/07/15 04:46:59 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <sstream>
00016 #include <boost/bind.hpp>
00017 #include <boost/program_options.hpp>
00018 #include <string.h>
00019 
00020 #include "TSystem.h"
00021 #include "TGLWidget.h"
00022 #include "TTimer.h"
00023 #include "TROOT.h"
00024 #include "TGFileDialog.h"
00025 #include "TGMsgBox.h"
00026 #include "TMonitor.h"
00027 #include "TServerSocket.h"
00028 #include "TEveLine.h"
00029 #include "TEveManager.h"
00030 #include "TFile.h"
00031 
00032 #include "Fireworks/Core/src/CmsShowMain.h"
00033 
00034 #include "Fireworks/Core/interface/FWEveViewManager.h"
00035 
00036 #include "Fireworks/Core/interface/FWTableViewManager.h"
00037 #include "Fireworks/Core/interface/FWTriggerTableViewManager.h"
00038 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00039 #include "Fireworks/Core/interface/FWViewManagerManager.h"
00040 #include "Fireworks/Core/interface/FWGUIManager.h"
00041 #include "Fireworks/Core/interface/FWLiteJobMetadataManager.h"
00042 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00043 #include "Fireworks/Core/interface/FWColorManager.h"
00044 #include "Fireworks/Core/src/FWColorSelect.h"
00045 #include "Fireworks/Core/interface/FWSelectionManager.h"
00046 #include "Fireworks/Core/interface/FWConfigurationManager.h"
00047 #include "Fireworks/Core/interface/FWMagField.h"
00048 #include "Fireworks/Core/interface/Context.h"
00049 
00050 #include "Fireworks/Core/interface/CmsShowNavigator.h"
00051 #include "Fireworks/Core/interface/CSGAction.h"
00052 #include "Fireworks/Core/interface/CSGContinuousAction.h"
00053 #include "Fireworks/Core/interface/FWLiteJobMetadataUpdateRequest.h"
00054 
00055 #include "Fireworks/Core/interface/ActionsList.h"
00056 
00057 #include "Fireworks/Core/src/CmsShowTaskExecutor.h"
00058 #include "Fireworks/Core/interface/CmsShowMainFrame.h"
00059 #include "Fireworks/Core/interface/CmsShowSearchFiles.h"
00060 
00061 #include "Fireworks/Core/interface/fwLog.h"
00062 
00063 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00064 
00065 //
00066 // constants, enums and typedefs
00067 //
00068 
00069 static const char* const kInputFilesOpt        = "input-files";
00070 static const char* const kInputFilesCommandOpt = "input-files,i";
00071 static const char* const kConfigFileOpt        = "config-file";
00072 static const char* const kConfigFileCommandOpt = "config-file,c";
00073 static const char* const kGeomFileOpt          = "geom-file";
00074 static const char* const kGeomFileCommandOpt   = "geom-file,g";
00075 static const char* const kSimGeomFileOpt       = "sim-geom-file";
00076 static const char* const kSimGeomFileCommandOpt= "sim-geom-file";
00077 static const char* const kNoConfigFileOpt      = "noconfig";
00078 static const char* const kNoConfigFileCommandOpt = "noconfig,n";
00079 static const char* const kPlayOpt              = "play";
00080 static const char* const kPlayCommandOpt       = "play,p";
00081 static const char* const kLoopOpt              = "loop";
00082 static const char* const kLoopCommandOpt       = "loop";
00083 static const char* const kDebugOpt             = "debug";
00084 static const char* const kDebugCommandOpt      = "debug,d";
00085 static const char* const kLogLevelCommandOpt   = "log";
00086 static const char* const kLogLevelOpt          = "log";
00087 static const char* const kEveOpt               = "eve";
00088 static const char* const kEveCommandOpt        = "eve";
00089 static const char* const kAdvancedRenderOpt        = "shine";
00090 static const char* const kAdvancedRenderCommandOpt = "shine,s";
00091 static const char* const kHelpOpt        = "help";
00092 static const char* const kHelpCommandOpt = "help,h";
00093 static const char* const kSoftCommandOpt = "soft";
00094 static const char* const kPortCommandOpt = "port";
00095 static const char* const kPlainRootCommandOpt = "prompt";
00096 static const char* const kRootInteractiveCommandOpt = "root-interactive,r";
00097 static const char* const kChainCommandOpt = "chain";
00098 static const char* const kLiveCommandOpt  = "live";
00099 static const char* const kFieldCommandOpt = "field";
00100 static const char* const kFreePaletteCommandOpt = "free-palette";
00101 static const char* const kAutoSaveAllViews = "auto-save-all-views";
00102 static const char* const kEnableFPE        = "enable-fpe";
00103 static const char* const kZeroWinOffsets   = "zero-window-offsets";
00104 static const char* const kNoVersionCheck   = "no-version-check";
00105 
00106 
00107 //
00108 // constructors and destructor
00109 //
00110 CmsShowMain::CmsShowMain(int argc, char *argv[]) 
00111    : CmsShowMainBase(),
00112      m_navigator(new CmsShowNavigator(*this)),
00113      m_metadataManager(new FWLiteJobMetadataManager()),
00114      m_context(new fireworks::Context(changeManager(),
00115                                       selectionManager(),
00116                                       eiManager(),
00117                                       colorManager(),
00118                                       m_metadataManager.get())),
00119      m_loadedAnyInputFile(false),
00120      m_openFile(0),
00121      m_live(false),
00122      m_liveTimer(new SignalTimer()),
00123      m_liveTimeout(600000),
00124      m_lastPointerPositionX(-999),
00125      m_lastPointerPositionY(-999),
00126      m_noVersionCheck(false)
00127 {
00128    try {
00129       TGLWidget* w = TGLWidget::Create(gClient->GetDefaultRoot(), kTRUE, kTRUE, 0, 10, 10);
00130       delete w;
00131    }
00132    catch (std::exception& iException) {
00133       std::cerr << "Failed creating an OpenGL window: " << iException.what() << "\n"
00134          "Things to check:\n"
00135          "- Is DISPLAY environment variable set?\n"
00136          "- Are OpenGL libraries installed?\n"
00137          "- If running remotely, make sure you use 'ssh -X' or 'ssh -Y'.\n"
00138          "See also: https://twiki.cern.ch/twiki/bin/viewauth/CMS/WorkBookFireworks\n";
00139       gSystem->Exit(1);
00140    }
00141 
00142    eiManager()->setContext(m_context.get());
00143 
00144    
00145    std::string descString(argv[0]);
00146    descString += " [options] <data file>\nAllowed options";
00147 
00148    namespace po = boost::program_options;
00149    po::options_description desc(descString);
00150    desc.add_options()
00151       (kInputFilesCommandOpt, po::value< std::vector<std::string> >(),   "Input root files")
00152       (kConfigFileCommandOpt, po::value<std::string>(),   "Include configuration file")
00153       (kGeomFileCommandOpt,   po::value<std::string>(),   "Include geometry file")
00154       (kNoConfigFileCommandOpt,                           "Don't load any configuration file")
00155       (kPlayCommandOpt, po::value<float>(),               "Start in play mode with given interval between events in seconds")
00156       (kPortCommandOpt, po::value<unsigned int>(),        "Listen to port for new data files to open")
00157       (kEveCommandOpt,                                    "Show Eve browser to help debug problems")
00158       (kLoopCommandOpt,                                   "Loop events in play mode")
00159       (kPlainRootCommandOpt,                              "Plain ROOT without event display")
00160       (kRootInteractiveCommandOpt,                        "Enable root interactive prompt")
00161       (kDebugCommandOpt,                                  "Start the display from a debugger and produce a crash report")
00162       (kEnableFPE,                                        "Enable detection of floating-point exceptions")
00163       (kLogLevelCommandOpt, po::value<unsigned int>(),    "Set log level starting from 0 to 4 : kDebug, kInfo, kWarning, kError")
00164       (kAdvancedRenderCommandOpt,                         "Use advance options to improve rendering quality       (anti-alias etc)")
00165       (kSoftCommandOpt,                                   "Try to force software rendering to avoid problems with bad hardware drivers")
00166       (kChainCommandOpt, po::value<unsigned int>(),       "Chain up to a given number of recently open files. Default is 1 - no chain")
00167       (kLiveCommandOpt,                                   "Enforce playback mode if a user is not using display")
00168       (kFieldCommandOpt, po::value<double>(),             "Set magnetic field value explicitly. Default is auto-field estimation")
00169       (kFreePaletteCommandOpt,                            "Allow free color selection (requires special configuration!)")
00170       (kAutoSaveAllViews, po::value<std::string>(),       "Auto-save all views with given prefix (run_event_lumi_view.png is appended)")
00171       (kZeroWinOffsets,                                   "Disable auto-detection of window position offsets.")
00172       (kNoVersionCheck,                                   "No file version check.")
00173       (kSimGeomFileCommandOpt,po::value<std::string>(),   "Set simulation geometry file to browse")
00174       (kHelpCommandOpt,                                   "Display help message");
00175    po::positional_options_description p;
00176    p.add(kInputFilesOpt, -1);
00177 
00178    int newArgc = argc;
00179    char **newArgv = argv;
00180    po::variables_map vm;
00181    //po::store(po::parse_command_line(newArgc, newArgv, desc), vm);
00182    //po::notify(vm);
00183    po::store(po::command_line_parser(newArgc, newArgv).
00184              options(desc).positional(p).run(), vm);
00185    po::notify(vm);
00186    if(vm.count(kHelpOpt)) {
00187       std::cout << desc <<std::endl;
00188       exit(0);
00189    }
00190       
00191    if(vm.count(kLogLevelOpt)) {
00192       fwlog::LogLevel level = (fwlog::LogLevel)(vm[kLogLevelOpt].as<unsigned int>());
00193       fwlog::setPresentLogLevel(level);
00194    }
00195 
00196    if(vm.count(kPlainRootCommandOpt)) {
00197       fwLog(fwlog::kInfo) << "Plain ROOT prompt requested" << std::endl;
00198       return;
00199    }
00200 
00201    const char* cmspath = gSystem->Getenv("CMSSW_BASE");
00202    if(0 == cmspath) {
00203       throw std::runtime_error("CMSSW_BASE environment variable not set");
00204    }
00205 
00206    // input file
00207    if (vm.count(kInputFilesOpt)) {
00208       m_inputFiles = vm[kInputFilesOpt].as< std::vector<std::string> >();
00209    }
00210 
00211    if (!m_inputFiles.size())
00212       fwLog(fwlog::kInfo) << "No data file given." << std::endl;
00213    else if (m_inputFiles.size() == 1)
00214       fwLog(fwlog::kInfo) << "Input " << m_inputFiles.front() << std::endl;
00215    else
00216       fwLog(fwlog::kInfo) << m_inputFiles.size() << " input files; first: " << m_inputFiles.front() << ", last: " << m_inputFiles.back() << std::endl;
00217 
00218    // configuration file
00219    if (vm.count(kConfigFileOpt)) {
00220       setConfigFilename(vm[kConfigFileOpt].as<std::string>());
00221       if (access(configFilename(), R_OK) == -1)
00222       {
00223          fwLog(fwlog::kError) << "Specified configuration file does not exist. Quitting.\n";
00224          exit(1);
00225       }
00226    } else {
00227       if (vm.count(kNoConfigFileOpt)) {
00228          fwLog(fwlog::kInfo) << "No configuration is loaded, show everything.\n";
00229          setConfigFilename("");
00230       } else
00231          setConfigFilename("default.fwc");
00232    }
00233    fwLog(fwlog::kInfo) << "Config "  <<  configFilename() << std::endl;
00234 
00235    // geometry
00236    if (vm.count(kGeomFileOpt)) {
00237       setGeometryFilename(vm[kGeomFileOpt].as<std::string>());
00238    } else {
00239       fwLog(fwlog::kInfo) << "No geom file name.  Choosing default.\n";
00240       setGeometryFilename("cmsGeom10.root");
00241    }
00242    fwLog(fwlog::kInfo) << "Geom " << geometryFilename() << std::endl;
00243 
00244    if (vm.count(kSimGeomFileOpt)) {
00245       setSimGeometryFilename(vm[kSimGeomFileOpt].as<std::string>());
00246    } else {
00247       setSimGeometryFilename("cmsSimGeom-14.root");
00248    }
00249    // Free-palette palette
00250    if (vm.count(kFreePaletteCommandOpt)) {
00251       FWColorPopup::EnableFreePalette();
00252       fwLog(fwlog::kInfo) << "Palette restriction removed on user request!\n";
00253    }
00254    bool eveMode = vm.count(kEveOpt);
00255 
00256    //Delay creating guiManager and enabling autoloading until here so that if we have a 'help' request we don't
00257    // open any graphics or build dictionaries
00258    AutoLibraryLoader::enable();
00259 
00260    TEveManager::Create(kFALSE, "FIV");
00261 
00262    setup(m_navigator.get(), m_context.get(), m_metadataManager.get());
00263 
00264    if (vm.count(kZeroWinOffsets))
00265    {
00266       guiManager()->resetWMOffsets();
00267       fwLog(fwlog::kInfo) << "Window offsets reset on user request!\n";
00268    }
00269 
00270    if ( vm.count(kAdvancedRenderOpt) ) 
00271    {
00272       TEveLine::SetDefaultSmooth(kTRUE);
00273    }
00274 
00275    //figure out where to find macros
00276    //tell ROOT where to find our macros
00277    CmsShowTaskExecutor::TaskFunctor f;
00278    // first check if port is not occupied
00279    if (vm.count(kPortCommandOpt)) {      
00280       f=boost::bind(&CmsShowMain::setupSocket, this, vm[kPortCommandOpt].as<unsigned int>());    
00281       startupTasks()->addTask(f);        
00282    }
00283     
00284    f=boost::bind(&CmsShowMainBase::loadGeometry,this);
00285    startupTasks()->addTask(f);
00286    f=boost::bind(&CmsShowMainBase::setupViewManagers,this);
00287    startupTasks()->addTask(f);
00288    f=boost::bind(&CmsShowMainBase::setupConfiguration,this);
00289    startupTasks()->addTask(f);
00290    f=boost::bind(&CmsShowMain::setupDataHandling,this);
00291    startupTasks()->addTask(f);
00292 
00293    if (vm.count(kLoopOpt))
00294       setPlayLoop();
00295 
00296    if (eveMode) {
00297       f = boost::bind(&CmsShowMainBase::setupDebugSupport,this);
00298       startupTasks()->addTask(f);
00299    }
00300    if(vm.count(kChainCommandOpt)) {
00301       f = boost::bind(&CmsShowNavigator::setMaxNumberOfFilesToChain, m_navigator.get(), vm[kChainCommandOpt].as<unsigned int>());
00302       startupTasks()->addTask(f);
00303    }
00304    if (vm.count(kPlayOpt)) {
00305       f = boost::bind(&CmsShowMainBase::setupAutoLoad, this, vm[kPlayOpt].as<float>());
00306       startupTasks()->addTask(f);
00307    }
00308 
00309    if(vm.count(kLiveCommandOpt))
00310    {
00311       f = boost::bind(&CmsShowMain::setLiveMode, this);
00312       startupTasks()->addTask(f);
00313    }
00314       
00315    if(vm.count(kFieldCommandOpt)) 
00316    {
00317       m_context->getField()->setSource(FWMagField::kUser);
00318       m_context->getField()->setUserField(vm[kFieldCommandOpt].as<double>());
00319    }
00320    if(vm.count(kAutoSaveAllViews)) {
00321       std::string fmt = vm[kAutoSaveAllViews].as<std::string>();
00322       fmt += "%d_%d_%d_%s.png";
00323       setAutoSaveAllViewsFormat(fmt);
00324    }
00325    if(vm.count(kNoVersionCheck)) {
00326       m_noVersionCheck=true;
00327    }
00328    if(vm.count(kEnableFPE)) {
00329       gSystem->SetFPEMask();
00330    }
00331 
00332    if (vm.count(kPortCommandOpt)) {      
00333       f=boost::bind(&CmsShowMain::connectSocket, this);          
00334       startupTasks()->addTask(f);        
00335    }
00336 
00337    startupTasks()->startDoingTasks();
00338 }
00339 
00340 //
00341 // Destruction
00342 //
00343 
00344 CmsShowMain::~CmsShowMain()
00345 {
00346    //avoids a seg fault from eve which happens if eve is terminated after the GUI is gone
00347    selectionManager()->clearSelection();
00348 }
00349 
00350 class DieTimer : public TTimer
00351 {
00352 protected:
00353    CmsShowMain* fApp;
00354 public:
00355    DieTimer(CmsShowMain* app) : TTimer(), fApp(app)
00356    {
00357       Start(0, kTRUE);
00358    }
00359 
00360    virtual Bool_t Notify()
00361    {
00362       TurnOff();
00363       fApp->doExit();
00364       delete this;
00365       return kFALSE;
00366    }
00367 };
00368 
00369 void CmsShowMain::quit()
00370 {
00371    new DieTimer(this);
00372 }
00373 
00374 void CmsShowMain::doExit()
00375 {
00376    // pre terminate eve
00377    m_context->deleteEveElements();
00378    guiManager()->evePreTerminate();
00379 
00380    // sleep at least 150 ms
00381    // windows in ROOT GUI are destroyed in 150 ms timeout after
00382    gSystem->Sleep(151);
00383    gSystem->ProcessEvents();
00384    gSystem->ExitLoop();
00385 }
00386 
00387 //
00388 // assignment operators
00389 //
00390 // const CmsShowMain& CmsShowMain::operator=(const CmsShowMain& rhs)
00391 // {
00392 //   //An exception safe implementation is
00393 //   CmsShowMain temp(rhs);
00394 //   swap(rhs);
00395 //
00396 //   return *this;
00397 // }
00398 
00399 //
00400 // member functions
00401 //
00402 
00403 const fwlite::Event* 
00404 CmsShowMain::getCurrentEvent() const
00405 {
00406    if (m_navigator.get())
00407      return static_cast<const fwlite::Event*>(m_navigator->getCurrentEvent());
00408    return 0;
00409 }
00410 
00411 void
00412 CmsShowMain::fileChangedSlot(const TFile *file)
00413 {
00414    m_openFile = file;
00415    if (file)
00416       guiManager()->titleChanged(m_navigator->frameTitle());
00417 
00418    m_metadataManager->update(new FWLiteJobMetadataUpdateRequest(getCurrentEvent(), m_openFile));
00419 }
00420 
00421 void
00422 CmsShowMain::eventChangedImp()
00423 {
00424    CmsShowMainBase::eventChangedImp();
00425    guiManager()->titleChanged(m_navigator->frameTitle());
00426    m_metadataManager->update(new FWLiteJobMetadataUpdateRequest(getCurrentEvent(), m_openFile));
00427 }
00428 
00429 void CmsShowMain::resetInitialization() {
00430    //printf("Need to reset\n");
00431 }
00432 
00433 void CmsShowMain::openData()
00434 {
00435    const char* kRootType[] = {"ROOT files","*.root", 0, 0};
00436    TGFileInfo fi;
00437    fi.fFileTypes = kRootType;
00438    /* this is how things used to be done:
00439       fi.fIniDir = ".";
00440       this is bad because the destructor calls delete[] on fIniDir.
00441     */
00442    fi.fIniDir = new char[128];
00443    strncpy(fi.fIniDir, ".", 127);  
00444    guiManager()->updateStatus("waiting for data file ...");
00445    new TGFileDialog(gClient->GetDefaultRoot(), guiManager()->getMainFrame(), kFDOpen, &fi);
00446    guiManager()->updateStatus("loading file ...");
00447    if (fi.fFilename) {
00448       m_navigator->openFile(fi.fFilename);
00449       m_loadedAnyInputFile = true;
00450       m_navigator->firstEvent();
00451       checkPosition();
00452       draw();
00453    }
00454    guiManager()->clearStatus();
00455 }
00456 
00457 void CmsShowMain::appendData()
00458 {
00459    const char* kRootType[] = {"ROOT files","*.root", 0, 0};
00460    TGFileInfo fi;
00461    fi.fFileTypes = kRootType;
00462    /* this is how things used to be done:
00463       fi.fIniDir = ".";
00464       this is bad because the destructor calls delete[] on fIniDir.
00465    */
00466    fi.fIniDir = new char[128];
00467    strncpy(fi.fIniDir,  ".", 127);
00468    guiManager()->updateStatus("waiting for data file ...");
00469    new TGFileDialog(gClient->GetDefaultRoot(), guiManager()->getMainFrame(), kFDOpen, &fi);
00470    guiManager()->updateStatus("loading file ...");
00471    if (fi.fFilename) {
00472       m_navigator->appendFile(fi.fFilename, false, false);
00473       m_loadedAnyInputFile = true;
00474       checkPosition();
00475       draw();
00476       guiManager()->titleChanged(m_navigator->frameTitle());
00477    }
00478    guiManager()->clearStatus();
00479 }
00480 
00481 void
00482 CmsShowMain::openDataViaURL()
00483 {
00484    if (m_searchFiles.get() == 0) {
00485       m_searchFiles = std::auto_ptr<CmsShowSearchFiles>(new CmsShowSearchFiles("",
00486                                                                                "Open Remote Data Files",
00487                                                                                guiManager()->getMainFrame(),
00488                                                                                500, 400));
00489       m_searchFiles->CenterOnParent(kTRUE,TGTransientFrame::kBottomRight);
00490    }
00491    std::string chosenFile = m_searchFiles->chooseFileFromURL();
00492    if(!chosenFile.empty()) {
00493       guiManager()->updateStatus("loading file ...");
00494       if(m_navigator->openFile(chosenFile.c_str())) {
00495          m_navigator->firstEvent();
00496          checkPosition();
00497          draw();
00498          guiManager()->clearStatus();
00499       } else {
00500          guiManager()->updateStatus("failed to load data file");
00501       }
00502    }
00503 }
00504 
00505 //
00506 // const member functions
00507 //
00508 
00509 //_______________________________________________________________________________
00510 void 
00511 CmsShowMain::autoLoadNewEvent()
00512 {
00513    stopAutoLoadTimer();
00514    
00515    // case when start with no input file
00516    if (!m_loadedAnyInputFile)
00517    {
00518       if (m_monitor.get()) 
00519          startAutoLoadTimer();
00520       return;
00521    }
00522 
00523    bool reachedEnd = (forward() && m_navigator->isLastEvent()) || (!forward() && m_navigator->isFirstEvent());
00524 
00525    if (loop() && reachedEnd)
00526    {
00527       forward() ? m_navigator->firstEvent() : m_navigator->lastEvent();
00528       draw();
00529    }
00530    else if (!reachedEnd)
00531    {
00532       forward() ? m_navigator->nextEvent() : m_navigator->previousEvent();
00533       draw();
00534    }
00535 
00536    // stop loop in case no loop or monitor mode
00537    if (reachedEnd && (loop() || m_monitor.get()) == kFALSE)
00538    {
00539       if (forward() && m_navigator->isLastEvent())
00540       {
00541          guiManager()->enableActions();
00542          checkPosition();
00543       }
00544 
00545       if ((!forward()) && m_navigator->isFirstEvent())
00546       {
00547          guiManager()->enableActions();
00548          checkPosition();
00549       }
00550    }
00551    else
00552       startAutoLoadTimer();
00553 }
00554 
00555 //______________________________________________________________________________
00556 
00557 void 
00558 CmsShowMain::checkPosition()
00559 {
00560    if ((m_monitor.get() || loop() ) && isPlaying())
00561       return;
00562    
00563    guiManager()->getMainFrame()->enableNavigatorControls();
00564 
00565    if (m_navigator->isFirstEvent())
00566       guiManager()->disablePrevious();
00567 
00568    if (m_navigator->isLastEvent())
00569    {
00570       guiManager()->disableNext();
00571       // force enable play events action in --port mode
00572       if (m_monitor.get() && !guiManager()->playEventsAction()->isEnabled())
00573          guiManager()->playEventsAction()->enable();
00574    }
00575 }
00576 
00577 //==============================================================================
00578 void
00579 CmsShowMain::setupDataHandling()
00580 {
00581    guiManager()->updateStatus("Setting up data handling...");
00582 
00583 
00584    // navigator filtering  ->
00585    m_navigator->fileChanged_.connect(boost::bind(&CmsShowMain::fileChangedSlot, this, _1));
00586    m_navigator->editFiltersExternally_.connect(boost::bind(&FWGUIManager::updateEventFilterEnable, guiManager(), _1));
00587    m_navigator->filterStateChanged_.connect(boost::bind(&CmsShowMain::navigatorChangedFilterState, this, _1));
00588    m_navigator->postFiltering_.connect(boost::bind(&CmsShowMain::postFiltering, this));
00589 
00590    // navigator fitlering <-
00591    guiManager()->showEventFilterGUI_.connect(boost::bind(&CmsShowNavigator::showEventFilterGUI, m_navigator.get(),_1));
00592    guiManager()->filterButtonClicked_.connect(boost::bind(&CmsShowMain::filterButtonClicked,this));
00593 
00594    // Data handling. File related and therefore not in the base class.
00595    if (guiManager()->getAction(cmsshow::sOpenData)    != 0) 
00596       guiManager()->getAction(cmsshow::sOpenData)->activated.connect(sigc::mem_fun(*this, &CmsShowMain::openData));
00597    if (guiManager()->getAction(cmsshow::sAppendData)  != 0) 
00598       guiManager()->getAction(cmsshow::sAppendData)->activated.connect(sigc::mem_fun(*this, &CmsShowMain::appendData));
00599    if (guiManager()->getAction(cmsshow::sSearchFiles) != 0)
00600       guiManager()->getAction(cmsshow::sSearchFiles)->activated.connect(sigc::mem_fun(*this, &CmsShowMain::openDataViaURL));
00601 
00602    setupActions();
00603    // init data from  CmsShowNavigator configuration, can do this with signals since there were not connected yet
00604    guiManager()->setFilterButtonIcon(m_navigator->getFilterState());
00605 
00606    for (unsigned int ii = 0; ii < m_inputFiles.size(); ++ii)
00607    {
00608       const std::string& fname = m_inputFiles[ii];
00609       if (fname.empty())
00610          continue;
00611       guiManager()->updateStatus("loading data file ...");
00612       if (!m_navigator->appendFile(fname, false, false))
00613       {
00614          guiManager()->updateStatus("failed to load data file");
00615       }
00616       else
00617       {
00618          m_loadedAnyInputFile = true;
00619 
00620       }
00621    }
00622 
00623    if (m_loadedAnyInputFile)
00624    {
00625       m_navigator->firstEvent();
00626       checkPosition();
00627       draw();
00628    }
00629    else if (m_monitor.get() == 0 && (eiManager()->begin() != eiManager()->end()) )
00630       openData();
00631 }
00632 
00633 void
00634 CmsShowMain::setupSocket(unsigned int iSocket)
00635 {
00636    m_monitor = std::auto_ptr<TMonitor>(new TMonitor);
00637    TServerSocket* server = new TServerSocket(iSocket,kTRUE);
00638    if (server->GetErrorCode())
00639    {
00640       fwLog(fwlog::kError) << "CmsShowMain::setupSocket, can't create socket on port "<< iSocket << "." << std::endl;
00641       exit(0);
00642    }    
00643    m_monitor->Add(server);
00644 }
00645 
00646 void
00647 CmsShowMain::connectSocket()
00648 {
00649   m_monitor->Connect("Ready(TSocket*)","CmsShowMain",this,"notified(TSocket*)");
00650 
00651 }
00652 
00653 void
00654 CmsShowMain::notified(TSocket* iSocket)
00655 {
00656    TServerSocket* server = dynamic_cast<TServerSocket*> (iSocket);
00657    if (server)
00658    {
00659       TSocket* connection = server->Accept();
00660       if (connection)
00661       {
00662          m_monitor->Add(connection);
00663          std::stringstream s;
00664          s << "received connection from "<<iSocket->GetInetAddress().GetHostName();
00665          guiManager()->updateStatus(s.str().c_str());
00666       }
00667    }
00668    else
00669    {
00670       char buffer[4096];
00671       memset(buffer,0,sizeof(buffer));
00672       if (iSocket->RecvRaw(buffer, sizeof(buffer)) <= 0)
00673       {
00674          m_monitor->Remove(iSocket);
00675          //std::stringstream s;
00676          //s << "closing connection to "<<iSocket->GetInetAddress().GetHostName();
00677          //m_guiManager->updateStatus(s.str().c_str());
00678          delete iSocket;
00679          return;
00680       }
00681       std::string fileName(buffer);
00682       std::string::size_type lastNonSpace = fileName.find_last_not_of(" \n\t");
00683       if (lastNonSpace != std::string::npos)
00684       {
00685          fileName.erase(lastNonSpace+1);
00686       }
00687 
00688       std::stringstream s;
00689       s <<"New file notified '"<<fileName<<"'";
00690       guiManager()->updateStatus(s.str().c_str());
00691 
00692       bool appended = m_navigator->appendFile(fileName, true, m_live);
00693 
00694       if (appended)
00695       {
00696          if (m_live && isPlaying() && forward())
00697             m_navigator->activateNewFileOnNextEvent();
00698          else if (!isPlaying())
00699             checkPosition();
00700 
00701          // bootstrap case: --port  and no input file
00702          if (!m_loadedAnyInputFile)
00703          {
00704             m_loadedAnyInputFile = true;
00705             m_navigator->firstEvent();
00706             if (!isPlaying())
00707                draw();
00708          }
00709 
00710          std::stringstream sr;
00711          sr <<"New file registered '"<<fileName<<"'";
00712          guiManager()->updateStatus(sr.str().c_str());
00713       }
00714       else
00715       {
00716          std::stringstream sr;
00717          sr <<"New file NOT registered '"<<fileName<<"'";
00718          guiManager()->updateStatus(sr.str().c_str());
00719       }
00720    }
00721 }
00722 
00723 void
00724 CmsShowMain::stopPlaying()
00725 {
00726    stopAutoLoadTimer();
00727    if (m_live)
00728       m_navigator->resetNewFileOnNextEvent();
00729    setIsPlaying(false);
00730    guiManager()->enableActions();
00731    checkPosition();
00732 }
00733 
00734 void
00735 CmsShowMain::navigatorChangedFilterState(int state)
00736 {
00737    guiManager()->setFilterButtonIcon(state);
00738    if (m_navigator->filesNeedUpdate() == false)
00739    {
00740       guiManager()->setFilterButtonText(m_navigator->filterStatusMessage());
00741       checkPosition();
00742    }
00743 }
00744 
00745 void
00746 CmsShowMain::filterButtonClicked()
00747 {
00748    if (m_navigator->getFilterState() == CmsShowNavigator::kWithdrawn )
00749       guiManager()->showEventFilterGUI();
00750    else
00751       m_navigator->toggleFilterEnable();
00752 }
00753 
00754 void
00755 CmsShowMain::preFiltering()
00756 {
00757    // called only if filter has changed
00758    guiManager()->updateStatus("Filtering events");
00759 }
00760 
00761 void
00762 CmsShowMain::postFiltering()
00763 {
00764    // called only filter is changed
00765    guiManager()->clearStatus();
00766    draw();
00767    checkPosition();
00768    guiManager()->setFilterButtonText(m_navigator->filterStatusMessage());
00769 }
00770 
00771 //______________________________________________________________________________
00772 
00773 void
00774 CmsShowMain::setLiveMode()
00775 {
00776    m_live = true;
00777    m_liveTimer.reset(new SignalTimer());
00778    m_liveTimer->timeout_.connect(boost::bind(&CmsShowMain::checkLiveMode,this));
00779 
00780    Window_t rootw, childw;
00781    Int_t root_x, root_y, win_x, win_y;
00782    UInt_t mask;
00783    gVirtualX->QueryPointer(gClient->GetDefaultRoot()->GetId(),
00784                            rootw, childw,
00785                            root_x, root_y,
00786                            win_x, win_y,
00787                            mask);
00788 
00789 
00790    m_liveTimer->SetTime(m_liveTimeout);
00791    m_liveTimer->Reset();
00792    m_liveTimer->TurnOn();
00793 }
00794 
00795 void
00796 CmsShowMain::checkLiveMode()
00797 {
00798    m_liveTimer->TurnOff();
00799 
00800    Window_t rootw, childw;
00801    Int_t root_x, root_y, win_x, win_y;
00802    UInt_t mask;
00803    gVirtualX->QueryPointer(gClient->GetDefaultRoot()->GetId(),
00804                            rootw, childw,
00805                            root_x, root_y,
00806                            win_x, win_y,
00807                            mask);
00808 
00809 
00810    if ( !isPlaying() &&
00811         m_lastPointerPositionX == root_x && 
00812         m_lastPointerPositionY == root_y )
00813    {
00814       guiManager()->playEventsAction()->switchMode();
00815    }
00816 
00817    m_lastPointerPositionX = root_x;
00818    m_lastPointerPositionY = root_y;
00819 
00820 
00821    m_liveTimer->SetTime((Long_t)(m_liveTimeout));
00822    m_liveTimer->Reset();
00823    m_liveTimer->TurnOn();
00824 }