CMS 3D CMS Logo

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