CMS 3D CMS Logo

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