Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-05-27 23:05:40

0001 /** \class PCLMetadataWriter
0002  *  No description available.
0003  *
0004  *  \author G. Cerminara - CERN
0005  */
0006 
0007 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0008 #include "CondFormats/Common/interface/DropBoxMetadata.h"
0009 #include "CondFormats/DataRecord/interface/DropBoxMetadataRcd.h"
0010 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/MessageLogger/interface/JobReport.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/ServiceRegistry/interface/Service.h"
0016 
0017 #include <string>
0018 #include <vector>
0019 #include <iostream>
0020 
0021 class PCLMetadataWriter : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0022 public:
0023   /// Constructor
0024   PCLMetadataWriter(const edm::ParameterSet &);
0025 
0026   /// Destructor
0027   ~PCLMetadataWriter() override = default;
0028 
0029   // Operations
0030   void analyze(const edm::Event &, const edm::EventSetup &) override;
0031   void beginRun(const edm::Run &, const edm::EventSetup &) override;
0032   void endRun(const edm::Run &, const edm::EventSetup &) override;
0033 
0034 protected:
0035 private:
0036   const edm::ESGetToken<DropBoxMetadata, DropBoxMetadataRcd> dropBoxToken_;
0037   bool readFromDB;
0038   std::map<std::string, std::map<std::string, std::string>> recordMap;
0039 };
0040 
0041 using namespace std;
0042 using namespace edm;
0043 
0044 PCLMetadataWriter::PCLMetadataWriter(const edm::ParameterSet &pSet)
0045     : dropBoxToken_(esConsumes<DropBoxMetadata, DropBoxMetadataRcd, edm::Transition::EndRun>()) {
0046   readFromDB = pSet.getParameter<bool>("readFromDB");
0047 
0048   vector<ParameterSet> recordsToMap = pSet.getParameter<vector<ParameterSet>>("recordsToMap");
0049   for (vector<ParameterSet>::const_iterator recordPset = recordsToMap.begin(); recordPset != recordsToMap.end();
0050        ++recordPset) {
0051     // record is the key which identifies one set of metadata in
0052     // DropBoxMetadataRcd (not necessarily a record in the strict framework
0053     // sense)
0054     string record = (*recordPset).getUntrackedParameter<string>("record");
0055 
0056     map<string, string> jrInfo;
0057     if (!readFromDB) {
0058       vector<string> paramKeys = (*recordPset).getParameterNames();
0059       for (vector<string>::const_iterator key = paramKeys.begin(); key != paramKeys.end(); ++key) {
0060         jrInfo["Source"] = "AlcaHarvesting";
0061         jrInfo["FileClass"] = "ALCA";
0062         if (*key != "record") {
0063           jrInfo[*key] = (*recordPset).getUntrackedParameter<string>(*key);
0064         }
0065       }
0066     }
0067     recordMap[record] = jrInfo;
0068   }
0069 }
0070 
0071 void PCLMetadataWriter::analyze(const edm::Event &event, const edm::EventSetup &eSetup) {}
0072 
0073 void PCLMetadataWriter::beginRun(const edm::Run &run, const edm::EventSetup &eSetup) {}
0074 
0075 void PCLMetadataWriter::endRun(const edm::Run &run, const edm::EventSetup &eSetup) {
0076   const DropBoxMetadata *metadata = nullptr;
0077 
0078   if (readFromDB) {
0079     // Read the objects
0080     metadata = &eSetup.getData(dropBoxToken_);
0081   }
0082 
0083   // get the PoolDBOutputService
0084   Service<cond::service::PoolDBOutputService> poolDbService;
0085   if (poolDbService.isAvailable()) {
0086     edm::Service<edm::JobReport> jr;
0087     if (jr.isAvailable()) {
0088       // the filename is unique for all records
0089       string filename = poolDbService->session().connectionString();
0090 
0091       // loop over all records
0092       for (map<string, map<string, string>>::const_iterator recordAndMap = recordMap.begin();
0093            recordAndMap != recordMap.end();
0094            ++recordAndMap) {
0095         string record = (*recordAndMap).first;
0096 
0097         // this is the map of metadata that we write in the JR
0098         map<string, string> jrInfo = (*recordAndMap).second;
0099         if (readFromDB) {
0100           if (metadata->knowsRecord(record)) {
0101             jrInfo = metadata->getRecordParameters(record).getParameterMap();
0102           }
0103         }
0104 
0105         // name of the the input tag in the metadata for the condUploader
0106         // metadata needs to be the same as the tag written out by the
0107         // harvesting step
0108         jrInfo["inputtag"] = poolDbService->tag(record);
0109 
0110         // actually write in the job report
0111         jr->reportAnalysisFile(filename, jrInfo);
0112       }
0113     }
0114   }
0115 }
0116 
0117 #include "FWCore/Framework/interface/MakerMacros.h"
0118 DEFINE_FWK_MODULE(PCLMetadataWriter);