CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Fireworks/Core/src/FWTableViewManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWTableViewManager
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:
00010 //         Created:  Sun Jan  6 22:01:27 EST 2008
00011 // $Id: FWTableViewManager.cc,v 1.27 2013/02/10 22:12:04 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <iostream>
00016 #include <boost/bind.hpp>
00017 #include <algorithm>
00018 
00019 #include "TEveManager.h"
00020 #include "TClass.h"
00021 #include "FWCore/Utilities/interface/BaseWithDict.h"
00022 #include "FWCore/Utilities/interface/MemberWithDict.h"
00023 #include "FWCore/Utilities/interface/FunctionWithDict.h"
00024 
00025 // user include files
00026 #include "Fireworks/Core/interface/FWConfiguration.h"
00027 #include "Fireworks/Core/interface/FWTableViewManager.h"
00028 #include "Fireworks/Core/interface/FWEventItem.h"
00029 #include "Fireworks/Core/interface/FWGUIManager.h"
00030 #include "Fireworks/Core/interface/FWColorManager.h"
00031 
00032 #include "Fireworks/Core/interface/FWTypeToRepresentations.h"
00033 #include "Fireworks/Core/interface/fwLog.h"
00034 
00035 
00036 //
00037 // constants, enums and typedefs
00038 //
00039 
00040 //
00041 // static data member definitions
00042 //
00043 
00044 //
00045 // constructors and destructor
00046 //
00047 FWTableViewManager::FWTableViewManager(FWGUIManager* iGUIMgr)
00048 :FWViewManagerBase()
00049 {
00050    FWGUIManager::ViewBuildFunctor f;
00051    f=boost::bind(&FWTableViewManager::buildView,
00052                  this, _1, _2);
00053    iGUIMgr->registerViewBuilder(FWViewType::idToName(FWViewType::kTable), f);
00054 
00055    // ---------- for some object types, we have default table contents ----------
00056    table("reco::GenParticle").
00057    column("pT", 1, "pt").
00058    column("eta", 3).
00059    column("phi", 3).
00060    column("status", TableEntry::INT).
00061    column("pdgId", TableEntry::INT);
00062 
00063    table("reco::Muon").
00064    column("q", TableEntry::INT, "charge").
00065    column("pT", 1, "pt").
00066    column("global", TableEntry::BOOL, "isGlobalMuon").
00067    column("tracker", TableEntry::BOOL, "isTrackerMuon").
00068    column("SA", TableEntry::BOOL, "isStandAloneMuon").
00069    column("calo", TableEntry::BOOL, "isCaloMuon").
00070    column("tr pt", 1, "track().pt()").
00071    column("eta", 3).
00072    column("phi", 3).
00073    column("matches", TableEntry::INT, "numberOfMatches('SegmentArbitration')").
00074    column("d0", 3, "track().d0()").
00075    column("d0 / d0Err", 3, "track().d0() / track().d0Error()");
00076 
00077    table("reco::GsfElectron").
00078    column("q", TableEntry::INT, "charge").
00079    column("pT", 1, "pt").
00080    column("eta", 3).
00081    column("phi", 3).
00082    column("E/p", 3, "eSuperClusterOverP").
00083    column("H/E", 3, "hadronicOverEm").
00084    column("fbrem", 3, "(trackMomentumAtVtx().R() - trackMomentumOut().R()) / trackMomentumAtVtx().R()").
00085    column("dei", 3, "deltaEtaSuperClusterTrackAtVtx()").
00086    column("dpi", 3, "deltaPhiSuperClusterTrackAtVtx()");
00087 
00088    table("reco::Photon").
00089    column("pT", 1, "pt").
00090    column("eta", 3).
00091    column("phi", 3).
00092    column("H/E", 3, "hadronicOverEm");
00093 
00094    table("reco::CaloJet").
00095    column("pT", 1, "pt").
00096    column("eta", 3).
00097    column("phi", 3).
00098    column("ECAL", 1, "p4().E() * emEnergyFraction()").
00099    column("HCAL", 1, "p4().E() * energyFractionHadronic()").
00100    column("emf", 3, "emEnergyFraction()");
00101 
00102    table("reco::Jet").
00103    column("pT", 1, "pt").
00104    column("eta", 3).
00105    column("phi", 3);
00106 
00107    table("reco::MET").
00108    column("et", 1).
00109    column("phi", 3).
00110    column("sumEt", 1).
00111    column("mEtSig", 3);
00112 
00113    table("reco::Track").
00114    column("q", TableEntry::INT, "charge").
00115    column("pT", 1, "pt").
00116    column("eta", 3).
00117    column("phi", 3).
00118    column("d0", 5).
00119    column("d0Err", 5, "d0Error").
00120    column("dz", 5).
00121    column("dzErr", 5, "dzError").
00122    column("vx", 5).
00123    column("vy", 5).
00124    column("vz", 5).
00125    column("pixel hits", TableEntry::INT, "hitPattern().numberOfValidPixelHits()").
00126    column("strip hits", TableEntry::INT, "hitPattern().numberOfValidStripHits()").
00127    column("chi2", 3).
00128    column("ndof", TableEntry::INT);
00129 
00130    table("reco::Vertex").
00131    column("x", 5).
00132    column("xError", 5).
00133    column("y", 5).
00134    column("yError", 5).
00135    column("z", 5).
00136    column("zError", 5).
00137    column("tracks", TableEntry::INT, "tracksSize").
00138    column("chi2", 3).
00139    column("ndof", 3);
00140 
00141    table("CaloTower").
00142    column("emEt", 1).
00143    column("hadEt", 1).
00144    column("et", 1, "Et").
00145    column("eta", 3).
00146    column("phi", 3);
00147    
00148    table("CaloRecHit").
00149    column("id", TableEntry::INT,"detid.rawId").
00150    column("energy",3).
00151    column("time",3).
00152    column("flags",TableEntry::INT,"flags");
00153 }
00154 
00155 FWTableViewManager::~FWTableViewManager()
00156 {
00157 }
00158 
00159 //
00160 // member functions
00161 //
00162 
00175 FWTableViewManager::TableHandle
00176 FWTableViewManager::table(const char *name)
00177 {
00178    TableHandle handle(name, m_tableFormats);
00179    return handle;
00180 }
00181 
00190 FWTableViewManager::TableHandle &
00191 FWTableViewManager::TableHandle::column(const char *name, int precision, const char *expression)
00192 {
00193    TableEntry columnEntry;
00194    columnEntry.name = name;
00195    columnEntry.precision = precision;
00196    columnEntry.expression = expression;
00197    
00198    m_specs[m_name].push_back(columnEntry);
00199    return *this;
00200 }
00201 
00205 FWTableViewManager::TableSpecs::iterator 
00206 FWTableViewManager::tableFormatsImpl(const edm::TypeWithDict &key) 
00207 {
00208    TableSpecs::iterator ret = m_tableFormats.find(key.name());
00209    if (ret != m_tableFormats.end())
00210       return ret;
00211 
00212    // if there is no exact match for the type, try the base classes
00213    edm::TypeBases bases(key);
00214    for (auto const& base : bases)  
00215    {
00216       ret = tableFormatsImpl(edm::BaseWithDict(base).typeOf());
00217       if (ret != m_tableFormats.end()) 
00218          return ret;
00219    }
00220 
00221    return m_tableFormats.end();
00222 }
00223 
00237 FWTableViewManager::TableSpecs::iterator
00238 FWTableViewManager::tableFormats(const edm::TypeWithDict &key) 
00239 {
00240    static const std::string isint("int");
00241    static const std::string isbool("bool");
00242    static const std::string isdouble("double");
00243    static const std::string isfloat("float");
00244 
00245    std::string keyType = key.name();
00246 
00247    TableSpecs::iterator ret = m_tableFormats.find(keyType);
00248 
00249    if (ret != m_tableFormats.end())
00250       return ret;
00251    
00252    ret = tableFormatsImpl(key); // recursive search for base classes
00253 
00254    if (ret != m_tableFormats.end()) 
00255       return ret;
00256 
00257    TableHandle handle = table(keyType.c_str());
00258    edm::TypeFunctionMembers functionMembers(key);
00259    for (auto const& member : functionMembers)
00260    {
00261       edm::FunctionWithDict m(member);
00262       if (m.functionParameterSize())
00263          continue;
00264       if (!m.isPublic())
00265          continue;
00266       if (!m.isConst())
00267          continue;
00268       if (m.returnType().name() == isint)
00269          handle.column(m.name().c_str(), TableEntry::INT);
00270       else if (m.returnType().name() == isbool)
00271          handle.column(m.name().c_str(), TableEntry::BOOL);
00272       else if (m.returnType().name() == isdouble)
00273          handle.column(m.name().c_str(), 5);
00274       else if (m.returnType().name() == isfloat)
00275          handle.column(m.name().c_str(), 3);
00276    }
00277    edm::TypeDataMembers dataMembers(key);
00278    for (auto const& member : dataMembers)
00279    {
00280       edm::MemberWithDict m(member);
00281       if (!m.isPublic())
00282          continue;
00283       if (!m.isConst())
00284          continue;
00285       if (m.typeOf().name() == isint)
00286          handle.column(m.name().c_str(), TableEntry::INT);
00287       else if (m.typeOf().name() == isbool)
00288          handle.column(m.name().c_str(), TableEntry::BOOL);
00289       else if (m.typeOf().name() == isdouble)
00290          handle.column(m.name().c_str(), 5);
00291       else if (m.typeOf().name() == isfloat)
00292          handle.column(m.name().c_str(), 3);
00293    }
00294    return m_tableFormats.find(keyType);
00295 }
00296 
00302 FWTableViewManager::TableSpecs::iterator 
00303 FWTableViewManager::tableFormats(const TClass &key) 
00304 {
00305    return tableFormats(edm::TypeWithDict::byName(key.GetName()));
00306 }
00307 
00308 class FWViewBase*
00309 FWTableViewManager::buildView(TEveWindowSlot* iParent, const std::string& /*type*/)
00310 {
00311    TEveManager::TRedrawDisabler disableRedraw(gEve);
00312    boost::shared_ptr<FWTableView> view(new FWTableView(iParent, this));
00313    view->setBackgroundColor(colorManager().background());
00314    m_views.push_back(view);
00315    view->beingDestroyed_.connect(boost::bind(&FWTableViewManager::beingDestroyed,
00316                                              this, _1));
00317    return view.get();
00318 }
00319 
00320 void
00321 FWTableViewManager::beingDestroyed(const FWViewBase* iView)
00322 {
00323    for(Views::iterator it = m_views.begin(), itEnd = m_views.end();
00324        it != itEnd;
00325        ++it) 
00326    {
00327       if(it->get() == iView) 
00328       {
00329          m_views.erase(it);
00330          return;
00331       }
00332    }
00333 }
00334 
00335 void
00336 FWTableViewManager::newItem(const FWEventItem* iItem)
00337 {
00338    m_items.push_back(iItem);
00339    iItem->goingToBeDestroyed_.connect(boost::bind(&FWTableViewManager::destroyItem,
00340                                                   this, _1));
00341    notifyViews();
00342 }
00343 
00345 void
00346 FWTableViewManager::notifyViews(void)
00347 {
00348    for(size_t i = 0, e = m_views.size(); i != e; ++i)
00349    { 
00350       FWTableView *view = m_views[i].get();
00351       view->updateItems();
00352       view->dataChanged();
00353    } 
00354 }
00355 
00361 void 
00362 FWTableViewManager::destroyItem(const FWEventItem *iItem)
00363 {
00364    // remove the item from the list
00365    // FIXME: why doesn't it use erase?? Boh...
00366    for (size_t i = 0, e = m_items.size(); i != e; ++i)
00367    {
00368       if (m_items[i] != iItem)
00369          continue;
00370       m_items[i] = 0;
00371    }
00372 
00373    notifyViews();
00374 }
00375 
00380 void
00381 FWTableViewManager::removeAllItems(void)
00382 {
00383    m_items.clear();
00384    notifyViews();
00385 }
00386 
00387 void
00388 FWTableViewManager::modelChangesComing()
00389 {
00390    gEve->DisableRedraw();
00391 }
00392 
00393 void
00394 FWTableViewManager::modelChangesDone()
00395 {
00396    gEve->EnableRedraw();
00397    // tell the views to update their item lists
00398    // FIXME: doesn't this need to call updateItems as well
00399    // and hence notifyViews would be more appropriate?? Boh...
00400    dataChanged();
00401 }
00402 
00404 void
00405 FWTableViewManager::colorsChanged()
00406 {
00407    for(size_t i = 0, e = m_views.size(); i != e; ++i)
00408       m_views[i].get()->resetColors(colorManager());
00409 }
00410 
00411 void
00412 FWTableViewManager::dataChanged()
00413 {
00414    for(size_t i = 0, e = m_views.size(); i != e; ++i)
00415       m_views[i].get()->dataChanged();
00416 }
00417 
00418 FWTypeToRepresentations
00419 FWTableViewManager::supportedTypesAndRepresentations() const
00420 {
00421    FWTypeToRepresentations returnValue;
00422    return returnValue;
00423 }
00424 
00425 const std::string FWTableViewManager::kConfigTypeNames = "typeNames";
00426 
00427 void 
00428 FWTableViewManager::addTo (FWConfiguration &iTo) const
00429 {
00430    // if there are views, it's the job of the first view to store
00431    // the configuration (this is to avoid ordering problems in the
00432    // case of multiple views)
00433    if (!m_views.empty())
00434       return;
00435    // if there are no views, then it's up to us to store the column
00436    // formats.  This is done in addToImpl, which can be called by
00437    // FWTableView as well
00438    addToImpl(iTo);
00439 }
00440      
00441 void 
00442 FWTableViewManager::addToImpl(FWConfiguration &iTo) const
00443 {
00444    FWConfiguration typeNames(1);
00445    char prec[100];
00446 
00447    for (TableSpecs::const_iterator 
00448         iType = m_tableFormats.begin(),
00449         iType_end = m_tableFormats.end();
00450         iType != iType_end; ++iType) 
00451    {
00452       const std::string &typeName = iType->first;
00453       typeNames.addValue(typeName);
00454       FWConfiguration columns(1);
00455       const TableEntries &entries = iType->second;
00456       for (size_t ei = 0, ee = entries.size(); ei != ee; ++ei)
00457       {
00458          const TableEntry &entry = entries[ei];
00459          columns.addValue(entry.name);
00460          columns.addValue(entry.expression);
00461          columns.addValue((snprintf(prec, 100, "%d", entry.precision), prec));
00462       }
00463       iTo.addKeyValue(typeName, columns);
00464    }
00465    iTo.addKeyValue(kConfigTypeNames, typeNames);
00466 }
00467 
00468 void 
00469 FWTableViewManager::setFrom(const FWConfiguration &iFrom)
00470 {
00471    try
00472    {
00473       const FWConfiguration *typeNames = iFrom.valueForKey(kConfigTypeNames);
00474       if (typeNames == 0)
00475       {
00476          fwLog(fwlog::kWarning) << "no table column configuration stored, using defaults\n";
00477          return;
00478       }
00479             
00480       //NOTE: FWTableViewTableManagers hold pointers into m_tableFormats so if we
00481       // clear it those pointers would be invalid
00482       // instead we will just clear the lists and fill them with their new values
00483       //m_tableFormats.clear();
00484       for (FWConfiguration::StringValuesIt 
00485            iType = typeNames->stringValues()->begin(),
00486            iTypeEnd = typeNames->stringValues()->end(); 
00487            iType != iTypeEnd; ++iType) 
00488       {
00489          //std::cout << "reading type " << *iType << std::endl;
00490          const FWConfiguration *columns = iFrom.valueForKey(*iType);
00491          assert(columns != 0);
00492          TableHandle handle = table(iType->c_str());
00493          for (FWConfiguration::StringValuesIt 
00494               it = columns->stringValues()->begin(),
00495               itEnd = columns->stringValues()->end(); 
00496               it != itEnd; ++it) 
00497          {
00498             const std::string &name = *it++;
00499             const std::string &expr = *it++;
00500             int prec = atoi(it->c_str());
00501             handle.column(name.c_str(), prec, expr.c_str());
00502          }
00503       }
00504    } 
00505    catch (...) 
00506    {
00507       // No info about types in the configuration; this is not an
00508       // error, it merely means that the types are handled by the
00509       // first FWTableView.
00510    }
00511 }