Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-27 18:27:18

0001 #include <memory>
0002 #include <string>
0003 #include <fstream>
0004 #include <iostream>
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0012 #include "CondFormats/PhysicsToolsObjects/interface/PerformancePayloadFromTable.h"
0013 
0014 #include "CondFormats/PhysicsToolsObjects/interface/PerformanceWorkingPoint.h"
0015 
0016 class PhysicsPerformanceDBWriterFromFile_WPandPayload : public edm::global::EDAnalyzer<> {
0017 public:
0018   PhysicsPerformanceDBWriterFromFile_WPandPayload(const edm::ParameterSet&);
0019   void beginJob() override;
0020   void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const override {}
0021   void endJob() override {}
0022   ~PhysicsPerformanceDBWriterFromFile_WPandPayload() override {}
0023 
0024 private:
0025   std::string inputTxtFile;
0026   std::string rec1, rec2;
0027 };
0028 
0029 PhysicsPerformanceDBWriterFromFile_WPandPayload::PhysicsPerformanceDBWriterFromFile_WPandPayload(
0030     const edm::ParameterSet& p) {
0031   inputTxtFile = p.getUntrackedParameter<std::string>("inputTxtFile");
0032   rec1 = p.getUntrackedParameter<std::string>("RecordPayload");
0033   rec2 = p.getUntrackedParameter<std::string>("RecordWP");
0034 }
0035 
0036 void PhysicsPerformanceDBWriterFromFile_WPandPayload::beginJob() {
0037   //
0038   // read object from file
0039   //
0040 
0041   //
0042   // File Format is
0043   // - tagger name
0044   // - cut
0045   // - concrete class name
0046   // - how many results and how many binning
0047   // - their values
0048   // - vector<float>
0049   //
0050 
0051   std::ifstream in;
0052   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "Opening " << inputTxtFile;
0053   in.open(inputTxtFile.c_str());
0054   std::string tagger;
0055   float cut;
0056 
0057   std::string concreteType;
0058   std::string comment;
0059   std::vector<float> pl;
0060   int stride;
0061 
0062   in >> tagger;
0063   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "WP Tagger is " << tagger;
0064 
0065   in >> cut;
0066   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "WP Cut is " << cut;
0067 
0068   in >> concreteType;
0069   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "concrete Type is " << concreteType;
0070 
0071   //  return ;
0072 
0073   // read # of results
0074 
0075   int nres, nbin;
0076   in >> nres;
0077   in >> nbin;
0078   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload")
0079       << " Results: " << nres << " Binning variables: " << nbin;
0080 
0081   stride = nres + nbin * 2;
0082   if (!stride) {
0083     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << " Malformed input file";
0084     exit(1);
0085   }
0086 
0087   int number = 0;
0088 
0089   std::vector<PerformanceResult::ResultType> res;
0090   std::vector<BinningVariables::BinningVariablesType> bin;
0091 
0092   while (number < nres && !in.eof()) {
0093     int tmp;
0094     in >> tmp;
0095     res.push_back((PerformanceResult::ResultType)(tmp));
0096     number++;
0097   }
0098   if (number != nres) {
0099     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "Table not well formed";
0100   }
0101   number = 0;
0102   while (number < nbin && !in.eof()) {
0103     int tmp;
0104     in >> tmp;
0105     bin.push_back((BinningVariables::BinningVariablesType)(tmp));
0106     number++;
0107   }
0108   if (number != nbin) {
0109     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "Table not well formed";
0110   }
0111 
0112   number = 0;
0113   while (!in.eof()) {
0114     float temp;
0115     in >> temp;
0116     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload")
0117         << " Intersing " << temp << " in position " << number;
0118     number++;
0119     pl.push_back(temp);
0120   }
0121 
0122   //
0123   // CHECKS
0124   //
0125   if (stride != nbin * 2 + nres) {
0126     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "Table not well formed";
0127   }
0128   if ((number % stride) != 0) {
0129     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << "Table not well formed";
0130   }
0131   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << " CLOSING ";
0132   in.close();
0133 
0134   /*  for (int k=0;k<(number/stride); k++){
0135     for (int j=0; j<stride; j++){
0136       std::cout << "Pos["<<k<<","<<j<<"] = "<<pl[k*stride+j]<<std::endl;
0137     }
0138   }
0139   */
0140 
0141   //
0142   // now create pl etc etc
0143   //
0144 
0145   PerformanceWorkingPoint wp(cut, tagger);
0146 
0147   PerformancePayloadFromTable btagpl;
0148 
0149   if (concreteType == "PerformancePayloadFromTable") {
0150     btagpl = PerformancePayloadFromTable(res, bin, stride, pl);
0151   } else {
0152     edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << " Non existing request: " << concreteType;
0153   }
0154 
0155   edm::LogInfo("PhysicsPerformanceDBWriterFromFile_WPandPayload") << " Created the " << concreteType << " object";
0156 
0157   edm::Service<cond::service::PoolDBOutputService> s;
0158   if (s.isAvailable()) {
0159     s->writeOneIOV(btagpl, s->beginOfTime(), rec1);
0160     // write also the WP
0161     s->writeOneIOV(wp, s->beginOfTime(), rec2);
0162   }
0163 }
0164 
0165 DEFINE_FWK_MODULE(PhysicsPerformanceDBWriterFromFile_WPandPayload);