File indexing completed on 2025-06-03 00:12:39
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <cmath>
0022 #include <cstdlib>
0023 #include <fstream>
0024 #include <iostream>
0025 #include <memory>
0026 #include <string>
0027 #include <vector>
0028
0029
0030
0031 #include "FWCore/Framework/interface/Frameworkfwd.h"
0032 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0033
0034 #include <CLHEP/Vector/ThreeVector.h>
0035 #include "FWCore/Framework/interface/Event.h"
0036 #include "FWCore/Framework/interface/EventSetup.h"
0037 #include "FWCore/Framework/interface/MakerMacros.h"
0038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0039 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0040 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0041 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0042 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0043 #include "Geometry/HGCalCommonData/interface/HGCalParameters.h"
0044 #include "Geometry/HGCalCommonData/interface/HGCalCellUV.h"
0045 #include "Geometry/HGCalCommonData/interface/HGCalCell.h"
0046 #include "Geometry/HGCalCommonData/interface/HGCGuardRing.h"
0047 #include "Geometry/HGCalCommonData/interface/HGCalWaferMask.h"
0048 #include "Geometry/HGCalCommonData/interface/HGCalWaferType.h"
0049 #include "SimG4CMS/Calo/interface/HGCMouseBite.h"
0050 #include "G4ThreeVector.hh"
0051
0052 class HGCalMouseBiteTester : public edm::one::EDAnalyzer<> {
0053 public:
0054 explicit HGCalMouseBiteTester(const edm::ParameterSet&);
0055 ~HGCalMouseBiteTester() override = default;
0056 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0057
0058 void beginJob() override {}
0059 void analyze(edm::Event const& iEvent, edm::EventSetup const&) override;
0060 void endJob() override {}
0061
0062 private:
0063 const std::string nameSense_;
0064 const int waferU_;
0065 const int waferV_;
0066 const int nTrials_;
0067 const int layer_;
0068 const edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord> dddToken_;
0069 std::ofstream outputFile;
0070 };
0071
0072 HGCalMouseBiteTester::HGCalMouseBiteTester(const edm::ParameterSet& iC)
0073 : nameSense_(iC.getParameter<std::string>("nameSense")),
0074 waferU_(iC.getParameter<int>("waferU")),
0075 waferV_(iC.getParameter<int>("waferV")),
0076 nTrials_(iC.getParameter<int>("numbberOfTrials")),
0077 layer_(iC.getParameter<int>("layer")),
0078 dddToken_(esConsumes<HGCalDDDConstants, IdealGeometryRecord>(edm::ESInputTag{"", nameSense_})) {
0079 edm::LogVerbatim("HGCalGeom") << "Test Guard_Ring for wafer in layer" << layer_ << " U " << waferU_ << " V "
0080 << waferV_ << " with " << nTrials_ << " trials";
0081
0082 outputFile.open("full1.csv");
0083 if (!outputFile.is_open()) {
0084 edm::LogError("HGCalGeom") << "Could not open output file.";
0085 } else {
0086 outputFile << "x,y,u,v,\n";
0087 }
0088 }
0089
0090 void HGCalMouseBiteTester::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0091 edm::ParameterSetDescription desc;
0092 desc.add<std::string>("nameSense", "HGCalEESensitive");
0093 desc.add<int>("waferU", 1);
0094 desc.add<int>("waferV", 9);
0095 desc.add<int>("numbberOfTrials", 1000000);
0096 desc.add<int>("layer", 1);
0097 descriptions.add("hgcalMouseBiteTester", desc);
0098 }
0099
0100
0101 void HGCalMouseBiteTester::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0102 const HGCalDDDConstants& hgcons_ = iSetup.getData(dddToken_);
0103 double waferSize_(hgcons_.waferSize(false));
0104 int zside(1);
0105 int layertype = hgcons_.layerType(layer_);
0106 int frontBack = HGCalTypes::layerFrontBack(layertype);
0107 const std::vector<double> angle_{90.0, 30.0};
0108 int index = HGCalWaferIndex::waferIndex(layer_, waferU_, waferV_);
0109 int partialType_ = HGCalWaferType::getPartial(index, hgcons_.getParameter()->waferInfoMap_);
0110 int orient = HGCalWaferType::getOrient(index, hgcons_.getParameter()->waferInfoMap_);
0111 int placeIndex_ = HGCalCell::cellPlacementIndex(zside, frontBack, orient);
0112 int waferType_ = HGCalWaferType::getType(index, hgcons_.getParameter()->waferInfoMap_);
0113 double mouseBiteCut_ = hgcons_.mouseBite(false);
0114 bool v17OrLess = hgcons_.v17OrLess();
0115 HGCGuardRing guardRing_(hgcons_);
0116 HGCMouseBite mouseBite_(hgcons_, angle_, (waferSize_ * tan(30.0 * CLHEP::deg) - mouseBiteCut_), true);
0117 const int nFine(12), nCoarse(8);
0118 double r2 = 0.5 * waferSize_;
0119 double R2 = 2 * r2 / sqrt(3);
0120 int nCells = (waferType_ == 0) ? nFine : nCoarse;
0121 std::cout << "start" << std::endl;
0122 HGCalCellUV wafer(waferSize_, 0.0, nFine, nCoarse);
0123 HGCalCell wafer2(waferSize_, nFine, nCoarse);
0124 std::pair<double, double> xy = hgcons_.waferPosition(layer_, waferU_, waferV_, false, false);
0125 double x0 = (zside > 0) ? xy.first : -xy.first;
0126 double y0 = xy.second;
0127 std::ofstream guard_ring("Guard_ring.csv");
0128
0129 std::ofstream mouse_bite("Mouse_bite.csv");
0130 std::ofstream selected("Selected.csv");
0131 edm::LogVerbatim("HGCalGeom") << "\nHGCalMouseBiteTester:: nCells " << nCells << " FrontBack " << frontBack
0132 << " Wafer Size " << waferSize_ << " and placement index " << placeIndex_
0133 << " WaferType " << waferType_ << " Partial " << partialType_ << " WaferX " << x0
0134 << " WaferY " << y0 << "\n\n";
0135 auto start_t = std::chrono::high_resolution_clock::now();
0136 std::cout << "v17 ? " << hgcons_.v17OrLess() << std::endl;
0137 for (int i = 0; i < nTrials_; i++) {
0138 double xi = (2 * r2 * static_cast<double>(rand()) / RAND_MAX) - r2;
0139 double yi = (2 * R2 * static_cast<double>(rand()) / RAND_MAX) - R2;
0140 bool goodPoint = true;
0141 int ug = 0;
0142 int vg = 0;
0143 if (partialType_ == 11 || partialType_ == 13 || partialType_ == 15 || partialType_ == 21 || partialType_ == 23 ||
0144 partialType_ == 25 || partialType_ == 0) {
0145 ug = 0;
0146 vg = 0;
0147 } else if (partialType_ == 12 || partialType_ == 14 || partialType_ == 16 || partialType_ == 22 ||
0148 partialType_ == 24) {
0149 ug = nCells + 1;
0150 vg = 2 * (nCells - 1);
0151 }
0152 std::pair<double, double> xyg = wafer2.cellUV2XY2(ug, vg, placeIndex_, waferType_);
0153 std::vector<std::pair<double, double> > wxy =
0154 HGCalWaferMask::waferXY(0, placeIndex_, waferSize_, 0.0, 0.0, 0.0, v17OrLess);
0155 for (unsigned int i = 0; i < (wxy.size() - 1); ++i) {
0156 double xp1 = wxy[i].first;
0157 double yp1 = wxy[i].second;
0158 double xp2 = wxy[i + 1].first;
0159 double yp2 = wxy[i + 1].second;
0160 if ((((xi - xp1) / (xp2 - xp1)) - ((yi - yp1) / (yp2 - yp1))) *
0161 (((xyg.first - xp1) / (xp2 - xp1)) - ((xyg.second - yp1) / (yp2 - yp1))) <=
0162 0) {
0163 goodPoint = false;
0164 }
0165 }
0166 if (goodPoint) {
0167 G4ThreeVector point(xi, yi, 0.0);
0168 std::pair<int32_t, int32_t> uv5;
0169 if (hgcons_.v17OrLess()) {
0170 uv5 = wafer.cellUVFromXY1(xi, yi, placeIndex_, waferType_, partialType_, true, false);
0171 } else {
0172 uv5 = wafer.cellUVFromXY2(xi, yi, placeIndex_, waferType_, partialType_, true, false);
0173 }
0174 if (guardRing_.exclude(point, zside, frontBack, layer_, waferU_, waferV_)) {
0175 guard_ring << xi << "," << yi << std::endl;
0176 }
0177
0178 if (guardRing_.excludePartial(point, zside, frontBack, layer_, waferU_, waferV_)) {
0179 guard_ring << xi << "," << yi << std::endl;
0180
0181 } else if (mouseBite_.exclude(point, zside, layer_, waferU_, waferV_)) {
0182 mouse_bite << xi << "," << yi << std::endl;
0183 } else {
0184 selected << xi << "," << yi << std::endl;
0185 outputFile << xi << "," << yi << "," << uv5.first << "," << uv5.second << "," << std::endl;
0186 }
0187 }
0188 }
0189 guard_ring.close();
0190 mouse_bite.close();
0191 selected.close();
0192 outputFile.close();
0193 auto end_t = std::chrono::high_resolution_clock::now();
0194 auto diff_t = end_t - start_t;
0195 edm::LogVerbatim("HGCalGeom") << "Execution time for " << nTrials_
0196 << " events = " << std::chrono::duration<double, std::milli>(diff_t).count() << " ms";
0197 }
0198
0199
0200 DEFINE_FWK_MODULE(HGCalMouseBiteTester);