Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:26

0001 #ifndef _VariableEventSelector_H
0002 #define _VariableEventSelector_H
0003 
0004 #include "FWCore/Framework/interface/ConsumesCollector.h"
0005 #include "CommonTools/UtilAlgos/interface/EventSelector.h"
0006 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
0007 #include "TFormula.h"
0008 #include "TString.h"
0009 
0010 class VariableFormulaEventSelector : public EventSelector {
0011 public:
0012   VariableFormulaEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
0013       : VariableFormulaEventSelector(pset, iC) {}
0014   VariableFormulaEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector& iC) : EventSelector(pset, iC) {
0015     const std::string name_ = pset.getParameter<std::string>("fname");
0016     TString ts(pset.getParameter<std::string>("formula"));
0017     //find the variables. register and replace
0018     int open = ts.Index("[");
0019     int close = ts.Index("]");
0020     while (open > 0) {
0021       ++open;
0022       TString sub(ts(open, close - open));
0023       //std::cout<<"found:"<< sub <<std::endl;
0024       vars_.insert(sub.Data());
0025       //vars_.insert(  ts(open,close-open));
0026       open = ts.Index("[", open);
0027       close = ts.Index("]", open);
0028     }
0029 
0030     unsigned int v_i;
0031     std::set<std::string>::iterator v_it;
0032     for (v_i = 0, v_it = vars_.begin(); v_i != vars_.size(); ++v_i, ++v_it) {
0033       ts.ReplaceAll(TString::Format("[%s]", v_it->c_str()), TString::Format("[%d]", v_i));
0034     }
0035 
0036     //std::cout<<" formula found:"<< ts <<std::endl;
0037     formula_ = new TFormula(name_.c_str(), ts);
0038     //vars_ = pset.getParameter<std::vector<std::string>>("variables");
0039     threshold_ = pset.getParameter<double>("threshold");
0040   }
0041 
0042   bool select(const edm::Event& e) const override {
0043     unsigned int v_i;
0044     std::set<std::string>::iterator v_it;
0045 
0046     for (v_i = 0, v_it = vars_.begin(); v_i != vars_.size(); ++v_i, ++v_it) {
0047       const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(*v_it);
0048       if (!var->compute(e))
0049         return false;
0050       double v = (*var)(e);
0051       formula_->SetParameter(v_i, v);
0052     }
0053 
0054     //should be valuated 0. or 1. in double
0055     return (formula_->Eval(0.) >= threshold_);
0056   }
0057 
0058 private:
0059   //std::string formula_;
0060   TFormula* formula_;
0061   std::set<std::string> vars_;
0062   double threshold_;
0063 };
0064 
0065 class VariableEventSelector : public EventSelector {
0066 public:
0067   VariableEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC) : VariableEventSelector(pset, iC) {}
0068   VariableEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector& iC) : EventSelector(pset, iC) {
0069     var_ = pset.getParameter<std::string>("var");
0070     doMin_ = pset.exists("min");
0071     if (doMin_)
0072       min_ = pset.getParameter<double>("min");
0073     doMax_ = pset.exists("max");
0074     if (doMax_)
0075       max_ = pset.getParameter<double>("max");
0076 
0077     std::stringstream ss;
0078     ss << "event selector based on VariableHelper variable: " << var_;
0079     description_.push_back(ss.str());
0080     ss.str("");
0081     if (doMin_) {
0082       ss << "with minimum boundary: " << min_;
0083       description_.push_back(ss.str());
0084       ss.str("");
0085     }
0086     if (doMax_) {
0087       ss << "with maximum boundary: " << max_;
0088       description_.push_back(ss.str());
0089       ss.str("");
0090     }
0091   }
0092   bool select(const edm::Event& e) const override {
0093     const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(var_);
0094     if (!var->compute(e))
0095       return false;
0096 
0097     double v = (*var)(e);
0098 
0099     if (doMin_ && v < min_)
0100       return false;
0101     else if (doMax_ && v > max_)
0102       return false;
0103     else
0104       return true;
0105   }
0106 
0107 private:
0108   std::string var_;
0109   bool doMin_;
0110   double min_;
0111   bool doMax_;
0112   double max_;
0113 };
0114 
0115 #endif