CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
28 
31 
38 
40 
41 #include <memory>
42 #include <vector>
43 #include <sstream>
44 
46 // class definition
48 template <typename T1, typename T2>
50 public:
51  // construction/destruction
53  ~ObjectViewMatcher() override;
54 
55  // member functions
56  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
57  void endJob() override;
58 
59 private:
60  // member data
62  std::vector<edm::EDGetTokenT<edm::View<T2>>> srcObjectsTokens_;
63  double deltaRMax_;
64 
66 
67  StringCutObjectSelector<T1, true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
69  objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
70 
71  unsigned int nObjectsTot_;
72  unsigned int nObjectsMatch_;
73 };
74 
76 // construction/destruction
78 
79 //______________________________________________________________________________
80 template <typename T1, typename T2>
82  : srcCandsToken_(consumes<edm::View<T1>>(iConfig.getParameter<edm::InputTag>("srcObject"))),
83  srcObjectsTokens_(
84  edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag>>("srcObjectsToMatch"),
85  [this](edm::InputTag const& tag) { return consumes<edm::View<T2>>(tag); })),
86  deltaRMax_(iConfig.getParameter<double>("deltaRMax")),
87  moduleLabel_(iConfig.getParameter<std::string>("@module_label")),
88  objCut_(iConfig.existsAs<std::string>("srcObjectSelection")
89  ? iConfig.getParameter<std::string>("srcObjectSelection")
90  : "",
91  true),
92  objMatchCut_(iConfig.existsAs<std::string>("srcObjectsToMatchSelection")
93  ? iConfig.getParameter<std::string>("srcObjectsToMatchSelection")
94  : "",
95  true),
96  nObjectsTot_(0),
97  nObjectsMatch_(0) {
98  produces<std::vector<T1>>();
99 }
100 
101 //______________________________________________________________________________
102 template <typename T1, typename T2>
104 
106 // implementation of member functions
108 
109 //______________________________________________________________________________
110 template <typename T1, typename T2>
112  auto cleanObjects = std::make_unique<std::vector<T1>>();
113 
115  iEvent.getByToken(srcCandsToken_, candidates);
116 
117  bool* isMatch = new bool[candidates->size()];
118  for (unsigned int iObject = 0; iObject < candidates->size(); iObject++)
119  isMatch[iObject] = false;
120 
121  for (unsigned int iSrc = 0; iSrc < srcObjectsTokens_.size(); iSrc++) {
123  iEvent.getByToken(srcObjectsTokens_[iSrc], objects);
124 
125  if (objects->empty())
126  continue;
127 
128  for (unsigned int iObject = 0; iObject < candidates->size(); iObject++) {
129  const T1& candidate = candidates->at(iObject);
130  if (!objCut_(candidate))
131  continue;
132 
133  for (unsigned int iObj = 0; iObj < objects->size(); iObj++) {
134  const T2& obj = objects->at(iObj);
135  if (!objMatchCut_(obj))
136  continue;
137  double deltaR = reco::deltaR(candidate, obj);
138  if (deltaR < deltaRMax_)
139  isMatch[iObject] = true;
140  }
141  }
142  }
143 
144  unsigned int counter = 0;
145  typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
146  for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
147  if (isMatch[counter])
148  cleanObjects->push_back(*tIt);
149  }
150 
151  nObjectsTot_ += candidates->size();
152  nObjectsMatch_ += cleanObjects->size();
153 
154  delete[] isMatch;
155  iEvent.put(std::move(cleanObjects));
156 }
157 
158 //______________________________________________________________________________
159 template <typename T1, typename T2>
161  std::stringstream ss;
162  ss << "nObjectsTot=" << nObjectsTot_ << " nObjectsMatched=" << nObjectsMatch_
163  << " fObjectsMatch=" << 100. * (nObjectsMatch_ / (double)nObjectsTot_) << "%\n";
164  std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++"
165  << "\n"
166  << moduleLabel_ << "(ObjectViewMatcher) SUMMARY:\n"
167  << ss.str() << "++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
168 }
169 
171 // plugin definition
173 
176 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
StringCutObjectSelector< T2, true > objMatchCut_
unsigned int nObjectsMatch_
unsigned int nObjectsTot_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< edm::EDGetTokenT< edm::View< T2 > > > srcObjectsTokens_
std::string moduleLabel_
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
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
StringCutObjectSelector< T1, true > objCut_
int iEvent
Definition: GenABIO.cc:224
const_iterator begin() const
ObjectViewMatcher< reco::Photon, reco::Track > TrackMatchedPhotonProducer
ObjectViewMatcher< reco::Jet, reco::Track > TrackMatchedJetProducer
def move
Definition: eostools.py:511
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
ObjectViewMatcher(const edm::ParameterSet &iConfig)
void endJob() override
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
static std::atomic< unsigned int > counter
tuple cout
Definition: gather_cfg.py:144
moduleLabel_(iConfig.getParameter< string >("@module_label"))
~ObjectViewMatcher() override
deltaRMax_(iConfig.getParameter< double >("deltaRMax"))