Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phase2-hgx360T Make a test code to find the area of HGCal cells through the method "cellArea" of HGCalDDConst #46886

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions Geometry/HGCalCommonData/test/HGCalTestCellArea.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// -*- C++ -*-
//
// Package: HGCalTestCellArea
// Class: HGCalTestCellArea
//
/**\class HGCalTestCellArea HGCalTestCellArea.cc
test/HGCalTestCellArea.cc

Description: <one line class summary>

Implementation:
<Notes on implementation>
*/
//
// Original Author: Sunanda Banerjee
// Created: Mon 2024/11/29
//
//

// system include files
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

// 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<edm::one::WatchRuns> {
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<std::string> nameDetectors_;
const std::string fileName_;
const std::vector<edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord>> tok_hgcal_;
std::vector<const HGCalDDDConstants *> hgcCons_;
std::vector<std::pair<DetId, uint32_t>> detIds_;
};

HGCalTestCellArea::HGCalTestCellArea(const edm::ParameterSet &iC)
: nameDetectors_(iC.getParameter<std::vector<std::string>>("nameDetectors")),
fileName_(iC.getParameter<std::string>("fileName")),
tok_hgcal_{edm::vector_transform(nameDetectors_, [this](const std::string &name) {
return esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(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<DetId::Detector> dets = {DetId::HGCalEE, DetId::HGCalHSi, DetId::HGCalHSc};
while (fInput.getline(buffer, 80)) {
std::vector<std::string> items = HGCalGeomUtils::splitString(std::string(buffer));
if (items.size() == 8) {
DetId::Detector det = static_cast<DetId::Detector>(std::atoi(items[0].c_str()));
auto itr = std::find(dets.begin(), dets.end(), det);
if (itr != dets.end()) {
uint32_t pos = static_cast<uint32_t>(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<DetId>(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<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
edm::ParameterSetDescription desc;
desc.add<std::vector<std::string>>("nameDetectors", names);
desc.add<std::string>("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<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
std::vector<DetId::Detector> dets = {DetId::HGCalEE, DetId::HGCalHSi};
std::map<DetId::Detector, uint32_t> 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<HGCalDDDConstants> &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<uint32_t>(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);
76 changes: 76 additions & 0 deletions Geometry/HGCalCommonData/test/python/testHGCalCellArea_cfg.py
Original file line number Diff line number Diff line change
@@ -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)