CMS 3D CMS Logo

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);
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);
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;
53  edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
54  return (*sel)(obj);
55 }
56 
57 double
59  if (expr.get() == 0) return 0;
60  edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
61  return expr->value(obj);
62 }
63 
64 bool
66  bool ok = true;
67  exprs_.push_back(helper::Parser::makeExpression(expr,objType_));
68  if (exprs_.back().get() == 0) {
69  std::cerr << "Failed to parse expression " << expr << std::endl;
70  exprs_.pop_back();
71  ok = false;
72  }
73  return ok;
74 }
75 
76 bool
78  bool ok = true;
79  cuts_[0] = helper::Parser::makeSelector(cut,objType_);
80  if (strlen(cut) && !cuts_[0].get()) {
81  std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
82  ok = false;
83  }
84  return ok;
85 }
86 
87 void
89  cuts_[0].reset();
90 }
91 
92 void
94  cuts_.resize(1);
95 }
96 
97 
98 bool
100  bool ok = true;
101  cuts_.push_back(helper::Parser::makeSelector(cut,objType_));
102  if (!cuts_.back().get()) {
103  std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
104  ok = false;
105  cuts_.pop_back();
106  }
107  return ok;
108 }
109 
110 
111 bool
112 helper::ScannerBase::test(const void *ptr, size_t icut) const {
113  if (icut >= cuts_.size()) return false;
114  if (cuts_[icut].get() == 0) return true;
115  try {
116  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
117  return (*cuts_[icut])(obj);
118  } catch (std::exception &ex) {
119  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
120  return false;
121  }
122 }
123 
124 double
125 helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
126  try {
127  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
128  if (exprs_.size() > iexpr) return exprs_[iexpr]->value(obj);
129  } catch (std::exception &ex) {
130  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
131  }
132  return 0;
133 }
134 
135 void
136 helper::ScannerBase::print(const void *ptr) const {
137  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
138  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
139  for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed; ++it) {
140  if (ptr == 0 || it->get() == 0) {
141  printf(" : %8s", "#ERR");
142  } else {
143  try {
144  double val = (*it)->value(obj);
145  // 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)
146  // So we have to go the ugly way
147  char buff[255];
148  int len = sprintf(buff," : % 8.6g",val); // this is usually ok, and should be 3+8 chars long
149  if (len == 3+8) {
150  std::cout << buff;
151  } else {
152  if (strchr(buff,'e')) {
153  printf((len == 3+13 ? " : % .0e" : " : % .1e"),val);
154  } else {
155  printf("%11.11s",buff);
156  }
157  }
158  } catch (std::exception &ex) {
159  printf(" : %8s", "EXCEPT");
160  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
161  }
162  }
163  }
164  for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin()+1, ed = cuts_.end(); it != ed; ++it) {
165  if (ptr == 0 || it->get() == 0) {
166  printf(" : %8s", "#ERR");
167  } else {
168  try {
169  int ret = (*it)->operator()(obj);
170  printf(" : %8d", ret);
171  } catch (std::exception &ex) {
172  printf(" : %8s", "EXCEPT");
173  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
174  }
175  }
176  }
177  fflush(stdout);
178  }
179 }
180 
181 
182 void
183 helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
184  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
185  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
186  try {
187  if (!exprs_.empty()) hist->Fill(exprs_[0]->value(obj));
188  } catch (std::exception &ex) {
189  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
190  }
191  }
192 }
193 
194 void
195 helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
196  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
197  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
198  try {
199  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
200  } catch (std::exception &ex) {
201  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
202  }
203  }
204 }
205 
206 void
207 helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
208  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
209  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
210  try {
211  if (exprs_.size() >= 2) graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
212  } catch (std::exception &ex) {
213  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
214  }
215  }
216 }
217 
218 
219 void
220 helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
221  edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
222  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
223  try {
224  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
225  } catch (std::exception &ex) {
226  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
227  }
228  }
229 }
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
void clearExtraCuts()
Clear all extra cuts ;.
boost::shared_ptr< ExpressionBase > ExpressionPtr
TypeWithDict nestedType(char const *) const
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
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
def parse(path, config)
Definition: dumpparser.py:13
boost::shared_ptr< SelectorBase > SelectorPtr
Definition: SelectorPtr.h:17
static edm::TypeWithDict elementType(const edm::TypeWithDict &wrapperType)
Perform the type deduction form edm::Wrapper<C> 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
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