CMS 3D CMS Logo

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 
35  : m_type(iType), m_titleWidth(0) {
36  if (!strcmp(iType.name().c_str(), "CaloTower")) {
37  if (iPurpose == "ECal")
38  addEntry("emEt", 1, "et", "GeV");
39  else if (iPurpose == "HCal")
40  addEntry("hadEt", 1, "et", "GeV");
41  else if (iPurpose == "HCal Outer")
42  addEntry("outerEt", 1, "et", "GeV");
43  } else if (strstr(iPurpose.c_str(), "Beam Spot")) {
44  addEntry("x0", 2, "x", "cm");
45  addEntry("y0", 2, "y", "cm");
46  addEntry("z0", 2, "z", "cm");
47  } else if (strstr(iPurpose.c_str(), "Vertices")) {
48  addEntry("x", 2, "x", "cm");
49  addEntry("y", 2, "y", "cm");
50  addEntry("z", 2, "z", "cm");
51  } else if (strstr(iPurpose.c_str(), "Conversion")) {
52  addEntry("pairMomentum().rho()", 1, "pt", "GeV");
53  addEntry("pairMomentum().eta()", 2, "eta");
54  addEntry("pairMomentum().phi()", 2, "phi");
55  } else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle")) {
56  addEntry("pdgId()", 0, "pdg");
57  bool x = addEntry("pt", 1);
58  if (!x)
59  x = addEntry("et", 1);
60  if (!x)
61  addEntry("energy", 1);
62  } else if (iPurpose == "Jets") {
63  addEntry("et", 1);
64  } else if (iPurpose == "DT-segments") {
65  addEntry("chamberId().wheel()", 0, "wh");
66  addEntry("chamberId().station()", 0, "st");
67  addEntry("chamberId().sector()", 0, "sc");
68 
69  } else if (iPurpose == "CSC-segments") {
70  addEntry("cscDetId().endcap()", 0, "ec");
71  addEntry("cscDetId().station()", 0, "st");
72  addEntry("cscDetId().ring()", 0, "rn");
73  } else if (iPurpose == "HGCal Trigger Cell" || iPurpose == "HGCal Trigger Cluster") {
74  addEntry("detId", 0);
75  } else if (iPurpose == "CaloParticle") {
76  addEntry("energy", 3);
77  addEntry("pdgId()", 3, "pdgId");
78  addEntry("simClusters().size()", 3, "SimClSize");
79  } else if (iPurpose == "HGCal MultiCluster") {
80  addEntry("energy", 3);
81  } else {
82  // by the default add pt, et, or energy
83  bool x = addEntry("pt", 1);
84  if (!x)
85  x = addEntry("et", 1);
86  if (!x)
87  addEntry("energy", 1);
88  }
89 
90  if (addEntry("eta", 2))
91  addEntry("phi", 2);
92 }
93 
94 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit) {
95  using namespace boost::spirit::classic;
96 
98  reco::parser::Grammar grammar(tmpPtr, m_type);
99 
100  if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
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  if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
109  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression : iTitle, iPrec));
110  m_titleWidth = TMath::Max(m_titleWidth, (int)m_entries.back().m_title.size());
111  return true;
112  }
113  } catch (const reco::parser::BaseException& e) {
114  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
115  }
116  }
117  return false;
118 }
119 
120 //______________________________________________________________________________
121 
122 double FWItemValueGetter::valueFor(const void* iObject, int idx) const {
123  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
124  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
125  return m_entries[idx].m_expr->value(o);
126 }
127 
128 UInt_t FWItemValueGetter::precision(int idx) const { return m_entries[idx].m_precision; }
129 
130 std::vector<std::string> FWItemValueGetter::getTitles() const {
131  std::vector<std::string> titles;
132  titles.reserve(m_entries.size());
133 
134  for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i)
135  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title);
136 
137  return titles;
138 }
139 
140 int FWItemValueGetter::numValues() const { return static_cast<int>(m_entries.size()); }
141 //______________________________________________________________________________
142 
143 const std::string& FWItemValueGetter::getToolTip(const void* iObject) const {
144  static std::string buff(128, 0);
145  static std::string fs = "\n %*s = %.*f";
146 
147  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
148 
149  int off = 0;
150  for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i) {
151  const Entry& e = *i;
152  off += snprintf(&buff[off],
153  127,
154  fs.c_str(),
155  m_titleWidth,
156  e.m_title.c_str(),
157  e.m_precision ? (e.m_precision + 1) : 0,
158  e.m_expr->value(o));
159  }
160 
161  // std::cout << buff;
162  return buff;
163 }
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:33
bool addEntry(std::string iExpression, int iPrec=2, std::string iTitle="", std::string iUnit="")
std::shared_ptr< ExpressionBase > ExpressionPtr
FWItemValueGetter(const edm::TypeWithDict &, const std::string &iPurpose)
std::string name() const
edm::TypeWithDict m_type
T Max(T a, T b)
Definition: MathUtil.h:44
def parse(path, config)
Definition: dumpparser.py:13
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
UInt_t precision(int idx) const