CMS 3D CMS Logo

HGCalTriggerNtupleHGCTriggerCells.cc
Go to the documentation of this file.
1 
13 
14 
15 
17 {
18 
19  public:
22  void initialize(TTree&, const edm::ParameterSet&, edm::ConsumesCollector&&) final;
23  void fill(const edm::Event& e, const edm::EventSetup& es) final;
24 
25  private:
26  double calibrate(double, int, int);
27  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);
28  void clear() final;
29 
31 
36  double keV2fC_;
37  std::vector<double> fcPerMip_;
38  std::vector<double> layerWeights_;
39  std::vector<double> thicknessCorrections_;
41 
42 
43  int tc_n_ ;
44  std::vector<uint32_t> tc_id_;
45  std::vector<int> tc_subdet_;
46  std::vector<int> tc_side_;
47  std::vector<int> tc_layer_;
48  std::vector<int> tc_wafer_;
49  std::vector<int> tc_wafertype_ ;
50  std::vector<int> tc_cell_;
51  std::vector<uint32_t> tc_data_;
52  std::vector<float> tc_mipPt_;
53  std::vector<float> tc_pt_;
54  std::vector<float> tc_energy_;
55  std::vector<float> tc_simenergy_;
56  std::vector<float> tc_eta_;
57  std::vector<float> tc_phi_;
58  std::vector<float> tc_x_;
59  std::vector<float> tc_y_;
60  std::vector<float> tc_z_;
61  std::vector<uint32_t> tc_cluster_id_;
62  std::vector<uint32_t> tc_multicluster_id_;
63  std::vector<float> tc_multicluster_pt_;
64 
65 };
66 
69  "HGCalTriggerNtupleHGCTriggerCells" );
70 
71 
74  fill_simenergy_(conf.getParameter<bool>("FillSimEnergy")),
75  filter_cells_in_multiclusters_(conf.getParameter<bool>("FilterCellsInMulticlusters"))
76 {
77  fill_simenergy_ = conf.getParameter<bool>("FillSimEnergy");
78  filter_cells_in_multiclusters_ = conf.getParameter<bool>("FilterCellsInMulticlusters");
79  keV2fC_ = conf.getParameter<double>("keV2fC");
80  fcPerMip_ = conf.getParameter<std::vector<double>>("fcPerMip");
81  layerWeights_ = conf.getParameter<std::vector<double>>("layerWeights");
82  thicknessCorrections_ = conf.getParameter<std::vector<double>>("thicknessCorrections");
83 }
84 
85 void
87 initialize(TTree& tree, const edm::ParameterSet& conf, edm::ConsumesCollector&& collector)
88 {
89  trigger_cells_token_ = collector.consumes<l1t::HGCalTriggerCellBxCollection>(conf.getParameter<edm::InputTag>("TriggerCells"));
90  multiclusters_token_ = collector.consumes<l1t::HGCalMulticlusterBxCollection>(conf.getParameter<edm::InputTag>("Multiclusters"));
91 
92  if (fill_simenergy_)
93  {
94  simhits_ee_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("eeSimHits"));
95  simhits_fh_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("fhSimHits"));
96  simhits_bh_token_ = collector.consumes<edm::PCaloHitContainer>(conf.getParameter<edm::InputTag>("bhSimHits"));
97  }
98 
99  tree.Branch("tc_n", &tc_n_, "tc_n/I");
100  tree.Branch("tc_id", &tc_id_);
101  tree.Branch("tc_subdet", &tc_subdet_);
102  tree.Branch("tc_zside", &tc_side_);
103  tree.Branch("tc_layer", &tc_layer_);
104  tree.Branch("tc_wafer", &tc_wafer_);
105  tree.Branch("tc_wafertype", &tc_wafertype_);
106  tree.Branch("tc_cell", &tc_cell_);
107  tree.Branch("tc_data", &tc_data_);
108  tree.Branch("tc_pt", &tc_pt_);
109  tree.Branch("tc_mipPt", &tc_mipPt_);
110  tree.Branch("tc_energy", &tc_energy_);
111  if(fill_simenergy_) tree.Branch("tc_simenergy", &tc_simenergy_);
112  tree.Branch("tc_eta", &tc_eta_);
113  tree.Branch("tc_phi", &tc_phi_);
114  tree.Branch("tc_x", &tc_x_);
115  tree.Branch("tc_y", &tc_y_);
116  tree.Branch("tc_z", &tc_z_);
117  tree.Branch("tc_cluster_id", &tc_cluster_id_);
118  tree.Branch("tc_multicluster_id", &tc_multicluster_id_);
119  tree.Branch("tc_multicluster_pt", &tc_multicluster_pt_);
120 
121 }
122 
123 void
125 fill(const edm::Event& e, const edm::EventSetup& es)
126 {
127 
128  // retrieve trigger cells
130  e.getByToken(trigger_cells_token_, trigger_cells_h);
131  const l1t::HGCalTriggerCellBxCollection& trigger_cells = *trigger_cells_h;
132 
133  // retrieve clusters
135  e.getByToken(multiclusters_token_, multiclusters_h);
136  const l1t::HGCalMulticlusterBxCollection& multiclusters = *multiclusters_h;
137 
138  // retrieve geometry
139  es.get<CaloGeometryRecord>().get(geometry_);
140 
141  // sim hit association
142  std::unordered_map<uint32_t, double> simhits_ee;
143  std::unordered_map<uint32_t, double> simhits_fh;
144  std::unordered_map<uint32_t, double> simhits_bh;
145  if(fill_simenergy_) simhits(e, simhits_ee, simhits_fh, simhits_bh);
146 
147  // Associate cells to clusters
148  std::unordered_map<uint32_t, uint32_t> cell2cluster;
149  std::unordered_map<uint32_t, l1t::HGCalMulticlusterBxCollection::const_iterator> cell2multicluster;
150  for(auto mcl_itr=multiclusters.begin(0); mcl_itr!=multiclusters.end(0); mcl_itr++)
151  {
152  // loop on 2D clusters inside 3D clusters
153  for(const auto& cl_ptr : mcl_itr->constituents())
154  {
155  // loop on TC inside 2D clusters
156  for(const auto& tc_ptr : cl_ptr.second->constituents())
157  {
158  cell2cluster.emplace(tc_ptr.second->detId(), cl_ptr.second->detId());
159  cell2multicluster.emplace(tc_ptr.second->detId(), mcl_itr);
160  }
161  }
162  }
163 
165 
166  clear();
167  for(auto tc_itr=trigger_cells.begin(0); tc_itr!=trigger_cells.end(0); tc_itr++)
168  {
169  if(tc_itr->hwPt()>0)
170  {
171  auto cl_itr = cell2cluster.find(tc_itr->detId());
172  auto mcl_itr = cell2multicluster.find(tc_itr->detId());
173  uint32_t cl_id = (cl_itr!=cell2cluster.end() ? cl_itr->second : 0);
174  uint32_t mcl_id = (mcl_itr!=cell2multicluster.end() ? mcl_itr->second->detId() : 0);
175  float mcl_pt = (mcl_itr!=cell2multicluster.end() ? mcl_itr->second->pt() : 0.);
176  // Filter cells not included in a multicluster, if requested
177  if(filter_cells_in_multiclusters_ && mcl_id==0) continue;
178  tc_n_++;
179  // hardware data
180  HGCalDetId id(tc_itr->detId());
181  tc_id_.emplace_back(tc_itr->detId());
182  tc_subdet_.emplace_back(id.subdetId());
183  tc_side_.emplace_back(id.zside());
184  tc_layer_.emplace_back(triggerTools_.layerWithOffset(id));
185  tc_wafer_.emplace_back(id.wafer());
186  tc_wafertype_.emplace_back(id.waferType());
187  tc_cell_.emplace_back(id.cell());
188  tc_data_.emplace_back(tc_itr->hwPt());
189  tc_mipPt_.emplace_back(tc_itr->mipPt());
190  // physical values
191  tc_pt_.emplace_back(tc_itr->pt());
192  tc_energy_.emplace_back(tc_itr->energy());
193  tc_eta_.emplace_back(tc_itr->eta());
194  tc_phi_.emplace_back(tc_itr->phi());
195  tc_x_.emplace_back(tc_itr->position().x());
196  tc_y_.emplace_back(tc_itr->position().y());
197  tc_z_.emplace_back(tc_itr->position().z());
198  // Links between TC and clusters
199  tc_cluster_id_.emplace_back(cl_id);
200  tc_multicluster_id_.emplace_back(mcl_id);
201  tc_multicluster_pt_.emplace_back(mcl_pt);
202 
203  if(fill_simenergy_)
204  {
205  double energy = 0;
206  int subdet = id.subdetId();
207  // search for simhit for all the cells inside the trigger cell
208  for(uint32_t c_id : geometry_->getCellsFromTriggerCell(id))
209  {
210  switch(subdet)
211  {
213  {
214  auto itr = simhits_ee.find(c_id);
215  if(itr!=simhits_ee.end())
216  {
217  HGCalDetId detid(c_id);
218  int thickness = geometry_->eeTopology().dddConstants().waferTypeL(detid.wafer())-1;
219  int layer = detid.layer();
220  energy += calibrate(itr->second, thickness, layer);
221  }
222  break;
223  }
225  {
226  auto itr = simhits_fh.find(c_id);
227  if(itr!=simhits_fh.end())
228  {
229  HGCalDetId detid(c_id);
230  int thickness = geometry_->fhTopology().dddConstants().waferTypeL(detid.wafer())-1;
231  int layer = detid.layer();
232  energy += calibrate(itr->second, thickness, layer);
233  }
234  break;
235  }
237  {
238  auto itr = simhits_bh.find(c_id);
239  if(itr!=simhits_bh.end()) energy += itr->second;
240  break;
241  }
242  default:
243  break;
244  }
245  }
246  tc_simenergy_.emplace_back(energy);
247  }
248  }
249  }
250 }
251 
252 double
254 calibrate(double energy, int thickness, int layer)
255 {
256  double fcPerMip = fcPerMip_[thickness];
257  double thicknessCorrection = thicknessCorrections_[thickness];
258  double layerWeight = layerWeights_[layer];
259  double TeV2GeV = 1.e3;
260  return energy*keV2fC_/fcPerMip*layerWeight*TeV2GeV/thicknessCorrection;
261 }
262 
263 void
265 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)
266 {
268  e.getByToken(simhits_ee_token_,ee_simhits_h);
269  const edm::PCaloHitContainer& ee_simhits = *ee_simhits_h;
271  e.getByToken(simhits_fh_token_,fh_simhits_h);
272  const edm::PCaloHitContainer& fh_simhits = *fh_simhits_h;
274  e.getByToken(simhits_bh_token_,bh_simhits_h);
275  const edm::PCaloHitContainer& bh_simhits = *bh_simhits_h;
276 
277  //EE
278  int layer=0,cell=0, sec=0, subsec=0, zp=0,subdet=0;
279  ForwardSubdetector mysubdet;
280  for( const auto& simhit : ee_simhits )
281  {
282  HGCalTestNumbering::unpackHexagonIndex(simhit.id(), subdet, zp, layer, sec, subsec, cell);
283  mysubdet = (ForwardSubdetector)subdet;
284  std::pair<int,int> recoLayerCell = geometry_->eeTopology().dddConstants().simToReco(cell,layer,sec,geometry_->eeTopology().detectorType());
285  cell = recoLayerCell.first;
286  layer = recoLayerCell.second;
287  if (layer<0 || cell<0) continue;
288  auto itr_insert = simhits_ee.emplace(HGCalDetId(mysubdet,zp,layer,subsec,sec,cell), 0.);
289  itr_insert.first->second += simhit.energy();
290  }
291 
292  // FH
293  layer=0; cell=0; sec=0; subsec=0; zp=0; subdet=0;
294  for( const auto& simhit : fh_simhits )
295  {
296  HGCalTestNumbering::unpackHexagonIndex(simhit.id(), subdet, zp, layer, sec, subsec, cell);
297  mysubdet = (ForwardSubdetector)(subdet);
298  std::pair<int,int> recoLayerCell = geometry_->fhTopology().dddConstants().simToReco(cell,layer,sec,geometry_->fhTopology().detectorType());
299  cell = recoLayerCell.first;
300  layer = recoLayerCell.second;
301  if (layer<0 || cell<0) continue;
302  auto itr_insert = simhits_fh.emplace(HGCalDetId(mysubdet,zp,layer,subsec,sec,cell), 0.);
303  itr_insert.first->second += simhit.energy();
304  }
305 
306  // BH
307  for( const auto& simhit : bh_simhits )
308  {
310  if (id.subdetId()!=HcalEndcap) continue;
311  auto itr_insert = simhits_bh.emplace(id, 0.);
312  itr_insert.first->second += simhit.energy();
313  }
314 }
315 
316 
317 void
320 {
321  tc_n_ = 0;
322  tc_id_.clear();
323  tc_subdet_.clear();
324  tc_side_.clear();
325  tc_layer_.clear();
326  tc_wafer_.clear();
327  tc_wafertype_.clear();
328  tc_cell_.clear();
329  tc_data_.clear();
330  tc_mipPt_.clear();
331  tc_pt_.clear();
332  tc_energy_.clear();
333  tc_simenergy_.clear();
334  tc_eta_.clear();
335  tc_phi_.clear();
336  tc_x_.clear();
337  tc_y_.clear();
338  tc_z_.clear();
339  tc_cluster_id_.clear();
340  tc_multicluster_id_.clear();
341  tc_multicluster_pt_.clear();
342 }
void initialize(TTree &, const edm::ParameterSet &, edm::ConsumesCollector &&) final
const_iterator end(int bx) const
T getParameter(std::string const &) const
edm::ESHandle< HGCalTriggerGeometryBase > geometry_
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
std::vector< PCaloHit > PCaloHitContainer
virtual geom_set getCellsFromTriggerCell(const unsigned cell_det_id) const =0
void eventSetup(const edm::EventSetup &)
void fill(const edm::Event &e, const edm::EventSetup &es) final
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
const HGCalTopology & eeTopology() const
HGCalTriggerNtupleHGCTriggerCells(const edm::ParameterSet &conf)
bool detectorType() const
unsigned layerWithOffset(const DetId &) const
ForwardSubdetector
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
const HcalTopology & bhTopology() const
int wafer() const
get the wafer #
Definition: HGCalDetId.h:42
int waferTypeL(int wafer) const
const HGCalDDDConstants & dddConstants() const
const T & get() const
Definition: EventSetup.h:55
#define DEFINE_EDM_PLUGIN(factory, type, name)
DetId relabel(const uint32_t testId) const
Definition: tree.py:1
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
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
const HGCalTopology & fhTopology() const
int layer() const
get the layer #
Definition: HGCalDetId.h:48