Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondTools/SiStrip
0004 // Class:      SiStripGainPayloadCopyAndExclude
0005 //
0006 /*
0007  *\class SiStripGainPayloadCopyAndExclude SiStripGainPayloadCopyAndExclude.cc CondTools/SiStrip/plugins/SiStripGainPayloadCopyAndExclude.cc
0008 
0009  Description: This module is meant to copy the content of a SiStrip APV Gain payload (either G1 or G2) from a local sqlite file (that should be feeded to the Event Setup via the SiStripApvGain3Rcd and put in another local sqlite file, excepted for the modules specified in the excludedModules parameter. If the doReverse parameter is true, the opposite action is performed. 
0010 
0011  Implementation: The implemenation takes advantage of the convenience record SiStripApvGain3Rcd in the EventSetup to be able to hold at the same time two instances of the Strip Gains in the same job.
0012 
0013 */
0014 //
0015 // Original Author:  Marco Musich
0016 //         Created:  Fri, 08 Jun 2018 08:28:01 GMT
0017 //
0018 //
0019 
0020 // system include files
0021 #include <memory>
0022 #include <iostream>
0023 
0024 // user include files
0025 #include "CLHEP/Random/RandGauss.h"
0026 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0027 #include "CalibTracker/Records/interface/SiStripGainRcd.h"
0028 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
0029 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0030 #include "CondFormats/DataRecord/interface/SiStripApvGainRcd.h"
0031 #include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
0032 #include "CondFormats/SiStripObjects/interface/SiStripSummary.h"
0033 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0034 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0035 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0036 #include "FWCore/Framework/interface/ESHandle.h"
0037 #include "FWCore/Framework/interface/Event.h"
0038 #include "FWCore/Framework/interface/EventSetup.h"
0039 #include "FWCore/Framework/interface/Frameworkfwd.h"
0040 #include "FWCore/Framework/interface/MakerMacros.h"
0041 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0042 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0043 #include "FWCore/ServiceRegistry/interface/Service.h"
0044 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0045 
0046 //
0047 // class declaration
0048 //
0049 class SiStripGainPayloadCopyAndExclude : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0050 public:
0051   explicit SiStripGainPayloadCopyAndExclude(const edm::ParameterSet&);
0052   ~SiStripGainPayloadCopyAndExclude() override = default;
0053 
0054   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0055 
0056 private:
0057   void analyze(const edm::Event&, const edm::EventSetup&) override;
0058   std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
0059 
0060   // ----------member data ---------------------------
0061   const edm::ESGetToken<SiStripGain, SiStripGainRcd> m_gainToken;
0062   const edm::ESGetToken<SiStripApvGain, SiStripApvGain3Rcd> m_gain3Token;
0063   std::vector<unsigned int> m_excludedMods;
0064   const std::string m_Record;
0065   const uint32_t m_gainType;
0066   const bool m_reverseSelect;
0067 };
0068 
0069 //
0070 // constructors and destructor
0071 //
0072 SiStripGainPayloadCopyAndExclude::SiStripGainPayloadCopyAndExclude(const edm::ParameterSet& iConfig)
0073     : m_gainToken{esConsumes()},
0074       m_gain3Token{esConsumes()},
0075       m_excludedMods{iConfig.getUntrackedParameter<std::vector<unsigned int>>("excludedModules")},
0076       m_Record{iConfig.getUntrackedParameter<std::string>("record", "SiStripApvGainRcd")},
0077       m_gainType{iConfig.getUntrackedParameter<uint32_t>("gainType", 1)},
0078       m_reverseSelect{iConfig.getUntrackedParameter<bool>("reverseSelection", false)} {
0079   usesResource(cond::service::PoolDBOutputService::kSharedResource);
0080 
0081   //now do what ever initialization is needed
0082   sort(m_excludedMods.begin(), m_excludedMods.end());
0083 
0084   edm::LogInfo("ExcludedModules") << "Selected module list";
0085   for (std::vector<unsigned int>::const_iterator mod = m_excludedMods.begin(); mod != m_excludedMods.end(); mod++) {
0086     edm::LogVerbatim("ExcludedModules") << *mod;
0087   }
0088 }
0089 
0090 //
0091 // member functions
0092 //
0093 
0094 // ------------ method called for each event  ------------
0095 void SiStripGainPayloadCopyAndExclude::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0096   using namespace edm;
0097 
0098   // gain to be validated
0099   edm::ESHandle<SiStripApvGain> gNew = iSetup.getHandle(m_gain3Token);
0100   edm::ESHandle<SiStripGain> gOld = iSetup.getHandle(m_gainToken);
0101 
0102   std::map<std::pair<uint32_t, int>, float> theMap, oldPayloadMap;
0103 
0104   std::vector<uint32_t> detid;
0105   gNew->getDetIds(detid);
0106   for (const auto& d : detid) {
0107     SiStripApvGain::Range range_new = gNew->getRange(d);
0108     SiStripApvGain::Range range_old = gOld->getRange(d, m_gainType);
0109     float nAPV = 0;
0110 
0111     for (int it = 0; it < range_new.second - range_new.first; it++) {
0112       nAPV += 1;
0113       float Gain = gNew->getApvGain(it, range_new);
0114       float patchGain = gOld->getApvGain(it, range_old);
0115       std::pair<uint32_t, int> index = std::make_pair(d, nAPV);
0116 
0117       oldPayloadMap[index] = Gain;
0118 
0119       bool found(false);
0120       for (const auto& mod : m_excludedMods) {
0121         if (d == mod) {
0122           edm::LogInfo("ModuleFound") << " module " << mod << " found! Excluded... " << std::endl;
0123           found = true;
0124           break;
0125         }
0126       }
0127 
0128       if (m_reverseSelect)
0129         found = (!found);
0130 
0131       if (!found) {
0132         theMap[index] = Gain;
0133       } else {
0134         theMap[index] = patchGain;
0135       }
0136 
0137     }  // loop over APVs
0138   }  // loop over DetIds
0139 
0140   std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
0141 
0142   // write out the APVGains record
0143   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0144 
0145   if (poolDbService.isAvailable())
0146     poolDbService->writeOneIOV(theAPVGains.get(), poolDbService->currentTime(), m_Record);
0147   else
0148     throw std::runtime_error("PoolDBService required.");
0149 }
0150 
0151 //********************************************************************************//
0152 std::unique_ptr<SiStripApvGain> SiStripGainPayloadCopyAndExclude::getNewObject(
0153     const std::map<std::pair<uint32_t, int>, float>& theMap) {
0154   std::unique_ptr<SiStripApvGain> obj = std::make_unique<SiStripApvGain>();
0155 
0156   std::vector<float> theSiStripVector;
0157   uint32_t PreviousDetId = 0;
0158   for (const auto& element : theMap) {
0159     uint32_t DetId = element.first.first;
0160     if (DetId != PreviousDetId) {
0161       if (!theSiStripVector.empty()) {
0162         SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
0163         if (!obj->put(PreviousDetId, range))
0164           printf("Bug to put detId = %i\n", PreviousDetId);
0165       }
0166       theSiStripVector.clear();
0167       PreviousDetId = DetId;
0168     }
0169     theSiStripVector.push_back(element.second);
0170 
0171     edm::LogInfo("SiStripGainPayloadCopyAndExclude")
0172         << " DetId: " << DetId << " APV:   " << element.first.second << " Gain:  " << element.second << std::endl;
0173   }
0174 
0175   if (!theSiStripVector.empty()) {
0176     SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
0177     if (!obj->put(PreviousDetId, range))
0178       printf("Bug to put detId = %i\n", PreviousDetId);
0179   }
0180 
0181   return obj;
0182 }
0183 
0184 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0185 void SiStripGainPayloadCopyAndExclude::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0186   edm::ParameterSetDescription desc;
0187   desc.addUntracked<std::vector<unsigned int>>("excludedModules", {});
0188   desc.addUntracked<std::string>("record", "SiStripApvGainRcd");
0189   desc.addUntracked<uint32_t>("gainType", 1);
0190   desc.addUntracked<bool>("reverseSelection", false);
0191   descriptions.addWithDefaultLabel(desc);
0192 }
0193 
0194 //define this as a plug-in
0195 DEFINE_FWK_MODULE(SiStripGainPayloadCopyAndExclude);