CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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
27 
30 
37 
39 
40 
41 #include <memory>
42 #include <vector>
43 #include <sstream>
44 
45 
47 // class definition
49 template<typename T1, typename T2>
51 {
52 public:
53  // construction/destruction
54  ObjectViewMatcher(const edm::ParameterSet& iConfig);
55  virtual ~ObjectViewMatcher();
56 
57  // member functions
58  void produce(edm::Event& iEvent,const edm::EventSetup& iSetup) override;
59  void endJob();
60 
61 private:
62  // member data
64  std::vector<edm::InputTag> srcObjects_;
65  double deltaRMax_;
66 
68 
69  StringCutObjectSelector<T1,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
70  StringCutObjectSelector<T2,true> objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
71 
72 
73  unsigned int nObjectsTot_;
74  unsigned int nObjectsMatch_;
75 };
76 
77 
78 
80 // construction/destruction
82 
83 //______________________________________________________________________________
84 template<typename T1, typename T2>
86  : srcCands_ (iConfig.getParameter<edm::InputTag> ("srcObject"))
87  , srcObjects_ (iConfig.getParameter<std::vector<edm::InputTag> >("srcObjectsToMatch"))
88  , deltaRMax_ (iConfig.getParameter<double> ("deltaRMax"))
89  , moduleLabel_(iConfig.getParameter<std::string> ("@module_label"))
90  , objCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
91  ,objMatchCut_(iConfig.existsAs<std::string>("srcObjectsToMatchSelection") ? iConfig.getParameter<std::string>("srcObjectsToMatchSelection") : "", true)
92  , nObjectsTot_(0)
93  , nObjectsMatch_(0)
94 {
95  produces<std::vector<T1> >();
96 }
97 
98 
99 //______________________________________________________________________________
100 template<typename T1, typename T2>
102 
103 
104 
106 // implementation of member functions
108 
109 //______________________________________________________________________________
110 template<typename T1, typename T2>
112 {
113  std::auto_ptr<std::vector<T1> > cleanObjects(new std::vector<T1 >);
114 
115  edm::Handle<edm::View<T1> > candidates;
116  iEvent.getByLabel(srcCands_,candidates);
117 
118  bool* isMatch = new bool[candidates->size()];
119  for (unsigned int iObject=0;iObject<candidates->size();iObject++) isMatch[iObject] = false;
120 
121  for (unsigned int iSrc=0;iSrc<srcObjects_.size();iSrc++) {
122  edm::Handle<edm::View<T2> > objects;
123  iEvent.getByLabel(srcObjects_[iSrc],objects);
124 
125  if(objects->size()==0) continue;
126 
127  for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
128  const T1& candidate = candidates->at(iObject);
129  if (!objCut_(candidate)) continue;
130 
131 
132  for (unsigned int iObj=0;iObj<objects->size();iObj++) {
133  const T2& obj = objects->at(iObj);
134  if (!objMatchCut_(obj)) continue;
135  double deltaR = reco::deltaR(candidate,obj);
136  if (deltaR<deltaRMax_) isMatch[iObject] = true;
137  }
138  }
139  }
140 
141 
142 
143  unsigned int counter=0;
144  typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
145  for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
146  if(isMatch[counter]) cleanObjects->push_back( *tIt );
147  }
148 
149  nObjectsTot_ +=candidates->size();
150  nObjectsMatch_+=cleanObjects->size();
151 
152  delete [] isMatch;
153  iEvent.put(cleanObjects);
154 }
155 
156 
157 //______________________________________________________________________________
158 template<typename T1, typename T2>
160 {
161  std::stringstream ss;
162  ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsMatched="<<nObjectsMatch_
163  <<" fObjectsMatch="<<100.*(nObjectsMatch_/(double)nObjectsTot_)<<"%\n";
164  std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
165  <<"\n"<<moduleLabel_<<"(ObjectViewMatcher) SUMMARY:\n"<<ss.str()
166  <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
167  << std::endl;
168 }
169 
170 
172 // plugin definition
174 
177 
edm::InputTag srcCands_
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
unsigned int nObjectsMatch_
unsigned int nObjectsTot_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
StringCutObjectSelector< T2, true > objMatchCut_
std::string moduleLabel_
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
int iEvent
Definition: GenABIO.cc:243
ObjectViewMatcher< reco::Photon, reco::Track > TrackMatchedPhotonProducer
ObjectViewMatcher< reco::Jet, reco::Track > TrackMatchedJetProducer
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
ObjectViewMatcher(const edm::ParameterSet &iConfig)
tuple cout
Definition: gather_cfg.py:121
std::vector< edm::InputTag > srcObjects_
StringCutObjectSelector< T1, true > objCut_