Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:17

0001 #ifndef _StringCutEventSelector
0002 #define _StringCutEventSelector
0003 
0004 #include "FWCore/Framework/interface/ConsumesCollector.h"
0005 #include "CommonTools/UtilAlgos/interface/EventSelector.h"
0006 #include "CommonTools/UtilAlgos/interface/InputTagDistributor.h"
0007 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0008 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0009 #include "FWCore/ServiceRegistry/interface/Service.h"
0010 
0011 template <typename Object, bool any = false>
0012 class StringCutEventSelector : public EventSelector {
0013 public:
0014   StringCutEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
0015       : StringCutEventSelector(pset, iC) {}
0016   StringCutEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector& iC)
0017       : EventSelector(pset, iC),
0018         src_(edm::Service<InputTagDistributorService>()->retrieve("src", pset)),
0019         srcToken_(iC.consumes<edm::View<Object> >(src_)),
0020         f_(pset.getParameter<std::string>("cut")),
0021         //put this guy to 0 to do the check on "all" object in the collection
0022         nFirst_(pset.getParameter<unsigned int>("nFirst")),
0023         order_(nullptr) {
0024     std::stringstream ss;
0025     ss << "string cut based selection on collection: " << src_;
0026     description_.push_back(ss.str());
0027     ss.str("");
0028     description_.push_back(std::string("selection cut is: ") + pset.getParameter<std::string>("cut"));
0029     if (pset.exists("order"))
0030       order_ = new StringObjectFunction<Object>(pset.getParameter<std::string>("order"));
0031   }
0032 
0033   bool select(const edm::Event& e) const override {
0034     edm::Handle<edm::View<Object> > oH;
0035     e.getByToken(srcToken_, oH);
0036     std::vector<const Object*> copyToSort(oH->size());
0037     for (uint i = 0; i != oH->size(); ++i)
0038       copyToSort[i] = &(*oH)[i];
0039     if (order_)
0040       std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(order_));
0041 
0042     //reject events if not enough object in collection
0043     //      if ((nFirst_!=0) && (oH->size()<nFirst_)) return false;
0044     unsigned int i = 0;
0045     unsigned int found = 0;
0046     for (; i != oH->size(); i++) {
0047       const Object& o = *(copyToSort[i]);
0048       if (any) {
0049         if (f_(o))
0050           ++found;
0051         if (found >= nFirst_)
0052           return true;
0053       } else {
0054         //stop doing the check if reaching too far in the collection
0055         if ((nFirst_ != 0) && (i >= nFirst_))
0056           break;
0057         if (!f_(o))
0058           return false;
0059       }
0060     }
0061     return !any;
0062   }
0063 
0064 private:
0065   edm::InputTag src_;
0066   edm::EDGetTokenT<edm::View<Object> > srcToken_;
0067   StringCutObjectSelector<Object> f_;
0068   unsigned int nFirst_;
0069   StringObjectFunction<Object>* order_;
0070 };
0071 
0072 template <typename Object, bool existenceMatter = true>
0073 class StringCutsEventSelector : public EventSelector {
0074 public:
0075   StringCutsEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
0076       : StringCutsEventSelector(pset, iC) {}
0077   StringCutsEventSelector(const edm::ParameterSet& pset, edm::ConsumesCollector& iC)
0078       : EventSelector(pset, iC),
0079         src_(edm::Service<InputTagDistributorService>()->retrieve("src", pset)),
0080         srcToken_(iC.consumes<edm::View<Object> >(src_)),
0081         order_(nullptr) {
0082     std::vector<std::string> selection = pset.getParameter<std::vector<std::string> >("cut");
0083     std::stringstream ss;
0084     ss << "string cut based selection on collection: " << src_;
0085     description_.push_back(ss.str());
0086     ss.str("");
0087     description_.push_back("selection cuts are:");
0088     for (unsigned int i = 0; i != selection.size(); i++)
0089       if (selection[i] != "-") {
0090         f_.push_back(new StringCutObjectSelector<Object>(selection[i]));
0091         ss << "[" << i << "]: " << selection[i];
0092         description_.push_back(ss.str());
0093         ss.str("");
0094       } else {
0095         f_.push_back(nullptr);
0096         ss << "[" << i << "]: no selection";
0097         description_.push_back(ss.str());
0098         ss.str("");
0099       }
0100     if (pset.exists("order"))
0101       order_ = new StringObjectFunction<Object>(pset.getParameter<std::string>("order"));
0102   }
0103   ~StringCutsEventSelector() override {
0104     unsigned int i = 0;
0105     for (; i != f_.size(); i++)
0106       if (f_[i]) {
0107         delete f_[i];
0108         f_[i] = nullptr;
0109       }
0110     if (order_)
0111       delete order_;
0112   }
0113 
0114   bool select(const edm::Event& e) const override {
0115     edm::Handle<edm::View<Object> > oH;
0116     e.getByToken(srcToken_, oH);
0117     std::vector<const Object*> copyToSort(oH->size());
0118     for (uint i = 0; i != oH->size(); ++i)
0119       copyToSort[i] = &(*oH)[i];
0120     if (order_)
0121       std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(order_));
0122 
0123     unsigned int i = 0;
0124     if (existenceMatter && oH->size() < f_.size())
0125       return false;
0126     for (; i != f_.size(); i++) {
0127       if (!existenceMatter && i == oH->size())
0128         break;
0129       if (!f_[i])
0130         continue;
0131       const Object& o = *(copyToSort[i]);
0132       if (!(*f_[i])(o))
0133         return false;
0134     }
0135     return true;
0136   }
0137 
0138 private:
0139   edm::InputTag src_;
0140   edm::EDGetTokenT<edm::View<Object> > srcToken_;
0141   std::vector<StringCutObjectSelector<Object>*> f_;
0142   StringObjectFunction<Object>* order_;
0143 };
0144 
0145 #endif