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
28 
31 
38 
40 
41 #include <memory>
42 #include <vector>
43 #include <sstream>
44 
46 // class definition
48 template <typename T1, typename T2>
49 class ObjectViewMatcher : public edm::EDProducer {
50 public:
51  // construction/destruction
52  ObjectViewMatcher(const edm::ParameterSet& iConfig);
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 
counter
Definition: counter.py:1
Muon.h
TrackMatchedPhotonProducer
ObjectViewMatcher< reco::Photon, reco::Track > TrackMatchedPhotonProducer
Definition: ObjectViewMatcher.cc:173
EDProducer.h
TrackMatchedJetProducer
ObjectViewMatcher< reco::Jet, reco::Track > TrackMatchedJetProducer
Definition: ObjectViewMatcher.cc:174
sistrip::View
View
Definition: ConstantsForView.h:26
ObjectViewMatcher::srcCandsToken_
edm::EDGetTokenT< edm::View< T1 > > srcCandsToken_
Definition: ObjectViewMatcher.cc:76
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
objects
Definition: __init__.py:1
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
ObjectViewMatcher::objCut_
StringCutObjectSelector< T1, true > objCut_
Definition: ObjectViewMatcher.cc:82
ObjectViewMatcher::~ObjectViewMatcher
~ObjectViewMatcher() override
Definition: ObjectViewMatcher.cc:102
Jet.h
ObjectViewMatcher::nObjectsTot_
unsigned int nObjectsTot_
Definition: ObjectViewMatcher.cc:86
watchdog.const
const
Definition: watchdog.py:83
edm::Handle
Definition: AssociativeIterator.h:50
ObjectViewMatcher::deltaRMax_
double deltaRMax_
Definition: ObjectViewMatcher.cc:78
ObjectViewMatcher::objMatchCut_
StringCutObjectSelector< T2, true > objMatchCut_
Definition: ObjectViewMatcher.cc:84
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
MakerMacros.h
Photon.h
Track.h
ObjectViewMatcher::srcObjectsTokens_
std::vector< edm::EDGetTokenT< edm::View< T2 > > > srcObjectsTokens_
Definition: ObjectViewMatcher.cc:77
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
GsfElectron.h
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::vector_transform
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
ObjectViewMatcher::produce
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: ObjectViewMatcher.cc:110
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
ObjectViewMatcher::endJob
void endJob() override
Definition: ObjectViewMatcher.cc:159
iEvent
int iEvent
Definition: GenABIO.cc:224
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
counter
static std::atomic< unsigned int > counter
Definition: SharedResourceNames.cc:18
edm::EventSetup
Definition: EventSetup.h:58
hgcalPerformanceValidation.objects
objects
Definition: hgcalPerformanceValidation.py:791
InputTag.h
Electron.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
StringCutObjectSelector.h
transform.h
HLT_FULL_cff.candidates
candidates
Definition: HLT_FULL_cff.py:54992
StringCutObjectSelector< T1, true >
reco::deltaR
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
edm::EDProducer
Definition: EDProducer.h:35
ObjectViewMatcher::nObjectsMatch_
unsigned int nObjectsMatch_
Definition: ObjectViewMatcher.cc:87
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
ObjectViewMatcher
Definition: ObjectViewMatcher.cc:48
View.h
ParameterSet.h
edm::Event
Definition: Event.h:73
ObjectViewMatcher::ObjectViewMatcher
ObjectViewMatcher(const edm::ParameterSet &iConfig)
Definition: ObjectViewMatcher.cc:80
ObjectViewMatcher::moduleLabel_
std::string moduleLabel_
Definition: ObjectViewMatcher.cc:80