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