Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    HGCalWaferIDTester
0004 // Class:      HGCalWaferIDTester
0005 //
0006 /**\class HGCalWaferIDTester HGCalWaferIDTester.cc
0007  test/HGCalWaferIDTester.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 2022/12/01
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 "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0038 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0039 #include "Geometry/HGCalCommonData/interface/HGCalGeomUtils.h"
0040 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0041 
0042 class HGCalWaferIDTester : public edm::one::EDAnalyzer<> {
0043 public:
0044   explicit HGCalWaferIDTester(const edm::ParameterSet&);
0045   ~HGCalWaferIDTester() override = default;
0046   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0047 
0048   void beginJob() override {}
0049   void analyze(edm::Event const& iEvent, edm::EventSetup const&) override;
0050   void endJob() override {}
0051 
0052 private:
0053   const std::string nameSense_, errorFile_;
0054   const int mode_, shift_;
0055   const edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord> dddToken_;
0056   std::vector<HGCSiliconDetId> detIds_;
0057   std::vector<std::pair<double, double>> posXY_;
0058 };
0059 
0060 HGCalWaferIDTester::HGCalWaferIDTester(const edm::ParameterSet& iC)
0061     : nameSense_(iC.getParameter<std::string>("nameSense")),
0062       errorFile_(iC.getParameter<std::string>("fileName")),
0063       mode_(iC.getParameter<int>("mode")),
0064       shift_(iC.getParameter<int>("shift")),
0065       dddToken_(esConsumes<HGCalDDDConstants, IdealGeometryRecord>(edm::ESInputTag{"", nameSense_})) {
0066   edm::LogVerbatim("HGCalGeomW") << "Test HGCSilicon DetID for " << nameSense_ << " of positions from the file "
0067                                  << errorFile_ << " for the mode " << mode_;
0068 
0069   edm::FileInPath filetmp("Geometry/HGCalCommonData/data/" + errorFile_);
0070   std::string fileName = filetmp.fullPath();
0071   std::ifstream fInput(fileName.c_str());
0072   if (!fInput.good()) {
0073     edm::LogWarning("HGCalGeom") << "Cannot open file " << fileName;
0074   } else {
0075     char buffer[80];
0076     int kount(0);
0077     while (fInput.getline(buffer, 80)) {
0078       std::vector<std::string> items = HGCalGeomUtils::splitString(std::string(buffer));
0079       ++kount;
0080       if (shift_ != 1) {
0081         DetId::Detector det = (nameSense_ == "HGCalEESensitive") ? DetId::HGCalEE : DetId::HGCalHSi;
0082         if (items.size() > 8) {
0083           int type = std::atoi(items[0].c_str());
0084           int zp = std::atoi(items[1].c_str());
0085           int layer = std::atoi(items[2].c_str());
0086           int waferU = std::atoi(items[3].c_str());
0087           int waferV = std::atoi(items[4].c_str());
0088           int cellU = std::atoi(items[5].c_str());
0089           int cellV = std::atoi(items[6].c_str());
0090           double xx = std::atof(items[7].c_str()) * CLHEP::cm;
0091           double yy = std::atof(items[8].c_str()) * CLHEP::cm;
0092           HGCSiliconDetId id(det, zp, type, layer, waferU, waferV, cellU, cellV);
0093           detIds_.emplace_back(id);
0094           posXY_.emplace_back(std::make_pair(xx, yy));
0095         }
0096       } else {
0097         if (items.size() > 9) {
0098           DetId::Detector det = static_cast<DetId::Detector>(std::atoi(items[0].c_str()));
0099           int type = std::atoi(items[1].c_str());
0100           int zp = 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           double xx = std::atof(items[8].c_str()) * CLHEP::cm;
0107           double yy = std::atof(items[9].c_str()) * CLHEP::cm;
0108           HGCSiliconDetId id(det, zp, type, layer, waferU, waferV, cellU, cellV);
0109           detIds_.emplace_back(id);
0110           posXY_.emplace_back(std::make_pair(xx, yy));
0111         }
0112       }
0113     }
0114     fInput.close();
0115     edm::LogVerbatim("HGCalGeomW") << "Reads a total of " << detIds_.size() << ":" << posXY_.size()
0116                                    << " entries out of " << kount << "\n";
0117     for (unsigned int k = 0; k < detIds_.size(); ++k)
0118       edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << detIds_[k] << " (" << posXY_[k].first << ", "
0119                                     << posXY_[k].second << ")";
0120   }
0121 }
0122 
0123 void HGCalWaferIDTester::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0124   edm::ParameterSetDescription desc;
0125   desc.add<std::string>("nameSense", "HGCalHESiliconSensitive");
0126   desc.add<std::string>("fileName", "cellIDHEF.txt");
0127   desc.add<int>("mode", 1);
0128   desc.add<int>("shift", 0);
0129   descriptions.add("hgcalWaferIDTesterHEF", desc);
0130 }
0131 
0132 // ------------ method called to produce the data  ------------
0133 void HGCalWaferIDTester::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0134   const HGCalDDDConstants& hgdc = iSetup.getData(dddToken_);
0135   edm::LogVerbatim("HGCalGeomW") << "\nStart testing " << nameSense_ << " for mode " << mode_ << std::endl;
0136   const DetId::Detector det = (nameSense_ == "HGCalEESensitive") ? DetId::HGCalEE : DetId::HGCalHSi;
0137   bool debug = (mode_ > 0) ? true : false;
0138 
0139   for (unsigned int k = 0; k < detIds_.size(); ++k) {
0140     if (detIds_[k].det() == det) {
0141       std::ostringstream st1;
0142       st1 << "Hit[" << k << "] " << detIds_[k];
0143       int cellU(0), cellV(0), waferType(-1), waferU(0), waferV(0);
0144       double wt(0);
0145       int layer = detIds_[k].layer();
0146       int zside = detIds_[k].zside();
0147       int indx = HGCalWaferIndex::waferIndex(layer, detIds_[k].waferU(), detIds_[k].waferV());
0148       st1 << " Part:Orient:Cassette:Shift " << std::get<1>(hgdc.waferFileInfo(indx)) << ":"
0149           << std::get<2>(hgdc.waferFileInfo(indx)) << ":" << std::get<3>(hgdc.waferFileInfo(indx)) << ":"
0150           << hgdc.cassetteShiftSilicon(zside, layer, detIds_[k].waferU(), detIds_[k].waferV());
0151       double xx = (zside < 0) ? -posXY_[k].first : posXY_[k].first;
0152       double yy = posXY_[k].second;
0153       hgdc.waferFromPosition(xx, yy, zside, layer, waferU, waferV, cellU, cellV, waferType, wt, false, debug);
0154       HGCSiliconDetId id(detIds_[k].det(), detIds_[k].zside(), waferType, layer, waferU, waferV, cellU, cellV);
0155       if (id.rawId() != detIds_[k].rawId())
0156         st1 << " non-matching DetId: new ID " << id;
0157       else
0158         st1 << " new ID " << id;
0159       indx = HGCalWaferIndex::waferIndex(layer, id.waferU(), id.waferV());
0160       st1 << " Part:Orient:Cassette:Shift " << std::get<1>(hgdc.waferFileInfo(indx)) << ":"
0161           << std::get<2>(hgdc.waferFileInfo(indx)) << ":" << std::get<3>(hgdc.waferFileInfo(indx)) << ":"
0162           << hgdc.cassetteShiftSilicon(zside, layer, id.waferU(), id.waferV());
0163       auto xy = hgdc.locateCell(id, true);
0164       double xx0 = (id.zside() > 0) ? xy.first : -xy.first;
0165       double yy0 = xy.second;
0166       double dx = xx0 - (posXY_[k].first / CLHEP::cm);
0167       double dy = yy0 - (yy / CLHEP::cm);
0168       double diff = std::sqrt(dx * dx + dy * dy);
0169       st1 << " input position: (" << posXY_[k].first / CLHEP::cm << ", " << yy / CLHEP::cm << "); position from ID ("
0170           << xx0 << ", " << yy0 << ") distance " << diff;
0171       constexpr double tol = 1.5;
0172       if (diff > tol)
0173         st1 << " ***** CheckID *****";
0174       bool valid1 = hgdc.isValidHex8(
0175           detIds_[k].layer(), detIds_[k].waferU(), detIds_[k].waferV(), detIds_[k].cellU(), detIds_[k].cellV(), true);
0176       bool valid2 = hgdc.isValidHex8(id.layer(), id.waferU(), id.waferV(), id.cellU(), id.cellV(), true);
0177       st1 << " Validity flag: " << valid1 << ":" << valid2;
0178       if ((!valid1) || (!valid2))
0179         st1 << " +++++ Validity Check +++++ ";
0180       edm::LogVerbatim("HGCalGeomW") << st1.str();
0181     }
0182   }
0183 }
0184 
0185 // define this as a plug-in
0186 DEFINE_FWK_MODULE(HGCalWaferIDTester);