CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWItemValueGetter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWItemValueGetter
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Nov 30 16:15:43 EST 2008
11 // $Id: FWItemValueGetter.cc,v 1.11 2012/12/02 09:49:59 amraktad Exp $
12 //
13 
14 // system include files
15 #include <sstream>
16 #include <cstdio>
17 #include "TMath.h"
18 #include "Reflex/Base.h"
19 #include "Reflex/Base.h"
20 
21 // user include files
23 
28 
30 
31 
32 //==============================================================================
33 //==============================================================================
34 //==============================================================================
35 
36 
37 FWItemValueGetter::FWItemValueGetter(const Reflex::Type& iType, const std::string& iPurpose):
38  m_type(iType),
39  m_titleWidth(0)
40 {
41  if ( iType.Name() == "CaloTower" )
42  {
43  if ( iPurpose == "ECal" )
44  addEntry("emEt", 1, "et", "GeV");
45  else if ( iPurpose == "HCal" )
46  addEntry("hadEt", 1, "et", "GeV");
47  else if (iPurpose == "HCal Outer")
48  addEntry("outerEt", 1, "et", "GeV");
49  }
50  else if (strstr(iPurpose.c_str(), "Beam Spot") )
51  {
52  addEntry("x0", 2, "x", "cm");
53  addEntry("y0", 2, "y", "cm");
54  addEntry("z0", 2, "z", "cm");
55  }
56  else if (strstr(iPurpose.c_str(), "Vertices") )
57  {
58  addEntry("x", 2, "x", "cm");
59  addEntry("y", 2, "y", "cm");
60  addEntry("z", 2, "z", "cm");
61  }
62  else if (strstr(iPurpose.c_str(), "Conversion") )
63  {
64  addEntry("pairMomentum().rho()", 1, "pt", "GeV" );
65  addEntry("pairMomentum().eta()", 2, "eta");
66  addEntry("pairMomentum().phi()", 2, "phi");
67  }
68  else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle"))
69  {
70  addEntry("pdgId()", 0, "pdg");
71  bool x = addEntry("pt", 1);
72  if (!x) x = addEntry("et", 1);
73  if (!x) addEntry("energy", 1);
74  }
75  else if (iPurpose == "Jets" )
76  {
77  addEntry("et", 1);
78  }
79  else {
80  // by the default add pt, et, or energy
81  bool x = addEntry("pt", 1);
82  if (!x) x = addEntry("et", 1);
83  if (!x) addEntry("energy", 1);
84  }
85 
86  if (addEntry("eta", 2))
87  addEntry("phi", 2);
88 }
89 
90 
91 
92 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
93 {
94  using namespace boost::spirit::classic;
95 
97  reco::parser::Grammar grammar(tmpPtr, m_type);
98 
99  if(m_type != Reflex::Type() && iExpression.size())
100  {
101  using namespace fireworks::expression;
102 
103  //Backwards compatibility with old format
104  std::string temp = oldToNewFormat(iExpression);
105 
106  //now setup the parser
107  try
108  {
109  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full)
110  {
111  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
112  m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
113  return true;
114  }
115  }
116  catch(const reco::parser::BaseException& e)
117  {
118  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
119  }
120  }
121  return false;
122 }
123 
124 
125 //______________________________________________________________________________
126 
127 double
128 FWItemValueGetter::valueFor(const void* iObject, int idx) const
129 {
130  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
131  Reflex::Object o(m_type, const_cast<void *>(iObject));
132  return m_entries[idx].m_expr->value(o);
133 }
134 
135 UInt_t
137 {
138  return m_entries[idx].m_precision;
139 }
140 
141 std::vector<std::string>
143 {
144  std::vector<std::string> titles;
145  titles.reserve(m_entries.size());
146 
147  for (std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i)
148  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
149 
150  return titles;
151 }
152 
153 int
155 {
156  return static_cast<int>(m_entries.size());
157 }
158 //______________________________________________________________________________
159 
160 const std::string&
161 FWItemValueGetter::getToolTip(const void* iObject) const
162 {
163  static std::string buff(128, 0);
164  static std::string fs = "\n %*s = %.*f";
165 
166  Reflex::Object o(m_type, const_cast<void *>(iObject));
167 
168  int off = 0;
169  for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
170  const Entry& e = *i;
171  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));
172  }
173 
174  // std::cout << buff;
175  return buff;
176 }
177 
int i
Definition: DBlmapReader.cc:9
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:37
Evaluator * parse(const T &text)
bool addEntry(std::string iExpression, int iPrec=2, std::string iTitle="", std::string iUnit="")
boost::shared_ptr< ExpressionBase > ExpressionPtr
FWItemValueGetter(const Reflex::Type &, const std::string &iPurpose)
unsigned int UInt_t
Definition: FUTypes.h:12
std::vector< std::string > getTitles() const
double valueFor(const void *, int idx) const
std::string oldToNewFormat(const std::string &iExpression)
const std::string & getToolTip(const void *iObject) const
reco::parser::ExpressionPtr m_expr
Definition: DDAxes.h:10
UInt_t precision(int idx) const