Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:46:26

0001 // -*- C++ -*-
0002 //
0003 // Package:    CastorDbProducer
0004 // Class:      CastorDbProducer
0005 //
0006 /**\class CastorDbProducer CastorDbProducer.h CalibFormats/CastorDbProducer/interface/CastorDbProducer.h
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Fedor Ratnikov
0015 //         Created:  Tue Aug  9 19:10:10 CDT 2005
0016 //         Adapted for CASTOR by L. Mundim
0017 //
0018 //
0019 
0020 // system include files
0021 #include <iostream>
0022 #include <fstream>
0023 #include <memory>
0024 
0025 #include "FWCore/Framework/interface/ModuleFactory.h"
0026 #include "FWCore/Framework/interface/ESProducer.h"
0027 #include "FWCore/Framework/interface/ESProductHost.h"
0028 #include "FWCore/Utilities/interface/ReusableObjectHolder.h"
0029 
0030 #include "CalibCalorimetry/CastorCalib/interface/CastorDbASCIIIO.h"
0031 #include "CalibFormats/CastorObjects/interface/CastorDbService.h"
0032 #include "CalibFormats/CastorObjects/interface/CastorDbRecord.h"
0033 
0034 #include "CondFormats/DataRecord/interface/CastorChannelQualityRcd.h"
0035 #include "CondFormats/DataRecord/interface/CastorElectronicsMapRcd.h"
0036 #include "CondFormats/DataRecord/interface/CastorGainWidthsRcd.h"
0037 #include "CondFormats/DataRecord/interface/CastorGainsRcd.h"
0038 #include "CondFormats/DataRecord/interface/CastorPedestalWidthsRcd.h"
0039 #include "CondFormats/DataRecord/interface/CastorPedestalsRcd.h"
0040 #include "CondFormats/DataRecord/interface/CastorQIEDataRcd.h"
0041 #include "CondFormats/CastorObjects/interface/AllObjects.h"
0042 
0043 class CastorDbProducer : public edm::ESProducer {
0044 public:
0045   CastorDbProducer(const edm::ParameterSet&);
0046   ~CastorDbProducer() override;
0047 
0048   std::shared_ptr<CastorDbService> produce(const CastorDbRecord&);
0049 
0050 private:
0051   // ----------member data ---------------------------
0052   using HostType = edm::ESProductHost<CastorDbService,
0053                                       CastorPedestalsRcd,
0054                                       CastorPedestalWidthsRcd,
0055                                       CastorGainsRcd,
0056                                       CastorGainWidthsRcd,
0057                                       CastorQIEDataRcd,
0058                                       CastorChannelQualityRcd,
0059                                       CastorElectronicsMapRcd>;
0060 
0061   template <typename RecordT, typename TokenT>
0062   void setupItem(const RecordT& fRecord, const TokenT& token, const char* name, CastorDbService* service);
0063 
0064   edm::ReusableObjectHolder<HostType> holder_;
0065 
0066   edm::ESGetToken<CastorPedestals, CastorPedestalsRcd> pedestalsToken_;
0067   edm::ESGetToken<CastorPedestalWidths, CastorPedestalWidthsRcd> pedestalWidthsToken_;
0068   edm::ESGetToken<CastorGains, CastorGainsRcd> gainsToken_;
0069   edm::ESGetToken<CastorGainWidths, CastorGainWidthsRcd> gainWidthsToken_;
0070   edm::ESGetToken<CastorQIEData, CastorQIEDataRcd> qieDataToken_;
0071   edm::ESGetToken<CastorChannelQuality, CastorChannelQualityRcd> channelQualityToken_;
0072   edm::ESGetToken<CastorElectronicsMap, CastorElectronicsMapRcd> electronicsMapToken_;
0073 
0074   std::vector<std::string> mDumpRequest;
0075   std::ostream* mDumpStream;
0076 };
0077 
0078 CastorDbProducer::CastorDbProducer(const edm::ParameterSet& fConfig)
0079     : ESProducer(), mDumpRequest(), mDumpStream(nullptr) {
0080   //the following line is needed to tell the framework what
0081   // data is being produced
0082   auto cc = setWhatProduced(this);
0083   pedestalsToken_ = cc.consumes();
0084   pedestalWidthsToken_ = cc.consumes();
0085   gainsToken_ = cc.consumes();
0086   gainWidthsToken_ = cc.consumes();
0087   qieDataToken_ = cc.consumes();
0088   channelQualityToken_ = cc.consumes();
0089   electronicsMapToken_ = cc.consumes();
0090 
0091   //now do what ever other initialization is needed
0092 
0093   mDumpRequest = fConfig.getUntrackedParameter<std::vector<std::string> >("dump", std::vector<std::string>());
0094   if (!mDumpRequest.empty()) {
0095     std::string otputFile = fConfig.getUntrackedParameter<std::string>("file", "");
0096     mDumpStream = otputFile.empty() ? &std::cout : new std::ofstream(otputFile.c_str());
0097   }
0098 }
0099 
0100 CastorDbProducer::~CastorDbProducer() {
0101   // do anything here that needs to be done at desctruction time
0102   // (e.g. close files, deallocate resources etc.)
0103   if (mDumpStream != &std::cout)
0104     delete mDumpStream;
0105 }
0106 
0107 //
0108 // member functions
0109 //
0110 
0111 // ------------ method called to produce the data  ------------
0112 std::shared_ptr<CastorDbService> CastorDbProducer::produce(const CastorDbRecord& record) {
0113   auto host = holder_.makeOrGet([]() { return new HostType; });
0114 
0115   bool needBuildCalibrations = false;
0116   bool needBuildCalibWidths = false;
0117 
0118   host->ifRecordChanges<CastorElectronicsMapRcd>(
0119       record, [this, h = host.get()](auto const& rec) { setupItem(rec, electronicsMapToken_, "ElectronicsMap", h); });
0120   host->ifRecordChanges<CastorChannelQualityRcd>(
0121       record, [this, h = host.get()](auto const& rec) { setupItem(rec, channelQualityToken_, "ChannelQuality", h); });
0122   host->ifRecordChanges<CastorGainWidthsRcd>(record, [this, h = host.get(), &needBuildCalibWidths](auto const& rec) {
0123     setupItem(rec, gainWidthsToken_, "GainWidths", h);
0124     needBuildCalibWidths = true;
0125   });
0126   host->ifRecordChanges<CastorQIEDataRcd>(
0127       record, [this, h = host.get(), &needBuildCalibrations, &needBuildCalibWidths](auto const& rec) {
0128         setupItem(rec, qieDataToken_, "QIEData", h);
0129         needBuildCalibrations = true;
0130         needBuildCalibWidths = true;
0131       });
0132   host->ifRecordChanges<CastorPedestalWidthsRcd>(record,
0133                                                  [this, h = host.get(), &needBuildCalibWidths](auto const& rec) {
0134                                                    setupItem(rec, pedestalWidthsToken_, "PedestalWidths", h);
0135                                                    needBuildCalibWidths = true;
0136                                                  });
0137   host->ifRecordChanges<CastorGainsRcd>(record, [this, h = host.get(), &needBuildCalibrations](auto const& rec) {
0138     setupItem(rec, gainsToken_, "Gains", h);
0139     needBuildCalibrations = true;
0140   });
0141   host->ifRecordChanges<CastorPedestalsRcd>(record, [this, h = host.get(), &needBuildCalibrations](auto const& rec) {
0142     setupItem(rec, pedestalsToken_, "Pedestals", h);
0143     needBuildCalibrations = true;
0144   });
0145 
0146   if (needBuildCalibWidths) {
0147     host->buildCalibWidths();
0148   }
0149 
0150   if (needBuildCalibrations) {
0151     host->buildCalibrations();
0152   }
0153 
0154   return host;  // automatically converts to std::shared_ptr<CastorDbService>
0155 }
0156 
0157 template <typename RecordT, typename TokenT>
0158 void CastorDbProducer::setupItem(const RecordT& fRecord,
0159                                  const TokenT& token,
0160                                  const char* name,
0161                                  CastorDbService* service) {
0162   const auto& item = fRecord.get(token);
0163   service->setData(&item);
0164   if (std::find(mDumpRequest.begin(), mDumpRequest.end(), name) != mDumpRequest.end()) {
0165     *mDumpStream << "New HCAL/CASTOR " << name << " set" << std::endl;
0166     CastorDbASCIIIO::dumpObject(*mDumpStream, item);
0167   }
0168 }
0169 
0170 DEFINE_FWK_EVENTSETUP_MODULE(CastorDbProducer);