CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/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 (std::runtime_error &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    catch (SimpleSAXParser::ParserError &e)
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    m_guiManager->updateStatus("");
00320 }
00321 
00322 void
00323 CmsShowMainBase::setupAutoLoad(float x)
00324 {
00325    m_playDelay = x;
00326    m_guiManager->setDelayBetweenEvents(m_playDelay);
00327    if (!m_guiManager->playEventsAction()->isEnabled())
00328       m_guiManager->playEventsAction()->enable();
00329 
00330    m_guiManager->playEventsAction()->switchMode();
00331 }
00332 
00333 void
00334 CmsShowMainBase::startAutoLoadTimer()
00335 {
00336    m_autoLoadTimer->SetTime((Long_t)(m_playDelay*1000));
00337    m_autoLoadTimer->Reset();
00338    m_autoLoadTimer->TurnOn();
00339    m_autoLoadTimerRunning = kTRUE;
00340 }
00341 
00342 void 
00343 CmsShowMainBase::stopAutoLoadTimer()
00344 {
00345    m_autoLoadTimer->TurnOff();
00346    m_autoLoadTimerRunning = kFALSE;
00347 }
00348 
00349 void
00350 CmsShowMainBase::setupConfiguration()
00351 {
00352    m_guiManager->updateStatus("Setting up configuration...");
00353    if(m_configFileName.empty() ) {
00354       fwLog(fwlog::kInfo) << "no configuration is loaded." << std::endl;
00355       m_guiManager->getMainFrame()->MapSubwindows();
00356       m_guiManager->getMainFrame()->Layout();
00357       m_guiManager->getMainFrame()->MapRaised();
00358       m_configFileName = "newconfig.fwc";
00359       m_guiManager->createView("Rho Phi"); 
00360       m_guiManager->createView("Rho Z"); 
00361    }
00362    else {
00363       char* whereConfig = gSystem->Which(TROOT::GetMacroPath(), m_configFileName.c_str(), kReadPermission);
00364       if(0==whereConfig) {
00365          fwLog(fwlog::kInfo) <<"unable to load configuration file '"<<m_configFileName<<"' will load default instead."<<std::endl;
00366          whereConfig = gSystem->Which(TROOT::GetMacroPath(), "default.fwc", kReadPermission);
00367          assert(whereConfig && "Default configuration cannot be found. Malformed Fireworks installation?");
00368       }
00369       m_configFileName = whereConfig;
00370 
00371       delete [] whereConfig;
00372       try
00373       {
00374          gEve->DisableRedraw();
00375          m_configurationManager->readFromFile(m_configFileName);
00376          gEve->EnableRedraw();
00377       }
00378       catch (std::runtime_error &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       catch (SimpleSAXParser::ParserError &e)
00387       {
00388          fwLog(fwlog::kError) <<"Unable to load configuration file '" 
00389                               << m_configFileName 
00390                               << "' which was specified on command line. Quitting." 
00391                               << std::endl;
00392          exit(1);
00393       }
00394    }
00395    /* 
00396       if(not m_configFileName.empty() ) {
00397       //when the program quits we will want to save the configuration automatically
00398       m_guiManager->goingToQuit_.connect(
00399       boost::bind(&FWConfigurationManager::writeToFile,
00400       m_configurationManager.get(),
00401       m_configFileName));
00402       }
00403    */
00404 }
00405 
00406 
00407 void
00408 CmsShowMainBase::setPlayDelay(Float_t val)
00409 {
00410    m_playDelay = val;
00411 }
00412 
00413 void
00414 CmsShowMainBase::setupDebugSupport()
00415 {
00416    m_guiManager->updateStatus("Setting up Eve debug window...");
00417    m_guiManager->openEveBrowserForDebugging();
00418 }
00419 
00420 void
00421 CmsShowMainBase::setPlayLoop()
00422 {
00423    if(!m_loop) {
00424       setPlayLoopImp();
00425       m_guiManager->loopAction()->activated();
00426    }
00427 }
00428 
00429 void
00430 CmsShowMainBase::unsetPlayLoop()
00431 {
00432    if(m_loop) {
00433       unsetPlayLoopImp();
00434       m_guiManager->loopAction()->stop();
00435    }
00436 }
00437 
00438 void
00439 CmsShowMainBase::setPlayLoopImp()
00440 {
00441    m_loop = true;
00442 }
00443 
00444 void
00445 CmsShowMainBase::unsetPlayLoopImp()
00446 {
00447    m_loop = false;
00448 }
00449 
00450 void 
00451 CmsShowMainBase::registerPhysicsObject(const FWPhysicsObjectDesc&iItem)
00452 {
00453    m_eiManager->add(iItem);
00454 }
00455 
00456 void
00457 CmsShowMainBase::playForward()
00458 {
00459    m_forward = true;
00460    m_isPlaying = true;
00461    guiManager()->enableActions(kFALSE);
00462    startAutoLoadTimer();
00463 }
00464 
00465 void
00466 CmsShowMainBase::playBackward()
00467 {
00468    m_forward = false;
00469    m_isPlaying = true;
00470    guiManager()->enableActions(kFALSE);
00471    startAutoLoadTimer();
00472 }
00473 
00474 void
00475 CmsShowMainBase::loadGeometry()
00476 {   // prepare geometry service
00477    // ATTN: this should be made configurable
00478    try 
00479    {
00480       guiManager()->updateStatus("Loading geometry...");
00481       m_geom.loadMap(m_geometryFilename.c_str());
00482       m_contextPtr->setGeom(&m_geom);
00483    }
00484    catch (const std::runtime_error& iException)
00485    {
00486       fwLog(fwlog::kError) << "CmsShowMain::loadGeometry() caught exception: \n"
00487                            << m_geometryFilename << " "
00488                            << iException.what() << std::endl;
00489       exit(0);
00490    }
00491 }
00492 
00493 void
00494 CmsShowMainBase::sendVersionInfo()
00495 {
00496    // Send version info to xrootd.t2.ucsd.edu receiver on port 9698.
00497 
00498    // receiver
00499    struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
00500    if (!h) return;
00501 
00502 
00503    struct sockaddr_in remoteServAddr;
00504    remoteServAddr.sin_family = h->h_addrtype;
00505    memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
00506    remoteServAddr.sin_port = htons(9698);
00507 
00508    // socket creation
00509    int sd = socket(AF_INET,SOCK_DGRAM, 0);
00510    if (sd  < 0) 
00511    {
00512       // std::cout << "can't create socket \n";
00513       return;
00514    }
00515    // bind any port
00516    // printf("bind port\n");
00517    struct sockaddr_in cliAddr;
00518    cliAddr.sin_family = AF_INET;
00519    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00520    cliAddr.sin_port = htons(0);
00521 
00522    int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); 
00523    if (rc < 0) {
00524       //  std::cout << "can't bind port %d " << rc << std::endl;
00525       return;
00526    }
00527 
00528    // send data 
00529    SysInfo_t sInfo;
00530    gSystem->GetSysInfo(&sInfo);
00531 
00532    char msg[128];
00533    if (gSystem->Getenv("CMSSW_VERSION"))
00534    {
00535       snprintf(msg, 64,"%s %s", gSystem->Getenv("CMSSW_VERSION"), sInfo.fOS.Data());
00536    }
00537    else
00538    {
00539       TString versionFileName("data/version.txt");
00540       fireworks::setPath(versionFileName);
00541       ifstream fs(versionFileName);
00542       TString infoText;
00543       infoText.ReadLine(fs);
00544       fs.close();
00545       snprintf(msg, 64,"Standalone %s %s", infoText.Data(), sInfo.fOS.Data() );
00546    }
00547    int flags = 0;
00548    sendto(sd, msg, strlen(msg), flags, 
00549           (struct sockaddr *) &remoteServAddr, 
00550           sizeof(remoteServAddr));
00551 }