File indexing completed on 2024-07-16 22:52:43
0001
0002 #include <cmath>
0003 #include <iostream>
0004 #include <fstream>
0005 #include <vector>
0006 #include <map>
0007 #include <string>
0008
0009 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0010
0011 #include "DataFormats/ForwardDetId/interface/ForwardSubdetector.h"
0012 #include "DataFormats/ForwardDetId/interface/HFNoseDetId.h"
0013 #include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
0014 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0015 #include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
0016 #include "DataFormats/HGCDigi/interface/HGCDigiCollections.h"
0017
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ServiceRegistry/interface/Service.h"
0026 #include "FWCore/Utilities/interface/transform.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028
0029 #include "Geometry/HGCalCommonData/interface/HGCalGeometryMode.h"
0030 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0031 #include "Geometry/HGCalGeometry/interface/HGCalGeometry.h"
0032 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0033
0034 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0035 #include "SimDataFormats/CaloTest/interface/HGCalTestNumbering.h"
0036 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
0037
0038 #include <CLHEP/Geometry/Point3D.h>
0039 #include <CLHEP/Geometry/Transform3D.h>
0040 #include <CLHEP/Geometry/Vector3D.h>
0041 #include <CLHEP/Units/SystemOfUnits.h>
0042 #include <CLHEP/Units/GlobalPhysicalConstants.h>
0043
0044 #include "TH2D.h"
0045
0046 class HGCalWaferStudy : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0047 public:
0048 explicit HGCalWaferStudy(const edm::ParameterSet&);
0049 ~HGCalWaferStudy() override = default;
0050 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0051
0052 protected:
0053 void beginJob() override {}
0054 void beginRun(edm::Run const&, edm::EventSetup const&) override;
0055 void analyze(edm::Event const&, edm::EventSetup const&) override;
0056 void endRun(edm::Run const&, edm::EventSetup const&) override {}
0057
0058 private:
0059 void analyzeHits(int, const std::string&, const std::vector<PCaloHit>&);
0060
0061
0062 const std::vector<std::string> nameDetectors_, caloHitSources_;
0063 const std::vector<edm::InputTag> digiSources_;
0064 const std::vector<edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord>> ddTokens_;
0065 const std::vector<edm::ESGetToken<HGCalGeometry, IdealGeometryRecord>> geomTokens_;
0066 const int verbosity_, nBinHit_, nBinDig_;
0067 const double xyMinHit_, xyMaxHit_;
0068 const double xyMinDig_, xyMaxDig_;
0069 const bool ifNose_;
0070 std::vector<int> layerMnSim_, layerMxSim_;
0071 std::vector<int> layerMnDig_, layerMxDig_;
0072 std::vector<int> layerSim_, layerDig_, layerFront_;
0073 std::vector<const HGCalDDDConstants*> hgcons_;
0074 std::vector<const HGCalGeometry*> hgeoms_;
0075 std::vector<edm::EDGetTokenT<edm::PCaloHitContainer>> tok_hits_;
0076 std::vector<edm::EDGetTokenT<HGCalDigiCollection>> tok_digi_;
0077
0078
0079 static const int nType = 2;
0080 std::vector<TH2D*> h_XYsi1_[nType], h_XYsi2_[nType];
0081 std::vector<TH2D*> h_XYdi1_[nType], h_XYdi2_[nType];
0082 };
0083
0084 HGCalWaferStudy::HGCalWaferStudy(const edm::ParameterSet& iConfig)
0085 : nameDetectors_(iConfig.getParameter<std::vector<std::string>>("detectorNames")),
0086 caloHitSources_(iConfig.getParameter<std::vector<std::string>>("caloHitSources")),
0087 digiSources_(iConfig.getParameter<std::vector<edm::InputTag>>("digiSources")),
0088 ddTokens_{
0089 edm::vector_transform(nameDetectors_,
0090 [this](const std::string& name) {
0091 return esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(
0092 edm::ESInputTag{"", name});
0093 })},
0094 geomTokens_{edm::vector_transform(
0095 nameDetectors_,
0096 [this](const std::string& name) {
0097 return esConsumes<HGCalGeometry, IdealGeometryRecord, edm::Transition::BeginRun>(edm::ESInputTag{"", name});
0098 })},
0099 verbosity_(iConfig.getUntrackedParameter<int>("verbosity", 0)),
0100 nBinHit_(iConfig.getUntrackedParameter<int>("nBinHit", 600)),
0101 nBinDig_(iConfig.getUntrackedParameter<int>("nBinDig", 600)),
0102 xyMinHit_(iConfig.getUntrackedParameter<double>("xyMinHit", -3000.0)),
0103 xyMaxHit_(iConfig.getUntrackedParameter<double>("xyMaxHit", 3000.0)),
0104 xyMinDig_(iConfig.getUntrackedParameter<double>("xyMinDig", -300.0)),
0105 xyMaxDig_(iConfig.getUntrackedParameter<double>("xyMaxDig", 300.0)),
0106 ifNose_(iConfig.getUntrackedParameter<bool>("ifNose", false)),
0107 layerMnSim_(iConfig.getUntrackedParameter<std::vector<int>>("layerMinSim")),
0108 layerMxSim_(iConfig.getUntrackedParameter<std::vector<int>>("layerMaxSim")),
0109 layerMnDig_(iConfig.getUntrackedParameter<std::vector<int>>("layerMinDig")),
0110 layerMxDig_(iConfig.getUntrackedParameter<std::vector<int>>("layerMaxDig")) {
0111 usesResource(TFileService::kSharedResource);
0112
0113 for (auto const& source : caloHitSources_) {
0114 tok_hits_.emplace_back(consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits", source)));
0115 edm::LogVerbatim("HGCalValidation") << "SimHitSource: " << source;
0116 }
0117 for (auto const& source : digiSources_) {
0118 tok_digi_.emplace_back(consumes<HGCalDigiCollection>(source));
0119 edm::LogVerbatim("HGCalValidation") << "DigiSource: " << source;
0120 }
0121 }
0122
0123 void HGCalWaferStudy::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0124 edm::ParameterSetDescription desc;
0125 std::vector<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
0126 std::vector<std::string> simSources = {"HGCHitsEE", "HGCHitsHEfront"};
0127 std::vector<edm::InputTag> digSources = {edm::InputTag("simHGCalUnsuppressedDigis", "EE"),
0128 edm::InputTag("simHGCalUnsuppressedDigis", "HEfront")};
0129 std::vector<int> layers = {28, 24};
0130 std::vector<int> layerMin = {1, 1};
0131 desc.add<std::vector<std::string>>("detectorNames", names);
0132 desc.add<std::vector<std::string>>("caloHitSources", simSources);
0133 desc.add<std::vector<edm::InputTag>>("digiSources", digSources);
0134 desc.addUntracked<int>("verbosity", 0);
0135 desc.addUntracked<int>("nBinHit", 600);
0136 desc.addUntracked<int>("nBinDig", 600);
0137 desc.addUntracked<double>("xyMinHit", -3000.0);
0138 desc.addUntracked<double>("xyMaxHit", 3000.0);
0139 desc.addUntracked<double>("xyMinDig", -300.0);
0140 desc.addUntracked<double>("xyMaxDig", 300.0);
0141 desc.addUntracked<bool>("ifNose", false);
0142 desc.addUntracked<std::vector<int>>("layerMaxSim", layers);
0143 desc.addUntracked<std::vector<int>>("layerMaxDig", layers);
0144 desc.addUntracked<std::vector<int>>("layerMinSim", layerMin);
0145 desc.addUntracked<std::vector<int>>("layerMinDig", layerMin);
0146 descriptions.add("hgcalWaferStudy", desc);
0147 }
0148
0149 void HGCalWaferStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0150
0151 for (unsigned int k = 0; k < tok_hits_.size(); ++k) {
0152 const edm::Handle<edm::PCaloHitContainer>& theCaloHitContainers = iEvent.getHandle(tok_hits_[k]);
0153 if (theCaloHitContainers.isValid()) {
0154 if (verbosity_ > 0)
0155 edm::LogVerbatim("HGCalValidation")
0156 << " PcalohitItr = " << theCaloHitContainers->size() << " Geometry Pointer " << hgcons_[k] << " # of Hists "
0157 << h_XYsi1_[k].size() << ":" << h_XYsi2_[k].size();
0158 int kount(0);
0159 for (auto const& hit : *(theCaloHitContainers)) {
0160 unsigned int id = hit.id();
0161 std::pair<float, float> xy;
0162 int layer(0), zside(1);
0163 bool wvtype(true);
0164 if (ifNose_) {
0165 HFNoseDetId detId = HFNoseDetId(id);
0166 layer = detId.layer();
0167 zside = detId.zside();
0168 wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0169 xy = hgcons_[k]->locateCell(zside,
0170 layer,
0171 detId.waferU(),
0172 detId.waferV(),
0173 detId.cellU(),
0174 detId.cellV(),
0175 false,
0176 true,
0177 false,
0178 false,
0179 false);
0180 } else if (hgcons_[k]->waferHexagon8()) {
0181 HGCSiliconDetId detId = HGCSiliconDetId(id);
0182 layer = detId.layer();
0183 zside = detId.zside();
0184 wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0185 xy = hgcons_[k]->locateCell(zside,
0186 layer,
0187 detId.waferU(),
0188 detId.waferV(),
0189 detId.cellU(),
0190 detId.cellV(),
0191 false,
0192 true,
0193 false,
0194 false,
0195 false);
0196 } else {
0197 int subdet, sector, type, cell;
0198 HGCalTestNumbering::unpackHexagonIndex(id, subdet, zside, layer, sector, type, cell);
0199 xy = hgcons_[k]->locateCell(cell, layer, sector, false);
0200 wvtype = hgcons_[k]->waferVirtual(layer, sector, 0);
0201 }
0202 double xp = (zside < 0) ? -xy.first : xy.first;
0203 double yp = xy.second;
0204 int ll = layer - layerMnSim_[k];
0205 if (verbosity_ > 1)
0206 edm::LogVerbatim("HGCalValidation")
0207 << "Hit [" << kount << "] Layer " << layer << ":" << ll << " x|y " << xp << ":" << yp << " Type "
0208 << wvtype << " Hist pointers " << h_XYsi1_[k][ll] << ":" << h_XYsi2_[k][ll];
0209 if (layer >= layerMnSim_[k] && layer <= layerMxSim_[k]) {
0210 if (wvtype)
0211 h_XYsi2_[k][ll]->Fill(xp, yp);
0212 else
0213 h_XYsi1_[k][ll]->Fill(xp, yp);
0214 }
0215 ++kount;
0216 }
0217 } else if (verbosity_ > 0) {
0218 edm::LogVerbatim("HGCalValidation") << "PCaloHitContainer does not "
0219 << "exist for " << nameDetectors_[k];
0220 }
0221 }
0222
0223
0224 for (unsigned int k = 0; k < tok_digi_.size(); ++k) {
0225 const edm::Handle<HGCalDigiCollection>& theHGCDigiContainer = iEvent.getHandle(tok_digi_[k]);
0226 if (theHGCDigiContainer.isValid()) {
0227 if (verbosity_ > 0)
0228 edm::LogVerbatim("HGCalValidation")
0229 << nameDetectors_[k] << " with " << theHGCDigiContainer->size() << " element(s) Geometry Pointer "
0230 << hgeoms_[k] << " # of Hists " << h_XYdi1_[k].size() << ":" << h_XYdi2_[k].size();
0231 int kount(0);
0232 for (const auto& it : *(theHGCDigiContainer.product())) {
0233 DetId id = it.id();
0234 int layer(0);
0235 bool wvtype(true);
0236 if (ifNose_) {
0237 HFNoseDetId detId = HFNoseDetId(id);
0238 layer = detId.layer();
0239 wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0240 } else if (hgcons_[k]->waferHexagon8()) {
0241 HGCSiliconDetId detId = HGCSiliconDetId(id);
0242 layer = detId.layer();
0243 wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0244 } else {
0245 HGCalDetId detId = HGCalDetId(id);
0246 layer = detId.layer();
0247 int wafer = detId.wafer();
0248 wvtype = hgcons_[k]->waferVirtual(layer, wafer, 0);
0249 }
0250 int ll = layer - layerMnDig_[k];
0251 const GlobalPoint& gcoord = hgeoms_[k]->getPosition(id);
0252 double xp = gcoord.x();
0253 double yp = gcoord.y();
0254 if (verbosity_ > 1)
0255 edm::LogVerbatim("HGCalValidation")
0256 << "Digi [" << kount << "] Layer " << layer << ":" << ll << " x|y " << xp << ":" << yp << " Type "
0257 << wvtype << " Hist pointers " << h_XYdi1_[k][ll] << ":" << h_XYdi2_[k][ll];
0258 if (layer >= layerMnDig_[k] && layer <= layerMxDig_[k]) {
0259 if (wvtype)
0260 h_XYdi2_[k][ll]->Fill(xp, yp);
0261 else
0262 h_XYdi1_[k][ll]->Fill(xp, yp);
0263 }
0264 ++kount;
0265 }
0266 } else {
0267 edm::LogVerbatim("HGCalValidation")
0268 << "DigiCollection handle " << digiSources_[k] << " does not exist for " << nameDetectors_[k] << " !!!";
0269 }
0270 }
0271 }
0272
0273
0274 void HGCalWaferStudy::beginRun(const edm::Run&, const edm::EventSetup& iSetup) {
0275 for (unsigned int k = 0; k < nameDetectors_.size(); ++k) {
0276 const auto& pHGDC = iSetup.getData(ddTokens_[k]);
0277 hgcons_.emplace_back(&pHGDC);
0278 layerSim_.emplace_back(hgcons_.back()->layers(false));
0279 layerDig_.emplace_back(hgcons_.back()->layers(true));
0280 layerFront_.emplace_back(hgcons_.back()->firstLayer());
0281 layerMnSim_[k] = std::max(layerMnSim_[k], layerFront_[k]);
0282 layerMnDig_[k] = std::max(layerMnDig_[k], layerFront_[k]);
0283 layerMxSim_[k] = std::min((layerFront_[k] + layerSim_[k] - 1), layerMxSim_[k]);
0284 layerMxDig_[k] = std::min((layerFront_[k] + layerDig_[k] - 1), layerMxDig_[k]);
0285
0286 const auto& geom = iSetup.getData(geomTokens_[k]);
0287 hgeoms_.emplace_back(&geom);
0288 if (verbosity_ > 0)
0289 edm::LogVerbatim("HGCalValidation")
0290 << nameDetectors_[k] << " defined with " << layerFront_[k] << ":" << layerSim_[k] << ":" << layerDig_[k]
0291 << " Layers which gives limits " << layerMnSim_[k] << ":" << layerMxSim_[k] << " for Sim " << layerMnDig_[k]
0292 << ":" << layerMxDig_[k] << " for Digi "
0293 << "\nLimits for plots " << nBinHit_ << ":" << xyMinHit_ << ":" << xyMaxHit_ << " (Sim) " << nBinDig_ << ":"
0294 << xyMinDig_ << ":" << xyMaxDig_ << " (Digi) with Pointers " << hgcons_.back() << ":" << hgeoms_.back();
0295 }
0296
0297
0298 edm::Service<TFileService> fs;
0299 std::ostringstream name, title;
0300 for (unsigned int ih = 0; ih <= nameDetectors_.size(); ++ih) {
0301 for (int i = layerMnSim_[ih]; i <= layerMxSim_[ih]; ++i) {
0302 name.str("");
0303 title.str("");
0304 name << "XY_" << nameDetectors_[ih] << "L" << i << "SimR";
0305 title << "y vs x (Layer " << i << ") real wafers for " << nameDetectors_[ih] << " at SimHit level";
0306 if (verbosity_ > 0)
0307 edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0308 h_XYsi1_[ih].emplace_back(fs->make<TH2D>(
0309 name.str().c_str(), title.str().c_str(), nBinHit_, xyMinHit_, xyMaxHit_, nBinHit_, xyMinHit_, xyMaxHit_));
0310 name.str("");
0311 title.str("");
0312 name << "XY_" << nameDetectors_[ih] << "L" << i << "SimV";
0313 title << "y vs x (Layer " << i << ") virtual wafers for " << nameDetectors_[ih] << " at SimHit level";
0314 if (verbosity_ > 0)
0315 edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0316 h_XYsi2_[ih].emplace_back(fs->make<TH2D>(
0317 name.str().c_str(), title.str().c_str(), nBinHit_, xyMinHit_, xyMaxHit_, nBinHit_, xyMinHit_, xyMaxHit_));
0318 }
0319 edm::LogVerbatim("HGCalValidation") << "Complete booking of Sim Plots for " << nameDetectors_[ih];
0320
0321 for (int i = layerMnDig_[ih]; i <= layerMxDig_[ih]; ++i) {
0322 name.str("");
0323 title.str("");
0324 name << "XY_" << nameDetectors_[ih] << "L" << i << "DigR";
0325 title << "y vs x (Layer " << i << ") real wafers for " << nameDetectors_[ih] << " at Digi level";
0326 if (verbosity_ > 0)
0327 edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0328 h_XYdi1_[ih].emplace_back(fs->make<TH2D>(
0329 name.str().c_str(), title.str().c_str(), nBinDig_, xyMinDig_, xyMaxDig_, nBinDig_, xyMinDig_, xyMaxDig_));
0330 name.str("");
0331 title.str("");
0332 name << "XY_" << nameDetectors_[ih] << "L" << i << "DigV";
0333 title << "y vs x (Layer " << i << ") virtual wafers for " << nameDetectors_[ih] << " at Digi level";
0334 if (verbosity_ > 0)
0335 edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0336 h_XYdi2_[ih].emplace_back(fs->make<TH2D>(
0337 name.str().c_str(), title.str().c_str(), nBinDig_, xyMinDig_, xyMaxDig_, nBinDig_, xyMinDig_, xyMaxDig_));
0338 }
0339 edm::LogVerbatim("HGCalValidation") << "Complete booking of Digi Plots for " << nameDetectors_[ih];
0340 }
0341 }
0342
0343 #include "FWCore/Framework/interface/MakerMacros.h"
0344
0345 DEFINE_FWK_MODULE(HGCalWaferStudy);