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
28 
31 
38 
40 
41 
42 #include <memory>
43 #include <vector>
44 #include <sstream>
45 
46 
48 // class definition
50 template<typename T1, typename T2>
52 {
53 public:
54  // construction/destruction
55  ObjectViewMatcher(const edm::ParameterSet& iConfig);
56  virtual ~ObjectViewMatcher();
57 
58  // member functions
59  void produce(edm::Event& iEvent,const edm::EventSetup& iSetup) override;
60  void endJob() override;
61 
62 private:
63  // member data
65  std::vector<edm::EDGetTokenT<edm::View<T2> > > srcObjectsTokens_;
66  double deltaRMax_;
67 
69 
70  StringCutObjectSelector<T1,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
71  StringCutObjectSelector<T2,true> objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
72 
73 
74  unsigned int nObjectsTot_;
75  unsigned int nObjectsMatch_;
76 };
77 
78 
79 
81 // construction/destruction
83 
84 //______________________________________________________________________________
85 template<typename T1, typename T2>
87  : srcCandsToken_ (consumes<edm::View<T1> >(iConfig.getParameter<edm::InputTag> ("srcObject")))
88  , srcObjectsTokens_ (edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >("srcObjectsToMatch"), [this](edm::InputTag const & tag){return consumes<edm::View<T2> >(tag);}))
89  , deltaRMax_ (iConfig.getParameter<double> ("deltaRMax"))
90  , moduleLabel_(iConfig.getParameter<std::string> ("@module_label"))
91  , objCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
92  ,objMatchCut_(iConfig.existsAs<std::string>("srcObjectsToMatchSelection") ? iConfig.getParameter<std::string>("srcObjectsToMatchSelection") : "", true)
93  , nObjectsTot_(0)
95 {
96  produces<std::vector<T1> >();
97 }
98 
99 
100 //______________________________________________________________________________
101 template<typename T1, typename T2>
103 
104 
105 
107 // implementation of member functions
109 
110 //______________________________________________________________________________
111 template<typename T1, typename T2>
113 {
114  std::auto_ptr<std::vector<T1> > cleanObjects(new std::vector<T1 >);
115 
117  iEvent.getByToken(srcCandsToken_,candidates);
118 
119  bool* isMatch = new bool[candidates->size()];
120  for (unsigned int iObject=0;iObject<candidates->size();iObject++) isMatch[iObject] = false;
121 
122  for (unsigned int iSrc=0;iSrc<srcObjectsTokens_.size();iSrc++) {
123  edm::Handle<edm::View<T2> > objects;
124  iEvent.getByToken(srcObjectsTokens_[iSrc],objects);
125 
126  if(objects->size()==0) continue;
127 
128  for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
129  const T1& candidate = candidates->at(iObject);
130  if (!objCut_(candidate)) continue;
131 
132 
133  for (unsigned int iObj=0;iObj<objects->size();iObj++) {
134  const T2& obj = objects->at(iObj);
135  if (!objMatchCut_(obj)) continue;
136  double deltaR = reco::deltaR(candidate,obj);
137  if (deltaR<deltaRMax_) isMatch[iObject] = true;
138  }
139  }
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]) cleanObjects->push_back( *tIt );
148  }
149 
150  nObjectsTot_ +=candidates->size();
151  nObjectsMatch_+=cleanObjects->size();
152 
153  delete [] isMatch;
154  iEvent.put(cleanObjects);
155 }
156 
157 
158 //______________________________________________________________________________
159 template<typename T1, typename T2>
161 {
162  std::stringstream ss;
163  ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsMatched="<<nObjectsMatch_
164  <<" fObjectsMatch="<<100.*(nObjectsMatch_/(double)nObjectsTot_)<<"%\n";
165  std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
166  <<"\n"<<moduleLabel_<<"(ObjectViewMatcher) SUMMARY:\n"<<ss.str()
167  <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
168  << std::endl;
169 }
170 
171 
173 // plugin definition
175 
178 
unsigned int nObjectsMatch_
unsigned int nObjectsTot_
nObjectsTot_(0)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
StringCutObjectSelector< T2, true > objMatchCut_
std::vector< edm::EDGetTokenT< edm::View< T2 > > > srcObjectsTokens_
std::string moduleLabel_
objCut_(iConfig.existsAs< std::string >("srcObjectSelection")?iConfig.getParameter< std::string >("srcObjectSelection"):"", true)
objMatchCut_(iConfig.existsAs< std::string >("srcObjectsToMatchSelection")?iConfig.getParameter< std::string >("srcObjectsToMatchSelection"):"", true)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
double deltaR(const T1 &t1, const T2 &t2)
Definition: deltaR.h:48
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
int iEvent
Definition: GenABIO.cc:230
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:113
nObjectsMatch_(0)
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
ObjectViewMatcher(const edm::ParameterSet &iConfig)
string const
Definition: compareJSON.py:14
void endJob() override
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
static std::atomic< unsigned int > counter
tuple cout
Definition: gather_cfg.py:121
moduleLabel_(iConfig.getParameter< string >("@module_label"))
StringCutObjectSelector< T1, true > objCut_
deltaRMax_(iConfig.getParameter< double >("deltaRMax"))