Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-24 01:11:21

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondFormats/SiPixelObjects
0004 // Class:      SiPixelQualityProbabilitiesTestWriter
0005 //
0006 /**\class SiPixelQualityProbabilitiesTestWriter SiPixelQualityProbabilitiesTestWriter.cc CondFormats/SiPixelObjects/plugins/SiPixelQualityProbabilitiesTestWriter.cc
0007  Description: class to build the SiPixel Quality probabilities 
0008 */
0009 //
0010 // Original Author:  Marco Musich
0011 //         Created:  Wed, 30 Nov 2018 13:22:00 GMT
0012 //
0013 //
0014 
0015 // system include files
0016 #include <memory>
0017 #include <fstream>
0018 
0019 // user include files
0020 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0021 #include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
0022 #include "CondFormats/SiPixelObjects/interface/SiPixelQualityProbabilities.h"
0023 #include "CondFormats/DataRecord/interface/SiPixelQualityFromDbRcd.h"
0024 #include "FWCore/Framework/interface/ESHandle.h"
0025 #include "FWCore/Framework/interface/ESWatcher.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/ServiceRegistry/interface/Service.h"
0032 
0033 //
0034 // class declaration
0035 //
0036 
0037 class SiPixelQualityProbabilitiesTestWriter : public edm::one::EDAnalyzer<> {
0038 public:
0039   explicit SiPixelQualityProbabilitiesTestWriter(const edm::ParameterSet&);
0040   ~SiPixelQualityProbabilitiesTestWriter() override;
0041 
0042   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0043 
0044 private:
0045   void analyze(const edm::Event&, const edm::EventSetup&) override;
0046   void endJob() override;
0047 
0048   // ----------member data ---------------------------
0049   const std::string m_ProbInputs;
0050   const std::string m_SnapshotInputs;
0051   const std::string m_record;
0052   const bool printdebug_;
0053   std::unique_ptr<SiPixelQualityProbabilities> myProbabilities;
0054 };
0055 
0056 //
0057 // constructors and destructor
0058 //
0059 SiPixelQualityProbabilitiesTestWriter::SiPixelQualityProbabilitiesTestWriter(const edm::ParameterSet& iConfig)
0060     : m_ProbInputs(iConfig.getParameter<std::string>("probabilities")),
0061       m_SnapshotInputs(iConfig.getParameter<std::string>("snapshots")),
0062       m_record(iConfig.getParameter<std::string>("record")),
0063       printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", false)) {
0064   //now do what ever initialization is needed
0065   myProbabilities = std::make_unique<SiPixelQualityProbabilities>();
0066 }
0067 
0068 SiPixelQualityProbabilitiesTestWriter::~SiPixelQualityProbabilitiesTestWriter() = default;
0069 
0070 //
0071 // member functions
0072 //
0073 
0074 // ------------ method called for each event  ------------
0075 void SiPixelQualityProbabilitiesTestWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0076   using namespace edm;
0077   std::ifstream myfile(m_ProbInputs);
0078   std::ifstream mysnapshots(m_SnapshotInputs);
0079   std::string line1, line2;
0080   std::map<int, std::string> snapshotIdToString;
0081 
0082   if (mysnapshots.is_open()) {
0083     while (getline(mysnapshots, line1)) {
0084       //edm::LogInfo("SiPixelQualityProbabilitiesTestWriter") << line1 << std::endl;
0085       std::istringstream iss(line1);
0086       int id, run, ls;
0087       iss >> id >> run >> ls;
0088       snapshotIdToString[id] = std::to_string(run) + "_" + std::to_string(ls);
0089     }
0090   }
0091 
0092   SiPixelQualityProbabilities::probabilityVec myProbVector;
0093 
0094   if (myfile.is_open()) {
0095     while (getline(myfile, line2)) {
0096       edm::LogInfo("SiPixelQualityProbabilitiesTestWriter") << line2 << std::endl;
0097       std::istringstream iss(line2);
0098       int pileupBinId, nEntries;
0099       iss >> pileupBinId >> nEntries;
0100       edm::LogInfo("SiPixelQualityProbabilitiesTestWriter")
0101           << "PILEUP BIN/ENTRIES:  " << pileupBinId << " " << nEntries << std::endl;
0102       std::vector<int> ids(nEntries, 0);
0103       std::vector<float> probs(nEntries, 0.0);
0104       for (int i = 0; i < nEntries; ++i) {
0105         iss >> ids.at(i) >> probs.at(i);
0106         //edm::LogInfo("SiPixelQualityProbabilitiesTestWriter") << ids.at(i) << " " << probs.at(i)<< std::endl;
0107         auto idAndProb = std::make_pair(snapshotIdToString.at(ids.at(i)), probs.at(i));
0108         myProbVector.push_back(idAndProb);
0109       }
0110       if (nEntries > 0)
0111         myProbabilities->setProbabilities(pileupBinId, myProbVector);
0112       myProbVector.clear();
0113     }
0114     myfile.close();
0115   }
0116 
0117   if (printdebug_) {
0118     edm::LogInfo("SiPixelQualityProbabilitiesTestWriter") << "Content of SiPixelQualityProbabilities " << std::endl;
0119     // use buil-in method in the CondFormat
0120     myProbabilities->printAll();
0121   }
0122 }
0123 
0124 // ------------ method called once each job just after ending the event loop  ------------
0125 void SiPixelQualityProbabilitiesTestWriter::endJob() {
0126   edm::LogInfo("SiPixelQualityProbabilitiesTestWriter")
0127       << "Size of SiPixelQualityProbabilities object " << myProbabilities->size() << std::endl
0128       << std::endl;
0129 
0130   // Form the data here
0131   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0132   if (poolDbService.isAvailable()) {
0133     cond::Time_t valid_time = poolDbService->currentTime();
0134     // this writes the payload to begin in current run defined in cfg
0135     poolDbService->writeOneIOV(*myProbabilities, valid_time, m_record);
0136   }
0137 }
0138 
0139 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0140 void SiPixelQualityProbabilitiesTestWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0141   edm::ParameterSetDescription desc;
0142   desc.setComment("Writes payloads of type SiPixelQualityProbabilities");
0143   desc.addUntracked<bool>("printDebug", true);
0144   desc.add<std::string>("record", "SiPixelStatusScenarioProbabilityRcd");
0145   desc.add<std::string>("snapshots", "");
0146   desc.add<std::string>("probabilities", "");
0147   descriptions.add("SiPixelQualityProbabilitiesTestWriter", desc);
0148 }
0149 
0150 //define this as a plug-in
0151 DEFINE_FWK_MODULE(SiPixelQualityProbabilitiesTestWriter);