CMS 3D CMS Logo

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