Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ICTauProducer.hh
Go to the documentation of this file.
1 #ifndef UserCode_ICHiggsTauTau_ICTauProducer_h
2 #define UserCode_ICHiggsTauTau_ICTauProducer_h
3 
4 #include <memory>
5 #include <vector>
6 #include "boost/functional/hash.hpp"
7 #include "boost/format.hpp"
8 #include "FWCore/Framework/interface/EDProducer.h"
9 #include "FWCore/ParameterSet/interface/ParameterSet.h"
10 #include "FWCore/Framework/interface/Event.h"
11 #include "FWCore/Framework/interface/EventSetup.h"
12 #include "DataFormats/TauReco/interface/PFTauFwd.h"
13 #include "DataFormats/TauReco/interface/PFTau.h"
14 #if CMSSW_MAJOR_VERSION>=7
15 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
16 #endif
17 #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
18 #include "DataFormats/PatCandidates/interface/Tau.h"
19 #include "DataFormats/VertexReco/interface/Vertex.h"
20 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
26 
30 template <class T>
31 class ICTauProducer : public edm::EDProducer {
32  public:
33  explicit ICTauProducer(const edm::ParameterSet &);
35 
36  private:
37  virtual void beginJob();
38  virtual void produce(edm::Event &, const edm::EventSetup &);
39  virtual void endJob();
40  void constructSpecific(edm::Handle<edm::View<T> > const& taus_handle,
41  edm::Event& event, const edm::EventSetup& setup);
42 
43  std::vector<ic::Tau> *taus_;
44  edm::InputTag input_;
45  std::string branch_;
46  boost::hash<T const*> tau_hasher_;
47  boost::hash<reco::Track const*> track_hasher_;
48  boost::hash<reco::Candidate const*> cand_hasher_;
49  edm::InputTag track_input_;
50  std::vector<std::pair<std::string, edm::InputTag> > tau_ids_;
51  edm::InputTag input_vertices_;
52  bool do_vertex_ip_;
53  bool request_trks_;
54  std::map<std::string, std::size_t> observed_id_;
55  bool do_total_charged_;
56  std::string total_charged_label_;
57  bool request_cands_;
58  edm::InputTag input_cands_;
59  bool is_slimmed_;
60 };
61 
62 // =============================
63 // Template class implementation
64 // =============================
65 template <class T>
66 ICTauProducer<T>::ICTauProducer(const edm::ParameterSet& config)
67  : input_(config.getParameter<edm::InputTag>("input")),
68  branch_(config.getParameter<std::string>("branch")),
69  input_vertices_(config.getParameter<edm::InputTag>("inputVertices")),
70  do_vertex_ip_(config.getParameter<bool>("includeVertexIP")),
71  request_trks_(config.getParameter<bool>("requestTracks")),
72  do_total_charged_(config.getParameter<bool>("includeTotalCharged")),
73  total_charged_label_(config.getParameter<std::string>("totalChargedLabel")),
74  request_cands_(config.getParameter<bool>("requestPFCandidates")),
75  input_cands_(config.getParameter<edm::InputTag>("inputPFCandidates")),
76  is_slimmed_(config.getParameter<bool>("isSlimmed")) {
77  consumes<edm::View<T>>(input_);
78  consumes<edm::View<reco::Vertex>>(input_vertices_);
79  taus_ = new std::vector<ic::Tau>();
80 
81  edm::ParameterSet pset = config.getParameter<edm::ParameterSet>("tauIDs");
82  std::vector<std::string> vec = pset.getParameterNamesForType<edm::InputTag>();
83  for (unsigned i = 0; i < vec.size(); ++i) {
84  tau_ids_.push_back(
85  std::make_pair(vec[i], pset.getParameter<edm::InputTag>(vec[i])));
86  consumes<reco::PFTauDiscriminator>(tau_ids_[i].second);
87  }
88 
89  if (request_trks_) {
90  produces<reco::TrackRefVector>("requestedTracks");
91  }
92 
93  if (request_cands_) {
94  consumes<edm::View<reco::Candidate>>(input_cands_);
95  if (is_slimmed_) {
96 #if CMSSW_MAJOR_VERSION >= 7
97  produces<pat::PackedCandidateRefVector>("requestedPFCandidates");
98 #endif
99  } else {
100  produces<reco::PFCandidateRefVector>("requestedPFCandidates");
101  }
102  }
103 
104  PrintHeaderWithProduces(config, input_, branch_);
105  PrintOptional(1, do_vertex_ip_, "includeVertexIP");
106  PrintOptional(1, do_total_charged_, "includeTotalCharged");
107  PrintOptional(1, request_trks_, "requestTracks");
108  PrintOptional(1, request_cands_, "requestPFCandidates");
109  PrintOptional(1, is_slimmed_, "isSlimmed");
110 }
111 
112 template <class T>
114 
115 // =============
116 // Main producer
117 // =============
118 template <class T>
119 void ICTauProducer<T>::produce(edm::Event& event,
120  const edm::EventSetup& setup) {
121  edm::Handle<edm::View<T> > taus_handle;
122  event.getByLabel(input_, taus_handle);
123 
124  edm::Handle<edm::View<reco::Vertex> > vertices_handle;
125  if (do_vertex_ip_) event.getByLabel(input_vertices_, vertices_handle);
126 
127  std::auto_ptr<reco::TrackRefVector> trk_requests(new reco::TrackRefVector());
128 
129  taus_->clear();
130  taus_->resize(taus_handle->size(), ic::Tau());
131  for (unsigned i = 0; i < taus_handle->size(); ++i) {
132  T const& src = taus_handle->at(i);
133  ic::Tau& dest = taus_->at(i);
134 
135  dest.set_id(tau_hasher_(&src));
136  dest.set_pt(src.pt());
137  dest.set_eta(src.eta());
138  dest.set_phi(src.phi());
139  dest.set_energy(src.energy());
140  dest.set_charge(src.charge());
141  dest.set_decay_mode(src.decayMode());
142 
143  if (src.leadPFChargedHadrCand().isNonnull()) {
144  dest.set_lead_ecal_energy(src.leadPFChargedHadrCand()->ecalEnergy());
145  dest.set_lead_hcal_energy(src.leadPFChargedHadrCand()->hcalEnergy());
146  dest.set_lead_p(src.leadPFChargedHadrCand()->p());
147  reco::TrackRef const& trk = src.leadPFChargedHadrCand()->trackRef();
148  reco::GsfTrackRef const& gsf = src.leadPFChargedHadrCand()->gsfTrackRef();
149  if (do_vertex_ip_ && vertices_handle->size() > 0) {
150  reco::Vertex const& vtx = vertices_handle->at(0);
151  if (trk.isNonnull()) {
152  dest.set_lead_dxy_vertex(trk->dxy(vtx.position()));
153  dest.set_lead_dz_vertex(trk->dz(vtx.position()));
154  } else if (gsf.isNonnull()) {
155  dest.set_lead_dxy_vertex(gsf->dxy(vtx.position()));
156  dest.set_lead_dz_vertex(gsf->dz(vtx.position()));
157  }
158  }
159  }
160 
161 
162  if (request_trks_) {
163  std::vector<std::size_t> trk_ids;
164  for (unsigned j = 0; j < src.signalPFChargedHadrCands().size(); ++j) {
165  reco::TrackRef const& trk =
166  src.signalPFChargedHadrCands().at(j)->trackRef();
167  if (trk.isNonnull()) {
168  trk_requests->push_back(trk);
169  trk_ids.push_back(track_hasher_(&(*trk)));
170  }
171  }
172  dest.set_constituent_tracks(trk_ids);
173  }
174 
175  dest.set_vx(src.vx());
176  dest.set_vy(src.vy());
177  dest.set_vz(src.vz());
178  }
179  constructSpecific(taus_handle, event, setup);
180  if (request_trks_) event.put(trk_requests, "requestedTracks");
181 }
182 
183 // ==================
184 // Specific producers
185 // ==================
186 template <class T>
188  edm::Handle<edm::View<T> > const& taus_handle, edm::Event& event,
189  const edm::EventSetup& setup) {}
190 
191 template <>
193  edm::Handle<edm::View<reco::PFTau> > const& taus_handle, edm::Event& event,
194  const edm::EventSetup& setup) {
195 
196  std::vector<edm::Handle<reco::PFTauDiscriminator> > id_handles(
197  tau_ids_.size());
198  for (unsigned i = 0; i < id_handles.size(); ++i) {
199  event.getByLabel(tau_ids_[i].second, id_handles[i]);
200  }
201 
202  for (unsigned i = 0; i < taus_handle->size(); ++i) {
203  //reco::PFTau const& src = taus_handle->at(i);
204  ic::Tau& dest = taus_->at(i);
205  reco::PFTauRef const& ref = taus_handle->refAt(i).castTo<reco::PFTauRef>();
206  for (unsigned j = 0; j < tau_ids_.size(); ++j) {
207  dest.SetTauID(tau_ids_[j].first, (*(id_handles[j]))[ref]);
208  observed_id_[tau_ids_[j].first] = CityHash64(tau_ids_[j].first);
209  }
210  }
211 }
212 
213 template <>
215  edm::Handle<edm::View<pat::Tau> > const& taus_handle, edm::Event& event,
216  const edm::EventSetup& setup) {
217 
218  edm::Handle<edm::View<reco::Candidate> > cands_handle;
219  if (request_cands_) {
220  event.getByLabel(input_cands_, cands_handle);
221  }
222 
223 #if CMSSW_MAJOR_VERSION >= 7
224  std::auto_ptr<pat::PackedCandidateRefVector> cand_requests_slimmed(new pat::PackedCandidateRefVector());
225 #endif
226  std::auto_ptr<reco::PFCandidateRefVector> cand_requests(new reco::PFCandidateRefVector());
227 
228  for (unsigned i = 0; i < taus_handle->size(); ++i) {
229  pat::Tau const& src = taus_handle->at(i);
230  ic::Tau& dest = taus_->at(i);
231  std::vector<std::pair<std::string, float> > tau_ids = src.tauIDs();
232  for (unsigned j = 0; j < tau_ids.size(); ++j) {
233  dest.SetTauID(tau_ids[j].first, tau_ids[j].second);
234  observed_id_[tau_ids[j].first] = CityHash64(tau_ids[j].first);
235  }
236  //dest.set_lead_ecal_energy(src.leadChargedHadrCand()->ecalEnergy());
237  //dest.set_lead_hcal_energy(src.leadChargedHadrCand()->hcalEnergy());
238 #if CMSSW_MAJOR_VERSION >= 7
239  if(src.leadChargedHadrCand().isNonnull()){
240  pat::PackedCandidate const* packedCand = dynamic_cast<pat::PackedCandidate const*>(src.leadChargedHadrCand().get());
241  if(packedCand){
242  dest.set_lead_dz_vertex(packedCand->dz());
243  dest.set_lead_dxy_vertex(packedCand->dxy());
244  dest.set_lead_p(packedCand->p());
245  }
246  }
247 
248  if (do_total_charged_ &&
249  src.signalChargedHadrCands().isNonnull() &&
250  src.isolationChargedHadrCands().isNonnull()) {
251  dest.SetTauID(total_charged_label_,
252  float(src.signalChargedHadrCands().size() + src.isolationChargedHadrCands().size()));
253  observed_id_[total_charged_label_] = CityHash64(total_charged_label_);
254  }
255 
256  if (request_cands_) {
257  if (src.signalChargedHadrCands().isNonnull()) {
258  auto cands = src.signalChargedHadrCands();
259  std::vector<std::size_t> ids;
260  for (unsigned c = 0; c < cands.size(); ++c) {
261  cand_requests_slimmed->push_back(cands_handle->refAt(cands[c].key()).castTo<pat::PackedCandidateRef>());
262  ids.push_back(cand_hasher_(&(*(cands[c]))));
263  }
264  dest.set_sig_charged_cands(ids);
265  }
266 
267  if (src.isolationChargedHadrCands().isNonnull()) {
268  auto cands = src.isolationChargedHadrCands();
269  std::vector<std::size_t> ids;
270  for (unsigned c = 0; c < cands.size(); ++c) {
271  cand_requests_slimmed->push_back(cands_handle->refAt(cands[c].key()).castTo<pat::PackedCandidateRef>());
272  ids.push_back(cand_hasher_(&(*(cands[c]))));
273  }
274  dest.set_iso_charged_cands(ids);
275  }
276 
277  if (src.signalGammaCands().isNonnull()) {
278  auto cands = src.signalGammaCands();
279  std::vector<std::size_t> ids;
280  for (unsigned c = 0; c < cands.size(); ++c) {
281  cand_requests_slimmed->push_back(cands_handle->refAt(cands[c].key()).castTo<pat::PackedCandidateRef>());
282  ids.push_back(cand_hasher_(&(*(cands[c]))));
283  }
284  dest.set_sig_gamma_cands(ids);
285  }
286 
287  if (src.isolationGammaCands().isNonnull()) {
288  auto cands = src.isolationGammaCands();
289  std::vector<std::size_t> ids;
290  for (unsigned c = 0; c < cands.size(); ++c) {
291  cand_requests_slimmed->push_back(cands_handle->refAt(cands[c].key()).castTo<pat::PackedCandidateRef>());
292  ids.push_back(cand_hasher_(&(*(cands[c]))));
293  }
294  dest.set_iso_gamma_cands(ids);
295  }
296  }
297 #endif
298  }
299 
300  if (request_cands_) {
301  if (is_slimmed_) {
302 #if CMSSW_MAJOR_VERSION >= 7
303  event.put(cand_requests_slimmed, "requestedPFCandidates");
304 #endif
305  } else {
306  event.put(cand_requests, "requestedPFCandidates");
307  }
308  }
309 
310 }
311 
312 template <class T>
314  ic::StaticTree::tree_->Branch(branch_.c_str(), &taus_);
315 }
316 
317 template <class T>
319  std::cout << std::string(78, '-') << "\n";
320  std::cout << boost::format("%-56s %20s\n")
321  % std::string("Tau Discriminators") % std::string("Hash Summmary");
322  std::map<std::string, std::size_t>::const_iterator iter;
323  for (iter = observed_id_.begin(); iter != observed_id_.end(); ++iter) {
324  std::cout << boost::format("%-56s| %020i\n") % iter->first % iter->second;
325  }
326 }
327 
328 #endif
void set_iso_gamma_cands(std::vector< std::size_t > const &iso_gamma_cands)
A vector referring to the isolation PFCandidates.
Definition: Tau.hh:160
void set_lead_ecal_energy(float const &lead_ecal_energy)
ECAL energy of the leading charged PF constituent.
Definition: Tau.hh:104
void SetTauID(std::string const &name, float const &value)
Add a new entry, overwriting any existing one with the same name.
Definition: Tau.cc:20
uint64 CityHash64(const char *buf, size_t len)
Definition: city.cc:200
void set_sig_gamma_cands(std::vector< std::size_t > const &sig_gamma_cands)
A vector referring to the isolation PFCandidates.
Definition: Tau.hh:154
void set_lead_hcal_energy(float const &lead_hcal_energy)
HCAL energy of the leading charged PF constituent.
Definition: Tau.hh:109
void set_lead_dz_vertex(float const &lead_dz_vertex)
Longitudinal impact parameter of the leading charged constituent track with the primary vertex...
Definition: Tau.hh:122
void set_phi(double const &phi)
Direct access to .
Definition: Candidate.hh:75
void set_constituent_tracks(std::vector< std::size_t > const &constituent_tracks)
A vector referring to the constituent track ic::Track::id()
Definition: Tau.hh:136
void set_sig_charged_cands(std::vector< std::size_t > const &sig_charged_cands)
A vector referring to the isolation PFCandidates.
Definition: Tau.hh:142
void set_id(std::size_t const &id)
Unique identifier.
Definition: Candidate.hh:66
void set_lead_p(float const &lead_p)
Momentum of the leading charged PF constituent.
Definition: Tau.hh:114
void set_charge(int const &charge)
Electric charge.
Definition: Candidate.hh:81
static TTree * tree_
Definition: StaticTree.hh:13
void set_vz(double const &z)
The z-coordinate of the leading track PCA.
Definition: Tau.hh:133
void set_vy(double const &y)
The y-coordinate of the leading track PCA.
Definition: Tau.hh:130
void set_vx(double const &x)
The x-coordinate of the leading track PCA.
Definition: Tau.hh:127
void set_energy(double const &energy)
Direct access to the energy.
Definition: Candidate.hh:78
void set_lead_dxy_vertex(float const &lead_dxy_vertex)
Transverse impact parameter of the leading charged constituent track with the primary vertex...
Definition: Tau.hh:117
void PrintOptional(unsigned depth, bool value, std::string text)
void set_decay_mode(int const &decay_mode)
The tau decay mode identifier.
Definition: Tau.hh:99
Definition: Consumes.h:19
See documentation here.
void set_pt(double const &pt)
Direct access to the .
Definition: Candidate.hh:69
void set_eta(double const &eta)
Direct access to .
Definition: Candidate.hh:72
void set_iso_charged_cands(std::vector< std::size_t > const &iso_charged_cands)
A vector referring to the isolation PFCandidates.
Definition: Tau.hh:148
ICTauProducer(const edm::ParameterSet &)
void PrintHeaderWithProduces(edm::ParameterSet const &config, edm::InputTag const &in, std::string branch)
This class stores a subset of the reco::PFTau properties which are most commonly used in analysis...
Definition: Tau.hh:17