CMS 3D CMS Logo

HGCalTimingAnalyzer.cc
Go to the documentation of this file.
1 // system include files
2 #include <fstream>
3 #include <iostream>
4 #include <map>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 // user include files
14 
25 
37 
38 // Root objects
39 #include "TROOT.h"
40 #include "TSystem.h"
41 #include "TTree.h"
42 
43 //#define EDM_ML_DEBUG
44 
45 class HGCalTimingAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
46 public:
47  explicit HGCalTimingAnalyzer(edm::ParameterSet const&);
48  ~HGCalTimingAnalyzer() override;
49 
50  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
51 
52 private:
53  void beginJob() override;
54  void beginRun(edm::Run const&, edm::EventSetup const&) override;
55  void endRun(edm::Run const&, edm::EventSetup const&) override {}
56  void analyze(edm::Event const&, edm::EventSetup const&) override;
57  void analyzeSimHits(int type, std::vector<PCaloHit> const& hits);
60 
65  double timeUnit_;
66  std::vector<int> idBeams_;
71  TTree* tree_;
72  std::vector<uint32_t> simHitCellIdEE_, simHitCellIdBeam_;
73  std::vector<float> simHitCellEnEE_, simHitCellEnBeam_;
74  std::vector<float> simHitCellTmEE_, simHitCellTmBeam_;
76 };
77 
79  usesResource("TFileService");
80 
81  // now do whatever initialization is needed
82  detectorEE_ = iConfig.getParameter<std::string>("DetectorEE");
83  detectorBeam_ = iConfig.getParameter<std::string>("DetectorBeam");
84  // Group hits (if groupHits_ = true) if hits come within timeUnit_
85  groupHits_ = iConfig.getParameter<bool>("GroupHits");
86  timeUnit_ = iConfig.getParameter<double>("TimeUnit");
87  // Only look into the beam counters with ID's as in idBeams_
88  idBeams_ = iConfig.getParameter<std::vector<int>>("IDBeams");
89  doTree_ = iConfig.getUntrackedParameter<bool>("DoTree", false);
90  if (!groupHits_)
91  timeUnit_ = 0.000001;
92 #ifdef EDM_ML_DEBUG
93  std::cout << "HGCalTimingAnalyzer:: Group Hits " << groupHits_ << " in " << timeUnit_ << " IdBeam " << idBeams_.size()
94  << ":";
95  for (const auto& id : idBeams_)
96  std::cout << " " << id;
97  std::cout << std::endl;
98 #endif
99  if (idBeams_.empty())
100  idBeams_.push_back(1001);
101 
102  edm::InputTag tmp0 = iConfig.getParameter<edm::InputTag>("GeneratorSrc");
103  tok_hepMC_ = consumes<edm::HepMCProduct>(tmp0);
104 #ifdef EDM_ML_DEBUG
105  std::cout << "HGCalTimingAnalyzer:: GeneratorSource = " << tmp0 << std::endl;
106 #endif
107  tok_simTk_ = consumes<edm::SimTrackContainer>(edm::InputTag("g4SimHits"));
108  tok_simVtx_ = consumes<edm::SimVertexContainer>(edm::InputTag("g4SimHits"));
109  std::string tmp1 = iConfig.getParameter<std::string>("CaloHitSrcEE");
110  tok_hitsEE_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits", tmp1));
111 #ifdef EDM_ML_DEBUG
112  std::cout << "HGCalTimingAnalyzer:: Detector " << detectorEE_ << " with tags " << tmp1 << std::endl;
113 #endif
114  tmp1 = iConfig.getParameter<std::string>("CaloHitSrcBeam");
115  tok_hitsBeam_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits", tmp1));
116 #ifdef EDM_ML_DEBUG
117  std::cout << "HGCalTimingAnalyzer:: Detector " << detectorBeam_ << " with tags " << tmp1 << std::endl;
118 #endif
119 }
120 
122 
125  desc.add<std::string>("DetectorEE", "HGCalEESensitive");
126  desc.add<std::string>("DetectorBeam", "HcalTB06BeamDetector");
127  desc.add<bool>("GroupHits", false);
128  desc.add<double>("TimeUnit", 0.001);
129  std::vector<int> ids = {1001, 1002, 1003, 1004, 1005};
130  desc.add<std::vector<int>>("IDBeams", ids);
131  desc.addUntracked<bool>("DoTree", true);
132  desc.add<edm::InputTag>("GeneratorSrc", edm::InputTag("generatorSmeared"));
133  desc.add<std::string>("CaloHitSrcEE", "HGCHitsEE");
134  desc.add<std::string>("CaloHitSrcBeam", "HcalTB06BeamHits");
135  descriptions.add("HGCalTimingAnalyzer", desc);
136 }
137 
140  if (doTree_) {
141  tree_ = fs_->make<TTree>("HGCTB", "SimHitEnergy");
142  tree_->Branch("xBeam", &xBeam_, "xBeam/D");
143  tree_->Branch("yBeam", &yBeam_, "yBeam/D");
144  tree_->Branch("zBeam", &zBeam_, "zBeam/D");
145  tree_->Branch("pBeam", &pBeam_, "pBeam/D");
146  tree_->Branch("simHitCellIdEE_", &simHitCellIdEE_);
147  tree_->Branch("simHitCellEnEE_", &simHitCellEnEE_);
148  tree_->Branch("simHitCellTmEE_", &simHitCellTmEE_);
149  tree_->Branch("simHitCellIdBeam_", &simHitCellIdBeam_);
150  tree_->Branch("simHitCellEnBeam_", &simHitCellEnBeam_);
151  tree_->Branch("simHitCellTmBeam_", &simHitCellTmBeam_);
152  }
153 }
154 
157  iSetup.get<IdealGeometryRecord>().get(detectorEE_, pHGDC);
158  hgcons_ = &(*pHGDC);
159 
160 #ifdef EDM_ML_DEBUG
161  std::cout << "HGCalTimingAnalyzer::" << detectorEE_ << " defined with " << hgcons_->layers(false) << " layers"
162  << std::endl;
163 #endif
164 }
165 
167 #ifdef EDM_ML_DEBUG
168  // Generator input
170  iEvent.getByToken(tok_hepMC_, evtMC);
171  if (!evtMC.isValid()) {
172  edm::LogWarning("HGCal") << "no HepMCProduct found";
173  } else {
174  const HepMC::GenEvent* myGenEvent = evtMC->GetEvent();
175  unsigned int k(0);
176  for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
177  ++p, ++k) {
178  std::cout << "Particle[" << k << "] with p " << (*p)->momentum().rho() << " theta " << (*p)->momentum().theta()
179  << " phi " << (*p)->momentum().phi() << std::endl;
180  }
181  }
182 #endif
183 
184  // Now the Simhits
186  iEvent.getByToken(tok_simTk_, SimTk);
188  iEvent.getByToken(tok_simVtx_, SimVtx);
189  analyzeSimTracks(SimTk, SimVtx);
190 
191  simHitCellIdEE_.clear();
192  simHitCellIdBeam_.clear();
193  simHitCellEnEE_.clear();
194  simHitCellEnBeam_.clear();
195  simHitCellTmEE_.clear();
196  simHitCellTmBeam_.clear();
197 
198  edm::Handle<edm::PCaloHitContainer> theCaloHitContainers;
199  std::vector<PCaloHit> caloHits;
200  iEvent.getByToken(tok_hitsEE_, theCaloHitContainers);
201  if (theCaloHitContainers.isValid()) {
202 #ifdef EDM_ML_DEBUG
203  std::cout << "PcalohitContainer for " << detectorEE_ << " has " << theCaloHitContainers->size() << " hits"
204  << std::endl;
205 #endif
206  caloHits.clear();
207  caloHits.insert(caloHits.end(), theCaloHitContainers->begin(), theCaloHitContainers->end());
208  analyzeSimHits(0, caloHits);
209  } else {
210 #ifdef EDM_ML_DEBUG
211  std::cout << "PCaloHitContainer does not exist for " << detectorEE_ << " !!!" << std::endl;
212 #endif
213  }
214 
215  iEvent.getByToken(tok_hitsBeam_, theCaloHitContainers);
216  if (theCaloHitContainers.isValid()) {
217 #ifdef EDM_ML_DEBUG
218  std::cout << "PcalohitContainer for " << detectorBeam_ << " has " << theCaloHitContainers->size() << " hits"
219  << std::endl;
220 #endif
221  caloHits.clear();
222  caloHits.insert(caloHits.end(), theCaloHitContainers->begin(), theCaloHitContainers->end());
223  analyzeSimHits(1, caloHits);
224  } else {
225 #ifdef EDM_ML_DEBUG
226  std::cout << "PCaloHitContainer does not exist for " << detectorBeam_ << " !!!" << std::endl;
227 #endif
228  }
229  if (doTree_)
230  tree_->Fill();
231 }
232 
233 void HGCalTimingAnalyzer::analyzeSimHits(int type, std::vector<PCaloHit> const& hits) {
234 #ifdef EDM_ML_DEBUG
235  unsigned int i(0);
236 #endif
237  std::map<std::pair<uint32_t, uint64_t>, std::pair<double, double>> map_hits;
238  for (const auto& hit : hits) {
239  double energy = hit.energy();
240  double time = hit.time();
241  uint32_t id = hit.id();
242  if (type == 0) {
243  int subdet, zside, layer, sector, subsector, cell;
244  HGCalTestNumbering::unpackHexagonIndex(id, subdet, zside, layer, sector, subsector, cell);
245  std::pair<int, int> recoLayerCell = hgcons_->simToReco(cell, layer, sector, true);
246  id = HGCalDetId((ForwardSubdetector)(subdet), zside, recoLayerCell.second, subsector, sector, recoLayerCell.first)
247  .rawId();
248 #ifdef EDM_ML_DEBUG
249  std::cout << "SimHit:Hit[" << i << "] Id " << subdet << ":" << zside << ":" << layer << ":" << sector << ":"
250  << subsector << ":" << recoLayerCell.first << ":" << recoLayerCell.second << " Energy " << energy
251  << " Time " << time << std::endl;
252 #endif
253  } else {
254 #ifdef EDM_ML_DEBUG
255  int subdet, layer, x, y;
256  HcalTestBeamNumbering::unpackIndex(id, subdet, layer, x, y);
257  std::cout << "SimHit:Hit[" << i << "] Beam Subdet " << subdet << " Layer " << layer << " x|y " << x << ":" << y
258  << " Energy " << energy << " Time " << time << std::endl;
259 #endif
260  }
261  uint64_t tid = (uint64_t)((time + 50.0) / timeUnit_);
262  std::pair<uint32_t, uint64_t> key(id, tid);
263  auto itr = map_hits.find(key);
264  if (itr == map_hits.end()) {
265  map_hits[key] = std::pair<double, double>(time, 0.0);
266  itr = map_hits.find(key);
267  }
268  energy += (itr->second).second;
269  map_hits[key] = std::pair<double, double>((itr->second).first, energy);
270 #ifdef EDM_ML_DEBUG
271  ++i;
272 #endif
273  }
274 
275 #ifdef EDM_ML_DEBUG
276  std::cout << "analyzeSimHits: Finds " << map_hits.size() << " hits "
277  << " from the Hit Vector of size " << hits.size() << " for type " << type << std::endl;
278 #endif
279  for (const auto& itr : map_hits) {
280  uint32_t id = (itr.first).first;
281  double time = (itr.second).first;
282  double energy = (itr.second).second;
283  if (type == 0) {
284  simHitCellIdEE_.push_back(id);
285  simHitCellEnEE_.push_back(energy);
286  simHitCellTmEE_.push_back(time);
287  } else {
288  simHitCellIdBeam_.push_back(id);
289  simHitCellEnBeam_.push_back(energy);
290  simHitCellTmBeam_.push_back(time);
291  }
292 #ifdef EDM_ML_DEBUG
293  std::cout << "SimHit::ID: " << std::hex << id << std::dec << " T: " << time << " E: " << energy << std::endl;
294 #endif
295  }
296 }
297 
299  edm::Handle<edm::SimVertexContainer> const& SimVtx) {
300  xBeam_ = yBeam_ = zBeam_ = pBeam_ = -1000000;
301  int vertIndex(-1);
302  for (edm::SimTrackContainer::const_iterator simTrkItr = SimTk->begin(); simTrkItr != SimTk->end(); simTrkItr++) {
303 #ifdef EDM_ML_DEBUG
304  std::cout << "Track " << simTrkItr->trackId() << " Vertex " << simTrkItr->vertIndex() << " Type "
305  << simTrkItr->type() << " Charge " << simTrkItr->charge() << " momentum " << simTrkItr->momentum() << " "
306  << simTrkItr->momentum().P() << std::endl;
307 #endif
308  if (vertIndex == -1) {
309  vertIndex = simTrkItr->vertIndex();
310  pBeam_ = simTrkItr->momentum().P();
311  }
312  }
313  if (vertIndex != -1 && vertIndex < (int)SimVtx->size()) {
314  edm::SimVertexContainer::const_iterator simVtxItr = SimVtx->begin();
315  for (int iv = 0; iv < vertIndex; iv++)
316  simVtxItr++;
317 #ifdef EDM_ML_DEBUG
318  std::cout << "Vertex " << vertIndex << " position " << simVtxItr->position() << std::endl;
319 #endif
320  xBeam_ = simVtxItr->position().X();
321  yBeam_ = simVtxItr->position().Y();
322  zBeam_ = simVtxItr->position().Z();
323  }
324 }
325 
326 // define this as a plug-in
327 
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
const HGCalDDDConstants * hgcons_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
void analyze(edm::Event const &, edm::EventSetup const &) override
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
edm::EDGetTokenT< edm::SimTrackContainer > tok_simTk_
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hitsEE_
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
int zside(DetId const &)
ForwardSubdetector
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
U second(std::pair< T, U > const &p)
HGCalTimingAnalyzer(edm::ParameterSet const &)
edm::EDGetTokenT< edm::HepMCProduct > tok_hepMC_
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
int iEvent
Definition: GenABIO.cc:224
std::vector< float > simHitCellEnEE_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int layers(bool reco) const
std::vector< float > simHitCellTmBeam_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
std::vector< int > idBeams_
std::vector< float > simHitCellEnBeam_
std::vector< uint32_t > simHitCellIdBeam_
std::vector< float > simHitCellTmEE_
std::vector< uint32_t > simHitCellIdEE_
unsigned int id
void endRun(edm::Run const &, edm::EventSetup const &) override
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
edm::Service< TFileService > fs_
unsigned long long uint64_t
Definition: Time.h:13
edm::EDGetTokenT< edm::SimVertexContainer > tok_simVtx_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
T get() const
Definition: EventSetup.h:73
void beginRun(edm::Run const &, edm::EventSetup const &) override
static void unpackIndex(const uint32_t &idx, int &det, int &lay, int &x, int &y)
void analyzeSimHits(int type, std::vector< PCaloHit > const &hits)
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
void analyzeSimTracks(edm::Handle< edm::SimTrackContainer > const &SimTk, edm::Handle< edm::SimVertexContainer > const &SimVtx)
Definition: Run.h:45
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hitsBeam_