CMS 3D CMS Logo

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