File indexing completed on 2024-04-06 12:24:04
0001 #ifndef PhysicsTools_PatUtils_interface_StringParserTools_h
0002 #define PhysicsTools_PatUtils_interface_StringParserTools_h
0003
0004 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0005 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0006
0007 #include "DataFormats/PatCandidates/interface/Electron.h"
0008 #include "DataFormats/PatCandidates/interface/Muon.h"
0009 #include "DataFormats/PatCandidates/interface/Tau.h"
0010 #include "DataFormats/PatCandidates/interface/Photon.h"
0011 #include "DataFormats/PatCandidates/interface/Jet.h"
0012 #include "DataFormats/PatCandidates/interface/MET.h"
0013 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0014 #include "DataFormats/PatCandidates/interface/PFParticle.h"
0015
0016 class PATStringObjectFunction {
0017 public:
0018 PATStringObjectFunction() {}
0019 PATStringObjectFunction(const std::string &string);
0020
0021 double operator()(const reco::Candidate &c) const;
0022
0023 private:
0024 std::string expr_;
0025 std::shared_ptr<StringObjectFunction<reco::Candidate> > candFunc_;
0026
0027 std::shared_ptr<StringObjectFunction<pat::Electron> > eleFunc_;
0028 std::shared_ptr<StringObjectFunction<pat::Muon> > muFunc_;
0029 std::shared_ptr<StringObjectFunction<pat::Tau> > tauFunc_;
0030 std::shared_ptr<StringObjectFunction<pat::Photon> > gamFunc_;
0031 std::shared_ptr<StringObjectFunction<pat::Jet> > jetFunc_;
0032 std::shared_ptr<StringObjectFunction<pat::MET> > metFunc_;
0033 std::shared_ptr<StringObjectFunction<pat::GenericParticle> > gpFunc_;
0034 std::shared_ptr<StringObjectFunction<pat::PFParticle> > pfFunc_;
0035
0036 template <typename Obj>
0037 std::shared_ptr<StringObjectFunction<Obj> > tryGet(const std::string &str) {
0038 try {
0039 return std::shared_ptr<StringObjectFunction<Obj> >(new StringObjectFunction<Obj>(str));
0040 } catch (cms::Exception const &) {
0041 return std::shared_ptr<StringObjectFunction<Obj> >();
0042 }
0043 }
0044
0045 template <typename Obj>
0046 double tryEval(const reco::Candidate &c, const std::shared_ptr<StringObjectFunction<Obj> > &func) const {
0047 if (func.get())
0048 return (*func)(static_cast<const Obj &>(c));
0049 else
0050 throwBadType(typeid(c));
0051 assert(false);
0052 return 0;
0053 }
0054
0055
0056 void throwBadType(const std::type_info &ty1) const;
0057 };
0058
0059 class PATStringCutObjectSelector {
0060 public:
0061 PATStringCutObjectSelector() {}
0062 PATStringCutObjectSelector(const std::string &string);
0063
0064 bool operator()(const reco::Candidate &c) const;
0065
0066 private:
0067 std::string expr_;
0068 std::shared_ptr<StringCutObjectSelector<reco::Candidate> > candFunc_;
0069
0070 std::shared_ptr<StringCutObjectSelector<pat::Electron> > eleFunc_;
0071 std::shared_ptr<StringCutObjectSelector<pat::Muon> > muFunc_;
0072 std::shared_ptr<StringCutObjectSelector<pat::Tau> > tauFunc_;
0073 std::shared_ptr<StringCutObjectSelector<pat::Photon> > gamFunc_;
0074 std::shared_ptr<StringCutObjectSelector<pat::Jet> > jetFunc_;
0075 std::shared_ptr<StringCutObjectSelector<pat::MET> > metFunc_;
0076 std::shared_ptr<StringCutObjectSelector<pat::GenericParticle> > gpFunc_;
0077 std::shared_ptr<StringCutObjectSelector<pat::PFParticle> > pfFunc_;
0078
0079 template <typename Obj>
0080 std::shared_ptr<StringCutObjectSelector<Obj> > tryGet(const std::string &str) {
0081 try {
0082 return std::shared_ptr<StringCutObjectSelector<Obj> >(new StringCutObjectSelector<Obj>(str));
0083 } catch (cms::Exception const &) {
0084 return std::shared_ptr<StringCutObjectSelector<Obj> >();
0085 }
0086 }
0087
0088 template <typename Obj>
0089 bool tryEval(const reco::Candidate &c, const std::shared_ptr<StringCutObjectSelector<Obj> > &func) const {
0090 if (func.get())
0091 return (*func)(static_cast<const Obj &>(c));
0092 else
0093 throwBadType(typeid(c));
0094 assert(false);
0095 return false;
0096 }
0097
0098
0099 void throwBadType(const std::type_info &ty1) const;
0100 };
0101
0102 #endif