CMS 3D CMS Logo

ObjectViewMatcher.cc
Go to the documentation of this file.
1 
2 /*****************************************************************************
3  * Project: CMS detector at the CERN
4  *
5  * Package: PhysicsTools/TagAndProbe
6  *
7  *
8  * Authors:
9  *
10  * Kalanand Mishra, Fermilab - kalanand@fnal.gov
11  *
12  * Description:
13  * - Matches a given object with other objects using deltaR-matching.
14  * - For example: can match a photon with track within a given deltaR.
15  * - Saves collection of the reference vectors of matched objects.
16  * History:
17  *
18  *
19  *****************************************************************************/
21 // Includes
30 
33 
40 
42 
43 #include <memory>
44 #include <vector>
45 #include <sstream>
46 
48 // class definition
50 
51 namespace ovm {
52  template <typename T1, typename T2>
53  struct StreamCache {
56  objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
58  objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
59  };
60 } // namespace ovm
61 
62 template <typename T1, typename T2>
63 class ObjectViewMatcher : public edm::global::EDProducer<edm::StreamCache<ovm::StreamCache<T1, T2>>> {
64 public:
65  // construction/destruction
66  ObjectViewMatcher(const edm::ParameterSet& iConfig);
67  ~ObjectViewMatcher() override;
68 
69  // member functions
70  std::unique_ptr<ovm::StreamCache<T1, T2>> beginStream(edm::StreamID) const override;
71  void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
72  void endJob() override;
73 
75 
76 private:
77  // member data
79  std::vector<edm::EDGetTokenT<edm::View<T2>>> srcObjectsTokens_;
80  double deltaRMax_;
81 
85 
86  mutable std::atomic<unsigned int> nObjectsTot_;
87  mutable std::atomic<unsigned int> nObjectsMatch_;
88 };
89 
91 // construction/destruction
93 
94 //______________________________________________________________________________
95 template <typename T1, typename T2>
97  : srcCandsToken_(this->consumes(iConfig.getParameter<edm::InputTag>("srcObject"))),
98  srcObjectsTokens_(edm::vector_transform(
99  iConfig.getParameter<std::vector<edm::InputTag>>("srcObjectsToMatch"),
100  [this](edm::InputTag const& tag) { return this->template consumes<edm::View<T2>>(tag); })),
101  deltaRMax_(iConfig.getParameter<double>("deltaRMax")),
102  moduleLabel_(iConfig.getParameter<std::string>("@module_label")),
103  cut_(iConfig.getParameter<std::string>("srcObjectSelection")),
104  match_(iConfig.getParameter<std::string>("srcObjectsToMatchSelection")),
105  nObjectsTot_(0),
106  nObjectsMatch_(0) {
107  this->template produces<std::vector<T1>>();
108 }
109 
110 //______________________________________________________________________________
111 template <typename T1, typename T2>
113 
115 // implementation of member functions
117 
118 //______________________________________________________________________________
119 template <typename T1, typename T2>
120 std::unique_ptr<ovm::StreamCache<T1, T2>> ObjectViewMatcher<T1, T2>::beginStream(edm::StreamID) const {
121  return std::make_unique<ovm::StreamCache<T1, T2>>(cut_, match_);
122 }
123 
124 //______________________________________________________________________________
125 template <typename T1, typename T2>
127  auto cleanObjects = std::make_unique<std::vector<T1>>();
128 
130  iEvent.getByToken(srcCandsToken_, candidates);
131 
132  bool* isMatch = new bool[candidates->size()];
133  for (unsigned int iObject = 0; iObject < candidates->size(); iObject++)
134  isMatch[iObject] = false;
135 
136  auto& objCut = this->streamCache(iID)->objCut_;
137  auto& objMatchCut = this->streamCache(iID)->objMatchCut_;
138  for (unsigned int iSrc = 0; iSrc < srcObjectsTokens_.size(); iSrc++) {
140  iEvent.getByToken(srcObjectsTokens_[iSrc], objects);
141 
142  if (objects->empty())
143  continue;
144 
145  for (unsigned int iObject = 0; iObject < candidates->size(); iObject++) {
146  const T1& candidate = candidates->at(iObject);
147  if (!objCut(candidate))
148  continue;
149 
150  for (unsigned int iObj = 0; iObj < objects->size(); iObj++) {
151  const T2& obj = objects->at(iObj);
152  if (!objMatchCut(obj))
153  continue;
154  double deltaR = reco::deltaR(candidate, obj);
155  if (deltaR < deltaRMax_)
156  isMatch[iObject] = true;
157  }
158  }
159  }
160 
161  unsigned int counter = 0;
162  typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
163  for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
164  if (isMatch[counter])
165  cleanObjects->push_back(*tIt);
166  }
167 
168  nObjectsTot_ += candidates->size();
169  nObjectsMatch_ += cleanObjects->size();
170 
171  delete[] isMatch;
172  iEvent.put(std::move(cleanObjects));
173 }
174 
175 //______________________________________________________________________________
176 template <typename T1, typename T2>
178  std::stringstream ss;
179  ss << "nObjectsTot=" << nObjectsTot_ << " nObjectsMatched=" << nObjectsMatch_
180  << " fObjectsMatch=" << 100. * (nObjectsMatch_ / (double)nObjectsTot_) << "%\n";
181  std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++"
182  << "\n"
183  << moduleLabel_ << "(ObjectViewMatcher) SUMMARY:\n"
184  << ss.str() << "++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
185 }
186 
187 //______________________________________________________________________________
188 template <typename T1, typename T2>
191  pset.add<edm::InputTag>("srcObject");
192  pset.add<std::vector<edm::InputTag>>("srcObjectsToMatch");
193  pset.add<double>("deltaRMax");
194  pset.add<std::string>("srcObjectSelection", "");
195  pset.add<std::string>("srcObjectsToMatchSelection", "");
196 
197  desc.addDefault(pset);
198 }
199 
201 // plugin definition
203 
206 
std::unique_ptr< ovm::StreamCache< T1, T2 > > beginStream(edm::StreamID) const override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< edm::EDGetTokenT< edm::View< T2 > > > srcObjectsTokens_
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
std::string moduleLabel_
edm::EDGetTokenT< edm::View< T1 > > srcCandsToken_
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
static void fillDescriptions(edm::ConfigurationDescriptions &)
StringCutObjectSelector< T1, true > objCut_
StreamCache(std::string const &cut, std::string const &match)
std::atomic< unsigned int > nObjectsMatch_
StringCutObjectSelector< T2, true > objMatchCut_
int iEvent
Definition: GenABIO.cc:224
ObjectViewMatcher< reco::Photon, reco::Track > TrackMatchedPhotonProducer
ObjectViewMatcher< reco::Jet, reco::Track > TrackMatchedJetProducer
std::atomic< unsigned int > nObjectsTot_
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
ObjectViewMatcher(const edm::ParameterSet &iConfig)
void endJob() override
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
static std::atomic< unsigned int > counter
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
def move(src, dest)
Definition: eostools.py:511
~ObjectViewMatcher() override