Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-22 22:55:13

0001 // system includes
0002 #include <functional>
0003 #include <memory>
0004 #include <string>
0005 
0006 // user includes
0007 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0008 #include "CalibTracker/Records/interface/SiStripGainRcd.h"
0009 #include "DataFormats/Common/interface/DetSetVector.h"
0010 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0011 #include "DataFormats/SiStripDigi/interface/SiStripProcessedRawDigi.h"
0012 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
0013 #include "FWCore/Framework/interface/stream/EDProducer.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Utilities/interface/InputTag.h"
0020 #include "FWCore/Utilities/interface/transform.h"
0021 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripCommonModeNoiseSubtractor.h"
0022 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripPedestalsSubtractor.h"
0023 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingFactory.h"
0024 
0025 class SiStripProcessedRawDigiProducer : public edm::stream::EDProducer<> {
0026 public:
0027   explicit SiStripProcessedRawDigiProducer(edm::ParameterSet const&);
0028 
0029 private:
0030   void produce(edm::Event& e, const edm::EventSetup& es) override;
0031   template <class T>
0032   std::string findInput(edm::Handle<T>& handle, const std::vector<edm::EDGetTokenT<T> >& tokens, const edm::Event& e);
0033 
0034   void vr_process(const edm::DetSetVector<SiStripRawDigi>&,
0035                   edm::DetSetVector<SiStripProcessedRawDigi>&,
0036                   const SiStripGain&);
0037   void pr_process(const edm::DetSetVector<SiStripRawDigi>&,
0038                   edm::DetSetVector<SiStripProcessedRawDigi>&,
0039                   const SiStripGain&);
0040   void zs_process(const edm::DetSetVector<SiStripDigi>&,
0041                   edm::DetSetVector<SiStripProcessedRawDigi>&,
0042                   const SiStripGain&);
0043   void common_process(const uint32_t,
0044                       std::vector<float>&,
0045                       edm::DetSetVector<SiStripProcessedRawDigi>&,
0046                       const SiStripGain&);
0047 
0048   const std::vector<edm::InputTag> inputTags_;
0049   const std::vector<edm::EDGetTokenT<edm::DetSetVector<SiStripDigi> > > inputTokensDigi_;
0050   const std::vector<edm::EDGetTokenT<edm::DetSetVector<SiStripRawDigi> > > inputTokensRawDigi_;
0051   const edm::ESGetToken<SiStripGain, SiStripGainRcd> gainToken_;
0052 
0053   std::unique_ptr<SiStripPedestalsSubtractor> subtractorPed_;
0054   std::unique_ptr<SiStripCommonModeNoiseSubtractor> subtractorCMN_;
0055 };
0056 
0057 SiStripProcessedRawDigiProducer::SiStripProcessedRawDigiProducer(edm::ParameterSet const& conf)
0058     : inputTags_(conf.getParameter<std::vector<edm::InputTag> >("DigiProducersList")),
0059       inputTokensDigi_(edm::vector_transform(
0060           inputTags_, [this](edm::InputTag const& tag) { return consumes<edm::DetSetVector<SiStripDigi> >(tag); })),
0061       inputTokensRawDigi_(edm::vector_transform(
0062           inputTags_, [this](edm::InputTag const& tag) { return consumes<edm::DetSetVector<SiStripRawDigi> >(tag); })),
0063       gainToken_(esConsumes()),
0064       subtractorPed_(SiStripRawProcessingFactory::create_SubtractorPed(conf, consumesCollector())),
0065       subtractorCMN_(SiStripRawProcessingFactory::create_SubtractorCMN(conf, consumesCollector())) {
0066   produces<edm::DetSetVector<SiStripProcessedRawDigi> >("");
0067 }
0068 
0069 void SiStripProcessedRawDigiProducer::produce(edm::Event& e, const edm::EventSetup& es) {
0070   std::unique_ptr<edm::DetSetVector<SiStripProcessedRawDigi> > output(new edm::DetSetVector<SiStripProcessedRawDigi>());
0071   edm::Handle<edm::DetSetVector<SiStripDigi> > inputDigis;
0072   edm::Handle<edm::DetSetVector<SiStripRawDigi> > inputRawdigis;
0073 
0074   const auto& gain = es.getData(gainToken_);
0075   subtractorPed_->init(es);
0076   subtractorCMN_->init(es);
0077 
0078   std::string label = findInput(inputRawdigis, inputTokensRawDigi_, e);
0079   if ("VirginRaw" == label)
0080     vr_process(*inputRawdigis, *output, gain);
0081   else if ("ProcessedRaw" == label)
0082     pr_process(*inputRawdigis, *output, gain);
0083   else if ("ZeroSuppressed" == findInput(inputDigis, inputTokensDigi_, e))
0084     zs_process(*inputDigis, *output, gain);
0085   else
0086     edm::LogError("Input Not Found");
0087 
0088   e.put(std::move(output));
0089 }
0090 
0091 template <class T>
0092 inline std::string SiStripProcessedRawDigiProducer::findInput(edm::Handle<T>& handle,
0093                                                               const std::vector<edm::EDGetTokenT<T> >& tokens,
0094                                                               const edm::Event& e) {
0095   for (typename std::vector<edm::EDGetTokenT<T> >::const_iterator token = tokens.begin(); token != tokens.end();
0096        ++token) {
0097     unsigned index(token - tokens.begin());
0098     e.getByToken(*token, handle);
0099     if (handle.isValid() && !handle->empty()) {
0100       edm::LogInfo("Input") << inputTags_.at(index);
0101       return inputTags_.at(index).instance();
0102     }
0103   }
0104   return "Input Not Found";
0105 }
0106 
0107 void SiStripProcessedRawDigiProducer::zs_process(const edm::DetSetVector<SiStripDigi>& input,
0108                                                  edm::DetSetVector<SiStripProcessedRawDigi>& output,
0109                                                  const SiStripGain& gain) {
0110   std::vector<float> digis;
0111   for (const auto& detset : input) {
0112     digis.clear();
0113     for (const auto& digi : detset) {
0114       digis.resize(digi.strip(), 0);
0115       digis.push_back(digi.adc());
0116     }
0117     common_process(detset.id, digis, output, gain);
0118   }
0119 }
0120 
0121 void SiStripProcessedRawDigiProducer::pr_process(const edm::DetSetVector<SiStripRawDigi>& input,
0122                                                  edm::DetSetVector<SiStripProcessedRawDigi>& output,
0123                                                  const SiStripGain& gain) {
0124   for (const auto& detset : input) {
0125     std::vector<float> digis;
0126     transform(
0127         detset.begin(), detset.end(), back_inserter(digis), std::bind(&SiStripRawDigi::adc, std::placeholders::_1));
0128     subtractorCMN_->subtract(detset.id, 0, digis);
0129     common_process(detset.id, digis, output, gain);
0130   }
0131 }
0132 
0133 void SiStripProcessedRawDigiProducer::vr_process(const edm::DetSetVector<SiStripRawDigi>& input,
0134                                                  edm::DetSetVector<SiStripProcessedRawDigi>& output,
0135                                                  const SiStripGain& gain) {
0136   for (const auto& detset : input) {
0137     std::vector<int16_t> int_digis(detset.size());
0138     subtractorPed_->subtract(detset, int_digis);
0139     std::vector<float> digis(int_digis.begin(), int_digis.end());
0140     subtractorCMN_->subtract(detset.id, 0, digis);
0141     common_process(detset.id, digis, output, gain);
0142   }
0143 }
0144 
0145 void SiStripProcessedRawDigiProducer::common_process(const uint32_t detId,
0146                                                      std::vector<float>& digis,
0147                                                      edm::DetSetVector<SiStripProcessedRawDigi>& output,
0148                                                      const SiStripGain& gain) {
0149   //Apply Gains
0150   SiStripApvGain::Range detGainRange = gain.getRange(detId);
0151   for (auto& it : digis)
0152     it /= (gain.getStripGain(it - *digis.begin(), detGainRange));
0153 
0154   //Insert as DetSet
0155   edm::DetSet<SiStripProcessedRawDigi> ds(detId);
0156   copy(digis.begin(), digis.end(), back_inserter(ds.data));
0157   output.insert(ds);
0158 }
0159 
0160 #include "FWCore/PluginManager/interface/ModuleDef.h"
0161 #include "FWCore/Framework/interface/MakerMacros.h"
0162 
0163 DEFINE_FWK_MODULE(SiStripProcessedRawDigiProducer);