Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:30

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/PerformancePayloadFromBinnedTFormula.h"
0013 #include "CondFormats/PhysicsToolsObjects/interface/PerformanceWorkingPoint.h"
0014 
0015 class PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL : public edm::global::EDAnalyzer<> {
0016 public:
0017   PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL(const edm::ParameterSet&);
0018   void beginJob() override;
0019   void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const override {}
0020   void endJob() override {}
0021   ~PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL() override {}
0022 
0023 private:
0024   std::string inputTxtFile;
0025   std::string rec1, rec2;
0026 };
0027 
0028 PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL::PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL(
0029     const edm::ParameterSet& p) {
0030   inputTxtFile = p.getUntrackedParameter<std::string>("inputTxtFile");
0031   rec1 = p.getUntrackedParameter<std::string>("RecordPayload");
0032   rec2 = p.getUntrackedParameter<std::string>("RecordWP");
0033 }
0034 
0035 void PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL::beginJob() {
0036   //
0037   // read object from file
0038   //
0039 
0040   //
0041   // File Format is
0042   // - tagger name
0043   // - cut
0044   // - concrete class name
0045   // number of results (btageff, btagSF....)
0046   // number of binning variables in the parameterization (eta, pt ...)
0047   // number of bins
0048   // - results (as ints)
0049   // - variables (as ints)
0050   // - formulas
0051   // - the limits
0052   //
0053 
0054   std::ifstream in;
0055   in.open(inputTxtFile.c_str());
0056   std::string tagger;
0057   float cut;
0058 
0059   std::string concreteType;
0060 
0061   in >> tagger;
0062   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "WP Tagger is " << tagger;
0063 
0064   in >> cut;
0065   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "WP Cut is " << cut;
0066 
0067   in >> concreteType;
0068   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "concrete Type is " << concreteType;
0069 
0070   int nres = 0, nvar = 0;
0071 
0072   in >> nres;
0073   in >> nvar;
0074 
0075   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL")
0076       << "Using " << nres << " results and " << nvar << " variables";
0077 
0078   unsigned int bins = 0;  //temporary for now!!!!!!
0079 
0080   in >> bins;
0081 
0082   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "Using " << bins << " bins";
0083 
0084   int number = 0;
0085   ;
0086 
0087   std::vector<PerformanceResult::ResultType> res;
0088   std::vector<BinningVariables::BinningVariablesType> bin;
0089   //
0090   // read results
0091   //
0092   number = 0;
0093   while (number < nres && !in.eof()) {
0094     int tmp;
0095     in >> tmp;
0096     res.push_back((PerformanceResult::ResultType)(tmp));
0097     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Result #" << number << " is " << tmp;
0098     number++;
0099   }
0100   if (number != nres) {
0101     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Table not well formed";
0102   }
0103 
0104   //
0105   // read the variables
0106   //
0107 
0108   PerformanceWorkingPoint wp(cut, tagger);
0109   PerformancePayloadFromBinnedTFormula btagpl;
0110 
0111   std::vector<PhysicsTFormulaPayload> v_ppl;
0112 
0113   number = 0;
0114   while (number < nvar && !in.eof()) {
0115     int tmp;
0116     in >> tmp;
0117     bin.push_back((BinningVariables::BinningVariablesType)(tmp));
0118     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Variable #" << number << " is " << tmp;
0119     number++;
0120   }
0121   if (number != nvar) {
0122     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Table not well formed";
0123   }
0124 
0125   //
0126   // now read the formulas
0127   //
0128 
0129   for (unsigned int recregion = 0; recregion < bins; ++recregion) {
0130     std::vector<std::pair<float, float> > limits;
0131     std::vector<std::string> formulas;
0132 
0133     number = 0;
0134 
0135     while (number < nres && (!in.eof())) {
0136       std::string temp;
0137       in >> temp;
0138       edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL")
0139           << " Inserting " << temp << " as formula in position " << number;
0140       number++;
0141       formulas.push_back(temp);
0142     }
0143     /*
0144       if (nres!= number ){
0145       std::cout <<" NOT OK, this is not what I would expect"<<std::endl;
0146       abort();
0147       }
0148     */
0149 
0150     number = 0;
0151     while (number < nvar && (!in.eof())) {
0152       float temp1, temp2;
0153       in >> temp1;
0154       in >> temp2;
0155       edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL")
0156           << " Inserting " << temp1 << "," << temp2 << " as limits in position " << number;
0157       number++;
0158       limits.push_back(std::pair<float, float>(temp1, temp2));
0159     }
0160     /*
0161       if (nvar != number ){
0162       std::cout <<" NOT OK, this is not what I would expect"<<std::endl;
0163       abort();
0164       }
0165     */
0166 
0167     //
0168     // push it
0169     //
0170 
0171     PhysicsTFormulaPayload ppl(limits, formulas);
0172     v_ppl.push_back(ppl);
0173   }
0174   in.close();
0175 
0176   if (concreteType == "PerformancePayloadFromBinnedTFormula") {
0177     btagpl = PerformancePayloadFromBinnedTFormula(res, bin, v_ppl);
0178     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL")
0179         << " CHECK: " << btagpl.formulaPayloads().size();
0180   } else {
0181     edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Non existing request: " << concreteType;
0182   }
0183 
0184   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << " Created the " << concreteType << " object";
0185 
0186   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "Start writing the payload and WP";
0187   edm::Service<cond::service::PoolDBOutputService> s;
0188   if (s.isAvailable()) {
0189     s->writeOneIOV(btagpl, s->beginOfTime(), rec1);
0190     // write also the WP
0191     s->writeOneIOV(wp, s->beginOfTime(), rec2);
0192   }
0193 
0194   edm::LogInfo("PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL") << "Finised writing the payload and WP";
0195 }
0196 
0197 DEFINE_FWK_MODULE(PhysicsPerformanceDBWriterTFormula_fromfile_WPandPL);