Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-19 23:19:51

0001 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
0002 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0003 #include "DataFormats/DetId/interface/DetId.h"
0004 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/EventSetup.h"
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0013 #include "Geometry/CommonTopologies/interface/StripTopology.h"
0014 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
0015 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0016 
0017 #include <iostream>
0018 #include <exception>
0019 
0020 class SiStripNoisesGetAllChecker : public edm::one::EDAnalyzer<> {
0021 public:
0022   explicit SiStripNoisesGetAllChecker(const edm::ParameterSet&);
0023   ~SiStripNoisesGetAllChecker() override = default;
0024 
0025   void analyze(const edm::Event&, const edm::EventSetup&) override;
0026 
0027 private:
0028   const edm::ESGetToken<SiStripNoises, SiStripNoisesRcd> noisesToken_;
0029   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tkGeomToken_;
0030   void checkModuleNoise(const SiStripNoises&, const uint32_t detID, uint16_t maxNStrips);
0031 };
0032 
0033 SiStripNoisesGetAllChecker::SiStripNoisesGetAllChecker(const edm::ParameterSet&)
0034     : noisesToken_(esConsumes()), tkGeomToken_(esConsumes()) {}
0035 
0036 void SiStripNoisesGetAllChecker::analyze(const edm::Event&, const edm::EventSetup& iSetup) {
0037   const auto& siStripNoises = iSetup.getData(noisesToken_);
0038 
0039   const auto& tkGeom = &iSetup.getData(tkGeomToken_);
0040   const auto& tkDets = tkGeom->dets();
0041 
0042   edm::LogInfo("SiStripNoisesGetAllChecker") << "Starting to loop over all SiStrip modules...";
0043 
0044   // Get all DetIDs associated with SiStripNoises
0045   std::vector<uint32_t> detIDs;
0046   siStripNoises.getDetIds(detIDs);
0047 
0048   size_t exceptionCounts{0};
0049   for (const auto& detID : detIDs) {
0050     uint16_t maxNStrips{0};
0051     auto det = std::find_if(tkDets.begin(), tkDets.end(), [detID](auto& elem) -> bool {
0052       return (elem->geographicalId().rawId() == detID);
0053     });
0054     const StripTopology& p = dynamic_cast<const StripGeomDetUnit*>(*det)->specificTopology();
0055     maxNStrips = p.nstrips();
0056 
0057     try {
0058       checkModuleNoise(siStripNoises, detID, maxNStrips);
0059     } catch (const std::exception& e) {
0060       // Increment the exception counter if checkModuleNoise itself throws an exception
0061       edm::LogError("SiStripNoisesGetAllChecker")
0062           << "Exception in checkModuleNoise for detID " << detID << ": " << e.what();
0063       ++exceptionCounts;
0064     } catch (...) {
0065       edm::LogError("SiStripNoisesGetAllChecker") << "Unknown exception in checkModuleNoise for detID " << detID;
0066       ++exceptionCounts;
0067     }
0068   }
0069 
0070   std::ostringstream message;
0071 
0072   // Define the box width
0073   const int boxWidth = 50;
0074 
0075   message << "\n"
0076           << std::string(boxWidth, '*') << "\n"
0077           << "* " << std::setw(boxWidth - 4) << std::left << "SiStripNoisesGetAllChecker Summary"
0078           << " *\n"
0079           << std::string(boxWidth, '*') << "\n"
0080           << "* " << std::setw(boxWidth - 4) << std::left
0081           << ("Completed loop over " + std::to_string(detIDs.size()) + " SiStrip modules.") << " *\n"
0082           << "* " << std::setw(boxWidth - 4) << std::left
0083           << ("Encountered " + std::to_string(exceptionCounts) + " exceptions.") << " *\n"
0084           << std::string(boxWidth, '*');
0085 
0086   edm::LogSystem("SiStripNoisesGetAllChecker") << message.str();
0087 }
0088 
0089 void SiStripNoisesGetAllChecker::checkModuleNoise(const SiStripNoises& siStripNoises,
0090                                                   const uint32_t detID,
0091                                                   uint16_t maxNStrips) {
0092   try {
0093     SiStripNoises::Range detNoiseRange = siStripNoises.getRange(detID);
0094     std::vector<float> noises;
0095     noises.resize(maxNStrips);
0096     siStripNoises.allNoises(noises, detNoiseRange);
0097     edm::LogInfo("SiStripNoisesGetAllChecker") << "Successfully processed detID: " << detID;
0098   } catch (const std::exception& e) {
0099     edm::LogError("SiStripNoisesGetAllChecker") << "Exception caught for detID " << detID << ": " << e.what();
0100     throw;  // Re-throw the exception to allow the outer loop to handle it
0101   } catch (...) {
0102     edm::LogError("SiStripNoisesGetAllChecker") << "Unknown exception caught for detID " << detID;
0103     throw;  // Re-throw the unknown exception
0104   }
0105 }
0106 
0107 // Define this as a plug-in
0108 DEFINE_FWK_MODULE(SiStripNoisesGetAllChecker);