CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RPCLBLinkMapHandler.cc
Go to the documentation of this file.
2 
3 #include <fstream>
4 #include <memory>
5 #include <sstream>
6 
10 
12 
13 #include "RelationalAccess/ICursor.h"
14 #include "RelationalAccess/IQuery.h"
15 #include "RelationalAccess/IQueryDefinition.h"
16 #include "RelationalAccess/ISchema.h"
17 #include "RelationalAccess/ISessionProxy.h"
18 #include "RelationalAccess/ITransaction.h"
19 
20 #include "CoralBase/Attribute.h"
21 #include "CoralBase/AttributeList.h"
22 
24 
25 RPCDetId RPCLBLinkMapHandler::getRPCDetId(int _region, int _disk_or_wheel, int _layer, int _sector
26  , std::string _subsector_string, std::string _partition)
27 {
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: _station = 1; break;
34  case 3:
35  case 4: _station = 2; _layer -= 2; break;
36  case 5: _station = 3; _layer = 1; break;
37  case 6: _station = 4; _layer = 1; break;
38  }
39  _ring = _disk_or_wheel;
40  // sector well-defined
41  _subsector = 1;
42  if (_subsector_string == "+"
43  && (_station == 3
44  || (_station==4 && (_sector != 4 && _sector != 9 && _sector != 11)))
45  )
46  _subsector = 2;
47  if (_station == 4 && _sector == 4) {
48  if (_subsector_string == "-")
49  _subsector=2;
50  else if (_subsector_string == "+")
51  _subsector=3;
52  else if (_subsector_string == "++")
53  _subsector=4;
54  }
55  } else { // endcap
56  _station = std::abs(_disk_or_wheel);
57  _ring = _layer;
58  _layer = 1;
59  if (_ring > 1 || _station == 1) {
60  _subsector = (_sector - 1) % 6 + 1;
61  _sector = (_sector - 1) / 6 + 1;
62  } else {
63  _subsector = (_sector - 1) % 3 + 1;
64  _sector = (_sector - 1) / 3 + 1;
65  }
66  }
67  // roll
68  if (_partition == "Backward" || _partition == "A")
69  _roll = 1;
70  else if (_partition == "Central" || _partition == "B")
71  _roll = 2;
72  else if (_partition == "Forward" || _partition == "C")
73  _roll = 3;
74  else if (_partition == "D")
75  _roll = 4;
76  else
77  throw cms::Exception("RPCLBLinkMapHandler") << "Unexpected partition name " << _partition;
78 
79  return RPCDetId(_region, _ring, _station, _sector, _layer, _subsector, _roll);
80 }
81 
83  : id_(_config.getParameter<std::string>("identifier"))
84  , data_tag_(_config.getParameter<std::string>("dataTag"))
85  , since_run_(_config.getParameter<unsigned long long>("sinceRun"))
86  , txt_file_(_config.getUntrackedParameter<std::string>("txtFile", ""))
87  , connect_(_config.getParameter<std::string>("connect"))
88 {
89  edm::LogInfo("RPCDCCLinkMapHandler") << "Configuring Input Connection";
90  connection_.setParameters(_config.getParameter<edm::ParameterSet>("DBParameters"));
92 }
93 
95 {}
96 
98 {
99  edm::LogInfo("RPCLBLinkMapHandler") << "getNewObjects";
100  cond::TagInfo const & _tag_info = tagInfo();
101  if (since_run_ < _tag_info.lastInterval.first)
102  throw cms::Exception("RPCLBLinkMapHandler") << "Refuse to create RPCLBLinkMap for run " << since_run_
103  << ", older than most recent tag" << _tag_info.lastInterval.first;
104 
105  edm::LogInfo("RPCDCCLinkMapHandler") << "Opening read-only Input Session";
106  auto _input_session = connection_.createCoralSession(connect_, false); // writeCapable
107  edm::LogInfo("RPCDCCLinkMapHandler") << "Started Input Transaction";
108  _input_session->transaction().start(true); // readOnly
109 
110  std::auto_ptr<coral::IQuery> _query(_input_session->schema("CMS_RPC_CONF").newQuery());
111  _query->addToTableList("BOARD");
112  _query->addToTableList("CHAMBERSTRIP");
113  _query->addToTableList("CHAMBERLOCATION");
114  _query->addToTableList("FEBLOCATION");
115  _query->addToTableList("FEBCONNECTOR");
116  coral::IQueryDefinition & _subquery_min_channel(_query->defineSubQuery("MIN_CHANNEL"));
117  _query->addToTableList("MIN_CHANNEL");
118  coral::IQueryDefinition & _subquery_max_strip(_query->defineSubQuery("MAX_STRIP"));
119  _query->addToTableList("MAX_STRIP");
120 
121  _query->addToOutputList("BOARD.NAME", "LB_NAME");
122  _query->addToOutputList("FEBCONNECTOR.LINKBOARDINPUTNUM", "CONNECTOR");
123  _query->addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "FIRST_STRIP");
124  _query->addToOutputList("CAST(DECODE(SIGN(MAX_STRIP.STRIP - CHAMBERSTRIP.CHAMBERSTRIPNUMBER), 1, 1, -1) AS INTEGER)", "SLOPE");
125  _query->addToOutputList("MIN_CHANNEL.CHANNELS", "CHANNELS");
126  _query->addToOutputList("CAST(DECODE(CHAMBERLOCATION.BARRELORENDCAP, 'Barrel', 0, DECODE(SIGN(CHAMBERLOCATION.DISKORWHEEL), 1, 1, -1)) AS INTEGER)", "REGION");
127  _query->addToOutputList("CHAMBERLOCATION.DISKORWHEEL", "DISKORWHEEL");
128  _query->addToOutputList("CHAMBERLOCATION.LAYER", "LAYER");
129  _query->addToOutputList("CHAMBERLOCATION.SECTOR", "SECTOR");
130  _query->addToOutputList("CHAMBERLOCATION.SUBSECTOR", "SUBSECTOR");
131  _query->addToOutputList("FEBLOCATION.FEBLOCALETAPARTITION", "ETAPARTITION");
132 
133  _subquery_min_channel.addToTableList("CHAMBERSTRIP");
134  _subquery_min_channel.addToOutputList("FC_FEBCONNECTORID");
135  _subquery_min_channel.addToOutputList("MIN(CABLECHANNELNUM)", "CHANNEL");
136  _subquery_min_channel.addToOutputList("CAST(SUM(POWER(2, CABLECHANNELNUM-1)) AS INTEGER)", "CHANNELS");
137  _subquery_min_channel.groupBy("FC_FEBCONNECTORID");
138  coral::AttributeList _subquery_min_channel_condition_data;
139  _subquery_min_channel.setCondition("CABLECHANNELNUM IS NOT NULL"
140  , _subquery_min_channel_condition_data);
141 
142  _subquery_max_strip.addToTableList("CHAMBERSTRIP");
143  coral::IQueryDefinition & _subquery_max_channel(_subquery_max_strip.defineSubQuery("MAX_CHANNEL"));
144  _subquery_max_strip.addToTableList("MAX_CHANNEL");
145  _subquery_max_strip.addToOutputList("CHAMBERSTRIP.FC_FEBCONNECTORID", "FC_FEBCONNECTORID");
146  _subquery_max_strip.addToOutputList("CHAMBERSTRIP.CHAMBERSTRIPNUMBER", "STRIP");
147  coral::AttributeList _subquery_max_strip_condition_data;
148  _subquery_max_strip.setCondition("CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_CHANNEL.FC_FEBCONNECTORID"
149  " AND CHAMBERSTRIP.CABLECHANNELNUM=MAX_CHANNEL.CHANNEL"
150  , _subquery_max_strip_condition_data);
151 
152  _subquery_max_channel.addToTableList("CHAMBERSTRIP");
153  _subquery_max_channel.addToOutputList("FC_FEBCONNECTORID");
154  _subquery_max_channel.addToOutputList("MAX(CABLECHANNELNUM)", "CHANNEL");
155  _subquery_max_channel.groupBy("FC_FEBCONNECTORID");
156  coral::AttributeList _subquery_max_channel_condition_data;
157  _subquery_max_channel.setCondition("CABLECHANNELNUM IS NOT NULL"
158  , _subquery_max_channel_condition_data);
159 
160  coral::AttributeList _query_condition_data;
161  _query->setCondition("CHAMBERSTRIP.FC_FEBCONNECTORID=MIN_CHANNEL.FC_FEBCONNECTORID"
162  " AND CHAMBERSTRIP.CABLECHANNELNUM=MIN_CHANNEL.CHANNEL"
163  " AND CHAMBERSTRIP.FC_FEBCONNECTORID=MAX_STRIP.FC_FEBCONNECTORID"
164  " AND CHAMBERSTRIP.FC_FEBCONNECTORID=FEBCONNECTOR.FEBCONNECTORID"
165  " AND FEBCONNECTOR.FL_FEBLOCATIONID=FEBLOCATION.FEBLOCATIONID"
166  " AND BOARD.BOARDID=FEBLOCATION.LB_LINKBOARDID"
167  " AND CHAMBERLOCATION.CHAMBERLOCATIONID=FEBLOCATION.CL_CHAMBERLOCATIONID"
168  , _query_condition_data);
169 
170  std::string _lb_name("");
171  int _first_strip(0), _slope(1);
172  ::uint16_t _channels(0x0);
173 
174  RPCLBLinkMap * _lb_link_map_object = new RPCLBLinkMap();
175  RPCLBLinkMap::map_type & _lb_link_map
176  = _lb_link_map_object->getMap();
177  RPCLBLink _lb_link;
178  RPCDetId _det_id;
179  std::string _subsector;
180 
181  edm::LogInfo("RPCLBLinkMapHandler") << "Execute query";
182  coral::ICursor & _cursor(_query->execute());
183  while (_cursor.next()) {
184  coral::AttributeList const & _row(_cursor.currentRow());
185 
186  // RPCLBLink
187  _lb_name = _row["LB_NAME"].data<std::string>();
188  RPCLBLinkNameParser::parse(_lb_name, _lb_link);
189  if (_lb_name != _lb_link.getName())
190  edm::LogWarning("RPCLBLinkMapHandler") << "Mismatch LinkBoard Name: " << _lb_name << " vs " << _lb_link;
191  _lb_link.setConnector(_row["CONNECTOR"].data<short>() - 1); // 1-6 to 0-5
192 
193  // RPCDetId
194  if (_row["SUBSECTOR"].isNull())
195  _subsector = "";
196  else
197  _subsector = _row["SUBSECTOR"].data<std::string>();
198  _det_id = getRPCDetId(_row["REGION"].data<long long>()
199  , _row["DISKORWHEEL"].data<short>()
200  , _row["LAYER"].data<short>()
201  , _row["SECTOR"].data<short>()
202  , _subsector
203  , _row["ETAPARTITION"].data<std::string>());
204 
205  // RPCFebConnector
206  _first_strip = _row["FIRST_STRIP"].data<int>();
207  _slope = _row["SLOPE"].data<long long>();
208  _channels = (::uint16_t)(_row["CHANNELS"].data<long long>());
209 
210  _lb_link_map.insert(std::pair<RPCLBLink, RPCFebConnector>(_lb_link, RPCFebConnector(_det_id
211  , _first_strip
212  , _slope
213  , _channels)));
214  }
215  _cursor.close();
216 
217  _input_session->transaction().commit();
218 
219  if (!txt_file_.empty()) {
220  edm::LogInfo("RPCLBLinkMapHandler") << "Fill txtFile";
221  std::ofstream _ofstream(txt_file_);
222  for (RPCLBLinkMap::map_type::const_iterator _link_connector = _lb_link_map.begin()
223  ; _link_connector != _lb_link_map.end() ; ++_link_connector) {
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, since_run_));
230 }
231 
233 {
234  return id_;
235 }
T getParameter(std::string const &) const
void setParameters(const edm::ParameterSet &connectionPset)
cond::persistency::ConnectionPool connection_
cond::ValidityInterval lastInterval
Definition: Types.h:73
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
map_type & getMap()
Definition: RPCLBLinkMap.h:28
std::string id() const
static RPCDetId getRPCDetId(int _region, int _disk_or_wheel, int _layer, int _sector, std::string _subsector_string, std::string _partition)
static void parse(std::string const &_name, RPCLBLink &_lb_link)
std::map< RPCLBLink, RPCFebConnector > map_type
Definition: RPCLBLinkMap.h:14
RPCLBLinkMapHandler(edm::ParameterSet const &_config)
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
cond::TagInfo_t const & tagInfo() const