CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ScannerHelpers.cc
Go to the documentation of this file.
2 
6 #include <iostream>
7 
11 
12  using namespace boost::spirit::classic;
13  reco::parser::Grammar grammar(ret, type, true);
14  const char* startingFrom = expr.c_str();
15  try {
16  parse(startingFrom, grammar.use_parser<1>() >> end_p, space_p).full;
18  std::cerr << "Expression parser error:"<<reco::parser::baseExceptionWhat(e)<<" (char "<<e.where-startingFrom<<")" << std::endl;
19  }
20  return ret;
21 }
22 
26 
27  using namespace boost::spirit::classic;
28  reco::parser::Grammar grammar(ret, type, true);
29  const char* startingFrom = expr.c_str();
30  try {
31  parse(startingFrom, grammar.use_parser<0>() >> end_p, space_p).full;
33  std::cerr << "Selector parser error:"<<reco::parser::baseExceptionWhat(e)<<" (char "<<e.where-startingFrom<<")" << std::endl;
34  }
35  return ret;
36 }
37 
41  // now search for value_type
42  edm::TypeWithDict objtype = collection.nestedType("value_type");
43  if(bool(objtype)) {
44  return objtype;
45  }
46  std::cerr << "Can't get a type out of " << wrapperType.name() << std::endl;
47  return edm::TypeWithDict();
48 }
49 
50 bool
52  if (sel.get() == 0) return false;edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
53  return (*sel)(obj);
54 }
55 
56 double
58  if (expr.get() == 0) return 0;
59  edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
60  return expr->value(obj);
61 }
62 
63 bool
65  bool ok = true;
66  exprs_.push_back(helper::Parser::makeExpression(expr,objType_));
67  if (exprs_.back().get() == 0) {
68  std::cerr << "Failed to parse expression " << expr << std::endl;
69  exprs_.pop_back();
70  ok = false;
71  }
72  return ok;
73 }
74 
75 bool
77  bool ok = true;
78  cuts_[0] = helper::Parser::makeSelector(cut,objType_);
79  if (strlen(cut) && !cuts_[0].get()) {
80  std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
81  ok = false;
82  }
83  return ok;
84 }
85 
86 void
88  cuts_[0].reset();
89 }
90 
91 void
93  cuts_.resize(1);
94 }
95 
96 
97 bool
99  bool ok = true;
100  cuts_.push_back(helper::Parser::makeSelector(cut,objType_));
101  if (!cuts_.back().get()) {
102  std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
103  ok = false;
104  cuts_.pop_back();
105  }
106  return ok;
107 }
108 
109 
110 bool
111 helper::ScannerBase::test(const void *ptr, size_t icut) const {
112  if (icut >= cuts_.size()) return false;
113  if (cuts_[icut].get() == 0) return true;
114  try {
115  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
116  return (*cuts_[icut])(obj);
117  } catch (std::exception &ex) {
118  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
119  return false;
120  }
121 }
122 
123 double
124 helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
125  try {
126  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
127  if (exprs_.size() > iexpr) return exprs_[iexpr]->value(obj);
128  } catch (std::exception &ex) {
129  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
130  }
131  return 0;
132 }
133 
134 void
135 helper::ScannerBase::print(const void *ptr) const {
136  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
137  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
138  for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed; ++it) {
139  if (ptr == 0 || it->get() == 0) {
140  printf(" : %8s", "#ERR");
141  } else {
142  try {
143  double val = (*it)->value(obj);
144  // I found no easy ways to enforce a fixed width from printf that works also with leading zeroes or large exponents (e.g. 1e15 or 1e101)
145  // So we have to go the ugly way
146  char buff[255];
147  int len = sprintf(buff," : % 8.6g",val); // this is usually ok, and should be 3+8 chars long
148  if (len == 3+8) {
149  std::cout << buff;
150  } else {
151  if (strchr(buff,'e')) {
152  printf((len == 3+13 ? " : % .0e" : " : % .1e"),val);
153  } else {
154  printf("%11.11s",buff);
155  }
156  }
157  } catch (std::exception &ex) {
158  printf(" : %8s", "EXCEPT");
159  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
160  }
161  }
162  }
163  for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin()+1, ed = cuts_.end(); it != ed; ++it) {
164  if (ptr == 0 || it->get() == 0) {
165  printf(" : %8s", "#ERR");
166  } else {
167  try {
168  int ret = (*it)->operator()(obj);
169  printf(" : %8d", ret);
170  } catch (std::exception &ex) {
171  printf(" : %8s", "EXCEPT");
172  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
173  }
174  }
175  }
176  fflush(stdout);
177  }
178 }
179 
180 
181 void
182 helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
183  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
184  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
185  try {
186  if (!exprs_.empty()) hist->Fill(exprs_[0]->value(obj));
187  } catch (std::exception &ex) {
188  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
189  }
190  }
191 }
192 
193 void
194 helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
195  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
196  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
197  try {
198  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
199  } catch (std::exception &ex) {
200  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
201  }
202  }
203 }
204 
205 void
206 helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
207  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
208  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
209  try {
210  if (exprs_.size() >= 2) graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
211  } catch (std::exception &ex) {
212  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
213  }
214  }
215 }
216 
217 
218 void
219 helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
220  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
221  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
222  try {
223  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
224  } catch (std::exception &ex) {
225  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
226  }
227  }
228 }
void print(const void *obj) const
type
Definition: HCALResponse.h:21
void fill2D(const void *obj, TH2 *hist2d) const
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:37
Evaluator * parse(const T &text)
void clearExtraCuts()
Clear all extra cuts ;.
boost::shared_ptr< ExpressionBase > ExpressionPtr
static bool test(const reco::parser::SelectorPtr &sel, const edm::TypeWithDict type, const void *obj)
Make a edm::ObjectWithDict(type, obj) and pass it to the selector.
bool addExtraCut(const char *cut)
Add one extra cut that can be evaluated separately (as if it was an expression)
TypeWithDict templateArgumentAt(size_t index) const
TypeWithDict nestedType(char const *name) const
std::string name() const
bool test(const void *obj, size_t icut=0) const
double eval(const void *obj, size_t iexpr=0) const
void fill1D(const void *obj, TH1 *hist) const
void clearCut()
Clear the default cut.
const char * baseExceptionWhat(const BaseException &e)
returns the appropriate &#39;what&#39; message for the exception
Definition: Exception.h:40
bool addExpression(const char *expr)
Definition: adjgraph.h:12
boost::shared_ptr< SelectorBase > SelectorPtr
Definition: SelectorPtr.h:17
static edm::TypeWithDict elementType(const edm::TypeWithDict &wrapperType)
Perform the type deduction form edm::Wrapper&lt;C&gt; to C::value_type and resolves typedefs.
bool setCut(const char *cut)
Set the default cut that is applied to the events.
static reco::parser::ExpressionPtr makeExpression(const std::string &expr, const edm::TypeWithDict &type)
Parse an expression for a given object type (using lazy parsing when resolving methods) ...
static double eval(const reco::parser::ExpressionPtr &sel, const edm::TypeWithDict type, const void *obj)
Make a edm::ObjectWithDict(type, obj) and pass it to the expression.
void fillProf(const void *obj, TProfile *prof) const
tuple cout
Definition: gather_cfg.py:121
static reco::parser::SelectorPtr makeSelector(const std::string &expr, const edm::TypeWithDict &type)
Parse an expression for a given object type (using lazy parsing when resolving methods) ...
void fillGraph(const void *obj, TGraph *graph) const