Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:22

0001 #include "CondTools/RPC/interface/RPCLBLinkMapHandler.h"
0002 
0003 #include <cstdint>
0004 #include <fstream>
0005 #include <memory>
0006 #include <sstream>
0007 
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 
0012 #include "CondCore/CondDB/interface/Types.h"
0013 
0014 #include "RelationalAccess/ICursor.h"
0015 #include "RelationalAccess/IQuery.h"
0016 #include "RelationalAccess/IQueryDefinition.h"
0017 #include "RelationalAccess/ISchema.h"
0018 #include "RelationalAccess/ISessionProxy.h"
0019 #include "RelationalAccess/ITransaction.h"
0020 
0021 #include "CoralBase/Attribute.h"
0022 #include "CoralBase/AttributeList.h"
0023 
0024 #include "CondTools/RPC/interface/RPCLBLinkNameParser.h"
0025 
0026 RPCDetId RPCLBLinkMapHandler::getRPCDetId(
0027     int region, int disk_or_wheel, int layer, int sector, std::string subsector_string, std::string partition) {
0028   int station(0), ring(0), subsector(0), roll(0);
0029   // region well-defined
0030   if (!region) {  // barrel
0031     switch (layer) {
0032       case 1:
0033       case 2:
0034         station = 1;
0035         break;
0036       case 3:
0037       case 4:
0038         station = 2;
0039         layer -= 2;
0040         break;
0041       case 5:
0042         station = 3;
0043         layer = 1;
0044         break;
0045       case 6:
0046         station = 4;
0047         layer = 1;
0048         break;
0049     }
0050     ring = disk_or_wheel;
0051     // sector well-defined
0052     subsector = 1;
0053     if (subsector_string == "+" && (station == 3 || (station == 4 && (sector != 4 && sector != 9 && sector != 11))))
0054       subsector = 2;
0055     if (station == 4 && sector == 4) {
0056       if (subsector_string == "-")
0057         subsector = 2;
0058       else if (subsector_string == "+")
0059         subsector = 3;
0060       else if (subsector_string == "++")
0061         subsector = 4;
0062     }
0063   } else {  // endcap
0064     station = std::abs(disk_or_wheel);
0065     ring = layer;
0066     layer = 1;
0067     if (ring > 1 || station == 1) {
0068       subsector = (sector - 1) % 6 + 1;
0069       sector = (sector - 1) / 6 + 1;
0070     } else {
0071       subsector = (sector - 1) % 3 + 1;
0072       sector = (sector - 1) / 3 + 1;
0073     }
0074   }
0075   // roll
0076   if (partition == "Backward" || partition == "A")
0077     roll = 1;
0078   else if (partition == "Central" || partition == "B")
0079     roll = 2;
0080   else if (partition == "Forward" || partition == "C")
0081     roll = 3;
0082   else if (partition == "D")
0083     roll = 4;
0084   else
0085     throw cms::Exception("RPCLBLinkMapHandler") << "Unexpected partition name " << partition;
0086 
0087   return RPCDetId(region, ring, station, sector, layer, subsector, roll);
0088 }
0089 
0090 RPCLBLinkMapHandler::RPCLBLinkMapHandler(edm::ParameterSet const& config)
0091     : id_(config.getParameter<std::string>("identifier")),
0092       data_tag_(config.getParameter<std::string>("dataTag")),
0093       since_run_(config.getParameter<unsigned long long>("sinceRun")),
0094       txt_file_(config.getUntrackedParameter<std::string>("txtFile", "")),
0095       connect_(config.getParameter<std::string>("connect")) {
0096   edm::LogInfo("RPCDCCLinkMapHandler") << "Configuring Input Connection";
0097   connection_.setParameters(config.getParameter<edm::ParameterSet>("DBParameters"));
0098   connection_.configure();
0099 }
0100 
0101 RPCLBLinkMapHandler::~RPCLBLinkMapHandler() {}
0102 
0103 void RPCLBLinkMapHandler::getNewObjects() {
0104   edm::LogInfo("RPCLBLinkMapHandler") << "getNewObjects";
0105   cond::TagInfo_t const& tag_info = tagInfo();
0106   if (since_run_ < tag_info.lastInterval.since)
0107     throw cms::Exception("RPCLBLinkMapHandler") << "Refuse to create RPCLBLinkMap for run " << since_run_
0108                                                 << ", older than most recent tag" << tag_info.lastInterval.since;
0109 
0110   edm::LogInfo("RPCDCCLinkMapHandler") << "Opening read-only Input Session";
0111   auto input_session = connection_.createCoralSession(connect_, false);  // writeCapable
0112   edm::LogInfo("RPCDCCLinkMapHandler") << "Started Input Transaction";
0113   input_session->transaction().start(true);  // readOnly
0114 
0115   std::unique_ptr<coral::IQuery> query(input_session->schema("CMS_RPC_CONF").newQuery());
0116   query->addToTableList("BOARD");
0117   query->addToTableList("CHAMBERSTRIP");
0118   query->addToTableList("CHAMBERLOCATION");
0119   query->addToTableList("FEBLOCATION");
0120   query->addToTableList("FEBCONNECTOR");
0121   coral::IQueryDefinition& subquery_min_channel(query->defineSubQuery("MIN_CHANNEL"));
0122   query->addToTableList("MIN_CHANNEL");
0123   coral::IQueryDefinition& subquery_max_strip(query->defineSubQuery("MAX_STRIP"));
0124   query->addToTableList("MAX_STRIP");
0125 
0126   query->addToOutputList("BOARD.NAME", "LB_NAME");
0127   query->addToOutputList("FEBCONNECTOR.LINKBOARDINPUTNUM", "CONNECTOR");
0128   query->addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "FIRST_STRIP");
0129   query->addToOutputList("CAST(DECODE(SIGN(MAX_STRIP.STRIP - CHAMBERSTRIP.CHAMBERSTRIPNUMBER), 1, 1, -1) AS INTEGER)",
0130                          "SLOPE");
0131   query->addToOutputList("MIN_CHANNEL.CHANNELS", "CHANNELS");
0132   query->addToOutputList(
0133       "CAST(DECODE(CHAMBERLOCATION.BARRELORENDCAP, 'Barrel', 0, DECODE(SIGN(CHAMBERLOCATION.DISKORWHEEL), 1, 1, -1)) "
0134       "AS INTEGER)",
0135       "REGION");
0136   query->addToOutputList("CHAMBERLOCATION.DISKORWHEEL", "DISKORWHEEL");
0137   query->addToOutputList("CHAMBERLOCATION.LAYER", "LAYER");
0138   query->addToOutputList("CHAMBERLOCATION.SECTOR", "SECTOR");
0139   query->addToOutputList("CHAMBERLOCATION.SUBSECTOR", "SUBSECTOR");
0140   query->addToOutputList("FEBLOCATION.FEBLOCALETAPARTITION", "ETAPARTITION");
0141 
0142   subquery_min_channel.addToTableList("CHAMBERSTRIP");
0143   subquery_min_channel.addToOutputList("FC_FEBCONNECTORID");
0144   subquery_min_channel.addToOutputList("MIN(CABLECHANNELNUM)", "CHANNEL");
0145   subquery_min_channel.addToOutputList("CAST(SUM(POWER(2, CABLECHANNELNUM-1)) AS INTEGER)", "CHANNELS");
0146   subquery_min_channel.groupBy("FC_FEBCONNECTORID");
0147   coral::AttributeList subquery_min_channel_condition_data;
0148   subquery_min_channel.setCondition("CABLECHANNELNUM IS NOT NULL", subquery_min_channel_condition_data);
0149 
0150   subquery_max_strip.addToTableList("CHAMBERSTRIP");
0151   coral::IQueryDefinition& subquery_max_channel(subquery_max_strip.defineSubQuery("MAX_CHANNEL"));
0152   subquery_max_strip.addToTableList("MAX_CHANNEL");
0153   subquery_max_strip.addToOutputList("CHAMBERSTRIP.FC_FEBCONNECTORID", "FC_FEBCONNECTORID");
0154   subquery_max_strip.addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "STRIP");
0155   coral::AttributeList subquery_max_strip_condition_data;
0156   subquery_max_strip.setCondition(
0157       "CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_CHANNEL.FC_FEBCONNECTORID"
0158       " AND CHAMBERSTRIP.CABLECHANNELNUM=MAX_CHANNEL.CHANNEL",
0159       subquery_max_strip_condition_data);
0160 
0161   subquery_max_channel.addToTableList("CHAMBERSTRIP");
0162   subquery_max_channel.addToOutputList("FC_FEBCONNECTORID");
0163   subquery_max_channel.addToOutputList("MAX(CABLECHANNELNUM)", "CHANNEL");
0164   subquery_max_channel.groupBy("FC_FEBCONNECTORID");
0165   coral::AttributeList subquery_max_channel_condition_data;
0166   subquery_max_channel.setCondition("CABLECHANNELNUM IS NOT NULL", subquery_max_channel_condition_data);
0167 
0168   coral::AttributeList query_condition_data;
0169   query->setCondition(
0170       "CHAMBERSTRIP.FC_FEBCONNECTORID=MIN_CHANNEL.FC_FEBCONNECTORID"
0171       " AND CHAMBERSTRIP.CABLECHANNELNUM=MIN_CHANNEL.CHANNEL"
0172       " AND CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_STRIP.FC_FEBCONNECTORID"
0173       " AND CHAMBERSTRIP.FC_FEBCONNECTORID=FEBCONNECTOR.FEBCONNECTORID"
0174       " AND FEBCONNECTOR.FL_FEBLOCATIONID=FEBLOCATION.FEBLOCATIONID"
0175       " AND BOARD.BOARDID=FEBLOCATION.LB_LINKBOARDID"
0176       " AND CHAMBERLOCATION.CHAMBERLOCATIONID=FEBLOCATION.CL_CHAMBERLOCATIONID",
0177       query_condition_data);
0178 
0179   std::string lb_name("");
0180   int first_strip(0), slope(1);
0181   std::uint16_t channels(0x0);
0182 
0183   std::unique_ptr<RPCLBLinkMap> lb_link_map_object(new RPCLBLinkMap());
0184   RPCLBLinkMap::map_type& lb_link_map = lb_link_map_object->getMap();
0185   RPCLBLink lb_link;
0186   RPCDetId det_id;
0187   std::string subsector;
0188 
0189   edm::LogInfo("RPCLBLinkMapHandler") << "Execute query";
0190   coral::ICursor& cursor(query->execute());
0191   while (cursor.next()) {
0192     coral::AttributeList const& row(cursor.currentRow());
0193 
0194     // RPCLBLink
0195     lb_name = row["LB_NAME"].data<std::string>();
0196     RPCLBLinkNameParser::parse(lb_name, lb_link);
0197     if (lb_name != lb_link.getName())
0198       edm::LogWarning("RPCLBLinkMapHandler") << "Mismatch LinkBoard Name: " << lb_name << " vs " << lb_link;
0199     lb_link.setConnector(row["CONNECTOR"].data<short>() - 1);  // 1-6 to 0-5
0200 
0201     // RPCDetId
0202     if (row["SUBSECTOR"].isNull())
0203       subsector = "";
0204     else
0205       subsector = row["SUBSECTOR"].data<std::string>();
0206     det_id = getRPCDetId(row["REGION"].data<long long>(),
0207                          row["DISKORWHEEL"].data<short>(),
0208                          row["LAYER"].data<short>(),
0209                          row["SECTOR"].data<short>(),
0210                          subsector,
0211                          row["ETAPARTITION"].data<std::string>());
0212 
0213     // RPCFebConnector
0214     first_strip = row["FIRST_STRIP"].data<int>();
0215     slope = row["SLOPE"].data<long long>();
0216     channels = (std::uint16_t)(row["CHANNELS"].data<long long>());
0217 
0218     lb_link_map.insert(
0219         std::pair<RPCLBLink, RPCFebConnector>(lb_link, RPCFebConnector(det_id, first_strip, slope, channels)));
0220   }
0221   cursor.close();
0222 
0223   input_session->transaction().commit();
0224 
0225   if (!txt_file_.empty()) {
0226     edm::LogInfo("RPCLBLinkMapHandler") << "Fill txtFile";
0227     std::ofstream ofstream(txt_file_);
0228     for (auto const& link_connector : lb_link_map) {
0229       ofstream << link_connector.first << ": " << link_connector.second << std::endl;
0230     }
0231   }
0232 
0233   edm::LogInfo("RPCLBLinkMapHandler") << "Add to transfer list";
0234   m_to_transfer.push_back(std::make_pair(lb_link_map_object.release(), since_run_));
0235 }
0236 
0237 std::string RPCLBLinkMapHandler::id() const { return id_; }