Go to the documentation of this file.00001 #include "RecoLuminosity/LumiProducer/interface/NormDML.h"
00002 #include "RelationalAccess/ISchema.h"
00003 #include "RelationalAccess/IQuery.h"
00004 #include "RelationalAccess/ICursor.h"
00005 #include "CoralBase/AttributeList.h"
00006 #include "CoralBase/Attribute.h"
00007 #include "CoralBase/AttributeSpecification.h"
00008 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00009 #include <algorithm>
00010 #include <map>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <boost/algorithm/string.hpp>
00014 #include <boost/tokenizer.hpp>
00015 lumi::NormDML::NormDML(){
00016 }
00017 unsigned long long
00018 lumi::NormDML::normIdByName(const coral::ISchema& schema,const std::string& normtagname){
00020 unsigned long long result=0;
00021 std::vector<unsigned long long> luminormids;
00022 coral::IQuery* qHandle=schema.newQuery();
00023 qHandle->addToTableList( lumi::LumiNames::luminormv2TableName() );
00024 qHandle->addToOutputList("DATA_ID");
00025 if(!normtagname.empty()){
00026 std::string qConditionStr("ENTRY_NAME=:normtagname");
00027 coral::AttributeList qCondition;
00028 qCondition.extend("normtagname",typeid(std::string));
00029 qCondition["normtagname"].data<std::string>()=normtagname;
00030 qHandle->setCondition(qConditionStr,qCondition);
00031 }
00032 coral::AttributeList qResult;
00033 qResult.extend("DATA_ID",typeid(unsigned long long));
00034 qHandle->defineOutput(qResult);
00035 coral::ICursor& cursor=qHandle->execute();
00036 while( cursor.next() ){
00037 const coral::AttributeList& row=cursor.currentRow();
00038 luminormids.push_back(row["DATA_ID"].data<unsigned long long>());
00039 }
00040 delete qHandle;
00041 std::vector<unsigned long long>::iterator resultIt;
00042 for(resultIt=luminormids.begin();resultIt!=luminormids.end();++resultIt){
00043 if( (*resultIt)>result){
00044 result=*resultIt;
00045 }
00046 }
00047 return result;
00048 }
00049 void
00050 lumi::NormDML::normIdByType(const coral::ISchema& schema,std::map<std::string,unsigned long long>& resultMap,lumi::NormDML::LumiType lumitype,bool defaultonly){
00052 coral::IQuery* qHandle=schema.newQuery();
00053 qHandle->addToTableList( lumi::LumiNames::luminormv2TableName() );
00054 qHandle->addToOutputList("DATA_ID");
00055 qHandle->addToOutputList("ENTRY_NAME");
00056 coral::AttributeList qCondition;
00057 std::string qConditionStr("LUMITYPE=:lumitype");
00058 qCondition.extend("lumitype",typeid(std::string));
00059 std::string lumitypeStr("HF");
00060 if(lumitype!=lumi::NormDML::HF){
00061 lumitypeStr="PIXEL";
00062 }
00063 qCondition["lumitype"].data<std::string>()=lumitypeStr;
00064 if(defaultonly){
00065 qConditionStr+=" AND ISTYPEDEFAULT=:istypedefault";
00066 qCondition.extend("istypedefault",typeid(unsigned int));
00067 qCondition["istypedefault"].data<unsigned int>()=1;
00068 }
00069 qHandle->setCondition(qConditionStr,qCondition);
00070 coral::AttributeList qResult;
00071 qResult.extend("DATA_ID",typeid(unsigned long long));
00072 qResult.extend("ENTRY_NAME",typeid(std::string));
00073 qHandle->defineOutput(qResult);
00074 try{
00075 coral::ICursor& cursor=qHandle->execute();
00076 while( cursor.next() ){
00077 const coral::AttributeList& row=cursor.currentRow();
00078 const std::string normname=row["ENTRY_NAME"].data<std::string>();
00079 unsigned long long normid=row["DATA_ID"].data<unsigned long long>();
00080 if(resultMap.find(normname)==resultMap.end()){
00081 resultMap.insert(std::make_pair(normname,normid));
00082 }else{
00083 if(resultMap[normname]<normid){
00084 resultMap.insert(std::make_pair(normname,normid));
00085 }
00086 }
00087 }
00088 }catch(const coral::Exception& er){
00089 std::cout<<"database error in NormDML::normIdByType "<<er.what()<<std::endl;
00090 delete qHandle;
00091 throw er;
00092 }catch(...){
00093 throw;
00094 }
00095 delete qHandle;
00096 }
00097 void
00098 lumi::NormDML::normById(const coral::ISchema&schema,
00099 unsigned long long normid,
00100 std::map< unsigned int,lumi::NormDML::normData >& result){
00102 coral::IQuery* qHandle=schema.newQuery();
00103 qHandle->addToTableList( lumi::LumiNames::luminormv2dataTableName() );
00104 std::string qConditionStr("DATA_ID=:normid ");
00105 coral::AttributeList qCondition;
00106 qCondition.extend("normid",typeid(unsigned long long));
00107 qCondition["normid"].data<unsigned long long>()=normid;
00108 qHandle->setCondition(qConditionStr,qCondition);
00109 coral::AttributeList qResult;
00110 coral::ICursor& cursor=qHandle->execute();
00111 while( cursor.next() ){
00112 const coral::AttributeList& row=cursor.currentRow();
00113 unsigned int since=row["SINCE"].data<unsigned int>();
00114 if(result.find(since)==result.end()){
00115 lumi::NormDML::normData thisnorm;
00116 result.insert(std::make_pair(since,thisnorm));
00117 }
00118 const std::string correctorStr=row["CORRECTOR"].data<std::string>();
00119 if(!row["AMODETAG"].isNull()){
00120 result[since].amodetag=row["AMODETAG"].data<std::string>();
00121 }
00122 if(!row["NOMINALEGEV"].isNull()){
00123 result[since].beamegev=row["NOMINALEGEV"].data<unsigned int>();
00124 }
00125
00126 std::vector<std::string> correctorParams;
00127 parseLumiCorrector(correctorStr,correctorParams);
00128 result[since].corrfunc=*(correctorParams.begin());
00129 for(std::vector<std::string>::iterator corrIt=correctorParams.begin()+1;
00130 corrIt!=correctorParams.end();corrIt++){
00131 std::string paramName=boost::to_upper_copy(*corrIt);
00132 if(paramName==std::string("AFTERGLOW")){
00133 const std::string afterglowStr=row["AFTERGLOW"].data<std::string>();
00134 parseAfterglows(afterglowStr,result[since].afterglows);
00135 }else{
00136 float param=row[paramName].data<float>();
00137 result[since].coefficientmap.insert(std::make_pair(paramName,param));
00138 }
00139 }
00140 }
00141 delete qHandle;
00142 }
00143 void
00144 lumi::NormDML::parseLumiCorrector(const std::string& correctorStr,
00145 std::vector<std::string>& correctorParams){
00146 std::string cleancorrectorStr(correctorStr);
00147 boost::trim(cleancorrectorStr);
00148 boost::split(correctorParams,cleancorrectorStr,boost::is_any_of(":,"));
00149 }
00150 void
00151 lumi::NormDML::parseAfterglows(const std::string& afterglowStr,std::map<unsigned int,float>& afterglowmap){
00152 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00153 boost::char_separator<char> sep("[(,)] ");
00154 tokenizer tokens(afterglowStr,sep);
00155 unsigned int counter=1;
00156 std::string thresholdStr;
00157 unsigned int threshold;
00158 for(tokenizer::iterator tok_iter=tokens.begin();tok_iter != tokens.end(); ++tok_iter){
00159 if(counter%2==0){
00160 std::string valStr=*(tok_iter);
00161 float val=0.;
00162 std::stringstream strStream(valStr);
00163 strStream>>val;
00164 afterglowmap.insert(std::make_pair(threshold,val));
00165 }else{
00166 thresholdStr=*(tok_iter);
00167 std::stringstream strStream(thresholdStr);
00168 strStream>>threshold;
00169 }
00170 ++counter;
00171 }
00172 }