CMS 3D CMS Logo

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