CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/FWLite/src/ScannerHelpers.cc

Go to the documentation of this file.
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     // now search for value_type
00042     edm::TypeWithDict objtype = collection.nestedType("value_type");
00043     if(bool(objtype)) {
00044       return objtype;
00045     }
00046     std::cerr << "Can't get a type out of " << wrapperType.name() << std::endl;
00047     return edm::TypeWithDict();
00048 }
00049 
00050 bool
00051 helper::Parser::test(const reco::parser::SelectorPtr &sel, const edm::TypeWithDict type, const void * ptr) {
00052     if (sel.get() == 0) return false;edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
00053     return (*sel)(obj);
00054 }
00055 
00056 double
00057 helper::Parser::eval(const reco::parser::ExpressionPtr &expr, const edm::TypeWithDict type, const void * ptr) {
00058     if (expr.get() == 0) return 0;
00059     edm::ObjectWithDict obj(type, const_cast<void *>(ptr));
00060     return expr->value(obj);
00061 }
00062 
00063 bool
00064 helper::ScannerBase::addExpression(const char *expr) {
00065     bool ok = true;
00066     exprs_.push_back(helper::Parser::makeExpression(expr,objType_));
00067     if (exprs_.back().get() == 0) {
00068         std::cerr << "Failed to parse expression " << expr << std::endl;
00069         exprs_.pop_back();
00070         ok = false;
00071     }
00072     return ok;
00073 }
00074 
00075 bool
00076 helper::ScannerBase::setCut(const char *cut) {
00077     bool ok = true;
00078     cuts_[0] = helper::Parser::makeSelector(cut,objType_);
00079     if (strlen(cut) && !cuts_[0].get()) {
00080         std::cerr << "Failed to set cut \"" << cut << "\"" << std::endl;
00081         ok = false;
00082     }
00083     return ok;
00084 }
00085 
00086 void
00087 helper::ScannerBase::clearCut() {
00088     cuts_[0].reset();
00089 }
00090 
00091 void
00092 helper::ScannerBase::clearExtraCuts() {
00093     cuts_.resize(1);
00094 }
00095 
00096 
00097 bool
00098 helper::ScannerBase::addExtraCut(const char *cut) {
00099     bool ok = true;
00100     cuts_.push_back(helper::Parser::makeSelector(cut,objType_));
00101     if (!cuts_.back().get()) {
00102         std::cerr << "Failed to add cut \"" << cut << "\"" << std::endl;
00103         ok = false;
00104         cuts_.pop_back();
00105     }
00106     return ok;
00107 }
00108 
00109 
00110 bool
00111 helper::ScannerBase::test(const void *ptr, size_t icut) const {
00112     if (icut >= cuts_.size()) return false;
00113     if (cuts_[icut].get() == 0) return true;
00114     try {
00115         edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00116         return (*cuts_[icut])(obj);
00117     } catch (std::exception &ex) {
00118         if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00119         return false;
00120     }
00121 }
00122 
00123 double
00124 helper::ScannerBase::eval(const void *ptr, size_t iexpr) const {
00125     try {
00126         edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00127         if (exprs_.size() > iexpr)  return exprs_[iexpr]->value(obj);
00128     } catch (std::exception &ex) {
00129         if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00130     }
00131     return 0;
00132 }
00133 
00134 void
00135 helper::ScannerBase::print(const void *ptr) const {
00136     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00137     if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00138         for (std::vector<reco::parser::ExpressionPtr>::const_iterator it = exprs_.begin(), ed = exprs_.end(); it != ed; ++it) {
00139             if (ptr == 0 || it->get() == 0) {
00140                 printf(" : %8s", "#ERR");
00141             } else {  
00142                 try {
00143                     double val = (*it)->value(obj);
00144                     // 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)
00145                     // So we have to go the ugly way
00146                     char buff[255];
00147                     int len = sprintf(buff," : % 8.6g",val); // this is usually ok, and should be 3+8 chars long
00148                     if (len == 3+8) {
00149                         std::cout << buff;
00150                     } else {
00151                         if (strchr(buff,'e')) {
00152                             printf((len == 3+13 ? " :  % .0e" : " : % .1e"),val);
00153                         } else {
00154                             printf("%11.11s",buff);
00155                         } 
00156                     } 
00157                 } catch (std::exception &ex) {
00158                     printf(" : %8s", "EXCEPT"); 
00159                     if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00160                 }
00161             }
00162         }
00163         for (std::vector<reco::parser::SelectorPtr>::const_iterator it = cuts_.begin()+1, ed = cuts_.end(); it != ed; ++it) {
00164             if (ptr == 0 || it->get() == 0) {
00165                 printf(" : %8s", "#ERR");
00166             } else {  
00167                 try {
00168                     int ret = (*it)->operator()(obj);
00169                     printf(" : %8d", ret);
00170                 } catch (std::exception &ex) {
00171                     printf(" : %8s", "EXCEPT"); 
00172                     if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00173                 }
00174             }
00175         }
00176         fflush(stdout);
00177     }
00178 }
00179 
00180 
00181 void
00182 helper::ScannerBase::fill1D(const void *ptr, TH1 *hist) const {
00183     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00184     if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00185         try {
00186             if (!exprs_.empty()) hist->Fill(exprs_[0]->value(obj));
00187         } catch (std::exception &ex) {
00188             if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00189         }
00190     }
00191 }
00192 
00193 void
00194 helper::ScannerBase::fill2D(const void *ptr, TH2 *hist) const {
00195     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00196     if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00197         try {
00198             if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
00199         } catch (std::exception &ex) {
00200             if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00201         }
00202     }
00203 }
00204 
00205 void
00206 helper::ScannerBase::fillGraph(const void *ptr, TGraph *graph) const {
00207     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00208     if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00209         try {
00210             if (exprs_.size() >= 2) graph->SetPoint(graph->GetN(), exprs_[0]->value(obj), exprs_[1]->value(obj));
00211         } catch (std::exception &ex) {
00212             if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00213         }
00214     }
00215 }
00216 
00217 
00218 void
00219 helper::ScannerBase::fillProf(const void *ptr, TProfile *hist) const {
00220     edm::ObjectWithDict obj(objType_, const_cast<void *>(ptr));
00221     if ((cuts_[0].get() == 0) || (*cuts_[0])(obj)) {
00222         try {
00223             if (exprs_.size() >= 2) hist->Fill(exprs_[0]->value(obj), exprs_[1]->value(obj));
00224         } catch (std::exception &ex) {
00225             if (!ignoreExceptions_) std::cerr << "Caught exception " << ex.what() << std::endl;
00226         }
00227     }
00228 }