CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LatencyHistosUsingDb.cc
Go to the documentation of this file.
1 
7 #include <iostream>
8 
9 #define MAXFEDCOARSE 15
10 
11 using namespace sistrip;
12 
13 // -----------------------------------------------------------------------------
16  : CommissioningHistograms(pset.getParameter<edm::ParameterSet>("LatencyParameters"), bei, sistrip::APV_LATENCY),
18  SamplingHistograms(pset.getParameter<edm::ParameterSet>("LatencyParameters"), bei, sistrip::APV_LATENCY) {
19  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
20  << " Constructing object...";
21 }
22 
23 // -----------------------------------------------------------------------------
26  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
27  << " Destructing object...";
28 }
29 
30 // -----------------------------------------------------------------------------
33  if (!db()) {
34  edm::LogWarning(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
35  << " NULL pointer to SiStripConfigDb interface!"
36  << " Aborting upload...";
37  return;
38  }
39 
42  bool upload = update(devices, feds);
43  // Check if new PLL settings are valid
44  if (!upload) {
45  edm::LogWarning(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
46  << " Found invalid PLL settings (coarse > 15)"
47  << " Aborting update to database...";
48  return;
49  }
50 
51  if (doUploadConf()) {
52  // Update APV descriptions with new Latency settings
53  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
54  << " Uploading APV settings to DB...";
56  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
57  << " Upload of APV settings to DB finished!";
58  // Update FED descriptions
59  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
60  << " Uploading FED delays to DB...";
62  LogTrace(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
63  << " Upload of FED delays to DB finished!";
64  } else {
65  edm::LogWarning(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
66  << " TEST only! No APV settings will be uploaded to DB...";
67  }
68 }
69 
70 // -----------------------------------------------------------------------------
74  // Obtain the latency from the analysis object
75  if (data().empty() || !data().begin()->second->isValid()) {
76  edm::LogVerbatim(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
77  << " Updated NO Latency settings. No analysis result available !";
78  return false;
79  }
80 
81  // Compute the minimum coarse delay
82  uint16_t minCoarseDelay = 256;
83  SiStripConfigDb::DeviceDescriptionsV::const_iterator idevice;
84  for (idevice = devices.begin(); idevice != devices.end(); idevice++) {
85  // Check device type
86  if ((*idevice)->getDeviceType() == PLL) {
87  // Cast to retrieve appropriate description object
88  pllDescription* desc = dynamic_cast<pllDescription*>(*idevice);
89  if (desc) {
90  /*
91  // add 1 to aim at 1 and not 0 (just to avoid a special 0 value for security)
92  int delayCoarse = desc->getDelayCoarse() - 1;
93  delayCoarse = delayCoarse < 0 ? 0 : delayCoarse;
94  minCoarseDelay = minCoarseDelay < delayCoarse ? minCoarseDelay : delayCoarse;
95 */
96  int delayCoarse = desc->getDelayCoarse();
97  minCoarseDelay = minCoarseDelay < delayCoarse ? minCoarseDelay : delayCoarse;
98  }
99  }
100  }
101 
102  // Compute latency and PLL shift from the sampling measurement
103  SamplingAnalysis* anal = nullptr;
104  for (CommissioningHistograms::Analysis it = data().begin(); it != data().end(); ++it) {
105  if (dynamic_cast<SamplingAnalysis*>(it->second) &&
106  dynamic_cast<SamplingAnalysis*>(it->second)->granularity() == sistrip::TRACKER)
107  anal = dynamic_cast<SamplingAnalysis*>(it->second);
108  }
109  if (!anal)
110  return false;
111  uint16_t globalLatency = uint16_t(ceil(anal->maximum() / (-25.)));
112  float globalShift = anal->maximum() - (globalLatency * (-25));
113 
114  // Compute latency and PLL shift per partition... this is an option
115  uint16_t latency = globalLatency;
116  float shift[5] = {0.};
117  for (CommissioningHistograms::Analysis it = data().begin(); it != data().end(); ++it) {
118  if (dynamic_cast<SamplingAnalysis*>(it->second) &&
119  dynamic_cast<SamplingAnalysis*>(it->second)->granularity() == sistrip::PARTITION) {
120  anal = dynamic_cast<SamplingAnalysis*>(it->second);
121  latency = uint16_t(ceil(anal->maximum() / (-25.))) > latency ? uint16_t(ceil(anal->maximum() / (-25.))) : latency;
122  }
123  }
124  for (CommissioningHistograms::Analysis it = data().begin(); it != data().end(); ++it) {
125  if (dynamic_cast<SamplingAnalysis*>(it->second) &&
126  dynamic_cast<SamplingAnalysis*>(it->second)->granularity() == sistrip::PARTITION) {
127  anal = dynamic_cast<SamplingAnalysis*>(it->second);
128  shift[SiStripFecKey(anal->fecKey()).fecCrate()] = anal->maximum() - (latency * (-25));
129  }
130  }
131  if (!perPartition_) {
132  latency = globalLatency;
133  for (int i = 0; i < 5; i++)
134  shift[i] = globalShift;
135  }
136 
137  // Take into account the minimum coarse delay to bring the coarse delay down
138  // the same quantity is subtracted to the coarse delay of each APV
139  latency -= minCoarseDelay;
140 
141  // Iterate through devices and update device descriptions
142  uint16_t updatedAPV = 0;
143  uint16_t updatedPLL = 0;
144  std::vector<SiStripFecKey> invalid;
145  for (idevice = devices.begin(); idevice != devices.end(); idevice++) {
146  // Check device type
147  if ((*idevice)->getDeviceType() != APV25) {
148  continue;
149  }
150  // Cast to retrieve appropriate description object
151  apvDescription* desc = dynamic_cast<apvDescription*>(*idevice);
152  if (!desc) {
153  continue;
154  }
155  // Retrieve device addresses from device description
157  // Do it!
158  std::stringstream ss;
159  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
160  << " Updating latency APV settings for crate/FEC/slot/ring/CCU/i2cAddr " << addr.fecCrate_ << "/"
161  << addr.fecSlot_ << "/" << addr.fecRing_ << "/" << addr.ccuAddr_ << "/" << addr.ccuChan_ << "/" << addr.i2cAddr_
162  << " from " << static_cast<uint16_t>(desc->getLatency());
163  desc->setLatency(latency);
164  ss << " to " << static_cast<uint16_t>(desc->getLatency());
165  LogTrace(mlDqmClient_) << ss.str();
166  updatedAPV++;
167  }
168 
169  // Change also the PLL delay
170  for (idevice = devices.begin(); idevice != devices.end(); idevice++) {
171  // Check device type
172  if ((*idevice)->getDeviceType() != PLL) {
173  continue;
174  }
175  // Cast to retrieve appropriate description object
176  pllDescription* desc = dynamic_cast<pllDescription*>(*idevice);
177  if (!desc) {
178  continue;
179  }
180  if (desc->getDelayCoarse() >= 15) {
181  continue;
182  }
183  // Retrieve device addresses from device description
185  // Construct key from device description
186  uint32_t fec_key =
187  SiStripFecKey(addr.fecCrate_, addr.fecSlot_, addr.fecRing_, addr.ccuAddr_, addr.ccuChan_, 0).key();
188  SiStripFecKey fec_path = SiStripFecKey(fec_key);
189  // Do it!
190  float delay = desc->getDelayCoarse() * 25 + desc->getDelayFine() * 25. / 24. + shift[addr.fecCrate_];
191  int delayCoarse = int(delay / 25);
192  int delayFine = int(round((delay - 25 * delayCoarse) * 24. / 25.));
193  if (delayFine == 24) {
194  delayFine = 0;
195  ++delayCoarse;
196  }
197  delayCoarse -= minCoarseDelay;
198  // maximum coarse setting
199  if (delayCoarse > 15) {
200  invalid.push_back(fec_key);
201  delayCoarse = sistrip::invalid_;
202  }
203  // Update PLL settings
204  if (delayCoarse != sistrip::invalid_ && delayFine != sistrip::invalid_) {
205  std::stringstream ss;
206  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
207  << " Updating coarse/fine PLL settings"
208  << " for Crate/FEC/slot/ring/CCU " << fec_path.fecCrate() << "/" << fec_path.fecSlot() << "/"
209  << fec_path.fecRing() << "/" << fec_path.ccuAddr() << "/" << fec_path.ccuChan() << " from "
210  << static_cast<uint16_t>(desc->getDelayCoarse()) << "/" << static_cast<uint16_t>(desc->getDelayFine());
211  desc->setDelayCoarse(delayCoarse);
212  desc->setDelayFine(delayFine);
213  updatedPLL++;
214  ss << " to " << static_cast<uint16_t>(desc->getDelayCoarse()) << "/"
215  << static_cast<uint16_t>(desc->getDelayFine());
216  LogTrace(mlDqmClient_) << ss.str();
217  }
218  }
219 
220  // Retrieve FED ids from cabling
221  auto ids = cabling()->fedIds();
222 
223  // loop over the FED ids to determine min and max values of coarse delay
224  uint16_t minDelay = 256;
225  uint16_t maxDelay = 0;
226  uint16_t fedDelayCoarse = 0;
227  for (auto ifed = feds.begin(); ifed != feds.end(); ifed++) {
228  // If FED id not found in list (from cabling), then continue
229  if (find(ids.begin(), ids.end(), (*ifed)->getFedId()) == ids.end()) {
230  continue;
231  }
232  auto conns = cabling()->fedConnections((*ifed)->getFedId());
233  // loop over the connections for that FED
234  for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
235  // check that this is a tracker module
236  if (DetId(iconn->detId()).det() != DetId::Tracker)
237  continue;
238  // build the Fed9UAddress for that channel. Used to update the description.
239  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
240  // retreive the current value for the delays
241  fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
242  // update min and max
243  minDelay = minDelay < fedDelayCoarse ? minDelay : fedDelayCoarse;
244  maxDelay = maxDelay > fedDelayCoarse ? maxDelay : fedDelayCoarse;
245  }
246  }
247 
248  // compute the FED coarse global offset
249  int offset = (10 - minDelay) * 25; // try to ensure 10BX room for later fine delay scan
250  if (maxDelay + (offset / 25) > MAXFEDCOARSE)
251  offset = (MAXFEDCOARSE - maxDelay) * 25; // otherwise, take the largest possible
252 
253  // loop over the FED ids
254  for (SiStripConfigDb::FedDescriptionsV::const_iterator ifed = feds.begin(); ifed != feds.end(); ifed++) {
255  // If FED id not found in list (from cabling), then continue
256  if (find(ids.begin(), ids.end(), (*ifed)->getFedId()) == ids.end()) {
257  continue;
258  }
259  auto conns = cabling()->fedConnections((*ifed)->getFedId());
260  // loop over the connections for that FED
261  for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
262  // check that this is a tracker module
263  if (DetId(iconn->detId()).det() != DetId::Tracker)
264  continue;
265  // build the Fed9UAddress for that channel. Used to update the description.
266  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
267  // retreive the current value for the delays
268  int fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
269  int fedDelayFine = (*ifed)->getFineDelay(fedChannel);
270  // compute the FED delay
271  // this is done by substracting the best (PLL) delay to the present value (from the db)
272  int fedDelay = int(fedDelayCoarse * 25. - fedDelayFine * 24. / 25. - round(shift[iconn->fecCrate()]) + offset);
273  fedDelayCoarse = (fedDelay / 25) + 1;
274  fedDelayFine = fedDelayCoarse * 25 - fedDelay;
275  if (fedDelayFine == 25) {
276  fedDelayFine = 0;
277  --fedDelayCoarse;
278  }
279  // update the FED delay
280  std::stringstream ss;
281  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
282  << " Updating the FED delay"
283  << " for loop FED id/ch " << (*ifed)->getFedId() << "/" << iconn->fedCh() << " from "
284  << (*ifed)->getCoarseDelay(fedChannel) << "/" << (*ifed)->getFineDelay(fedChannel) << " to ";
285  (*ifed)->setDelay(fedChannel, fedDelayCoarse, fedDelayFine);
286  ss << (*ifed)->getCoarseDelay(fedChannel) << "/" << (*ifed)->getFineDelay(fedChannel);
287  LogTrace(mlDqmClient_) << ss.str();
288  }
289  }
290 
291  // Summary output
292  edm::LogVerbatim(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "]"
293  << " Updated FED delays for " << ids.size() << " FEDs!";
294 
295  // Check if invalid settings were found
296  if (!invalid.empty()) {
297  std::stringstream ss;
298  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
299  << " Found PLL coarse setting of 15"
300  << " (not allowed!) for following channels"
301  << " (Crate/FEC/slot/ring/CCU/LLD): ";
302  std::vector<SiStripFecKey>::iterator ikey = invalid.begin();
303  std::vector<SiStripFecKey>::iterator jkey = invalid.end();
304  for (; ikey != jkey; ++ikey) {
305  ss << ikey->fecCrate() << "/" << ikey->fecSlot() << "/" << ikey->fecRing() << "/" << ikey->ccuAddr() << "/"
306  << ikey->ccuChan() << ", ";
307  }
308  edm::LogWarning(mlDqmClient_) << ss.str();
309  return false;
310  }
311 
312  // Summary output
313  edm::LogVerbatim(mlDqmClient_) << "[LatencyHistosUsingDb::" << __func__ << "] "
314  << "Updated settings for " << updatedAPV << " APV devices and " << updatedPLL
315  << " PLL devices.";
316  return true;
317 }
318 
319 // -----------------------------------------------------------------------------
322  SamplingAnalysis* anal = dynamic_cast<SamplingAnalysis*>(analysis->second);
323  if (!anal) {
324  return;
325  }
326 
327  SiStripFecKey fec_key(anal->fecKey()); //@@ analysis->first
328  SiStripFedKey fed_key(anal->fedKey());
329 
330  uint16_t latency = static_cast<uint16_t>((anal->maximum() / (-25.)) + 0.5);
331 
332  ApvLatencyAnalysisDescription* tmp;
333  tmp = new ApvLatencyAnalysisDescription(latency,
334  0,
335  0,
336  0,
337  0,
338  0,
339  0,
340  db()->dbParams().partitions().begin()->second.partitionName(),
341  db()->dbParams().partitions().begin()->second.runNumber(),
342  anal->isValid(),
343  "",
344  fed_key.fedId(),
345  fed_key.feUnit(),
346  fed_key.feChan(),
347  fed_key.fedApv());
348 
349  // Add comments
350  typedef std::vector<std::string> Strings;
351  Strings errors = anal->getErrorCodes();
352  Strings::const_iterator istr = errors.begin();
353  Strings::const_iterator jstr = errors.end();
354  for (; istr != jstr; ++istr) {
355  tmp->addComments(*istr);
356  }
357 
358  // Store description
359  desc.push_back(tmp);
360 }
361 
364  perPartition_ = this->pset().getParameter<bool>("OptimizePerPartition");
365 }
def upload(url, args, files)
T getParameter(std::string const &) const
Analysis for latency run.
const uint32_t & fedKey() const
Analyses & data(bool getMaskedData=false)
const uint16_t & fecRing() const
A container class for generic run and event-related info, information required by the commissioning a...
Definition: SiStripFedKey.h:56
const edm::ParameterSet & pset() const
virtual bool isValid() const
FedDescriptionsRange getFedDescriptions(std::string partition="")
std::vector< std::string > Strings
Definition: MsgTools.h:18
bool update(SiStripConfigDb::DeviceDescriptionsRange, SiStripConfigDb::FedDescriptionsRange)
FedDescriptions::range FedDescriptionsRange
static const char mlDqmClient_[]
void uploadConfigurations() override
const uint16_t & fecSlot() const
void configure(const edm::ParameterSet &, const edm::EventSetup &) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
constexpr int32_t ceil(float num)
sistrip classes
const uint32_t & key() const
Definition: SiStripKey.h:120
Utility class that identifies a position within the strip tracker control structure, down to the level of an APV25.
Definition: SiStripFecKey.h:45
U second(std::pair< T, U > const &p)
const_iterator_range partitions() const
#define MAXFEDCOARSE
FedsConstIterRange fedIds() const
DeviceDescriptions::range DeviceDescriptionsRange
An interface class to the DeviceFactory.
const uint32_t & fecKey() const
DeviceAddress deviceAddress(const deviceDescription &)
#define LogTrace(id)
LatencyHistosUsingDb(const edm::ParameterSet &pset, DQMStore *, SiStripConfigDb *const)
const uint16_t & fecCrate() const
Definition: DetId.h:17
void create(SiStripConfigDb::AnalysisDescriptionsV &, Analysis) override
const SiStripDbParams & dbParams() const
void configure(const edm::ParameterSet &, const edm::EventSetup &) override
void uploadFedDescriptions(std::string partition="")
const uint16_t & ccuAddr() const
static const uint16_t invalid_
Definition: Constants.h:16
ConnsConstIterRange fedConnections(uint16_t fed_id) const
#define begin
Definition: vmac.h:32
HLT enums.
std::vector< AnalysisDescription * > AnalysisDescriptionsV
static unsigned int const shift
const uint16_t & ccuChan() const
const float & maximum() const
Definition: errors.py:1
SiStripConfigDb *const db() const
DeviceDescriptionsRange getDeviceDescriptions(std::string partition="")
void uploadDeviceDescriptions(std::string partition="")
tmp
align.sh
Definition: createJobs.py:716
SiStripFedCabling *const cabling() const
const VString & getErrorCodes() const