Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:27

0001 #include <PhysicsTools/FWLite/interface/ScannerHelpers.h>
0002 
0003 #include "CommonTools/Utils/interface/ExpressionPtr.h"
0004 #include "CommonTools/Utils/interface/Grammar.h"
0005 #include "FWCore/Utilities/interface/EDMException.h"
0006 #include <iostream>
0007 
0008 reco::parser::ExpressionPtr helper::Parser::makeExpression(const std::string &expr, const edm::TypeWithDict &type) {
0009   reco::parser::ExpressionPtr ret;
0010 
0011   using namespace boost::spirit::classic;
0012   reco::parser::Grammar grammar(ret, type, true);
0013   const char *startingFrom = expr.c_str();
0014   try {
0015     parse(startingFrom,
0016           grammar.use_parser<1>() >> end_p,
0017           space_p);  /// NOTA BENE: <0> for cuts, <1> for expresions (why? boh!)
0018   } catch (reco::parser::BaseException &e) {
0019     std::cerr << "Expression parser error:" << reco::parser::baseExceptionWhat(e) << " (char " << e.where - startingFrom
0020               << ")" << std::endl;
0021   }
0022   return ret;
0023 }
0024 
0025 reco::parser::SelectorPtr helper::Parser::makeSelector(const std::string &expr, const edm::TypeWithDict &type) {
0026   reco::parser::SelectorPtr ret;
0027 
0028   using namespace boost::spirit::classic;
0029   reco::parser::Grammar grammar(ret, type, true);
0030   const char *startingFrom = expr.c_str();
0031   try {
0032     parse(startingFrom,
0033           grammar.use_parser<0>() >> end_p,
0034           space_p);  /// NOTA BENE: <0> for cuts, <1> for expresions (why? boh!)
0035   } catch (reco::parser::BaseException &e) {
0036     std::cerr << "Selector parser error:" << reco::parser::baseExceptionWhat(e) << " (char " << e.where - startingFrom
0037               << ")" << std::endl;
0038   }
0039   return ret;
0040 }
0041 
0042 edm::TypeWithDict helper::Parser::elementType(const edm::TypeWithDict &wrapperType) {
0043   edm::TypeWithDict collection = wrapperType.templateArgumentAt(0);
0044   // now search for value_type
0045   edm::TypeWithDict objtype = collection.nestedType("value_type");
0046   if (bool(objtype)) {
0047     return objtype;
0048   }
0049   std::cerr << "Can't get a type out of " << wrapperType.name() << std::endl;
0050   return edm::TypeWithDict();
0051 }
0052 
0053 bool helper::Parser::test(const reco::parser::SelectorPtr &sel, const edm::TypeWithDict type, const void *ptr) {
0054   if (sel.get() == nullptr)
0055     return false;
0056   edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
0057   return (*sel)(obj);
0058 }
0059 
0060 double helper::Parser::eval(const reco::parser::ExpressionPtr &expr, const edm::TypeWithDict type, const void *ptr) {
0061   if (expr.get() == nullptr)
0062     return 0;
0063   edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
0064   return expr->value(obj);
0065 }
0066 
0067 bool helper::ScannerBase::addExpression(const char *expr) {
0068   bool ok = true;
0069   exprs_.push_back(helper::Parser::makeExpression(expr, objType_));
0070   if (exprs_.back().get() == nullptr) {
0071     std::cerr << "Failed to parse expression " << expr << std::endl;
0072     exprs_.pop_back();
0073     ok = false;
0074   }
0075   return ok;
0076 }
0077 
0078 bool helper::ScannerBase::setCut(const char *cut) {
0079   bool ok = true;
0080   cuts_[0] = helper::Parser::makeSelector(cut, objType_);
0081   if (strlen(cut) && !cuts_[0].get()) {
0082     std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
0083     ok = false;
0084   }
0085   return ok;
0086 }
0087 
0088 void helper::ScannerBase::clearCut() { cuts_[0].reset(); }
0089 
0090 void helper::ScannerBase::clearExtraCuts() { cuts_.resize(1); }
0091 
0092 bool helper::ScannerBase::addExtraCut(const char *cut) {
0093   bool ok = true;
0094   cuts_.push_back(helper::Parser::makeSelector(cut, objType_));
0095   if (!cuts_.back().get()) {
0096     std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
0097     ok = false;
0098     cuts_.pop_back();
0099   }
0100   return ok;
0101 }
0102 
0103 bool helper::ScannerBase::test(const void *ptr, size_t icut) const {
0104   if (icut >= cuts_.size())
0105     return false;
0106   if (cuts_[icut].get() == nullptr)
0107     return true;
0108   try {
0109     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0110     return (*cuts_[icut])(obj);
0111   } catch (std::exception &ex) {
0112     if (!ignoreExceptions_)
0113       std::cerr << "Caught exception " << ex.what() << std::endl;
0114     return false;
0115   }
0116 }
0117 
0118 double helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
0119   try {
0120     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0121     if (exprs_.size() > iexpr)
0122       return exprs_[iexpr]->value(obj);
0123   } catch (std::exception &ex) {
0124     if (!ignoreExceptions_)
0125       std::cerr << "Caught exception " << ex.what() << std::endl;
0126   }
0127   return 0;
0128 }
0129 
0130 void helper::ScannerBase::print(const void *ptr) const {
0131   edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0132   if ((cuts_[0].get() == nullptr) || (*cuts_[0])(obj)) {
0133     for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed;
0134          ++it) {
0135       if (ptr == nullptr || it->get() == nullptr) {
0136         printf(" : %8s", "#ERR");
0137       } else {
0138         try {
0139           double val = (*it)->value(obj);
0140           // 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)
0141           // So we have to go the ugly way
0142           char buff[255];
0143           int len = sprintf(buff, " : % 8.6g", val);  // this is usually ok, and should be 3+8 chars long
0144           if (len == 3 + 8) {
0145             std::cout << buff;
0146           } else {
0147             if (strchr(buff, 'e')) {
0148               printf((len == 3 + 13 ? " :  % .0e" : " : % .1e"), val);
0149             } else {
0150               printf("%11.11s", buff);
0151             }
0152           }
0153         } catch (std::exception &ex) {
0154           printf(" : %8s", "EXCEPT");
0155           if (!ignoreExceptions_)
0156             std::cerr << "Caught exception " << ex.what() << std::endl;
0157         }
0158       }
0159     }
0160     for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin() + 1, ed = cuts_.end(); it != ed;
0161          ++it) {
0162       if (ptr == nullptr || it->get() == nullptr) {
0163         printf(" : %8s", "#ERR");
0164       } else {
0165         try {
0166           int ret = (*it)->operator()(obj);
0167           printf(" : %8d", ret);
0168         } catch (std::exception &ex) {
0169           printf(" : %8s", "EXCEPT");
0170           if (!ignoreExceptions_)
0171             std::cerr << "Caught exception " << ex.what() << std::endl;
0172         }
0173       }
0174     }
0175     fflush(stdout);
0176   }
0177 }
0178 
0179 void helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
0180   edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0181   if ((cuts_[0].get() == nullptr) || (*cuts_[0])(obj)) {
0182     try {
0183       if (!exprs_.empty())
0184         hist->Fill(exprs_[0]->value(obj));
0185     } catch (std::exception &ex) {
0186       if (!ignoreExceptions_)
0187         std::cerr << "Caught exception " << ex.what() << std::endl;
0188     }
0189   }
0190 }
0191 
0192 void helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
0193   edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0194   if ((cuts_[0].get() == nullptr) || (*cuts_[0])(obj)) {
0195     try {
0196       if (exprs_.size() >= 2)
0197         hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
0198     } catch (std::exception &ex) {
0199       if (!ignoreExceptions_)
0200         std::cerr << "Caught exception " << ex.what() << std::endl;
0201     }
0202   }
0203 }
0204 
0205 void helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
0206   edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0207   if ((cuts_[0].get() == nullptr) || (*cuts_[0])(obj)) {
0208     try {
0209       if (exprs_.size() >= 2)
0210         graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
0211     } catch (std::exception &ex) {
0212       if (!ignoreExceptions_)
0213         std::cerr << "Caught exception " << ex.what() << std::endl;
0214     }
0215   }
0216 }
0217 
0218 void helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
0219   edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
0220   if ((cuts_[0].get() == nullptr) || (*cuts_[0])(obj)) {
0221     try {
0222       if (exprs_.size() >= 2)
0223         hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
0224     } catch (std::exception &ex) {
0225       if (!ignoreExceptions_)
0226         std::cerr << "Caught exception " << ex.what() << std::endl;
0227     }
0228   }
0229 }