CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Fireworks/Core/src/CmsShowMainBase.cc

Go to the documentation of this file.
00001 #include "Fireworks/Core/interface/CmsShowMainBase.h"
00002 
00003 #include "Fireworks/Core/interface/ActionsList.h"
00004 #include "Fireworks/Core/interface/CSGAction.h"
00005 #include "Fireworks/Core/interface/CSGContinuousAction.h"
00006 #include "Fireworks/Core/interface/CmsShowMainFrame.h"
00007 #include "Fireworks/Core/interface/CmsShowSearchFiles.h"
00008 #include "Fireworks/Core/interface/Context.h"
00009 #include "Fireworks/Core/interface/FWColorManager.h"
00010 #include "Fireworks/Core/interface/FWConfigurationManager.h"
00011 #include "Fireworks/Core/interface/FWEveViewManager.h"
00012 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00013 #include "Fireworks/Core/interface/FWGUIManager.h"
00014 #include "Fireworks/Core/interface/FWJobMetadataManager.h"
00015 #include "Fireworks/Core/interface/FWMagField.h"
00016 #include "Fireworks/Core/interface/FWBeamSpot.h"
00017 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00018 #include "Fireworks/Core/interface/FWNavigatorBase.h"
00019 #include "Fireworks/Core/interface/FWSelectionManager.h"
00020 #include "Fireworks/Core/interface/FWTableViewManager.h"
00021 #include "Fireworks/Core/interface/FWTriggerTableViewManager.h"
00022 #include "Fireworks/Core/interface/FWViewManagerManager.h"
00023 #include "Fireworks/Core/src/CmsShowTaskExecutor.h"
00024 #include "Fireworks/Core/src/FWColorSelect.h"
00025 #include "Fireworks/Core/src/SimpleSAXParser.h"
00026 #include "Fireworks/Core/interface/CmsShowCommon.h"
00027 
00028 #include "Fireworks/Core/interface/fwLog.h"
00029 
00030 #include "TGLWidget.h"
00031 #include "TGMsgBox.h"
00032 #include "TROOT.h"
00033 #include "TSystem.h"
00034 #include "TStopwatch.h"
00035 #include "TTimer.h"
00036 #include "TEveManager.h"
00037 
00038 #include <boost/bind.hpp>
00039 
00040 CmsShowMainBase::CmsShowMainBase()
00041    : 
00042      m_changeManager(new FWModelChangeManager),
00043      m_colorManager( new FWColorManager(m_changeManager.get())),
00044      m_configurationManager(new FWConfigurationManager),
00045      m_eiManager(new FWEventItemsManager(m_changeManager.get())),
00046      m_guiManager(0),
00047      m_selectionManager(new FWSelectionManager(m_changeManager.get())),
00048      m_startupTasks(new CmsShowTaskExecutor),
00049      m_viewManager(new FWViewManagerManager(m_changeManager.get(), m_colorManager.get())),
00050      m_autoLoadTimer(new SignalTimer()),
00051      m_navigatorPtr(0),
00052      m_metadataManagerPtr(0),
00053      m_contextPtr(0),
00054      m_autoLoadTimerRunning(kFALSE),
00055      m_forward(true),
00056      m_isPlaying(false),
00057      m_loop(false),
00058      m_playDelay(3.f)
00059 {
00060 }
00061 
00062 CmsShowMainBase::~CmsShowMainBase()
00063 {
00064    //avoids a seg fault from eve which happens if eve is terminated after the GUI is gone
00065    m_selectionManager->clearSelection();
00066 }
00067 
00068 void
00069 CmsShowMainBase::setupActions()
00070 { 
00071    m_guiManager->writeToPresentConfigurationFile_.connect(sigc::mem_fun(*this, &CmsShowMainBase::writeToCurrentConfigFile));
00072 
00073    // init TGSlider state before signals are connected
00074    m_guiManager->setDelayBetweenEvents(m_playDelay);
00075 
00076    m_navigatorPtr->newEvent_.connect(boost::bind(&CmsShowMainBase::eventChangedSlot, this));
00077    if (m_guiManager->getAction(cmsshow::sNextEvent) != 0)
00078       m_guiManager->getAction(cmsshow::sNextEvent)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doNextEvent));
00079    if (m_guiManager->getAction(cmsshow::sPreviousEvent) != 0)
00080       m_guiManager->getAction(cmsshow::sPreviousEvent)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doPreviousEvent));
00081    if (m_guiManager->getAction(cmsshow::sGotoFirstEvent) != 0)
00082       m_guiManager->getAction(cmsshow::sGotoFirstEvent)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doFirstEvent));
00083    if (m_guiManager->getAction(cmsshow::sGotoLastEvent) != 0)
00084       m_guiManager->getAction(cmsshow::sGotoLastEvent)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doLastEvent));
00085    if (m_guiManager->getAction(cmsshow::sQuit) != 0) 
00086       m_guiManager->getAction(cmsshow::sQuit)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::quit));
00087  
00088    m_guiManager->changedEventId_.connect(boost::bind(&CmsShowMainBase::goToRunEvent,this,_1,_2,_3));
00089    m_guiManager->playEventsAction()->started_.connect(sigc::mem_fun(*this, &CmsShowMainBase::playForward));
00090    m_guiManager->playEventsBackwardsAction()->started_.connect(sigc::mem_fun(*this,&CmsShowMainBase::playBackward));
00091    m_guiManager->loopAction()->started_.connect(sigc::mem_fun(*this,&CmsShowMainBase::setPlayLoopImp));
00092    m_guiManager->loopAction()->stopped_.connect(sigc::mem_fun(*this,&CmsShowMainBase::unsetPlayLoopImp));
00093    m_guiManager->changedDelayBetweenEvents_.connect(boost::bind(&CmsShowMainBase::setPlayDelay,this,_1));
00094    m_guiManager->playEventsAction()->stopped_.connect(sigc::mem_fun(*this,&CmsShowMainBase::stopPlaying));
00095    m_guiManager->playEventsBackwardsAction()->stopped_.connect(sigc::mem_fun(*this,&CmsShowMainBase::stopPlaying));
00096  
00097    m_autoLoadTimer->timeout_.connect(boost::bind(&CmsShowMainBase::autoLoadNewEvent, this));
00098 }
00099 
00100 void
00101 CmsShowMainBase::setupViewManagers()
00102 {
00103    guiManager()->updateStatus("Setting up view manager...");
00104 
00105    boost::shared_ptr<FWViewManagerBase> eveViewManager(new FWEveViewManager(guiManager()));
00106    eveViewManager->setContext(m_contextPtr);
00107    viewManager()->add(eveViewManager);
00108 
00109    boost::shared_ptr<FWTableViewManager> tableViewManager(new FWTableViewManager(guiManager()));
00110    configurationManager()->add(std::string("Tables"), tableViewManager.get());
00111    viewManager()->add(tableViewManager);
00112    eiManager()->goingToClearItems_.connect(boost::bind(&FWTableViewManager::removeAllItems, tableViewManager.get()));
00113 
00114    boost::shared_ptr<FWTriggerTableViewManager> triggerTableViewManager(new FWTriggerTableViewManager(guiManager()));
00115    configurationManager()->add(std::string("TriggerTables"), triggerTableViewManager.get());
00116    configurationManager()->add(std::string("L1TriggerTables"), triggerTableViewManager.get()); // AMT: added for backward compatibilty
00117    triggerTableViewManager->setContext(m_contextPtr);
00118    viewManager()->add(triggerTableViewManager);
00119 
00120     
00121    // Unfortunately, due to the plugin mechanism, we need to delay
00122    // until here the creation of the FWJobMetadataManager, because
00123    // otherwise the supportedTypesAndRepresentations map is empty.
00124    // FIXME: should we have a signal for whenever the above mentioned map
00125    //        changes? Can that actually happer (maybe if we add support
00126    //        for loading plugins on the fly??).
00127    m_metadataManagerPtr->initReps(viewManager()->supportedTypesAndRepresentations());
00128 }
00129 
00130 void
00131 CmsShowMainBase::eventChangedSlot()
00132 {
00133    eventChangedImp();
00134 }
00135 
00136 void
00137 CmsShowMainBase::eventChangedImp()
00138 {
00139    guiManager()->eventChangedCallback();
00140 }
00141 
00142 void
00143 CmsShowMainBase::doFirstEvent()
00144 {
00145    m_navigatorPtr->firstEvent();
00146    checkPosition();
00147    draw();
00148 }
00149 
00150 void
00151 CmsShowMainBase::doNextEvent()
00152 {
00153    m_navigatorPtr->nextEvent();
00154    checkPosition();
00155    draw();
00156 }
00157 
00158 void
00159 CmsShowMainBase::doPreviousEvent()
00160 {
00161    m_navigatorPtr->previousEvent();
00162    checkPosition();
00163    draw();
00164 }
00165 void
00166 CmsShowMainBase::doLastEvent()
00167 {
00168    m_navigatorPtr->lastEvent();
00169    checkPosition();
00170    draw();
00171 }
00172 
00173 void
00174 CmsShowMainBase::goToRunEvent(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event)
00175 {
00176    m_navigatorPtr->goToRunEvent(run, lumi, event);
00177    checkPosition();
00178    draw();
00179 }
00180 
00181 
00182 void
00183 CmsShowMainBase::draw()
00184 {
00185    m_guiManager->updateStatus("loading event ...");
00186 
00187    if (m_contextPtr->getField()->getSource() != FWMagField::kUser)
00188    {
00189       m_contextPtr->getField()->checkFieldInfo(m_navigatorPtr->getCurrentEvent());
00190    }
00191    m_contextPtr->getBeamSpot()->checkBeamSpot(m_navigatorPtr->getCurrentEvent());
00192 
00193    TStopwatch sw;
00194    m_viewManager->eventBegin();
00195    m_eiManager->newEvent(m_navigatorPtr->getCurrentEvent());
00196    m_viewManager->eventEnd();
00197    sw.Stop();
00198    fwLog(fwlog::kDebug) << "CmsShowMainBase::draw CpuTime " << sw.CpuTime()
00199                         <<" RealTime " << sw.RealTime() << std::endl;
00200 
00201    if (!m_autoSaveAllViewsFormat.empty())
00202    {
00203       m_guiManager->updateStatus("auto saving images ...");
00204       m_guiManager->exportAllViews(m_autoSaveAllViewsFormat);
00205    }
00206 
00207    m_guiManager->clearStatus();
00208 }
00209 
00210 void
00211 CmsShowMainBase::setup(FWNavigatorBase *navigator,
00212                        fireworks::Context *context,
00213                        FWJobMetadataManager *metadataManager)
00214 {
00215    m_navigatorPtr = navigator;
00216    m_contextPtr = context;
00217    m_metadataManagerPtr = metadataManager;
00218    
00219    m_colorManager->initialize();
00220    m_contextPtr->initEveElements();
00221    m_guiManager.reset(new FWGUIManager(m_contextPtr, m_viewManager.get(), m_navigatorPtr));
00222 
00223    m_eiManager->newItem_.connect(boost::bind(&FWModelChangeManager::newItemSlot,
00224                                              m_changeManager.get(), _1) );
00225    
00226    m_eiManager->newItem_.connect(boost::bind(&FWViewManagerManager::registerEventItem,
00227                                              m_viewManager.get(), _1));
00228    m_configurationManager->add("EventItems",m_eiManager.get());
00229    m_configurationManager->add("GUI",m_guiManager.get());
00230    m_configurationManager->add("EventNavigator", m_navigatorPtr);
00231    m_configurationManager->add("CommonPreferences", m_contextPtr->commonPrefs()); // must be after GUIManager in alphabetical order
00232 
00233    m_guiManager->writeToConfigurationFile_.connect(boost::bind(&CmsShowMainBase::writeToConfigFile,
00234                                                                 this,_1));
00235 
00236    m_guiManager->loadFromConfigurationFile_.connect(boost::bind(&CmsShowMainBase::reloadConfiguration,
00237                                                                 this, _1));
00238    std::string macPath(gSystem->Getenv("CMSSW_BASE"));
00239    macPath += "/src/Fireworks/Core/macros";
00240    const char* base = gSystem->Getenv("CMSSW_RELEASE_BASE");
00241    if(0!=base) {
00242       macPath+=":";
00243       macPath +=base;
00244       macPath +="/src/Fireworks/Core/macros";
00245    }
00246    gROOT->SetMacroPath((std::string("./:")+macPath).c_str());
00247    
00248    m_startupTasks->tasksCompleted_.connect(boost::bind(&FWGUIManager::clearStatus,
00249                                                        m_guiManager.get()) );
00250 }
00251 
00252 void
00253 CmsShowMainBase::writeToConfigFile(const std::string &name)
00254 {
00255    m_configFileName = name;
00256    m_configurationManager->writeToFile(m_configFileName);
00257 }
00258 
00259 void
00260 CmsShowMainBase::writeToCurrentConfigFile()
00261 {
00262    m_configurationManager->writeToFile(m_configFileName);
00263 }
00264 
00265 void
00266 CmsShowMainBase::reloadConfiguration(const std::string &config)
00267 {
00268    if (config.empty())
00269       return;
00270 
00271    m_configFileName = config;
00272 
00273    std::string msg = "Reloading configuration "
00274       + config + "...";
00275    fwLog(fwlog::kDebug) << msg << std::endl;
00276    m_guiManager->updateStatus(msg.c_str());
00277    m_guiManager->subviewDestroyAll();
00278    m_eiManager->clearItems();
00279    m_configFileName = config;
00280    try
00281    {
00282       gEve->DisableRedraw();
00283       m_configurationManager->readFromFile(config);
00284       gEve->EnableRedraw();
00285    }
00286    catch (std::runtime_error &e)
00287    {
00288       Int_t chosen;
00289       new TGMsgBox(gClient->GetDefaultRoot(),
00290                    gClient->GetDefaultRoot(),
00291                    "Bad configuration",
00292                    ("Configuration " + config + " cannot be parsed.").c_str(),
00293                    kMBIconExclamation,
00294                    kMBCancel,
00295                    &chosen);
00296    }
00297    catch (SimpleSAXParser::ParserError &e)
00298    {
00299       Int_t chosen;
00300       new TGMsgBox(gClient->GetDefaultRoot(),
00301                    gClient->GetDefaultRoot(),
00302                    "Bad configuration",
00303                    ("Configuration " + config + " cannot be parsed.").c_str(),
00304                    kMBIconExclamation,
00305                    kMBCancel,
00306                    &chosen);
00307    }
00308    m_guiManager->updateStatus("");
00309 }
00310 
00311 void
00312 CmsShowMainBase::setupAutoLoad(float x)
00313 {
00314    m_playDelay = x;
00315    m_guiManager->setDelayBetweenEvents(m_playDelay);
00316    if (!m_guiManager->playEventsAction()->isEnabled())
00317       m_guiManager->playEventsAction()->enable();
00318 
00319    m_guiManager->playEventsAction()->switchMode();
00320 }
00321 
00322 void
00323 CmsShowMainBase::startAutoLoadTimer()
00324 {
00325    m_autoLoadTimer->SetTime((Long_t)(m_playDelay*1000));
00326    m_autoLoadTimer->Reset();
00327    m_autoLoadTimer->TurnOn();
00328    m_autoLoadTimerRunning = kTRUE;
00329 }
00330 
00331 void 
00332 CmsShowMainBase::stopAutoLoadTimer()
00333 {
00334    m_autoLoadTimer->TurnOff();
00335    m_autoLoadTimerRunning = kFALSE;
00336 }
00337 
00338 void
00339 CmsShowMainBase::setupConfiguration()
00340 {
00341    m_guiManager->updateStatus("Setting up configuration...");
00342    if(m_configFileName.empty() ) {
00343       fwLog(fwlog::kInfo) << "no configuration is loaded." << std::endl;
00344       m_guiManager->getMainFrame()->MapSubwindows();
00345       m_guiManager->getMainFrame()->Layout();
00346       m_guiManager->getMainFrame()->MapRaised();
00347       m_configFileName = "newconfig.fwc";
00348       m_guiManager->createView("Rho Phi"); 
00349       m_guiManager->createView("Rho Z"); 
00350    }
00351    else {
00352       char* whereConfig = gSystem->Which(TROOT::GetMacroPath(), m_configFileName.c_str(), kReadPermission);
00353       if(0==whereConfig) {
00354          fwLog(fwlog::kInfo) <<"unable to load configuration file '"<<m_configFileName<<"' will load default instead."<<std::endl;
00355          whereConfig = gSystem->Which(TROOT::GetMacroPath(), "default.fwc", kReadPermission);
00356          assert(whereConfig && "Default configuration cannot be found. Malformed Fireworks installation?");
00357       }
00358       m_configFileName = whereConfig;
00359 
00360       delete [] whereConfig;
00361       try
00362       {
00363          gEve->DisableRedraw();
00364          m_configurationManager->readFromFile(m_configFileName);
00365          gEve->EnableRedraw();
00366       }
00367       catch (std::runtime_error &e)
00368       {
00369          fwLog(fwlog::kError) <<"Unable to load configuration file '" 
00370                               << m_configFileName 
00371                               << "' which was specified on command line. Quitting." 
00372                               << std::endl;
00373          exit(1);
00374       }
00375       catch (SimpleSAXParser::ParserError &e)
00376       {
00377          fwLog(fwlog::kError) <<"Unable to load configuration file '" 
00378                               << m_configFileName 
00379                               << "' which was specified on command line. Quitting." 
00380                               << std::endl;
00381          exit(1);
00382       }
00383    }
00384    /* 
00385       if(not m_configFileName.empty() ) {
00386       //when the program quits we will want to save the configuration automatically
00387       m_guiManager->goingToQuit_.connect(
00388       boost::bind(&FWConfigurationManager::writeToFile,
00389       m_configurationManager.get(),
00390       m_configFileName));
00391       }
00392    */
00393 }
00394 
00395 
00396 void
00397 CmsShowMainBase::setPlayDelay(Float_t val)
00398 {
00399    m_playDelay = val;
00400 }
00401 
00402 void
00403 CmsShowMainBase::setupDebugSupport()
00404 {
00405    m_guiManager->updateStatus("Setting up Eve debug window...");
00406    m_guiManager->openEveBrowserForDebugging();
00407 }
00408 
00409 void
00410 CmsShowMainBase::setPlayLoop()
00411 {
00412    if(!m_loop) {
00413       setPlayLoopImp();
00414       m_guiManager->loopAction()->activated();
00415    }
00416 }
00417 
00418 void
00419 CmsShowMainBase::unsetPlayLoop()
00420 {
00421    if(m_loop) {
00422       unsetPlayLoopImp();
00423       m_guiManager->loopAction()->stop();
00424    }
00425 }
00426 
00427 void
00428 CmsShowMainBase::setPlayLoopImp()
00429 {
00430    m_loop = true;
00431 }
00432 
00433 void
00434 CmsShowMainBase::unsetPlayLoopImp()
00435 {
00436    m_loop = false;
00437 }
00438 
00439 void 
00440 CmsShowMainBase::registerPhysicsObject(const FWPhysicsObjectDesc&iItem)
00441 {
00442    m_eiManager->add(iItem);
00443 }
00444 
00445 void
00446 CmsShowMainBase::playForward()
00447 {
00448    m_forward = true;
00449    m_isPlaying = true;
00450    guiManager()->enableActions(kFALSE);
00451    startAutoLoadTimer();
00452 }
00453 
00454 void
00455 CmsShowMainBase::playBackward()
00456 {
00457    m_forward = false;
00458    m_isPlaying = true;
00459    guiManager()->enableActions(kFALSE);
00460    startAutoLoadTimer();
00461 }
00462 
00463 void
00464 CmsShowMainBase::loadGeometry()
00465 {   // prepare geometry service
00466    // ATTN: this should be made configurable
00467    try 
00468    {
00469       guiManager()->updateStatus("Loading geometry...");
00470       m_geom.loadMap(m_geometryFilename.c_str());
00471       m_contextPtr->setGeom(&m_geom);
00472    }
00473    catch (const std::runtime_error& iException)
00474    {
00475       fwLog(fwlog::kError) << "CmsShowMain::loadGeometry() caught exception: \n"
00476                            << m_geometryFilename << " "
00477                            << iException.what() << std::endl;
00478       exit(0);
00479    }
00480 }