Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:36

0001 #include <memory>
0002 #include <ostream>
0003 
0004 #include "CalibTracker/SiStripAPVAnalysis/interface/ApvAnalysisFactory.h"
0005 #include "CalibTracker/SiStripAPVAnalysis/interface/ApvFactoryService.h"
0006 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
0007 #include "CondFormats/DataRecord/interface/SiStripPedestalsRcd.h"
0008 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0009 #include "CondFormats/SiStripObjects/interface/SiStripPedestals.h"
0010 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0011 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
0012 #include "DQM/SiStripCommon/interface/ExtractTObject.h"
0013 #include "DQM/SiStripCommon/interface/UpdateTProfile.h"
0014 #include "DQMServices/Core/interface/DQMStore.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/ServiceRegistry/interface/Service.h"
0017 
0018 #include "DQM/SiStripCommissioningSources/interface/NoiseTask.h"
0019 
0020 using namespace sistrip;
0021 
0022 std::ostream &operator<<(std::ostream &rOut, ApvAnalysis::PedestalType &rPEDS) {
0023   for (ApvAnalysis::PedestalType::const_iterator pedsIter = rPEDS.begin(); pedsIter != rPEDS.end(); ++pedsIter) {
0024     rOut << ' ' << *pedsIter;
0025   }
0026 
0027   return rOut;
0028 }
0029 
0030 // -----------------------------------------------------------------------------
0031 //
0032 NoiseTask::NoiseTask(DQMStore *dqm,
0033                      const FedChannelConnection &conn,
0034                      edm::ESGetToken<SiStripPedestals, SiStripPedestalsRcd> pedestalToken,
0035                      edm::ESGetToken<SiStripNoises, SiStripNoisesRcd> noiseToken)
0036     : CommissioningTask(dqm, conn, "NoiseTask"), pedestalToken_(pedestalToken), noiseToken_(noiseToken) {
0037   //@@ NOT GUARANTEED TO BE THREAD SAFE!
0038   pApvFactory_ = edm::Service<ApvFactoryService>().operator->()->getApvFactory();
0039 
0040   LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "]"
0041                          << " Constructing object...";
0042 }
0043 
0044 // -----------------------------------------------------------------------------
0045 //
0046 NoiseTask::~NoiseTask() {
0047   LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "]"
0048                          << " Destructing object...";
0049 
0050   // Have to delete pApvFactory_ manually even though we didn't create it
0051   // ourself. :(
0052   if (pApvFactory_) {
0053     delete pApvFactory_;
0054   }
0055 }
0056 
0057 // -----------------------------------------------------------------------------
0058 //
0059 void NoiseTask::book() {
0060   LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "]";
0061 
0062   // CACHING
0063   static std::unique_ptr<SiStripPedestals> pDBPedestals;
0064   static std::unique_ptr<SiStripNoises> pDBNoises;
0065 
0066   const uint16_t nBINS = 256;
0067 
0068   {
0069     // Pedestals
0070     std::string title = SiStripHistoTitle(sistrip::EXPERT_HISTO,
0071                                           sistrip::NOISE,
0072                                           sistrip::FED_KEY,
0073                                           fedKey(),
0074                                           sistrip::LLD_CHAN,
0075                                           connection().lldChannel(),
0076                                           sistrip::extrainfo::pedestals_)
0077                             .title();
0078 
0079     HistoSet oHSet;
0080     oHSet.isProfile_ = true;
0081 
0082     oHSet.vNumOfEntries_.resize(nBINS, 0);
0083     oHSet.vSumOfContents_.resize(nBINS, 0);
0084     oHSet.vSumOfSquares_.resize(nBINS, 0);
0085 
0086     oHSet.histo(dqm()->bookProfile(title, title, nBINS, -0.5, nBINS * 1. - 0.5, 1025, 0., 1025.));
0087 
0088     peds_.push_back(oHSet);
0089   }
0090 
0091   {
0092     // Corrected Noise
0093     std::string title = SiStripHistoTitle(sistrip::EXPERT_HISTO,
0094                                           sistrip::NOISE,
0095                                           sistrip::FED_KEY,
0096                                           fedKey(),
0097                                           sistrip::LLD_CHAN,
0098                                           connection().lldChannel(),
0099                                           sistrip::extrainfo::noise_)
0100                             .title();
0101 
0102     HistoSet oHSet;
0103     oHSet.isProfile_ = true;
0104 
0105     oHSet.vNumOfEntries_.resize(nBINS, 0);
0106     oHSet.vSumOfContents_.resize(nBINS, 0);
0107     oHSet.vSumOfSquares_.resize(nBINS, 0);
0108 
0109     oHSet.histo(dqm()->bookProfile(title, title, nBINS, -0.5, nBINS * 1. - 0.5, 1025, 0., 1025.));
0110 
0111     peds_.push_back(oHSet);
0112   }
0113 
0114   const uint16_t nCM_BINS = 1024;
0115   for (uint16_t nApv = 0; 2 > nApv; ++nApv) {
0116     std::string title = SiStripHistoTitle(sistrip::EXPERT_HISTO,
0117                                           sistrip::PEDESTALS,
0118                                           sistrip::FED_KEY,
0119                                           fedKey(),
0120                                           sistrip::APV,
0121                                           connection().i2cAddr(nApv),
0122                                           sistrip::extrainfo::commonMode_)
0123                             .title();
0124 
0125     HistoSet oHSet;
0126 
0127     oHSet.isProfile_ = false;
0128 
0129     oHSet.vNumOfEntries_.resize(nCM_BINS, 0);
0130 
0131     oHSet.histo(dqm()->book1D(title, title, nCM_BINS, nCM_BINS / 2 * -1. - 0.5, nCM_BINS / 2 * 1. - 0.5));
0132 
0133     cm_.push_back(oHSet);
0134   }
0135 
0136   // Initialize Apv
0137   pApvFactory_->instantiateApvs(connection().detId(), connection().nApvs());
0138 
0139   // --[ RETRIEVE PEDESTALS FROM DB ]--
0140   // Operation should be performed only once
0141   if (!pDBPedestals.get()) {
0142     LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0143                            << "Retrieving Pedestals from DB";
0144 
0145     // Directly retrieve Pedestals from EventSetup, and cache
0146     pDBPedestals = std::make_unique<SiStripPedestals>(eventSetup()->getData(pedestalToken_));
0147 
0148     LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0149                            << "Done Retrieving Pedestals from DB";
0150   }  // End retrieve Pedestals from DB
0151 
0152   // --[ RETRIEVE NOISES FROM DB ]--
0153   // Operation should be performed only once
0154   if (!pDBNoises.get()) {
0155     LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0156                            << "Retrieving Noises from DB";
0157 
0158     // Directly retrieve Noises from EventSetup, and cache
0159     pDBNoises = std::make_unique<SiStripNoises>(eventSetup()->getData(noiseToken_));
0160 
0161     LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0162                            << "Done Retrieving Noises from DB";
0163   }  // End retrieve Noises from DB
0164 
0165   // Get ApvAnalysis associated with given DetId
0166   ApvAnalysisFactory::ApvAnalysisVector apvAnalysisVector(pApvFactory_->getApvAnalysis(connection().detId()));
0167 
0168   SiStripPedestals::Range pedestalsRange(pDBPedestals->getRange(connection().detId()));
0169   SiStripNoises::Range noisesRange(pDBNoises->getRange(connection().detId()));
0170 
0171   // Cache Apv Pair #
0172   const uint16_t nAPV_PAIR = connection().apvPairNumber();
0173 
0174   for (uint16_t nLclApv = 0; 2 > nLclApv; ++nLclApv) {
0175     // Retrieve pedestals and noises associated with given DetId/Apv
0176     ApvAnalysis::PedestalType pedestals;
0177     ApvAnalysis::PedestalType noises;
0178     for (uint16_t nStrip = nAPV_PAIR * 256 + nLclApv * 128, nMaxStrip = nStrip + 128; nMaxStrip > nStrip; ++nStrip) {
0179       pedestals.push_back(pDBPedestals->getPed(nStrip, pedestalsRange));
0180       noises.push_back(pDBNoises->getNoise(nStrip, noisesRange));
0181     }
0182 
0183     try {
0184       // Checked access
0185       ApvAnalysisFactory::ApvAnalysisVector::reference rApvAnalysis = apvAnalysisVector.at(nAPV_PAIR * 2 + nLclApv);
0186 
0187       rApvAnalysis->pedestalCalculator().setPedestals(pedestals);
0188       rApvAnalysis->pedestalCalculator().setNoise(noises);
0189 
0190       /*
0191     std::stringstream out;
0192     LogTrace( mlDqmSource_)
0193         << "[NoiseTask::" << __func__ << "] "
0194         << "DetId|Apv# -> " 
0195         << connection().detId() << '|' << ( nAPV_PAIR * 2 + nLclApv)
0196         << "   Pedestals: " 
0197         << ( out << pedestals 
0198     << "   Noises: " << noises, out.str());
0199       */
0200     } catch (std::out_of_range const &) {
0201       // Hmm, didn't find appropriate Apv :((( -> VERY, VERY BAD
0202       LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0203                              << "Could not set Pedestals/Noises for DetId|Apv# -> " << connection().detId() << '|'
0204                              << (nAPV_PAIR * 2 + nLclApv) << ". !!! POSSIBLE BUG !!!";
0205     }  // End Try block
0206   }    // End Local Apvs loop
0207 }
0208 
0209 // -----------------------------------------------------------------------------
0210 //
0211 void NoiseTask::fill(const SiStripEventSummary &rSummary, const edm::DetSet<SiStripRawDigi> &rDigis) {
0212   pApvFactory_->updatePair(connection().detId(), connection().apvPairNumber(), rDigis);
0213 }
0214 
0215 // -----------------------------------------------------------------------------
0216 //
0217 void NoiseTask::update() {
0218   static UpdateTProfile updateTProfile;
0219 
0220   TProfile *pedsProf = ExtractTObject<TProfile>().extract(peds_[0].histo());
0221   TProfile *noiseProf = ExtractTObject<TProfile>().extract(peds_[1].histo());
0222 
0223   for (uint16_t nLclApv = 2 * connection().apvPairNumber(), nMaxLclApv = nLclApv + 2, nApv = 0; nMaxLclApv > nLclApv;
0224        ++nLclApv, ++nApv) {
0225     ApvAnalysis::PedestalType lclPedestals;
0226     ApvAnalysis::PedestalType lclNoises;
0227     ApvAnalysis::PedestalType lclCommonMode(pApvFactory_->getCommonMode(connection().detId(), nLclApv));
0228 
0229     pApvFactory_->getPedestal(connection().detId(), nLclApv, lclPedestals);
0230     pApvFactory_->getNoise(connection().detId(), nLclApv, lclNoises);
0231 
0232     const uint16_t nSTART_BIN = 128 * (nLclApv % 2);
0233 
0234     for (uint16_t nBin = 0, nAbsBin = nSTART_BIN + nBin + 1; 128 > nBin; ++nBin, ++nAbsBin) {
0235       updateTProfile.setBinContent(pedsProf, nAbsBin, 5, lclPedestals[nBin], lclNoises[nBin]);
0236       updateTProfile.setBinContent(noiseProf, nAbsBin, 5, lclNoises[nBin], 0);
0237     }  // End loop over BINs
0238 
0239     // Samvel: Assume Once CM value is calculated per chip.
0240     //         In principle Chip can be divided into a several ranges. Then CM
0241     //         will be calculated per range. !!! UPDATE CODE THEN !!!
0242     for (ApvAnalysis::PedestalType::const_iterator cmIterator = lclCommonMode.begin();
0243          cmIterator != lclCommonMode.end();
0244          ++cmIterator) {
0245       //uint32_t nCM = static_cast<uint32_t>( *cmIterator);
0246       //if( nCM >= 1024) nCM = 1023;
0247       //updateHistoSet( cm_[nApv], nCM);
0248       float nCM = static_cast<float>(*cmIterator);
0249       updateHistoSet(cm_[nApv], nCM);
0250     }
0251 
0252     std::stringstream out;
0253     LogTrace(mlDqmSource_) << "[NoiseTask::" << __func__ << "] "
0254                            << "DET ID [" << connection().detId() << "] has Common Mode size " << lclCommonMode.size()
0255                            << " : " << (out << lclCommonMode, out.str());
0256   }  // End loop over Local Apvs
0257 
0258   updateHistoSet(cm_[0]);
0259   updateHistoSet(cm_[1]);
0260 }