Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-17 23:31:50

0001 // system includes
0002 #include <memory>
0003 
0004 // user includes
0005 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0006 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0007 #include "CalibTracker/Records/interface/SiStripGainRcd.h"
0008 #include "CalibTracker/Records/interface/SiStripQualityRcd.h"
0009 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0010 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
0011 #include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
0012 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0013 #include "FWCore/Framework/interface/ESProducer.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 
0016 class ClusterizerUnitTesterESProducer : public edm::ESProducer {
0017   typedef edm::ParameterSet PSet;
0018   typedef std::vector<PSet> VPSet;
0019   typedef VPSet::const_iterator iter_t;
0020 
0021 public:
0022   ClusterizerUnitTesterESProducer(const PSet&);
0023   ~ClusterizerUnitTesterESProducer() {}
0024   std::shared_ptr<const SiStripGain> produceGainRcd(const SiStripGainRcd&) { return gain_; }
0025   std::shared_ptr<const SiStripNoises> produceNoisesRcd(const SiStripNoisesRcd&) { return noises_; }
0026   std::shared_ptr<const SiStripQuality> produceQualityRcd(const SiStripQualityRcd&) { return quality_; }
0027 
0028 private:
0029   void extractNoiseGainQuality(const PSet&, SiStripQuality*, SiStripApvGain*, SiStripNoises*);
0030   void extractNoiseGainQualityForDetId(uint32_t, const VPSet&, SiStripQuality*, SiStripApvGain*, SiStripNoises*);
0031 
0032   void setNoises(uint32_t, std::vector<std::pair<uint16_t, float> >&, SiStripNoises*);
0033   void setGains(uint32_t, std::vector<std::pair<uint16_t, float> >&, SiStripApvGain*);
0034 
0035   // These objects might be shared across multiple concurrent
0036   // IOVs and are not allowed to be modified after the module
0037   // constructor finishes.
0038   std::unique_ptr<const SiStripApvGain> apvGain_;
0039   std::shared_ptr<const SiStripGain> gain_;
0040   std::shared_ptr<const SiStripNoises> noises_;
0041   std::shared_ptr<const SiStripQuality> quality_;
0042 };
0043 
0044 ClusterizerUnitTesterESProducer::ClusterizerUnitTesterESProducer(const edm::ParameterSet& conf) {
0045   const auto detInfo =
0046       SiStripDetInfoFileReader::read(edm::FileInPath{SiStripDetInfoFileReader::kDefaultFile}.fullPath());
0047 
0048   auto quality = std::make_unique<SiStripQuality>(detInfo);
0049   auto apvGain = std::make_unique<SiStripApvGain>();
0050   auto noises = std::make_unique<SiStripNoises>();
0051 
0052   extractNoiseGainQuality(conf, quality.get(), apvGain.get(), noises.get());
0053 
0054   gain_ = std::make_shared<const SiStripGain>(*apvGain, 1, detInfo);
0055 
0056   quality->cleanUp();
0057   quality->fillBadComponents();
0058 
0059   quality_ = std::move(quality);
0060   apvGain_ = std::move(apvGain);
0061   noises_ = std::move(noises);
0062 
0063   setWhatProduced(this, &ClusterizerUnitTesterESProducer::produceGainRcd);
0064   setWhatProduced(this, &ClusterizerUnitTesterESProducer::produceNoisesRcd);
0065   setWhatProduced(this, &ClusterizerUnitTesterESProducer::produceQualityRcd);
0066 }
0067 
0068 void ClusterizerUnitTesterESProducer::extractNoiseGainQuality(const edm::ParameterSet& conf,
0069                                                               SiStripQuality* quality,
0070                                                               SiStripApvGain* apvGain,
0071                                                               SiStripNoises* noises) {
0072   uint32_t detId = 0;
0073   VPSet groups = conf.getParameter<VPSet>("ClusterizerTestGroups");
0074   for (iter_t group = groups.begin(); group < groups.end(); group++) {
0075     VPSet tests = group->getParameter<VPSet>("Tests");
0076     for (iter_t test = tests.begin(); test < tests.end(); test++)
0077       extractNoiseGainQualityForDetId(detId++, test->getParameter<VPSet>("Digis"), quality, apvGain, noises);
0078   }
0079 }
0080 
0081 void ClusterizerUnitTesterESProducer::extractNoiseGainQualityForDetId(
0082     uint32_t detId, const VPSet& digiset, SiStripQuality* quality, SiStripApvGain* apvGain, SiStripNoises* noises) {
0083   std::vector<std::pair<uint16_t, float> > detNoises;
0084   std::vector<std::pair<uint16_t, float> > detGains;
0085   std::vector<unsigned> detBadStrips;
0086   for (iter_t digi = digiset.begin(); digi < digiset.end(); digi++) {
0087     uint16_t strip = digi->getParameter<unsigned>("Strip");
0088     if (digi->getParameter<unsigned>("ADC") != 0) {
0089       detNoises.push_back(std::make_pair(strip, digi->getParameter<double>("Noise")));
0090       detGains.push_back(std::make_pair(strip, digi->getParameter<double>("Gain")));
0091     }
0092     if (!digi->getParameter<bool>("Quality"))
0093       detBadStrips.push_back(quality->encode(strip, 1));
0094   }
0095   setNoises(detId, detNoises, noises);
0096   setGains(detId, detGains, apvGain);
0097   if (detBadStrips.size())
0098     quality->add(detId, std::make_pair(detBadStrips.begin(), detBadStrips.end()));
0099 }
0100 
0101 void ClusterizerUnitTesterESProducer::setNoises(uint32_t detId,
0102                                                 std::vector<std::pair<uint16_t, float> >& digiNoises,
0103                                                 SiStripNoises* noises) {
0104   std::sort(digiNoises.begin(), digiNoises.end());
0105   std::vector<float> detnoise;
0106   for (std::vector<std::pair<uint16_t, float> >::const_iterator digi = digiNoises.begin(); digi < digiNoises.end();
0107        ++digi) {
0108     detnoise.resize(digi->first, 1);  //pad with default noise 1
0109     detnoise.push_back(digi->second);
0110   }
0111   if (detnoise.size() > 768)
0112     throw cms::Exception("Faulty noise construction") << "No strip numbers greater than 767 please" << std::endl;
0113   detnoise.resize(768, 1.0);
0114 
0115   SiStripNoises::InputVector theSiStripVector;
0116   for (uint16_t strip = 0; strip < detnoise.size(); strip++) {
0117     noises->setData(detnoise.at(strip), theSiStripVector);
0118   }
0119   noises->put(detId, theSiStripVector);
0120 }
0121 
0122 void ClusterizerUnitTesterESProducer::setGains(uint32_t detId,
0123                                                std::vector<std::pair<uint16_t, float> >& digiGains,
0124                                                SiStripApvGain* apvGain) {
0125   std::sort(digiGains.begin(), digiGains.end());
0126   std::vector<float> detApvGains;
0127   for (std::vector<std::pair<uint16_t, float> >::const_iterator digi = digiGains.begin(); digi < digiGains.end();
0128        ++digi) {
0129     if (detApvGains.size() <= digi->first / 128) {
0130       detApvGains.push_back(digi->second);
0131     } else if (detApvGains.at(digi->first / 128) != digi->second) {
0132       throw cms::Exception("Faulty gain construction.") << "  Only one gain setting per APV please.\n";
0133     }
0134   }
0135   detApvGains.resize(6, 1.);
0136 
0137   SiStripApvGain::Range range(detApvGains.begin(), detApvGains.end());
0138   if (!apvGain->put(detId, range))
0139     throw cms::Exception("Trying to set gain twice for same detId: ") << detId;
0140   return;
0141 }
0142 
0143 #include "FWCore/PluginManager/interface/ModuleDef.h"
0144 #include "FWCore/Framework/interface/MakerMacros.h"
0145 #include "FWCore/Framework/interface/ModuleFactory.h"
0146 #include "FWCore/Utilities/interface/typelookup.h"
0147 #include "FWCore/Framework/interface/eventsetuprecord_registration_macro.h"
0148 DEFINE_FWK_EVENTSETUP_MODULE(ClusterizerUnitTesterESProducer);