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"
20 
21 // user include files
23 
28 
30 
31 
32 //==============================================================================
33 //==============================================================================
34 //==============================================================================
35 
36 
38  m_type(iType),
39  m_titleWidth(0)
40 {
41  if (!strcmp(iType.name().c_str(), "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(), "Conversion") )
57  {
58  addEntry("pairMomentum().rho()", 1, "pt", "GeV" );
59  addEntry("pairMomentum().eta()", 2, "eta");
60  addEntry("pairMomentum().phi()", 2, "phi");
61  }
62  else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle"))
63  {
64  addEntry("pdgId()", 0, "pdg");
65  bool x = addEntry("pt", 1);
66  if (!x) x = addEntry("et", 1);
67  if (!x) addEntry("energy", 1);
68  }
69  else if (iPurpose == "Jets" )
70  {
71  addEntry("et", 1);
72  }
73  else {
74  // by the default add pt, et, or energy
75  bool x = addEntry("pt", 1);
76  if (!x) x = addEntry("et", 1);
77  if (!x) addEntry("energy", 1);
78  }
79 
80  if (addEntry("eta", 2))
81  addEntry("phi", 2);
82 }
83 
84 
85 
86 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
87 {
88  using namespace boost::spirit::classic;
89 
91  reco::parser::Grammar grammar(tmpPtr, m_type);
92 
93  if(m_type != edm::TypeWithDict() && iExpression.size())
94  {
95  using namespace fireworks::expression;
96 
97  //Backwards compatibility with old format
98  std::string temp = oldToNewFormat(iExpression);
99 
100  //now setup the parser
101  try
102  {
103  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full)
104  {
105  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
106  m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
107  return true;
108  }
109  }
110  catch(const reco::parser::BaseException& e)
111  {
112  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
113  }
114  }
115  return false;
116 }
117 
118 
119 //______________________________________________________________________________
120 
121 double
122 FWItemValueGetter::valueFor(const void* iObject, int idx) const
123 {
124  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
125  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
126  return m_entries[idx].m_expr->value(o);
127 }
128 
129 UInt_t
131 {
132  return m_entries[idx].m_precision;
133 }
134 
135 std::vector<std::string>
137 {
138  std::vector<std::string> titles;
139  titles.reserve(m_entries.size());
140 
141  for (std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i)
142  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
143 
144  return titles;
145 }
146 
147 int
149 {
150  return static_cast<int>(m_entries.size());
151 }
152 //______________________________________________________________________________
153 
154 const std::string&
155 FWItemValueGetter::getToolTip(const void* iObject) const
156 {
157  static std::string buff(128, 0);
158  static std::string fs = "\n %*s = %.*f";
159 
160  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
161 
162  int off = 0;
163  for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
164  const Entry& e = *i;
165  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));
166  }
167 
168  // std::cout << buff;
169  return buff;
170 }
171 
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="")
FWItemValueGetter(const edm::TypeWithDict &, const std::string &iPurpose)
boost::shared_ptr< ExpressionBase > ExpressionPtr
std::string name() const
edm::TypeWithDict m_type
unsigned int UInt_t
Definition: FUTypes.h:12
std::vector< std::string > getTitles() const
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
double valueFor(const void *, int idx) const
size_t size() 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