CMS 3D CMS Logo

RPCLBLinkMapHandler.cc
Go to the documentation of this file.
2 
3 #include <cstdint>
4 #include <fstream>
5 #include <memory>
6 #include <sstream>
7 
11 
13 
14 #include "RelationalAccess/ICursor.h"
15 #include "RelationalAccess/IQuery.h"
16 #include "RelationalAccess/IQueryDefinition.h"
17 #include "RelationalAccess/ISchema.h"
18 #include "RelationalAccess/ISessionProxy.h"
19 #include "RelationalAccess/ITransaction.h"
20 
21 #include "CoralBase/Attribute.h"
22 #include "CoralBase/AttributeList.h"
23 
25 
27  int region, int disk_or_wheel, int layer, int sector, std::string subsector_string, std::string partition) {
28  int station(0), ring(0), subsector(0), roll(0);
29  // region well-defined
30  if (!region) { // barrel
31  switch (layer) {
32  case 1:
33  case 2:
34  station = 1;
35  break;
36  case 3:
37  case 4:
38  station = 2;
39  layer -= 2;
40  break;
41  case 5:
42  station = 3;
43  layer = 1;
44  break;
45  case 6:
46  station = 4;
47  layer = 1;
48  break;
49  }
50  ring = disk_or_wheel;
51  // sector well-defined
52  subsector = 1;
53  if (subsector_string == "+" && (station == 3 || (station == 4 && (sector != 4 && sector != 9 && sector != 11))))
54  subsector = 2;
55  if (station == 4 && sector == 4) {
56  if (subsector_string == "-")
57  subsector = 2;
58  else if (subsector_string == "+")
59  subsector = 3;
60  else if (subsector_string == "++")
61  subsector = 4;
62  }
63  } else { // endcap
64  station = std::abs(disk_or_wheel);
65  ring = layer;
66  layer = 1;
67  if (ring > 1 || station == 1) {
68  subsector = (sector - 1) % 6 + 1;
69  sector = (sector - 1) / 6 + 1;
70  } else {
71  subsector = (sector - 1) % 3 + 1;
72  sector = (sector - 1) / 3 + 1;
73  }
74  }
75  // roll
76  if (partition == "Backward" || partition == "A")
77  roll = 1;
78  else if (partition == "Central" || partition == "B")
79  roll = 2;
80  else if (partition == "Forward" || partition == "C")
81  roll = 3;
82  else if (partition == "D")
83  roll = 4;
84  else
85  throw cms::Exception("RPCLBLinkMapHandler") << "Unexpected partition name " << partition;
86 
88 }
89 
91  : id_(config.getParameter<std::string>("identifier")),
92  data_tag_(config.getParameter<std::string>("dataTag")),
93  since_run_(config.getParameter<unsigned long long>("sinceRun")),
94  txt_file_(config.getUntrackedParameter<std::string>("txtFile", "")),
95  connect_(config.getParameter<std::string>("connect")) {
96  edm::LogInfo("RPCDCCLinkMapHandler") << "Configuring Input Connection";
97  connection_.setParameters(config.getParameter<edm::ParameterSet>("DBParameters"));
99 }
100 
102 
104  edm::LogInfo("RPCLBLinkMapHandler") << "getNewObjects";
105  cond::TagInfo_t const& tag_info = tagInfo();
106  if (since_run_ < tag_info.lastInterval.since)
107  throw cms::Exception("RPCLBLinkMapHandler") << "Refuse to create RPCLBLinkMap for run " << since_run_
108  << ", older than most recent tag" << tag_info.lastInterval.since;
109 
110  edm::LogInfo("RPCDCCLinkMapHandler") << "Opening read-only Input Session";
111  auto input_session = connection_.createCoralSession(connect_, false); // writeCapable
112  edm::LogInfo("RPCDCCLinkMapHandler") << "Started Input Transaction";
113  input_session->transaction().start(true); // readOnly
114 
115  std::unique_ptr<coral::IQuery> query(input_session->schema("CMS_RPC_CONF").newQuery());
116  query->addToTableList("BOARD");
117  query->addToTableList("CHAMBERSTRIP");
118  query->addToTableList("CHAMBERLOCATION");
119  query->addToTableList("FEBLOCATION");
120  query->addToTableList("FEBCONNECTOR");
121  coral::IQueryDefinition& subquery_min_channel(query->defineSubQuery("MIN_CHANNEL"));
122  query->addToTableList("MIN_CHANNEL");
123  coral::IQueryDefinition& subquery_max_strip(query->defineSubQuery("MAX_STRIP"));
124  query->addToTableList("MAX_STRIP");
125 
126  query->addToOutputList("BOARD.NAME", "LB_NAME");
127  query->addToOutputList("FEBCONNECTOR.LINKBOARDINPUTNUM", "CONNECTOR");
128  query->addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "FIRST_STRIP");
129  query->addToOutputList("CAST(DECODE(SIGN(MAX_STRIP.STRIP - CHAMBERSTRIP.CHAMBERSTRIPNUMBER), 1, 1, -1) AS INTEGER)",
130  "SLOPE");
131  query->addToOutputList("MIN_CHANNEL.CHANNELS", "CHANNELS");
132  query->addToOutputList(
133  "CAST(DECODE(CHAMBERLOCATION.BARRELORENDCAP, 'Barrel', 0, DECODE(SIGN(CHAMBERLOCATION.DISKORWHEEL), 1, 1, -1)) "
134  "AS INTEGER)",
135  "REGION");
136  query->addToOutputList("CHAMBERLOCATION.DISKORWHEEL", "DISKORWHEEL");
137  query->addToOutputList("CHAMBERLOCATION.LAYER", "LAYER");
138  query->addToOutputList("CHAMBERLOCATION.SECTOR", "SECTOR");
139  query->addToOutputList("CHAMBERLOCATION.SUBSECTOR", "SUBSECTOR");
140  query->addToOutputList("FEBLOCATION.FEBLOCALETAPARTITION", "ETAPARTITION");
141 
142  subquery_min_channel.addToTableList("CHAMBERSTRIP");
143  subquery_min_channel.addToOutputList("FC_FEBCONNECTORID");
144  subquery_min_channel.addToOutputList("MIN(CABLECHANNELNUM)", "CHANNEL");
145  subquery_min_channel.addToOutputList("CAST(SUM(POWER(2, CABLECHANNELNUM-1)) AS INTEGER)", "CHANNELS");
146  subquery_min_channel.groupBy("FC_FEBCONNECTORID");
147  coral::AttributeList subquery_min_channel_condition_data;
148  subquery_min_channel.setCondition("CABLECHANNELNUM IS NOT NULL", subquery_min_channel_condition_data);
149 
150  subquery_max_strip.addToTableList("CHAMBERSTRIP");
151  coral::IQueryDefinition& subquery_max_channel(subquery_max_strip.defineSubQuery("MAX_CHANNEL"));
152  subquery_max_strip.addToTableList("MAX_CHANNEL");
153  subquery_max_strip.addToOutputList("CHAMBERSTRIP.FC_FEBCONNECTORID", "FC_FEBCONNECTORID");
154  subquery_max_strip.addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "STRIP");
155  coral::AttributeList subquery_max_strip_condition_data;
156  subquery_max_strip.setCondition(
157  "CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_CHANNEL.FC_FEBCONNECTORID"
158  " AND CHAMBERSTRIP.CABLECHANNELNUM=MAX_CHANNEL.CHANNEL",
159  subquery_max_strip_condition_data);
160 
161  subquery_max_channel.addToTableList("CHAMBERSTRIP");
162  subquery_max_channel.addToOutputList("FC_FEBCONNECTORID");
163  subquery_max_channel.addToOutputList("MAX(CABLECHANNELNUM)", "CHANNEL");
164  subquery_max_channel.groupBy("FC_FEBCONNECTORID");
165  coral::AttributeList subquery_max_channel_condition_data;
166  subquery_max_channel.setCondition("CABLECHANNELNUM IS NOT NULL", subquery_max_channel_condition_data);
167 
168  coral::AttributeList query_condition_data;
169  query->setCondition(
170  "CHAMBERSTRIP.FC_FEBCONNECTORID=MIN_CHANNEL.FC_FEBCONNECTORID"
171  " AND CHAMBERSTRIP.CABLECHANNELNUM=MIN_CHANNEL.CHANNEL"
172  " AND CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_STRIP.FC_FEBCONNECTORID"
173  " AND CHAMBERSTRIP.FC_FEBCONNECTORID=FEBCONNECTOR.FEBCONNECTORID"
174  " AND FEBCONNECTOR.FL_FEBLOCATIONID=FEBLOCATION.FEBLOCATIONID"
175  " AND BOARD.BOARDID=FEBLOCATION.LB_LINKBOARDID"
176  " AND CHAMBERLOCATION.CHAMBERLOCATIONID=FEBLOCATION.CL_CHAMBERLOCATIONID",
177  query_condition_data);
178 
179  std::string lb_name("");
180  int first_strip(0), slope(1);
181  std::uint16_t channels(0x0);
182 
183  std::unique_ptr<RPCLBLinkMap> lb_link_map_object(new RPCLBLinkMap());
184  RPCLBLinkMap::map_type& lb_link_map = lb_link_map_object->getMap();
185  RPCLBLink lb_link;
186  RPCDetId det_id;
188 
189  edm::LogInfo("RPCLBLinkMapHandler") << "Execute query";
190  coral::ICursor& cursor(query->execute());
191  while (cursor.next()) {
192  coral::AttributeList const& row(cursor.currentRow());
193 
194  // RPCLBLink
195  lb_name = row["LB_NAME"].data<std::string>();
196  RPCLBLinkNameParser::parse(lb_name, lb_link);
197  if (lb_name != lb_link.getName())
198  edm::LogWarning("RPCLBLinkMapHandler") << "Mismatch LinkBoard Name: " << lb_name << " vs " << lb_link;
199  lb_link.setConnector(row["CONNECTOR"].data<short>() - 1); // 1-6 to 0-5
200 
201  // RPCDetId
202  if (row["SUBSECTOR"].isNull())
203  subsector = "";
204  else
205  subsector = row["SUBSECTOR"].data<std::string>();
206  det_id = getRPCDetId(row["REGION"].data<long long>(),
207  row["DISKORWHEEL"].data<short>(),
208  row["LAYER"].data<short>(),
209  row["SECTOR"].data<short>(),
210  subsector,
211  row["ETAPARTITION"].data<std::string>());
212 
213  // RPCFebConnector
214  first_strip = row["FIRST_STRIP"].data<int>();
215  slope = row["SLOPE"].data<long long>();
216  channels = (std::uint16_t)(row["CHANNELS"].data<long long>());
217 
218  lb_link_map.insert(
219  std::pair<RPCLBLink, RPCFebConnector>(lb_link, RPCFebConnector(det_id, first_strip, slope, channels)));
220  }
221  cursor.close();
222 
223  input_session->transaction().commit();
224 
225  if (!txt_file_.empty()) {
226  edm::LogInfo("RPCLBLinkMapHandler") << "Fill txtFile";
227  std::ofstream ofstream(txt_file_);
228  for (auto const& link_connector : lb_link_map) {
229  ofstream << link_connector.first << ": " << link_connector.second << std::endl;
230  }
231  }
232 
233  edm::LogInfo("RPCLBLinkMapHandler") << "Add to transfer list";
234  m_to_transfer.push_back(std::make_pair(lb_link_map_object.release(), since_run_));
235 }
236 
void getNewObjects() override
Iov_t lastInterval
Definition: Types.h:73
static const double slope[3]
Time_t since
Definition: Types.h:53
static RPCDetId getRPCDetId(int region, int disk_or_wheel, int layer, int sector, std::string subsector_string, std::string partition)
Definition: config.py:1
void setParameters(const edm::ParameterSet &connectionPset)
Definition: query.py:1
cond::TagInfo_t const & tagInfo() const
cond::persistency::ConnectionPool connection_
std::vector< std::pair< RPCLBLinkMap *, Time_t > > m_to_transfer
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Log< level::Info, false > LogInfo
RPCLBLinkMapHandler(edm::ParameterSet const &config)
std::string id() const override
std::map< RPCLBLink, RPCFebConnector > map_type
Definition: RPCLBLinkMap.h:13
static void parse(std::string const &name, RPCLBLink &lb_link)
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)