Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:10

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWDetailViewManager
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Wed Mar  5 09:13:47 EST 2008
0011 //
0012 
0013 #include <cstdio>
0014 #include <functional>
0015 #include <algorithm>
0016 #include <sstream>
0017 
0018 #include "TClass.h"
0019 #include "TROOT.h"
0020 #include "TGWindow.h"
0021 #include "TGFrame.h"
0022 #include "TEveManager.h"
0023 #include "TEveWindowManager.h"
0024 #include "TEveWindow.h"
0025 #include "TQObject.h"
0026 #include "TObjString.h"
0027 
0028 #include "Fireworks/Core/interface/FWDetailViewManager.h"
0029 #include "Fireworks/Core/interface/FWColorManager.h"
0030 #include "Fireworks/Core/interface/FWDetailViewBase.h"
0031 #include "Fireworks/Core/interface/FWModelId.h"
0032 #include "Fireworks/Core/interface/FWEventItem.h"
0033 #include "Fireworks/Core/interface/FWDetailViewFactory.h"
0034 #include "Fireworks/Core/interface/FWGUIManager.h"
0035 #include "Fireworks/Core/interface/FWSimpleRepresentationChecker.h"
0036 #include "Fireworks/Core/interface/FWRepresentationInfo.h"
0037 #include "Fireworks/Core/interface/fwLog.h"
0038 #include "Fireworks/Core/interface/FWJobMetadataManager.h"
0039 
0040 #include "DataFormats/Provenance/interface/BranchDescription.h"
0041 #include "FWCore/Common/interface/EventBase.h"
0042 #include "DataFormats/FWLite/interface/Event.h"
0043 static std::string viewNameFrom(const std::string& iFull) {
0044   std::string::size_type first = iFull.find_first_of('@');
0045   std::string::size_type second = iFull.find_first_of('@', first + 1);
0046   return iFull.substr(first + 1, second - first - 1);
0047 }
0048 //
0049 // constructors and destructor
0050 //
0051 FWDetailViewManager::FWDetailViewManager(fireworks::Context* iCtx) : m_context(iCtx) {
0052   // force white background for all embedded canvases
0053   gROOT->SetStyle("Plain");
0054 
0055   m_context->colorManager()->colorsHaveChanged_.connect(std::bind(&FWDetailViewManager::colorsChanged, this));
0056   gEve->GetWindowManager()->Connect(
0057       "WindowDeleted(TEveWindow*)", "FWDetailViewManager", this, "eveWindowDestroyed(TEveWindow*)");
0058 }
0059 
0060 FWDetailViewManager::~FWDetailViewManager() {
0061   newEventCallback();
0062   gEve->GetWindowManager()->Disconnect("WindowDeleted(TEveWindow*)", this, "eveWindowDestroyed(TEveWindow*)");
0063 }
0064 
0065 FWDetailViewManager::ViewFrame::ViewFrame(TEveCompositeFrameInMainFrame* f,
0066                                           std::unique_ptr<FWDetailViewBase> v,
0067                                           TEveWindow* w)
0068     : m_eveFrame(f), m_detailView(std::move(v)), m_eveWindow(w) {}
0069 FWDetailViewManager::ViewFrame::~ViewFrame() = default;
0070 
0071 void FWDetailViewManager::openDetailViewFor(const FWModelId& id, const std::string& iViewName) {
0072   TEveWindowSlot* slot = TEveWindow::CreateWindowMainFrame();
0073   TEveCompositeFrameInMainFrame* eveFrame = (TEveCompositeFrameInMainFrame*)slot->GetEveFrame();
0074 
0075   // find the right viewer for this item
0076   std::string typeName = edm::TypeWithDict(*(id.item()->modelType()->GetTypeInfo())).name();
0077   std::vector<std::string> viewerNames = findViewersFor(typeName);
0078   if (viewerNames.empty()) {
0079     fwLog(fwlog::kError) << "FWDetailViewManager: don't know what detailed view to "
0080                             "use for object "
0081                          << id.item()->name() << std::endl;
0082     assert(!viewerNames.empty());
0083   }
0084 
0085   //see if one of the names matches iViewName
0086   std::string match;
0087   for (std::vector<std::string>::iterator it = viewerNames.begin(), itEnd = viewerNames.end(); it != itEnd; ++it) {
0088     std::string t = viewNameFrom(*it);
0089     //std::cout <<"'"<<iViewName<< "' '"<<t<<"'"<<std::endl;
0090     if (t == iViewName) {
0091       match = *it;
0092       break;
0093     }
0094   }
0095   assert(!match.empty());
0096   std::unique_ptr<FWDetailViewBase> detailView{FWDetailViewFactory::get()->create(match)};
0097   assert(nullptr != detailView);
0098 
0099   TEveWindowSlot* ws = (TEveWindowSlot*)(eveFrame->GetEveWindow());
0100   detailView->init(ws);
0101   detailView->build(id);
0102   detailView->setBackgroundColor(m_context->colorManager()->background());
0103 
0104   TGMainFrame* mf = (TGMainFrame*)(eveFrame->GetParent());
0105   mf->SetWindowName(Form("%s Detail View [%d]", id.item()->name().c_str(), id.index()));
0106 
0107   m_views.emplace_back(eveFrame, std::move(detailView), eveFrame->GetEveWindow());
0108 
0109   mf->MapRaised();
0110 }
0111 
0112 std::vector<std::string> FWDetailViewManager::detailViewsFor(const FWModelId& iId) const {
0113   std::string typeName = edm::TypeWithDict(*(iId.item()->modelType()->GetTypeInfo())).name();
0114   std::vector<std::string> fullNames = findViewersFor(typeName);
0115   std::vector<std::string> justViewNames;
0116   justViewNames.reserve(fullNames.size());
0117   std::transform(fullNames.begin(), fullNames.end(), std::back_inserter(justViewNames), &viewNameFrom);
0118   return justViewNames;
0119 }
0120 namespace {
0121   bool pluginComapreFunc(std::string a, std::string b) {
0122     std::string::size_type as = a.find_first_of('&');
0123     a = a.substr(0, as);
0124     std::string::size_type bs = b.find_first_of('&');
0125     b = b.substr(0, bs);
0126     return a == b;
0127   }
0128 }  // namespace
0129 
0130 std::vector<std::string> FWDetailViewManager::findViewersFor(const std::string& iType) const {
0131   std::vector<std::string> returnValue;
0132 
0133   std::map<std::string, std::vector<std::string> >::const_iterator itFind = m_typeToViewers.find(iType);
0134   if (itFind != m_typeToViewers.end()) {
0135     return itFind->second;
0136   }
0137 
0138   std::set<std::string> detailViews;
0139 
0140   std::vector<edmplugin::PluginInfo> all =
0141       edmplugin::PluginManager::get()->categoryToInfos().find(FWDetailViewFactory::get()->category())->second;
0142   std::transform(all.begin(),
0143                  all.end(),
0144                  std::inserter(detailViews, detailViews.begin()),
0145                  std::bind(&edmplugin::PluginInfo::name_, std::placeholders::_1));
0146   unsigned int closestMatch = 0xFFFFFFFF;
0147 
0148   for (std::set<std::string>::iterator it = detailViews.begin(), itEnd = detailViews.end(); it != itEnd; ++it) {
0149     if (m_context->getHidePFBuilders()) {
0150       std::size_t found = it->find("PF ");
0151       if (found != std::string::npos)
0152         continue;
0153     }
0154     std::string::size_type first = it->find_first_of('@');
0155     std::string type = it->substr(0, first);
0156 
0157     //see if we match via inheritance
0158     FWSimpleRepresentationChecker checker(type, "", 0, false);
0159     FWRepresentationInfo info = checker.infoFor(iType);
0160     bool pass = false;
0161     if (closestMatch > info.proximity()) {
0162       pass = true;
0163       std::string::size_type firstD = it->find_first_of('&') + 1;
0164       if (firstD != std::string::npos) {
0165         std::stringstream ss(it->substr(firstD));
0166         std::string ml;
0167         while (std::getline(ss, ml, '&')) {
0168           if (!m_context->metadataManager()->hasModuleLabel(ml)) {
0169             fwLog(fwlog::kDebug) << "DetailView " << *it << " requires module label " << ml << std::endl;
0170             pass = false;
0171             break;
0172           }
0173         }
0174       }
0175       if (pass) {
0176         returnValue.push_back(*it);
0177       } else {
0178         std::string::size_type first = (*it).find_first_of('@');
0179         std::string vn = *it;
0180         vn.insert(++first, "!");
0181         returnValue.push_back(vn);
0182       }
0183     }
0184   }
0185 
0186   std::vector<std::string>::iterator it;
0187   it = std::unique(returnValue.begin(), returnValue.end(), pluginComapreFunc);
0188   returnValue.resize(std::distance(returnValue.begin(), it));
0189 
0190   m_typeToViewers[iType] = returnValue;
0191   return returnValue;
0192 }
0193 
0194 void FWDetailViewManager::colorsChanged() {
0195   for (vViews_i i = m_views.begin(); i != m_views.end(); ++i)
0196     (*i).m_detailView->setBackgroundColor(m_context->colorManager()->background());
0197 }
0198 
0199 void FWDetailViewManager::newEventCallback() {
0200   while (!m_views.empty()) {
0201     m_views.front().m_eveWindow->DestroyWindowAndSlot();
0202   }
0203 }
0204 
0205 void FWDetailViewManager::eveWindowDestroyed(TEveWindow* ew) {
0206   for (vViews_i i = m_views.begin(); i != m_views.end(); ++i) {
0207     if (ew == i->m_eveWindow) {
0208       // printf("========================== delete %s \n", ew->GetElementName());
0209       m_views.erase(i);
0210       break;
0211     }
0212   }
0213 }