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