Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // Original Author:  Anne-Marie Magnan
0002 //         Created:  2010/01/21
0003 //
0004 
0005 #include <sstream>
0006 #include <memory>
0007 #include <list>
0008 #include <algorithm>
0009 #include <cassert>
0010 #include <vector>
0011 
0012 #include "FWCore/Utilities/interface/EDGetToken.h"
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/EventSetup.h"
0017 #include "FWCore/Framework/interface/ESWatcher.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Utilities/interface/InputTag.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "FWCore/ServiceRegistry/interface/Service.h"
0022 #include "FWCore/Utilities/interface/Exception.h"
0023 
0024 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0025 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0026 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0027 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0028 #include "DataFormats/Common/interface/DetSetVector.h"
0029 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
0030 #include "DataFormats/SiStripDigi/interface/SiStripProcessedRawDigi.h"
0031 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0032 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
0033 
0034 #include "CondFormats/SiStripObjects/interface/SiStripPedestals.h"
0035 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0036 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0037 
0038 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
0039 
0040 //for the zero suppression algorithm(s)
0041 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingFactory.h"
0042 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingAlgorithms.h"
0043 
0044 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDEmulator.h"
0045 #include "DQM/SiStripMonitorHardware/interface/SiStripSpyUtilities.h"
0046 
0047 using edm::LogError;
0048 using edm::LogInfo;
0049 using edm::LogWarning;
0050 
0051 namespace sistrip {
0052 
0053   //
0054   // Class declaration
0055   //
0056 
0057   class FEDEmulatorModule : public edm::stream::EDProducer<> {
0058   public:
0059     explicit FEDEmulatorModule(const edm::ParameterSet&);
0060     ~FEDEmulatorModule() override;
0061 
0062   private:
0063     void produce(edm::Event&, const edm::EventSetup&) override;
0064     //virtual void endJob();
0065 
0066     //tag of spydata collection
0067     edm::InputTag spyReorderedDigisTag_;
0068     edm::InputTag spyVirginRawDigisTag_;
0069     edm::EDGetTokenT<edm::DetSetVector<SiStripRawDigi> > spyReorderedDigisToken_;
0070     edm::EDGetTokenT<edm::DetSetVector<SiStripRawDigi> > spyVirginRawDigisToken_;
0071 
0072     //by fedIndex or module detid
0073     bool byModule_;
0074 
0075     sistrip::FEDEmulator fedEmulator_;
0076 
0077     static const char* const messageLabel_;
0078 
0079     std::unique_ptr<SiStripRawProcessingAlgorithms> algorithms_;  //!< object for zero-suppression
0080 
0081     edm::ESGetToken<SiStripFedCabling, SiStripFedCablingRcd> fedCablingToken_;
0082     edm::ESGetToken<SiStripPedestals, SiStripPedestalsRcd> pedestalsToken_;
0083     edm::ESGetToken<SiStripNoises, SiStripNoisesRcd> noisesToken_;
0084     const SiStripFedCabling* fedCabling_;
0085     const SiStripPedestals* pedestals_;
0086     const SiStripNoises* noises_;
0087 
0088     edm::ESWatcher<SiStripFedCablingRcd> cablingWatcher_;
0089     edm::ESWatcher<SiStripPedestalsRcd> pedestalsWatcher_;
0090     edm::ESWatcher<SiStripNoisesRcd> noisesWatcher_;
0091     void updateFedCabling(const SiStripFedCablingRcd& rcd);
0092     void updatePedestals(const SiStripPedestalsRcd& rcd);
0093     void updateNoises(const SiStripNoisesRcd& rcd);
0094   };
0095 
0096 }  // namespace sistrip
0097 
0098 namespace sistrip {
0099 
0100   //
0101   // Constructors and destructor
0102   //
0103   const char* const FEDEmulatorModule::messageLabel_ = "SiStripFEDEmulatorModule";
0104 
0105   FEDEmulatorModule::FEDEmulatorModule(const edm::ParameterSet& iConfig)
0106       : spyReorderedDigisTag_(iConfig.getParameter<edm::InputTag>("SpyReorderedDigisTag")),
0107         spyVirginRawDigisTag_(iConfig.getParameter<edm::InputTag>("SpyVirginRawDigisTag")),
0108         byModule_(iConfig.getParameter<bool>("ByModule")),
0109         algorithms_(SiStripRawProcessingFactory::create(iConfig.getParameter<edm::ParameterSet>("Algorithms"),
0110                                                         consumesCollector())),
0111         fedCablingToken_(esConsumes<>()),
0112         pedestalsToken_(esConsumes<>()),
0113         noisesToken_(esConsumes<>()),
0114         cablingWatcher_(this, &sistrip::FEDEmulatorModule::updateFedCabling),
0115         pedestalsWatcher_(this, &sistrip::FEDEmulatorModule::updatePedestals),
0116         noisesWatcher_(this, &sistrip::FEDEmulatorModule::updateNoises) {
0117     spyReorderedDigisToken_ = consumes<edm::DetSetVector<SiStripRawDigi> >(spyReorderedDigisTag_);
0118     spyVirginRawDigisToken_ = consumes<edm::DetSetVector<SiStripRawDigi> >(spyVirginRawDigisTag_);
0119 
0120     fedEmulator_.initialise(byModule_);
0121 
0122     if (!byModule_) {  //if not by module
0123       //the medians will be produced by fed id/channel
0124       produces<std::map<uint32_t, std::vector<uint32_t> > >("Medians");
0125       produces<edm::DetSetVector<SiStripRawDigi> >("PedestalsOrdered");
0126       produces<edm::DetSetVector<SiStripProcessedRawDigi> >("NoisesOrdered");
0127       produces<edm::DetSetVector<SiStripRawDigi> >("PedSubtrDigisOrdered");
0128       produces<edm::DetSetVector<SiStripRawDigi> >("CMSubtrDigisOrdered");
0129     } else {  //by module
0130       produces<edm::DetSetVector<SiStripRawDigi> >("ModulePedestals");
0131       produces<edm::DetSetVector<SiStripProcessedRawDigi> >("ModuleNoises");
0132       produces<edm::DetSetVector<SiStripRawDigi> >("PedSubtrModuleDigis");
0133       produces<std::map<uint32_t, std::vector<uint32_t> > >("ModuleMedians");
0134       produces<edm::DetSetVector<SiStripRawDigi> >("CMSubtrModuleDigis");
0135       produces<edm::DetSetVector<SiStripDigi> >("ZSModuleDigis");
0136     }  //end of by module check
0137 
0138   }  //end of FEDEmulatorModule constructor
0139 
0140   FEDEmulatorModule::~FEDEmulatorModule() {}
0141 
0142   void FEDEmulatorModule::updateFedCabling(const SiStripFedCablingRcd& rcd) {
0143     fedCabling_ = &rcd.get(fedCablingToken_);
0144   }
0145   void FEDEmulatorModule::updatePedestals(const SiStripPedestalsRcd& rcd) { pedestals_ = &rcd.get(pedestalsToken_); }
0146   void FEDEmulatorModule::updateNoises(const SiStripNoisesRcd& rcd) { noises_ = &rcd.get(noisesToken_); }
0147 
0148   // ------------ method called to for each event  ------------
0149   void FEDEmulatorModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0150     cablingWatcher_.check(iSetup);
0151     pedestalsWatcher_.check(iSetup);
0152     noisesWatcher_.check(iSetup);
0153 
0154     //initialise the algorithms object for the zero suppression
0155     algorithms_->initialize(iSetup);
0156 
0157     //retrieve the digis
0158     edm::Handle<edm::DetSetVector<SiStripRawDigi> > lDigisHandle;
0159     try {  //to get the digis from the event
0160       if (!byModule_) {
0161         iEvent.getByToken(spyReorderedDigisToken_, lDigisHandle);
0162       } else {  //digis supplied by module
0163         iEvent.getByToken(spyVirginRawDigisToken_, lDigisHandle);
0164       }  //end of by module check
0165     } catch (const cms::Exception& e) {
0166       std::cout << e.what();
0167       return;
0168     }  //end of get digis try
0169 
0170     const edm::DetSetVector<SiStripRawDigi>* lInputDigis = lDigisHandle.product();
0171 
0172     unsigned int lNDigis = lInputDigis->size();
0173 
0174     //define output containers
0175     //reserve space, will push_back elements
0176     std::vector<edm::DetSetVector<SiStripRawDigi>::detset> pedsData;
0177     pedsData.reserve(lNDigis);
0178     std::vector<edm::DetSetVector<SiStripProcessedRawDigi>::detset> noiseData;
0179     noiseData.reserve(lNDigis);
0180     std::vector<edm::DetSetVector<SiStripRawDigi>::detset> pedSubtrData;
0181     pedSubtrData.reserve(lNDigis);
0182     std::vector<edm::DetSetVector<SiStripRawDigi>::detset> cmSubtrData;
0183     cmSubtrData.reserve(lNDigis);
0184     //zero suppressed contained - no fixed size (could be empty) so no need to reserve elements.
0185     std::vector<edm::DetSet<SiStripDigi> > zsData;
0186 
0187     //this is a map: no reserve/resize
0188     std::map<uint32_t, std::vector<uint32_t> > medsData;
0189 
0190     edm::DetSetVector<SiStripRawDigi>::const_iterator inputChannel = lInputDigis->begin();
0191 
0192     for (; inputChannel != lInputDigis->end(); ++inputChannel) {  //loop on input channels
0193       uint32_t lDetId = inputChannel->detId();                    //either fedIndex or detId
0194 
0195       pedsData.push_back(edm::DetSetVector<SiStripRawDigi>::detset(lDetId));
0196       noiseData.push_back(edm::DetSetVector<SiStripProcessedRawDigi>::detset(lDetId));
0197       pedSubtrData.push_back(edm::DetSetVector<SiStripRawDigi>::detset(lDetId));
0198       cmSubtrData.push_back(edm::DetSetVector<SiStripRawDigi>::detset(lDetId));
0199 
0200       unsigned int lNStrips = inputChannel->size();
0201 
0202       //define output digi containers
0203       std::vector<SiStripRawDigi>& pedsDetSetData = pedsData.back().data;
0204       pedsDetSetData.reserve(lNStrips);
0205       std::vector<SiStripProcessedRawDigi>& noiseDetSetData = noiseData.back().data;
0206       noiseDetSetData.reserve(lNStrips);
0207       std::vector<SiStripRawDigi>& pedSubtrDetSetData = pedSubtrData.back().data;
0208       pedSubtrDetSetData.reserve(lNStrips);
0209       std::vector<SiStripRawDigi>& cmSubtrDetSetData = cmSubtrData.back().data;
0210       cmSubtrDetSetData.reserve(lNStrips);
0211       //zero suppressed - slightly different procedure as not fixed size
0212       edm::DetSet<SiStripDigi> zsDetSetData(lDetId);
0213 
0214       //determine the number of APV pairs in the channel
0215       uint32_t lNPairs = static_cast<uint32_t>(lNStrips * 1. / sistrip::STRIPS_PER_FEDCH);
0216       uint32_t lPair = 0;
0217 
0218       std::vector<uint32_t> medsDetSetData;
0219       medsDetSetData.reserve(lNPairs * 2);  //2*number of pairs per module. If not by module, lNPairs = 1...
0220 
0221       if (!byModule_) {  //the input is not stored by module
0222         //need to retrieve the proper detId from cabling
0223         uint16_t lFedId = 0;
0224         uint16_t lFedChannel = 0;
0225         sistrip::SpyUtilities::fedIndex(lDetId, lFedId, lFedChannel);
0226 
0227         const FedChannelConnection& lConnection = fedCabling_->fedConnection(lFedId, lFedChannel);
0228         lDetId = lConnection.detId();
0229         lNPairs = lConnection.nApvPairs();
0230         lPair = lConnection.apvPairNumber();
0231       }  //end of by module check
0232 
0233       fedEmulator_.initialiseModule(lDetId, lNPairs, lPair);
0234 
0235       //get the pedestal values
0236       //stored by module in the database
0237       fedEmulator_.retrievePedestals(pedestals_);  // FIXME maybe move into FEDEmulator then?
0238       fedEmulator_.retrieveNoises(noises_);
0239 
0240       //last option: fill medians from these ped subtr data
0241       //if want something else, need to call a method to fill
0242       //the data member medians_ of the class fedEmulator.
0243       fedEmulator_.subtractPedestals(
0244           inputChannel, pedsDetSetData, noiseDetSetData, pedSubtrDetSetData, medsDetSetData, true);
0245 
0246       fedEmulator_.subtractCM(pedSubtrDetSetData, cmSubtrDetSetData);
0247 
0248       //fill the median map
0249       medsData[inputChannel->detId()] = medsDetSetData;
0250 
0251       //zero suppress the digis
0252       fedEmulator_.zeroSuppress(cmSubtrDetSetData, zsDetSetData, algorithms_);
0253       if (!zsDetSetData.empty())
0254         zsData.push_back(zsDetSetData);
0255 
0256     }  //loop on input channels
0257 
0258     std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > lPeds(new edm::DetSetVector<SiStripRawDigi>(pedsData, true));
0259     std::unique_ptr<edm::DetSetVector<SiStripProcessedRawDigi> > lNoises(
0260         new edm::DetSetVector<SiStripProcessedRawDigi>(noiseData, true));
0261 
0262     std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > lOutputPedSubtr(
0263         new edm::DetSetVector<SiStripRawDigi>(pedSubtrData, true));
0264 
0265     std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > lOutputCMSubtr(
0266         new edm::DetSetVector<SiStripRawDigi>(cmSubtrData, true));
0267 
0268     std::unique_ptr<std::map<uint32_t, std::vector<uint32_t> > > lMedians(
0269         new std::map<uint32_t, std::vector<uint32_t> >(medsData));
0270 
0271     //zero suppressed digis
0272     std::unique_ptr<edm::DetSetVector<SiStripDigi> > lOutputZS(new edm::DetSetVector<SiStripDigi>(zsData));
0273 
0274     if (!byModule_) {
0275       iEvent.put(std::move(lMedians), "Medians");
0276       iEvent.put(std::move(lPeds), "PedestalsOrdered");
0277       iEvent.put(std::move(lNoises), "NoisesOrdered");
0278       iEvent.put(std::move(lOutputPedSubtr), "PedSubtrDigisOrdered");
0279       iEvent.put(std::move(lOutputCMSubtr), "CMSubtrDigisOrdered");
0280     } else {
0281       iEvent.put(std::move(lPeds), "ModulePedestals");
0282       iEvent.put(std::move(lNoises), "ModuleNoises");
0283       iEvent.put(std::move(lOutputPedSubtr), "PedSubtrModuleDigis");
0284       iEvent.put(std::move(lMedians), "ModuleMedians");
0285       iEvent.put(std::move(lOutputCMSubtr), "CMSubtrModuleDigis");
0286       iEvent.put(std::move(lOutputZS), "ZSModuleDigis");
0287     }
0288 
0289   }  //produce method
0290 }  //namespace sistrip
0291 
0292 //
0293 // Define as a plug-in
0294 //
0295 
0296 #include "FWCore/Framework/interface/MakerMacros.h"
0297 typedef sistrip::FEDEmulatorModule SiStripFEDEmulatorModule;
0298 DEFINE_FWK_MODULE(SiStripFEDEmulatorModule);