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 
9 helper::Parser::makeExpression(const std::string &expr, const Reflex::Type &type) {
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 
24 helper::Parser::makeSelector(const std::string &expr, const Reflex::Type &type) {
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 
40  Reflex::Type collection = wrapperType.TemplateArgumentAt(0);
41  while (collection.IsTypedef()) collection = collection.ToType();
42  // now search for value_type
43  for (size_t i = 0; i < collection.SubTypeSize(); ++i) {
44  Reflex::Type objtype = collection.SubTypeAt(i);
45  if (objtype.Name() == "value_type") {
46  while (objtype.IsTypedef()) objtype = objtype.ToType();
47  return objtype;
48  }
49  }
50  std::cerr << "Can't get a type out of " << wrapperType.Name(Reflex::SCOPED) << std::endl;
51  return Reflex::Type();
52 }
53 
54 bool
56  if (sel.get() == 0) return false;Reflex::Object obj(type, const_cast<void *>(ptr));
57  return (*sel)(obj);
58 }
59 
60 double
61 helper::Parser::eval(const reco::parser::ExpressionPtr &expr, const Reflex::Type type, const void * ptr) {
62  if (expr.get() == 0) return 0;
63  Reflex::Object obj(type, const_cast<void *>(ptr));
64  return expr->value(obj);
65 }
66 
67 bool
69  bool ok = true;
70  exprs_.push_back(helper::Parser::makeExpression(expr,objType_));
71  if (exprs_.back().get() == 0) {
72  std::cerr << "Failed to parse expression " << expr << std::endl;
73  exprs_.pop_back();
74  ok = false;
75  }
76  return ok;
77 }
78 
79 bool
81  bool ok = true;
82  cuts_[0] = helper::Parser::makeSelector(cut,objType_);
83  if (strlen(cut) && !cuts_[0].get()) {
84  std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
85  ok = false;
86  }
87  return ok;
88 }
89 
90 void
92  cuts_[0].reset();
93 }
94 
95 void
97  cuts_.resize(1);
98 }
99 
100 
101 bool
103  bool ok = true;
104  cuts_.push_back(helper::Parser::makeSelector(cut,objType_));
105  if (!cuts_.back().get()) {
106  std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
107  ok = false;
108  cuts_.pop_back();
109  }
110  return ok;
111 }
112 
113 
114 bool
115 helper::ScannerBase::test(const void *ptr, size_t icut) const {
116  if (icut >= cuts_.size()) return false;
117  if (cuts_[icut].get() == 0) return true;
118  try {
119  Reflex::Object obj(objType_, const_cast<void *>(ptr));
120  return (*cuts_[icut])(obj);
121  } catch (std::exception &ex) {
122  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
123  return false;
124  }
125 }
126 
127 double
128 helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
129  try {
130  Reflex::Object obj(objType_, const_cast<void *>(ptr));
131  if (exprs_.size() > iexpr) return exprs_[iexpr]->value(obj);
132  } catch (std::exception &ex) {
133  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
134  }
135  return 0;
136 }
137 
138 void
139 helper::ScannerBase::print(const void *ptr) const {
140  Reflex::Object obj(objType_, const_cast<void *>(ptr));
141  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
142  for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed; ++it) {
143  if (ptr == 0 || it->get() == 0) {
144  printf(" : %8s", "#ERR");
145  } else {
146  try {
147  double val = (*it)->value(obj);
148  // 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)
149  // So we have to go the ugly way
150  char buff[255];
151  int len = sprintf(buff," : % 8.6g",val); // this is usually ok, and should be 3+8 chars long
152  if (len == 3+8) {
153  std::cout << buff;
154  } else {
155  if (strchr(buff,'e')) {
156  printf((len == 3+13 ? " : % .0e" : " : % .1e"),val);
157  } else {
158  printf("%11.11s",buff);
159  }
160  }
161  } catch (std::exception &ex) {
162  printf(" : %8s", "EXCEPT");
163  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
164  }
165  }
166  }
167  for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin()+1, ed = cuts_.end(); it != ed; ++it) {
168  if (ptr == 0 || it->get() == 0) {
169  printf(" : %8s", "#ERR");
170  } else {
171  try {
172  int ret = (*it)->operator()(obj);
173  printf(" : %8d", ret);
174  } catch (std::exception &ex) {
175  printf(" : %8s", "EXCEPT");
176  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
177  }
178  }
179  }
180  fflush(stdout);
181  }
182 }
183 
184 
185 void
186 helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
187  Reflex::Object obj(objType_, const_cast<void *>(ptr));
188  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
189  try {
190  if (!exprs_.empty()) hist->Fill(exprs_[0]->value(obj));
191  } catch (std::exception &ex) {
192  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
193  }
194  }
195 }
196 
197 void
198 helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
199  Reflex::Object obj(objType_, const_cast<void *>(ptr));
200  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
201  try {
202  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
203  } catch (std::exception &ex) {
204  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
205  }
206  }
207 }
208 
209 void
210 helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
211  Reflex::Object obj(objType_, const_cast<void *>(ptr));
212  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
213  try {
214  if (exprs_.size() >= 2) graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
215  } catch (std::exception &ex) {
216  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
217  }
218  }
219 }
220 
221 
222 void
223 helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
224  Reflex::Object obj(objType_, const_cast<void *>(ptr));
225  if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
226  try {
227  if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
228  } catch (std::exception &ex) {
229  if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
230  }
231  }
232 }
void print(const void *obj) const
type
Definition: HCALResponse.h:22
int i
Definition: DBlmapReader.cc:9
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 ;.
static reco::parser::SelectorPtr makeSelector(const std::string &expr, const Reflex::Type &type)
Parse an expression for a given object type (using lazy parsing when resolving methods) ...
boost::shared_ptr< ExpressionBase > ExpressionPtr
static bool test(const reco::parser::SelectorPtr &sel, const Reflex::Type type, const void *obj)
Make a Reflex::Object(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)
static double eval(const reco::parser::ExpressionPtr &sel, const Reflex::Type type, const void *obj)
Make a Reflex::Object(type, obj) and pass it to the expression.
bool test(const void *obj, size_t icut=0) const
double eval(const void *obj, size_t iexpr=0) const
static Reflex::Type elementType(const Reflex::Type &wrapperType)
Perform the type deduction form edm::Wrapper&lt;C&gt; to C::value_type and resolves typedefs.
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
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 Reflex::Type &type)
Parse an expression for a given object type (using lazy parsing when resolving methods) ...
void fillProf(const void *obj, TProfile *prof) const
tuple cout
Definition: gather_cfg.py:121
void fillGraph(const void *obj, TGraph *graph) const