CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/Fireworks/Core/src/FWEventItem.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWEventItem
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Thu Jan  3 14:59:23 EST 2008
00011 // $Id: FWEventItem.cc,v 1.58 2011/11/18 02:57:08 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <iostream>
00016 #include <algorithm>
00017 #include <exception>
00018 #include <TClass.h>
00019 
00020 // user include files
00021 #include "Fireworks/Core/interface/FWEventItem.h"
00022 #include "DataFormats/FWLite/interface/Event.h"
00023 // Needed to test edm::Event access
00024 // #include "FWCore/Framework/interface/Event.h"
00025 #include "Fireworks/Core/interface/FWModelId.h"
00026 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00027 #include "Fireworks/Core/interface/FWSelectionManager.h"
00028 #include "Fireworks/Core/interface/FWItemAccessorBase.h"
00029 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00030 #include "Fireworks/Core/interface/FWProxyBuilderConfiguration.h"
00031 #include "Fireworks/Core/src/FWGenericHandle.h"
00032 #include "Fireworks/Core/interface/FWGeometry.h"
00033 #include "Fireworks/Core/interface/fwLog.h"
00034 
00035 //
00036 // static data member definitions
00037 //
00038 
00039 int FWEventItem::minLayerValue()
00040 {
00041    return -100;
00042 }
00043 
00044 int FWEventItem::maxLayerValue()
00045 {
00046    return 100;
00047 }
00048 
00049 
00050 //
00051 // constructors and destructor
00052 //
00053 FWEventItem::FWEventItem(fireworks::Context* iContext,
00054                          unsigned int iId,
00055                          boost::shared_ptr<FWItemAccessorBase> iAccessor,
00056                          const FWPhysicsObjectDesc& iDesc,  const FWConfiguration* pbc) :
00057    m_context(iContext),
00058    m_id(iId),
00059    m_name(iDesc.name()),
00060    m_type(iDesc.type()),
00061    m_purpose(iDesc.purpose()),
00062    m_accessor(iAccessor),
00063    m_displayProperties(iDesc.displayProperties()),
00064    m_layer(iDesc.layer()),
00065    m_moduleLabel(iDesc.moduleLabel()),
00066    m_productInstanceLabel(iDesc.productInstanceLabel()),
00067    m_processName(iDesc.processName()),
00068    m_event(0),
00069    m_interestingValueGetter(ROOT::Reflex::Type::ByTypeInfo(*(m_accessor->modelType()->GetTypeInfo())), m_purpose),
00070    m_filter(iDesc.filterExpression(),""),
00071    m_printedErrorThisEvent(false),
00072    m_isSelected(false),
00073    m_proxyBuilderConfig(0)
00074 {
00075    //assert(m_type->GetTypeInfo());
00076    //ROOT::Reflex::Type dataType( ROOT::Reflex::Type::ByTypeInfo(*(m_type->GetTypeInfo())));
00077    //assert(dataType != ROOT::Reflex::Type() );
00078    //
00079    //std::string dataTypeName = dataType.Name(ROOT::Reflex::SCOPED);
00080    //if (dataTypeName[dataTypeName.size() -1] == '>')
00081    //   dataTypeName += " ";
00082    //std::string wrapperName = "edm::Wrapper<" + dataTypeName + ">";
00083    //
00084    //fwLog(fwlog::kDebug) << "Looking for the wrapper name" 
00085    //                    << wrapperName << std::endl;
00086    //m_wrapperType = ROOT::Reflex::Type::ByName(wrapperName);
00087    //
00088    //assert(m_wrapperType != ROOT::Reflex::Type());
00089    if(!m_accessor->isCollection()) {
00090       m_itemInfos.reserve(1);
00091    }
00092    m_filter.setClassName(modelType()->GetName());
00093    m_proxyBuilderConfig = new FWProxyBuilderConfiguration(pbc, this);
00094 }
00095 // FWEventItem::FWEventItem(const FWEventItem& rhs)
00096 // {
00097 //    // do actual copying here;
00098 // }
00099 
00100 FWEventItem::~FWEventItem()
00101 {
00102   delete m_proxyBuilderConfig;
00103 }
00104  
00105 //
00106 // assignment operators
00107 //
00108 // const FWEventItem& FWEventItem::operator=(const FWEventItem& rhs)
00109 // {
00110 //   //An exception safe implementation is
00111 //   FWEventItem temp(rhs);
00112 //   swap(rhs);
00113 //
00114 //   return *this;
00115 // }
00116 
00117 //
00118 // member functions
00119 //
00120 void
00121 FWEventItem::setEvent(const edm::EventBase* iEvent)
00122 {
00123    m_printedErrorThisEvent = false;
00124    m_event = iEvent;
00125    m_accessor->reset();
00126    m_itemInfos.clear();
00127    handleChange();
00128 }
00129 
00130 void
00131 FWEventItem::setLabels(const std::string& iModule,
00132                        const std::string& iProductInstance,
00133                        const std::string& iProcess)
00134 {
00135    m_moduleLabel = iModule;
00136    m_productInstanceLabel = iProductInstance;
00137    m_processName = iProcess;
00138    m_accessor->reset();
00139    m_itemInfos.clear();
00140    handleChange();
00141 }
00142 
00143 void
00144 FWEventItem::setName(const std::string& iName)
00145 {
00146    m_name = iName;
00147 }
00148 
00155 void
00156 FWEventItem::setDefaultDisplayProperties(const FWDisplayProperties& iProp)
00157 {
00158    bool visChange = m_displayProperties.isVisible() != iProp.isVisible();
00159    bool colorChanged = m_displayProperties.color() != iProp.color();
00160    bool transparencyChanged = m_displayProperties.transparency() != iProp.transparency();
00161 
00162    if(!visChange && !colorChanged && !transparencyChanged) {
00163       return;
00164    }
00165    //If the default visibility is changed, we want to also change the the visibility of the children
00166    // BUT we want to remember the old visibility so if the visibility is changed again we return
00167    // to the previous state.
00168    // only the visible ones need to be marked as 'changed'
00169    FWChangeSentry sentry(*(changeManager()));
00170 
00171    for(int index=0; index <static_cast<int>(size()); ++index) {
00172       FWDisplayProperties prp = m_itemInfos[index].displayProperties();
00173       bool vis=prp.isVisible();
00174       bool changed = false;
00175       changed = visChange && vis;
00176 
00177       if(colorChanged) {
00178          if(m_displayProperties.color()==prp.color()) {
00179             prp.setColor(iProp.color());
00180             changed = true;
00181          }
00182       }
00183       if (transparencyChanged) {
00184          if(m_displayProperties.transparency() == prp.transparency()) {
00185             prp.setTransparency(iProp.transparency());
00186             changed = true;
00187          }
00188       }
00189       if(changed) {
00190          m_itemInfos[index].m_displayProperties=prp;
00191          FWModelId id(this,index);
00192          changeManager()->changed(id);
00193       }
00194    }
00195    m_displayProperties= iProp;
00196    defaultDisplayPropertiesChanged_(this);
00197 }
00198 
00199 void
00200 FWEventItem::setFilterExpression(const std::string& iExpression)
00201 {
00202    m_filter.setExpression(iExpression);
00203    filterChanged_(this);
00204    runFilter();
00205 }
00206 
00207 void
00208 FWEventItem::runFilter()
00209 {
00210    if(m_accessor->isCollection() && m_accessor->data()) {
00211       //std::cout <<"runFilter"<<std::endl;
00212       FWChangeSentry sentry(*(this->changeManager()));
00213       int size = m_accessor->size();
00214       std::vector<ModelInfo>::iterator itInfo = m_itemInfos.begin();
00215       try {
00216          for(int index = 0; index != size; ++index,++itInfo) {
00217             bool changed = false;
00218             bool wasVisible = itInfo->m_displayProperties.isVisible();
00219             if(not m_filter.passesFilter(m_accessor->modelData(index))) {
00220                itInfo->m_displayProperties.setIsVisible(false);
00221                changed = wasVisible==true;
00222             } else {
00223                itInfo->m_displayProperties.setIsVisible(true);
00224                changed = wasVisible==false;
00225             }
00226             if(changed) {
00227                FWModelId id(this,index);
00228                changeManager()->changed(id);
00229             }
00230          }
00231       } catch( const std::exception& iException) {
00232          //Should log this error
00233          std::cerr <<"Exception occurred while running filter on "<<name()<<"\n"
00234          <<iException.what()<<std::endl;
00235       }
00236    }
00237 }
00238 
00239 void
00240 FWEventItem::unselect(int iIndex) const
00241 {
00242    //check if this is a change
00243    if(bool& sel = m_itemInfos.at(iIndex).m_isSelected) {
00244       sel=false;
00245       FWModelId id(this,iIndex);
00246       selectionManager()->unselect(id);
00247       changeManager()->changed(id);
00248    }
00249 }
00250 void
00251 FWEventItem::select(int iIndex) const
00252 {
00253    bool& sel = m_itemInfos.at(iIndex).m_isSelected;
00254    if(not sel) {
00255       sel = true;
00256       FWModelId id(this,iIndex);
00257       selectionManager()->select(id);
00258       //want to make it obvious what type of object was selected
00259       // therefore we also select the item
00260       const_cast<FWEventItem*>(this)->selectItem();
00261       changeManager()->changed(id);
00262    }
00263 }
00264 void
00265 FWEventItem::toggleSelect(int iIndex) const
00266 {
00267    bool& sel = m_itemInfos.at(iIndex).m_isSelected;
00268    sel = not sel;
00269    FWModelId id(this,iIndex);
00270    if (sel)
00271       selectionManager()->select(id);
00272    else selectionManager()->unselect(id);
00273    changeManager()->changed(id);
00274 }
00275 
00276 void
00277 FWEventItem::setDisplayProperties(int iIndex, const FWDisplayProperties& iProps) const
00278 {
00279    FWDisplayProperties& prop = m_itemInfos.at(iIndex).m_displayProperties;
00280    if(m_displayProperties.isVisible()) {
00281       if( prop
00282           != iProps ) {
00283          prop = iProps;
00284          FWModelId id(this,iIndex);
00285          //selectionManager()->select(id);
00286          changeManager()->changed(id);
00287       }
00288    } else {
00289       if(iProps.isVisible()) {
00290          FWChangeSentry sentry(*(this->changeManager()));
00291          int size = m_accessor->size();
00292          std::vector<ModelInfo>::iterator itInfo = m_itemInfos.begin();
00293          for(int index = 0; index != size; ++index,++itInfo) {
00294             if( itInfo->m_displayProperties.isVisible() ) {
00295                itInfo->m_displayProperties.setIsVisible(false);
00296                FWModelId id(this,index);
00297                changeManager()->changed(id);
00298             }
00299          }
00300          m_itemInfos.at(iIndex).m_displayProperties.setIsVisible(true);
00301          FWModelId id(this,iIndex);
00302          changeManager()->changed(id);
00303          const_cast<FWEventItem*>(this)->m_displayProperties.setIsVisible(true);
00304          //NOTE: need to send out a signal here
00305          defaultDisplayPropertiesChanged_(this);
00306       }
00307    }
00308 }
00309 
00310 void
00311 FWEventItem::moveToFront()
00312 {
00313    assert(0!=m_context->eventItemsManager());
00314    int largest = layer();
00315    for(FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
00316           itEnd = m_context->eventItemsManager()->end();
00317        it != itEnd;
00318        ++it) {
00319       if ((*it) && (*it != this) && (*it)->layer() > largest) {
00320          largest= (*it)->layer();
00321       }
00322    }
00323 
00324    if(largest >= layer()) {
00325       m_layer = std::min(largest+1, maxLayerValue());
00326    }
00327 
00328    m_itemInfos.clear();
00329    m_accessor->reset();
00330    handleChange();
00331 }
00332 
00333 void
00334 FWEventItem::moveToBack()
00335 {
00336    assert(0!=m_context->eventItemsManager());
00337    int smallest = layer();
00338    for(FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
00339                                            itEnd = m_context->eventItemsManager()->end();
00340        it != itEnd;
00341        ++it) {
00342       if((*it) && (*it != this) && (*it)->layer() < smallest) {
00343          smallest= (*it)->layer();
00344       }
00345    }
00346 
00347    if(smallest <= layer()) {
00348       m_layer = std::max(smallest-1, minLayerValue());
00349    }
00350 
00351    m_itemInfos.clear();
00352    m_accessor->reset();
00353    handleChange();
00354 }
00355 
00356 void
00357 FWEventItem::moveToLayer(int layer)
00358 {
00359    assert(0!=m_context->eventItemsManager());
00360 
00361    m_layer = std::max(std::min(layer, maxLayerValue()), minLayerValue());
00362 
00363    m_itemInfos.clear();
00364    m_accessor->reset();
00365    handleChange();
00366 }
00367 
00368 void
00369 FWEventItem::proxyConfigChanged()
00370 {
00371    m_itemInfos.clear();
00372    m_accessor->reset();
00373    if (m_context->eventItemsManager())
00374       handleChange();
00375 
00376 }
00377 
00378 void 
00379 FWEventItem::handleChange()
00380 {
00381    preItemChanged_(this);
00382    FWChangeSentry sentry(*(this->changeManager()));
00383    //want filter to rerun after all changes have been made
00384    changeManager()->changed(this);
00385    getPrimaryData();
00386    runFilter();
00387 }
00388 
00389 //
00390 // const member functions
00391 //
00392 const void*
00393 FWEventItem::data(const std::type_info& iInfo) const
00394 {
00395    using namespace Reflex;
00396    //At the moment this is a programming error
00397    assert(iInfo == *(m_type->GetTypeInfo()));
00398 
00399    //lookup data if we don't already have it
00400    if (m_accessor->data())
00401       return m_accessor->data();
00402 
00403    m_errorMessage.clear();
00404    if (!m_event)
00405       return m_accessor->data();
00406    
00407    // Retrieve the data from the event.
00408    edm::InputTag tag(m_moduleLabel, m_productInstanceLabel, m_processName);
00409    edm::FWGenericHandle handle(Reflex::Type::ByTypeInfo(iInfo));
00410    try
00411    {
00412       m_event->getByLabel(tag, handle);
00413       setData(*handle);
00414    }
00415    catch (std::exception& iException)
00416    {
00417       if (!m_printedErrorThisEvent) 
00418       {
00419          std::ostringstream s;
00420          s << "Failed to get " << name() << " because \n" <<iException.what();
00421          m_errorMessage=s.str();
00422          m_printedErrorThisEvent = true;
00423       }
00424       return 0;
00425    }
00426    
00427    return m_accessor->data();
00428 }
00429 
00430 void
00431 FWEventItem::setData(const Reflex::Object& iData) const
00432 {
00433    m_accessor->setData(iData);
00434    //std::cout <<"size "<<m_accessor->size()<<std::endl;
00435    if(m_accessor->isCollection()) {
00436       m_itemInfos.reserve(m_accessor->size());
00437       m_itemInfos.resize(m_accessor->size(),ModelInfo(m_displayProperties,false));
00438    } else {
00439       m_itemInfos.push_back(ModelInfo(m_displayProperties,false));
00440    }
00441 }
00442 
00443 void
00444 FWEventItem::getPrimaryData() const
00445 {
00446    //if(0!=m_data) return;
00447    if(0!=m_accessor->data()) return;
00448    this->data(*(m_type->GetTypeInfo()));
00449 }
00450 
00451 const FWDisplayProperties&
00452 FWEventItem::defaultDisplayProperties() const
00453 {
00454    return m_displayProperties;
00455 }
00456 
00457 int
00458 FWEventItem::layer() const
00459 {
00460    return m_layer;
00461 }
00462 
00463 bool
00464 FWEventItem::isInFront() const
00465 {
00466    assert(0!=m_context->eventItemsManager());
00467    for(FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
00468                                            itEnd = m_context->eventItemsManager()->end();
00469        it != itEnd;
00470        ++it) {
00471       if((*it) && (*it != this) && (*it)->layer() >= layer()) {
00472          return false;
00473       }
00474    }
00475    return true;
00476 }
00477 
00478 bool
00479 FWEventItem::isInBack() const
00480 {
00481    assert(0!=m_context->eventItemsManager());
00482    for(FWEventItemsManager::const_iterator it = m_context->eventItemsManager()->begin(),
00483                                            itEnd = m_context->eventItemsManager()->end();
00484        it != itEnd;
00485        ++it) {
00486       if((*it) && (*it != this) && (*it)->layer() <= layer()) {
00487          return false;
00488       }
00489    }
00490    return true;
00491 }
00492 
00493 
00494 unsigned int
00495 FWEventItem::id() const
00496 {
00497    return m_id;
00498 }
00499 
00500 const std::string&
00501 FWEventItem::name() const
00502 {
00503    return m_name;
00504 }
00505 
00506 const TClass*
00507 FWEventItem::type() const
00508 {
00509    return m_type;
00510 }
00511 
00512 const std::string&
00513 FWEventItem::purpose() const
00514 {
00515    return m_purpose;
00516 }
00517 
00518 const std::string&
00519 FWEventItem::moduleLabel() const
00520 {
00521    return m_moduleLabel;
00522 }
00523 const std::string&
00524 FWEventItem::productInstanceLabel() const
00525 {
00526    return m_productInstanceLabel;
00527 }
00528 
00529 const std::string&
00530 FWEventItem::processName() const
00531 {
00532    return m_processName;
00533 }
00534 
00535 FWEventItem::ModelInfo
00536 FWEventItem::modelInfo(int iIndex) const
00537 {
00538    getPrimaryData();
00539    if(m_displayProperties.isVisible()) {
00540       return m_itemInfos.at(iIndex);
00541    }
00542    FWDisplayProperties dp(m_itemInfos.at(iIndex).displayProperties());
00543    dp.setIsVisible(false);
00544    ModelInfo t(dp,m_itemInfos.at(iIndex).isSelected());
00545    return t;
00546 }
00547 
00548 size_t
00549 FWEventItem::size() const
00550 {
00551    getPrimaryData();
00552    return m_itemInfos.size();
00553 }
00554 
00555 bool
00556 FWEventItem::isCollection() const
00557 {
00558    return m_accessor->isCollection();
00559 }
00560 
00561 const TClass*
00562 FWEventItem::modelType() const
00563 {
00564    return m_accessor->modelType();
00565 }
00566 
00567 const void*
00568 FWEventItem::modelData(int iIndex) const
00569 {
00570    getPrimaryData();
00571    return m_accessor->modelData(iIndex);
00572 }
00573 
00574 std::string
00575 FWEventItem::modelName(int iIndex) const
00576 {
00577    std::ostringstream s;
00578    size_t lastChar = name().size();
00579    //if name ends in 's' assume it is plural and remove the s for the individual object
00580    if(name()[lastChar-1]=='s') {
00581       --lastChar;
00582    }
00583    s<<name().substr(0,lastChar)<<" "<<iIndex;
00584    return s.str();
00585 }
00586 
00587 bool
00588 FWEventItem::haveInterestingValue() const
00589 {
00590    return true; //m_interestingValueGetter.isValid();
00591 }
00592 
00593  
00594 const std::string&
00595 FWEventItem::modelInterestingValueAsString(int iIndex) const
00596 {
00597    getPrimaryData();
00598    return m_interestingValueGetter.getToolTip(m_accessor->modelData(iIndex));
00599 }
00600 
00601 
00602 const std::string&
00603 FWEventItem::filterExpression() const
00604 {
00605    return m_filter.expression();
00606 }
00607 
00608 void
00609 FWEventItem::destroy() const
00610 {
00611    //NOTE: need to unselect first before announcing destruction
00612    // because some items are listening to the display change and may
00613    // not properly release their connection to that signal after they
00614    // are destroyed via a connection to goingToBeDestroyed_
00615    const_cast<FWEventItem*>(this)->unselectItem();
00616    {
00617       FWChangeSentry sentry(*(changeManager()));
00618    
00619       for(int index=0; index <static_cast<int>(size()); ++index) {
00620          if(m_itemInfos.at(index).m_isSelected) {
00621             FWModelId id(this,index);
00622             selectionManager()->unselect(id);
00623             changeManager()->changed(id);
00624          }
00625       }
00626    }
00627    goingToBeDestroyed_(this);
00628    delete this;
00629 }
00630 
00631 
00632 void
00633 FWEventItem::selectItem()
00634 {
00635    if(!m_isSelected) {
00636       m_isSelected=true;
00637       selectionManager()->selectItem(this);
00638       defaultDisplayPropertiesChanged_(this);
00639    }
00640 }
00641 void
00642 FWEventItem::unselectItem()
00643 {
00644    if(m_isSelected) {
00645       m_isSelected=false;
00646       selectionManager()->unselectItem(this);
00647       defaultDisplayPropertiesChanged_(this);
00648    }
00649 }
00650 void
00651 FWEventItem::toggleSelectItem()
00652 {
00653    m_isSelected = !m_isSelected;
00654    if(m_isSelected) {
00655       selectionManager()->selectItem(this);
00656    }else {
00657       selectionManager()->unselectItem(this);
00658    }
00659    defaultDisplayPropertiesChanged_(this);
00660 }
00661 bool
00662 FWEventItem::itemIsSelected() const
00663 {
00664    return m_isSelected;
00665 }
00666 
00667 bool
00668 FWEventItem::hasError() const {
00669    return !errorMessage().empty();
00670 }
00671 
00672 const std::string&
00673 FWEventItem::errorMessage() const
00674 {
00675    if(m_errorMessage.empty()) {
00676       getPrimaryData();
00677    }
00678    return m_errorMessage;
00679 }
00680 
00681 const FWGeometry* 
00682 FWEventItem::getGeom() const {
00683    return m_context->getGeom();
00684 }
00685 //
00686 // static member functions
00687 //