CMS 3D CMS Logo

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