CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/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.11 2012/12/02 09:49:59 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <sstream>
00016 #include <cstdio>
00017 #include "TMath.h"
00018 #include "Reflex/Base.h"
00019 #include "Reflex/Base.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 Reflex::Type& iType, const std::string& iPurpose):
00038    m_type(iType),
00039    m_titleWidth(0)
00040 {
00041    if ( iType.Name() == "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(), "Vertices") )
00057    {
00058       addEntry("x", 2, "x", "cm");
00059       addEntry("y", 2, "y", "cm");
00060       addEntry("z", 2, "z", "cm");
00061    }
00062    else if (strstr(iPurpose.c_str(), "Conversion") )
00063    {
00064       addEntry("pairMomentum().rho()", 1, "pt", "GeV" );
00065       addEntry("pairMomentum().eta()", 2, "eta");
00066       addEntry("pairMomentum().phi()", 2, "phi");
00067    }
00068    else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle"))
00069    {
00070       addEntry("pdgId()", 0, "pdg");
00071       bool x = addEntry("pt", 1);
00072       if (!x) x = addEntry("et", 1);
00073       if (!x) addEntry("energy", 1);
00074    }
00075    else if (iPurpose == "Jets" )
00076    {
00077       addEntry("et", 1);
00078    }
00079    else {
00080       // by the default  add pt, et, or energy
00081       bool x = addEntry("pt", 1);
00082       if (!x) x = addEntry("et", 1);
00083       if (!x) addEntry("energy", 1);
00084    }
00085 
00086    if (addEntry("eta", 2))
00087       addEntry("phi",  2);
00088 }
00089 
00090 
00091 
00092 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
00093 {
00094    using namespace boost::spirit::classic;
00095 
00096    reco::parser::ExpressionPtr tmpPtr;
00097    reco::parser::Grammar grammar(tmpPtr, m_type);
00098 
00099    if(m_type != Reflex::Type() && iExpression.size()) 
00100    {
00101       using namespace fireworks::expression;
00102 
00103       //Backwards compatibility with old format
00104       std::string temp = oldToNewFormat(iExpression);
00105 
00106       //now setup the parser
00107       try 
00108       {
00109          if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) 
00110          {
00111             m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
00112             m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
00113             return true;
00114          }
00115       } 
00116       catch(const reco::parser::BaseException& e)
00117       {
00118          // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
00119       }
00120    }
00121    return false;
00122 }
00123 
00124 
00125 //______________________________________________________________________________
00126 
00127 double
00128 FWItemValueGetter::valueFor(const void* iObject, int idx) const
00129 {
00130    //  std::cout << " value for " << idx << "size " <<  m_entries.size() <<std::endl;
00131    Reflex::Object o(m_type, const_cast<void *>(iObject));
00132    return m_entries[idx].m_expr->value(o);
00133 }
00134 
00135 UInt_t
00136 FWItemValueGetter::precision(int idx) const
00137 {
00138    return m_entries[idx].m_precision;
00139 }
00140 
00141 std::vector<std::string> 
00142 FWItemValueGetter::getTitles() const
00143 {
00144    std::vector<std::string> titles;
00145    titles.reserve(m_entries.size());
00146 
00147    for (std::vector<Entry >::const_iterator i  = m_entries.begin() ; i != m_entries.end(); ++i) 
00148       titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
00149 
00150    return titles;
00151 }
00152 
00153 int 
00154 FWItemValueGetter::numValues() const
00155 {
00156    return static_cast<int>(m_entries.size());
00157 }
00158 //______________________________________________________________________________
00159 
00160 const std::string& 
00161 FWItemValueGetter::getToolTip(const void* iObject) const
00162 {
00163    static std::string buff(128, 0);
00164    static std::string fs = "\n %*s = %.*f";
00165 
00166    Reflex::Object o(m_type, const_cast<void *>(iObject));
00167 
00168    int off = 0;
00169    for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
00170       const Entry& e = *i;
00171       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));
00172    }
00173 
00174    // std::cout << buff;
00175    return buff;
00176 }
00177