CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EcalSRCondTools.cc
Go to the documentation of this file.
1 //emacs settings:-*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  *
4  * author: Ph Gras. June, 2010
5  */
6 
8 
14 
15 #include <fstream>
16 #include <iostream>
17 #include <algorithm>
18 
19 constexpr int tccNum[12][12] = {
20  /* EE- */
21  {36, 19, 20, 21, 22, 23, 18, 1, 2, 3, 4, 5}, //SRP 1
22  {24, 25, 26, 27, 28, 29, 6, 7, 8, 9, 10, 11}, //SRP 2
23  {30, 31, 32, 33, 34, 35, 12, 13, 14, 15, 16, 17}, //SRP 3
24  /* EB- */
25  {54, 37, 38, 39, 40, 41, -1, -1, -1, -1, -1, -1}, //SRP 4
26  {42, 43, 44, 45, 46, 47, -1, -1, -1, -1, -1, -1}, //SRP 5
27  {48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1, -1}, //SRP 6
28  /* EB+ */
29  {72, 55, 56, 57, 58, 59, -1, -1, -1, -1, -1, -1}, //SRP 7
30  {60, 61, 62, 63, 64, 65, -1, -1, -1, -1, -1, -1}, //SRP 8
31  {66, 67, 68, 69, 70, 71, -1, -1, -1, -1, -1, -1}, //SRP 9
32  /* EE+ */
33  {90, 73, 74, 75, 76, 77, 108, 91, 92, 93, 94, 95}, //SRP 10
34  {78, 79, 80, 81, 82, 83, 96, 97, 98, 99, 100, 101}, //SRP 11
35  {84, 85, 86, 87, 88, 89, 102, 103, 104, 105, 106, 107} //SRP 12
36 };
37 
38 constexpr int dccNum[12][12] = {
39  {1, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //SRP 1
40  {4, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //SRP 2
41  {7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //SRP 3
42  {10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1}, //SRP 4
43  {16, 17, 18, 19, 20, 21, -1, -1, -1, -1, -1, -1}, //SRP 5
44  {22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1}, //SRP 6
45  {28, 29, 30, 31, 32, 33, -1, -1, -1, -1, -1, -1}, //SRP 7
46  {34, 35, 36, 37, 38, 39, -1, -1, -1, -1, -1, -1}, //SRP 8
47  {40, 41, 42, 43, 44, 45, -1, -1, -1, -1, -1, -1}, //SRP 9
48  {46, 47, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //SRP 10
49  {49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //SRP 11
50  {52, 53, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1} //SRP 12
51 };
52 
53 using namespace std;
54 
56  : ps_(ps), mode_(ps.getParameter<string>("mode")), iomode_write_(true), done_(false) {
57  if (mode_ == "read") {
58  iomode_write_ = false;
61  }
62 }
63 
65 
67  if (done_)
68  return;
69  EcalSRSettings sr;
70 
71  if (mode_ == "online_config" || mode_ == "combine_config") {
72  string fname = ps_.getParameter<string>("onlineSrpConfigFile");
73  ifstream f(fname.c_str());
74  if (!f.good()) {
75  throw cms::Exception("EcalSRCondTools") << "Failed to open file " << fname;
76  }
77  importSrpConfigFile(sr, f, true);
78  }
79 
80  if (mode_ == "python_config" || mode_ == "combine_config") {
82  }
83 
84  if (!(mode_ == "python_config" || mode_ == "online_config" || mode_ == "combine_config" || (mode_ == "read"))) {
85  throw cms::Exception("Config") << "Invalid value," << mode_ << ", for parameter mode. "
86  << "Valid values: online_config, python_config, combine_config, read";
87  }
88 
89  if (iomode_write_) {
90  sr.bxGlobalOffset_ = ps_.getParameter<int>("bxGlobalOffset");
91  sr.automaticSrpSelect_ = ps_.getParameter<int>("automaticSrpSelect");
92  sr.automaticMasks_ = ps_.getParameter<int>("automaticMasks");
93 
95  if (!db.isAvailable()) {
96  throw cms::Exception("CondDBAccess") << "Failed to connect to PoolDBOutputService\n";
97  }
98  //fillup DB
99  //create new infinite IOV
100  cond::Time_t firstSinceTime = db->beginOfTime();
101  db->writeOneIOV(sr, firstSinceTime, "EcalSRSettingsRcd");
102  done_ = true;
103  } else { //read mode
105  if (!hSr.isValid()) {
106  cout << "EcalSRSettings record not found. Check the Cond DB Global tag.\n";
107  } else {
108  const EcalSRSettings* ssr = hSr.product();
109  cout << "ECAL Seletive readout settings:\n";
110  cout << *ssr << "\n" << endl;
111  }
112 
113  //trigger tower thresholds (from FENIX configuration):
115  if (!hTp.isValid()) {
116  cout << "EcalTPGPhysicsConst record not found. Check the Cond DB Global tag.\n";
117  } else {
118  const EcalTPGPhysicsConst* tp = hTp.product();
119  const EcalTPGPhysicsConstMap& mymap = tp->getMap();
120  if (mymap.size() != 2) {
121  cout << "Error: TPG physics record is of unexpected size: " << mymap.size()
122  << " elements instead of two (one for EB, one for EE)\n";
123  } else {
124  EcalTPGPhysicsConstMap::const_iterator it = mymap.begin();
125  cout << "----------------------------------------------------------------------\n"
126  "Trigger tower Et thresholds extracted from TPG configuration \n"
127  "(EcalSRCondTools modules supports only read mode for these parameters):\n\n";
128  cout << "EB: "
129  << "LT = " << it->second.ttf_threshold_Low << " GeV "
130  << "HT = " << it->second.ttf_threshold_High << " GeV\n";
131  ++it;
132  cout << "EE: "
133  << "LT = " << it->second.ttf_threshold_Low << " GeV "
134  << "HT = " << it->second.ttf_threshold_High << " GeV\n";
135  }
136  }
137  }
138 }
139 
141  sr.deltaPhi_.resize(1);
142  sr.deltaPhi_[0] = ps.getParameter<int>("deltaPhi");
143  sr.deltaEta_.resize(1);
144  sr.deltaEta_[0] = ps.getParameter<int>("deltaEta");
145  sr.ecalDccZs1stSample_.resize(1);
146  sr.ecalDccZs1stSample_[0] = ps.getParameter<int>("ecalDccZs1stSample");
147  sr.ebDccAdcToGeV_ = ps.getParameter<double>("ebDccAdcToGeV");
148  sr.eeDccAdcToGeV_ = ps.getParameter<double>("eeDccAdcToGeV");
149  sr.dccNormalizedWeights_.resize(1);
150  const std::vector<double>& w = ps.getParameter<std::vector<double> >("dccNormalizedWeights");
151  sr.dccNormalizedWeights_[0].resize(w.size());
152  std::copy(w.begin(), w.end(), sr.dccNormalizedWeights_[0].begin());
153  sr.symetricZS_.resize(1);
154  sr.symetricZS_[0] = ps.getParameter<bool>("symetricZS");
155  sr.srpLowInterestChannelZS_.resize(2);
156  const int eb = 0;
157  const int ee = 1;
158  sr.srpLowInterestChannelZS_[eb] = ps.getParameter<double>("srpBarrelLowInterestChannelZS");
159  sr.srpLowInterestChannelZS_[ee] = ps.getParameter<double>("srpEndcapLowInterestChannelZS");
160  sr.srpHighInterestChannelZS_.resize(2);
161  sr.srpHighInterestChannelZS_[eb] = ps.getParameter<double>("srpBarrelHighInterestChannelZS");
162  sr.srpHighInterestChannelZS_[ee] = ps.getParameter<double>("srpEndcapHighInterestChannelZS");
163  //sr.trigPrimBypass_.resize(1);
164  //sr.trigPrimBypass_[0] = ps.getParameter<bool >("trigPrimBypass");
165  //sr.trigPrimBypassMode_.resize(1);
166  //sr.trigPrimBypassMode_[0] = ps.getParameter<int >("trigPrimBypassMode");
167  //sr.trigPrimBypassLTH_.resize(1);
168  //sr.trigPrimBypassLTH_[0] = ps.getParameter<double >("trigPrimBypassLTH");
169  //sr.trigPrimBypassHTH_.resize(1);
170  //sr.trigPrimBypassHTH_[0] = ps.getParameter<double >("trigPrimBypassHTH");
171  //sr.trigPrimBypassWithPeakFinder_.resize(1);
172  //sr.trigPrimBypassWithPeakFinder_[0] = ps.getParameter<bool >("trigPrimBypassWithPeakFinder");
173  //sr.defaultTtf_.resize(1);
174  //sr.defaultTtf_[0] = ps.getParameter<int >("defaultTtf");
175  sr.actions_ = ps.getParameter<std::vector<int> >("actions");
176 }
177 
178 void EcalSRCondTools::importSrpConfigFile(EcalSRSettings& sr, std::istream& f, bool d) {
179  //initialize vectors:
180  sr.deltaEta_ = vector<int>(1, 0);
181  sr.deltaPhi_ = vector<int>(1, 0);
182  sr.actions_ = vector<int>(4, 0);
183  sr.tccMasksFromConfig_ = vector<short>(EcalSRSettings::nTccs_, 0);
184  sr.srpMasksFromConfig_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(8, 0));
185  sr.dccMasks_ = vector<short>(EcalSRSettings::nDccs_);
186  sr.srfMasks_ = vector<short>(EcalSRSettings::nDccs_);
187  sr.substitutionSrfs_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(68, 0));
188  sr.testerTccEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
189  sr.testerSrpEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
190  sr.testerDccTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
191  sr.testerSrpTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
192  sr.bxOffsets_ = vector<short>(EcalSRSettings::nSrps_, 0);
193  sr.automaticMasks_ = 0;
194  sr.automaticSrpSelect_ = 0;
195 
196  //string line;
197  int iLine = 0;
198  int iValueSet = -1;
199  const int nValueSets = 6 * EcalSRSettings::nSrps_ + 9;
200  string line;
201  stringstream sErr("");
202  while (!f.eof() && sErr.str().empty()) {
203  getline(f, line);
204  ++iLine;
205  line = trim(line);
206  if (line[0] == '#' || line.empty()) { //comment line and empty line to ignore
207  continue;
208  } else {
209  ++iValueSet;
210  }
211  if (iValueSet >= nValueSets)
212  break;
213  uint32_t value;
214  string sValue;
215  int pos = 0;
216  int iCh = 0;
217  int nChs[nValueSets] = {
218  //TCC masks: 0-11
219  12,
220  12,
221  12,
222  12,
223  12,
224  12,
225  12,
226  12,
227  12,
228  12,
229  12,
230  12,
231  //SRP masks: 12-23
232  8,
233  8,
234  8,
235  8,
236  8,
237  8,
238  8,
239  8,
240  8,
241  8,
242  8,
243  8,
244  //DCC masks: 24-35
245  12,
246  12,
247  12,
248  12,
249  12,
250  12,
251  12,
252  12,
253  12,
254  12,
255  12,
256  12,
257  //SRF Masks: 36-47
258  6,
259  6,
260  6,
261  6,
262  6,
263  6,
264  6,
265  6,
266  6,
267  6,
268  6,
269  6,
270  //substitution SRFs: 48-59
271  68,
272  68,
273  68,
274  68,
275  68,
276  68,
277  68,
278  68,
279  68,
280  68,
281  68,
282  68,
283  //Tester card to emulate or test: 60-71
284  4,
285  4,
286  4,
287  4,
288  4,
289  4,
290  4,
291  4,
292  4,
293  4,
294  4,
295  4,
296  //Bx offsets: 72
297  12,
298  //algo type: 73
299  1,
300  //action flags: 74
301  4,
302  //pattern file directory: 75
303  1,
304  //VME slots: 76
305  12,
306  //card types: 77
307  12,
308  //config Mode
309  1,
310  //VME Interface card
311  1,
312  //Spy Mode
313  12,
314  };
315 
316  while (((sValue = tokenize(line, " \t", pos)) != string("")) && (iCh < nChs[iValueSet]) && sErr.str().empty()) {
317  value = strtoul(sValue.c_str(), nullptr, 0);
318  const int iSrp = iValueSet % EcalSRSettings::nSrps_;
319  if (iValueSet < 12) { //TCC
320  assert((unsigned)iSrp < sizeof(tccNum) / sizeof(tccNum[0]));
321  assert((unsigned)iCh < sizeof(tccNum[0]) / sizeof(tccNum[0][0]));
322  int tcc = tccNum[iSrp][iCh];
323  if (tcc >= 0) {
324  if (d)
325  cout << "tccMasksFromConfig_[" << tcc << "] <- " << value << "\n";
326  sr.tccMasksFromConfig_[tcc - 1] = value;
327  }
328  } else if (iValueSet < 24) { //SRP-SRP
329  if (d)
330  cout << "srpMasks_[" << iSrp << "][" << iCh << "] <- " << value << "\n";
331  sr.srpMasksFromConfig_[iSrp][iCh] = value;
332  } else if (iValueSet < 36) { //DCC output
333  assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
334  assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
335  int dcc = dccNum[iSrp][iCh];
336  if (dcc > 0) {
337  assert((unsigned)(dcc - 1) < sr.dccMasks_.size());
338  if (d)
339  cout << "dccMasks_[" << (dcc - 1) << "] <- " << value << "\n";
340  sr.dccMasks_[dcc - 1] = value;
341  }
342  } else if (iValueSet < 48) { //SRF masks
343  assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
344  assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
345  int dcc = dccNum[iSrp][iCh];
346  if (dcc > 0) {
347  if (d)
348  cout << "srfMasks_[" << (dcc - 1) << "] <- " << value << "\n";
349  assert((unsigned)(dcc - 1) < sr.srfMasks_.size());
350  sr.srfMasks_[dcc - 1] = value;
351  }
352  } else if (iValueSet < 60) { //substiution SRFs
353  assert((unsigned)iSrp < sr.substitutionSrfs_.size());
354  assert((unsigned)iCh < sr.substitutionSrfs_[0].size());
355  if (d)
356  cout << "substitutionMasks_[" << iSrp << "][" << iCh << "] <- " << value << "\n";
357  sr.substitutionSrfs_[iSrp][iCh] = value;
358  } else if (iValueSet < 72) { //Tester card config
359  switch (iCh) {
360  case 0:
361  assert((unsigned)iSrp < sr.testerTccEmuSrpIds_.size());
362  if (d)
363  cout << "testerTccEmuSrpIds_[" << iSrp << "] <- " << value << "\n";
364  sr.testerTccEmuSrpIds_[iSrp] = value;
365  break;
366  case 1:
367  assert((unsigned)iSrp < sr.testerSrpEmuSrpIds_.size());
368  if (d)
369  cout << "testerSrpEmuSrpIds_[" << iSrp << "] <- " << value << "\n";
370  sr.testerSrpEmuSrpIds_[iSrp] = value;
371  break;
372  case 2:
373  assert((unsigned)iSrp < sr.testerDccTestSrpIds_.size());
374  if (d)
375  cout << "testerDccTestSrpIds_[" << iSrp << "] <- " << value << "\n";
376  sr.testerDccTestSrpIds_[iSrp] = value;
377  break;
378  case 3:
379  assert((unsigned)iSrp < sr.testerSrpTestSrpIds_.size());
380  if (d)
381  cout << "testerSrpTestSrpIds_[" << iSrp << "] <- " << value << "\n";
382  sr.testerSrpTestSrpIds_[iSrp] = value;
383  break;
384  default:
385  sErr << "Syntax error in SRP system configuration "
386  << " line " << iLine << ".";
387  }
388  } else if (iValueSet < 73) { //bx offsets
389  assert((unsigned)iCh < sr.bxOffsets_.size());
390  if (d)
391  cout << "bxOffset_[" << iCh << "] <- " << value << "\n";
392  sr.bxOffsets_[iCh] = value;
393  } else if (iValueSet < 74) { //algo type
394  int algo = value;
395  switch (algo) {
396  case 0:
397  sr.deltaEta_[0] = sr.deltaPhi_[0] = 1;
398  break;
399  case 1:
400  sr.deltaEta_[0] = sr.deltaPhi_[0] = 2;
401  break;
402  default:
403  throw cms::Exception("OutOfRange")
404  << "Value of parameter algo ," << algo << ", is invalid. Valid values are 0 and 1.";
405  }
406  if (d)
407  cout << "deltaEta_[0] <- " << sr.deltaEta_[0] << "\t"
408  << "deltaPhi_[0] <- " << sr.deltaPhi_[0] << "\n";
409  } else if (iValueSet < 75) { //action flags
410  assert((unsigned)iCh < sr.actions_.size());
411  if (d)
412  cout << "actions_[" << iCh << "] <- " << value << "\n";
413  sr.actions_[iCh] = value;
414  } else if (iValueSet < 76) { //pattern file directory
415  // emuDir_ = sValue;
416  // if(d) cout << "emuDir_ <= "
417  // << value << "\n";
418  } else if (iValueSet < 77) { //VME slots
419  // slotIds_[iCh] = value;
420  // if(d) cout << "slotIds_[" << iCh << "] <= "
421  // << value << "\n";
422  } else if (iValueSet < 78) { //card types
423  // cardTypes_[iCh] = sValue[0];
424  // if(d) cout << "cardTypes_[" << iCh << "] <= "
425  // << value << "\n";
426  } else if (iValueSet < 79) { //config mode
427  //TODO validity check on value
428  // configMode_ = (ConfigMode)value;
429  // if(d) cout << "config mode <= " << value << "\n";
430  } else if (iValueSet < 80) { //VME I/F
431  //TODO validity check on value
432  // vmeInterface_ = (Vme::type_t)value;
433  //if(d) cout << "Vme Interface code <= " << value << "\n";
434  } else if (iValueSet < 81) { //Spy Mode
435  //TODO validity check on value
436  // spyMode_[iCh] = value & 0x3;
437  // if(d) cout << "Spy mode <= " << value << "\n";
438  } else { //should never be reached!
439  assert(false);
440  }
441  ++iCh;
442  }
443  if (iCh != nChs[iValueSet]) { //error
444  sErr << "Syntax error in imported SRP system configuration file "
445  /*<< filename <<*/ " line "
446  << iLine << ".";
447  }
448  }
449  if (sErr.str().empty() && iValueSet != (nValueSets - 1)) { //error
450  sErr << "Syntax Error in imported SRP system configuration file "
451  /*<< filename <<*/ " line "
452  << iLine << ".";
453  }
454  if (!sErr.str().empty())
455  throw cms::Exception("SyntaxError") << sErr.str();
456 }
457 
458 double EcalSRCondTools::normalizeWeights(int hwWeight) {
459  //Fix sign bit in case only the 12 least significant bits of hwWeight were set
460  //(hardware reprensentation uses only 12 bits)
461  if (hwWeight & (1 << 11))
462  hwWeight |= ~0xEFF;
463  return hwWeight / 1024.;
464 }
465 
466 string EcalSRCondTools::tokenize(const string& s, const string& delim, int& pos) {
467  if (pos < 0)
468  return "";
469  int pos0 = pos;
470  int len = s.size();
471  //eats delimeters at beginning of the string
472  while (pos0 < len && find(delim.begin(), delim.end(), s[pos0]) != delim.end()) {
473  ++pos0;
474  }
475  if (pos0 == len)
476  return "";
477  pos = s.find_first_of(delim, pos0);
478  return s.substr(pos0, (pos > 0 ? pos : len) - pos0);
479 }
480 
482  std::string::size_type pos0 = s.find_first_not_of(" \t");
483  if (pos0 == string::npos) {
484  pos0 = 0;
485  }
486  string::size_type pos1 = s.find_last_not_of(" \t") + 1;
487  if (pos1 == string::npos) {
488  pos1 = pos0;
489  }
490  return s.substr(pos0, pos1 - pos0);
491 }
static const int nTccs_
static void importSrpConfigFile(EcalSRSettings &sr, std::istream &f, bool debug=false)
static const int nDccs_
const std::string mode_
const edm::ParameterSet ps_
static std::string tokenize(const std::string &s, const std::string &delim, int &pos)
static void importParameterSet(EcalSRSettings &sr, const edm::ParameterSet &ps)
static double normalizeWeights(int hwWeight)
constexpr int tccNum[12][12]
tuple db
Definition: EcalCondDB.py:153
std::vector< short > srfMasks_
std::vector< std::vector< float > > dccNormalizedWeights_
std::vector< int > testerSrpEmuSrpIds_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
assert(be >=bs)
edm::ESGetToken< EcalSRSettings, EcalSRSettingsRcd > hSrToken_
uint16_t size_type
static std::string trim(std::string s)
std::vector< float > srpHighInterestChannelZS_
std::vector< int > actions_
tuple d
Definition: ztail.py:151
const int nChs
void analyze(const edm::Event &evt, const edm::EventSetup &es) override
unsigned long long Time_t
Definition: Time.h:14
constexpr int dccNum[12][12]
std::vector< std::vector< short > > substitutionSrfs_
std::vector< int > testerDccTestSrpIds_
std::vector< int > ecalDccZs1stSample_
std::vector< short > tccMasksFromConfig_
std::vector< short > bxOffsets_
std::vector< short > dccMasks_
std::vector< int > deltaPhi_
const std::map< uint32_t, Item > & getMap() const
std::vector< int > symetricZS_
T const * product() const
Definition: ESHandle.h:86
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
float ebDccAdcToGeV_
ADC to GeV conversion factor used in ZS filter for EB.
std::vector< int > testerTccEmuSrpIds_
string fname
main script
std::vector< float > srpLowInterestChannelZS_
std::vector< int > deltaEta_
std::vector< int > testerSrpTestSrpIds_
tuple cout
Definition: gather_cfg.py:144
edm::ESGetToken< EcalTPGPhysicsConst, EcalTPGPhysicsConstRcd > tpgPhysicsConstToken_
std::map< uint32_t, EcalTPGPhysicsConst::Item > EcalTPGPhysicsConstMap
T w() const
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
float eeDccAdcToGeV_
ADC to GeV conversion factor used in ZS filter for EE.
static const int nSrps_
bool isValid() const
Definition: ESHandle.h:44
~EcalSRCondTools() override
EcalSRCondTools(const edm::ParameterSet &)
std::vector< std::vector< short > > srpMasksFromConfig_