Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

#include "DQM/SiStripCommissioningDbClients/interface/PedestalsHistosUsingDb.h"
#include "CondFormats/SiStripObjects/interface/PedestalsAnalysis.h"
#include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
#include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
#include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include <iostream>

using namespace sistrip;

// -----------------------------------------------------------------------------
/** */
PedestalsHistosUsingDb::PedestalsHistosUsingDb(const edm::ParameterSet& pset,
                                               DQMStore* bei,
                                               SiStripConfigDb* const db,
                                               edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken)
    : CommissioningHistograms(pset.getParameter<edm::ParameterSet>("PedestalsParameters"), bei, sistrip::PEDESTALS),
      CommissioningHistosUsingDb(db, tTopoToken, sistrip::PEDESTALS),
      PedestalsHistograms(pset.getParameter<edm::ParameterSet>("PedestalsParameters"), bei) {
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                         << " Constructing object...";
  highThreshold_ = this->pset().getParameter<double>("HighThreshold");
  lowThreshold_ = this->pset().getParameter<double>("LowThreshold");
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                         << " Set FED zero suppression high/low threshold to " << highThreshold_ << "/"
                         << lowThreshold_;
  disableBadStrips_ = this->pset().getParameter<bool>("DisableBadStrips");
  keepStripsDisabled_ = this->pset().getParameter<bool>("KeepStripsDisabled");
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                         << " Disabling strips: " << disableBadStrips_
                         << " ; keeping previously disabled strips: " << keepStripsDisabled_;

  allowSelectiveUpload_ =
      this->pset().existsAs<bool>("doSelectiveUpload") ? this->pset().getParameter<bool>("doSelectiveUpload") : false;
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                         << " Selective upload of modules set to : " << allowSelectiveUpload_;
}

// -----------------------------------------------------------------------------
/** */
PedestalsHistosUsingDb::~PedestalsHistosUsingDb() {
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                         << " Destructing object...";
}

// -----------------------------------------------------------------------------
/** */
void PedestalsHistosUsingDb::uploadConfigurations() {
  LogTrace(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]";

  if (!db()) {
    edm::LogError(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                << " NULL pointer to SiStripConfigDb interface!"
                                << " Aborting upload...";
    return;
  }

  // Update FED descriptions with new peds/noise values
  SiStripConfigDb::FedDescriptionsRange feds = db()->getFedDescriptions();
  update(feds);
  if (doUploadConf()) {
    edm::LogVerbatim(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                   << " Uploading pedestals/noise to DB...";
    db()->uploadFedDescriptions();
    edm::LogVerbatim(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                   << " Completed database upload of " << feds.size() << " FED descriptions!";
  } else {
    edm::LogWarning(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                  << " TEST! No pedestals/noise values will be uploaded to DB...";
  }
}

// -----------------------------------------------------------------------------
/** */
void PedestalsHistosUsingDb::update(SiStripConfigDb::FedDescriptionsRange feds) {
  // Iterate through feds and update fed descriptions
  uint16_t updated = 0;
  SiStripConfigDb::FedDescriptionsV::const_iterator ifed;
  for (ifed = feds.begin(); ifed != feds.end(); ifed++) {
    for (uint16_t ichan = 0; ichan < sistrip::FEDCH_PER_FED; ichan++) {
      // Build FED and FEC keys
      const FedChannelConnection& conn = cabling()->fedConnection((*ifed)->getFedId(), ichan);
      if (conn.fecCrate() == sistrip::invalid_ || conn.fecSlot() == sistrip::invalid_ ||
          conn.fecRing() == sistrip::invalid_ || conn.ccuAddr() == sistrip::invalid_ ||
          conn.ccuChan() == sistrip::invalid_ || conn.lldChannel() == sistrip::invalid_) {
        continue;
      }
      SiStripFedKey fed_key(conn.fedId(), SiStripFedKey::feUnit(conn.fedCh()), SiStripFedKey::feChan(conn.fedCh()));
      SiStripFecKey fec_key(
          conn.fecCrate(), conn.fecSlot(), conn.fecRing(), conn.ccuAddr(), conn.ccuChan(), conn.lldChannel());

      // Locate appropriate analysis object
      Analyses::const_iterator iter = data(allowSelectiveUpload_).find(fec_key.key());
      if (iter != data(allowSelectiveUpload_).end()) {
        // Check if analysis is valid
        if (!iter->second->isValid()) {
          continue;
        }

        PedestalsAnalysis* anal = dynamic_cast<PedestalsAnalysis*>(iter->second);
        if (!anal) {
          edm::LogError(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                      << " NULL pointer to analysis object!";
          continue;
        }

        // Determine the pedestal shift to apply
        uint32_t pedshift = 127;
        for (uint16_t iapv = 0; iapv < sistrip::APVS_PER_FEDCH; iapv++) {
          uint32_t pedmin = (uint32_t)anal->pedsMin()[iapv];
          pedshift = pedmin < pedshift ? pedmin : pedshift;
          std::stringstream ss;
          ss << "iapv: " << iapv << " pedsMin()[iapv]: " << anal->pedsMin()[iapv] << " pedmin: " << pedmin
             << " pedshift: " << pedshift;
          edm::LogWarning(mlDqmClient_) << ss.str();
        }

        // Iterate through APVs and strips
        for (uint16_t iapv = 0; iapv < sistrip::APVS_PER_FEDCH; iapv++) {
          for (uint16_t istr = 0; istr < anal->peds()[iapv].size(); istr++) {
            // get the information on the strip as it was on the db
            Fed9U::Fed9UAddress addr(ichan, iapv, istr);
            Fed9U::Fed9UStripDescription temp = (*ifed)->getFedStrips().getStrip(addr);

            if (anal->peds()[iapv][istr] < 1.) {  //@@ ie, zero
              edm::LogWarning(mlDqmClient_)
                  << "[PedestalsHistosUsingDb::" << __func__ << "]"
                  << " Skipping ZERO pedestal value (ie, NO UPLOAD TO DB!) for FedKey/Id/Ch: " << hex << setw(8)
                  << setfill('0') << fed_key.key() << dec << "/" << (*ifed)->getFedId() << "/" << ichan
                  << " and device with FEC/slot/ring/CCU/LLD " << fec_key.fecCrate() << "/" << fec_key.fecSlot() << "/"
                  << fec_key.fecRing() << "/" << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << "/"
                  << fec_key.channel();
              continue;  //@@ do not upload
            }

            // determine whether we need to disable the strip
            bool disableStrip = false;
            if (keepStripsDisabled_) {
              disableStrip = temp.getDisable();
            } else if (disableBadStrips_) {
              PedestalsAnalysis::VInt dead = anal->dead()[iapv];
              if (find(dead.begin(), dead.end(), istr) != dead.end())
                disableStrip = true;
              PedestalsAnalysis::VInt noisy = anal->noisy()[iapv];
              if (find(noisy.begin(), noisy.end(), istr) != noisy.end())
                disableStrip = true;
            }

            Fed9U::Fed9UStripDescription data(static_cast<uint32_t>(anal->peds()[iapv][istr] - pedshift),
                                              highThreshold_,
                                              lowThreshold_,
                                              anal->noise()[iapv][istr],
                                              disableStrip);

            std::stringstream ss;
            if (data.getDisable() && edm::isDebugEnabled()) {
              ss << "[PedestalsHistosUsingDb::" << __func__ << "]"
                 << " Disabling strip in Fed9UStripDescription object..." << std::endl
                 << " for FED id/channel and APV/strip : " << fed_key.fedId() << "/" << fed_key.fedChannel() << " "
                 << iapv << "/" << istr << std::endl
                 << " and crate/FEC/ring/CCU/module    : " << fec_key.fecCrate() << "/" << fec_key.fecSlot() << "/"
                 << fec_key.fecRing() << "/" << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << std::endl
                 << " from ped/noise/high/low/disable  : " << static_cast<uint16_t>(temp.getPedestal()) << "/"
                 << static_cast<uint16_t>(temp.getHighThreshold()) << "/"
                 << static_cast<uint16_t>(temp.getLowThreshold()) << "/" << static_cast<uint16_t>(temp.getNoise())
                 << "/" << static_cast<uint16_t>(temp.getDisable()) << std::endl;
            }
            (*ifed)->getFedStrips().setStrip(addr, data);
            if (data.getDisable() && edm::isDebugEnabled()) {
              ss << " to ped/noise/high/low/disable    : " << static_cast<uint16_t>(data.getPedestal()) << "/"
                 << static_cast<uint16_t>(data.getHighThreshold()) << "/"
                 << static_cast<uint16_t>(data.getLowThreshold()) << "/" << static_cast<uint16_t>(data.getNoise())
                 << "/" << static_cast<uint16_t>(data.getDisable()) << std::endl;
              LogTrace(mlDqmClient_) << ss.str();
            }

          }  // end loop on strips
        }  // end loop on apvs
        updated++;

      } else {
        if (deviceIsPresent(fec_key)) {
          edm::LogWarning(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                        << " Unable to find pedestals/noise for FedKey/Id/Ch: " << hex << setw(8)
                                        << setfill('0') << fed_key.key() << dec << "/" << (*ifed)->getFedId() << "/"
                                        << ichan << " and device with FEC/slot/ring/CCU/LLD " << fec_key.fecCrate()
                                        << "/" << fec_key.fecSlot() << "/" << fec_key.fecRing() << "/"
                                        << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << "/" << fec_key.channel();
        }
      }
    }
  }

  edm::LogVerbatim(mlDqmClient_) << "[PedestalsHistosUsingDb::" << __func__ << "]"
                                 << " Updated FED pedestals/noise for " << updated << " channels";
}

// -----------------------------------------------------------------------------
/** */
void PedestalsHistosUsingDb::create(SiStripConfigDb::AnalysisDescriptionsV& desc, Analysis analysis) {
  PedestalsAnalysis* anal = dynamic_cast<PedestalsAnalysis*>(analysis->second);
  if (!anal) {
    return;
  }

  SiStripFecKey fec_key(anal->fecKey());
  SiStripFedKey fed_key(anal->fedKey());

  for (uint16_t iapv = 0; iapv < 2; ++iapv) {
    // Create description
    PedestalsAnalysisDescription* tmp;
    tmp = new PedestalsAnalysisDescription(anal->dead()[iapv],
                                           anal->noisy()[iapv],
                                           anal->pedsMean()[iapv],
                                           anal->pedsSpread()[iapv],
                                           anal->noiseMean()[iapv],
                                           anal->noiseSpread()[iapv],
                                           anal->rawMean()[iapv],
                                           anal->rawSpread()[iapv],
                                           anal->pedsMax()[iapv],
                                           anal->pedsMin()[iapv],
                                           anal->noiseMax()[iapv],
                                           anal->noiseMin()[iapv],
                                           anal->rawMax()[iapv],
                                           anal->rawMin()[iapv],
                                           fec_key.fecCrate(),
                                           fec_key.fecSlot(),
                                           fec_key.fecRing(),
                                           fec_key.ccuAddr(),
                                           fec_key.ccuChan(),
                                           SiStripFecKey::i2cAddr(fec_key.lldChan(), !iapv),
                                           db()->dbParams().partitions().begin()->second.partitionName(),
                                           db()->dbParams().partitions().begin()->second.runNumber(),
                                           anal->isValid(),
                                           "",
                                           fed_key.fedId(),
                                           fed_key.feUnit(),
                                           fed_key.feChan(),
                                           fed_key.fedApv());

    // Add comments
    typedef std::vector<std::string> Strings;
    Strings errors = anal->getErrorCodes();
    Strings::const_iterator istr = errors.begin();
    Strings::const_iterator jstr = errors.end();
    for (; istr != jstr; ++istr) {
      tmp->addComments(*istr);
    }

    // Store description
    desc.push_back(tmp);
  }
}