File indexing completed on 2023-03-17 11:19:59
0001 #include "RecoLuminosity/LumiProducer/interface/NormDML.h"
0002 #include "RelationalAccess/ISchema.h"
0003 #include "RelationalAccess/IQuery.h"
0004 #include "RelationalAccess/ICursor.h"
0005 #include "CoralBase/AttributeList.h"
0006 #include "CoralBase/Attribute.h"
0007 #include "CoralBase/AttributeSpecification.h"
0008 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0009 #include <algorithm>
0010 #include <map>
0011 #include <iostream>
0012 #include <sstream>
0013 #include <boost/algorithm/string.hpp>
0014 #include <boost/tokenizer.hpp>
0015 lumi::NormDML::NormDML() {}
0016 unsigned long long lumi::NormDML::normIdByName(const coral::ISchema& schema, const std::string& normtagname) {
0017
0018 unsigned long long result = 0;
0019 std::vector<unsigned long long> luminormids;
0020 coral::IQuery* qHandle = schema.newQuery();
0021 qHandle->addToTableList(lumi::LumiNames::luminormv2TableName());
0022 qHandle->addToOutputList("DATA_ID");
0023 if (!normtagname.empty()) {
0024 std::string qConditionStr("ENTRY_NAME=:normtagname");
0025 coral::AttributeList qCondition;
0026 qCondition.extend("normtagname", typeid(std::string));
0027 qCondition["normtagname"].data<std::string>() = normtagname;
0028 qHandle->setCondition(qConditionStr, qCondition);
0029 }
0030 coral::AttributeList qResult;
0031 qResult.extend("DATA_ID", typeid(unsigned long long));
0032 qHandle->defineOutput(qResult);
0033 coral::ICursor& cursor = qHandle->execute();
0034 while (cursor.next()) {
0035 const coral::AttributeList& row = cursor.currentRow();
0036 luminormids.push_back(row["DATA_ID"].data<unsigned long long>());
0037 }
0038 delete qHandle;
0039 std::vector<unsigned long long>::iterator resultIt;
0040 for (resultIt = luminormids.begin(); resultIt != luminormids.end(); ++resultIt) {
0041 if ((*resultIt) > result) {
0042 result = *resultIt;
0043 }
0044 }
0045 return result;
0046 }
0047 void lumi::NormDML::normIdByType(const coral::ISchema& schema,
0048 std::map<std::string, unsigned long long>& resultMap,
0049 lumi::NormDML::LumiType lumitype,
0050 bool defaultonly) {
0051
0052 coral::IQuery* qHandle = schema.newQuery();
0053 qHandle->addToTableList(lumi::LumiNames::luminormv2TableName());
0054 qHandle->addToOutputList("DATA_ID");
0055 qHandle->addToOutputList("ENTRY_NAME");
0056 coral::AttributeList qCondition;
0057 std::string qConditionStr("LUMITYPE=:lumitype");
0058 qCondition.extend("lumitype", typeid(std::string));
0059 std::string lumitypeStr("HF");
0060 if (lumitype != lumi::NormDML::HF) {
0061 lumitypeStr = "PIXEL";
0062 }
0063 qCondition["lumitype"].data<std::string>() = lumitypeStr;
0064 if (defaultonly) {
0065 qConditionStr += " AND ISTYPEDEFAULT=:istypedefault";
0066 qCondition.extend("istypedefault", typeid(unsigned int));
0067 qCondition["istypedefault"].data<unsigned int>() = 1;
0068 }
0069 qHandle->setCondition(qConditionStr, qCondition);
0070 coral::AttributeList qResult;
0071 qResult.extend("DATA_ID", typeid(unsigned long long));
0072 qResult.extend("ENTRY_NAME", typeid(std::string));
0073 qHandle->defineOutput(qResult);
0074 try {
0075 coral::ICursor& cursor = qHandle->execute();
0076 while (cursor.next()) {
0077 const coral::AttributeList& row = cursor.currentRow();
0078 const std::string normname = row["ENTRY_NAME"].data<std::string>();
0079 unsigned long long normid = row["DATA_ID"].data<unsigned long long>();
0080 if (resultMap.find(normname) == resultMap.end()) {
0081 resultMap.insert(std::make_pair(normname, normid));
0082 } else {
0083 if (resultMap[normname] < normid) {
0084 resultMap.insert(std::make_pair(normname, normid));
0085 }
0086 }
0087 }
0088 } catch (const coral::Exception& er) {
0089 std::cout << "database error in NormDML::normIdByType " << er.what() << std::endl;
0090 delete qHandle;
0091 throw er;
0092 } catch (...) {
0093 throw;
0094 }
0095 delete qHandle;
0096 }
0097 void lumi::NormDML::normById(const coral::ISchema& schema,
0098 unsigned long long normid,
0099 std::map<unsigned int, lumi::NormDML::normData>& result) {
0100
0101 coral::IQuery* qHandle = schema.newQuery();
0102 qHandle->addToTableList(lumi::LumiNames::luminormv2dataTableName());
0103 std::string qConditionStr("DATA_ID=:normid ");
0104 coral::AttributeList qCondition;
0105 qCondition.extend("normid", typeid(unsigned long long));
0106 qCondition["normid"].data<unsigned long long>() = normid;
0107 qHandle->setCondition(qConditionStr, qCondition);
0108 coral::AttributeList qResult;
0109 coral::ICursor& cursor = qHandle->execute();
0110 while (cursor.next()) {
0111 const coral::AttributeList& row = cursor.currentRow();
0112 unsigned int since = row["SINCE"].data<unsigned int>();
0113 if (result.find(since) == result.end()) {
0114 lumi::NormDML::normData thisnorm;
0115 result.insert(std::make_pair(since, thisnorm));
0116 }
0117 const std::string correctorStr = row["CORRECTOR"].data<std::string>();
0118 if (!row["AMODETAG"].isNull()) {
0119 result[since].amodetag = row["AMODETAG"].data<std::string>();
0120 }
0121 if (!row["NOMINALEGEV"].isNull()) {
0122 result[since].beamegev = row["NOMINALEGEV"].data<unsigned int>();
0123 }
0124
0125 std::vector<std::string> correctorParams;
0126 parseLumiCorrector(correctorStr, correctorParams);
0127 result[since].corrfunc = *(correctorParams.begin());
0128 for (std::vector<std::string>::iterator corrIt = correctorParams.begin() + 1; corrIt != correctorParams.end();
0129 corrIt++) {
0130 std::string paramName = boost::to_upper_copy(*corrIt);
0131 if (paramName == std::string("AFTERGLOW")) {
0132 const std::string afterglowStr = row["AFTERGLOW"].data<std::string>();
0133 parseAfterglows(afterglowStr, result[since].afterglows);
0134 } else {
0135 float param = row[paramName].data<float>();
0136 result[since].coefficientmap.insert(std::make_pair(paramName, param));
0137 }
0138 }
0139 }
0140 delete qHandle;
0141 }
0142 void lumi::NormDML::parseLumiCorrector(const std::string& correctorStr, std::vector<std::string>& correctorParams) {
0143 std::string cleancorrectorStr(correctorStr);
0144 boost::trim(cleancorrectorStr);
0145 boost::split(correctorParams, cleancorrectorStr, boost::is_any_of(":,"));
0146 }
0147 void lumi::NormDML::parseAfterglows(const std::string& afterglowStr, std::map<unsigned int, float>& afterglowmap) {
0148 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
0149 boost::char_separator<char> sep("[(,)] ");
0150 tokenizer tokens(afterglowStr, sep);
0151 unsigned int counter = 1;
0152 std::string thresholdStr;
0153 unsigned int threshold;
0154 for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
0155 if (counter % 2 == 0) {
0156 const std::string& valStr = *(tok_iter);
0157 float val = 0.;
0158 std::stringstream strStream(valStr);
0159 strStream >> val;
0160 afterglowmap.insert(std::make_pair(threshold, val));
0161 } else {
0162 thresholdStr = *(tok_iter);
0163 std::stringstream strStream(thresholdStr);
0164 strStream >> threshold;
0165 }
0166 ++counter;
0167 }
0168 }