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  } else if (iPurpose == "CSC-segments") {
69  addEntry("cscDetId().endcap()", 0, "ec");
70  addEntry("cscDetId().station()", 0, "st");
71  addEntry("cscDetId().ring()", 0, "rn");
72  } else if (iPurpose == "BTLclusters" || iPurpose == "BTLrechits") {
73  addEntry("energy()", 2, "E", "MeV");
74  addEntry("time()", 2, "T", "ns");
75  } else if (iPurpose == "ETLclusters" || iPurpose == "ETLrechits") {
76  addEntry("energy()", 3, "E", "MeV");
77  addEntry("time()", 2, "T", "ns");
78  } else if (iPurpose == "HGCal Trigger Cell" || iPurpose == "HGCal Trigger Cluster") {
79  addEntry("detId", 0);
80  } else if (iPurpose == "CaloParticle") {
81  addEntry("energy", 3);
82  addEntry("pdgId()", 3, "pdgId");
83  addEntry("simClusters().size()", 3, "SimClSize");
84  } else if (iPurpose == "Trackster" || iPurpose == "Trackster hits" || iPurpose == "Trackster layers") {
85  addEntry("raw_energy", 3, "E", "GeV");
86  addEntry("barycenter().Eta()", 3, "eta");
87  addEntry("barycenter().Phi()", 3, "phi");
88  } else if (iPurpose == "HGCal MultiCluster") {
89  addEntry("energy", 3);
90  } else {
91  // by the default add pt, et, or energy
92  bool x = addEntry("pt", 1);
93  if (!x)
94  x = addEntry("et", 1);
95  if (!x)
96  addEntry("energy", 1);
97  }
98 
99  if (addEntry("eta", 2))
100  addEntry("phi", 2);
101 }
102 
103 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit) {
104  using namespace boost::spirit::classic;
105 
107  reco::parser::Grammar grammar(tmpPtr, m_type);
108 
109  if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
110  using namespace fireworks::expression;
111 
112  //Backwards compatibility with old format
113  std::string temp = oldToNewFormat(iExpression);
114 
115  //now setup the parser
116  try {
117  if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
118  m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression : iTitle, iPrec));
119  m_titleWidth = TMath::Max(m_titleWidth, (int)m_entries.back().m_title.size());
120  return true;
121  }
122  } catch (const reco::parser::BaseException& e) {
123  // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
124  }
125  }
126  return false;
127 }
128 
129 //______________________________________________________________________________
130 
131 double FWItemValueGetter::valueFor(const void* iObject, int idx) const {
132  // std::cout << " value for " << idx << "size " << m_entries.size() <<std::endl;
133  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
134  return m_entries[idx].m_expr->value(o);
135 }
136 
137 UInt_t FWItemValueGetter::precision(int idx) const { return m_entries[idx].m_precision; }
138 
139 std::vector<std::string> FWItemValueGetter::getTitles() const {
140  std::vector<std::string> titles;
141  titles.reserve(m_entries.size());
142 
143  for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i)
144  titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title);
145 
146  return titles;
147 }
148 
149 int FWItemValueGetter::numValues() const { return static_cast<int>(m_entries.size()); }
150 //______________________________________________________________________________
151 
152 const std::string& FWItemValueGetter::getToolTip(const void* iObject) const {
153  static std::string buff(128, 0);
154  static std::string fs = "\n %*s = %.*f";
155 
156  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
157 
158  int off = 0;
159  for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i) {
160  const Entry& e = *i;
161  off += snprintf(&buff[off],
162  127,
163  fs.c_str(),
164  m_titleWidth,
165  e.m_title.c_str(),
166  e.m_precision ? (e.m_precision + 1) : 0,
167  e.m_expr->value(o));
168  }
169 
170  // std::cout << buff;
171  return buff;
172 }
static const std::array< std::string, 5 > titles
vector< string > parse(string line, const string &delimiter)
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)
UInt_t precision(int idx) const
std::vector< std::string > getTitles() const
const std::string & getToolTip(const void *iObject) const
edm::TypeWithDict m_type
std::string name() const
std::string oldToNewFormat(const std::string &iExpression)
double valueFor(const void *, int idx) const
buff
***.cc ################