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 if ( iPurpose == "DT-segments")
79  {
80  addEntry("chamberId().wheel()", 0, "wh");
81  addEntry("chamberId().station()", 0, "st");
82  addEntry("chamberId().sector()", 0, "sc");
83 
84  }
85  else if ( iPurpose == "CSC-segments")
86  {
87  addEntry("cscDetId().endcap()", 0, "ec");
88  addEntry("cscDetId().station()", 0, "st");
89  addEntry("cscDetId().ring()", 0, "rn");
90  }
91  else {
92  // by the default add pt, et, or energy
93  bool x = addEntry("pt", 1);
94  if (!x) x = addEntry("et", 1);
95  if (!x) addEntry("energy", 1);
96  }
97 
98  if (addEntry("eta", 2))
99  addEntry("phi", 2);
100 }
101 
102 
103 
104 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit)
105 {
106  using namespace boost::spirit::classic;
107 
109  reco::parser::Grammar grammar(tmpPtr, m_type);
110 
111  if(m_type != edm::TypeWithDict() && iExpression.size())
112  {
113  using namespace fireworks::expression;
114 
115  //Backwards compatibility with old format
116  std::string temp = oldToNewFormat(iExpression);
117 
118  //now setup the parser
119  try
120  {
121  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full)
122  {
123  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression :iTitle , iPrec));
124  m_titleWidth = TMath::Max(m_titleWidth, (int) m_entries.back().m_title.size());
125  return true;
126  }
127  }
128  catch(const reco::parser::BaseException& e)
129  {
130  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
131  }
132  }
133  return false;
134 }
135 
136 
137 //______________________________________________________________________________
138 
139 double
140 FWItemValueGetter::valueFor(const void* iObject, int idx) const
141 {
142  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
143  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
144  return m_entries[idx].m_expr->value(o);
145 }
146 
147 UInt_t
149 {
150  return m_entries[idx].m_precision;
151 }
152 
153 std::vector<std::string>
155 {
156  std::vector<std::string> titles;
157  titles.reserve(m_entries.size());
158 
159  for (std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i)
160  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title );
161 
162  return titles;
163 }
164 
165 int
167 {
168  return static_cast<int>(m_entries.size());
169 }
170 //______________________________________________________________________________
171 
172 const std::string&
173 FWItemValueGetter::getToolTip(const void* iObject) const
174 {
175  static std::string buff(128, 0);
176  static std::string fs = "\n %*s = %.*f";
177 
178  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
179 
180  int off = 0;
181  for ( std::vector<Entry >::const_iterator i = m_entries.begin() ; i != m_entries.end(); ++i) {
182  const Entry& e = *i;
183  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));
184  }
185 
186  // std::cout << buff;
187  return buff;
188 }
189 
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
T Max(T a, T b)
Definition: MathUtil.h:44
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
UInt_t precision(int idx) const