CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HGCalTriggerNtupleHGCTriggerCells.cc
Go to the documentation of this file.
1 
17 
19 public:
22  void initialize(TTree&, const edm::ParameterSet&, edm::ConsumesCollector&&) final;
23  void fill(const edm::Event& e, const HGCalTriggerNtupleEventSetup& es) final;
24 
25 private:
26  double calibrate(double, int, unsigned);
27  void simhits(const edm::Event& e,
28  std::unordered_map<uint32_t, double>& simhits_ee,
29  std::unordered_map<uint32_t, double>& simhits_fh,
30  std::unordered_map<uint32_t, double>& simhits_bh);
31  void clear() final;
32 
34 
41  double keV2fC_;
42  std::vector<double> fcPerMip_;
43  std::vector<double> layerWeights_;
45 
46  int tc_n_;
47  std::vector<uint32_t> tc_id_;
48  std::vector<int> tc_subdet_;
49  std::vector<int> tc_side_;
50  std::vector<int> tc_layer_;
51  std::vector<int> tc_waferu_;
52  std::vector<int> tc_waferv_;
53  std::vector<int> tc_wafertype_;
54  std::vector<int> tc_cellu_;
55  std::vector<int> tc_cellv_;
56  std::vector<uint32_t> tc_data_;
58  std::vector<uint32_t> tc_compressedCharge_;
59  std::vector<float> tc_mipPt_;
60  std::vector<float> tc_pt_;
61  std::vector<float> tc_energy_;
62  std::vector<float> tc_simenergy_;
63  std::vector<float> tc_eta_;
64  std::vector<float> tc_phi_;
65  std::vector<float> tc_x_;
66  std::vector<float> tc_y_;
67  std::vector<float> tc_z_;
68  std::vector<uint32_t> tc_cluster_id_;
69  std::vector<uint32_t> tc_multicluster_id_;
72 
73  typedef edm::AssociationMap<edm::OneToMany<CaloParticleCollection, l1t::HGCalTriggerCellBxCollection>> CaloToCellsMap;
74 };
75 
77 
78 HGCalTriggerNtupleHGCTriggerCells::HGCalTriggerNtupleHGCTriggerCells(const edm::ParameterSet& conf)
79  : HGCalTriggerNtupleBase(conf),
80  fill_simenergy_(conf.getParameter<bool>("FillSimEnergy")),
81  fill_truthmap_(conf.getParameter<bool>("FillTruthMap")),
82  filter_cells_in_multiclusters_(conf.getParameter<bool>("FilterCellsInMulticlusters")),
83  keV2fC_(conf.getParameter<double>("keV2fC")),
84  fcPerMip_(conf.getParameter<std::vector<double>>("fcPerMip")),
85  layerWeights_(conf.getParameter<std::vector<double>>("layerWeights")),
86  thicknessCorrections_(conf.getParameter<std::vector<double>>("thicknessCorrections")) {
87  accessEventSetup_ = false;
88 }
89 
91  const edm::ParameterSet& conf,
92  edm::ConsumesCollector&& collector) {
94  collector.consumes<l1t::HGCalTriggerCellBxCollection>(conf.getParameter<edm::InputTag>("TriggerCells"));
96  collector.consumes<l1t::HGCalMulticlusterBxCollection>(conf.getParameter<edm::InputTag>("Multiclusters"));
97 
98  if (fill_simenergy_) {
99  simhits_ee_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("eeSimHits"));
100  simhits_fh_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("fhSimHits"));
101  simhits_bh_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("bhSimHits"));
102  }
103 
104  if (fill_truthmap_)
106  collector.consumes<CaloToCellsMap>(conf.getParameter<edm::InputTag>("caloParticlesToCells"));
107 
108  std::string prefix(conf.getUntrackedParameter<std::string>("Prefix", "tc"));
109 
110  std::string bname;
111  auto withPrefix([&prefix, &bname](char const* vname) -> char const* {
112  bname = prefix + "_" + vname;
113  return bname.c_str();
114  });
115 
116  tree.Branch(withPrefix("n"), &tc_n_, (prefix + "_n/I").c_str());
117  tree.Branch(withPrefix("id"), &tc_id_);
118  tree.Branch(withPrefix("subdet"), &tc_subdet_);
119  tree.Branch(withPrefix("zside"), &tc_side_);
120  tree.Branch(withPrefix("layer"), &tc_layer_);
121  tree.Branch(withPrefix("waferu"), &tc_waferu_);
122  tree.Branch(withPrefix("waferv"), &tc_waferv_);
123  tree.Branch(withPrefix("wafertype"), &tc_wafertype_);
124  tree.Branch(withPrefix("cellu"), &tc_cellu_);
125  tree.Branch(withPrefix("cellv"), &tc_cellv_);
126  tree.Branch(withPrefix("data"), &tc_data_);
127  tree.Branch(withPrefix("uncompressedCharge"), &tc_uncompressedCharge_);
128  tree.Branch(withPrefix("compressedCharge"), &tc_compressedCharge_);
129  tree.Branch(withPrefix("pt"), &tc_pt_);
130  tree.Branch(withPrefix("mipPt"), &tc_mipPt_);
131  tree.Branch(withPrefix("energy"), &tc_energy_);
132  if (fill_simenergy_)
133  tree.Branch(withPrefix("simenergy"), &tc_simenergy_);
134  tree.Branch(withPrefix("eta"), &tc_eta_);
135  tree.Branch(withPrefix("phi"), &tc_phi_);
136  tree.Branch(withPrefix("x"), &tc_x_);
137  tree.Branch(withPrefix("y"), &tc_y_);
138  tree.Branch(withPrefix("z"), &tc_z_);
139  tree.Branch(withPrefix("cluster_id"), &tc_cluster_id_);
140  tree.Branch(withPrefix("multicluster_id"), &tc_multicluster_id_);
141  tree.Branch(withPrefix("multicluster_pt"), &tc_multicluster_pt_);
142  if (fill_truthmap_)
143  tree.Branch(withPrefix("genparticle_index"), &tc_genparticle_index_);
144 }
145 
147  // retrieve trigger cells
149  e.getByToken(trigger_cells_token_, trigger_cells_h);
150  const l1t::HGCalTriggerCellBxCollection& trigger_cells = *trigger_cells_h;
151 
152  // retrieve clusters
154  e.getByToken(multiclusters_token_, multiclusters_h);
155  const l1t::HGCalMulticlusterBxCollection& multiclusters = *multiclusters_h;
156 
157  // sim hit association
158  std::unordered_map<uint32_t, double> simhits_ee;
159  std::unordered_map<uint32_t, double> simhits_fh;
160  std::unordered_map<uint32_t, double> simhits_bh;
161  if (fill_simenergy_)
162  simhits(e, simhits_ee, simhits_fh, simhits_bh);
163 
164  edm::Handle<CaloToCellsMap> caloparticles_map_h;
165  std::unordered_map<uint32_t, unsigned> cell_to_genparticle;
166  if (fill_truthmap_) {
167  e.getByToken(caloparticles_map_token_, caloparticles_map_h);
168  for (auto& keyval : *caloparticles_map_h) {
169  for (auto& tcref : keyval.val)
170  cell_to_genparticle.emplace(tcref->detId(), keyval.key->g4Tracks().at(0).genpartIndex() - 1);
171  }
172  }
173 
174  // Associate cells to clusters
175  std::unordered_map<uint32_t, uint32_t> cell2cluster;
176  std::unordered_map<uint32_t, l1t::HGCalMulticlusterBxCollection::const_iterator> cell2multicluster;
177  for (auto mcl_itr = multiclusters.begin(0); mcl_itr != multiclusters.end(0); mcl_itr++) {
178  // loop on 2D clusters inside 3D clusters
179  for (const auto& cl_ptr : mcl_itr->constituents()) {
180  // loop on TC inside 2D clusters
181  for (const auto& tc_ptr : cl_ptr.second->constituents()) {
182  cell2cluster.emplace(tc_ptr.second->detId(), cl_ptr.second->detId());
183  cell2multicluster.emplace(tc_ptr.second->detId(), mcl_itr);
184  }
185  }
186  }
187 
189 
190  clear();
191  for (auto tc_itr = trigger_cells.begin(0); tc_itr != trigger_cells.end(0); tc_itr++) {
192  if (tc_itr->hwPt() > 0) {
193  auto cl_itr = cell2cluster.find(tc_itr->detId());
194  auto mcl_itr = cell2multicluster.find(tc_itr->detId());
195  uint32_t cl_id = (cl_itr != cell2cluster.end() ? cl_itr->second : 0);
196  uint32_t mcl_id = (mcl_itr != cell2multicluster.end() ? mcl_itr->second->detId() : 0);
197  float mcl_pt = (mcl_itr != cell2multicluster.end() ? mcl_itr->second->pt() : 0.);
198  // Filter cells not included in a multicluster, if requested
199  if (filter_cells_in_multiclusters_ && mcl_id == 0)
200  continue;
201  tc_n_++;
202  // hardware data
203  DetId id(tc_itr->detId());
204  tc_id_.emplace_back(tc_itr->detId());
205  tc_side_.emplace_back(triggerTools_.zside(id));
206  tc_layer_.emplace_back(triggerTools_.layerWithOffset(id));
207  if (id.det() == DetId::HGCalTrigger) {
208  HGCalTriggerDetId idtrg(id);
209  tc_subdet_.emplace_back(idtrg.subdet());
210  tc_waferu_.emplace_back(idtrg.waferU());
211  tc_waferv_.emplace_back(idtrg.waferV());
212  tc_wafertype_.emplace_back(idtrg.type());
213  tc_cellu_.emplace_back(idtrg.triggerCellU());
214  tc_cellv_.emplace_back(idtrg.triggerCellV());
215  } else if (id.det() == DetId::HGCalHSc) {
216  HGCScintillatorDetId idsci(id);
217  tc_subdet_.emplace_back(idsci.subdet());
218  tc_waferu_.emplace_back(-999);
219  tc_waferv_.emplace_back(-999);
220  tc_wafertype_.emplace_back(idsci.type());
221  tc_cellu_.emplace_back(idsci.ietaAbs());
222  tc_cellv_.emplace_back(idsci.iphi());
223  } else {
224  throw cms::Exception("InvalidHGCalTriggerDetid")
225  << "Found unexpected trigger cell detid to be filled in HGCal Trigger Cell ntuple.";
226  }
227  tc_data_.emplace_back(tc_itr->hwPt());
228  tc_uncompressedCharge_.emplace_back(tc_itr->uncompressedCharge());
229  tc_compressedCharge_.emplace_back(tc_itr->compressedCharge());
230  tc_mipPt_.emplace_back(tc_itr->mipPt());
231  // physical values
232  tc_pt_.emplace_back(tc_itr->pt());
233  tc_energy_.emplace_back(tc_itr->energy());
234  tc_eta_.emplace_back(tc_itr->eta());
235  tc_phi_.emplace_back(tc_itr->phi());
236  tc_x_.emplace_back(tc_itr->position().x());
237  tc_y_.emplace_back(tc_itr->position().y());
238  tc_z_.emplace_back(tc_itr->position().z());
239  // Links between TC and clusters
240  tc_cluster_id_.emplace_back(cl_id);
241  tc_multicluster_id_.emplace_back(mcl_id);
242  tc_multicluster_pt_.emplace_back(mcl_pt);
243 
244  if (fill_simenergy_) {
245  double energy = 0;
246  unsigned layer = triggerTools_.layerWithOffset(id);
247  // search for simhit for all the cells inside the trigger cell
248  for (uint32_t c_id : triggerTools_.getTriggerGeometry()->getCellsFromTriggerCell(id)) {
250  if (triggerTools_.isEm(id)) {
251  auto itr = simhits_ee.find(c_id);
252  if (itr != simhits_ee.end())
253  energy += calibrate(itr->second, thickness, layer);
254  } else if (triggerTools_.isSilicon(id)) {
255  auto itr = simhits_fh.find(c_id);
256  if (itr != simhits_fh.end())
257  energy += calibrate(itr->second, thickness, layer);
258  } else {
259  auto itr = simhits_bh.find(c_id);
260  if (itr != simhits_bh.end())
261  energy += itr->second;
262  }
263  }
264  tc_simenergy_.emplace_back(energy);
265  }
266  }
267 
268  if (fill_truthmap_) {
269  auto itr(cell_to_genparticle.find(tc_itr->detId()));
270  if (itr == cell_to_genparticle.end())
271  tc_genparticle_index_.push_back(-1);
272  else
273  tc_genparticle_index_.push_back(itr->second);
274  }
275  }
276 }
277 
279  double fcPerMip = fcPerMip_[thickness];
281  double layerWeight = layerWeights_[layer];
282  double TeV2GeV = 1.e3;
283  return energy * keV2fC_ / fcPerMip * layerWeight * TeV2GeV / thicknessCorrection;
284 }
285 
287  std::unordered_map<uint32_t, double>& simhits_ee,
288  std::unordered_map<uint32_t, double>& simhits_fh,
289  std::unordered_map<uint32_t, double>& simhits_bh) {
291  e.getByToken(simhits_ee_token_, ee_simhits_h);
292  const edm::PCaloHitContainer& ee_simhits = *ee_simhits_h;
294  e.getByToken(simhits_fh_token_, fh_simhits_h);
295  const edm::PCaloHitContainer& fh_simhits = *fh_simhits_h;
297  e.getByToken(simhits_bh_token_, bh_simhits_h);
298  const edm::PCaloHitContainer& bh_simhits = *bh_simhits_h;
299 
300  // EE
301  for (const auto& simhit : ee_simhits) {
303  if (id.rawId() == 0)
304  continue;
305  auto itr_insert = simhits_ee.emplace(id, 0.);
306  itr_insert.first->second += simhit.energy();
307  }
308  // FH
309  for (const auto& simhit : fh_simhits) {
311  if (id.rawId() == 0)
312  continue;
313  auto itr_insert = simhits_fh.emplace(id, 0.);
314  itr_insert.first->second += simhit.energy();
315  }
316  // BH
317  for (const auto& simhit : bh_simhits) {
319  if (id.rawId() == 0)
320  continue;
321  auto itr_insert = simhits_bh.emplace(id, 0.);
322  itr_insert.first->second += simhit.energy();
323  }
324 }
325 
327  tc_n_ = 0;
328  tc_id_.clear();
329  tc_subdet_.clear();
330  tc_side_.clear();
331  tc_layer_.clear();
332  tc_waferu_.clear();
333  tc_waferv_.clear();
334  tc_wafertype_.clear();
335  tc_cellu_.clear();
336  tc_cellv_.clear();
337  tc_data_.clear();
338  tc_uncompressedCharge_.clear();
339  tc_compressedCharge_.clear();
340  tc_mipPt_.clear();
341  tc_pt_.clear();
342  tc_energy_.clear();
343  tc_simenergy_.clear();
344  tc_eta_.clear();
345  tc_phi_.clear();
346  tc_x_.clear();
347  tc_y_.clear();
348  tc_z_.clear();
349  tc_cluster_id_.clear();
350  tc_multicluster_id_.clear();
351  tc_multicluster_pt_.clear();
352  tc_genparticle_index_.clear();
353 }
void initialize(TTree &, const edm::ParameterSet &, edm::ConsumesCollector &&) final
const_iterator end(int bx) const
int thicknessIndex(const DetId &) const
T getUntrackedParameter(std::string const &, T const &) const
std::vector< PCaloHit > PCaloHitContainer
HGCalTriggerSubdetector subdet() const
get the subdetector
BXVector< HGCalTriggerCell > HGCalTriggerCellBxCollection
uint16_t *__restrict__ id
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
const HGCalTopology & eeTopology() const
int triggerCellU() const
get the cell #&#39;s in u,v or in x,y
HGCalTriggerNtupleHGCTriggerCells(const edm::ParameterSet &conf)
int type() const
get/set the type
const HGCalTriggerGeometryBase * getTriggerGeometry() const
virtual geom_set getCellsFromTriggerCell(const unsigned cell_det_id) const =0
unsigned layerWithOffset(const DetId &) const
edm::ESHandle< HGCalTriggerGeometryBase > geometry
DetId::Detector subdet() const
get the subdetector
int type() const
get the type
constexpr std::array< uint8_t, layerIndexSize > layer
void setGeometry(const HGCalTriggerGeometryBase *const)
int zside(const DetId &) const
int iphi() const
get the phi index
int triggerCellV() const
void fill(const edm::Event &e, const HGCalTriggerNtupleEventSetup &es) final
Definition: DetId.h:17
bool isSilicon(const DetId &) const
const HGCalTopology & hscTopology() const
T const * product() const
Definition: ESHandle.h:86
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool isEm(const DetId &) const
std::vector< CaloParticle > CaloParticleCollection
#define DEFINE_EDM_PLUGIN(factory, type, name)
DetId simToReco(const DetId &, const HGCalTopology &) const
void simhits(const edm::Event &e, std::unordered_map< uint32_t, double > &simhits_ee, std::unordered_map< uint32_t, double > &simhits_fh, std::unordered_map< uint32_t, double > &simhits_bh)
const_iterator begin(int bx) const
const HGCalTopology & fhTopology() const