Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:09

0001 // -*- C++ -*-
0002 //
0003 // Package:    HGCalPartialIDTester
0004 // Class:      HGCalPartialIDTester
0005 //
0006 /**\class HGCalPartialIDTester HGCalPartialIDTester.cc
0007  test/HGCalPartialIDTester.cc
0008 
0009  Description: <one line class summary>
0010 
0011  Implementation:
0012      <Notes on implementation>
0013 */
0014 //
0015 // Original Author:  Sunanda Banerjee
0016 //         Created:  Mon 2023/07/22
0017 //
0018 //
0019 
0020 // system include files
0021 #include <fstream>
0022 #include <iostream>
0023 #include <sstream>
0024 #include <string>
0025 #include <vector>
0026 
0027 // user include files
0028 #include "FWCore/Framework/interface/Frameworkfwd.h"
0029 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/EventSetup.h"
0032 #include "FWCore/Framework/interface/MakerMacros.h"
0033 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0034 #include "FWCore/ParameterSet/interface/FileInPath.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0037 #include "FWCore/Utilities/interface/transform.h"
0038 
0039 #include "DataFormats/DetId/interface/DetId.h"
0040 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0041 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0042 #include "Geometry/HGCalCommonData/interface/HGCalGeomUtils.h"
0043 
0044 class HGCalPartialIDTester : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0045 public:
0046   explicit HGCalPartialIDTester(const edm::ParameterSet &);
0047   ~HGCalPartialIDTester() override = default;
0048   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0049 
0050   void beginJob() override {}
0051   void beginRun(edm::Run const &, edm::EventSetup const &) override;
0052   void analyze(edm::Event const &iEvent, edm::EventSetup const &) override {}
0053   void endRun(edm::Run const &, edm::EventSetup const &) override {}
0054   void endJob() override {}
0055 
0056 private:
0057   const std::string nameDetector_;
0058   const std::string fileName_;
0059   const bool invert_, debug_;
0060   const edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord> tok_hgcal_;
0061   const HGCalDDDConstants *hgcCons_;
0062   std::vector<int> zside_, layer_, waferU_, waferV_;
0063   std::vector<double> xpos_, ypos_, zpos_;
0064 };
0065 
0066 HGCalPartialIDTester::HGCalPartialIDTester(const edm::ParameterSet &iC)
0067     : nameDetector_(iC.getParameter<std::string>("nameDetector")),
0068       fileName_(iC.getParameter<std::string>("fileName")),
0069       invert_(iC.getParameter<bool>("invert")),
0070       debug_(iC.getParameter<bool>("debug")),
0071       tok_hgcal_(esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(
0072           edm::ESInputTag{"", nameDetector_})) {
0073   edm::LogVerbatim("HGCGeom") << "Test DetId from position for " << nameDetector_ << " with inputs from " << fileName_
0074                               << " and invert set to " << invert_;
0075   const DetId::Detector dets = (nameDetector_ == "HGCalEESensitive") ? DetId::HGCalEE : DetId::HGCalHSi;
0076   if (!fileName_.empty()) {
0077     edm::FileInPath filetmp("Geometry/HGCalCommonData/data/" + fileName_);
0078     std::string fileName = filetmp.fullPath();
0079     std::ifstream fInput(fileName.c_str());
0080     if (!fInput.good()) {
0081       edm::LogVerbatim("HGCalGeom") << "Cannot open file " << fileName;
0082     } else {
0083       char buffer[80];
0084       while (fInput.getline(buffer, 80)) {
0085         std::vector<std::string> items = HGCalGeomUtils::splitString(std::string(buffer));
0086         if (items.size() == 5) {
0087           DetId::Detector det = static_cast<DetId::Detector>(std::atoi(items[0].c_str()));
0088           if (det == dets) {
0089             if ((det == DetId::HGCalEE) || (det == DetId::HGCalHSi)) {
0090               zside_.emplace_back(std::atoi(items[1].c_str()));
0091               layer_.emplace_back(std::atoi(items[2].c_str()));
0092               waferU_.emplace_back(0);
0093               waferV_.emplace_back(0);
0094               xpos_.emplace_back(std::atof(items[3].c_str()));
0095               ypos_.emplace_back(std::atof(items[4].c_str()));
0096               zpos_.emplace_back(0);
0097             }
0098           }
0099         } else if (items.size() == 8) {
0100           DetId::Detector det = static_cast<DetId::Detector>(std::atoi(items[0].c_str()));
0101           if (det == dets) {
0102             if ((det == DetId::HGCalEE) || (det == DetId::HGCalHSi)) {
0103               layer_.emplace_back(std::atoi(items[1].c_str()));
0104               int module = std::atoi(items[2].c_str());
0105               waferU_.emplace_back(HGCalTypes::getUnpackedU(module));
0106               waferV_.emplace_back(HGCalTypes::getUnpackedV(module));
0107               int zside = std::atoi(items[4].c_str());
0108               zside_.emplace_back(zside);
0109               xpos_.emplace_back(zside * std::atof(items[5].c_str()));
0110               ypos_.emplace_back(std::atof(items[6].c_str()));
0111               zpos_.emplace_back(std::atof(items[7].c_str()));
0112             }
0113           }
0114         }
0115       }
0116       fInput.close();
0117     }
0118   }
0119   edm::LogVerbatim("HGCalGeom") << "Reads " << layer_.size() << " posiitons for det " << dets << " from " << fileName_;
0120   for (unsigned int k = 0; k < layer_.size(); ++k) {
0121     edm::LogVerbatim("HGCalGeom") << "[" << k << "]  Layer " << layer_[k] << " Wafer " << waferU_[k] << ":"
0122                                   << waferV_[k] << " zside " << zside_[k] << " position " << xpos_[k] << ":" << ypos_[k]
0123                                   << ":" << zpos_[k];
0124   }
0125 }
0126 
0127 void HGCalPartialIDTester::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0128   edm::ParameterSetDescription desc;
0129   desc.add<std::string>("nameDetector", "HGCalEESensitive");
0130   desc.add<std::string>("fileName", "partialD98.txt");
0131   desc.add<bool>("invert", false);
0132   desc.add<bool>("debug", true);
0133   descriptions.add("hgcalPartialIDTesterEE", desc);
0134 }
0135 
0136 // ------------ method called to produce the data  ------------
0137 void HGCalPartialIDTester::beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup) {
0138   constexpr double tolR = 14.0;
0139   //initiating hgc Geometry
0140   const edm::ESHandle<HGCalDDDConstants> &hgcCons = iSetup.getHandle(tok_hgcal_);
0141   if (!hgcCons.isValid()) {
0142     edm::LogWarning("HGCGeom") << "Cannot initiate HGCalDDDConstants for " << nameDetector_;
0143   } else {
0144     hgcCons_ = hgcCons.product();
0145     const DetId::Detector dets = (nameDetector_ == "HGCalEESensitive") ? DetId::HGCalEE : DetId::HGCalHSi;
0146     for (uint32_t i = 0; i < layer_.size(); i++) {
0147       int waferU(waferU_[i]), waferV(waferV_[i]), cellU(0), cellV(0), waferType(0);
0148       double wt(0);
0149       edm::LogVerbatim("HGCalGeom") << "Input " << xpos_[i] << ":" << ypos_[i] << ":" << zside_[i] << ":" << layer_[i]
0150                                     << ":" << waferU << ":" << waferV;
0151       hgcCons_->waferFromPosition(
0152           xpos_[i], ypos_[i], zside_[i], layer_[i], waferU, waferV, cellU, cellV, waferType, wt, false, debug_);
0153       HGCalParameters::waferInfo info = hgcCons_->waferInfo(layer_[i], waferU, waferV);
0154       double dR(0);
0155       if ((waferU_[i] != 0) || (waferV_[i] != 0)) {
0156         std::pair<float, float> xy =
0157             hgcCons_->locateCell(zside_[i], layer_[i], waferU, waferV, cellU, cellV, false, true, false, false);
0158         double dx = (xpos_[i] - xy.first);
0159         double dy = (ypos_[i] - xy.second);
0160         dR = std::sqrt(dx * dx + dy * dy);
0161       }
0162       std::string ck = (dR > tolR) ? " ***** ERROR *****" : "";
0163       edm::LogVerbatim("HGCalGeom") << "Input " << dets << ":" << zside_[i] << ":" << layer_[i] << ":"
0164                                     << std::setprecision(4) << xpos_[i] << ":" << std::setprecision(4) << ypos_[i]
0165                                     << " WaferType " << waferType << " Wafer " << waferU << ":" << waferU_[i] << ":"
0166                                     << waferV << ":" << waferV_[i] << " Cell " << cellU << ":" << cellV << " wt " << wt
0167                                     << " part:orien:cass " << info.part << ":" << info.orient << ":" << info.cassette
0168                                     << " deltaR " << dR << ck;
0169       if (invert_ && (zside_[i] == -1)) {
0170         waferU = waferV = cellU = cellV = waferType = wt = dR = 0;
0171         double xx = ((waferU_[i] != 0) || (waferV_[i] != 0)) ? xpos_[i] : -xpos_[i];
0172         hgcCons_->waferFromPosition(
0173             xx, ypos_[i], zside_[i], layer_[i], waferU, waferV, cellU, cellV, waferType, wt, false, debug_);
0174         info = hgcCons_->waferInfo(layer_[i], waferU, waferV);
0175         if ((waferU_[i] != 0) || (waferV_[i] != 0)) {
0176           std::pair<float, float> xy =
0177               hgcCons_->locateCell(zside_[i], layer_[i], waferU, waferV, cellU, cellV, false, true, false, false);
0178           double dx = (xpos_[i] - xy.first);
0179           double dy = (ypos_[i] - xy.second);
0180           dR = std::sqrt(dx * dx + dy * dy);
0181         }
0182         ck = (dR > tolR) ? " ***** ERROR *****" : "";
0183         edm::LogVerbatim("HGCalGeom") << "Input " << dets << ":" << zside_[i] << ":" << layer_[i] << ":"
0184                                       << std::setprecision(4) << -xpos_[i] << ":" << std::setprecision(4) << ypos_[i]
0185                                       << " WaferType " << waferType << " Wafer " << waferU << ":" << waferV << " Cell "
0186                                       << cellU << ":" << cellV << " wt " << wt << " part:orien:cass " << info.part
0187                                       << ":" << info.orient << ":" << info.cassette << " deltaR " << dR << ck;
0188       }
0189     }
0190   }
0191 }
0192 
0193 // define this as a plug-in
0194 DEFINE_FWK_MODULE(HGCalPartialIDTester);