CMS 3D CMS Logo

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