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 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
reco::parser::baseExceptionWhat
const char * baseExceptionWhat(const BaseException &e)
returns the appropriate 'what' message for the exception
Definition: Exception.h:36
TkAlMuonSelectors_cfi.cut
cut
Definition: TkAlMuonSelectors_cfi.py:5
boost::spirit::classic
Definition: DDPartSelection.cc:10
edm::ObjectWithDict
Definition: ObjectWithDict.h:17
gather_cfg.cout
cout
Definition: gather_cfg.py:144
edm::TypeWithDict::templateArgumentAt
TypeWithDict templateArgumentAt(size_t index) const
Definition: TypeWithDict.cc:697
helper::Parser::elementType
static edm::TypeWithDict elementType(const edm::TypeWithDict &wrapperType)
Perform the type deduction form edm::Wrapper<C> to C::value_type and resolves typedefs.
Definition: ScannerHelpers.cc:42
convertSQLiteXML.ok
bool ok
Definition: convertSQLiteXML.py:98
helper::ScannerBase::addExpression
bool addExpression(const char *expr)
Definition: ScannerHelpers.cc:67
EDMException.h
helper::Parser::eval
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.
Definition: ScannerHelpers.cc:60
helper::ScannerBase::print
void print(const void *obj) const
Definition: ScannerHelpers.cc:130
helper::ScannerBase::addExtraCut
bool addExtraCut(const char *cut)
Add one extra cut that can be evaluated separately (as if it was an expression)
Definition: ScannerHelpers.cc:92
helper::Parser::makeSelector
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)
Definition: ScannerHelpers.cc:25
helper::Parser::makeExpression
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)
Definition: ScannerHelpers.cc:8
helper::ScannerBase::fillProf
void fillProf(const void *obj, TProfile *prof) const
Definition: ScannerHelpers.cc:218
ScannerHelpers.h
helper::ScannerBase::clearCut
void clearCut()
Clear the default cut.
Definition: ScannerHelpers.cc:88
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
dumpparser.parse
def parse(path, config)
Definition: dumpparser.py:13
edm::TypeWithDict
Definition: TypeWithDict.h:38
reco::parser::BaseException
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:33
mps_setup.stdout
stdout
Definition: mps_setup.py:250
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
gpuVertexFinder::hist
__shared__ Hist hist
Definition: gpuClusterTracksDBSCAN.h:48
helper::ScannerBase::fill1D
void fill1D(const void *obj, TH1 *hist) const
Definition: ScannerHelpers.cc:179
helper::ScannerBase::fillGraph
void fillGraph(const void *obj, TGraph *graph) const
Definition: ScannerHelpers.cc:205
universalConfigTemplate.collection
collection
Definition: universalConfigTemplate.py:81
helper::ScannerBase::setCut
bool setCut(const char *cut)
Set the default cut that is applied to the events.
Definition: ScannerHelpers.cc:78
reco::parser::ExpressionPtr
std::shared_ptr< ExpressionBase > ExpressionPtr
Definition: ExpressionBase.h:25
helper::Parser::test
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.
Definition: ScannerHelpers.cc:53
helper::ScannerBase::clearExtraCuts
void clearExtraCuts()
Clear all extra cuts ;.
Definition: ScannerHelpers.cc:90
get
#define get
reco::parser::Grammar
Definition: Grammar.h:45
helper::ScannerBase::eval
double eval(const void *obj, size_t iexpr=0) const
Definition: ScannerHelpers.cc:118
heppy_batch.val
val
Definition: heppy_batch.py:351
ExpressionPtr.h
jets_cff.expr
expr
Definition: jets_cff.py:489
reco::parser::SelectorPtr
std::shared_ptr< SelectorBase > SelectorPtr
Definition: SelectorPtr.h:18
relativeConstraints.value
value
Definition: relativeConstraints.py:53
helper::ScannerBase::test
bool test(const void *obj, size_t icut=0) const
Definition: ScannerHelpers.cc:103
EgammaValidation_Wenu_cff.sel
sel
Definition: EgammaValidation_Wenu_cff.py:33
helper::ScannerBase::fill2D
void fill2D(const void *obj, TH2 *hist2d) const
Definition: ScannerHelpers.cc:192
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
edm::TypeWithDict::name
std::string name() const
Definition: TypeWithDict.cc:456
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
Grammar.h