00001 #include <PhysicsTools/FWLite/interface/ScannerHelpers.h>
00002
00003 #include "CommonTools/Utils/src/ExpressionPtr.h"
00004 #include "CommonTools/Utils/src/Grammar.h"
00005 #include "FWCore/Utilities/interface/EDMException.h"
00006 #include <iostream>
00007
00008 reco::parser::ExpressionPtr
00009 helper::Parser::makeExpression(const std::string &expr, const edm::TypeWithDict &type) {
00010 reco::parser::ExpressionPtr ret;
00011
00012 using namespace boost::spirit::classic;
00013 reco::parser::Grammar grammar(ret, type, true);
00014 const char* startingFrom = expr.c_str();
00015 try {
00016 parse(startingFrom, grammar.use_parser<1>() >> end_p, space_p).full;
00017 } catch(reco::parser::BaseException&e){
00018 std::cerr << "Expression parser error:"<<reco::parser::baseExceptionWhat(e)<<" (char "<<e.where-startingFrom<<")" << std::endl;
00019 }
00020 return ret;
00021 }
00022
00023 reco::parser::SelectorPtr
00024 helper::Parser::makeSelector(const std::string &expr, const edm::TypeWithDict &type) {
00025 reco::parser::SelectorPtr ret;
00026
00027 using namespace boost::spirit::classic;
00028 reco::parser::Grammar grammar(ret, type, true);
00029 const char* startingFrom = expr.c_str();
00030 try {
00031 parse(startingFrom, grammar.use_parser<0>() >> end_p, space_p).full;
00032 } catch(reco::parser::BaseException&e){
00033 std::cerr << "Selector parser error:"<<reco::parser::baseExceptionWhat(e)<<" (char "<<e.where-startingFrom<<")" << std::endl;
00034 }
00035 return ret;
00036 }
00037
00038 edm::TypeWithDict
00039 helper::Parser::elementType(const edm::TypeWithDict &wrapperType) {
00040 edm::TypeWithDict collection = wrapperType.templateArgumentAt(0);
00041 while (collection.isTypedef()) collection = collection.toType();
00042
00043 edm::TypeWithDict objtype = collection.nestedType("value_type");
00044 if(bool(objtype)) {
00045 while (objtype.isTypedef()) objtype = objtype.toType();
00046 return objtype;
00047 }
00048 std::cerr << "Can't get a type out of " << wrapperType.name(edm::TypeNameHandling::Scoped) << std::endl;
00049 return edm::TypeWithDict();
00050 }
00051
00052 bool
00053 helper::Parser::test(const reco::parser::SelectorPtr &sel, const edm::TypeWithDict type, const void * ptr) {
00054 if (sel.get() == 0) return false;edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
00055 return (*sel)(obj);
00056 }
00057
00058 double
00059 helper::Parser::eval(const reco::parser::ExpressionPtr &expr, const edm::TypeWithDict type, const void * ptr) {
00060 if (expr.get() == 0) return 0;
00061 edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
00062 return expr->value(obj);
00063 }
00064
00065 bool
00066 helper::ScannerBase::addExpression(const char *expr) {
00067 bool ok = true;
00068 exprs_.push_back(helper::Parser::makeExpression(expr,objType_));
00069 if (exprs_.back().get() == 0) {
00070 std::cerr << "Failed to parse expression " << expr << std::endl;
00071 exprs_.pop_back();
00072 ok = false;
00073 }
00074 return ok;
00075 }
00076
00077 bool
00078 helper::ScannerBase::setCut(const char *cut) {
00079 bool ok = true;
00080 cuts_[0] = helper::Parser::makeSelector(cut,objType_);
00081 if (strlen(cut) && !cuts_[0].get()) {
00082 std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
00083 ok = false;
00084 }
00085 return ok;
00086 }
00087
00088 void
00089 helper::ScannerBase::clearCut() {
00090 cuts_[0].reset();
00091 }
00092
00093 void
00094 helper::ScannerBase::clearExtraCuts() {
00095 cuts_.resize(1);
00096 }
00097
00098
00099 bool
00100 helper::ScannerBase::addExtraCut(const char *cut) {
00101 bool ok = true;
00102 cuts_.push_back(helper::Parser::makeSelector(cut,objType_));
00103 if (!cuts_.back().get()) {
00104 std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
00105 ok = false;
00106 cuts_.pop_back();
00107 }
00108 return ok;
00109 }
00110
00111
00112 bool
00113 helper::ScannerBase::test(const void *ptr, size_t icut) const {
00114 if (icut >= cuts_.size()) return false;
00115 if (cuts_[icut].get() == 0) return true;
00116 try {
00117 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00118 return (*cuts_[icut])(obj);
00119 } catch (std::exception &ex) {
00120 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00121 return false;
00122 }
00123 }
00124
00125 double
00126 helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
00127 try {
00128 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00129 if (exprs_.size() > iexpr) return exprs_[iexpr]->value(obj);
00130 } catch (std::exception &ex) {
00131 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00132 }
00133 return 0;
00134 }
00135
00136 void
00137 helper::ScannerBase::print(const void *ptr) const {
00138 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00139 if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00140 for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed; ++it) {
00141 if (ptr == 0 || it->get() == 0) {
00142 printf(" : %8s", "#ERR");
00143 } else {
00144 try {
00145 double val = (*it)->value(obj);
00146
00147
00148 char buff[255];
00149 int len = sprintf(buff," : % 8.6g",val);
00150 if (len == 3+8) {
00151 std::cout << buff;
00152 } else {
00153 if (strchr(buff,'e')) {
00154 printf((len == 3+13 ? " : % .0e" : " : % .1e"),val);
00155 } else {
00156 printf("%11.11s",buff);
00157 }
00158 }
00159 } catch (std::exception &ex) {
00160 printf(" : %8s", "EXCEPT");
00161 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00162 }
00163 }
00164 }
00165 for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin()+1, ed = cuts_.end(); it != ed; ++it) {
00166 if (ptr == 0 || it->get() == 0) {
00167 printf(" : %8s", "#ERR");
00168 } else {
00169 try {
00170 int ret = (*it)->operator()(obj);
00171 printf(" : %8d", ret);
00172 } catch (std::exception &ex) {
00173 printf(" : %8s", "EXCEPT");
00174 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00175 }
00176 }
00177 }
00178 fflush(stdout);
00179 }
00180 }
00181
00182
00183 void
00184 helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
00185 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00186 if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00187 try {
00188 if (!exprs_.empty()) hist->Fill(exprs_[0]->value(obj));
00189 } catch (std::exception &ex) {
00190 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00191 }
00192 }
00193 }
00194
00195 void
00196 helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
00197 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00198 if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00199 try {
00200 if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
00201 } catch (std::exception &ex) {
00202 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00203 }
00204 }
00205 }
00206
00207 void
00208 helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
00209 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00210 if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00211 try {
00212 if (exprs_.size() >= 2) graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
00213 } catch (std::exception &ex) {
00214 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00215 }
00216 }
00217 }
00218
00219
00220 void
00221 helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
00222 edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00223 if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00224 try {
00225 if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
00226 } catch (std::exception &ex) {
00227 if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00228 }
00229 }
00230 }