CMS 3D CMS Logo

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

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