CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VariableEventSelector.h
Go to the documentation of this file.
1 #ifndef _VariableEventSelector_H
2 #define _VariableEventSelector_H
3 
7 #include "TFormula.h"
8 #include "TString.h"
9 
11 public:
13  : VariableFormulaEventSelector(pset, iC) {}
15  const std::string name_ = pset.getParameter<std::string>("fname");
16  TString ts(pset.getParameter<std::string>("formula"));
17  //find the variables. register and replace
18  int open = ts.Index("[");
19  int close = ts.Index("]");
20  while (open > 0) {
21  ++open;
22  TString sub(ts(open, close - open));
23  //std::cout<<"found:"<< sub <<std::endl;
24  vars_.insert(sub.Data());
25  //vars_.insert( ts(open,close-open));
26  open = ts.Index("[", open);
27  close = ts.Index("]", open);
28  }
29 
30  unsigned int v_i;
31  std::set<std::string>::iterator v_it;
32  for (v_i = 0, v_it = vars_.begin(); v_i != vars_.size(); ++v_i, ++v_it) {
33  ts.ReplaceAll(TString::Format("[%s]", v_it->c_str()), TString::Format("[%d]", v_i));
34  }
35 
36  //std::cout<<" formula found:"<< ts <<std::endl;
37  formula_ = new TFormula(name_.c_str(), ts);
38  //vars_ = pset.getParameter<std::vector<std::string>>("variables");
39  threshold_ = pset.getParameter<double>("threshold");
40  }
41 
42  bool select(const edm::Event& e) const override {
43  unsigned int v_i;
44  std::set<std::string>::iterator v_it;
45 
46  for (v_i = 0, v_it = vars_.begin(); v_i != vars_.size(); ++v_i, ++v_it) {
47  const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(*v_it);
48  if (!var->compute(e))
49  return false;
50  double v = (*var)(e);
51  formula_->SetParameter(v_i, v);
52  }
53 
54  //should be valuated 0. or 1. in double
55  return (formula_->Eval(0.) >= threshold_);
56  }
57 
58 private:
59  //std::string formula_;
60  TFormula* formula_;
61  std::set<std::string> vars_;
62  double threshold_;
63 };
64 
66 public:
69  var_ = pset.getParameter<std::string>("var");
70  doMin_ = pset.exists("min");
71  if (doMin_)
72  min_ = pset.getParameter<double>("min");
73  doMax_ = pset.exists("max");
74  if (doMax_)
75  max_ = pset.getParameter<double>("max");
76 
77  std::stringstream ss;
78  ss << "event selector based on VariableHelper variable: " << var_;
79  description_.push_back(ss.str());
80  ss.str("");
81  if (doMin_) {
82  ss << "with minimum boundary: " << min_;
83  description_.push_back(ss.str());
84  ss.str("");
85  }
86  if (doMax_) {
87  ss << "with maximum boundary: " << max_;
88  description_.push_back(ss.str());
89  ss.str("");
90  }
91  }
92  bool select(const edm::Event& e) const override {
93  const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(var_);
94  if (!var->compute(e))
95  return false;
96 
97  double v = (*var)(e);
98 
99  if (doMin_ && v < min_)
100  return false;
101  else if (doMax_ && v > max_)
102  return false;
103  else
104  return true;
105  }
106 
107 private:
109  bool doMin_;
110  double min_;
111  bool doMax_;
112  double max_;
113 };
114 
115 #endif
VariableFormulaEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &iC)
bool exists(std::string const &parameterName) const
checks if a parameter exists
VariableEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &&iC)
bool select(const edm::Event &e) const override
decision of the selector module
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
A selector of events.
Definition: EventSelector.h:16
bool compute(const edm::Event &iEvent) const
std::vector< std::string > description_
Definition: EventSelector.h:33
std::string name_
Definition: EventSelector.h:32
VariableEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &iC)
bool select(const edm::Event &e) const override
decision of the selector module
std::set< std::string > vars_
VariableFormulaEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &&iC)