File indexing completed on 2024-04-06 12:02:19
0001
0002
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "CondFormats/JetMETObjects/interface/MEtXYcorrectParameters.h"
0005 #include "CondFormats/JetMETObjects/interface/Utilities.h"
0006 #include <iostream>
0007 #include <iomanip>
0008 #include <fstream>
0009 #include <sstream>
0010 #include <cstdlib>
0011 #include <algorithm>
0012 #include <cmath>
0013 #include <iterator>
0014
0015
0016
0017
0018
0019
0020 MEtXYcorrectParameters::Definitions::Definitions(const std::vector<std::string>& fBinVar,
0021 const std::vector<std::string>& fParVar,
0022 const std::string& fFormula) {
0023 mBinVar.reserve(fBinVar.size());
0024 for (unsigned i = 0; i < fBinVar.size(); i++)
0025 mBinVar.push_back(fBinVar[i]);
0026
0027 mParVar.reserve(fParVar.size());
0028 for (unsigned i = 0; i < fParVar.size(); i++)
0029 mParVar.push_back(getUnsigned(fParVar[i]));
0030
0031 mFormula = fFormula;
0032 }
0033
0034
0035
0036
0037 MEtXYcorrectParameters::Definitions::Definitions(const std::string& fLine) {
0038 std::vector<std::string> tokens = getTokens(fLine);
0039
0040 if (!tokens.empty()) {
0041 if (tokens.size() < 6) {
0042 std::stringstream sserr;
0043 sserr << "(line " << fLine << "): Great than or equal to 6 expected but the number of tokens:" << tokens.size();
0044 handleError("MEtXYcorrectParameters::Definitions", sserr.str());
0045 }
0046
0047
0048 ptclType = getSigned(tokens[0]);
0049 unsigned nBinVar = getUnsigned(tokens[1]);
0050 unsigned nParVar = getUnsigned(tokens[nBinVar + 2]);
0051 mBinVar.reserve(nBinVar);
0052 mParVar.reserve(nParVar);
0053
0054 for (unsigned i = 0; i < nBinVar; i++) {
0055 mBinVar.push_back(tokens[i + 2]);
0056 }
0057 for (unsigned i = 0; i < nParVar; i++) {
0058 mParVar.push_back(getUnsigned(tokens[nBinVar + 3 + i]));
0059 }
0060 mFormula = tokens[nParVar + nBinVar + 3];
0061 if (tokens.size() != nParVar + nBinVar + 4) {
0062 std::stringstream sserr;
0063 sserr << "(line " << fLine << "): token size should be:" << nParVar + nBinVar + 4 << " but it is "
0064 << tokens.size();
0065 handleError("MEtXYcorrectParameters::Definitions", sserr.str());
0066 }
0067 }
0068 }
0069
0070
0071
0072
0073 MEtXYcorrectParameters::Record::Record(const std::string& fLine, unsigned fNvar) : mMin(0), mMax(0), mParameters(0) {
0074 mNvar = fNvar;
0075
0076 std::vector<std::string> tokens = getTokens(fLine);
0077 if (!tokens.empty()) {
0078 if (tokens.size() < 5) {
0079 std::stringstream sserr;
0080 sserr << "(line " << fLine << "): "
0081 << "five tokens expected, " << tokens.size() << " provided.";
0082 handleError("MEtXYcorrectParameters::Record", sserr.str());
0083 }
0084 mMetAxis = tokens[0];
0085 for (unsigned i = 0; i < mNvar; i++) {
0086 mMin.push_back(getFloat(tokens[i * 2 + 1]));
0087 mMax.push_back(getFloat(tokens[i * 2 + 2]));
0088 }
0089 unsigned nParam = getUnsigned(tokens[2 * mNvar + 1]);
0090 if (nParam != tokens.size() - (2 * mNvar + 2)) {
0091 std::stringstream sserr;
0092 sserr << "(line " << fLine << "): " << tokens.size() - (2 * mNvar + 2) << " parameters, but nParam=" << nParam
0093 << ".";
0094 handleError("MEtXYcorrectParameters::Record", sserr.str());
0095 }
0096 for (unsigned i = (2 * mNvar + 2); i < tokens.size(); ++i) {
0097 mParameters.push_back(getFloat(tokens[i]));
0098 }
0099 }
0100 }
0101
0102
0103
0104
0105 MEtXYcorrectParameters::MEtXYcorrectParameters(const std::string& fFile, const std::string& fSection) {
0106 std::ifstream input(fFile.c_str());
0107 std::string currentSection = "";
0108 std::string line;
0109 std::string currentDefinitions = "";
0110 while (std::getline(input, line)) {
0111 std::string section = getSection(line);
0112 std::string tmp = getDefinitions(line);
0113 if (!section.empty() && tmp.empty()) {
0114 currentSection = section;
0115 continue;
0116 }
0117 if (currentSection == fSection) {
0118 if (!tmp.empty()) {
0119 currentDefinitions = tmp;
0120 continue;
0121 }
0122 Definitions definitions(currentDefinitions);
0123 if (!(definitions.nBinVar() == 0 && definitions.formula().empty()))
0124 mDefinitions = definitions;
0125 Record record(line, mDefinitions.nBinVar());
0126 bool check(true);
0127 for (unsigned i = 0; i < mDefinitions.nBinVar(); ++i)
0128 if (record.xMin(i) == 0 && record.xMax(i) == 0)
0129 check = false;
0130 if (record.nParameters() == 0)
0131 check = false;
0132 if (check)
0133 mRecords.push_back(record);
0134 }
0135 }
0136 if (currentDefinitions.empty())
0137 handleError("MEtXYcorrectParameters", "No definitions found!!!");
0138 if (mRecords.empty() && currentSection.empty())
0139 mRecords.push_back(Record());
0140 if (mRecords.empty() && !currentSection.empty()) {
0141 std::stringstream sserr;
0142 sserr << "the requested section " << fSection << " doesn't exist!";
0143 handleError("MEtXYcorrectParameters", sserr.str());
0144 }
0145 std::sort(mRecords.begin(), mRecords.end());
0146 valid_ = true;
0147 }
0148
0149
0150
0151 void MEtXYcorrectParameters::printScreen() const {
0152 std::cout << "--------------------------------------------" << std::endl;
0153 std::cout << "//////// PARAMETERS: //////////////////////" << std::endl;
0154 std::cout << "--------------------------------------------" << std::endl;
0155 std::cout << "Number of binning variables: " << definitions().nBinVar() << std::endl;
0156 std::cout << "Names of binning variables: ";
0157 for (unsigned i = 0; i < definitions().nBinVar(); i++)
0158 std::cout << definitions().binVar(i) << " ";
0159 std::cout << std::endl;
0160 std::cout << "--------------------------------------------" << std::endl;
0161 std::cout << "Number of parameter variables: " << definitions().nParVar() << std::endl;
0162 std::cout << "Names of parameter variables: ";
0163 for (unsigned i = 0; i < definitions().nParVar(); i++)
0164 std::cout << definitions().parVar(i) << " ";
0165 std::cout << std::endl;
0166 std::cout << "--------------------------------------------" << std::endl;
0167 std::cout << "Parametrization Formula: " << definitions().formula() << std::endl;
0168 std::cout << "--------------------------------------------" << std::endl;
0169 std::cout << "------- Bin contents -----------------------" << std::endl;
0170 for (unsigned i = 0; i < size(); i++) {
0171 for (unsigned j = 0; j < definitions().nBinVar(); j++)
0172 std::cout << record(i).xMin(j) << " " << record(i).xMax(j) << " ";
0173 std::cout << record(i).nParameters() << " ";
0174 for (unsigned j = 0; j < record(i).nParameters(); j++)
0175 std::cout << record(i).parameter(j) << " ";
0176 std::cout << std::endl;
0177 }
0178 }
0179 void MEtXYcorrectParameters::printScreen(const std::string& Section) const {
0180 std::cout << "--------------------------------------------" << std::endl;
0181 std::cout << "//////// PARAMETERS: //////////////////////" << std::endl;
0182 std::cout << "--------------------------------------------" << std::endl;
0183 std::cout << "[" << Section << "]"
0184 << "\n";
0185 std::cout << "Number of binning variables: " << definitions().nBinVar() << std::endl;
0186 std::cout << "Names of binning variables: ";
0187 for (unsigned i = 0; i < definitions().nBinVar(); i++)
0188 std::cout << definitions().binVar(i) << " ";
0189 std::cout << std::endl;
0190 std::cout << "--------------------------------------------" << std::endl;
0191 std::cout << "Number of parameter variables: " << definitions().nParVar() << std::endl;
0192 std::cout << "Names of parameter variables: ";
0193 for (unsigned i = 0; i < definitions().nParVar(); i++)
0194 std::cout << definitions().parVar(i) << " ";
0195 std::cout << std::endl;
0196 std::cout << "--------------------------------------------" << std::endl;
0197 std::cout << "Parametrization Formula: " << definitions().formula() << std::endl;
0198 std::cout << "--------------------------------------------" << std::endl;
0199 std::cout << "------- Bin contents -----------------------" << std::endl;
0200 for (unsigned i = 0; i < size(); i++)
0201 {
0202 std::cout << record(i).MetAxis() << " ";
0203 std::cout << "nBinVar (" << definitions().nBinVar() << ") ";
0204 for (unsigned j = 0; j < definitions().nBinVar(); j++)
0205 std::cout << record(i).xMin(j) << " " << record(i).xMax(j) << " ";
0206 std::cout << "nParameters (" << record(i).nParameters() << ") ";
0207 for (unsigned j = 0; j < record(i).nParameters(); j++)
0208 std::cout << record(i).parameter(j) << " ";
0209 std::cout << std::endl;
0210 }
0211 }
0212
0213
0214
0215 void MEtXYcorrectParameters::printFile(const std::string& fFileName) const {
0216 std::ofstream txtFile;
0217 txtFile.open(fFileName.c_str());
0218 txtFile.setf(std::ios::right);
0219 txtFile << "{" << definitions().nBinVar() << std::setw(15);
0220 for (unsigned i = 0; i < definitions().nBinVar(); i++)
0221 txtFile << definitions().binVar(i) << std::setw(15);
0222 txtFile << definitions().nParVar() << std::setw(15);
0223 for (unsigned i = 0; i < definitions().nParVar(); i++)
0224 txtFile << definitions().parVar(i) << std::setw(15);
0225 txtFile << std::setw(definitions().formula().size() + 15) << definitions().formula() << std::setw(15);
0226 txtFile << "}"
0227 << "\n";
0228 for (unsigned i = 0; i < size(); i++) {
0229 for (unsigned j = 0; j < definitions().nBinVar(); j++)
0230 txtFile << record(i).xMin(j) << std::setw(15) << record(i).xMax(j) << std::setw(15);
0231 txtFile << record(i).nParameters() << std::setw(15);
0232 for (unsigned j = 0; j < record(i).nParameters(); j++)
0233 txtFile << record(i).parameter(j) << std::setw(15);
0234 txtFile << "\n";
0235 }
0236 txtFile.close();
0237 }
0238 void MEtXYcorrectParameters::printFile(const std::string& fFileName, const std::string& Section) const {
0239 std::ofstream txtFile;
0240 txtFile.open(fFileName.c_str(), std::ofstream::app);
0241 txtFile.setf(std::ios::right);
0242 txtFile << "[" << Section << "]"
0243 << "\n";
0244 txtFile << "{"
0245 << " " << definitions().PtclType() << " " << definitions().nBinVar();
0246 for (unsigned i = 0; i < definitions().nBinVar(); i++)
0247 txtFile << " " << definitions().binVar(i);
0248 txtFile << " " << definitions().nParVar();
0249 for (unsigned i = 0; i < definitions().nParVar(); i++)
0250 txtFile << " " << definitions().parVar(i);
0251 txtFile << " " << definitions().formula();
0252 txtFile << "}"
0253 << "\n";
0254 for (unsigned i = 0; i < size(); i++)
0255 {
0256 txtFile << record(i).MetAxis();
0257 for (unsigned j = 0; j < definitions().nBinVar(); j++)
0258 txtFile << " " << record(i).xMin(j) << " " << record(i).xMax(j);
0259 txtFile << " " << record(i).nParameters();
0260 for (unsigned j = 0; j < record(i).nParameters(); j++)
0261 txtFile << " " << record(i).parameter(j);
0262 txtFile << "\n";
0263 }
0264 txtFile.close();
0265 }
0266
0267 namespace {
0268 const std::vector<std::string> labels_ = {"shiftMC", "shiftDY", "shiftTTJets", "shiftWJets", "shiftData"};
0269 const std::vector<std::string> shiftFlavors_ = {"hEtaPlus",
0270 "hEtaMinus",
0271 "h0Barrel",
0272 "h0EndcapPlus",
0273 "h0EndcapMinus",
0274 "gammaBarrel",
0275 "gammaEndcapPlus",
0276 "gammaEndcapMinus",
0277 "hHFPlus",
0278 "hHFMinus",
0279 "egammaHFPlus",
0280 "egammaHFMinus"};
0281
0282 }
0283
0284 std::string MEtXYcorrectParametersCollection::findLabel(key_type k) {
0285 if (isShiftMC(k)) {
0286 return findShiftMCflavor(k);
0287 } else if (MEtXYcorrectParametersCollection::isShiftDY(k)) {
0288 return findShiftDYflavor(k);
0289 } else if (MEtXYcorrectParametersCollection::isShiftTTJets(k)) {
0290 return findShiftTTJetsFlavor(k);
0291 } else if (MEtXYcorrectParametersCollection::isShiftWJets(k)) {
0292 return findShiftWJetsFlavor(k);
0293 } else if (MEtXYcorrectParametersCollection::isShiftData(k)) {
0294 return findShiftDataFlavor(k);
0295 }
0296 return labels_[k];
0297 }
0298
0299 std::string MEtXYcorrectParametersCollection::levelName(key_type k) {
0300 if (isShiftMC(k)) {
0301 return labels_[shiftMC];
0302 } else if (isShiftDY(k)) {
0303 return labels_[shiftDY];
0304 } else if (isShiftTTJets(k)) {
0305 return labels_[shiftTTJets];
0306 } else if (isShiftWJets(k)) {
0307 return labels_[shiftWJets];
0308 } else if (isShiftData(k)) {
0309 return labels_[shiftData];
0310 } else {
0311 return "Can't find the level name !!!!";
0312 }
0313 }
0314
0315 std::string MEtXYcorrectParametersCollection::findShiftMCflavor(key_type k) {
0316 if (k == shiftMC)
0317 return labels_[shiftMC];
0318 else
0319 return shiftFlavors_[k - (shiftMC + 1) * 100 - 1];
0320 }
0321 std::string MEtXYcorrectParametersCollection::findShiftDYflavor(key_type k) {
0322 if (k == shiftDY)
0323 return labels_[shiftDY];
0324 else
0325 return shiftFlavors_[k - (shiftDY + 1) * 100 - 1];
0326 }
0327 std::string MEtXYcorrectParametersCollection::findShiftTTJetsFlavor(key_type k) {
0328 if (k == shiftTTJets)
0329 return labels_[shiftTTJets];
0330 else
0331 return shiftFlavors_[k - (shiftTTJets + 1) * 100 - 1];
0332 }
0333 std::string MEtXYcorrectParametersCollection::findShiftWJetsFlavor(key_type k) {
0334 if (k == shiftWJets)
0335 return labels_[shiftWJets];
0336 else
0337 return shiftFlavors_[k - (shiftWJets + 1) * 100 - 1];
0338 }
0339 std::string MEtXYcorrectParametersCollection::findShiftDataFlavor(key_type k) {
0340 if (k == shiftData)
0341 return labels_[shiftData];
0342 else
0343 return shiftFlavors_[k - (shiftData + 1) * 100 - 1];
0344 }
0345
0346 void MEtXYcorrectParametersCollection::getSections(std::string inputFile, std::vector<std::string>& outputs) {
0347 outputs.clear();
0348 std::ifstream input(inputFile.c_str());
0349 while (!input.eof()) {
0350 char buff[10000];
0351 input.getline(buff, 10000);
0352 std::string in(buff);
0353 if (in[0] == '[') {
0354 std::string tok = getSection(in);
0355 if (!tok.empty()) {
0356 outputs.push_back(tok);
0357 }
0358 }
0359 }
0360
0361
0362 std::string sectionNames;
0363 for (std::vector<std::string>::const_iterator it = outputs.begin(); it != outputs.end(); it++) {
0364 sectionNames += *it;
0365 sectionNames += "\n";
0366 }
0367 edm::LogInfo("getSections") << "Sections read from file: "
0368 << "\n"
0369 << sectionNames;
0370 }
0371
0372
0373 void MEtXYcorrectParametersCollection::push_back(key_type i, value_type const& j, label_type const& flav) {
0374 if (isShiftMC(i)) {
0375 correctionsShift_.push_back(pair_type(getShiftMcFlavBin(flav), j));
0376 } else if (isShiftDY(i)) {
0377 correctionsShift_.push_back(pair_type(getShiftDyFlavBin(flav), j));
0378 } else if (isShiftTTJets(i)) {
0379 correctionsShift_.push_back(pair_type(getShiftTTJetsFlavBin(flav), j));
0380 } else if (isShiftWJets(i)) {
0381 correctionsShift_.push_back(pair_type(getShiftWJetsFlavBin(flav), j));
0382 } else if (isShiftData(i)) {
0383 correctionsShift_.push_back(pair_type(getShiftDataFlavBin(flav), j));
0384 } else {
0385 std::stringstream sserr;
0386 sserr << "The level type: " << i << " is not in the level list";
0387 handleError("MEtXYcorrectParameters::Definitions", sserr.str());
0388 }
0389 }
0390
0391
0392
0393 MEtXYcorrectParameters const& MEtXYcorrectParametersCollection::operator[](key_type k) const {
0394 collection_type::const_iterator ibegin, iend, i;
0395 if (isShiftMC(k) || isShiftDY(k) || isShiftTTJets(k) || isShiftWJets(k) || isShiftData(k)) {
0396 ibegin = correctionsShift_.begin();
0397 iend = correctionsShift_.end();
0398 i = ibegin;
0399 }
0400 for (; i != iend; ++i) {
0401 if (k == i->first)
0402 return i->second;
0403 }
0404 throw cms::Exception("InvalidInput") << " cannot find key " << static_cast<int>(k)
0405 << " in the METC payload, this usually means you have to change the global tag"
0406 << std::endl;
0407 }
0408
0409
0410
0411 void MEtXYcorrectParametersCollection::validKeys(std::vector<key_type>& keys) const {
0412 keys.clear();
0413 for (collection_type::const_iterator ibegin = correctionsShift_.begin(), iend = correctionsShift_.end(), i = ibegin;
0414 i != iend;
0415 ++i) {
0416 keys.push_back(i->first);
0417 }
0418 }
0419
0420 MEtXYcorrectParametersCollection::key_type MEtXYcorrectParametersCollection::getShiftMcFlavBin(std::string const& flav) {
0421 std::vector<std::string>::const_iterator found = find(shiftFlavors_.begin(), shiftFlavors_.end(), flav);
0422 if (found != shiftFlavors_.end()) {
0423 return (found - shiftFlavors_.begin() + 1) + (shiftMC + 1) * 100;
0424 } else {
0425 throw cms::Exception("InvalidInput") << "************** Can't find ShiftSection: " << flav << std::endl;
0426 }
0427 return 0;
0428 }
0429
0430 MEtXYcorrectParametersCollection::key_type MEtXYcorrectParametersCollection::getShiftDyFlavBin(std::string const& flav) {
0431 std::vector<std::string>::const_iterator found = find(shiftFlavors_.begin(), shiftFlavors_.end(), flav);
0432 if (found != shiftFlavors_.end()) {
0433 return (found - shiftFlavors_.begin() + 1) + (shiftDY + 1) * 100;
0434 } else {
0435 throw cms::Exception("InvalidInput") << "************** Can't find ShiftSection: " << flav << std::endl;
0436 }
0437 return 0;
0438 }
0439 MEtXYcorrectParametersCollection::key_type MEtXYcorrectParametersCollection::getShiftTTJetsFlavBin(
0440 std::string const& flav) {
0441 std::vector<std::string>::const_iterator found = find(shiftFlavors_.begin(), shiftFlavors_.end(), flav);
0442 if (found != shiftFlavors_.end()) {
0443 return (found - shiftFlavors_.begin() + 1) + (shiftTTJets + 1) * 100;
0444 } else {
0445 throw cms::Exception("InvalidInput") << "************** Can't find shiftSection: " << flav << std::endl;
0446 }
0447 return 0;
0448 }
0449 MEtXYcorrectParametersCollection::key_type MEtXYcorrectParametersCollection::getShiftWJetsFlavBin(
0450 std::string const& flav) {
0451 std::vector<std::string>::const_iterator found = find(shiftFlavors_.begin(), shiftFlavors_.end(), flav);
0452 if (found != shiftFlavors_.end()) {
0453 return (found - shiftFlavors_.begin() + 1) + (shiftWJets + 1) * 100;
0454 } else {
0455 throw cms::Exception("InvalidInput") << "************** Can't find shiftSection: " << flav << std::endl;
0456 }
0457 return 0;
0458 }
0459 MEtXYcorrectParametersCollection::key_type MEtXYcorrectParametersCollection::getShiftDataFlavBin(
0460 std::string const& flav) {
0461 std::vector<std::string>::const_iterator found = find(shiftFlavors_.begin(), shiftFlavors_.end(), flav);
0462 if (found != shiftFlavors_.end()) {
0463 return (found - shiftFlavors_.begin() + 1) + (shiftData + 1) * 100;
0464 } else {
0465 throw cms::Exception("InvalidInput") << "************** Can't find shiftSection: " << flav << std::endl;
0466 }
0467 return 0;
0468 }
0469
0470 bool MEtXYcorrectParametersCollection::isShiftMC(key_type k) {
0471 return k == shiftMC || (k > (shiftMC + 1) * 100 && k < (shiftMC + 2) * 100);
0472 }
0473 bool MEtXYcorrectParametersCollection::isShiftDY(key_type k) {
0474 return k == shiftDY || (k > (shiftDY + 1) * 100 && k < (shiftDY + 2) * 100);
0475 }
0476 bool MEtXYcorrectParametersCollection::isShiftTTJets(key_type k) {
0477 return k == shiftTTJets || (k > (shiftTTJets + 1) * 100 && k < (shiftTTJets + 2) * 100);
0478 }
0479 bool MEtXYcorrectParametersCollection::isShiftWJets(key_type k) {
0480 return k == shiftWJets || (k > (shiftWJets + 1) * 100 && k < (shiftWJets + 2) * 100);
0481 }
0482 bool MEtXYcorrectParametersCollection::isShiftData(key_type k) {
0483 return k == shiftData || (k > (shiftData + 1) * 100 && k < (shiftData + 2) * 100);
0484 }
0485
0486 #include "FWCore/Utilities/interface/typelookup.h"
0487
0488 TYPELOOKUP_DATA_REG(MEtXYcorrectParameters);
0489 TYPELOOKUP_DATA_REG(MEtXYcorrectParametersCollection);