Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:38

0001 #include <string>
0002 #include <vector>
0003 #include <map>
0004 #include <cmath>
0005 #include <stdexcept>
0006 #include <cstdio>
0007 #include <cstdlib>
0008 #include <iostream>
0009 #include "CoralBase/AttributeList.h"
0010 #include "CoralBase/Attribute.h"
0011 #include "CoralBase/AttributeSpecification.h"
0012 #include "CoralBase/Exception.h"
0013 #include "CoralBase/TimeStamp.h"
0014 #include "CoralBase/MessageStream.h"
0015 #include "CoralKernel/Context.h"
0016 #include "CoralKernel/IHandle.h"
0017 #include "CoralKernel/IProperty.h"
0018 #include "CoralKernel/IPropertyManager.h"
0019 #include "RelationalAccess/AccessMode.h"
0020 #include "RelationalAccess/ConnectionService.h"
0021 #include "RelationalAccess/ISessionProxy.h"
0022 #include "RelationalAccess/IConnectionServiceConfiguration.h"
0023 #include "RelationalAccess/ITransaction.h"
0024 #include "RelationalAccess/ITypeConverter.h"
0025 #include "RelationalAccess/IQuery.h"
0026 #include "RelationalAccess/ICursor.h"
0027 #include "RelationalAccess/ISchema.h"
0028 #include "RelationalAccess/ITable.h"
0029 #include <boost/regex.hpp>
0030 #include "RecoLuminosity/LumiProducer/interface/Utils.h"
0031 
0032 namespace lumitest {
0033   void stringSplit(const std::string& instr, char delim, std::vector<std::string>& results) {
0034     size_t cutAt = 0;
0035     std::string str = instr;
0036     while ((cutAt = str.find_first_of(delim)) != std::string::npos) {
0037       if (cutAt > 0) {
0038         results.push_back(str.substr(0, cutAt));
0039         str = str.substr(cutAt + 1);
0040       }
0041     }
0042     if (str.length() > 0) {
0043       results.push_back(str);
0044     }
0045   }
0046 
0047   void fillMap(const std::vector<std::string>& inVector, std::map<unsigned int, std::string>& result) {
0048     std::vector<std::string>::const_iterator it;
0049     std::vector<std::string>::const_iterator itBeg = inVector.begin();
0050     std::vector<std::string>::const_iterator itEnd = inVector.end();
0051     for (it = itBeg; it != itEnd; ++it) {
0052       //std::cout<<*it<<std::endl;
0053       std::string rundelimeterStr(it->begin(), it->end() - 1);
0054       std::string stateStr(it->end() - 1, it->end());
0055       //std::cout<<"rundelimeterStr "<<rundelimeterStr<<std::endl;
0056       float rundelimeter = 0.0;
0057       if (!lumi::from_string(rundelimeter, rundelimeterStr, std::dec)) {
0058         std::cout << "failed to convert string to float" << std::endl;
0059       }
0060       //std::cout<<"stateStr "<<stateStr<<std::endl;
0061       //
0062       //logic of rounding:
0063       //for states physics_declared T,F, use ceil function to round up then convert to unsigned int, because T,F will be set at the next LS boundary
0064       //for state paused P, use floor function to round down then convert to unsigned int, because we count the LS as paused as long as it contains pause (this logic could be changed)
0065       //
0066       if (stateStr == "P") {
0067         result.insert(std::make_pair((unsigned int)std::floor(rundelimeter), stateStr));
0068       } else if (stateStr == "T" || stateStr == "F") {
0069         result.insert(std::make_pair((unsigned int)std::ceil(rundelimeter), stateStr));
0070       } else {
0071         throw std::runtime_error("unknown LS state");
0072       }
0073     }
0074   }
0075 }  // namespace lumitest
0076 int main(int argc, char** argv) {
0077   unsigned int runnumber = 0;
0078   if (!(runnumber = ::atoi(argv[1]))) {
0079     std::cout << "must specify the run number" << std::endl;
0080     return 0;
0081   }
0082   const boost::regex physicsE("%PHYSICS_DECLARED&(true|false|N/A)&(true|false|N/A)%");
0083   const boost::regex nameE("^CMS.LVL0:RUNSECTION_DELIMITER_DCSLHCFLAGS_([0-9]+)");
0084   boost::match_results<std::string::const_iterator> what;
0085 
0086   //
0087   //query runinfo db
0088   //
0089   //select name,string_value from runsession_parameter where  runnumber=:runnumber and (name like 'CMS.LVL0:RUNSECTION_DELIMITER_LS_%' or name like 'CMS.LVL0:RUNSECTION_DELIMITER_DCSLHCFLAGS_%') order by time;
0090   //
0091   std::string serviceName("oracle://cms_omds_lb/CMS_RUNINFO");
0092   std::string authName("/afs/cern.ch/user/x/xiezhen/authentication.xml");
0093   std::string tabname("RUNSESSION_PARAMETER");
0094   try {
0095     coral::ConnectionService* conService = new coral::ConnectionService();
0096     coral::Context::instance().PropertyManager().property("AuthenticationFile")->set(authName);
0097     conService->configuration().setAuthenticationService("CORAL/Services/XMLAuthenticationService");
0098     conService->configuration().disablePoolAutomaticCleanUp();
0099     conService->configuration().setConnectionTimeOut(0);
0100     coral::MessageStream::setMsgVerbosity(coral::Error);
0101     coral::ISessionProxy* session = conService->connect(serviceName, coral::ReadOnly);
0102     coral::ITypeConverter& tpc = session->typeConverter();
0103 
0104     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0105     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0106     tpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0107 
0108     coral::ITransaction& transaction = session->transaction();
0109     transaction.start(true);  //true means readonly transaction
0110     coral::ISchema& schema = session->nominalSchema();
0111     if (!schema.existsTable(tabname)) {
0112       std::cout << "table " << tabname << " doesn't exist" << std::endl;
0113       return 0;
0114     }
0115     coral::IQuery* query = schema.tableHandle(tabname).newQuery();
0116     coral::AttributeList qoutput;
0117     qoutput.extend("NAME", typeid(std::string));
0118     qoutput.extend("STRING_VALUE", typeid(std::string));
0119     coral::AttributeList qcondition;
0120     qcondition.extend("runnumber", typeid(unsigned int));
0121     qcondition.extend("delimiterls", typeid(std::string));
0122     qcondition.extend("dcslhcflag", typeid(std::string));
0123     qcondition["runnumber"].data<unsigned int>() = runnumber;
0124     qcondition["delimiterls"].data<std::string>() = "CMS.LVL0:RUNSECTION_DELIMITER_LS_%";
0125     qcondition["dcslhcflag"].data<std::string>() = "CMS.LVL0:RUNSECTION_DELIMITER_DCSLHCFLAGS_%";
0126     query->addToOutputList("NAME");
0127     query->addToOutputList("STRING_VALUE");
0128     query->setCondition("RUNNUMBER =:runnumber AND (NAME like :delimiterls OR NAME like :dcslhcflag)", qcondition);
0129     query->addToOrderList("TIME");
0130     query->defineOutput(qoutput);
0131     coral::ICursor& cursor = query->execute();
0132     while (cursor.next()) {
0133       const coral::AttributeList& row = cursor.currentRow();
0134       //row.toOutputStream(std::cout)<<std::endl;
0135       std::string name = row["NAME"].data<std::string>();
0136       std::string value = row["STRING_VALUE"].data<std::string>();
0137       std::cout << "name: " << name << ", value: " << value << std::endl;
0138       boost::regex_match(name, what, nameE, boost::match_default);
0139       std::string statChar;
0140       if (what[0].matched) {
0141         if (value == "null") {
0142           statChar = "P";  //is a pause
0143         } else {
0144           boost::regex_search(value, what, physicsE, boost::match_default);
0145           if (what[0].matched) {
0146             std::string operatorBitValue = std::string(what[2].first, what[2].second);
0147             if (operatorBitValue == "true") {
0148               statChar = "T";
0149             } else {
0150               statChar = "F";
0151             }
0152           }
0153         }
0154       }
0155     }
0156     delete query;
0157     transaction.commit();
0158     delete session;
0159     delete conService;
0160   } catch (const std::exception& er) {
0161     std::cout << "caught exception " << er.what() << std::endl;
0162     throw er;
0163   }
0164   const boost::regex e("%*PHYSICS_DECLARED&(true)|(false)|(N/A)&(true)|(false)|(N/A)%");
0165 
0166   // float number as string for runsection delimiter,
0167   // "T" for true,"F" for false, "P" for pause
0168   //
0169   // the output of the parsing are 2 booleans per lumisection
0170   //
0171   // physicsDeclared
0172   // isPaused
0173   //
0174   // a lumisection can be considered for recorded luminosity only if
0175   // PhysicsDeclared && !isPaused
0176   //
0177   // source of this decision is documented here:
0178   // https://savannah.cern.ch/support/?112921
0179   //
0180   std::string LSstateInputstr("1.0T,19.9P,21.6F,23.54P");  //output of database query in one string format
0181   unsigned int totalLS = 40;  //suppose there are 40 cms ls, I want to assign T,F,P state to each of them
0182   std::vector<std::string> parseresult;
0183   lumitest::stringSplit(
0184       LSstateInputstr, ',', parseresult);  //split the input string into a vector of string pairs by ','
0185   std::map<unsigned int, std::string> delimiterMap;
0186   lumitest::fillMap(parseresult, delimiterMap);  //parse the vector into state boundary LS(key) to LS state(value) map
0187   //keys [1,19,22,23]
0188   for (unsigned int ls = 1; ls <= totalLS; ++ls) {  //loop over my LS comparing it to the state boundaries
0189     std::map<unsigned int, std::string>::const_iterator lsItUp;
0190     lsItUp = delimiterMap.upper_bound(ls);
0191     std::string r;
0192     if (lsItUp != delimiterMap.end()) {
0193       lsItUp = delimiterMap.upper_bound(ls);
0194       --lsItUp;
0195       r = (*lsItUp).second;
0196       //std::cout<<"LS "<<ls<<std::endl;
0197       //std::cout<<"boundary "<<(*lsItUp).first<<std::endl;
0198       //std::cout<<"state "<<r<<std::endl;
0199     } else {
0200       std::map<unsigned int, std::string>::reverse_iterator lsItLast = delimiterMap.rbegin();
0201       r = (*lsItLast).second;
0202       //std::cout<<"LS "<<ls<<std::endl;
0203       //std::cout<<"boundary "<<(*lsItLast).first<<std::endl;
0204       //std::cout<<"state "<<r<<std::endl;
0205     }
0206     std::cout << "LS : " << ls << " , state : " << r << std::endl;
0207   }
0208 }