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;
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 
ConfigurationDescriptions.h
DDAxes::y
EDAnalyzer.h
mps_fire.i
i
Definition: mps_fire.py:428
HGCalTimingAnalyzer::tree_
TTree * tree_
Definition: HGCalTimingAnalyzer.cc:71
hit::id
unsigned int id
Definition: SiStripHitEffFromCalibTree.cc:92
HGCalTimingAnalyzer::idBeams_
std::vector< int > idBeams_
Definition: HGCalTimingAnalyzer.cc:66
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
HGCalTimingAnalyzer::tok_hepMC_
edm::EDGetTokenT< edm::HepMCProduct > tok_hepMC_
Definition: HGCalTimingAnalyzer.cc:70
ESHandle.h
HGCalTimingAnalyzer::simHitCellIdBeam_
std::vector< uint32_t > simHitCellIdBeam_
Definition: HGCalTimingAnalyzer.cc:72
HGCalTimingAnalyzer::simHitCellIdEE_
std::vector< uint32_t > simHitCellIdEE_
Definition: HGCalTimingAnalyzer.cc:72
edm::Run
Definition: Run.h:45
ecaldqm::zside
int zside(DetId const &)
Definition: EcalDQMCommonUtils.cc:189
ForwardSubdetector
ForwardSubdetector
Definition: ForwardSubdetector.h:4
edm::EDGetTokenT< edm::PCaloHitContainer >
HGCalTimingAnalyzer::endRun
void endRun(edm::Run const &, edm::EventSetup const &) override
Definition: HGCalTimingAnalyzer.cc:55
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
gpuVertexFinder::iv
int32_t *__restrict__ iv
Definition: gpuClusterTracksDBSCAN.h:42
gather_cfg.cout
cout
Definition: gather_cfg.py:144
HGCalTestNumbering::unpackHexagonIndex
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
Definition: HGCalTestNumbering.cc:46
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
HGCalTimingAnalyzer::detectorBeam_
std::string detectorBeam_
Definition: HGCalTimingAnalyzer.cc:64
protons_cff.time
time
Definition: protons_cff.py:39
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
HcalRecNumberingRecord.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
HGCalTimingAnalyzer::simHitCellTmBeam_
std::vector< float > simHitCellTmBeam_
Definition: HGCalTimingAnalyzer.cc:74
DDAxes::x
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
edm::Handle< edm::SimTrackContainer >
HGCalTimingAnalyzer::analyzeSimTracks
void analyzeSimTracks(edm::Handle< edm::SimTrackContainer > const &SimTk, edm::Handle< edm::SimVertexContainer > const &SimVtx)
Definition: HGCalTimingAnalyzer.cc:298
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
HGCalTimingAnalyzer::tok_hitsBeam_
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hitsBeam_
Definition: HGCalTimingAnalyzer.cc:67
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
HGCalDDDConstants
Definition: HGCalDDDConstants.h:27
HGCalTimingAnalyzer::yBeam_
double yBeam_
Definition: HGCalTimingAnalyzer.cc:75
MakerMacros.h
HGCalDDDConstants::simToReco
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
Definition: HGCalDDDConstants.cc:1018
edm::EventSetup::get
T get() const
Definition: EventSetup.h:87
HGCalTimingAnalyzer::timeUnit_
double timeUnit_
Definition: HGCalTimingAnalyzer.cc:65
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
HGCalTimingAnalyzer::fs_
edm::Service< TFileService > fs_
Definition: HGCalTimingAnalyzer.cc:61
HcalTestBeamNumbering.h
Service.h
HGCalTimingAnalyzer::analyzeSimHits
void analyzeSimHits(int type, std::vector< PCaloHit > const &hits)
Definition: HGCalTimingAnalyzer.cc:233
edm::ESHandle
Definition: DTSurvey.h:22
HGCalTimingAnalyzer::~HGCalTimingAnalyzer
~HGCalTimingAnalyzer() override
Definition: HGCalTimingAnalyzer.cc:121
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
dqmdumpme.k
k
Definition: dqmdumpme.py:60
first
auto first
Definition: CAHitNtupletGeneratorKernelsImpl.h:112
HGCalTimingAnalyzer::zBeam_
double zBeam_
Definition: HGCalTimingAnalyzer.cc:75
CaloGeometryRecord.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
phase1PixelTopology::layer
constexpr std::array< uint8_t, layerIndexSize > layer
Definition: phase1PixelTopology.h:99
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
HGCalTimingAnalyzer::beginJob
void beginJob() override
Definition: HGCalTimingAnalyzer.cc:138
TFileService.h
edm::HandleBase::clear
void clear()
Definition: HandleBase.h:51
HGCRecHitCollections.h
HGCalGeometry.h
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
HGCalTimingAnalyzer::doTree_
bool doTree_
Definition: HGCalTimingAnalyzer.cc:63
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
HcalTestBeamNumbering::unpackIndex
static void unpackIndex(const uint32_t &idx, int &det, int &lay, int &x, int &y)
Definition: HcalTestBeamNumbering.cc:31
HGCalDDDConstants::layers
unsigned int layers(bool reco) const
Definition: HGCalDDDConstants.cc:561
edm::Service< TFileService >
iEvent
int iEvent
Definition: GenABIO.cc:224
HGCalTimingAnalyzer::hgcons_
const HGCalDDDConstants * hgcons_
Definition: HGCalTimingAnalyzer.cc:62
HGCDigiCollections.h
HGCalTimingAnalyzer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: HGCalTimingAnalyzer.cc:123
HGCalTimingAnalyzer::analyze
void analyze(edm::Event const &, edm::EventSetup const &) override
Definition: HGCalTimingAnalyzer.cc:166
IdealGeometryRecord.h
HGCalTimingAnalyzer::pBeam_
double pBeam_
Definition: HGCalTimingAnalyzer.cc:75
edm::EventSetup
Definition: EventSetup.h:58
edm::HepMCProduct::GetEvent
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
get
#define get
HGCalTimingAnalyzer::HGCalTimingAnalyzer
HGCalTimingAnalyzer(edm::ParameterSet const &)
Definition: HGCalTimingAnalyzer.cc:78
InputTag.h
HGCalTimingAnalyzer::tok_simTk_
edm::EDGetTokenT< edm::SimTrackContainer > tok_simTk_
Definition: HGCalTimingAnalyzer.cc:68
HGCalTimingAnalyzer::tok_hitsEE_
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hitsEE_
Definition: HGCalTimingAnalyzer.cc:67
HGCalDetId
Definition: HGCalDetId.h:8
HGCalTimingAnalyzer::xBeam_
double xBeam_
Definition: HGCalTimingAnalyzer.cc:75
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
HGCalDetId.h
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
HGCalTimingAnalyzer::groupHits_
bool groupHits_
Definition: HGCalTimingAnalyzer.cc:63
Frameworkfwd.h
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
PCaloHitContainer.h
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
HGCalTimingAnalyzer::detectorEE_
std::string detectorEE_
Definition: HGCalTimingAnalyzer.cc:64
HGCalDDDConstants.h
ParameterSet.h
HGCalTimingAnalyzer::simHitCellEnBeam_
std::vector< float > simHitCellEnBeam_
Definition: HGCalTimingAnalyzer.cc:73
HepMCProduct.h
HGCalTimingAnalyzer::simHitCellTmEE_
std::vector< float > simHitCellTmEE_
Definition: HGCalTimingAnalyzer.cc:74
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
HGCalTimingAnalyzer::simHitCellEnEE_
std::vector< float > simHitCellEnEE_
Definition: HGCalTimingAnalyzer.cc:73
edm::Event
Definition: Event.h:73
crabWrapper.key
key
Definition: crabWrapper.py:19
HGCalTimingAnalyzer::beginRun
void beginRun(edm::Run const &, edm::EventSetup const &) override
Definition: HGCalTimingAnalyzer.cc:155
TauDecayModes.dec
dec
Definition: TauDecayModes.py:143
SimTrackContainer.h
edm::InputTag
Definition: InputTag.h:15
SimVertexContainer.h
IdealGeometryRecord
Definition: IdealGeometryRecord.h:25
HGCalTimingAnalyzer
Definition: HGCalTimingAnalyzer.cc:45
hit
Definition: SiStripHitEffFromCalibTree.cc:88
TFileService::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileService.h:64
HGCalTestNumbering.h
HGCalTimingAnalyzer::tok_simVtx_
edm::EDGetTokenT< edm::SimVertexContainer > tok_simVtx_
Definition: HGCalTimingAnalyzer.cc:69