CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/Fireworks/Core/src/FWEventItemsManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWEventItemsManager
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Fri Jan  4 10:38:18 EST 2008
00011 // $Id: FWEventItemsManager.cc,v 1.49 2013/01/25 19:44:03 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <sstream>
00016 #include <boost/bind.hpp>
00017 #include "TClass.h"
00018 
00019 // user include files
00020 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00021 #include "Fireworks/Core/interface/FWEventItem.h"
00022 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00023 #include "Fireworks/Core/interface/FWColorManager.h"
00024 #include "Fireworks/Core/interface/FWConfiguration.h"
00025 #include "Fireworks/Core/interface/FWDisplayProperties.h"
00026 #include "Fireworks/Core/interface/FWItemAccessorFactory.h"
00027 #include "Fireworks/Core/interface/FWProxyBuilderConfiguration.h"
00028 #include "Fireworks/Core/interface/fwLog.h"
00029 #include <cassert>
00030 
00031 //
00032 // constants, enums and typedefs
00033 //
00034 
00035 //
00036 // static data member definitions
00037 //
00038 
00039 //
00040 // constructors and destructor
00041 //
00042 FWEventItemsManager::FWEventItemsManager(FWModelChangeManager* iManager) :
00043    m_changeManager(iManager),
00044    m_context(0),
00045    m_event(0),
00046    m_accessorFactory(new FWItemAccessorFactory())
00047 {
00048 }
00049 
00050 // FWEventItemsManager::FWEventItemsManager(const FWEventItemsManager& rhs)
00051 // {
00052 //    // do actual copying here;
00053 // }
00054 
00061 FWEventItemsManager::~FWEventItemsManager()
00062 {
00063    for (size_t i = 0, e = m_items.size(); i != e; ++i)
00064       delete m_items[i];
00065 
00066    m_items.clear();
00067 }
00068 
00069 //
00070 // assignment operators
00071 //
00072 // const FWEventItemsManager& FWEventItemsManager::operator=(const FWEventItemsManager& rhs)
00073 // {
00074 //   //An exception safe implementation is
00075 //   FWEventItemsManager temp(rhs);
00076 //   swap(rhs);
00077 //
00078 //   return *this;
00079 // }
00080 
00081 //
00082 // member functions
00083 //
00084 const FWEventItem*
00085 FWEventItemsManager::add(const FWPhysicsObjectDesc& iItem, const FWConfiguration* pbc)
00086 {
00087    FWPhysicsObjectDesc temp(iItem);
00088    
00089    if(! m_context->colorManager()->colorHasIndex(temp.displayProperties().color())) {
00090       FWDisplayProperties prop(temp.displayProperties());
00091       fwLog(fwlog::kWarning) << Form("FWEventItemsManager::add(const FWPhysicsObjectDesc& iItem), color index  not valid. Set Color idex to %d\n", FWColorManager::getDefaultStartColorIndex());
00092       prop.setColor(FWColorManager::getDefaultStartColorIndex());
00093       temp.setDisplayProperties(prop);
00094    }
00095    
00096    m_items.push_back(new FWEventItem(m_context,m_items.size(),m_accessorFactory->accessorFor(temp.type()),
00097                                      temp, pbc));
00098    newItem_(m_items.back());
00099    m_items.back()->goingToBeDestroyed_.connect(boost::bind(&FWEventItemsManager::removeItem,this,_1));
00100    if(m_event) {
00101       FWChangeSentry sentry(*m_changeManager);
00102       m_items.back()->setEvent(m_event);
00103    }
00104    return m_items.back();
00105 }
00106 
00110 void
00111 FWEventItemsManager::newEvent(const edm::EventBase* iEvent)
00112 {
00113    FWChangeSentry sentry(*m_changeManager);
00114    m_event = iEvent;
00115    for(size_t i = 0, e = m_items.size(); i != e; ++i)
00116    {
00117       FWEventItem *item = m_items[i];
00118       if(item)
00119          item->setEvent(iEvent);
00120    }
00121 }
00122 
00128 void
00129 FWEventItemsManager::clearItems(void)
00130 {
00131    for (size_t i = 0, e = m_items.size(); i != e; ++i)
00132    {
00133       FWEventItem *item = m_items[i];
00134       if (item) {
00135          item->destroy();
00136       }
00137       m_items[i]=0;
00138    }
00139    goingToClearItems_();
00140 
00141    m_items.clear();
00142 }
00143 
00144 static const std::string kType("type");
00145 static const std::string kModuleLabel("moduleLabel");
00146 static const std::string kProductInstanceLabel("productInstanceLabel");
00147 static const std::string kProcessName("processName");
00148 static const std::string kFilterExpression("filterExpression");
00149 static const std::string kColor("color");
00150 static const std::string kIsVisible("isVisible");
00151 static const std::string kTrue("t");
00152 static const std::string kFalse("f");
00153 static const std::string kLayer("layer");
00154 static const std::string kPurpose("purpose");
00155 static const std::string kTransparency("transparency");
00156 
00157 void
00158 FWEventItemsManager::addTo(FWConfiguration& iTo) const
00159 {
00160    FWColorManager* cm = m_context->colorManager();
00161    assert(0!=cm);
00162    for(std::vector<FWEventItem*>::const_iterator it = m_items.begin();
00163        it != m_items.end();
00164        ++it)
00165    {
00166       if(!*it) continue;
00167       FWConfiguration conf(6);
00168       edm::TypeWithDict dataType((*((*it)->type()->GetTypeInfo())));
00169       assert(dataType != edm::TypeWithDict() );
00170 
00171       conf.addKeyValue(kType,FWConfiguration(dataType.name()));
00172       conf.addKeyValue(kModuleLabel,FWConfiguration((*it)->moduleLabel()));
00173       conf.addKeyValue(kProductInstanceLabel, FWConfiguration((*it)->productInstanceLabel()));
00174       conf.addKeyValue(kProcessName, FWConfiguration((*it)->processName()));
00175       conf.addKeyValue(kFilterExpression, FWConfiguration((*it)->filterExpression()));
00176       {
00177          std::ostringstream os;
00178          os << (*it)->defaultDisplayProperties().color();
00179          conf.addKeyValue(kColor, FWConfiguration(os.str()));
00180       }
00181       conf.addKeyValue(kIsVisible, FWConfiguration((*it)->defaultDisplayProperties().isVisible() ? kTrue : kFalse));
00182       {
00183          std::ostringstream os;
00184          os << (*it)->layer();
00185          conf.addKeyValue(kLayer,FWConfiguration(os.str()));
00186       }
00187       conf.addKeyValue(kPurpose,(*it)->purpose());
00188       {
00189          std::ostringstream os;
00190          os << static_cast<int>((*it)->defaultDisplayProperties().transparency());
00191          conf.addKeyValue(kTransparency, FWConfiguration(os.str()));
00192       }
00193 
00194       FWConfiguration pbTmp;
00195       (*it)->getConfig()->addTo(pbTmp);
00196       conf.addKeyValue("PBConfig",pbTmp, true);
00197 
00198       iTo.addKeyValue((*it)->name(), conf, true);
00199    }
00200 }
00201 
00204 void
00205 FWEventItemsManager::setFrom(const FWConfiguration& iFrom)
00206 {
00207  
00208    FWColorManager* cm = m_context->colorManager();
00209    assert(0!=cm);
00210 
00211    clearItems();
00212    const FWConfiguration::KeyValues* keyValues =  iFrom.keyValues();
00213 
00214    if (keyValues == 0) return;
00215 
00216    for (FWConfiguration::KeyValues::const_iterator it = keyValues->begin();
00217         it != keyValues->end();
00218         ++it)
00219    {
00220       const std::string& name = it->first;
00221       const FWConfiguration& conf = it->second;
00222       const FWConfiguration::KeyValues* keyValues =  conf.keyValues();
00223       assert(0!=keyValues);
00224       const std::string& type = (*keyValues)[0].second.value();
00225       const std::string& moduleLabel = (*keyValues)[1].second.value();
00226       const std::string& productInstanceLabel = (*keyValues)[2].second.value();
00227       const std::string& processName = (*keyValues)[3].second.value();
00228       const std::string& filterExpression = (*keyValues)[4].second.value();
00229       const std::string& sColor = (*keyValues)[5].second.value();
00230       const bool isVisible = (*keyValues)[6].second.value() == kTrue;
00231 
00232       unsigned int colorIndex;
00233       if(conf.version() < 5)
00234       {
00235          std::istringstream is(sColor);
00236          Color_t color;
00237          is >> color;
00238          colorIndex = cm->oldColorToIndex(color, conf.version());
00239       }
00240       else
00241       {
00242          // In version 4 we assume:
00243          //   fireworks colors start at ROOT index 1000
00244          //   geometry  colors start at ROOT index 1100
00245          // We save them as such -- no conversions needed.
00246          std::istringstream is(sColor);
00247          is >> colorIndex;
00248       }
00249       
00250       int transparency = 0;
00251 
00252       // Read transparency from file. We don't care about checking errors
00253       // because strtol returns 0 in that case.
00254       if (conf.version() > 3)
00255          transparency = strtol((*keyValues)[9].second.value().c_str(), 0, 10);
00256 
00257       FWDisplayProperties dp(colorIndex, isVisible, transparency);
00258 
00259       unsigned int layer = strtol((*keyValues)[7].second.value().c_str(), 0, 10);
00260 
00261       //For older configs assume name is the same as purpose
00262       std::string purpose(name);
00263       if (conf.version() > 1)
00264          purpose = (*keyValues)[8].second.value();
00265 
00266       FWConfiguration* proxyConfig = (FWConfiguration*) conf.valueForKey("PBConfig") ? new FWConfiguration(*conf.valueForKey("PBConfig")) : 0;
00267 
00268       // beckward compatibilty for obsolete proxy builders
00269       if (conf.version() < 6)
00270       {
00271          assert(proxyConfig == 0);
00272          if (purpose == "VerticesWithTracks")
00273          {
00274             purpose = "Vertices";
00275             proxyConfig = new FWConfiguration();
00276             FWConfiguration vTmp; vTmp.addKeyValue("Draw Tracks", FWConfiguration("1"));
00277             proxyConfig->addKeyValue("Var", vTmp,true);
00278          }
00279       }
00280 
00281       FWPhysicsObjectDesc desc(name,
00282                                TClass::GetClass(type.c_str()),
00283                                purpose,
00284                                dp,
00285                                moduleLabel,
00286                                productInstanceLabel,
00287                                processName,
00288                                filterExpression,
00289                                layer);
00290       
00291       add(desc, proxyConfig );
00292    }
00293 }
00294 
00305 void
00306 FWEventItemsManager::removeItem(const FWEventItem* iItem)
00307 {
00308    assert(iItem->id() < m_items.size());
00309    m_items[iItem->id()] = 0;
00310 }
00311 
00312 void
00313 FWEventItemsManager::setContext(fireworks::Context* iContext)
00314 {
00315    m_context = iContext;
00316 }
00317 
00318 //
00319 // const member functions
00320 //
00321 FWEventItemsManager::const_iterator
00322 FWEventItemsManager::begin() const
00323 {
00324    return m_items.begin();
00325 }
00326 FWEventItemsManager::const_iterator
00327 FWEventItemsManager::end() const
00328 {
00329    return m_items.end();
00330 }
00331 
00334 const FWEventItem*
00335 FWEventItemsManager::find(const std::string& iName) const
00336 {
00337    for (size_t i = 0, e = m_items.size(); i != e; ++i)
00338    {
00339       const FWEventItem *item = m_items[i];
00340       if (item && item->name() == iName)
00341          return item;
00342    }
00343    return 0;
00344 }
00345 
00346 //
00347 // static member functions
00348 //