Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:47:52

0001 #ifndef CommonTools_Utils_StringObjectFunction_h
0002 #define CommonTools_Utils_StringObjectFunction_h
0003 /* \class StringCutObjectSelector
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  * $Id: StringObjectFunction.h,v 1.4 2012/06/26 21:09:37 wmtan Exp $
0008  */
0009 #include "FWCore/Utilities/interface/EDMException.h"
0010 #include "CommonTools/Utils/interface/ExpressionPtr.h"
0011 #include "CommonTools/Utils/interface/ExpressionBase.h"
0012 #include "CommonTools/Utils/interface/expressionParser.h"
0013 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0014 
0015 template <typename T, bool DefaultLazyness = false>
0016 struct StringObjectFunction {
0017   StringObjectFunction(const std::string &expr, bool lazy = DefaultLazyness) : type_(typeid(T)) {
0018     if (!reco::parser::expressionParser<T>(expr, expr_, lazy)) {
0019       throw edm::Exception(edm::errors::Configuration, "failed to parse \"" + expr + "\"");
0020     }
0021   }
0022   StringObjectFunction(const reco::parser::ExpressionPtr &expr) : expr_(expr), type_(typeid(T)) {}
0023   double operator()(const T &t) const {
0024     edm::ObjectWithDict o(type_, const_cast<T *>(&t));
0025     return expr_->value(o);
0026   }
0027 
0028 private:
0029   reco::parser::ExpressionPtr expr_;
0030   edm::TypeWithDict type_;
0031 };
0032 
0033 template <typename Object>
0034 class sortByStringFunction {
0035 public:
0036   sortByStringFunction(StringObjectFunction<Object> *f) : f_(f) {}
0037   ~sortByStringFunction() {}
0038 
0039   bool operator()(const Object *o1, const Object *o2) { return (*f_)(*o1) > (*f_)(*o2); }
0040 
0041 private:
0042   StringObjectFunction<Object> *f_;
0043 };
0044 
0045 #endif