CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CommonTools/UtilAlgos/interface/StringCutEventSelector.h

Go to the documentation of this file.
00001 #ifndef _StringCutEventSelector
00002 #define _StringCutEventSelector
00003 
00004 #include "CommonTools/UtilAlgos/interface/EventSelector.h"
00005 #include "CommonTools/UtilAlgos/interface/InputTagDistributor.h"
00006 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00007 #include "FWCore/ServiceRegistry/interface/Service.h"
00008 
00009 template<typename Object>
00010 class  StringCutEventSelector : public EventSelector {
00011  public:
00012   StringCutEventSelector(const edm::ParameterSet& pset) :
00013     EventSelector(pset),
00014     src_(edm::Service<InputTagDistributorService>()->retrieve("src",pset)),
00015     f_(pset.getParameter<std::string>("cut")),
00016     //put this guy to 0 to do the check on "all" object in the collection
00017     nFirst_(pset.getParameter<unsigned int>("nFirst"))
00018       {
00019           std::stringstream ss;
00020           ss<<"string cut based selection on collection: "<<src_;
00021           description_.push_back(ss.str());
00022           ss.str("");
00023           description_.push_back(std::string("selection cut is: ")+pset.getParameter<std::string>("cut"));
00024       }
00025     
00026     bool select (const edm::Event& e) const{
00027       edm::Handle<edm::View<Object> > oH;
00028       e.getByLabel(src_, oH);
00029       //reject events if not enough object in collection
00030       //      if ((nFirst_!=0) && (oH->size()<nFirst_)) return false;
00031       unsigned int i=0;
00032       for (;i!=oH->size();i++)
00033         {
00034           //stop doing the check if reaching too far in the collection
00035           if ((nFirst_!=0) && (i>=nFirst_)) break;
00036           const Object & o = (*oH)[i];
00037           if (!f_(o)) return false;
00038         }
00039       return true;
00040     }
00041     
00042  private:
00043     edm::InputTag src_;
00044     StringCutObjectSelector<Object> f_;
00045     unsigned int nFirst_;
00046 };
00047 
00048 
00049 template<typename Object, bool existenceMatter=true>
00050 class  StringCutsEventSelector : public EventSelector {
00051  public:
00052   StringCutsEventSelector(const edm::ParameterSet& pset) :
00053     EventSelector(pset),
00054     src_(edm::Service<InputTagDistributorService>()->retrieve("src",pset))
00055       {
00056         std::vector<std::string> selection=pset.getParameter<std::vector<std::string > >("cut");
00057         std::stringstream ss;
00058         ss<<"string cut based selection on collection: "<<src_;
00059         description_.push_back(ss.str());           ss.str("");
00060         description_.push_back("selection cuts are:");
00061         for (unsigned int i=0;i!=selection.size();i++)
00062           if (selection[i]!="-"){
00063             f_.push_back( new StringCutObjectSelector<Object>(selection[i]));
00064             ss<<"["<<i<<"]: "<<selection[i];
00065             description_.push_back(ss.str());           ss.str("");
00066           }
00067           else
00068             {
00069               f_.push_back(0);
00070               ss<<"["<<i<<"]: no selection";
00071               description_.push_back(ss.str());           ss.str("");
00072             }
00073       }
00074  ~StringCutsEventSelector(){unsigned int i=0; for (;i!=f_.size();i++) if (f_[i]){ delete f_[i];f_[i]=0;}}
00075  
00076     bool select (const edm::Event& e) const{
00077       edm::Handle<edm::View<Object> > oH;
00078       e.getByLabel(src_, oH);
00079       unsigned int i=0;
00080       if (existenceMatter && oH->size()<f_.size()) return false;
00081       for (;i!=f_.size();i++)
00082         {  
00083           if (!existenceMatter && i==oH->size()) break;
00084           if (!f_[i]) continue;
00085           const Object & o = (*oH)[i];
00086           if (!(*f_[i])(o)) return false;
00087         }
00088       return true;
00089     }
00090     
00091  private:
00092     edm::InputTag src_;
00093     std::vector<StringCutObjectSelector<Object> *> f_;
00094 };
00095 
00096 #endif