Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-17 23:10:44

0001 #include <fstream>
0002 #include <sys/types.h>
0003 #include <sys/socket.h>
0004 #include <netinet/in.h>
0005 #include <arpa/inet.h>
0006 #include <netdb.h>
0007 
0008 #include <functional>
0009 #include <memory>
0010 
0011 #include "TGLWidget.h"
0012 #include "TGMsgBox.h"
0013 #include "TROOT.h"
0014 #include "TSystem.h"
0015 #include "TStopwatch.h"
0016 #include "TTimer.h"
0017 #include "TEveManager.h"
0018 #include "TPRegexp.h"
0019 
0020 #include "Fireworks/Core/interface/CmsShowMainBase.h"
0021 #include "Fireworks/Core/interface/ActionsList.h"
0022 #include "Fireworks/Core/interface/CSGAction.h"
0023 #include "Fireworks/Core/interface/CSGContinuousAction.h"
0024 #include "Fireworks/Core/interface/CmsShowMainFrame.h"
0025 #include "Fireworks/Core/interface/CmsShowSearchFiles.h"
0026 #include "Fireworks/Core/interface/Context.h"
0027 #include "Fireworks/Core/interface/FWColorManager.h"
0028 #include "Fireworks/Core/interface/FWConfigurationManager.h"
0029 #include "Fireworks/Core/interface/FWEveViewManager.h"
0030 #include "Fireworks/Core/interface/FWEventItemsManager.h"
0031 #include "Fireworks/Core/interface/FWGUIManager.h"
0032 #include "Fireworks/Core/interface/FWJobMetadataManager.h"
0033 #include "Fireworks/Core/interface/FWMagField.h"
0034 #include "Fireworks/Core/interface/FWBeamSpot.h"
0035 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0036 #include "Fireworks/Core/interface/FWNavigatorBase.h"
0037 #include "Fireworks/Core/interface/FWSelectionManager.h"
0038 #include "Fireworks/Core/interface/FWTableViewManager.h"
0039 #include "Fireworks/Core/interface/FWTriggerTableViewManager.h"
0040 #include "Fireworks/Core/interface/FWGeometryTableViewManager.h"
0041 #include "Fireworks/Core/interface/FWViewManagerManager.h"
0042 #include "Fireworks/Core/interface/CmsShowTaskExecutor.h"
0043 #include "Fireworks/Core/src/FWColorSelect.h"
0044 #include "Fireworks/Core/interface/SimpleSAXParser.h"
0045 #include "Fireworks/Core/interface/CmsShowCommon.h"
0046 #include "Fireworks/Core/interface/fwLog.h"
0047 #include "Fireworks/Core/interface/fwPaths.h"
0048 #include "Fireworks/Core/interface/FWPartialConfig.h"
0049 
0050 CmsShowMainBase::CmsShowMainBase()
0051     : m_changeManager(new FWModelChangeManager),
0052       m_colorManager(new FWColorManager(m_changeManager.get())),
0053       m_configurationManager(new FWConfigurationManager),
0054       m_eiManager(new FWEventItemsManager(m_changeManager.get())),
0055       m_guiManager(nullptr),
0056       m_selectionManager(new FWSelectionManager(m_changeManager.get())),
0057       m_startupTasks(new CmsShowTaskExecutor),
0058       m_viewManager(new FWViewManagerManager(m_changeManager.get(), m_colorManager.get())),
0059       m_autoLoadTimer(new SignalTimer()),
0060       m_navigatorPtr(nullptr),
0061       m_metadataManagerPtr(nullptr),
0062       m_contextPtr(nullptr),
0063       m_autoSaveAllViewsHeight(-1),
0064       m_autoLoadTimerRunning(kFALSE),
0065       m_forward(true),
0066       m_isPlaying(false),
0067       m_loop(false),
0068       m_playDelay(3.f) {
0069   sendVersionInfo();
0070 }
0071 
0072 CmsShowMainBase::~CmsShowMainBase() noexcept(false) {}
0073 
0074 void CmsShowMainBase::setupActions() {
0075   m_guiManager->writeToPresentConfigurationFile_.connect(
0076       sigc::mem_fun(*this, &CmsShowMainBase::writeToCurrentConfigFile));
0077 
0078   // init TGSlider state before signals are connected
0079   m_guiManager->setDelayBetweenEvents(m_playDelay);
0080 
0081   m_navigatorPtr->newEvent_.connect(std::bind(&CmsShowMainBase::eventChangedSlot, this));
0082   if (m_guiManager->getAction(cmsshow::sNextEvent) != nullptr)
0083     m_guiManager->getAction(cmsshow::sNextEvent)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doNextEvent));
0084   if (m_guiManager->getAction(cmsshow::sPreviousEvent) != nullptr)
0085     m_guiManager->getAction(cmsshow::sPreviousEvent)
0086         ->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doPreviousEvent));
0087   if (m_guiManager->getAction(cmsshow::sGotoFirstEvent) != nullptr)
0088     m_guiManager->getAction(cmsshow::sGotoFirstEvent)
0089         ->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doFirstEvent));
0090   if (m_guiManager->getAction(cmsshow::sGotoLastEvent) != nullptr)
0091     m_guiManager->getAction(cmsshow::sGotoLastEvent)
0092         ->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::doLastEvent));
0093   if (m_guiManager->getAction(cmsshow::sQuit) != nullptr)
0094     m_guiManager->getAction(cmsshow::sQuit)->activated.connect(sigc::mem_fun(*this, &CmsShowMainBase::quit));
0095 
0096   m_guiManager->changedEventId_.connect(std::bind(
0097       &CmsShowMainBase::goToRunEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
0098   m_guiManager->playEventsAction()->started_.connect(sigc::mem_fun(*this, &CmsShowMainBase::playForward));
0099   m_guiManager->playEventsBackwardsAction()->started_.connect(sigc::mem_fun(*this, &CmsShowMainBase::playBackward));
0100   m_guiManager->loopAction()->started_.connect(sigc::mem_fun(*this, &CmsShowMainBase::setPlayLoopImp));
0101   m_guiManager->loopAction()->stopped_.connect(sigc::mem_fun(*this, &CmsShowMainBase::unsetPlayLoopImp));
0102   m_guiManager->changedDelayBetweenEvents_.connect(
0103       std::bind(&CmsShowMainBase::setPlayDelay, this, std::placeholders::_1));
0104   m_guiManager->playEventsAction()->stopped_.connect(sigc::mem_fun(*this, &CmsShowMainBase::stopPlaying));
0105   m_guiManager->playEventsBackwardsAction()->stopped_.connect(sigc::mem_fun(*this, &CmsShowMainBase::stopPlaying));
0106 
0107   m_autoLoadTimer->timeout_.connect(std::bind(&CmsShowMainBase::autoLoadNewEvent, this));
0108 }
0109 
0110 void CmsShowMainBase::setupViewManagers() {
0111   guiManager()->updateStatus("Setting up view manager...");
0112 
0113   std::shared_ptr<FWViewManagerBase> eveViewManager = std::make_shared<FWEveViewManager>(guiManager());
0114   eveViewManager->setContext(m_contextPtr);
0115   viewManager()->add(eveViewManager);
0116 
0117   auto tableViewManager = std::make_shared<FWTableViewManager>(guiManager());
0118   configurationManager()->add(std::string("Tables"), tableViewManager.get());
0119   viewManager()->add(tableViewManager);
0120   eiManager()->goingToClearItems_.connect(std::bind(&FWTableViewManager::removeAllItems, tableViewManager.get()));
0121 
0122   auto triggerTableViewManager = std::make_shared<FWTriggerTableViewManager>(guiManager());
0123   configurationManager()->add(std::string("TriggerTables"), triggerTableViewManager.get());
0124   configurationManager()->add(std::string("L1TriggerTables"),
0125                               triggerTableViewManager.get());  // AMT: added for backward compatibilty
0126   triggerTableViewManager->setContext(m_contextPtr);
0127   viewManager()->add(triggerTableViewManager);
0128 
0129   auto geoTableViewManager =
0130       std::make_shared<FWGeometryTableViewManager>(guiManager(), m_simGeometryFilename, m_TGeoName);
0131   geoTableViewManager->setContext(m_contextPtr);
0132   viewManager()->add(geoTableViewManager);
0133 
0134   // Unfortunately, due to the plugin mechanism, we need to delay
0135   // until here the creation of the FWJobMetadataManager, because
0136   // otherwise the supportedTypesAndRepresentations map is empty.
0137   // FIXME: should we have a signal for whenever the above mentioned map
0138   //        changes? Can that actually happer (maybe if we add support
0139   //        for loading plugins on the fly??).
0140   m_metadataManagerPtr->initReps(viewManager()->supportedTypesAndRepresentations());
0141 }
0142 
0143 void CmsShowMainBase::eventChangedSlot() { eventChangedImp(); }
0144 
0145 void CmsShowMainBase::eventChangedImp() { guiManager()->eventChangedCallback(); }
0146 
0147 void CmsShowMainBase::doFirstEvent() {
0148   m_navigatorPtr->firstEvent();
0149   checkPosition();
0150   draw();
0151 }
0152 
0153 void CmsShowMainBase::doNextEvent() {
0154   m_navigatorPtr->nextEvent();
0155   checkPosition();
0156   draw();
0157 }
0158 
0159 void CmsShowMainBase::doPreviousEvent() {
0160   m_navigatorPtr->previousEvent();
0161   checkPosition();
0162   draw();
0163 }
0164 void CmsShowMainBase::doLastEvent() {
0165   m_navigatorPtr->lastEvent();
0166   checkPosition();
0167   draw();
0168 }
0169 
0170 void CmsShowMainBase::goToRunEvent(edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event) {
0171   m_navigatorPtr->goToRunEvent(run, lumi, event);
0172   checkPosition();
0173   draw();
0174 }
0175 
0176 void CmsShowMainBase::draw() {
0177   m_guiManager->updateStatus("loading event ...");
0178 
0179   if (m_contextPtr->getField()->getSource() != FWMagField::kUser) {
0180     m_contextPtr->getField()->checkFieldInfo(m_navigatorPtr->getCurrentEvent());
0181   }
0182   m_contextPtr->getBeamSpot()->checkBeamSpot(m_navigatorPtr->getCurrentEvent());
0183 
0184   TStopwatch sw;
0185   m_viewManager->eventBegin();
0186   m_eiManager->newEvent(m_navigatorPtr->getCurrentEvent());
0187   m_viewManager->eventEnd();
0188   sw.Stop();
0189   fwLog(fwlog::kDebug) << "CmsShowMainBase::draw CpuTime " << sw.CpuTime() << " RealTime " << sw.RealTime()
0190                        << std::endl;
0191 
0192   if (!m_autoSaveAllViewsFormat.empty()) {
0193     m_guiManager->updateStatus("auto saving images ...");
0194     m_guiManager->exportAllViews(m_autoSaveAllViewsFormat, m_autoSaveAllViewsHeight);
0195   }
0196   m_guiManager->clearStatus();
0197 }
0198 
0199 void CmsShowMainBase::setup(FWNavigatorBase *navigator,
0200                             fireworks::Context *context,
0201                             FWJobMetadataManager *metadataManager) {
0202   m_navigatorPtr = navigator;
0203   m_contextPtr = context;
0204   m_contextPtr->setGeom(&m_geom);
0205   m_metadataManagerPtr = metadataManager;
0206 
0207   m_colorManager->initialize();
0208   m_contextPtr->initEveElements();
0209   m_guiManager = std::make_unique<FWGUIManager>(m_contextPtr, m_viewManager.get(), m_navigatorPtr);
0210 
0211   m_eiManager->newItem_.connect(
0212       std::bind(&FWModelChangeManager::newItemSlot, m_changeManager.get(), std::placeholders::_1));
0213 
0214   m_eiManager->newItem_.connect(
0215       std::bind(&FWViewManagerManager::registerEventItem, m_viewManager.get(), std::placeholders::_1));
0216   m_configurationManager->add("EventItems", m_eiManager.get());
0217   m_configurationManager->add("GUI", m_guiManager.get());
0218   m_configurationManager->add("EventNavigator", m_navigatorPtr);
0219   m_configurationManager->add("CommonPreferences",
0220                               m_contextPtr->commonPrefs());  // must be after GUIManager in alphabetical order
0221 
0222   m_guiManager->writeToConfigurationFile_.connect(
0223       std::bind(&CmsShowMainBase::writeToConfigFile, this, std::placeholders::_1));
0224 
0225   m_guiManager->loadFromConfigurationFile_.connect(
0226       std::bind(&CmsShowMainBase::reloadConfiguration, this, std::placeholders::_1));
0227   m_guiManager->loadPartialFromConfigurationFile_.connect(
0228       std::bind(&CmsShowMainBase::partialLoadConfiguration, this, std::placeholders::_1));
0229 
0230   m_guiManager->writePartialToConfigurationFile_.connect(
0231       std::bind(&CmsShowMainBase::partialWriteToConfigFile, this, std::placeholders::_1));
0232 
0233   std::string macPath(gSystem->Getenv("CMSSW_BASE"));
0234   macPath += "/src/Fireworks/Core/macros";
0235   const char *base = gSystem->Getenv("CMSSW_RELEASE_BASE");
0236   if (nullptr != base) {
0237     macPath += ":";
0238     macPath += base;
0239     macPath += "/src/Fireworks/Core/macros";
0240   }
0241   gROOT->SetMacroPath((std::string("./:") + macPath).c_str());
0242 
0243   m_startupTasks->tasksCompleted_.connect(std::bind(&FWGUIManager::clearStatus, m_guiManager.get()));
0244 }
0245 
0246 void CmsShowMainBase::writeToConfigFile(const std::string &name) {
0247   m_configFileName = name;
0248   m_configurationManager->writeToFile(m_configFileName);
0249 }
0250 
0251 void CmsShowMainBase::writeToCurrentConfigFile() { m_configurationManager->writeToFile(m_configFileName); }
0252 
0253 void CmsShowMainBase::partialWriteToConfigFile(const std::string &name) {
0254   std::string p = (name == "current") ? m_configFileName.c_str() : name.c_str();
0255   new FWPartialConfigSaveGUI(p.c_str(), m_configFileName.c_str(), m_configurationManager.get());
0256 }
0257 
0258 void CmsShowMainBase::reloadConfiguration(const std::string &config) {
0259   if (config.empty())
0260     return;
0261 
0262   m_configFileName = config;
0263 
0264   std::string msg = "Reloading configuration " + config + "...";
0265   fwLog(fwlog::kDebug) << msg << std::endl;
0266   m_guiManager->updateStatus(msg.c_str());
0267   m_guiManager->subviewDestroyAll();
0268   m_eiManager->clearItems();
0269   m_configFileName = config;
0270   try {
0271     gEve->DisableRedraw();
0272     m_configurationManager->readFromFile(config);
0273     gEve->EnableRedraw();
0274   } catch (SimpleSAXParser::ParserError &e) {
0275     Int_t chosen;
0276     new TGMsgBox(gClient->GetDefaultRoot(),
0277                  gClient->GetDefaultRoot(),
0278                  "Bad configuration",
0279                  ("Configuration " + config + " cannot be parsed: " + e.error()).c_str(),
0280                  kMBIconExclamation,
0281                  kMBCancel,
0282                  &chosen);
0283   } catch (...) {
0284     Int_t chosen;
0285     new TGMsgBox(gClient->GetDefaultRoot(),
0286                  gClient->GetDefaultRoot(),
0287                  "Bad configuration",
0288                  ("Configuration " + config + " cannot be parsed.").c_str(),
0289                  kMBIconExclamation,
0290                  kMBCancel,
0291                  &chosen);
0292   }
0293 
0294   m_guiManager->updateStatus("");
0295 }
0296 
0297 void CmsShowMainBase::partialLoadConfiguration(const std::string &name) {
0298   new FWPartialConfigLoadGUI(name.c_str(), m_configurationManager.get(), m_eiManager.get());
0299 }
0300 
0301 void CmsShowMainBase::setupAutoLoad(float x) {
0302   m_playDelay = x;
0303   m_guiManager->setDelayBetweenEvents(m_playDelay);
0304   if (!m_guiManager->playEventsAction()->isEnabled())
0305     m_guiManager->playEventsAction()->enable();
0306 
0307   m_guiManager->playEventsAction()->switchMode();
0308 }
0309 
0310 void CmsShowMainBase::startAutoLoadTimer() {
0311   m_autoLoadTimer->SetTime((Long_t)(m_playDelay * 1000));
0312   m_autoLoadTimer->Reset();
0313   m_autoLoadTimer->TurnOn();
0314   m_autoLoadTimerRunning = kTRUE;
0315 }
0316 
0317 void CmsShowMainBase::stopAutoLoadTimer() {
0318   m_autoLoadTimer->TurnOff();
0319   m_autoLoadTimerRunning = kFALSE;
0320 }
0321 
0322 void CmsShowMainBase::setupConfiguration() {
0323   m_guiManager->updateStatus("Setting up configuration...");
0324 
0325   try {
0326     gEve->DisableRedraw();
0327     if (m_configFileName.empty()) {
0328       m_configFileName = m_configurationManager->guessAndReadFromFile(m_metadataManagerPtr);
0329     } else {
0330       char *whereConfig = gSystem->Which(TROOT::GetMacroPath(), m_configFileName.c_str(), kReadPermission);
0331       m_configFileName = whereConfig;
0332       delete[] whereConfig;
0333       m_configurationManager->readFromFile(m_configFileName);
0334     }
0335     gEve->EnableRedraw();
0336   } catch (SimpleSAXParser::ParserError &e) {
0337     fwLog(fwlog::kError) << "Unable to load configuration file '" << m_configFileName << "': " << e.error()
0338                          << std::endl;
0339     exit(1);
0340   } catch (std::runtime_error &e) {
0341     fwLog(fwlog::kError) << "Unable to load configuration file '" << m_configFileName
0342                          << "' which was specified on command line. Quitting." << std::endl;
0343     exit(1);
0344   }
0345 }
0346 
0347 void CmsShowMainBase::setPlayDelay(Float_t val) { m_playDelay = val; }
0348 
0349 void CmsShowMainBase::setupDebugSupport() {
0350   m_guiManager->updateStatus("Setting up Eve debug window...");
0351   m_guiManager->openEveBrowserForDebugging();
0352 }
0353 
0354 void CmsShowMainBase::setPlayLoop() {
0355   if (!m_loop) {
0356     setPlayLoopImp();
0357     m_guiManager->loopAction()->activated();
0358   }
0359 }
0360 
0361 void CmsShowMainBase::unsetPlayLoop() {
0362   if (m_loop) {
0363     unsetPlayLoopImp();
0364     m_guiManager->loopAction()->stop();
0365   }
0366 }
0367 
0368 void CmsShowMainBase::setPlayLoopImp() { m_loop = true; }
0369 
0370 void CmsShowMainBase::unsetPlayLoopImp() { m_loop = false; }
0371 
0372 void CmsShowMainBase::registerPhysicsObject(const FWPhysicsObjectDesc &iItem) { m_eiManager->add(iItem); }
0373 
0374 void CmsShowMainBase::stopPlaying() {
0375   m_isPlaying = false;
0376   checkKeyBindingsOnPLayEventsStateChanged();
0377 }
0378 
0379 void CmsShowMainBase::playForward() {
0380   m_forward = true;
0381   m_isPlaying = true;
0382   guiManager()->enableActions(kFALSE);
0383   checkKeyBindingsOnPLayEventsStateChanged();
0384   startAutoLoadTimer();
0385 }
0386 
0387 void CmsShowMainBase::playBackward() {
0388   m_forward = false;
0389   m_isPlaying = true;
0390   guiManager()->enableActions(kFALSE);
0391   startAutoLoadTimer();
0392   checkKeyBindingsOnPLayEventsStateChanged();
0393 }
0394 
0395 void CmsShowMainBase::loadGeometry() {  // prepare geometry service
0396   // ATTN: this should be made configurable
0397   try {
0398     guiManager()->updateStatus("Loading geometry...");
0399     m_geom.loadMap(m_geometryFilename.c_str());
0400   } catch (const std::runtime_error &iException) {
0401     fwLog(fwlog::kError) << "CmsShowMain::loadGeometry() caught exception: \n"
0402                          << m_geometryFilename << " " << iException.what() << std::endl;
0403     exit(0);
0404   }
0405 }
0406 
0407 void CmsShowMainBase::sendVersionInfo() {
0408   // Send version info to xrootd.t2.ucsd.edu receiver on port 9698.
0409 
0410   // receiver
0411   struct hostent *h = gethostbyname("xrootd.t2.ucsd.edu");
0412   if (!h)
0413     return;
0414 
0415   struct sockaddr_in remoteServAddr;
0416   remoteServAddr.sin_family = h->h_addrtype;
0417   memcpy((char *)&remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
0418   remoteServAddr.sin_port = htons(9698);
0419 
0420   // socket creation
0421   int sd = socket(AF_INET, SOCK_DGRAM, 0);
0422   if (sd < 0) {
0423     // std::cout << "can't create socket \n";
0424     return;
0425   }
0426   // bind any port
0427   // printf("bind port\n");
0428   struct sockaddr_in cliAddr;
0429   cliAddr.sin_family = AF_INET;
0430   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
0431   cliAddr.sin_port = htons(0);
0432 
0433   int rc = bind(sd, (struct sockaddr *)&cliAddr, sizeof(cliAddr));
0434   if (rc < 0) {
0435     //  std::cout << "can't bind port %d " << rc << std::endl;
0436     return;
0437   }
0438 
0439   // get OSX version
0440   TString osx_version;
0441   try {
0442     std::ifstream infoFile("/System/Library/CoreServices/SystemVersion.plist");
0443     osx_version.ReadFile(infoFile);
0444     TPMERegexp re("ProductVersion\\</key\\>\\n\\t\\<string\\>(10.*)\\</string\\>");
0445     re.Match(osx_version);
0446     osx_version = re[1];
0447   } catch (...) {
0448   }
0449 
0450   // send data
0451   SysInfo_t sInfo;
0452   gSystem->GetSysInfo(&sInfo);
0453   char msg[128];
0454 
0455   if (gSystem->Getenv("CMSSW_VERSION")) {
0456     snprintf(msg, 64, "%s %s %s", gSystem->Getenv("CMSSW_VERSION"), sInfo.fOS.Data(), osx_version.Data());
0457   } else {
0458     TString versionFileName("data/version.txt");
0459     fireworks::setPath(versionFileName);
0460     std::ifstream fs(versionFileName);
0461     TString infoText;
0462     infoText.ReadLine(fs);
0463     fs.close();
0464     snprintf(msg, 64, "Standalone %s %s %s", infoText.Data(), sInfo.fOS.Data(), osx_version.Data());
0465   }
0466 
0467   int flags = 0;
0468   sendto(sd, msg, strlen(msg), flags, (struct sockaddr *)&remoteServAddr, sizeof(remoteServAddr));
0469 }