CMS 3D CMS Logo

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, typename TN = float>
32 public:
33  typedef TN value_type;
35  typedef std::vector<value_type> ValueVector;
36 
38  : srcToken_(consumes<typename edm::View<T> >(params.getParameter<edm::InputTag>("src"))),
39  matchedToken_(consumes<typename edm::View<C> >(params.getParameter<edm::InputTag>("matched"))),
40  distMax_(params.getParameter<double>("distMax")),
41  value_(params.existsAs<std::string>("value") ? params.getParameter<std::string>("value") : ""),
42  values_(params.existsAs<std::vector<std::string> >("values")
43  ? params.getParameter<std::vector<std::string> >("values")
44  : std::vector<std::string>()),
45  valueLabels_(params.existsAs<std::vector<std::string> >("valueLabels")
46  ? params.getParameter<std::vector<std::string> >("valueLabels")
47  : std::vector<std::string>()),
48  lazyParser_(params.existsAs<bool>("lazyParser") ? params.getParameter<bool>("lazyParser") : false),
50  if (!value_.empty()) {
51  if (value_ != "index") {
52  evaluationMap_.insert(std::make_pair(
54  }
55  produces<JetValueMap>();
56  }
57 
58  if (!valueLabels_.empty() || !values_.empty()) {
59  if (valueLabels_.size() == values_.size()) {
60  multiValue_ = true;
61  for (size_t i = 0; i < valueLabels_.size(); ++i) {
62  evaluationMap_.insert(std::make_pair(
63  valueLabels_[i],
65  produces<JetValueMap>(valueLabels_[i]);
66  }
67  } else
68  edm::LogWarning("ValueLabelMismatch")
69  << "The number of value labels does not match the number of values. Values will not be evaluated.";
70  }
71  }
72 
74 
75 private:
76  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
78  iEvent.getByToken(srcToken_, h_jets1);
80  iEvent.getByToken(matchedToken_, h_jets2);
81 
82  ValueVector values(h_jets1->size(), -99999);
83 
84  std::map<std::string, std::vector<float> > valuesMap;
85  if (multiValue_) {
86  for (size_t i = 0; i < valueLabels_.size(); ++i)
87  valuesMap.insert(std::make_pair(valueLabels_[i], std::vector<float>(h_jets1->size(), -99999)));
88  }
89  std::vector<bool> jets1_locks(h_jets1->size(), false);
90 
91  for (typename edm::View<C>::const_iterator ibegin = h_jets2->begin(), iend = h_jets2->end(), ijet = ibegin;
92  ijet != iend;
93  ++ijet) {
94  float matched_dR2 = 1e9;
95  int matched_index = -1;
96  int iindex = ijet - ibegin;
97 
98  for (typename edm::View<T>::const_iterator jbegin = h_jets1->begin(), jend = h_jets1->end(), jjet = jbegin;
99  jjet != jend;
100  ++jjet) {
101  int jindex = jjet - jbegin;
102 
103  if (jets1_locks.at(jindex))
104  continue; // skip jets that have already been matched
105 
106  float temp_dR2 = reco::deltaR2(ijet->eta(), ijet->phi(), jjet->eta(), jjet->phi());
107  if (temp_dR2 < matched_dR2) {
108  matched_dR2 = temp_dR2;
109  matched_index = jindex;
110  }
111  } // end loop over src jets
112 
113  if (matched_index >= 0) {
114  if (matched_dR2 > distMax_ * distMax_)
115  edm::LogInfo("MatchedJetsFarApart") << "Matched jets separated by dR greater than distMax=" << distMax_;
116  else {
117  jets1_locks.at(matched_index) = true;
118  if (!value_.empty()) {
119  if (value_ == "index") {
120  values.at(matched_index) = iindex;
121  } else {
122  values.at(matched_index) = (*(evaluationMap_.at(value_)))(*ijet);
123  }
124  }
125  if (multiValue_) {
126  for (size_t i = 0; i < valueLabels_.size(); ++i)
127  valuesMap.at(valueLabels_[i]).at(matched_index) = (*(evaluationMap_.at(valueLabels_[i])))(*ijet);
128  }
129  }
130  }
131  } // end loop over matched jets
132 
133  if (!value_.empty()) {
134  std::unique_ptr<JetValueMap> jetValueMap(new JetValueMap());
135 
136  typename JetValueMap::Filler filler(*jetValueMap);
137  filler.insert(h_jets1, values.begin(), values.end());
138  filler.fill();
139 
140  // put in Event
141  iEvent.put(std::move(jetValueMap));
142  }
143  if (multiValue_) {
144  for (size_t i = 0; i < valueLabels_.size(); ++i) {
145  std::unique_ptr<JetValueMap> jetValueMap(new JetValueMap());
146 
147  typename JetValueMap::Filler filler(*jetValueMap);
148  filler.insert(h_jets1, valuesMap.at(valueLabels_[i]).begin(), valuesMap.at(valueLabels_[i]).end());
149  filler.fill();
150 
151  // put in Event
152  iEvent.put(std::move(jetValueMap), valueLabels_[i]);
153  }
154  }
155  }
156 
159  const double distMax_;
161  const std::vector<std::string> values_;
162  const std::vector<std::string> valueLabels_;
163  const bool lazyParser_;
165  std::map<std::string, std::unique_ptr<const StringObjectFunction<C> > > evaluationMap_;
166 };
167 
172 
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
const edm::EDGetTokenT< typename edm::View< T > > srcToken_
const std::vector< std::string > valueLabels_
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
JetDeltaRValueMapProducer< pat::Jet > PatJetDeltaRValueMapProducer
edm::ValueMap< value_type > JetValueMap
int iEvent
Definition: GenABIO.cc:224
std::vector< value_type > ValueVector
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
JetDeltaRValueMapProducer(edm::ParameterSet const &params)
Log< level::Info, false > LogInfo
JetDeltaRValueMapProducer< reco::Jet, reco::GenJet, int > RecoJetToGenJetDeltaRValueMapProducer
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
JetDeltaRValueMapProducer< reco::Jet, pat::Jet > RecoJetToPatJetDeltaRValueMapProducer
std::map< std::string, std::unique_ptr< const StringObjectFunction< C > > > evaluationMap_
HLT enums.
const edm::EDGetTokenT< typename edm::View< C > > matchedToken_
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
JetDeltaRValueMapProducer< reco::Jet > RecoJetDeltaRValueMapProducer
Log< level::Warning, false > LogWarning
long double T
def move(src, dest)
Definition: eostools.py:511
const std::vector< std::string > values_