Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:37

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWEventItem
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:
0010 //         Created:  Thu Jan  3 14:59:23 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <iostream>
0015 #include <algorithm>
0016 #include <exception>
0017 #include <TClass.h>
0018 
0019 // user include files
0020 #include "Fireworks/Core/interface/FWEventItem.h"
0021 #include "DataFormats/FWLite/interface/Event.h"
0022 // Needed to test edm::Event access
0023 // #include "FWCore/Framework/interface/Event.h"
0024 #include "Fireworks/Core/interface/FWModelId.h"
0025 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0026 #include "Fireworks/Core/interface/FWSelectionManager.h"
0027 #include "Fireworks/Core/interface/FWItemAccessorBase.h"
0028 #include "Fireworks/Core/interface/FWEventItemsManager.h"
0029 #include "Fireworks/Core/interface/FWProxyBuilderConfiguration.h"
0030 #include "Fireworks/Core/src/FWGenericHandle.h"
0031 #include "Fireworks/Core/interface/FWGeometry.h"
0032 #include "Fireworks/Core/interface/fwLog.h"
0033 
0034 //
0035 // static data member definitions
0036 //
0037 
0038 int FWEventItem::minLayerValue() { return -100; }
0039 
0040 int FWEventItem::maxLayerValue() { return 100; }
0041 
0042 //
0043 // constructors and destructor
0044 //
0045 FWEventItem::FWEventItem(fireworks::Context* iContext,
0046                          unsigned int iId,
0047                          std::shared_ptr<FWItemAccessorBase> iAccessor,
0048                          const FWPhysicsObjectDesc& iDesc,
0049                          const FWConfiguration* pbc)
0050     : m_context(iContext),
0051       m_id(iId),
0052       m_name(iDesc.name()),
0053       m_type(iDesc.type()),
0054       m_purpose(iDesc.purpose()),
0055       m_accessor(iAccessor),
0056       m_displayProperties(iDesc.displayProperties()),
0057       m_layer(iDesc.layer()),
0058       m_moduleLabel(iDesc.moduleLabel()),
0059       m_productInstanceLabel(iDesc.productInstanceLabel()),
0060       m_processName(iDesc.processName()),
0061       m_event(nullptr),
0062       m_interestingValueGetter(edm::TypeWithDict(*(m_accessor->modelType()->GetTypeInfo())), m_purpose),
0063       m_filter(iDesc.filterExpression(), ""),
0064       m_printedErrorThisEvent(false),
0065       m_isSelected(false),
0066       m_origColor(0),
0067       m_proxyBuilderConfig(nullptr) {
0068   //assert(m_type->GetTypeInfo());
0069   //edm::TypeWithDict dataType(*(m_type->GetTypeInfo()));
0070   //assert(dataType != edm::TypeWithDict() );
0071   //
0072   //std::string dataTypeName = dataType.name();
0073   //if (dataTypeName[dataTypeName.size() -1] == '>')
0074   //   dataTypeName += " ";
0075   //std::string wrapperName = "edm::Wrapper<" + dataTypeName + ">";
0076   //
0077   //fwLog(fwlog::kDebug) << "Looking for the wrapper name"
0078   //                    << wrapperName << std::endl;
0079   //m_wrapperType = edm::TypeWithDict::byName(wrapperName);
0080   //
0081   //assert(m_wrapperType != edm::TypeWithDict());
0082   if (!m_accessor->isCollection()) {
0083     m_itemInfos.reserve(1);
0084   }
0085   m_filter.setClassName(modelType()->GetName());
0086   m_proxyBuilderConfig = new FWProxyBuilderConfiguration(pbc, this);
0087   m_origColor = iDesc.displayProperties().color();
0088 }
0089 // FWEventItem::FWEventItem(const FWEventItem& rhs)
0090 // {
0091 //    // do actual copying here;
0092 // }
0093 
0094 FWEventItem::~FWEventItem() { delete m_proxyBuilderConfig; }
0095 
0096 //
0097 // assignment operators
0098 //
0099 // const FWEventItem& FWEventItem::operator=(const FWEventItem& rhs)
0100 // {
0101 //   //An exception safe implementation is
0102 //   FWEventItem temp(rhs);
0103 //   swap(rhs);
0104 //
0105 //   return *this;
0106 // }
0107 
0108 //
0109 // member functions
0110 //
0111 void FWEventItem::setEvent(const edm::EventBase* iEvent) {
0112   m_printedErrorThisEvent = false;
0113   m_event = iEvent;
0114   m_accessor->reset();
0115   m_itemInfos.clear();
0116   handleChange();
0117 }
0118 
0119 void FWEventItem::setLabels(const std::string& iModule,
0120                             const std::string& iProductInstance,
0121                             const std::string& iProcess) {
0122   m_moduleLabel = iModule;
0123   m_productInstanceLabel = iProductInstance;
0124   m_processName = iProcess;
0125   m_accessor->reset();
0126   m_itemInfos.clear();
0127   handleChange();
0128 }
0129 
0130 void FWEventItem::setName(const std::string& iName) { m_name = iName; }
0131 
0132 /** This is the place where not only display properties are changed,
0133     but which is also responsible to notify the FWModelChangeManager about
0134     the change. If you've just added some property, you have a nice GUI for
0135     it and still nothing works, this is probably the place where you want to 
0136     look. 
0137   */
0138 void FWEventItem::setDefaultDisplayProperties(const FWDisplayProperties& iProp) {
0139   bool visChange = m_displayProperties.isVisible() != iProp.isVisible();
0140   bool colorChanged = m_displayProperties.color() != iProp.color();
0141   bool transparencyChanged = m_displayProperties.transparency() != iProp.transparency();
0142 
0143   if (!visChange && !colorChanged && !transparencyChanged) {
0144     return;
0145   }
0146   //If the default visibility is changed, we want to also change the the visibility of the children
0147   // BUT we want to remember the old visibility so if the visibility is changed again we return
0148   // to the previous state.
0149   // only the visible ones need to be marked as 'changed'
0150   FWChangeSentry sentry(*(changeManager()));
0151 
0152   for (int index = 0; index < static_cast<int>(size()); ++index) {
0153     FWDisplayProperties prp = m_itemInfos[index].displayProperties();
0154     bool vis = prp.isVisible();
0155     bool changed = false;
0156     changed = visChange && vis;
0157 
0158     if (colorChanged) {
0159       if (m_displayProperties.color() == prp.color()) {
0160         prp.setColor(iProp.color());
0161         changed = true;
0162       }
0163     }
0164     if (transparencyChanged) {
0165       if (m_displayProperties.transparency() == prp.transparency()) {
0166         prp.setTransparency(iProp.transparency());
0167         changed = true;
0168       }
0169     }
0170     if (changed) {
0171       m_itemInfos[index].m_displayProperties = prp;
0172       FWModelId id(this, index);
0173       changeManager()->changed(id);
0174     }
0175   }
0176   m_displayProperties = iProp;
0177   defaultDisplayPropertiesChanged_(this);
0178 }
0179 
0180 void FWEventItem::setFilterExpression(const std::string& iExpression) {
0181   m_filter.setExpression(iExpression);
0182   filterChanged_(this);
0183   runFilter();
0184 }
0185 
0186 void FWEventItem::runFilter() {
0187   if (m_accessor->isCollection() && m_accessor->data()) {
0188     //std::cout <<"runFilter"<<std::endl;
0189     FWChangeSentry sentry(*(this->changeManager()));
0190     int size = m_accessor->size();
0191     std::vector<ModelInfo>::iterator itInfo = m_itemInfos.begin();
0192     try {
0193       for (int index = 0; index != size; ++index, ++itInfo) {
0194         bool changed = false;
0195         bool wasVisible = itInfo->m_displayProperties.isVisible();
0196         if (not m_filter.passesFilter(m_accessor->modelData(index))) {
0197           itInfo->m_displayProperties.setIsVisible(false);
0198           changed = wasVisible == true;
0199         } else {
0200           itInfo->m_displayProperties.setIsVisible(true);
0201           changed = wasVisible == false;
0202         }
0203         if (changed) {
0204           FWModelId id(this, index);
0205           changeManager()->changed(id);
0206         }
0207       }
0208     } catch (const std::exception& iException) {
0209       //Should log this error
0210       std::cerr << "Exception occurred while running filter on " << name() << "\n" << iException.what() << std::endl;
0211     }
0212   }
0213 }
0214 
0215 void FWEventItem::unselect(int iIndex) const {
0216   //check if this is a change
0217   if (bool& sel = m_itemInfos.at(iIndex).m_isSelected) {
0218     sel = false;
0219     FWModelId id(this, iIndex);
0220     selectionManager()->unselect(id);
0221     changeManager()->changed(id);
0222   }
0223 }
0224 void FWEventItem::select(int iIndex) const {
0225   bool& sel = m_itemInfos.at(iIndex).m_isSelected;
0226   if (not sel) {
0227     sel = true;
0228     FWModelId id(this, iIndex);
0229     selectionManager()->select(id);
0230     //want to make it obvious what type of object was selected
0231     // therefore we also select the item
0232     const_cast<FWEventItem*>(this)->selectItem();
0233     changeManager()->changed(id);
0234   }
0235 }
0236 void FWEventItem::toggleSelect(int iIndex) const {
0237   bool& sel = m_itemInfos.at(iIndex).m_isSelected;
0238   sel = not sel;
0239   FWModelId id(this, iIndex);
0240   if (sel)
0241     selectionManager()->select(id);
0242   else
0243     selectionManager()->unselect(id);
0244   changeManager()->changed(id);
0245 }
0246 
0247 void FWEventItem::setDisplayProperties(int iIndex, const FWDisplayProperties& iProps) const {
0248   FWDisplayProperties& prop = m_itemInfos.at(iIndex).m_displayProperties;
0249   if (m_displayProperties.isVisible()) {
0250     if (prop != iProps) {
0251       prop = iProps;
0252       FWModelId id(this, iIndex);
0253       //selectionManager()->select(id);
0254       changeManager()->changed(id);
0255     }
0256   } else {
0257     if (iProps.isVisible()) {
0258       FWChangeSentry sentry(*(this->changeManager()));
0259       int size = m_accessor->size();
0260       std::vector<ModelInfo>::iterator itInfo = m_itemInfos.begin();
0261       for (int index = 0; index != size; ++index, ++itInfo) {
0262         if (itInfo->m_displayProperties.isVisible()) {
0263           itInfo->m_displayProperties.setIsVisible(false);
0264           FWModelId id(this, index);
0265           changeManager()->changed(id);
0266         }
0267       }
0268       m_itemInfos.at(iIndex).m_displayProperties.setIsVisible(true);
0269       FWModelId id(this, iIndex);
0270       changeManager()->changed(id);
0271       const_cast<FWEventItem*>(this)->m_displayProperties.setIsVisible(true);
0272       //NOTE: need to send out a signal here
0273       defaultDisplayPropertiesChanged_(this);
0274     }
0275   }
0276 }
0277 
0278 void FWEventItem::moveToFront() {
0279   assert(nullptr != m_context->eventItemsManager());
0280   int largest = layer();
0281   for (FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
0282                                            itEnd = m_context->eventItemsManager()->end();
0283        it != itEnd;
0284        ++it) {
0285     if ((*it) && (*it != this) && (*it)->layer() > largest) {
0286       largest = (*it)->layer();
0287     }
0288   }
0289 
0290   if (largest >= layer()) {
0291     m_layer = std::min(largest + 1, maxLayerValue());
0292   }
0293 
0294   m_itemInfos.clear();
0295   m_accessor->reset();
0296   handleChange();
0297 }
0298 
0299 void FWEventItem::moveToBack() {
0300   assert(nullptr != m_context->eventItemsManager());
0301   int smallest = layer();
0302   for (FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
0303                                            itEnd = m_context->eventItemsManager()->end();
0304        it != itEnd;
0305        ++it) {
0306     if ((*it) && (*it != this) && (*it)->layer() < smallest) {
0307       smallest = (*it)->layer();
0308     }
0309   }
0310 
0311   if (smallest <= layer()) {
0312     m_layer = std::max(smallest - 1, minLayerValue());
0313   }
0314 
0315   m_itemInfos.clear();
0316   m_accessor->reset();
0317   handleChange();
0318 }
0319 
0320 void FWEventItem::moveToLayer(int layer) {
0321   assert(nullptr != m_context->eventItemsManager());
0322 
0323   m_layer = std::max(std::min(layer, maxLayerValue()), minLayerValue());
0324 
0325   m_itemInfos.clear();
0326   m_accessor->reset();
0327   handleChange();
0328 }
0329 
0330 void FWEventItem::proxyConfigChanged(bool k) {
0331   if (!k) {
0332     m_itemInfos.clear();
0333     m_accessor->reset();
0334     if (m_context->eventItemsManager())
0335       handleChange();
0336   } else {
0337     changeManager()->changed(this);
0338   }
0339 }
0340 
0341 void FWEventItem::handleChange() {
0342   preItemChanged_(this);
0343   FWChangeSentry sentry(*(this->changeManager()));
0344   //want filter to rerun after all changes have been made
0345   changeManager()->changed(this);
0346   getPrimaryData();
0347   runFilter();
0348 }
0349 
0350 //
0351 // const member functions
0352 //
0353 const void* FWEventItem::data(const std::type_info& iInfo) const {
0354   //At the moment this is a programming error
0355   assert(iInfo == *(m_type->GetTypeInfo()));
0356 
0357   //lookup data if we don't already have it
0358   if (m_accessor->data())
0359     return m_accessor->data();
0360 
0361   m_errorMessage.clear();
0362   if (!m_event)
0363     return m_accessor->data();
0364 
0365   // Retrieve the data from the event.
0366   edm::InputTag tag(m_moduleLabel, m_productInstanceLabel, m_processName);
0367   edm::TypeWithDict type(iInfo);
0368   edm::FWGenericHandle handle(type);
0369   try {
0370     m_event->getByLabel(tag, handle);
0371     setData(*handle);
0372   } catch (std::exception& iException) {
0373     if (!m_printedErrorThisEvent) {
0374       std::ostringstream s;
0375       s << "Failed to get " << name() << " because \n" << iException.what();
0376       m_errorMessage = s.str();
0377       m_printedErrorThisEvent = true;
0378     }
0379     return nullptr;
0380   }
0381 
0382   return m_accessor->data();
0383 }
0384 
0385 void FWEventItem::setData(const edm::ObjectWithDict& iData) const {
0386   m_accessor->setData(iData);
0387   //std::cout <<"size "<<m_accessor->size()<<std::endl;
0388   if (m_accessor->isCollection()) {
0389     m_itemInfos.reserve(m_accessor->size());
0390     m_itemInfos.resize(m_accessor->size(), ModelInfo(m_displayProperties, false));
0391   } else {
0392     m_itemInfos.push_back(ModelInfo(m_displayProperties, false));
0393   }
0394 }
0395 
0396 void FWEventItem::getPrimaryData() const {
0397   //if(0!=m_data) return;
0398   if (nullptr != m_accessor->data())
0399     return;
0400   this->data(*(m_type->GetTypeInfo()));
0401 }
0402 
0403 const FWDisplayProperties& FWEventItem::defaultDisplayProperties() const { return m_displayProperties; }
0404 
0405 int FWEventItem::layer() const { return m_layer; }
0406 
0407 bool FWEventItem::isInFront() const {
0408   assert(nullptr != m_context->eventItemsManager());
0409   for (FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
0410                                            itEnd = m_context->eventItemsManager()->end();
0411        it != itEnd;
0412        ++it) {
0413     if ((*it) && (*it != this) && (*it)->layer() >= layer()) {
0414       return false;
0415     }
0416   }
0417   return true;
0418 }
0419 
0420 bool FWEventItem::isInBack() const {
0421   assert(nullptr != m_context->eventItemsManager());
0422   for (FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
0423                                            itEnd = m_context->eventItemsManager()->end();
0424        it != itEnd;
0425        ++it) {
0426     if ((*it) && (*it != this) && (*it)->layer() <= layer()) {
0427       return false;
0428     }
0429   }
0430   return true;
0431 }
0432 
0433 unsigned int FWEventItem::id() const { return m_id; }
0434 
0435 const std::string& FWEventItem::name() const { return m_name; }
0436 
0437 const TClass* FWEventItem::type() const { return m_type; }
0438 
0439 const std::string& FWEventItem::purpose() const { return m_purpose; }
0440 
0441 const std::string& FWEventItem::moduleLabel() const { return m_moduleLabel; }
0442 const std::string& FWEventItem::productInstanceLabel() const { return m_productInstanceLabel; }
0443 
0444 const std::string& FWEventItem::processName() const { return m_processName; }
0445 
0446 FWEventItem::ModelInfo FWEventItem::modelInfo(int iIndex) const {
0447   getPrimaryData();
0448   if (m_displayProperties.isVisible()) {
0449     return m_itemInfos.at(iIndex);
0450   }
0451   FWDisplayProperties dp(m_itemInfos.at(iIndex).displayProperties());
0452   dp.setIsVisible(false);
0453   ModelInfo t(dp, m_itemInfos.at(iIndex).isSelected());
0454   return t;
0455 }
0456 
0457 size_t FWEventItem::size() const {
0458   getPrimaryData();
0459   return m_itemInfos.size();
0460 }
0461 
0462 bool FWEventItem::isCollection() const { return m_accessor->isCollection(); }
0463 
0464 const TClass* FWEventItem::modelType() const { return m_accessor->modelType(); }
0465 
0466 const void* FWEventItem::modelData(int iIndex) const {
0467   getPrimaryData();
0468   return m_accessor->modelData(iIndex);
0469 }
0470 
0471 std::string FWEventItem::modelName(int iIndex) const {
0472   std::ostringstream s;
0473   size_t lastChar = name().size();
0474   //if name ends in 's' assume it is plural and remove the s for the individual object
0475   if (name()[lastChar - 1] == 's') {
0476     --lastChar;
0477   }
0478   s << name().substr(0, lastChar) << " " << iIndex;
0479   return s.str();
0480 }
0481 
0482 bool FWEventItem::haveInterestingValue() const {
0483   return true;  //m_interestingValueGetter.isValid();
0484 }
0485 
0486 const std::string& FWEventItem::modelInterestingValueAsString(int iIndex) const {
0487   getPrimaryData();
0488   return m_interestingValueGetter.getToolTip(m_accessor->modelData(iIndex));
0489 }
0490 
0491 const std::string& FWEventItem::filterExpression() const { return m_filter.expression(); }
0492 
0493 void FWEventItem::destroy() const {
0494   //NOTE: need to unselect first before announcing destruction
0495   // because some items are listening to the display change and may
0496   // not properly release their connection to that signal after they
0497   // are destroyed via a connection to goingToBeDestroyed_
0498   const_cast<FWEventItem*>(this)->unselectItem();
0499   {
0500     FWChangeSentry sentry(*(changeManager()));
0501 
0502     for (int index = 0; index < static_cast<int>(size()); ++index) {
0503       if (m_itemInfos.at(index).m_isSelected) {
0504         FWModelId id(this, index);
0505         selectionManager()->unselect(id);
0506         changeManager()->changed(id);
0507       }
0508     }
0509   }
0510   goingToBeDestroyed_(this);
0511   delete this;
0512 }
0513 
0514 void FWEventItem::selectItem() {
0515   if (!m_isSelected) {
0516     m_isSelected = true;
0517     selectionManager()->selectItem(this);
0518     defaultDisplayPropertiesChanged_(this);
0519   }
0520 }
0521 void FWEventItem::unselectItem() {
0522   if (m_isSelected) {
0523     m_isSelected = false;
0524     selectionManager()->unselectItem(this);
0525     defaultDisplayPropertiesChanged_(this);
0526   }
0527 }
0528 void FWEventItem::toggleSelectItem() {
0529   m_isSelected = !m_isSelected;
0530   if (m_isSelected) {
0531     selectionManager()->selectItem(this);
0532   } else {
0533     selectionManager()->unselectItem(this);
0534   }
0535   defaultDisplayPropertiesChanged_(this);
0536 }
0537 bool FWEventItem::itemIsSelected() const { return m_isSelected; }
0538 
0539 bool FWEventItem::hasError() const { return !errorMessage().empty(); }
0540 
0541 const std::string& FWEventItem::errorMessage() const {
0542   if (m_errorMessage.empty()) {
0543     getPrimaryData();
0544   }
0545   return m_errorMessage;
0546 }
0547 
0548 const FWGeometry* FWEventItem::getGeom() const { return m_context->getGeom(); }
0549 
0550 void FWEventItem::resetColor() {
0551   m_displayProperties.setColor(m_origColor);
0552 
0553   FWChangeSentry sentry(*(this->changeManager()));
0554   for (int index = 0; index < static_cast<int>(size()); ++index) {
0555     m_itemInfos.at(index).m_displayProperties.setColor(m_origColor);
0556     FWModelId id(this, index);
0557     changeManager()->changed(id);
0558   }
0559 
0560   defaultDisplayPropertiesChanged_(this);
0561 }
0562 //
0563 // static member functions
0564 //