File indexing completed on 2025-07-22 22:30:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <fstream>
0022 #include <iostream>
0023 #include <sstream>
0024 #include <string>
0025 #include <vector>
0026 #include <cmath>
0027
0028
0029 #include "FWCore/Framework/interface/Frameworkfwd.h"
0030 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0031 #include "FWCore/Framework/interface/Event.h"
0032 #include "FWCore/Framework/interface/EventSetup.h"
0033 #include "FWCore/Framework/interface/MakerMacros.h"
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 #include "FWCore/ParameterSet/interface/FileInPath.h"
0036 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0037 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0038 #include "FWCore/Utilities/interface/transform.h"
0039
0040 #include "DataFormats/DetId/interface/DetId.h"
0041 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0042 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0043 #include "Geometry/HGCalCommonData/interface/HGCalGeomUtils.h"
0044 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0045 #include "Geometry/HGCalCommonData/interface/HGCalCell.h"
0046 #include "Geometry/HGCalCommonData/interface/HGCalWaferIndex.h"
0047
0048 class HGCalTestDDDCons : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0049 public:
0050 explicit HGCalTestDDDCons(const edm::ParameterSet &);
0051 ~HGCalTestDDDCons() override = default;
0052 static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0053
0054 void beginJob() override {}
0055 void beginRun(edm::Run const &, edm::EventSetup const &) override;
0056 void analyze(edm::Event const &iEvent, edm::EventSetup const &) override {}
0057 void endRun(edm::Run const &, edm::EventSetup const &) override {}
0058 void endJob() override {}
0059
0060 private:
0061 const std::vector<std::string> nameDetectors_;
0062 const std::string fileName_;
0063 const std::vector<edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord>> tok_hgcal_;
0064 int size;
0065 std::vector<const HGCalDDDConstants *> hgcCons_;
0066 std::vector<std::pair<DetId, uint32_t>> detIds_;
0067 std::vector<double> xwafer_, ywafer_, xcell_, ycell_, xcellOff_, ycellOff_;
0068 };
0069
0070 HGCalTestDDDCons::HGCalTestDDDCons(const edm::ParameterSet &iC)
0071 : nameDetectors_(iC.getParameter<std::vector<std::string>>("nameDetectors")),
0072 fileName_(iC.getParameter<std::string>("fileName")),
0073 tok_hgcal_{edm::vector_transform(nameDetectors_, [this](const std::string &name) {
0074 return esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(edm::ESInputTag{"", name});
0075 })} {
0076 std::ostringstream st1;
0077 for (const auto &name : nameDetectors_)
0078 st1 << " : " << name;
0079 edm::LogVerbatim("HGCGeom") << "Test validity of cells for " << nameDetectors_.size() << " detectors" << st1.str()
0080 << " with inputs from " << fileName_;
0081 if (!fileName_.empty()) {
0082 edm::FileInPath filetmp("Geometry/HGCalCommonData/data/" + fileName_);
0083 std::string fileName = filetmp.fullPath();
0084 std::ifstream fInput(fileName.c_str());
0085 if (!fInput.good()) {
0086 edm::LogVerbatim("HGCGeom") << "Cannot open file " << fileName;
0087 } else {
0088 char buffer[200];
0089 const std::vector<DetId::Detector> dets = {DetId::HGCalEE, DetId::HGCalHSi, DetId::HGCalHSc};
0090 while (fInput.getline(buffer, 200)) {
0091 std::vector<std::string> items = HGCalGeomUtils::splitString(std::string(buffer));
0092 if (items.size() == 14) {
0093 DetId::Detector det = static_cast<DetId::Detector>(std::atoi(items[0].c_str()));
0094 auto itr = std::find(dets.begin(), dets.end(), det);
0095 if (itr != dets.end()) {
0096 uint32_t pos = static_cast<uint32_t>(itr - dets.begin());
0097 DetId id(0);
0098 if ((det == DetId::HGCalEE) || (det == DetId::HGCalHSi)) {
0099 int type = std::atoi(items[1].c_str());
0100 int zside = std::atoi(items[2].c_str());
0101 int layer = std::atoi(items[3].c_str());
0102 int waferU = std::atoi(items[4].c_str());
0103 int waferV = std::atoi(items[5].c_str());
0104 int cellU = std::atoi(items[6].c_str());
0105 int cellV = std::atoi(items[7].c_str());
0106 id = static_cast<DetId>(HGCSiliconDetId(det, zside, type, layer, waferU, waferV, cellU, cellV));
0107 detIds_.emplace_back(id, pos);
0108 xwafer_.emplace_back(std::atof(items[8].c_str()));
0109 ywafer_.emplace_back(std::atof(items[9].c_str()));
0110 xcellOff_.emplace_back(std::atof(items[10].c_str()));
0111 ycellOff_.emplace_back(std::atof(items[11].c_str()));
0112 xcell_.emplace_back(std::atof(items[12].c_str()));
0113 ycell_.emplace_back(std::atof(items[13].c_str()));
0114 }
0115 }
0116 }
0117 }
0118 fInput.close();
0119 }
0120 }
0121 size = detIds_.size();
0122 edm::LogVerbatim("HGCGeom") << "Reads " << detIds_.size() << " ID's from " << fileName_;
0123 }
0124
0125 void HGCalTestDDDCons::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0126 std::vector<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
0127 edm::ParameterSetDescription desc;
0128 desc.add<std::vector<std::string>>("nameDetectors", names);
0129 desc.add<std::string>("fileName", "missD120.txt");
0130 descriptions.add("hgcalTestDDDCons", desc);
0131 }
0132
0133
0134 void HGCalTestDDDCons::beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup) {
0135
0136 std::vector<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
0137 std::vector<DetId::Detector> dets = {DetId::HGCalEE, DetId::HGCalHSi};
0138 std::map<DetId::Detector, uint32_t> detMap;
0139 for (uint32_t i = 0; i < nameDetectors_.size(); i++) {
0140 edm::LogVerbatim("HGCGeom") << "Tries to initialize HGCalGeometry and HGCalDDDConstants for " << i << ":"
0141 << nameDetectors_[i];
0142 const edm::ESHandle<HGCalDDDConstants> &hgcCons = iSetup.getHandle(tok_hgcal_[i]);
0143 if (hgcCons.isValid()) {
0144 hgcCons_.push_back(hgcCons.product());
0145 } else {
0146 edm::LogWarning("HGCGeom") << "Cannot initiate HGCalDDDConstants for " << nameDetectors_[i] << std::endl;
0147 }
0148 auto ii = std::find(names.begin(), names.end(), nameDetectors_[i]);
0149 if (ii != names.end()) {
0150 uint32_t k = static_cast<uint32_t>(ii - names.begin());
0151 detMap[dets[k]] = i;
0152 }
0153 }
0154 edm::LogVerbatim("HGCGeom") << "Loaded HGCalDDConstants for " << detMap.size() << " detectors";
0155
0156 for (auto itr = detMap.begin(); itr != detMap.end(); ++itr)
0157 edm::LogVerbatim("HGCGeom") << "[" << itr->second << "]: " << nameDetectors_[itr->second] << " for Detector "
0158 << itr->first;
0159
0160 int cellU(0), cellV(0), waferType(-1), waferU(0), waferV(0);
0161 double wt(1.0);
0162 for (int k = 0; k < size; ++k) {
0163 const HGCalDDDConstants *cons = hgcCons_[detMap[(detIds_[k].first).det()]];
0164 HGCSiliconDetId id(detIds_[k].first);
0165 auto hgpar_ = cons->getParameter();
0166 HGCalCell celli(hgpar_->waferSize_, hgpar_->nCellsFine_, hgpar_->nCellsCoarse_);
0167 auto placement = cons->placementIndex(id);
0168 int ncell_ = id.lowDensity() ? hgpar_->nCellsCoarse_ : hgpar_->nCellsFine_;
0169 auto partialType = cons->partialWaferType(id.layer(), id.waferU(), id.waferV());
0170 auto cellType = HGCalCell::cellType(id.cellU(), id.cellV(), ncell_, placement, partialType);
0171 auto waferxy = cons->waferPositionWithCshift(id.layer(), id.waferU(), id.waferV(), true, true, false);
0172 auto cellxy_cog = cons->locateCell(id, true, false);
0173 auto cellxy_ncog = cons->locateCell(id, false, false);
0174 waferU = id.waferU();
0175 waferV = id.waferV();
0176 double xx = id.zside() * xcell_[k];
0177 cons->waferFromPosition(
0178 xx, ycell_[k], id.zside(), id.layer(), waferU, waferV, cellU, cellV, waferType, wt, false, false);
0179 auto valid = cons->isValidHex8(id.layer(), id.waferU(), id.waferV(), id.cellU(), id.cellV(), true);
0180 float scale = 0.1;
0181 edm::LogVerbatim("HGCGeom") << "Hit[" << k << "] " << id << " Valid " << valid
0182 << " zside:layer:waferU:waferV:cellU:cellV " << id.layer() << ":" << id.waferU() << ":"
0183 << id.waferV() << ":" << id.cellU() << ":" << id.cellV()
0184 << " Observed coordinates wafer:cellCOG:cell " << waferxy.first << "," << waferxy.second
0185 << ":" << cellxy_ncog.first << "," << cellxy_ncog.second << ":" << cellxy_cog.first
0186 << "," << cellxy_cog.second << " CellType:CellPosition " << cellType.second << ":"
0187 << cellType.first;
0188 if (std::sqrt(std::pow(waferxy.first + scale * xwafer_[k], 2) + std::pow(waferxy.second - scale * ywafer_[k], 2)) >
0189 0.01) {
0190 edm::LogVerbatim("HGCGeom") << " Error wafer mismatch actual:observed (" << xwafer_[k] << "," << ywafer_[k]
0191 << "):(" << waferxy.first << "," << waferxy.second << ") ";
0192 }
0193 if (std::sqrt(std::pow(cellxy_ncog.first + scale * xcell_[k], 2) +
0194 std::pow(cellxy_ncog.second - scale * ycell_[k], 2)) > 0.01) {
0195 edm::LogVerbatim("HGCGeom") << " Error cell COG mismatch actual:observed (" << xcell_[k] << "," << ycell_[k]
0196 << "):(" << cellxy_ncog.first << "," << cellxy_ncog.second << ") ";
0197 }
0198 if (std::sqrt(std::pow(cellxy_cog.first + scale * xcellOff_[k], 2) +
0199 std::pow(cellxy_cog.second - scale * ycellOff_[k], 2)) > 0.01) {
0200 edm::LogVerbatim("HGCGeom") << " Error cell center mismatch actual:observed (" << xcellOff_[k] << ","
0201 << ycellOff_[k] << "):(" << cellxy_cog.first << "," << cellxy_cog.second << ") ";
0202 }
0203 }
0204 }
0205
0206
0207 DEFINE_FWK_MODULE(HGCalTestDDDCons);