Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:24:12

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/FileInPath.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/Utilities/interface/InputTag.h"
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 
0015 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0016 #include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
0017 
0018 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
0019 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0020 #include "SimG4CMS/Calo/interface/CaloSimUtils.h"
0021 
0022 #include "Geometry/CaloTopology/interface/HGCalTopology.h"
0023 #include "Geometry/HGCalGeometry/interface/HGCalGeometry.h"
0024 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0025 #include "Geometry/HGCalCommonData/interface/HGCalParameters.h"
0026 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0027 
0028 #include <fstream>
0029 #include <sstream>
0030 #include <string>
0031 #include <vector>
0032 
0033 class HGCalTestPartialWaferHits : public edm::one::EDAnalyzer<> {
0034 public:
0035   HGCalTestPartialWaferHits(const edm::ParameterSet& ps);
0036   ~HGCalTestPartialWaferHits() override = default;
0037   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0038 
0039 protected:
0040   void analyze(edm::Event const&, edm::EventSetup const&) override;
0041   void beginJob() override {}
0042   void endJob() override {}
0043 
0044 private:
0045   const std::string g4Label_, caloHitSource_, nameSense_, missingFile_;
0046   const edm::EDGetTokenT<edm::PCaloHitContainer> tok_calo_;
0047   const edm::ESGetToken<HGCalGeometry, IdealGeometryRecord> geomToken_;
0048   std::vector<int> wafers_;
0049   std::vector<int> dumpDets_;
0050 };
0051 
0052 HGCalTestPartialWaferHits::HGCalTestPartialWaferHits(const edm::ParameterSet& ps)
0053     : g4Label_(ps.getParameter<std::string>("moduleLabel")),
0054       caloHitSource_(ps.getParameter<std::string>("caloHitSource")),
0055       nameSense_(ps.getParameter<std::string>("nameSense")),
0056       missingFile_(ps.getParameter<std::string>("missingFile")),
0057       tok_calo_(consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label_, caloHitSource_))),
0058       geomToken_(esConsumes<HGCalGeometry, IdealGeometryRecord>(edm::ESInputTag{"", nameSense_})) {
0059   edm::LogVerbatim("HGCalSim") << "Test Hit ID using SimHits for " << nameSense_ << " with module Label: " << g4Label_
0060                                << "   Hits: " << caloHitSource_ << " Missing Wafer file " << missingFile_;
0061   if (!missingFile_.empty()) {
0062     edm::FileInPath filetmp("SimG4CMS/Calo/data/" + missingFile_);
0063     std::string fileName = filetmp.fullPath();
0064     std::ifstream fInput(fileName.c_str());
0065     if (!fInput.good()) {
0066       edm::LogVerbatim("HGCalSim") << "Cannot open file " << fileName;
0067     } else {
0068       char buffer[80];
0069       while (fInput.getline(buffer, 80)) {
0070         std::vector<std::string> items = CaloSimUtils::splitString(std::string(buffer));
0071         if (items.size() > 2) {
0072           int layer = std::atoi(items[0].c_str());
0073           int waferU = std::atoi(items[1].c_str());
0074           int waferV = std::atoi(items[2].c_str());
0075           wafers_.emplace_back(HGCalWaferIndex::waferIndex(layer, waferU, waferV, false));
0076         } else if (items.size() == 1) {
0077           int dumpdet = std::atoi(items[0].c_str());
0078           dumpDets_.emplace_back(dumpdet);
0079           edm::LogVerbatim("HGCalSim") << nameSense_ << " Dump detector " << dumpdet;
0080         }
0081       }
0082       edm::LogVerbatim("HGCalSim") << "HGCalTestPartialWaferHits::Reads in " << wafers_.size() << ":"
0083                                    << dumpDets_.size() << " wafer|detector information from " << fileName;
0084       fInput.close();
0085     }
0086   }
0087 }
0088 
0089 void HGCalTestPartialWaferHits::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0090   edm::ParameterSetDescription desc;
0091   desc.add<std::string>("moduleLabel", "g4SimHits");
0092   desc.add<std::string>("caloHitSource", "HGCHitsEE");
0093   desc.add<std::string>("nameSense", "HGCalEESensitive");
0094   desc.add<std::string>("missingFile", "");
0095   descriptions.add("hgcalHitPartialEE", desc);
0096 }
0097 
0098 void HGCalTestPartialWaferHits::analyze(const edm::Event& e, const edm::EventSetup& iS) {
0099   // get HGCalGeometry
0100   const HGCalGeometry* geom = &iS.getData(geomToken_);
0101   const HGCalDDDConstants& hgc = geom->topology().dddConstants();
0102   int firstLayer = hgc.getLayerOffset();
0103   // get the hit collection
0104   const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(tok_calo_);
0105   bool getHits = (hitsCalo.isValid());
0106   uint32_t nhits = (getHits) ? hitsCalo->size() : 0;
0107   uint32_t good(0), allSi(0), all(0), allSc(0), bad(0);
0108   constexpr double tol = 2.0;
0109   edm::LogVerbatim("HGCalSim") << "HGCalTestPartialWaferHits: Input flags Hits " << getHits << " with " << nhits
0110                                << " hits: Layer Offset " << firstLayer;
0111 
0112   if (getHits) {
0113     std::vector<PCaloHit> hits;
0114     hits.insert(hits.end(), hitsCalo->begin(), hitsCalo->end());
0115     if (!hits.empty()) {
0116       // Loop over all hits
0117       for (auto hit : hits) {
0118         ++all;
0119         DetId id(hit.id());
0120         bool valid = (geom->topology()).valid(id);
0121         std::ostringstream st1;
0122         if ((id.det() == DetId::HGCalEE) || (id.det() == DetId::HGCalHSi)) {
0123           ++allSi;
0124           HGCSiliconDetId hid(id);
0125           st1 << hid;
0126           if (((id.det() == DetId::HGCalEE) && (nameSense_ == "HGCalEESensitive")) ||
0127               ((id.det() == DetId::HGCalHSi) && (nameSense_ == "HGCalHESiliconSensitive"))) {
0128             std::string_view pid =
0129                 ((hgc.cassetteShiftSilicon(hid.layer(), hid.waferU(), hid.waferV())) ? "HGCSim" : "HGCalSim");
0130             const auto& info = hgc.waferInfo(hid.layer(), hid.waferU(), hid.waferV());
0131             if (!valid)
0132               st1 << " Wafer Type:Part:Orient:Cassette " << info.type << ":" << info.part << ":" << info.orient << ":"
0133                   << info.cassette;
0134             bool toCheck(false);
0135             if (!wafers_.empty()) {
0136               int indx = HGCalWaferIndex::waferIndex(firstLayer + hid.layer(), hid.waferU(), hid.waferV(), false);
0137               if (std::find(wafers_.begin(), wafers_.end(), indx) != wafers_.end())
0138                 toCheck = true;
0139             } else if (!dumpDets_.empty()) {
0140               if ((std::find(dumpDets_.begin(), dumpDets_.end(), static_cast<int>(id.det())) != dumpDets_.end()) &&
0141                   (info.part != HGCalTypes::WaferFull))
0142                 toCheck = true;
0143             } else {
0144               // Only partial wafers
0145               toCheck = (info.part != HGCalTypes::WaferFull);
0146             }
0147             if (toCheck) {
0148               ++good;
0149               GlobalPoint pos = geom->getPosition(id);
0150               bool valid1 = geom->topology().valid(id);
0151               bool valid2 = hgc.isValidHex8(hid.layer(), hid.waferU(), hid.waferV(), hid.cellU(), hid.cellV(), false);
0152               auto xy = hgc.locateCell(hid, false);
0153               double xx = (hid.zside() > 0) ? xy.first : -xy.first;
0154               double dx = xx - pos.x();
0155               double dy = xy.second - pos.y();
0156               double diff = std::sqrt(dx * dx + dy * dy);
0157               if ((diff > tol) || (!valid1) || (!valid2))
0158                 pid = "HGCalError";
0159               edm::LogVerbatim(pid) << "Hit[" << all << ":" << allSi << ":" << good << "]" << hid
0160                                     << " Wafer Type:Part:Orient:Cassette " << info.type << ":" << info.part << ":"
0161                                     << info.orient << ":" << info.cassette << " at (" << pos.x() << ":" << xx << ":"
0162                                     << dx << ", " << pos.y() << ":" << xy.second << ":" << dy << ", " << pos.z()
0163                                     << ") Valid " << valid1 << ":" << valid2 << " Distance " << diff;
0164             }
0165           }
0166         } else if (id.det() == DetId::HGCalHSc) {
0167           ++allSc;
0168           HGCScintillatorDetId hid(id);
0169           st1 << hid;
0170           if ((id.det() == DetId::HGCalHSc) && (nameSense_ == "HGCalHEScintillatorSensitive")) {
0171             std::string_view pid = ((hgc.cassetteShiftScintillator(hid.layer(), hid.iphi())) ? "HGCSim" : "HGCalSim");
0172             GlobalPoint pos = geom->getPosition(id);
0173             bool valid1 = geom->topology().valid(id);
0174             bool valid2 = hgc.isValidTrap(hid.zside(), hid.layer(), hid.ring(), hid.iphi());
0175             if ((!valid1) || (!valid2))
0176               pid = "HGCalError";
0177             int cassette = hgc.cassetteTile(hid.iphi());
0178             edm::LogVerbatim(pid) << "Hit[" << all << ":" << allSc << "] " << hid << " Cassette " << cassette << " at ("
0179                                   << pos.x() << ", " << pos.y() << ", " << pos.z() << ") Valid " << valid1 << ":"
0180                                   << valid2;
0181           }
0182         } else {
0183           st1 << std::hex << id.rawId() << std::dec;
0184         }
0185         if (!valid) {
0186           edm::LogVerbatim("HGCalError") << "Invalid ID " << st1.str();
0187           ++bad;
0188         }
0189       }
0190     }
0191   }
0192   edm::LogVerbatim("HGCalSim") << "Total hits = " << all << ":" << nhits << " Good Silicon DetIds = " << allSi << ":"
0193                                << good << " Scintitllator = " << allSc << " Invalid = " << bad;
0194 }
0195 
0196 //define this as a plug-in
0197 DEFINE_FWK_MODULE(HGCalTestPartialWaferHits);