CMS 3D CMS Logo

StringCutEventSelector.h
Go to the documentation of this file.
1 #ifndef _StringCutEventSelector
2 #define _StringCutEventSelector
3 
10 
11 template <typename Object, bool any = false>
13 public:
17  : EventSelector(pset, iC),
18  src_(edm::Service<InputTagDistributorService>()->retrieve("src", pset)),
19  srcToken_(iC.consumes<edm::View<Object> >(src_)),
20  f_(pset.getParameter<std::string>("cut")),
21  //put this guy to 0 to do the check on "all" object in the collection
22  nFirst_(pset.getParameter<unsigned int>("nFirst")),
23  order_(nullptr) {
24  std::stringstream ss;
25  ss << "string cut based selection on collection: " << src_;
26  description_.push_back(ss.str());
27  ss.str("");
28  description_.push_back(std::string("selection cut is: ") + pset.getParameter<std::string>("cut"));
29  if (pset.exists("order"))
30  order_ = new StringObjectFunction<Object>(pset.getParameter<std::string>("order"));
31  }
32 
33  bool select(const edm::Event& e) const override {
35  e.getByToken(srcToken_, oH);
36  std::vector<const Object*> copyToSort(oH->size());
37  for (uint i = 0; i != oH->size(); ++i)
38  copyToSort[i] = &(*oH)[i];
39  if (order_)
40  std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(order_));
41 
42  //reject events if not enough object in collection
43  // if ((nFirst_!=0) && (oH->size()<nFirst_)) return false;
44  unsigned int i = 0;
45  unsigned int found = 0;
46  for (; i != oH->size(); i++) {
47  const Object& o = *(copyToSort[i]);
48  if (any) {
49  if (f_(o))
50  ++found;
51  if (found >= nFirst_)
52  return true;
53  } else {
54  //stop doing the check if reaching too far in the collection
55  if ((nFirst_ != 0) && (i >= nFirst_))
56  break;
57  if (!f_(o))
58  return false;
59  }
60  }
61  return !any;
62  }
63 
64 private:
68  unsigned int nFirst_;
70 };
71 
72 template <typename Object, bool existenceMatter = true>
74 public:
78  : EventSelector(pset, iC),
79  src_(edm::Service<InputTagDistributorService>()->retrieve("src", pset)),
80  srcToken_(iC.consumes<edm::View<Object> >(src_)),
81  order_(nullptr) {
82  std::vector<std::string> selection = pset.getParameter<std::vector<std::string> >("cut");
83  std::stringstream ss;
84  ss << "string cut based selection on collection: " << src_;
85  description_.push_back(ss.str());
86  ss.str("");
87  description_.push_back("selection cuts are:");
88  for (unsigned int i = 0; i != selection.size(); i++)
89  if (selection[i] != "-") {
91  ss << "[" << i << "]: " << selection[i];
92  description_.push_back(ss.str());
93  ss.str("");
94  } else {
95  f_.push_back(nullptr);
96  ss << "[" << i << "]: no selection";
97  description_.push_back(ss.str());
98  ss.str("");
99  }
100  if (pset.exists("order"))
101  order_ = new StringObjectFunction<Object>(pset.getParameter<std::string>("order"));
102  }
104  unsigned int i = 0;
105  for (; i != f_.size(); i++)
106  if (f_[i]) {
107  delete f_[i];
108  f_[i] = nullptr;
109  }
110  if (order_)
111  delete order_;
112  }
113 
114  bool select(const edm::Event& e) const override {
116  e.getByToken(srcToken_, oH);
117  std::vector<const Object*> copyToSort(oH->size());
118  for (uint i = 0; i != oH->size(); ++i)
119  copyToSort[i] = &(*oH)[i];
120  if (order_)
121  std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(order_));
122 
123  unsigned int i = 0;
124  if (existenceMatter && oH->size() < f_.size())
125  return false;
126  for (; i != f_.size(); i++) {
127  if (!existenceMatter && i == oH->size())
128  break;
129  if (!f_[i])
130  continue;
131  const Object& o = *(copyToSort[i]);
132  if (!(*f_[i])(o))
133  return false;
134  }
135  return true;
136  }
137 
138 private:
141  std::vector<StringCutObjectSelector<Object>*> f_;
143 };
144 
145 #endif
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:36
selection
main part
Definition: corrVsCorr.py:100
bool select(const edm::Event &e) const override
decision of the selector module
StringCutsEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &&iC)
StringCutEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &&iC)
std::vector< StringCutObjectSelector< Object > * > f_
StringObjectFunction< Object > * order_
edm::EDGetTokenT< edm::View< Object > > srcToken_
bool select(const edm::Event &e) const override
decision of the selector module
edm::EDGetTokenT< edm::View< Object > > srcToken_
A selector of events.
Definition: EventSelector.h:16
StringObjectFunction< Object > * order_
HLT enums.
StringCutObjectSelector< Object > f_
std::vector< std::string > description_
Definition: EventSelector.h:33
StringCutsEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &iC)
StringCutEventSelector(const edm::ParameterSet &pset, edm::ConsumesCollector &iC)