Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-02 23:48:47

0001 #include "DQM/SiStripCommissioningSources/interface/SiStripCommissioningSource.h"
0002 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
0003 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0004 #include "DQM/SiStripCommissioningSources/interface/ApvTimingTask.h"
0005 #include "DQM/SiStripCommissioningSources/interface/Averages.h"
0006 #include "DQM/SiStripCommissioningSources/interface/CalibrationScanTask.h"
0007 #include "DQM/SiStripCommissioningSources/interface/CalibrationTask.h"
0008 #include "DQM/SiStripCommissioningSources/interface/DaqScopeModeTask.h"
0009 #include "DQM/SiStripCommissioningSources/interface/FastFedCablingTask.h"
0010 #include "DQM/SiStripCommissioningSources/interface/FedCablingTask.h"
0011 #include "DQM/SiStripCommissioningSources/interface/FedTimingTask.h"
0012 #include "DQM/SiStripCommissioningSources/interface/FineDelayTask.h"
0013 #include "DQM/SiStripCommissioningSources/interface/LatencyTask.h"
0014 #include "DQM/SiStripCommissioningSources/interface/NoiseTask.h"
0015 #include "DQM/SiStripCommissioningSources/interface/OptoScanTask.h"
0016 #include "DQM/SiStripCommissioningSources/interface/PedestalsTask.h"
0017 #include "DQM/SiStripCommissioningSources/interface/PedsFullNoiseTask.h"
0018 #include "DQM/SiStripCommissioningSources/interface/PedsOnlyTask.h"
0019 #include "DQM/SiStripCommissioningSources/interface/VpspScanTask.h"
0020 #include "DQMServices/Core/interface/DQMStore.h"
0021 #include "DataFormats/Common/interface/Handle.h"
0022 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0023 #include "DataFormats/SiStripCommon/interface/SiStripEventSummary.h"
0024 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0025 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/ServiceRegistry/interface/Service.h"
0031 #include "FWCore/Utilities/interface/Exception.h"
0032 #include <ctime>
0033 #include <iomanip>
0034 #include <memory>
0035 #include <sstream>
0036 
0037 #include <sys/types.h>
0038 #include <unistd.h>
0039 #include <iomanip>
0040 
0041 #include <arpa/inet.h>
0042 #include <cstdio>
0043 #include <netdb.h>
0044 #include <sys/socket.h>
0045 #include <sys/unistd.h>
0046 #include <cstdint>
0047 
0048 using namespace sistrip;
0049 
0050 // -----------------------------------------------------------------------------
0051 //
0052 SiStripCommissioningSource::SiStripCommissioningSource(const edm::ParameterSet& pset)
0053     : dqm_(nullptr),
0054       fedCablingToken_(esConsumes<edm::Transition::BeginRun>()),
0055       fecCabling_(nullptr),
0056       inputModuleLabel_(pset.getParameter<std::string>("InputModuleLabel")),
0057       inputModuleLabelAlt_(pset.existsAs<std::string>("InputModuleLabelAlt")
0058                                ? pset.getParameter<std::string>("InputModuleLabelAlt")
0059                                : ""),
0060       inputModuleLabelSummary_(pset.getParameter<std::string>("SummaryInputModuleLabel")),
0061       inputClusterLabel_(
0062           pset.existsAs<std::string>("InputClusterLabel") ? pset.getParameter<std::string>("InputClusterLabel") : ""),
0063       filename_(pset.getUntrackedParameter<std::string>("RootFileName", sistrip::dqmSourceFileName_)),
0064       run_(0),
0065       partitionName_(pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName")
0066                                                                  : ""),
0067       time_(0),
0068       isSpy_(pset.existsAs<bool>("isSpy") ? pset.getParameter<bool>("isSpy") : false),
0069       taskConfigurable_(pset.getUntrackedParameter<std::string>("CommissioningTask", "UNDEFINED")),
0070       task_(sistrip::UNDEFINED_RUN_TYPE),
0071       tasks_(),
0072       cablingTasks_(),
0073       tasksExist_(false),
0074       cablingTask_(false),
0075       updateFreq_(pset.getUntrackedParameter<int>("HistoUpdateFreq", 1)),
0076       base_(""),
0077       view_(pset.getUntrackedParameter<std::string>("View", "Default")),
0078       parameters_(pset) {
0079   usesResource("DQMStore");
0080   inputModuleSummaryToken_ = consumes<SiStripEventSummary>(edm::InputTag(inputModuleLabelSummary_));
0081   digiVirginRawToken_ = mayConsume<edm::DetSetVector<SiStripRawDigi> >(edm::InputTag(inputModuleLabel_, "VirginRaw"));
0082   digiFineDelaySelectionToken_ =
0083       mayConsume<edm::DetSetVector<SiStripRawDigi> >(edm::InputTag(inputModuleLabel_, "FineDelaySelection"));
0084   digiReorderedToken_ = mayConsume<edm::DetSetVector<SiStripRawDigi> >(edm::InputTag(inputModuleLabel_, "Reordered"));
0085   /////////////////
0086   if (not isSpy_)
0087     digiScopeModeToken_ = mayConsume<edm::DetSetVector<SiStripRawDigi> >(edm::InputTag(inputModuleLabel_, "ScopeMode"));
0088   else {
0089     digiScopeModeToken_ =
0090         mayConsume<edm::DetSetVector<SiStripRawDigi> >(edm::InputTag(inputModuleLabelAlt_, "ScopeRawDigis"));
0091     clustersToken_ = mayConsume<edmNew::DetSetVector<SiStripCluster> >(edm::InputTag(inputClusterLabel_));
0092   }
0093   /////////////////
0094   LogTrace(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0095                          << " Constructing object...";
0096   tasks_.clear();
0097   tasks_.resize(1024, VecOfTasks(96, static_cast<CommissioningTask*>(nullptr)));
0098 
0099   if (task_ == sistrip::NOISE) {
0100     noiseToken_ = esConsumes();
0101   }
0102   if (task_ == sistrip::NOISE || task_ == sistrip::CALIBRATION_SCAN || task_ == sistrip::CALIBRATION_SCAN_DECO ||
0103       task_ == sistrip::CALIBRATION || task_ == sistrip::CALIBRATION_DECO) {
0104     pedestalToken_ = esConsumes();
0105   }
0106 }
0107 
0108 // -----------------------------------------------------------------------------
0109 //
0110 SiStripCommissioningSource::~SiStripCommissioningSource() {
0111   LogTrace(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0112                          << " Destructing object...";
0113 }
0114 
0115 // -----------------------------------------------------------------------------
0116 //
0117 SiStripCommissioningSource::DQMStore* const SiStripCommissioningSource::dqm(std::string method) const {
0118   if (!dqm_) {
0119     std::stringstream ss;
0120     if (!method.empty()) {
0121       ss << "[SiStripCommissioningSource::" << method << "]" << std::endl;
0122     } else {
0123       ss << "[SiStripCommissioningSource]" << std::endl;
0124     }
0125     ss << " NULL pointer to DQMStore";
0126     edm::LogWarning(mlDqmSource_) << ss.str();
0127     return nullptr;
0128   } else {
0129     return dqm_;
0130   }
0131 }
0132 
0133 // -----------------------------------------------------------------------------
0134 // Retrieve DQM interface, control cabling and "control view" utility
0135 // class, create histogram directory structure and generate "reverse"
0136 // control cabling.
0137 void SiStripCommissioningSource::beginRun(edm::Run const& run, const edm::EventSetup& setup) {
0138   LogTrace(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0139                          << " Configuring..." << std::endl;
0140 
0141   // ---------- DQM back-end interface ----------
0142 
0143   dqm_ = edm::Service<DQMStore>().operator->();
0144   edm::LogInfo(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0145                              << " DQMStore service: " << dqm_;
0146   dqm(__func__);
0147 
0148   // ---------- Base directory ----------
0149 
0150   std::stringstream dir("");
0151   base_ = dir.str();
0152 
0153   // ---------- FED and FEC cabling ----------
0154 
0155   const auto& fed_cabling = setup.getData(fedCablingToken_);
0156   fedCabling_ = const_cast<SiStripFedCabling*>(&fed_cabling);
0157   LogDebug(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0158                          << "Initialized FED cabling. Number of FEDs is " << fedCabling_->fedIds().size();
0159   fecCabling_ = new SiStripFecCabling(fed_cabling);
0160   if (fecCabling_->crates().empty()) {
0161     std::stringstream ss;
0162     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0163        << " Empty std::vector returned by FEC cabling object!"
0164        << " Check if database connection failed...";
0165     edm::LogWarning(mlDqmSource_) << ss.str();
0166   }
0167 
0168   // ---------- Reset ----------
0169 
0170   tasksExist_ = false;
0171   task_ = sistrip::UNDEFINED_RUN_TYPE;
0172   cablingTask_ = false;
0173 
0174   remove();
0175 
0176   clearCablingTasks();
0177   clearTasks();
0178 }
0179 
0180 // -----------------------------------------------------------------------------
0181 //
0182 void SiStripCommissioningSource::endJob() {
0183   LogTrace(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0184                          << " Halting..." << std::endl;
0185 
0186   // ---------- Update histograms ----------
0187   // Cabling task
0188   for (TaskMap::iterator itask = cablingTasks_.begin(); itask != cablingTasks_.end(); itask++) {
0189     if (itask->second) {
0190       itask->second->updateHistograms();
0191     }
0192   }
0193 
0194   if (task_ == sistrip::APV_LATENCY) {
0195     for (uint16_t partition = 0; partition < 4; ++partition) {
0196       tasks_[0][partition]->updateHistograms();
0197     }
0198   } else if (task_ == sistrip::FINE_DELAY) {
0199     tasks_[0][0]->updateHistograms();
0200   } else {
0201     // All tasks except cabling
0202     uint16_t fed_id = 0;
0203     uint16_t fed_ch = 0;
0204     auto ifed = fedCabling_->fedIds().begin();
0205     for (; ifed != fedCabling_->fedIds().end(); ifed++) {
0206       auto conns = fedCabling_->fedConnections(*ifed);
0207       for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
0208         if (!iconn->isConnected()) {
0209           continue;
0210         }
0211         fed_id = iconn->fedId();
0212         fed_ch = iconn->fedCh();
0213         if (tasks_[fed_id][fed_ch]) {
0214           tasks_[fed_id][fed_ch]->updateHistograms();
0215           delete tasks_[fed_id][fed_ch];
0216         }
0217       }
0218     }
0219   }
0220   // ---------- Save histos to root file ----------
0221 
0222   // Strip filename of ".root" extension
0223   std::string name;
0224   if (filename_.find(".root", 0) == std::string::npos) {
0225     name = filename_;
0226   } else {
0227     name = filename_.substr(0, filename_.find(".root", 0));
0228   }
0229 
0230   // Retrieve SCRATCH directory
0231   std::string scratch = "SCRATCH";  //@@ remove trailing slash!!!
0232   std::string dir = "";
0233   if (std::getenv(scratch.c_str()) != nullptr) {
0234     dir = std::getenv(scratch.c_str());
0235   }
0236 
0237   // Add directory path
0238   std::stringstream ss;
0239   if (!dir.empty()) {
0240     ss << dir << "/";
0241   } else {
0242     ss << "/tmp/";
0243   }
0244 
0245   // Add filename with run number, host ip, pid and .root extension
0246   ss << name << "_";
0247   if (task_ == sistrip::DAQ_SCOPE_MODE and not partitionName_.empty())  // only for spy-runs
0248     ss << partitionName_ << "_";
0249 
0250   directory(ss, run_);
0251   ss << ".root";
0252 
0253   // Save file with appropriate filename (if run number is known)
0254   if (!filename_.empty()) {
0255     if (run_ != 0) {
0256       dqm()->save(ss.str());
0257     } else {
0258       edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0259                                     << " NULL value for RunNumber! No root file saved!";
0260     }
0261   } else {
0262     edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0263                                   << " NULL value for filename! No root file saved!";
0264   }
0265 
0266   LogTrace(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0267                          << " Saved all histograms to file \"" << ss.str() << "\"";
0268 
0269   // ---------- Delete histograms ----------
0270   // Remove all MonitorElements in "SiStrip" dir and below
0271   // remove();
0272 
0273   // Delete histogram objects
0274   // clearCablingTasks();
0275   // clearTasks();
0276 
0277   // ---------- Delete cabling ----------
0278   if (fedCabling_) {
0279     fedCabling_ = nullptr;
0280   }
0281   if (fecCabling_) {
0282     delete fecCabling_;
0283     fecCabling_ = nullptr;
0284   }
0285 }
0286 
0287 // ----------------------------------------------------------------------------
0288 //
0289 void SiStripCommissioningSource::analyze(const edm::Event& event, const edm::EventSetup& setup) {
0290   // Retrieve commissioning information from "event summary"
0291   edm::Handle<SiStripEventSummary> summary;
0292   //  event.getByLabel( inputModuleLabelSummary_, summary );
0293   event.getByToken(inputModuleSummaryToken_, summary);
0294 
0295   // Check if EventSummary has info attached
0296   if ((summary->runType() == sistrip::UNDEFINED_RUN_TYPE || summary->runType() == sistrip::UNKNOWN_RUN_TYPE) &&
0297       summary->nullParams()) {
0298     edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0299                                   << " Unknown/undefined RunType and NULL parameter values!"
0300                                   << " It may be that the 'trigger FED' object was not found!";
0301   }
0302 
0303   // Check if need to rebuild FED/FEC cabling objects for connection run
0304   //cablingForConnectionRun( summary->runType() ); //@@ do not use!
0305 
0306   // Extract run number and forward to client
0307   if (event.id().run() != run_) {
0308     run_ = event.id().run();
0309     createRunNumber();
0310   }
0311 
0312   // Coarse event rate counter
0313   if (!(event.id().event() % updateFreq_)) {
0314     std::stringstream ss;
0315     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0316        << " The last " << updateFreq_ << " events were processed at a rate of ";
0317     if (time(nullptr) == time_) {
0318       ss << ">" << updateFreq_ << " Hz";
0319     } else {
0320       ss << (updateFreq_ / (time(nullptr) - time_)) << " Hz";
0321     }
0322     edm::LogVerbatim(mlDqmSource_) << ss.str();
0323     time_ = time(nullptr);
0324   }
0325 
0326   // Create commissioning task objects
0327   if (!tasksExist_) {
0328     createTask(summary.product(), setup);
0329   } else {
0330     for (auto& v : tasks_) {
0331       for (auto& t : v) {
0332         t->eventSetup(&setup);
0333       }
0334     }
0335   }
0336 
0337   // Retrieve raw digis with mode appropriate to task
0338   edm::Handle<edm::DetSetVector<SiStripRawDigi> > raw;
0339   edm::Handle<edm::DetSetVector<SiStripRawDigi> > rawAlt;
0340   edm::Handle<edmNew::DetSetVector<SiStripCluster> > cluster;
0341 
0342   if (task_ == sistrip::DAQ_SCOPE_MODE) {  // scop-mode runs
0343     if (not isSpy_ and
0344         summary->fedReadoutMode() == FED_VIRGIN_RAW) {  // if the readout is virgin raw just take the VR digis
0345       event.getByToken(digiVirginRawToken_, raw);
0346     } else if (not isSpy_ and summary->fedReadoutMode() == FED_SCOPE_MODE) {
0347       event.getByToken(digiScopeModeToken_, raw);
0348     } else if (isSpy_) {  // spy case take both re-ordered and scope mode
0349       event.getByToken(digiScopeModeToken_, rawAlt);
0350       event.getByToken(digiReorderedToken_, raw);
0351       if (not inputClusterLabel_.empty())
0352         event.getByToken(clustersToken_, cluster);
0353     } else {
0354       std::stringstream ss;
0355       ss << "[SiStripCommissioningSource::" << __func__ << "]"
0356          << " Requested DAQ_SCOPE_MODE but unknown FED"
0357          << " readout mode retrieved from SiStripEventSummary: "
0358          << SiStripEnumsAndStrings::fedReadoutMode(summary->fedReadoutMode());
0359       edm::LogWarning(mlDqmSource_) << ss.str();
0360     }
0361   } else if (task_ == sistrip::FAST_CABLING || task_ == sistrip::FED_CABLING || task_ == sistrip::APV_TIMING ||
0362              task_ == sistrip::FED_TIMING || task_ == sistrip::OPTO_SCAN) {
0363     event.getByToken(digiScopeModeToken_, raw);
0364   } else if (task_ == sistrip::VPSP_SCAN || task_ == sistrip::CALIBRATION || task_ == sistrip::CALIBRATION_DECO ||
0365              task_ == sistrip::CALIBRATION_SCAN || task_ == sistrip::CALIBRATION_SCAN_DECO ||
0366              task_ == sistrip::PEDESTALS || task_ == sistrip::PEDS_ONLY || task_ == sistrip::NOISE ||
0367              task_ == sistrip::PEDS_FULL_NOISE) {
0368     event.getByToken(digiVirginRawToken_, raw);
0369   } else if (task_ == sistrip::APV_LATENCY || task_ == sistrip::FINE_DELAY) {
0370     event.getByToken(digiFineDelaySelectionToken_, raw);
0371   } else {
0372     std::stringstream ss;
0373     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0374        << " Unknown CommissioningTask: " << SiStripEnumsAndStrings::runType(task_)
0375        << " Unable to establish FED readout mode and retrieve digi container!"
0376        << " Check if SiStripEventSummary object is found/present in Event";
0377     edm::LogWarning(mlDqmSource_) << ss.str();
0378     return;
0379   }
0380 
0381   // Check for NULL pointer to digi container
0382   if (not raw.isValid()) {
0383     std::stringstream ss;
0384     ss << "[SiStripCommissioningSource::" << __func__ << "]" << std::endl
0385        << " NULL pointer to DetSetVector!" << std::endl
0386        << " Unable to fill histograms!";
0387     edm::LogWarning(mlDqmSource_) << ss.str();
0388     return;
0389   }
0390 
0391   if (isSpy_ and not inputModuleLabelAlt_.empty() and not rawAlt.isValid()) {
0392     std::stringstream ss;
0393     ss << "[SiStripCommissioningSource::" << __func__ << "]" << std::endl
0394        << " NULL pointer to DetSetVector!" << std::endl
0395        << " Unable to fill histograms!";
0396     edm::LogWarning(mlDqmSource_) << ss.str();
0397     return;
0398   }
0399 
0400   if (isSpy_ and not inputClusterLabel_.empty() and not cluster.isValid()) {
0401     std::stringstream ss;
0402     ss << "[SiStripCommissioningSource::" << __func__ << "]" << std::endl
0403        << " NULL pointer to DetSetVector!" << std::endl
0404        << " Unable to fill histograms!";
0405     edm::LogWarning(mlDqmSource_) << ss.str();
0406     return;
0407   }
0408 
0409   if (!cablingTask_) {
0410     fillHistos(summary.product(), *raw, *rawAlt, *cluster);
0411   } else {
0412     fillCablingHistos(summary.product(), *raw);
0413   }
0414 }
0415 
0416 // ----------------------------------------------------------------------------
0417 //
0418 void SiStripCommissioningSource::fillCablingHistos(const SiStripEventSummary* const summary,
0419                                                    const edm::DetSetVector<SiStripRawDigi>& raw) {
0420   // Create FEC key using DCU id and LLD channel from SiStripEventSummary
0421   const SiStripModule& module = fecCabling_->module(summary->dcuId());
0422   uint16_t lld_channel = (summary->deviceId() & 0x3) + 1;
0423   SiStripFecKey key_object(module.key().fecCrate(),
0424                            module.key().fecSlot(),
0425                            module.key().fecRing(),
0426                            module.key().ccuAddr(),
0427                            module.key().ccuChan(),
0428                            lld_channel);
0429   uint32_t fec_key = key_object.key();
0430   std::stringstream sss;
0431   sss << "[SiStripCommissioningSource::" << __func__ << "]"
0432       << " Found DcuId 0x" << std::hex << std::setw(8) << std::setfill('0') << summary->dcuId() << std::dec
0433       << " with Crate/FEC/Ring/CCU/Module/LLD: " << module.key().fecCrate() << "/" << module.key().fecSlot() << "/"
0434       << module.key().fecRing() << "/" << module.key().ccuAddr() << "/" << module.key().ccuChan() << "/" << lld_channel;
0435   edm::LogWarning(mlDqmSource_) << sss.str();
0436 
0437   //LogTrace(mlTest_) << "TEST : " << key_object;
0438 
0439   // Check on whether DCU id is found
0440   if (key_object.isInvalid(sistrip::CCU_CHAN)) {
0441     std::stringstream ss;
0442     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0443        << " DcuId 0x" << std::hex << std::setw(8) << std::setfill('0') << summary->dcuId() << std::dec
0444        << " in 'DAQ register' field not found in cabling map!"
0445        << " (NULL values returned for FEC path)";
0446     edm::LogWarning(mlDqmSource_) << ss.str();
0447     return;
0448   }
0449 
0450   // Iterate through FED ids
0451   for (auto ifed = fedCabling_->fedIds().begin(); ifed != fedCabling_->fedIds().end(); ifed++) {
0452     // Check if FedId is non-zero
0453     if (*ifed == sistrip::invalid_) {
0454       continue;
0455     }
0456 
0457     // Container to hold median signal level for FED cabling task
0458     std::map<uint16_t, float> medians;
0459     medians.clear();
0460     std::map<uint16_t, float> medians1;
0461     medians1.clear();
0462 
0463     // Iterate through FED channels
0464     for (uint16_t ichan = 0; ichan < 96; ichan++) {
0465       // Retrieve digis for given FED key
0466       uint32_t fed_key = ((*ifed & sistrip::invalid_) << 16) | (ichan & sistrip::invalid_);
0467 
0468       std::vector<edm::DetSet<SiStripRawDigi> >::const_iterator digis = raw.find(fed_key);
0469       if (digis != raw.end()) {
0470         if (digis->data.empty()) {
0471           continue;
0472         }
0473 
0474         Averages ave;
0475         for (uint16_t idigi = 0; idigi < digis->data.size(); idigi++) {
0476           ave.add(static_cast<uint32_t>(digis->data[idigi].adc()));
0477         }
0478         Averages::Params params;
0479         ave.calc(params);
0480         medians[ichan] = params.median_;  // Store median signal level
0481         medians1[ichan] = digis->data[0].adc();
0482       }
0483 
0484     }  // fed channel loop
0485 
0486     // Calculate mean and spread on all (median) signal levels
0487     Averages average;
0488     std::map<uint16_t, float>::const_iterator ii = medians.begin();
0489     for (; ii != medians.end(); ii++) {
0490       average.add(ii->second);
0491     }
0492     Averages::Params tmp;
0493     average.calc(tmp);
0494 
0495     std::stringstream ss;
0496     ss << "FED Averages:" << std::endl
0497        << "  nChans: " << medians.size() << std::endl
0498        << "  num/mean/median/rms/max/min: " << tmp.num_ << "/" << tmp.mean_ << "/" << tmp.median_ << "/" << tmp.rms_
0499        << "/" << tmp.max_ << "/" << tmp.min_ << std::endl;
0500     LogTrace(mlDqmSource_) << ss.str();
0501 
0502     // Calculate mean and spread on "filtered" data
0503     Averages truncated;
0504     std::map<uint16_t, float>::const_iterator jj = medians.begin();
0505     for (; jj != medians.end(); jj++) {
0506       if (jj->second < tmp.median_ + tmp.rms_) {
0507         truncated.add(jj->second);
0508       }
0509     }
0510     Averages::Params params;
0511     truncated.calc(params);
0512 
0513     std::stringstream ss1;
0514     ss1 << "Truncated Averages:" << std::endl
0515         << "  nChans: " << medians.size() << std::endl
0516         << "  num/mean/median/rms/max/min: " << params.num_ << "/" << params.mean_ << "/" << params.median_ << "/"
0517         << params.rms_ << "/" << params.max_ << "/" << params.min_ << std::endl;
0518     LogTrace(mlDqmSource_) << ss1.str();
0519 
0520     // Identify channels with signal
0521     std::stringstream ss2;
0522     std::stringstream ss3;
0523     std::map<uint16_t, float> channels;
0524     std::map<uint16_t, float>::const_iterator ichan = medians.begin();
0525     for (; ichan != medians.end(); ichan++) {
0526       if (ichan->second > 200.) {
0527         LogTrace(mlTest_) << "TEST FOUND SIGNAL HIGH: " << *ifed << " " << ichan->first << " " << ichan->second;
0528         channels[ichan->first] = ichan->second;
0529       }
0530       ss2  //<< ichan->first << "/"
0531           << ichan->second << " ";
0532       ss3  //<< ichan->first << "/"
0533           << medians1[ichan->first] << " ";
0534     }
0535 
0536     ss2 << std::endl;
0537     ss3 << std::endl;
0538     LogTrace(mlTest_) << "DUMP for FED  " << *ifed << ": " << ss2.str();
0539     LogTrace(mlTest_) << "FIRST ADC VAL " << *ifed << ": " << ss3.str();
0540 
0541     // Fill cabling histograms
0542     if (cablingTasks_.find(fec_key) != cablingTasks_.end()) {
0543       if (!channels.empty()) {
0544         cablingTasks_[fec_key]->fillHistograms(*summary, *ifed, channels);
0545         SiStripFecKey path(fec_key);
0546         std::stringstream ss;
0547         ss << "[SiStripCommissioningSource::" << __func__ << "]"
0548            << " Filled histogram for '" << cablingTasks_[fec_key]->myName() << "' object with FecKey: 0x" << std::hex
0549            << std::setfill('0') << std::setw(8) << fec_key << std::dec
0550            << " and Crate/FEC/ring/CCU/module/LLDchan: " << path.fecCrate() << "/" << path.fecSlot() << "/"
0551            << path.fecRing() << "/" << path.ccuAddr() << "/" << path.ccuChan() << "/" << path.channel();
0552         LogTrace(mlDqmSource_) << ss.str();
0553       }
0554     } else {
0555       SiStripFecKey path(fec_key);
0556       std::stringstream ss;
0557       ss << "[SiStripCommissioningSource::" << __func__ << "]"
0558          << " Unable to find CommissioningTask object with FecKey: 0x" << std::hex << std::setfill('0') << std::setw(8)
0559          << fec_key << std::dec << " and Crate/FEC/ring/CCU/module/LLDchan: " << path.fecCrate() << "/"
0560          << path.fecSlot() << "/" << path.fecRing() << "/" << path.ccuAddr() << "/" << path.ccuChan() << "/"
0561          << path.channel();
0562       edm::LogWarning(mlDqmSource_) << ss.str();
0563     }
0564 
0565   }  // fed id loop
0566 }
0567 
0568 // ----------------------------------------------------------------------------
0569 //
0570 void SiStripCommissioningSource::fillHistos(const SiStripEventSummary* const summary,
0571                                             const edm::DetSetVector<SiStripRawDigi>& raw,
0572                                             const edm::DetSetVector<SiStripRawDigi>& rawAlt,
0573                                             const edmNew::DetSetVector<SiStripCluster>& clusters) {
0574   // Iterate through FED ids and channels
0575   std::vector<uint16_t> stripOnClusters;
0576   auto ifed = fedCabling_->fedIds().begin();
0577   for (; ifed != fedCabling_->fedIds().end(); ifed++) {
0578     // Iterate through connected FED channels
0579     auto conns = fedCabling_->fedConnections(*ifed);
0580     for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
0581       if (!(iconn->fedId()) || iconn->fedId() > sistrip::valid_) {
0582         continue;
0583       }
0584       if (!iconn->isConnected()) {
0585         continue;
0586       }
0587 
0588       // Create FED key and check if non-zero
0589       // note: the key is not computed using the same formula as in commissioning histograms.
0590       // beware that changes here must match changes in raw2digi and in SiStripFineDelayHit
0591       uint32_t fed_key = ((iconn->fedId() & sistrip::invalid_) << 16) | (iconn->fedCh() & sistrip::invalid_);
0592       // Retrieve digis for given FED key and check if found
0593       std::vector<edm::DetSet<SiStripRawDigi> >::const_iterator digis = raw.find(fed_key);
0594 
0595       // only for spy data-taking --> tick measurement
0596       std::vector<edm::DetSet<SiStripRawDigi> >::const_iterator digisAlt;
0597       if (not rawAlt.empty()) {
0598         digisAlt = rawAlt.find(fed_key);
0599         if (digisAlt == rawAlt.end())
0600           continue;
0601       }
0602 
0603       // find the strips belonging to the clusters connected to this APV pair
0604       stripOnClusters.clear();
0605       if (not clusters.empty()) {
0606         for (edmNew::DetSetVector<SiStripCluster>::const_iterator DSViter = clusters.begin(); DSViter != clusters.end();
0607              DSViter++) {
0608           if (DSViter->id() != iconn->detId())
0609             continue;  // select clusters on this module
0610           for (edmNew::DetSet<SiStripCluster>::const_iterator DSiter = DSViter->begin(); DSiter != DSViter->end();
0611                DSiter++) {  // loop on the clusters
0612             if (DSiter->firstStrip() >= iconn->apvPairNumber() * 256 and
0613                 DSiter->firstStrip() < (1 + iconn->apvPairNumber()) * 256) {  // found the right APV
0614               for (size_t istrip = 0; istrip < DSiter->amplitudes().size(); istrip++) {
0615                 stripOnClusters.push_back(DSiter->firstStrip() + istrip - iconn->apvPairNumber() * 256);
0616               }
0617             }
0618           }
0619         }
0620       }
0621 
0622       if (digis != raw.end()) {
0623         // tasks involving tracking have partition-level histos, so treat separately
0624         if (task_ == sistrip::APV_LATENCY) {
0625           if (tasks_[0][iconn->fecCrate() - 1]) {
0626             tasks_[0][iconn->fecCrate() - 1]->fillHistograms(*summary, *digis);
0627           } else {
0628             std::stringstream ss;
0629             ss << "[SiStripCommissioningSource::" << __func__ << "]"
0630                << " Unable to find CommissioningTask for FEC crate " << iconn->fecCrate()
0631                << ". Unable to fill histograms!";
0632             edm::LogWarning(mlDqmSource_) << ss.str();
0633           }
0634         } else if (task_ == sistrip::FINE_DELAY) {
0635           if (tasks_[0][0]) {
0636             tasks_[0][0]->fillHistograms(*summary, *digis);
0637           } else {
0638             std::stringstream ss;
0639             ss << "[SiStripCommissioningSource::" << __func__ << "]"
0640                << " Unable to find global CommissioningTask for FineDelay. Unable to fill histograms!";
0641             edm::LogWarning(mlDqmSource_) << ss.str();
0642           }
0643         } else {
0644           if (tasks_[iconn->fedId()][iconn->fedCh()]) {
0645             if (not rawAlt.empty() or digisAlt == rawAlt.end())
0646               tasks_[iconn->fedId()][iconn->fedCh()]->fillHistograms(*summary, *digis);
0647             else {  // for spy-data
0648               if (stripOnClusters.empty())
0649                 tasks_[iconn->fedId()][iconn->fedCh()]->fillHistograms(*summary, *digis, *digisAlt);
0650               else {
0651                 tasks_[iconn->fedId()][iconn->fedCh()]->fillHistograms(*summary, *digis, *digisAlt, stripOnClusters);
0652               }
0653             }
0654           } else {
0655             std::stringstream ss;
0656             ss << "[SiStripCommissioningSource::" << __func__ << "]"
0657                << " Unable to find CommissioningTask object with FED key " << std::hex << std::setfill('0')
0658                << std::setw(8) << fed_key << std::dec << " and FED id/ch " << iconn->fedId() << "/" << iconn->fedCh()
0659                << " Unable to fill histograms!";
0660             edm::LogWarning(mlDqmSource_) << ss.str();
0661           }
0662         }
0663       } else {
0664         // issue a warning only for standard runs, as latency and fine delay only deliver
0665         // pseudo zero-suppressed data
0666         if (task_ != sistrip::APV_LATENCY && task_ != sistrip::FINE_DELAY) {
0667           std::stringstream ss;
0668           ss << "[SiStripCommissioningSource::" << __func__ << "]"
0669              << " Unable to find any DetSet containing digis for FED key " << std::hex << std::setfill('0')
0670              << std::setw(8) << fed_key << std::dec << " and FED id/ch " << iconn->fedId() << "/" << iconn->fedCh();
0671           edm::LogWarning(mlDqmSource_) << ss.str();
0672         }
0673       }
0674     }  // fed channel loop
0675   }    // fed id loop
0676 }
0677 
0678 // -----------------------------------------------------------------------------
0679 //
0680 void SiStripCommissioningSource::createRunNumber() {
0681   // Set commissioning task to default ("undefined") value
0682   if (!run_) {
0683     edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0684                                   << " NULL run number!";
0685     return;
0686   }
0687 
0688   // Create MonitorElement that identifies run number
0689   dqm()->setCurrentFolder(base_ + sistrip::root_);
0690   std::stringstream run;
0691   run << run_;
0692   dqm()->bookString(std::string(sistrip::runNumber_) + sistrip::sep_ + run.str(), run.str());
0693 }
0694 
0695 // -----------------------------------------------------------------------------
0696 //
0697 void SiStripCommissioningSource::createTask(const SiStripEventSummary* const summary, const edm::EventSetup& setup) {
0698   // Set commissioning task to default ("undefined") value
0699   task_ = sistrip::UNDEFINED_RUN_TYPE;
0700 
0701   // Retrieve commissioning task from EventSummary
0702   if (summary) {
0703     task_ = summary->runType();
0704     std::stringstream ss;
0705     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0706        << " Identified CommissioningTask from EventSummary to be \"" << SiStripEnumsAndStrings::runType(task_) << "\"";
0707     LogTrace(mlDqmSource_) << ss.str();
0708   } else {
0709     task_ = sistrip::UNKNOWN_RUN_TYPE;
0710     std::stringstream ss;
0711     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0712        << " NULL pointer to SiStripEventSummary!"
0713        << " Check SiStripEventSummary is found/present in Event";
0714     edm::LogWarning(mlDqmSource_) << ss.str();
0715   }
0716 
0717   // Override task with ParameterSet configurable (if defined)
0718   sistrip::RunType configurable = SiStripEnumsAndStrings::runType(taskConfigurable_);
0719   if (configurable != sistrip::UNDEFINED_RUN_TYPE && configurable != sistrip::UNKNOWN_RUN_TYPE) {
0720     std::stringstream ss;
0721     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0722        << " Overriding CommissioningTask from EventSummary (\"" << SiStripEnumsAndStrings::runType(task_)
0723        << "\") with value retrieved from .cfg file (\"" << SiStripEnumsAndStrings::runType(configurable) << "\")!";
0724     LogTrace(mlDqmSource_) << ss.str();
0725     task_ = configurable;
0726   }
0727 
0728   // Create ME (std::string) that identifies commissioning task
0729   dqm()->setCurrentFolder(base_ + sistrip::root_);
0730   std::string task_str = SiStripEnumsAndStrings::runType(task_);
0731   dqm()->bookString(std::string(sistrip::taskId_) + sistrip::sep_ + task_str, task_str);
0732 
0733   // Check commissioning task is known / defined
0734   if (task_ == sistrip::UNKNOWN_RUN_TYPE || task_ == sistrip::UNDEFINED_RUN_TYPE) {
0735     std::stringstream ss;
0736     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0737        << " Unexpected CommissioningTask found (" << static_cast<uint16_t>(task_) << ") \""
0738        << SiStripEnumsAndStrings::runType(task_) << "\""
0739        << " Unexpected value found in SiStripEventSummary and/or cfg file"
0740        << " If SiStripEventSummary is not present in Event,"
0741        << " check 'CommissioningTask' configurable in cfg file";
0742     edm::LogWarning(mlDqmSource_) << ss.str();
0743     return;
0744   } else {
0745     std::stringstream ss;
0746     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0747        << " Identified CommissioningTask to be \"" << SiStripEnumsAndStrings::runType(task_) << "\"";
0748     LogTrace(mlDqmSource_) << ss.str();
0749   }
0750 
0751   // Check if commissioning task is FED cabling
0752   if (task_ == sistrip::FED_CABLING) {
0753     cablingTask_ = true;
0754   } else {
0755     cablingTask_ = false;
0756   }
0757 
0758   std::stringstream ss;
0759   ss << "[SiStripCommissioningSource::" << __func__ << "]"
0760      << " CommissioningTask: " << SiStripEnumsAndStrings::runType(summary->runType());
0761   LogTrace(mlDqmSource_) << ss.str();
0762 
0763   edm::LogVerbatim(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0764                                  << " Creating CommissioningTask objects and booking histograms...";
0765   if (cablingTask_) {
0766     createCablingTasks();
0767   } else {
0768     createTasks(task_, setup);
0769   }
0770   edm::LogVerbatim(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0771                                  << " Finished booking histograms!";
0772   tasksExist_ = true;
0773 }
0774 
0775 // -----------------------------------------------------------------------------
0776 //
0777 void SiStripCommissioningSource::createCablingTasks() {
0778   // Iterate through FEC cabling and create commissioning task objects
0779   uint16_t booked = 0;
0780   for (std::vector<SiStripFecCrate>::const_iterator icrate = fecCabling_->crates().begin();
0781        icrate != fecCabling_->crates().end();
0782        icrate++) {
0783     for (std::vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++) {
0784       for (std::vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end();
0785            iring++) {
0786         for (std::vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end();
0787              iccu++) {
0788           for (std::vector<SiStripModule>::const_iterator imodule = iccu->modules().begin();
0789                imodule != iccu->modules().end();
0790                imodule++) {
0791             // Build FEC key
0792             SiStripFecKey path(
0793                 icrate->fecCrate(), ifec->fecSlot(), iring->fecRing(), iccu->ccuAddr(), imodule->ccuChan());
0794 
0795             // Check if FEC key is invalid
0796             if (!path.isValid()) {
0797               continue;
0798             }
0799 
0800             // Set working directory prior to booking histograms
0801             std::string dir = base_ + path.path();
0802             dqm()->setCurrentFolder(dir);
0803 
0804             // Iterate through all APV pairs for this module
0805             for (uint16_t ipair = 0; ipair < imodule->nApvPairs(); ipair++) {
0806               // Retrieve active APV devices
0807               SiStripModule::PairOfU16 apvs = imodule->activeApvPair(imodule->lldChannel(ipair));
0808 
0809               // Create connection object to hold all relevant info
0810               FedChannelConnection conn(icrate->fecCrate(),
0811                                         ifec->fecSlot(),
0812                                         iring->fecRing(),
0813                                         iccu->ccuAddr(),
0814                                         imodule->ccuChan(),
0815                                         apvs.first,
0816                                         apvs.second,
0817                                         imodule->dcuId(),
0818                                         imodule->detId(),
0819                                         imodule->nApvPairs());
0820 
0821               // Define key encoding control path
0822               uint32_t key = SiStripFecKey(icrate->fecCrate(),
0823                                            ifec->fecSlot(),
0824                                            iring->fecRing(),
0825                                            iccu->ccuAddr(),
0826                                            imodule->ccuChan(),
0827                                            imodule->lldChannel(ipair))
0828                                  .key();
0829 
0830               // Check key is non zero
0831               if (!key) {
0832                 edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0833                                               << " Unexpected NULL value for FEC key!";
0834                 continue;
0835               }
0836 
0837               // Create cabling task objects if not already existing
0838               if (cablingTasks_.find(key) == cablingTasks_.end()) {
0839                 if (task_ == sistrip::FED_CABLING) {
0840                   cablingTasks_[key] = new FedCablingTask(dqm(), conn);
0841                 } else if (task_ == sistrip::UNDEFINED_RUN_TYPE) {
0842                   edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0843                                                 << " Undefined CommissioningTask"
0844                                                 << " Unable to create FedCablingTask object!";
0845                 } else if (task_ == sistrip::UNKNOWN_RUN_TYPE) {
0846                   edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0847                                                 << " Unknown CommissioningTask"
0848                                                 << " Unable to create FedCablingTask object!";
0849                 } else {
0850                   edm::LogWarning(mlDqmSource_)
0851                       << "[SiStripCommissioningSource::" << __func__ << "]"
0852                       << " Unexpected CommissioningTask: " << SiStripEnumsAndStrings::runType(task_)
0853                       << " Unable to create FedCablingTask object!";
0854                 }
0855 
0856                 // Check if key is found and, if so, book histos and set update freq
0857                 if (cablingTasks_.find(key) != cablingTasks_.end()) {
0858                   if (cablingTasks_[key]) {
0859                     cablingTasks_[key]->bookHistograms();
0860                     cablingTasks_[key]->updateFreq(1);  //@@ hardwired to update every event!!!
0861                     booked++;
0862                     std::stringstream ss;
0863                     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0864                        << " Booking histograms for '" << cablingTasks_[key]->myName() << "' object with key 0x"
0865                        << std::hex << std::setfill('0') << std::setw(8) << key << std::dec << " in directory \"" << dir
0866                        << "\"";
0867                     LogTrace(mlDqmSource_) << ss.str();
0868                   } else {
0869                     std::stringstream ss;
0870                     ss << "[SiStripCommissioningSource::" << __func__ << "]"
0871                        << " NULL pointer to CommissioningTask for key 0x" << std::hex << std::setfill('0')
0872                        << std::setw(8) << key << std::dec << " in directory " << dir << " Unable to book histograms!";
0873                     edm::LogWarning(mlDqmSource_) << ss.str();
0874                   }
0875                 } else {
0876                   std::stringstream ss;
0877                   ss << "[SiStripCommissioningSource::" << __func__ << "]"
0878                      << " Unable to find CommissioningTask for key 0x" << std::hex << std::setfill('0') << std::setw(8)
0879                      << key << std::dec << " in directory " << dir << " Unable to book histograms!";
0880                   edm::LogWarning(mlDqmSource_) << ss.str();
0881                 }
0882 
0883               } else {
0884                 std::stringstream ss;
0885                 ss << "[SiStripCommissioningSource::" << __func__ << "]"
0886                    << " CommissioningTask object already exists for key 0x" << std::hex << std::setfill('0')
0887                    << std::setw(8) << key << std::dec << " in directory " << dir
0888                    << " Unable to create FedCablingTask object!";
0889                 edm::LogWarning(mlDqmSource_) << ss.str();
0890               }
0891 
0892             }  // loop through apv pairs
0893           }    // loop through modules
0894         }      // loop through ccus
0895       }        // loop through rings
0896     }          // loop through fecs
0897   }            // loop through crates
0898 
0899   edm::LogVerbatim(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0900                                  << " Created " << booked << " CommissioningTask objects and booked histograms";
0901 }
0902 
0903 // -----------------------------------------------------------------------------
0904 //
0905 void SiStripCommissioningSource::createTasks(sistrip::RunType run_type, const edm::EventSetup& setup) {
0906   uint16_t booked = 0;
0907 
0908   // latency has partition-level histos, so treat separately
0909   if (task_ == sistrip::APV_LATENCY) {
0910     for (uint16_t partition = 0; partition < 4; ++partition) {
0911       // make a task for every partition; tracker-wide histo is shared
0912       tasks_[0][partition] = new LatencyTask(
0913           dqm(),
0914           FedChannelConnection(
0915               partition + 1, sistrip::invalid_, sistrip::invalid_, sistrip::invalid_, sistrip::invalid_));
0916       tasks_[0][partition]->eventSetup(&setup);
0917       tasks_[0][partition]->bookHistograms();
0918       tasks_[0][partition]->updateFreq(updateFreq_);
0919       booked++;
0920     }
0921 
0922     // fine-delay has 1 histo for the whole tracker, so treat separately
0923   } else if (task_ == sistrip::FINE_DELAY) {
0924     tasks_[0][0] = new FineDelayTask(dqm(), FedChannelConnection());
0925     tasks_[0][0]->eventSetup(&setup);
0926     tasks_[0][0]->bookHistograms();
0927     tasks_[0][0]->updateFreq(updateFreq_);
0928     booked++;
0929 
0930   } else {  // now do any other task
0931 
0932     // Iterate through FED ids and channels
0933     for (auto ifed = fedCabling_->fedIds().begin(); ifed != fedCabling_->fedIds().end(); ++ifed) {
0934       // Iterate through connected FED channels
0935       auto conns = fedCabling_->fedConnections(*ifed);
0936       for (auto iconn = conns.begin(); iconn != conns.end(); ++iconn) {
0937         // Create FEC key
0938         SiStripFecKey fec_key(
0939             iconn->fecCrate(), iconn->fecSlot(), iconn->fecRing(), iconn->ccuAddr(), iconn->ccuChan());
0940         // Create FED key and check if non-zero
0941         SiStripFedKey fed_key(
0942             iconn->fedId(), SiStripFedKey::feUnit(iconn->fedCh()), SiStripFedKey::feChan(iconn->fedCh()));
0943         if (!iconn->isConnected()) {
0944           continue;
0945         }
0946 
0947         // define the view in which to work and paths for histograms
0948         //   currently FecView (control view) and FedView (readout view)
0949         //   DetView (detector view) implementation has started
0950         // Set working directory prior to booking histograms
0951         std::stringstream dir;
0952         dir << base_;
0953         if (view_ == "Default") {  // default
0954           if (run_type == sistrip::FAST_CABLING) {
0955             dir << fed_key.path();  // cabling in fed view
0956           } else {
0957             dir << fec_key.path();  // all other runs in control view
0958           }
0959         } else if (view_ == "FecView") {
0960           dir << fec_key.path();
0961         } else if (view_ == "FedView") {
0962           dir << fed_key.path();
0963         } else if (view_ == "DetView") {
0964           // currently just by detid from the connection, which is empty...
0965           dir << sistrip::root_ << sistrip::dir_ << sistrip::detectorView_ << sistrip::dir_ << iconn->detId();
0966         } else {
0967           edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
0968                                         << " Invalid view " << view_ << std::endl
0969                                         << " Histograms will end up all in the top directory.";
0970         }  // end if view_ == ...
0971         dqm()->setCurrentFolder(dir.str());
0972 
0973         // Create commissioning task objects
0974         if (!tasks_[iconn->fedId()][iconn->fedCh()]) {
0975           if (task_ == sistrip::FAST_CABLING) {
0976             tasks_[iconn->fedId()][iconn->fedCh()] = new FastFedCablingTask(dqm(), *iconn);
0977           } else if (task_ == sistrip::APV_TIMING) {
0978             tasks_[iconn->fedId()][iconn->fedCh()] = new ApvTimingTask(dqm(), *iconn);
0979           } else if (task_ == sistrip::FED_TIMING) {
0980             tasks_[iconn->fedId()][iconn->fedCh()] = new FedTimingTask(dqm(), *iconn);
0981           } else if (task_ == sistrip::OPTO_SCAN) {
0982             tasks_[iconn->fedId()][iconn->fedCh()] = new OptoScanTask(dqm(), *iconn);
0983           } else if (task_ == sistrip::VPSP_SCAN) {
0984             tasks_[iconn->fedId()][iconn->fedCh()] = new VpspScanTask(dqm(), *iconn);
0985           } else if (task_ == sistrip::PEDESTALS) {
0986             tasks_[iconn->fedId()][iconn->fedCh()] = new PedestalsTask(dqm(), *iconn);
0987           } else if (task_ == sistrip::PEDS_ONLY) {
0988             tasks_[iconn->fedId()][iconn->fedCh()] = new PedsOnlyTask(dqm(), *iconn);
0989           } else if (task_ == sistrip::NOISE) {
0990             tasks_[iconn->fedId()][iconn->fedCh()] = new NoiseTask(dqm(), *iconn, pedestalToken_, noiseToken_);
0991           } else if (task_ == sistrip::PEDS_FULL_NOISE) {
0992             tasks_[iconn->fedId()][iconn->fedCh()] = new PedsFullNoiseTask(dqm(), *iconn, parameters_);
0993           } else if (task_ == sistrip::DAQ_SCOPE_MODE) {
0994             tasks_[iconn->fedId()][iconn->fedCh()] = new DaqScopeModeTask(dqm(), *iconn, parameters_);
0995           } else if (task_ == sistrip::CALIBRATION_SCAN || task_ == sistrip::CALIBRATION_SCAN_DECO) {
0996             tasks_[iconn->fedId()][iconn->fedCh()] =
0997                 new CalibrationScanTask(dqm(), *iconn, task_, filename_.c_str(), run_, setup.getData(pedestalToken_));
0998           } else if (task_ == sistrip::CALIBRATION || task_ == sistrip::CALIBRATION_DECO) {
0999             tasks_[iconn->fedId()][iconn->fedCh()] =
1000                 new CalibrationTask(dqm(), *iconn, task_, filename_.c_str(), run_, setup.getData(pedestalToken_));
1001           } else if (task_ == sistrip::UNDEFINED_RUN_TYPE) {
1002             edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
1003                                           << " Undefined CommissioningTask"
1004                                           << " Unable to create CommissioningTask object!";
1005           } else {
1006             edm::LogWarning(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
1007                                           << " Unknown CommissioningTask"
1008                                           << " Unable to create CommissioningTask object!";
1009           }
1010 
1011           // Check if fed_key is found and, if so, book histos and set update freq
1012           if (tasks_[iconn->fedId()][iconn->fedCh()]) {
1013             tasks_[iconn->fedId()][iconn->fedCh()]->eventSetup(&setup);
1014 
1015             if (task_ != sistrip::CALIBRATION_SCAN and task_ != sistrip::CALIBRATION_SCAN_DECO and
1016                 task_ != sistrip::CALIBRATION and task_ != sistrip::CALIBRATION_DECO)
1017               tasks_[iconn->fedId()][iconn->fedCh()]->bookHistograms();
1018             else {
1019               if (task_ == sistrip::CALIBRATION_SCAN or task_ == sistrip::CALIBRATION_SCAN_DECO)
1020                 static_cast<CalibrationScanTask*>(tasks_[iconn->fedId()][iconn->fedCh()])->setCurrentFolder(dir.str());
1021               else if (task_ == sistrip::CALIBRATION or task_ == sistrip::CALIBRATION_DECO)
1022                 static_cast<CalibrationTask*>(tasks_[iconn->fedId()][iconn->fedCh()])->setCurrentFolder(dir.str());
1023             }
1024             tasks_[iconn->fedId()][iconn->fedCh()]->updateFreq(updateFreq_);
1025             booked++;
1026           } else {
1027             std::stringstream ss;
1028             ss << "[SiStripCommissioningSource::" << __func__ << "]"
1029                << " NULL pointer to CommissioningTask for key 0x" << std::hex << std::setfill('0') << std::setw(8)
1030                << fed_key.key() << std::dec << " in directory " << dir.str() << " Unable to book histograms!";
1031             edm::LogWarning(mlDqmSource_) << ss.str();
1032           }
1033         } else {
1034           std::stringstream ss;
1035           ss << "[SiStripCommissioningSource::" << __func__ << "]"
1036              << " CommissioningTask object already exists for key 0x" << std::hex << std::setfill('0') << std::setw(8)
1037              << fed_key.key() << std::dec << " in directory " << dir.str()
1038              << " Unable to create CommissioningTask object!";
1039           edm::LogWarning(mlDqmSource_) << ss.str();
1040         }
1041       }  // loop over fed channels
1042     }    // loop over feds
1043   }      // end other tasks
1044   edm::LogVerbatim(mlDqmSource_) << "[SiStripCommissioningSource::" << __func__ << "]"
1045                                  << " Created " << booked << " CommissioningTask objects and booked histograms";
1046 }
1047 
1048 // ----------------------------------------------------------------------------
1049 //
1050 void SiStripCommissioningSource::clearCablingTasks() {
1051   if (cablingTasks_.empty()) {
1052     return;
1053   }
1054   for (TaskMap::iterator itask = cablingTasks_.begin(); itask != cablingTasks_.end(); itask++) {
1055     if (itask->second) {
1056       delete itask->second;
1057     }
1058   }
1059   cablingTasks_.clear();
1060 }
1061 
1062 // ----------------------------------------------------------------------------
1063 //
1064 void SiStripCommissioningSource::clearTasks() {
1065   if (tasks_.empty()) {
1066     return;
1067   }
1068   VecOfVecOfTasks::iterator ifed = tasks_.begin();
1069   for (; ifed != tasks_.end(); ifed++) {
1070     VecOfTasks::iterator ichan = ifed->begin();
1071     for (; ichan != ifed->end(); ichan++) {
1072       if (*ichan) {
1073         delete *ichan;
1074         *ichan = 0;
1075       }
1076     }
1077     ifed->resize(96, nullptr);
1078   }
1079   tasks_.resize(1024);
1080 }
1081 
1082 // ----------------------------------------------------------------------------
1083 //
1084 void SiStripCommissioningSource::remove() {
1085   // TODO: remove no longer supported in DQMStore.
1086 }
1087 
1088 // -----------------------------------------------------------------------------
1089 //
1090 void SiStripCommissioningSource::directory(std::stringstream& dir, uint32_t run_number) {
1091   // Get details about host
1092   char hn[256];
1093   gethostname(hn, sizeof(hn));
1094   struct hostent* he;
1095   he = gethostbyname(hn);
1096 
1097   // Extract host name and ip
1098   std::string host_name;
1099   std::string host_ip;
1100   if (he) {
1101     host_name = std::string(he->h_name);
1102     host_ip = std::string(inet_ntoa(*(struct in_addr*)(he->h_addr)));
1103   } else {
1104     host_name = "unknown.cern.ch";
1105     host_ip = "255.255.255.255";
1106   }
1107 
1108   // Reformat IP address
1109   std::string::size_type pos = 0;
1110   std::stringstream ip;
1111   //for ( uint16_t ii = 0; ii < 4; ++ii ) {
1112   while (pos != std::string::npos) {
1113     std::string::size_type tmp = host_ip.find('.', pos);
1114     if (tmp != std::string::npos) {
1115       ip << std::setw(3) << std::setfill('0') << host_ip.substr(pos, tmp - pos) << ".";
1116       pos = tmp + 1;  // skip the delimiter "."
1117     } else {
1118       ip << std::setw(3) << std::setfill('0') << host_ip.substr(pos);
1119       pos = std::string::npos;
1120     }
1121   }
1122 
1123   // Get pid
1124   pid_t pid = getpid();
1125 
1126   // Construct string
1127   if (run_number) {
1128     dir << std::setw(8) << std::setfill('0') << run_number << "_";
1129   }
1130   dir << ip.str() << "_" << std::setw(5) << std::setfill('0') << pid;
1131 }
1132 
1133 // -----------------------------------------------------------------------------
1134 //
1135 // void SiStripCommissioningSource::cablingForConnectionRun( const sistrip::RunType& type ) {
1136 
1137 //   if ( type == sistrip::FED_CABLING ||
1138 //        type == sistrip::QUITE_FAST_CABLING ||
1139 //        type == sistrip::FAST_CABLING ) {
1140 //     std::stringstream ss;
1141 //     ss << "[SiStripCommissioningSource::" << __func__ << "]"
1142 //        << " Run type is " << SiStripEnumsAndStrings::runType( type ) << "!"
1143 //        << " Checking if cabling should be rebuilt using FED and device descriptions!...";
1144 //     edm::LogVerbatim(mlDqmSource_) << ss.str();
1145 //   } else { return; }
1146 
1147 //   // Build and retrieve SiStripConfigDb object using service
1148 //   SiStripConfigDb* db = edm::Service<SiStripConfigDb>().operator->(); //@@ NOT GUARANTEED TO BE THREAD SAFE!
1149 //   LogTrace(mlCabling_)
1150 //     << "[SiStripCommissioningSource::" << __func__ << "]"
1151 //     << " Nota bene: using the SiStripConfigDb API"
1152 //     << " as a \"service\" does not presently guarantee"
1153 //     << " thread-safe behaviour!...";
1154 
1155 //   if ( !db ) {
1156 //     edm::LogError(mlCabling_)
1157 //       << "[SiStripCommissioningSource::" << __func__ << "]"
1158 //       << " NULL pointer to SiStripConfigDb returned by service!"
1159 //       << " Cannot check if cabling needs to be rebuilt!";
1160 //     return;
1161 //   }
1162 
1163 //   if ( db->getFedConnections().empty() ) {
1164 //     edm::LogVerbatim(mlCabling_)
1165 //       << "[SiStripCommissioningSource::" << __func__ << "]"
1166 //       << " Datbase does not contain FED connections!"
1167 //       << " Do not need to rebuild cabling object based on FED and device descriptions!";
1168 //    return;
1169 //   }
1170 
1171 //   if ( fecCabling_ ) { delete fecCabling_; fecCabling_ = 0; }
1172 //   if ( fedCabling_ ) { delete fedCabling_; fedCabling_ = 0; }
1173 
1174 //   // Build FEC cabling
1175 //   fecCabling_ = new SiStripFecCabling();
1176 //   SiStripConfigDb::DcuDetIdMap mapping;
1177 //   SiStripFedCablingBuilderFromDb::buildFecCablingFromDevices( db,
1178 //                                *fecCabling_,
1179 //                                mapping );
1180 //   // Build FED cabling
1181 //   fedCabling_ = new SiStripFedCabling();
1182 //   SiStripFedCablingBuilderFromDb::getFedCabling( *fecCabling_,
1183 //                       *fedCabling_ );
1184 
1185 //   edm::LogVerbatim(mlCabling_)
1186 //     << "[SiStripCommissioningSource::" << __func__ << "]"
1187 //     << " Cabling object rebuilt using on FED and device descriptions!";
1188 
1189 // }