Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:52

0001 #ifndef CalibTracker_SiStripESProducers_SiStripGainFactor_h
0002 #define CalibTracker_SiStripESProducers_SiStripGainFactor_h
0003 
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 
0007 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0008 
0009 class SiStripGainFactor {
0010 public:
0011   SiStripGainFactor(const edm::ParameterSet& iConfig)
0012       : automaticMode_{iConfig.getParameter<bool>("AutomaticNormalization")},
0013         printdebug_{iConfig.getUntrackedParameter<bool>("printDebug", false)} {}
0014 
0015   void push_back_norm(double norm) { norm_.push_back(norm); }
0016 
0017   void resetIfBadNorm() {
0018     bool badNorm = std::find_if(norm_.begin(), norm_.end(), [](double x) { return x <= 0.; }) != norm_.end();
0019 
0020     if (!automaticMode_ && badNorm) {
0021       edm::LogError("SiStripGainESProducer") << "[SiStripGainESProducer] - ERROR: negative or zero Normalization "
0022                                                 "factor provided. Assuming 1 for such factor"
0023                                              << std::endl;
0024       norm_ = std::vector<double>(norm_.size(), 1.);
0025     }
0026   }
0027 
0028   double get(const SiStripApvGain& gain, const int apvGainIndex) const {
0029     double NFactor = 0.;
0030 
0031     if (automaticMode_ || printdebug_) {
0032       std::vector<uint32_t> DetIds;
0033       gain.getDetIds(DetIds);
0034 
0035       double SumOfGains = 0.;
0036       int NGains = 0;
0037       for (uint32_t detid : DetIds) {
0038         SiStripApvGain::Range detRange = gain.getRange(detid);
0039 
0040         int iComp = 0;
0041         for (std::vector<float>::const_iterator apvit = detRange.first; apvit != detRange.second; apvit++) {
0042           SumOfGains += (*apvit);
0043           NGains++;
0044           if (printdebug_)
0045             edm::LogInfo("SiStripGainESProducer::produce()")
0046                 << "detid/component: " << detid << "/" << iComp << "   gain factor " << *apvit;
0047           iComp++;
0048         }
0049       }
0050 
0051       if (automaticMode_) {
0052         if (SumOfGains > 0 && NGains > 0) {
0053           NFactor = SumOfGains / NGains;
0054         } else {
0055           edm::LogError(
0056               "SiStripGainESProducer::produce() - ERROR: empty set of gain values received. Cannot compute "
0057               "normalization factor. Assuming 1 for such factor")
0058               << std::endl;
0059           NFactor = 1.;
0060         }
0061       }
0062     }
0063 
0064     if (!automaticMode_) {
0065       NFactor = norm_[apvGainIndex];
0066     }
0067 
0068     if (printdebug_)
0069       edm::LogInfo("SiStripGainESProducer")
0070           << " putting A SiStrip Gain object in eventSetup with normalization factor " << NFactor;
0071     return NFactor;
0072   }
0073 
0074 private:
0075   std::vector<double> norm_;
0076   bool automaticMode_;
0077   bool printdebug_;
0078 };
0079 
0080 #endif