Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:11

0001 // Author: Benedikt Hegner
0002 // Email:  benedikt.hegner@cern.ch
0003 
0004 #include <memory>
0005 #include <string>
0006 #include <fstream>
0007 #include <iostream>
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "FWCore/Utilities/interface/EDMException.h"
0015 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0016 #include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
0017 
0018 class JetCorrectorDBWriter : public edm::one::EDAnalyzer<> {
0019 public:
0020   JetCorrectorDBWriter(const edm::ParameterSet&);
0021   void beginJob() override;
0022   void analyze(const edm::Event&, const edm::EventSetup&) override {}
0023   void endJob() override {}
0024   ~JetCorrectorDBWriter() override {}
0025 
0026 private:
0027   std::string era;
0028   std::string algo;
0029   std::string path;
0030   std::string inputTxtFile;
0031   std::string payloadTag;
0032 };
0033 
0034 // Constructor
0035 JetCorrectorDBWriter::JetCorrectorDBWriter(const edm::ParameterSet& pSet) {
0036   era = pSet.getUntrackedParameter<std::string>("era");
0037   algo = pSet.getUntrackedParameter<std::string>("algo");
0038   path = pSet.getUntrackedParameter<std::string>("path");
0039   //payloadTag = "JetCorrectorParametersCollection_"+era+"_"+algo;
0040   payloadTag = algo;
0041 }
0042 
0043 // Begin Job
0044 void JetCorrectorDBWriter::beginJob() {
0045   //  std::string path("CondFormats/JetMETObjects/data/");
0046 
0047   JetCorrectorParametersCollection payload;
0048   std::cout << "Starting to import payload " << payloadTag << " from text files." << std::endl;
0049   for (int i = 0; i < JetCorrectorParametersCollection::N_LEVELS; ++i) {
0050     std::string append("_");
0051     std::string ilev =
0052         JetCorrectorParametersCollection::findLabel(static_cast<JetCorrectorParametersCollection::Level_t>(i));
0053     append += ilev;
0054     append += "_";
0055     append += algo;
0056     append += ".txt";
0057     inputTxtFile = path + era + append;
0058     try {
0059       edm::FileInPath fip(inputTxtFile);
0060       std::cout << "Opened file " << inputTxtFile << std::endl;
0061       // create the parameter object from file
0062       std::vector<std::string> sections;
0063       JetCorrectorParametersCollection::getSections(fip.fullPath(), sections);
0064       if (sections.empty()) {
0065         payload.push_back(i, JetCorrectorParameters(fip.fullPath(), ""));
0066       } else {
0067         for (std::vector<std::string>::const_iterator isectbegin = sections.begin(),
0068                                                       isectend = sections.end(),
0069                                                       isect = isectbegin;
0070              isect != isectend;
0071              ++isect) {
0072           payload.push_back(i, JetCorrectorParameters(fip.fullPath(), *isect), ilev + "_" + *isect);
0073           std::cout << "Added " << ilev + "_" + *isect << " to record " << i << std::endl;
0074         }
0075       }
0076       std::cout << "Added record " << i << std::endl;
0077     } catch (edm::Exception const&) {
0078       std::cout << "Did not find JEC file " << inputTxtFile << std::endl;
0079     }
0080   }
0081 
0082   std::cout << "Opening PoolDBOutputService" << std::endl;
0083 
0084   // now write it into the DB
0085   edm::Service<cond::service::PoolDBOutputService> s;
0086   if (s.isAvailable()) {
0087     std::cout << "Setting up payload tag " << payloadTag << std::endl;
0088     s->writeOneIOV(payload, s->beginOfTime(), payloadTag);
0089   }
0090   std::cout << "Wrote in CondDB payload label: " << payloadTag << std::endl;
0091 }
0092 
0093 DEFINE_FWK_MODULE(JetCorrectorDBWriter);