CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWItemValueGetter
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Sun Nov 30 16:15:43 EST 2008
00011 //
00012 
00013 // system include files
00014 #include <sstream>
00015 #include <cstdio>
00016 #include "TMath.h"
00017 #include "FWCore/Utilities/interface/BaseWithDict.h"
00018 #include "FWCore/Utilities/interface/ObjectWithDict.h"
00019 
00020 // user include files
00021 #include "Fireworks/Core/interface/FWItemValueGetter.h"
00022 
00023 #include "Fireworks/Core/interface/FWExpressionEvaluator.h"
00024 #include "Fireworks/Core/interface/FWExpressionException.h"
00025 #include "CommonTools/Utils/src/Grammar.h"
00026 #include "CommonTools/Utils/interface/Exception.h"
00027 
00028 #include "Fireworks/Core/src/expressionFormatHelpers.h"
00029 
00030 
00031 //==============================================================================
00032 //==============================================================================
00033 //==============================================================================
00034 
00035 
00036 FWItemValueGetter::FWItemValueGetter(const edm::TypeWithDict& iType, const std::string& iPurpose):
00037    m_type(iType),
00038    m_titleWidth(0)
00039 {
00040    if (!strcmp(iType.name().c_str(), "CaloTower"))
00041    {
00042       if ( iPurpose == "ECal" )
00043          addEntry("emEt", 1, "et", "GeV");
00044       else if ( iPurpose == "HCal" )
00045          addEntry("hadEt", 1, "et", "GeV");
00046       else if (iPurpose == "HCal Outer")
00047          addEntry("outerEt", 1, "et", "GeV");
00048    }
00049    else if (strstr(iPurpose.c_str(), "Beam Spot") )
00050    {
00051       addEntry("x0", 2, "x", "cm");
00052       addEntry("y0", 2, "y", "cm");
00053       addEntry("z0", 2, "z", "cm");
00054    }
00055    else if (strstr(iPurpose.c_str(), "Vertices") )
00056    {
00057       addEntry("x", 2, "x", "cm");
00058       addEntry("y", 2, "y", "cm");
00059       addEntry("z", 2, "z", "cm");
00060    }
00061    else if (strstr(iPurpose.c_str(), "Conversion") )
00062    {
00063       addEntry("pairMomentum().rho()", 1, "pt", "GeV" );
00064       addEntry("pairMomentum().eta()", 2, "eta");
00065       addEntry("pairMomentum().phi()", 2, "phi");
00066    }
00067    else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle"))
00068    {
00069       addEntry("pdgId()", 0, "pdg");
00070       bool x = addEntry("pt", 1);
00071       if (!x) x = addEntry("et", 1);
00072       if (!x) addEntry("energy", 1);
00073    }
00074    else if (iPurpose == "Jets" )
00075    {
00076       addEntry("et", 1);
00077    }
00078    else {
00079       // by the default  add pt, et, or energy
00080       bool x = addEntry("pt", 1);
00081       if (!x) x = addEntry("et", 1);
00082       if (!x) addEntry("energy", 1);
00083    }
00084 
00085    if (addEntry("eta", 2))
00086       addEntry("phi",  2);
00087 }
00088 
00089 
00090 
00091 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
00092 {
00093    using namespace boost::spirit::classic;
00094 
00095    reco::parser::ExpressionPtr tmpPtr;
00096    reco::parser::Grammar grammar(tmpPtr, m_type);
00097 
00098    if(m_type != edm::TypeWithDict() && iExpression.size()) 
00099    {
00100       using namespace fireworks::expression;
00101 
00102       //Backwards compatibility with old format
00103       std::string temp = oldToNewFormat(iExpression);
00104 
00105       //now setup the parser
00106       try 
00107       {
00108          if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) 
00109          {
00110             m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
00111             m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
00112             return true;
00113          }
00114       } 
00115       catch(const reco::parser::BaseException& e)
00116       {
00117          // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
00118       }
00119    }
00120    return false;
00121 }
00122 
00123 
00124 //______________________________________________________________________________
00125 
00126 double
00127 FWItemValueGetter::valueFor(const void* iObject, int idx) const
00128 {
00129    //  std::cout << " value for " << idx << "size " <<  m_entries.size() <<std::endl;
00130    edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
00131    return m_entries[idx].m_expr->value(o);
00132 }
00133 
00134 UInt_t
00135 FWItemValueGetter::precision(int idx) const
00136 {
00137    return m_entries[idx].m_precision;
00138 }
00139 
00140 std::vector<std::string> 
00141 FWItemValueGetter::getTitles() const
00142 {
00143    std::vector<std::string> titles;
00144    titles.reserve(m_entries.size());
00145 
00146    for (std::vector<Entry >::const_iterator i  = m_entries.begin() ; i != m_entries.end(); ++i) 
00147       titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
00148 
00149    return titles;
00150 }
00151 
00152 int 
00153 FWItemValueGetter::numValues() const
00154 {
00155    return static_cast<int>(m_entries.size());
00156 }
00157 //______________________________________________________________________________
00158 
00159 const std::string& 
00160 FWItemValueGetter::getToolTip(const void* iObject) const
00161 {
00162    static std::string buff(128, 0);
00163    static std::string fs = "\n %*s = %.*f";
00164 
00165    edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
00166 
00167    int off = 0;
00168    for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
00169       const Entry& e = *i;
00170       off += snprintf(&buff[off], 127, fs.c_str(), m_titleWidth, e.m_title.c_str(),  e.m_precision ? (e.m_precision+1) : 0,  e.m_expr->value(o));
00171    }
00172 
00173    // std::cout << buff;
00174    return buff;
00175 }
00176