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 //
12 
13 // system include files
14 #include <sstream>
15 #include <cstdio>
16 #include "TMath.h"
19 
20 // user include files
22 
27 
29 
30 
31 //==============================================================================
32 //==============================================================================
33 //==============================================================================
34 
35 
37  m_type(iType),
38  m_titleWidth(0)
39 {
40  if (!strcmp(iType.name().c_str(), "CaloTower"))
41  {
42  if ( iPurpose == "ECal" )
43  addEntry("emEt", 1, "et", "GeV");
44  else if ( iPurpose == "HCal" )
45  addEntry("hadEt", 1, "et", "GeV");
46  else if (iPurpose == "HCal Outer")
47  addEntry("outerEt", 1, "et", "GeV");
48  }
49  else if (strstr(iPurpose.c_str(), "Beam Spot") )
50  {
51  addEntry("x0", 2, "x", "cm");
52  addEntry("y0", 2, "y", "cm");
53  addEntry("z0", 2, "z", "cm");
54  }
55  else if (strstr(iPurpose.c_str(), "Vertices") )
56  {
57  addEntry("x", 2, "x", "cm");
58  addEntry("y", 2, "y", "cm");
59  addEntry("z", 2, "z", "cm");
60  }
61  else if (strstr(iPurpose.c_str(), "Conversion") )
62  {
63  addEntry("pairMomentum().rho()", 1, "pt", "GeV" );
64  addEntry("pairMomentum().eta()", 2, "eta");
65  addEntry("pairMomentum().phi()", 2, "phi");
66  }
67  else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle"))
68  {
69  addEntry("pdgId()", 0, "pdg");
70  bool x = addEntry("pt", 1);
71  if (!x) x = addEntry("et", 1);
72  if (!x) addEntry("energy", 1);
73  }
74  else if (iPurpose == "Jets" )
75  {
76  addEntry("et", 1);
77  }
78  else {
79  // by the default add pt, et, or energy
80  bool x = addEntry("pt", 1);
81  if (!x) x = addEntry("et", 1);
82  if (!x) addEntry("energy", 1);
83  }
84 
85  if (addEntry("eta", 2))
86  addEntry("phi", 2);
87 }
88 
89 
90 
91 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
92 {
93  using namespace boost::spirit::classic;
94 
96  reco::parser::Grammar grammar(tmpPtr, m_type);
97 
98  if(m_type != edm::TypeWithDict() && iExpression.size())
99  {
100  using namespace fireworks::expression;
101 
102  //Backwards compatibility with old format
103  std::string temp = oldToNewFormat(iExpression);
104 
105  //now setup the parser
106  try
107  {
108  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full)
109  {
110  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
111  m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
112  return true;
113  }
114  }
115  catch(const reco::parser::BaseException& e)
116  {
117  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
118  }
119  }
120  return false;
121 }
122 
123 
124 //______________________________________________________________________________
125 
126 double
127 FWItemValueGetter::valueFor(const void* iObject, int idx) const
128 {
129  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
130  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
131  return m_entries[idx].m_expr->value(o);
132 }
133 
134 UInt_t
136 {
137  return m_entries[idx].m_precision;
138 }
139 
140 std::vector<std::string>
142 {
143  std::vector<std::string> titles;
144  titles.reserve(m_entries.size());
145 
146  for (std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i)
147  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
148 
149  return titles;
150 }
151 
152 int
154 {
155  return static_cast<int>(m_entries.size());
156 }
157 //______________________________________________________________________________
158 
159 const std::string&
160 FWItemValueGetter::getToolTip(const void* iObject) const
161 {
162  static std::string buff(128, 0);
163  static std::string fs = "\n %*s = %.*f";
164 
165  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
166 
167  int off = 0;
168  for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
169  const Entry& e = *i;
170  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));
171  }
172 
173  // std::cout << buff;
174  return buff;
175 }
176 
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