CMS 3D CMS Logo

FwdPtrCollectionFilter.h
Go to the documentation of this file.
1 #ifndef CommonTools_UtilAlgos_FwdPtrCollectionFilter_h
2 #define CommonTools_UtilAlgos_FwdPtrCollectionFilter_h
3 
19 
20 #include <algorithm>
21 #include <vector>
22 
23 namespace edm {
24 
25  template <class T, class S, class H = ProductFromFwdPtrFactory<T>>
27  public :
28 
30  srcToken_{consumes<std::vector<edm::FwdPtr<T>>>(ps.getParameter<edm::InputTag>("src"))},
31  srcViewToken_{mayConsume<edm::View<T>>(ps.getParameter<edm::InputTag>("src"))},
32  filter_{ps.exists("filter") ? ps.getParameter<bool>("filter") : false},
33  makeClones_{ps.exists("makeClones") ? ps.getParameter<bool>("makeClones") : false},
34  selector_{ps}
35  {
36  produces<std::vector<edm::FwdPtr<T>>>();
37  if (makeClones_) {
38  produces<std::vector<T>>();
39  }
40  }
41 
42  bool filter(edm::Event& iEvent, edm::EventSetup const& iSetup) override
43  {
44  auto pOutput = std::make_unique<std::vector<edm::FwdPtr<T>>>();
45 
46  // First try to access as a vector<FwdPtr<T>>; otherwise try as a View<T>.
48  if (iEvent.getByToken(srcToken_, hSrcAsFwdPtr)) {
49  std::copy_if(std::cbegin(*hSrcAsFwdPtr), std::cend(*hSrcAsFwdPtr), std::back_inserter(*pOutput),
50  [this](auto const ptr) { return selector_(*ptr); });
51  }
52  else {
53  edm::Handle<edm::View<T>> hSrcAsView;
54  iEvent.getByToken(srcViewToken_, hSrcAsView);
55  for (auto ibegin = std::cbegin(*hSrcAsView), iend = std::cend(*hSrcAsView), i = ibegin; i!= iend; ++i) {
56  if (selector_(*i)) {
57  auto const p = hSrcAsView->ptrAt(i-ibegin);
58  pOutput->emplace_back(p,p);
59  }
60  }
61  }
62 
63  // Must form pClones *before* std::move(pOutput) has been called.
64  if (makeClones_) {
65  H factory;
66  auto pClones = std::make_unique<std::vector<T>>();
67  std::transform(std::cbegin(*pOutput), std::cend(*pOutput), std::back_inserter(*pClones),
68  [&factory](auto ptr){ return factory(ptr); });
69  iEvent.put(std::move(pClones));
70  }
71 
72  bool const pass {!pOutput->empty()};
73  iEvent.put(std::move(pOutput));
74 
75  return filter_ ? pass : true;
76  }
77 
78  protected :
81  bool const filter_;
82  bool const makeClones_;
84  };
85 }
86 
87 #endif
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< std::vector< edm::FwdPtr< T > > > const srcToken_
FwdPtrCollectionFilter(edm::ParameterSet const &ps)
bool filter(edm::Event &iEvent, edm::EventSetup const &iSetup) override
Selects a list of FwdPtr&#39;s to a product T (templated) that satisfy a method S(T) (templated). Can also handle input as View<T>. Can also have a factory class to create new instances of clones if desired.
HLT enums.
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< edm::View< T > > const srcViewToken_