CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
JetDeltaRValueMapProducer.cc
Go to the documentation of this file.
1 /* \class JetDeltaRValueMapProducer
2  *
3  * Associates jets using delta-R matching, and writes out
4  * a valuemap of single float variables based on a StringObjectFunction.
5  * This is used for miniAOD.
6  *
7  * \author: Sal Rappoccio
8  *
9  *
10  * for more details about the cut syntax, see the documentation
11  * page below:
12  *
13  * https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsCutParser
14  *
15  *
16  */
17 
23 
29 
30 template <class T, class C = T>
32 public:
34 
36  : srcToken_(consumes<typename edm::View<T> >(params.getParameter<edm::InputTag>("src"))),
37  matchedToken_(consumes<typename edm::View<C> >(params.getParameter<edm::InputTag>("matched"))),
38  distMax_(params.getParameter<double>("distMax")),
39  value_(params.existsAs<std::string>("value") ? params.getParameter<std::string>("value") : ""),
40  values_(params.existsAs<std::vector<std::string> >("values")
41  ? params.getParameter<std::vector<std::string> >("values")
42  : std::vector<std::string>()),
43  valueLabels_(params.existsAs<std::vector<std::string> >("valueLabels")
44  ? params.getParameter<std::vector<std::string> >("valueLabels")
45  : std::vector<std::string>()),
46  lazyParser_(params.existsAs<bool>("lazyParser") ? params.getParameter<bool>("lazyParser") : false),
48  if (!value_.empty()) {
49  evaluationMap_.insert(std::make_pair(
51  produces<JetValueMap>();
52  }
53 
54  if (!valueLabels_.empty() || !values_.empty()) {
55  if (valueLabels_.size() == values_.size()) {
56  multiValue_ = true;
57  for (size_t i = 0; i < valueLabels_.size(); ++i) {
58  evaluationMap_.insert(std::make_pair(
59  valueLabels_[i],
61  produces<JetValueMap>(valueLabels_[i]);
62  }
63  } else
64  edm::LogWarning("ValueLabelMismatch")
65  << "The number of value labels does not match the number of values. Values will not be evaluated.";
66  }
67  }
68 
70 
71 private:
72  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
74  iEvent.getByToken(srcToken_, h_jets1);
76  iEvent.getByToken(matchedToken_, h_jets2);
77 
78  std::vector<float> values(h_jets1->size(), -99999);
79  std::map<std::string, std::vector<float> > valuesMap;
80  if (multiValue_) {
81  for (size_t i = 0; i < valueLabels_.size(); ++i)
82  valuesMap.insert(std::make_pair(valueLabels_[i], std::vector<float>(h_jets1->size(), -99999)));
83  }
84  std::vector<bool> jets1_locks(h_jets1->size(), false);
85 
86  for (typename edm::View<C>::const_iterator ibegin = h_jets2->begin(), iend = h_jets2->end(), ijet = ibegin;
87  ijet != iend;
88  ++ijet) {
89  float matched_dR2 = 1e9;
90  int matched_index = -1;
91 
92  for (typename edm::View<T>::const_iterator jbegin = h_jets1->begin(), jend = h_jets1->end(), jjet = jbegin;
93  jjet != jend;
94  ++jjet) {
95  int index = jjet - jbegin;
96 
97  if (jets1_locks.at(index))
98  continue; // skip jets that have already been matched
99 
100  float temp_dR2 = reco::deltaR2(ijet->eta(), ijet->phi(), jjet->eta(), jjet->phi());
101  if (temp_dR2 < matched_dR2) {
102  matched_dR2 = temp_dR2;
103  matched_index = index;
104  }
105  } // end loop over src jets
106 
107  if (matched_index >= 0) {
108  if (matched_dR2 > distMax_ * distMax_)
109  edm::LogInfo("MatchedJetsFarApart") << "Matched jets separated by dR greater than distMax=" << distMax_;
110  else {
111  jets1_locks.at(matched_index) = true;
112  if (!value_.empty())
113  values.at(matched_index) = (*(evaluationMap_.at(value_)))(*ijet);
114  if (multiValue_) {
115  for (size_t i = 0; i < valueLabels_.size(); ++i)
116  valuesMap.at(valueLabels_[i]).at(matched_index) = (*(evaluationMap_.at(valueLabels_[i])))(*ijet);
117  }
118  }
119  }
120  } // end loop over matched jets
121 
122  if (!value_.empty()) {
123  std::unique_ptr<JetValueMap> jetValueMap(new JetValueMap());
124 
125  JetValueMap::Filler filler(*jetValueMap);
126  filler.insert(h_jets1, values.begin(), values.end());
127  filler.fill();
128 
129  // put in Event
130  iEvent.put(std::move(jetValueMap));
131  }
132  if (multiValue_) {
133  for (size_t i = 0; i < valueLabels_.size(); ++i) {
134  std::unique_ptr<JetValueMap> jetValueMap(new JetValueMap());
135 
136  JetValueMap::Filler filler(*jetValueMap);
137  filler.insert(h_jets1, valuesMap.at(valueLabels_[i]).begin(), valuesMap.at(valueLabels_[i]).end());
138  filler.fill();
139 
140  // put in Event
141  iEvent.put(std::move(jetValueMap), valueLabels_[i]);
142  }
143  }
144  }
145 
148  const double distMax_;
150  const std::vector<std::string> values_;
151  const std::vector<std::string> valueLabels_;
152  const bool lazyParser_;
154  std::map<std::string, std::unique_ptr<const StringObjectFunction<C> > > evaluationMap_;
155 };
156 
160 
edm::ValueMap< float > JetValueMap
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
JetDeltaRValueMapProducer(edm::ParameterSet const &params)
const std::vector< std::string > valueLabels_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
const edm::EDGetTokenT< typename edm::View< T > > srcToken_
std::map< std::string, std::unique_ptr< const StringObjectFunction< C > > > evaluationMap_
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
JetDeltaRValueMapProducer< pat::Jet > PatJetDeltaRValueMapProducer
int iEvent
Definition: GenABIO.cc:224
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
const edm::EDGetTokenT< typename edm::View< C > > matchedToken_
def move
Definition: eostools.py:511
Log< level::Info, false > LogInfo
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
JetDeltaRValueMapProducer< reco::Jet, pat::Jet > RecoJetToPatJetDeltaRValueMapProducer
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
JetDeltaRValueMapProducer< reco::Jet > RecoJetDeltaRValueMapProducer
Log< level::Warning, false > LogWarning
const std::vector< std::string > values_
long double T