Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWEventItemsManager
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:
0010 //         Created:  Fri Jan  4 10:38:18 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <sstream>
0015 #include <functional>
0016 #include "TClass.h"
0017 
0018 // user include files
0019 #include "Fireworks/Core/interface/FWEventItemsManager.h"
0020 #include "Fireworks/Core/interface/FWEventItem.h"
0021 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0022 #include "Fireworks/Core/interface/FWColorManager.h"
0023 #include "Fireworks/Core/interface/FWConfiguration.h"
0024 #include "Fireworks/Core/interface/FWDisplayProperties.h"
0025 #include "Fireworks/Core/interface/FWItemAccessorFactory.h"
0026 #include "Fireworks/Core/interface/FWProxyBuilderConfiguration.h"
0027 #include "Fireworks/Core/interface/fwLog.h"
0028 #include <cassert>
0029 
0030 //
0031 // constants, enums and typedefs
0032 //
0033 
0034 //
0035 // static data member definitions
0036 //
0037 
0038 //
0039 // constructors and destructor
0040 //
0041 FWEventItemsManager::FWEventItemsManager(FWModelChangeManager* iManager)
0042     : m_changeManager(iManager), m_context(nullptr), m_event(nullptr), m_accessorFactory(new FWItemAccessorFactory()) {}
0043 
0044 // FWEventItemsManager::FWEventItemsManager(const FWEventItemsManager& rhs)
0045 // {
0046 //    // do actual copying here;
0047 // }
0048 
0049 /** FWEventItemsManager has ownership of the items it contains.
0050 
0051     Note that because of the way we keep track of removed items,
0052     m_items[i] could actually be 0 for indices corresponding
0053     to removed items.
0054  */
0055 FWEventItemsManager::~FWEventItemsManager() {
0056   for (size_t i = 0, e = m_items.size(); i != e; ++i)
0057     delete m_items[i];
0058 
0059   m_items.clear();
0060 }
0061 
0062 //
0063 // assignment operators
0064 //
0065 // const FWEventItemsManager& FWEventItemsManager::operator=(const FWEventItemsManager& rhs)
0066 // {
0067 //   //An exception safe implementation is
0068 //   FWEventItemsManager temp(rhs);
0069 //   swap(rhs);
0070 //
0071 //   return *this;
0072 // }
0073 
0074 //
0075 // member functions
0076 //
0077 FWEventItem* FWEventItemsManager::add(const FWPhysicsObjectDesc& iItem, const FWConfiguration* pbc, bool doSetEvent) {
0078   FWPhysicsObjectDesc temp(iItem);
0079 
0080   if (!m_context->colorManager()->colorHasIndex(temp.displayProperties().color())) {
0081     FWDisplayProperties prop(temp.displayProperties());
0082     fwLog(fwlog::kWarning) << Form(
0083         "FWEventItemsManager::add(const FWPhysicsObjectDesc& iItem), color index  not valid. Set Color idex to %d\n",
0084         FWColorManager::getDefaultStartColorIndex());
0085     prop.setColor(FWColorManager::getDefaultStartColorIndex());
0086     temp.setDisplayProperties(prop);
0087   }
0088 
0089   m_items.push_back(new FWEventItem(m_context, m_items.size(), m_accessorFactory->accessorFor(temp.type()), temp, pbc));
0090   newItem_(m_items.back());
0091   m_items.back()->goingToBeDestroyed_.connect(std::bind(&FWEventItemsManager::removeItem, this, std::placeholders::_1));
0092   if (doSetEvent && m_event) {
0093     FWChangeSentry sentry(*m_changeManager);
0094     m_items.back()->setEvent(m_event);
0095   }
0096   return m_items.back();
0097 }
0098 
0099 /** Prepare to handle a new event by associating
0100     all the items to watch it.
0101   */
0102 void FWEventItemsManager::newEvent(const edm::EventBase* iEvent) {
0103   FWChangeSentry sentry(*m_changeManager);
0104   m_event = iEvent;
0105   for (size_t i = 0, e = m_items.size(); i != e; ++i) {
0106     FWEventItem* item = m_items[i];
0107     if (item)
0108       item->setEvent(iEvent);
0109   }
0110 }
0111 
0112 /** Clear all the items in the model. 
0113     
0114     Notice that a previous implementation was setting all the items to 0, I
0115     guess to track accessing delete items.
0116   */
0117 void FWEventItemsManager::clearItems(void) {
0118   for (size_t i = 0, e = m_items.size(); i != e; ++i) {
0119     FWEventItem* item = m_items[i];
0120     if (item) {
0121       item->destroy();
0122     }
0123     m_items[i] = nullptr;
0124   }
0125   goingToClearItems_();
0126 
0127   m_items.clear();
0128 }
0129 
0130 static const std::string kType("type");
0131 static const std::string kModuleLabel("moduleLabel");
0132 static const std::string kProductInstanceLabel("productInstanceLabel");
0133 static const std::string kProcessName("processName");
0134 static const std::string kFilterExpression("filterExpression");
0135 static const std::string kColor("color");
0136 static const std::string kIsVisible("isVisible");
0137 static const std::string kTrue("t");
0138 static const std::string kFalse("f");
0139 static const std::string kLayer("layer");
0140 static const std::string kPurpose("purpose");
0141 static const std::string kTransparency("transparency");
0142 
0143 void FWEventItemsManager::addTo(FWConfiguration& iTo) const {
0144   FWColorManager* cm = m_context->colorManager();
0145   assert(nullptr != cm);
0146   for (std::vector<FWEventItem*>::const_iterator it = m_items.begin(); it != m_items.end(); ++it) {
0147     if (!*it)
0148       continue;
0149     FWConfiguration conf(6);
0150     edm::TypeWithDict dataType((*((*it)->type()->GetTypeInfo())));
0151     assert(dataType != edm::TypeWithDict());
0152 
0153     conf.addKeyValue(kType, FWConfiguration(dataType.name()));
0154     conf.addKeyValue(kModuleLabel, FWConfiguration((*it)->moduleLabel()));
0155     conf.addKeyValue(kProductInstanceLabel, FWConfiguration((*it)->productInstanceLabel()));
0156     conf.addKeyValue(kProcessName, FWConfiguration((*it)->processName()));
0157     conf.addKeyValue(kFilterExpression, FWConfiguration((*it)->filterExpression()));
0158     {
0159       std::ostringstream os;
0160       os << (*it)->defaultDisplayProperties().color();
0161       conf.addKeyValue(kColor, FWConfiguration(os.str()));
0162     }
0163     conf.addKeyValue(kIsVisible, FWConfiguration((*it)->defaultDisplayProperties().isVisible() ? kTrue : kFalse));
0164     {
0165       std::ostringstream os;
0166       os << (*it)->layer();
0167       conf.addKeyValue(kLayer, FWConfiguration(os.str()));
0168     }
0169     conf.addKeyValue(kPurpose, (*it)->purpose());
0170     {
0171       std::ostringstream os;
0172       os << static_cast<int>((*it)->defaultDisplayProperties().transparency());
0173       conf.addKeyValue(kTransparency, FWConfiguration(os.str()));
0174     }
0175 
0176     FWConfiguration pbTmp;
0177     (*it)->getConfig()->addTo(pbTmp);
0178     conf.addKeyValue("PBConfig", pbTmp, true);
0179 
0180     iTo.addKeyValue((*it)->name(), conf, true);
0181   }
0182 }
0183 
0184 /** This is responsible for resetting the status of items from configuration  
0185   */
0186 void FWEventItemsManager::setFrom(const FWConfiguration& iFrom) {
0187   FWColorManager* cm = m_context->colorManager();
0188   assert(nullptr != cm);
0189 
0190   clearItems();
0191   const FWConfiguration::KeyValues* keyValues = iFrom.keyValues();
0192 
0193   if (keyValues == nullptr)
0194     return;
0195 
0196   std::vector<FWEventItem*> newItems;
0197   newItems.reserve(keyValues->size());
0198 
0199   for (FWConfiguration::KeyValues::const_iterator it = keyValues->begin(); it != keyValues->end(); ++it) {
0200     const std::string& name = it->first;
0201     const FWConfiguration& conf = it->second;
0202     const FWConfiguration::KeyValues* keyValues = conf.keyValues();
0203     assert(nullptr != keyValues);
0204     const std::string& type = (*keyValues)[0].second.value();
0205     const std::string& moduleLabel = (*keyValues)[1].second.value();
0206     const std::string& productInstanceLabel = (*keyValues)[2].second.value();
0207     const std::string& processName = (*keyValues)[3].second.value();
0208     const std::string& filterExpression = (*keyValues)[4].second.value();
0209     const std::string& sColor = (*keyValues)[5].second.value();
0210     const bool isVisible = (*keyValues)[6].second.value() == kTrue;
0211 
0212     unsigned int colorIndex;
0213     if (conf.version() < 5) {
0214       std::istringstream is(sColor);
0215       Color_t color;
0216       is >> color;
0217       colorIndex = cm->oldColorToIndex(color, conf.version());
0218     } else {
0219       // In version 4 we assume:
0220       //   fireworks colors start at ROOT index 1000
0221       //   geometry  colors start at ROOT index 1100
0222       // We save them as such -- no conversions needed.
0223       std::istringstream is(sColor);
0224       is >> colorIndex;
0225     }
0226 
0227     int transparency = 0;
0228 
0229     // Read transparency from file. We don't care about checking errors
0230     // because strtol returns 0 in that case.
0231     if (conf.version() > 3)
0232       transparency = strtol((*keyValues)[9].second.value().c_str(), nullptr, 10);
0233 
0234     FWDisplayProperties dp(colorIndex, isVisible, transparency);
0235 
0236     unsigned int layer = strtol((*keyValues)[7].second.value().c_str(), nullptr, 10);
0237 
0238     //For older configs assume name is the same as purpose
0239     std::string purpose(name);
0240     if (conf.version() > 1)
0241       purpose = (*keyValues)[8].second.value();
0242 
0243     FWConfiguration* proxyConfig =
0244         (FWConfiguration*)conf.valueForKey("PBConfig") ? new FWConfiguration(*conf.valueForKey("PBConfig")) : nullptr;
0245 
0246     // beckward compatibilty for obsolete proxy builders
0247     if (conf.version() < 6) {
0248       assert(proxyConfig == nullptr);
0249       if (purpose == "VerticesWithTracks") {
0250         purpose = "Vertices";
0251         proxyConfig = new FWConfiguration();
0252         FWConfiguration vTmp;
0253         vTmp.addKeyValue("Draw Tracks", FWConfiguration("1"));
0254         proxyConfig->addKeyValue("Var", vTmp, true);
0255       }
0256     }
0257 
0258     FWPhysicsObjectDesc desc(name,
0259                              TClass::GetClass(type.c_str()),
0260                              purpose,
0261                              dp,
0262                              moduleLabel,
0263                              productInstanceLabel,
0264                              processName,
0265                              filterExpression,
0266                              layer);
0267 
0268     newItems.push_back(add(desc, proxyConfig, false));
0269   }
0270 
0271   if (m_event) {
0272     FWChangeSentry sentry(*m_changeManager);
0273     for (auto ip : newItems)
0274       ip->setEvent(m_event);
0275   }
0276 }
0277 
0278 /** Remove one item. 
0279   
0280     Notice that rather than erasing the item from the list, it is preferred to
0281     set it to zero, I guess to catch accesses to remove items and to avoid 
0282     having to recalculate the current selection.
0283     
0284     GE: I think this is a broken way of handling removal of objects.  The object
0285         should be properly deleted and the current selection should be updated
0286         accordingly.
0287   */
0288 void FWEventItemsManager::removeItem(const FWEventItem* iItem) {
0289   assert(iItem->id() < m_items.size());
0290   removingItem_(iItem);
0291   m_items[iItem->id()] = nullptr;
0292 }
0293 
0294 void FWEventItemsManager::setContext(fireworks::Context* iContext) { m_context = iContext; }
0295 
0296 //
0297 // const member functions
0298 //
0299 FWEventItemsManager::const_iterator FWEventItemsManager::begin() const { return m_items.begin(); }
0300 FWEventItemsManager::const_iterator FWEventItemsManager::end() const { return m_items.end(); }
0301 
0302 /** Look up an item by name.
0303   */
0304 const FWEventItem* FWEventItemsManager::find(const std::string& iName) const {
0305   for (size_t i = 0, e = m_items.size(); i != e; ++i) {
0306     const FWEventItem* item = m_items[i];
0307     if (item && item->name() == iName)
0308       return item;
0309   }
0310   return nullptr;
0311 }
0312 
0313 //
0314 // static member functions
0315 //