CMS 3D CMS Logo

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