From 06a5667e6ee43f343e17dad628e955182e11301f Mon Sep 17 00:00:00 2001 From: Sunanda Date: Fri, 6 Dec 2024 10:23:18 +0100 Subject: [PATCH] Make a test code to find the area of HGCal cells through the method cellArea of HGCalDDConst --- .../HGCalCommonData/test/HGCalTestCellArea.cc | 159 ++++++++++++++++++ .../test/python/testHGCalCellArea_cfg.py | 76 +++++++++ 2 files changed, 235 insertions(+) create mode 100644 Geometry/HGCalCommonData/test/HGCalTestCellArea.cc create mode 100644 Geometry/HGCalCommonData/test/python/testHGCalCellArea_cfg.py diff --git a/Geometry/HGCalCommonData/test/HGCalTestCellArea.cc b/Geometry/HGCalCommonData/test/HGCalTestCellArea.cc new file mode 100644 index 0000000000000..3dab1ed91c45e --- /dev/null +++ b/Geometry/HGCalCommonData/test/HGCalTestCellArea.cc @@ -0,0 +1,159 @@ +// -*- C++ -*- +// +// Package: HGCalTestCellArea +// Class: HGCalTestCellArea +// +/**\class HGCalTestCellArea HGCalTestCellArea.cc + test/HGCalTestCellArea.cc + + Description: + + Implementation: + +*/ +// +// Original Author: Sunanda Banerjee +// Created: Mon 2024/11/29 +// +// + +// system include files +#include +#include +#include +#include +#include + +// user include files +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/one/EDAnalyzer.h" +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/ParameterSet/interface/FileInPath.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" +#include "FWCore/Utilities/interface/transform.h" + +#include "DataFormats/DetId/interface/DetId.h" +#include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h" +#include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h" +#include "Geometry/HGCalCommonData/interface/HGCalGeomUtils.h" +#include "Geometry/Records/interface/IdealGeometryRecord.h" + +class HGCalTestCellArea : public edm::one::EDAnalyzer { +public: + explicit HGCalTestCellArea(const edm::ParameterSet &); + ~HGCalTestCellArea() override = default; + static void fillDescriptions(edm::ConfigurationDescriptions &descriptions); + + void beginJob() override {} + void beginRun(edm::Run const &, edm::EventSetup const &) override; + void analyze(edm::Event const &iEvent, edm::EventSetup const &) override {} + void endRun(edm::Run const &, edm::EventSetup const &) override {} + void endJob() override {} + +private: + const std::vector nameDetectors_; + const std::string fileName_; + const std::vector> tok_hgcal_; + std::vector hgcCons_; + std::vector> detIds_; +}; + +HGCalTestCellArea::HGCalTestCellArea(const edm::ParameterSet &iC) + : nameDetectors_(iC.getParameter>("nameDetectors")), + fileName_(iC.getParameter("fileName")), + tok_hgcal_{edm::vector_transform(nameDetectors_, [this](const std::string &name) { + return esConsumes(edm::ESInputTag{"", name}); + })} { + std::ostringstream st1; + for (const auto &name : nameDetectors_) + st1 << " : " << name; + edm::LogVerbatim("HGCGeom") << "Test validity of cells for " << nameDetectors_.size() << " detectors" << st1.str() + << " with inputs from " << fileName_; + if (!fileName_.empty()) { + edm::FileInPath filetmp("Geometry/HGCalCommonData/data/" + fileName_); + std::string fileName = filetmp.fullPath(); + std::ifstream fInput(fileName.c_str()); + if (!fInput.good()) { + edm::LogVerbatim("HGCGeom") << "Cannot open file " << fileName; + } else { + char buffer[80]; + const std::vector dets = {DetId::HGCalEE, DetId::HGCalHSi, DetId::HGCalHSc}; + while (fInput.getline(buffer, 80)) { + std::vector items = HGCalGeomUtils::splitString(std::string(buffer)); + if (items.size() == 8) { + DetId::Detector det = static_cast(std::atoi(items[0].c_str())); + auto itr = std::find(dets.begin(), dets.end(), det); + if (itr != dets.end()) { + uint32_t pos = static_cast(itr - dets.begin()); + DetId id(0); + if ((det == DetId::HGCalEE) || (det == DetId::HGCalHSi)) { + int type = std::atoi(items[1].c_str()); + int zside = std::atoi(items[2].c_str()); + int layer = std::atoi(items[3].c_str()); + int waferU = std::atoi(items[4].c_str()); + int waferV = std::atoi(items[5].c_str()); + int cellU = std::atoi(items[6].c_str()); + int cellV = std::atoi(items[7].c_str()); + id = static_cast(HGCSiliconDetId(det, zside, type, layer, waferU, waferV, cellU, cellV)); + detIds_.emplace_back(id, pos); + } + } + } + } + fInput.close(); + } + } + edm::LogVerbatim("HGCGeom") << "Reads " << detIds_.size() << " ID's from " << fileName_; + for (unsigned int k = 0; k < detIds_.size(); ++k) { + edm::LogVerbatim("HGCGeom") << "[" << k << "] " << HGCSiliconDetId(detIds_[k].first) << " from DDConstant " << (detIds_[k].second); + } +} + +void HGCalTestCellArea::fillDescriptions(edm::ConfigurationDescriptions &descriptions) { + std::vector names = {"HGCalEESensitive", "HGCalHESiliconSensitive"}; + edm::ParameterSetDescription desc; + desc.add>("nameDetectors", names); + desc.add("fileName", "missD88.txt"); + descriptions.add("hgcalTestCellArea", desc); +} + +// ------------ method called to produce the data ------------ +void HGCalTestCellArea::beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup) { + //initiating hgc Geometry + std::vector names = {"HGCalEESensitive", "HGCalHESiliconSensitive"}; + std::vector dets = {DetId::HGCalEE, DetId::HGCalHSi}; + std::map detMap; + for (uint32_t i = 0; i < nameDetectors_.size(); i++) { + edm::LogVerbatim("HGCGeom") << "Tries to initialize HGCalGeometry and HGCalDDDConstants for " << i << ":" + << nameDetectors_[i]; + const edm::ESHandle &hgcCons = iSetup.getHandle(tok_hgcal_[i]); + if (hgcCons.isValid()) { + hgcCons_.push_back(hgcCons.product()); + } else { + edm::LogWarning("HGCGeom") << "Cannot initiate HGCalDDDConstants for " << nameDetectors_[i] << std::endl; + } + auto ii = std::find(names.begin(), names.end(), nameDetectors_[i]); + if (ii != names.end()) { + uint32_t k = static_cast(ii - names.begin()); + detMap[dets[k]] = i; + } + } + edm::LogVerbatim("HGCGeom") << "Loaded HGCalDDConstants for " << detMap.size() << " detectors"; + + for (auto itr = detMap.begin(); itr != detMap.end(); ++itr) + edm::LogVerbatim("HGCGeom") << "[" << itr->second << "]: " << nameDetectors_[itr->second] << " for Detector " + << itr->first; + + for (unsigned int k = 0; k < detIds_.size(); ++k) { + const HGCalDDDConstants *cons = hgcCons_[detMap[(detIds_[k].first).det()]]; + HGCSiliconDetId id(detIds_[k].first); + edm::LogVerbatim("HGCGeom") << "Hit[" << k << "] " << id << " Area " << cons->cellArea(id, false) << " Valid " << cons->isValidHex8(id.layer(), id.waferU(), id.waferV(), id.cellU(), id.cellV(), true); + } +} + +// define this as a plug-in +DEFINE_FWK_MODULE(HGCalTestCellArea); diff --git a/Geometry/HGCalCommonData/test/python/testHGCalCellArea_cfg.py b/Geometry/HGCalCommonData/test/python/testHGCalCellArea_cfg.py new file mode 100644 index 0000000000000..5f3a719d91150 --- /dev/null +++ b/Geometry/HGCalCommonData/test/python/testHGCalCellArea_cfg.py @@ -0,0 +1,76 @@ +############################################################################### +# Way to use this: +# cmsRun testHGCalNumbering_cfg.py type=V19 +# +# Options for type V16, V17, V17n, V18, V19 +# +############################################################################### +import FWCore.ParameterSet.Config as cms +import os, sys, importlib, re +import FWCore.ParameterSet.VarParsing as VarParsing + +#################################################################### +### SETUP OPTIONS +options = VarParsing.VarParsing('standard') +options.register('type', + "V19", + VarParsing.VarParsing.multiplicity.singleton, + VarParsing.VarParsing.varType.string, + "type of operations: V16, V17, V17n, V18, V19") + +### get and parse the command line arguments +options.parseArguments() +print(options) + +from Configuration.Eras.Era_Phase2C17I13M9_cff import Phase2C17I13M9 +process = cms.Process("HGCalCellArea",Phase2C17I13M9) + +geomFile = "Geometry.HGCalCommonData.testHGCal" + options.type + "XML_cfi" +print("Geometry file: ", geomFile) + + +process.load(geomFile) +process.load("Geometry.HGCalCommonData.hgcalParametersInitialization_cfi") +process.load("Geometry.HGCalCommonData.hgcalNumberingInitialization_cfi") +process.load("Geometry.EcalCommonData.ecalSimulationParameters_cff") +process.load("Geometry.HcalCommonData.hcalDDDSimConstants_cff") +process.load("SimGeneral.HepPDTESSource.pdt_cfi") +process.load('FWCore.MessageService.MessageLogger_cfi') + +if hasattr(process,'MessageLogger'): + process.MessageLogger.HGCalGeom=dict() + process.MessageLogger.HGCGeom=dict() + +process.load("IOMC.RandomEngine.IOMC_cff") +process.RandomNumberGeneratorService.generator.initialSeed = 456789 + +process.source = cms.Source("EmptySource") + +process.generator = cms.EDProducer("FlatRandomEGunProducer", + PGunParameters = cms.PSet( + PartID = cms.vint32(14), + MinEta = cms.double(-3.5), + MaxEta = cms.double(3.5), + MinPhi = cms.double(-3.14159265359), + MaxPhi = cms.double(3.14159265359), + MinE = cms.double(9.99), + MaxE = cms.double(10.01) + ), + AddAntiParticle = cms.bool(False), + Verbosity = cms.untracked.int32(0), + firstRun = cms.untracked.uint32(1) + ) + +process.maxEvents = cms.untracked.PSet( + input = cms.untracked.int32(1) +) + +process.SimpleMemoryCheck = cms.Service("SimpleMemoryCheck", + ignoreTotal = cms.untracked.int32(1), + moduleMemorySummary = cms.untracked.bool(True) +) + +process.load("Geometry.HGCalCommonData.hgcalTestCellArea_cfi") + + +process.p1 = cms.Path(process.generator*process.hgcalTestCellArea)