CMS 3D CMS Logo

L1CaloHcalScaleConfigOnlineProd.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1EmEtScaleOnlineProd
4 // Class: L1EmEtScaleOnlineProd
5 //
13 //
14 // Original Author: Werner Man-Li Sun
15 // Created: Tue Sep 16 22:43:22 CEST 2008
16 //
17 //
18 
19 // system include files
20 
21 // user include files
30 
31 #include <cmath>
32 #include <iostream>
33 #include <iomanip>
34 
35 //
36 // class declaration
37 //
38 
39 class L1CaloHcalScaleConfigOnlineProd : public L1ConfigOnlineProdBase<L1CaloHcalScaleRcd, L1CaloHcalScale> {
40 public:
43 
44  std::unique_ptr<L1CaloHcalScale> produce(const L1CaloHcalScaleRcd& iRecord) override;
45 
46  std::unique_ptr<L1CaloHcalScale> newObject(const std::string& objectKey) override;
47 
48 private:
52  typedef std::vector<double> RCTdecompression;
53  std::vector<RCTdecompression> hcaluncomp;
54 
55  // HcaluLUTTPGCoder* tpgCoder;// = new HcaluLUTTPGCoder();
56 
58 
59  // ----------member data ---------------------------
60 };
61 
62 //
63 // constants, enums and typedefs
64 //
65 
66 //
67 // static data member definitions
68 //
69 
70 //
71 // constructors and destructor
72 //
74  : L1ConfigOnlineProdBase<L1CaloHcalScaleRcd, L1CaloHcalScale>(iConfig), theTrigTowerGeometry(nullptr) {
77 }
78 
80  // do anything here that needs to be done at desctruction time
81  // (e.g. close files, deallocate resources etc.)
82 
83  if (caloTPG != nullptr)
84  delete caloTPG;
85 }
86 
87 std::unique_ptr<L1CaloHcalScale> L1CaloHcalScaleConfigOnlineProd::newObject(const std::string& objectKey) {
88  assert(theTrigTowerGeometry != nullptr);
89 
90  edm::LogInfo("L1CaloHcalScaleConfigOnlineProd") << "object Key " << objectKey;
91 
92  if (objectKey == "NULL" || objectKey.empty()) { // return default blank ecal scale
93  return std::make_unique<L1CaloHcalScale>(0);
94  }
95  if (objectKey == "IDENTITY") { // return identity ecal scale
96  return std::make_unique<L1CaloHcalScale>(1);
97  }
98 
99  std::vector<unsigned int> analyticalLUT(1024, 0);
100  std::vector<unsigned int> identityLUT(1024, 0);
101 
102  // Compute compression LUT
103  for (unsigned int i = 0; i < 1024; i++) {
104  analyticalLUT[i] = (unsigned int)(sqrt(14.94 * log(1. + i / 14.94) * i) + 0.5);
105  identityLUT[i] = std::min(i, 0xffu);
106  }
107 
108  hcaluncomp.clear();
109  for (int i = 0; i < 4176; i++) {
110  RCTdecompression decompressionTable(256, 0);
111  hcaluncomp.push_back(decompressionTable);
112  }
113 
114  std::vector<std::string> mainStrings;
115  mainStrings.push_back("HCAL_LUT_METADATA");
116  mainStrings.push_back("HCAL_LUT_CHAN_DATA");
117 
118  // ~~~~~~~~~ Cut values ~~~~~~~~~
119 
120  std::vector<std::string> metaStrings;
121  metaStrings.push_back("RCTLSB");
122  metaStrings.push_back("NOMINAL_GAIN");
123 
124  l1t::OMDSReader::QueryResults paramResults =
125  m_omdsReader.basicQueryView(metaStrings,
126  "CMS_HCL_HCAL_COND",
127  "V_HCAL_LUT_METADATA_V1",
128  "V_HCAL_LUT_METADATA_V1.TAG_NAME",
129  m_omdsReader.basicQuery("HCAL_LUT_METADATA",
130  "CMS_RCT",
131  "HCAL_SCALE_KEY",
132  "HCAL_SCALE_KEY.HCAL_TAG",
133  m_omdsReader.singleAttribute(objectKey)));
134 
135  if (paramResults.queryFailed() || (paramResults.numberRows() != 1)) // check query successful
136  {
137  edm::LogError("L1-O2O") << "Problem with L1CaloHcalScale key. Unable to find lutparam dat table";
138  return std::unique_ptr<L1CaloHcalScale>();
139  }
140 
141  double hcalLSB, nominal_gain;
142  paramResults.fillVariable("RCTLSB", hcalLSB);
143  paramResults.fillVariable("NOMINAL_GAIN", nominal_gain);
144 
145  float rctlsb = hcalLSB;
146 
147  l1t::OMDSReader::QueryResults chanKey = m_omdsReader.basicQuery("HCAL_LUT_CHAN_DATA",
148  "CMS_RCT",
149  "HCAL_SCALE_KEY",
150  "HCAL_SCALE_KEY.HCAL_TAG",
151  m_omdsReader.singleAttribute(objectKey));
152 
153  //coral::AttributeList myresult;
154  // myresult.extend(
155 
156  std::string schemaName("CMS_HCL_HCAL_COND");
157  coral::ISchema& schema = m_omdsReader.dbSession().coralSession().schema(schemaName);
158  coral::IQuery* query = schema.newQuery();
159  ;
160 
161  std::vector<std::string> channelStrings;
162  channelStrings.push_back("IPHI");
163  channelStrings.push_back("IETA");
164  channelStrings.push_back("DEPTH");
165  channelStrings.push_back("LUT_GRANULARITY");
166  channelStrings.push_back("OUTPUT_LUT_THRESHOLD");
167  channelStrings.push_back("OBJECTNAME");
168 
169  std::vector<std::string>::const_iterator it = channelStrings.begin();
170  std::vector<std::string>::const_iterator end = channelStrings.end();
171  for (; it != end; ++it) {
172  query->addToOutputList(*it);
173  }
174 
175  std::string ob = "OBJECTNAME";
176  coral::AttributeList myresult;
177  myresult.extend("IPHI", typeid(int));
178  myresult.extend("IETA", typeid(int));
179  myresult.extend("DEPTH", typeid(int));
180  myresult.extend("LUT_GRANULARITY", typeid(int));
181  myresult.extend("OUTPUT_LUT_THRESHOLD", typeid(int));
182  myresult.extend(ob, typeid(std::string)); //, typeid(std::string));
183 
184  query->defineOutput(myresult);
185 
186  query->addToTableList("V_HCAL_LUT_CHAN_DATA_V1");
187 
188  query->setCondition("V_HCAL_LUT_CHAN_DATA_V1.TAG_NAME = :" + chanKey.columnNames().front(),
189  chanKey.attributeLists().front());
190 
191  coral::ICursor& cursor = query->execute();
192 
193  // when the query goes out of scope.
194  std::vector<coral::AttributeList> atts;
195  while (cursor.next()) {
196  atts.push_back(cursor.currentRow());
197  };
198 
199  delete query;
200 
201  l1t::OMDSReader::QueryResults chanResults(channelStrings, atts);
202  if (chanResults.queryFailed() || (chanResults.numberRows() == 0)) // check query successful
203  {
204  edm::LogError("L1-O2O") << "Problem with L1CaloHcalScale key. Unable to find lutparam dat table nrows"
205  << chanResults.numberRows();
206  return std::unique_ptr<L1CaloHcalScale>();
207  }
208 
209  chanResults.attributeLists();
210  for (int i = 0; i < chanResults.numberRows(); ++i) {
212  chanResults.fillVariableFromRow("OBJECTNAME", i, objectName);
213  // int
214  if (objectName == "HcalTrigTowerDetId") { //trig tower
215  int ieta, iphi, depth, lutGranularity, threshold;
216 
217  chanResults.fillVariableFromRow("LUT_GRANULARITY", i, lutGranularity);
218  chanResults.fillVariableFromRow("IPHI", i, iphi);
219  chanResults.fillVariableFromRow("IETA", i, ieta);
220  chanResults.fillVariableFromRow("DEPTH", i, depth);
221  chanResults.fillVariableFromRow("OUTPUT_LUT_THRESHOLD", i, threshold);
222 
223  unsigned int outputLut[1024];
224 
225  const int tp_version = depth / 10;
226  uint32_t lutId = caloTPG->getOutputLUTId(ieta, iphi, tp_version);
227 
228  double eta_low = 0., eta_high = 0.;
229  theTrigTowerGeometry->towerEtaBounds(ieta, tp_version, eta_low, eta_high);
230  double cosh_ieta = fabs(cosh((eta_low + eta_high) / 2.));
231 
232  if (!caloTPG->HTvalid(ieta, iphi, tp_version))
233  continue;
234  double factor = 0.;
235  if (abs(ieta) >= theTrigTowerGeometry->firstHFTower(tp_version))
236  factor = rctlsb;
237  else
238  factor = nominal_gain / cosh_ieta * lutGranularity;
239  for (int k = 0; k < threshold; ++k)
240  outputLut[k] = 0;
241 
242  for (unsigned int k = threshold; k < 1024; ++k)
243  outputLut[k] = (abs(ieta) < theTrigTowerGeometry->firstHFTower(tp_version)) ? analyticalLUT[k] : identityLUT[k];
244 
245  // tpg - compressed value
246  unsigned int tpg = outputLut[0];
247 
248  int low = 0;
249 
250  for (unsigned int k = 0; k < 1024; ++k) {
251  if (outputLut[k] != tpg) {
252  unsigned int mid = (low + k) / 2;
253  hcaluncomp[lutId][tpg] = (tpg == 0 ? low : factor * mid);
254  low = k;
255  tpg = outputLut[k];
256  }
257  }
258  hcaluncomp[lutId][tpg] = factor * low;
259  }
260  }
261 
262  auto hcalScale = std::make_unique<L1CaloHcalScale>(0);
263 
264  // XXX L1CaloHcalScale is only setup for 2x3 TP
265  const int tp_version = 0;
266  for (unsigned short ieta = 1; ieta <= L1CaloHcalScale::nBinEta; ++ieta) {
267  for (int pos = 0; pos <= 1; pos++) {
268  for (unsigned short irank = 0; irank < L1CaloHcalScale::nBinRank; ++irank) {
269  int zside = (int)pow(-1, pos);
270  int nphi = 0;
271  double etvalue = 0.;
272 
273  for (int iphi = 1; iphi <= 72; iphi++) {
274  if (!caloTPG->HTvalid(ieta, iphi, tp_version))
275  continue;
276  uint32_t lutId = caloTPG->getOutputLUTId(ieta, iphi, tp_version);
277  nphi++;
278  etvalue += (double)hcaluncomp[lutId][irank];
279 
280  } // phi
281  if (nphi > 0)
282  etvalue /= nphi;
283 
284  hcalScale->setBin(irank, ieta, zside, etvalue);
285 
286  } // rank
287  } // zside
288  } // eta
289 
290  std::stringstream s;
291  s << std::setprecision(10);
292  hcalScale->print(s);
293  edm::LogInfo("L1CaloHcalScaleConfigOnlineProd") << s.str();
294  // ------------ method called to produce the data ------------
295  return hcalScale;
296 }
297 
298 std::unique_ptr<L1CaloHcalScale> L1CaloHcalScaleConfigOnlineProd::produce(const L1CaloHcalScaleRcd& iRecord) {
300 
302 }
303 
304 //define this as a plug-in
const int nphi
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:259
virtual bool HTvalid(const int ieta, const int iphi, const int version) const
L1CaloHcalScaleConfigOnlineProd(const edm::ParameterSet &iConfig)
int zside(DetId const &)
std::optional< edm::ESConsumesCollectorT< L1CaloHcalScaleRcd > > m_consumesCollector
Log< level::Error, false > LogError
assert(be >=bs)
std::unique_ptr< L1CaloHcalScale > newObject(const std::string &objectKey) override
Definition: query.py:1
int firstHFTower(int version) const
std::unique_ptr< L1CaloHcalScale > produce(const L1CaloHcalScaleRcd &iRecord) override
const std::vector< coral::AttributeList > & attributeLists() const
Definition: OMDSReader.h:58
const HcalTrigTowerGeometry * theTrigTowerGeometry
edm::ESGetToken< HcalTrigTowerGeometry, CaloGeometryRecord > theTrigTowerGeometryToken
T sqrt(T t)
Definition: SSEVec.h:23
static const unsigned short nBinRank
const QueryResults basicQuery(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &tableName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
Definition: OMDSReader.cc:75
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const std::vector< std::string > & columnNames() const
Definition: OMDSReader.h:57
bool fillVariableFromRow(const std::string &columnName, int rowNumber, T &outputVariable) const
Definition: OMDSReader.h:279
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:274
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:61
Log< level::Info, false > LogInfo
std::vector< RCTdecompression > hcaluncomp
const QueryResults basicQueryView(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &viewName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
Definition: OMDSReader.cc:158
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
cond::persistency::Session dbSession()
Definition: DataManager.h:26
void towerEtaBounds(int ieta, int version, double &eta1, double &eta2) const
where this tower begins and ends in eta
static const unsigned short nBinEta
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
virtual int getOutputLUTId(const HcalTrigTowerDetId &id) const