CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/SiStripMonitorHardware/plugins/SiStripFEDEmulatorModule.cc

Go to the documentation of this file.
00001 // Original Author:  Anne-Marie Magnan
00002 //         Created:  2010/01/21
00003 // $Id: SiStripFEDEmulatorModule.cc,v 1.3 2010/04/29 09:45:12 dellaric Exp $
00004 //
00005 
00006 #include <sstream>
00007 #include <memory>
00008 #include <list>
00009 #include <algorithm>
00010 #include <cassert>
00011 #include <vector>
00012 
00013 #include "FWCore/Framework/interface/Frameworkfwd.h"
00014 #include "FWCore/Framework/interface/EDProducer.h"
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Framework/interface/EventSetup.h"
00017 #include "FWCore/Framework/interface/ESHandle.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 #include "FWCore/Utilities/interface/InputTag.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include "FWCore/ServiceRegistry/interface/Service.h"
00022 #include "FWCore/Utilities/interface/Exception.h"
00023 
00024 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00025 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00026 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00027 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00028 #include "DataFormats/Common/interface/DetSetVector.h"
00029 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
00030 #include "DataFormats/SiStripDigi/interface/SiStripProcessedRawDigi.h"
00031 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00032 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
00033 
00034 #include "CondFormats/SiStripObjects/interface/SiStripPedestals.h"
00035 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
00036 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00037 
00038 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
00039 
00040 //for the zero suppression algorithm(s)
00041 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingFactory.h"
00042 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingAlgorithms.h"
00043 
00044 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDEmulator.h"
00045 #include "DQM/SiStripMonitorHardware/interface/SiStripSpyUtilities.h"
00046 
00047 using edm::LogError;
00048 using edm::LogInfo;
00049 using edm::LogWarning;
00050 
00051 namespace sistrip
00052 {
00053 
00054   //
00055   // Class declaration
00056   //
00057 
00058   class FEDEmulatorModule : public edm::EDProducer
00059   {
00060   public:
00061 
00062     explicit FEDEmulatorModule(const edm::ParameterSet&);
00063     ~FEDEmulatorModule();
00064 
00065   private:
00066 
00067     virtual void beginRun( edm::Run&, const edm::EventSetup& );
00068     virtual void produce(edm::Event&, const edm::EventSetup&);
00069     //virtual void endJob();
00070 
00071     //tag of spydata collection
00072     edm::InputTag spyReorderedDigisTag_;
00073     edm::InputTag spyVirginRawDigisTag_;
00074 
00075     //by fedIndex or module detid
00076     bool byModule_;
00077 
00078     sistrip::FEDEmulator fedEmulator_;
00079 
00080     static const char* messageLabel_;
00081   
00082     std::auto_ptr<SiStripRawProcessingAlgorithms> algorithms_; 
00083 
00084     //utilities for cabling etc...
00085     SpyUtilities utility_;
00086 
00087   };
00088 
00089 }//~sistrip
00090 
00091 namespace sistrip {
00092 
00093   //
00094   // Constructors and destructor
00095   //
00096   const char* FEDEmulator::messageLabel_ = "SiStripFEDEmulatorModule";
00097 
00098 
00099   FEDEmulatorModule::FEDEmulatorModule(const edm::ParameterSet& iConfig): 
00100     spyReorderedDigisTag_(iConfig.getParameter<edm::InputTag>("SpyReorderedDigisTag")),
00101     spyVirginRawDigisTag_(iConfig.getParameter<edm::InputTag>("SpyVirginRawDigisTag")),
00102     byModule_(iConfig.getParameter<bool>("ByModule")),
00103     algorithms_(SiStripRawProcessingFactory::create(iConfig.getParameter<edm::ParameterSet>("Algorithms"))) 
00104   {
00105 
00106     fedEmulator_.initialise(byModule_);
00107 
00108     if (!byModule_) { //if not by module
00109       //the medians will be produced by fed id/channel
00110       produces<std::map<uint32_t,std::vector<uint32_t> > >("Medians");
00111       produces<edm::DetSetVector<SiStripRawDigi> >("PedestalsOrdered");
00112       produces<edm::DetSetVector<SiStripProcessedRawDigi> >("NoisesOrdered");
00113       produces<edm::DetSetVector<SiStripRawDigi> >("PedSubtrDigisOrdered");
00114       produces<edm::DetSetVector<SiStripRawDigi> >("CMSubtrDigisOrdered");
00115     }
00116     else { //by module
00117       produces<edm::DetSetVector<SiStripRawDigi> >("ModulePedestals");
00118       produces<edm::DetSetVector<SiStripProcessedRawDigi> >("ModuleNoises");
00119       produces<edm::DetSetVector<SiStripRawDigi> >("PedSubtrModuleDigis");
00120       produces<std::map<uint32_t,std::vector<uint32_t> > >("ModuleMedians");
00121       produces<edm::DetSetVector<SiStripRawDigi> >("CMSubtrModuleDigis");
00122       produces<edm::DetSetVector<SiStripDigi> >("ZSModuleDigis");
00123     }//end of by module check
00124 
00125   }//end of FEDEmulatorModule constructor
00126 
00127   FEDEmulatorModule::~FEDEmulatorModule()
00128   {
00129   }
00130 
00131   // ------------ method called to for each event  ------------
00132   void
00133   FEDEmulatorModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00134   {
00135     //update cabling and pedestals
00136     const SiStripFedCabling* lCabling = utility_.getCabling( iSetup );
00137     edm::ESHandle<SiStripPedestals> lPedsHandle = utility_.getPedestalHandle(iSetup);
00138     edm::ESHandle<SiStripNoises> lNoiseHandle = utility_.getNoiseHandle(iSetup);
00139   
00140     //initialise the algorithms object for the zero suppression
00141     algorithms_->initialize(iSetup);
00142 
00143     //retrieve the digis
00144     edm::Handle<edm::DetSetVector<SiStripRawDigi> > lDigisHandle;
00145     try { //to get the digis from the event
00146       if (!byModule_) {
00147         iEvent.getByLabel(spyReorderedDigisTag_, lDigisHandle);
00148       }
00149       else { //digis supplied by module
00150         iEvent.getByLabel(spyVirginRawDigisTag_, lDigisHandle);
00151       }//end of by module check
00152     } catch (const cms::Exception& e) {
00153       std::cout << e.what() ;
00154       return;
00155     } //end of get digis try
00156 
00157     const edm::DetSetVector<SiStripRawDigi> * lInputDigis = lDigisHandle.product();
00158 
00159     unsigned int lNDigis = lInputDigis->size();
00160 
00161     //define output containers
00162     //reserve space, will push_back elements
00163     std::vector<edm::DetSetVector<SiStripRawDigi>::detset > pedsData;
00164     pedsData.reserve(lNDigis);
00165     std::vector<edm::DetSetVector<SiStripProcessedRawDigi>::detset > noiseData;
00166     noiseData.reserve(lNDigis);
00167     std::vector<edm::DetSetVector<SiStripRawDigi>::detset > pedSubtrData;
00168     pedSubtrData.reserve(lNDigis);
00169     std::vector<edm::DetSetVector<SiStripRawDigi>::detset > cmSubtrData;
00170     cmSubtrData.reserve(lNDigis);
00171     //zero suppressed contained - no fixed size (could be empty) so no need to reserve elements.
00172     std::vector< edm::DetSet<SiStripDigi> > zsData;
00173     
00174     //this is a map: no reserve/resize
00175     std::map<uint32_t,std::vector<uint32_t> > medsData;
00176 
00177     edm::DetSetVector<SiStripRawDigi>::const_iterator inputChannel = lInputDigis->begin();
00178 
00179     for ( ; inputChannel!=lInputDigis->end(); ++inputChannel){//loop on input channels
00180       uint32_t lDetId = inputChannel->detId(); //either fedIndex or detId
00181 
00182       pedsData.push_back(    edm::DetSetVector<SiStripRawDigi>::detset(lDetId) );
00183       noiseData.push_back(   edm::DetSetVector<SiStripProcessedRawDigi>::detset(lDetId) );
00184       pedSubtrData.push_back(edm::DetSetVector<SiStripRawDigi>::detset(lDetId) );
00185       cmSubtrData.push_back( edm::DetSetVector<SiStripRawDigi>::detset(lDetId) );
00186 
00187       unsigned int lNStrips = inputChannel->size();
00188 
00189       //define output digi containers
00190       std::vector<SiStripRawDigi>& pedsDetSetData = pedsData.back().data;
00191       pedsDetSetData.reserve(lNStrips);
00192       std::vector<SiStripProcessedRawDigi>& noiseDetSetData = noiseData.back().data;
00193       noiseDetSetData.reserve(lNStrips);
00194       std::vector<SiStripRawDigi>& pedSubtrDetSetData = pedSubtrData.back().data;
00195       pedSubtrDetSetData.reserve(lNStrips);
00196       std::vector<SiStripRawDigi>& cmSubtrDetSetData = cmSubtrData.back().data;
00197       cmSubtrDetSetData.reserve(lNStrips);
00198       //zero suppressed - slightly different procedure as not fixed size
00199       edm::DetSet<SiStripDigi> zsDetSetData(lDetId);
00200       
00201       //determine the number of APV pairs in the channel
00202       uint32_t lNPairs = static_cast<uint32_t>(lNStrips*1./sistrip::STRIPS_PER_FEDCH);
00203       uint32_t lPair = 0;
00204       
00205       std::vector<uint32_t> medsDetSetData;
00206       medsDetSetData.reserve(lNPairs*2); //2*number of pairs per module. If not by module, lNPairs = 1...
00207       
00208       if (!byModule_) { //the input is not stored by module
00209         //need to retrieve the proper detId from cabling
00210         uint16_t lFedId = 0;
00211         uint16_t lFedChannel = 0;
00212         sistrip::SpyUtilities::fedIndex(lDetId, lFedId, lFedChannel);
00213                 
00214         const FedChannelConnection & lConnection = lCabling->connection(lFedId,lFedChannel);
00215         lDetId = lConnection.detId();
00216         lNPairs = lConnection.nApvPairs();
00217         lPair = lConnection.apvPairNumber();
00218       }//end of by module check
00219 
00220       fedEmulator_.initialiseModule(lDetId,lNPairs,lPair);
00221 
00222       //get the pedestal values
00223       //stored by module in the database
00224       fedEmulator_.retrievePedestals(lPedsHandle);
00225       fedEmulator_.retrieveNoises(lNoiseHandle);
00226       
00227       //last option: fill medians from these ped subtr data
00228       //if want something else, need to call a method to fill
00229       //the data member medians_ of the class fedEmulator.
00230       fedEmulator_.subtractPedestals(inputChannel,
00231                                      pedsDetSetData,
00232                                      noiseDetSetData,
00233                                      pedSubtrDetSetData,
00234                                      medsDetSetData,
00235                                      true);
00236       
00237       fedEmulator_.subtractCM(pedSubtrDetSetData,cmSubtrDetSetData);
00238 
00239       //fill the median map
00240       medsData[inputChannel->detId()] = medsDetSetData;
00241 
00242       //zero suppress the digis
00243       fedEmulator_.zeroSuppress(cmSubtrDetSetData, zsDetSetData, algorithms_);
00244       if (zsDetSetData.size()) zsData.push_back( zsDetSetData );
00245       
00246     }//loop on input channels
00247 
00248 
00249     std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > lPeds(new edm::DetSetVector<SiStripRawDigi>(pedsData,true));
00250     std::auto_ptr<edm::DetSetVector<SiStripProcessedRawDigi> > lNoises(new edm::DetSetVector<SiStripProcessedRawDigi>(noiseData,true));
00251 
00252     std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > lOutputPedSubtr(new edm::DetSetVector<SiStripRawDigi>(pedSubtrData,true));
00253 
00254     std::auto_ptr<edm::DetSetVector<SiStripRawDigi> > lOutputCMSubtr(new edm::DetSetVector<SiStripRawDigi>(cmSubtrData,true));
00255 
00256     std::auto_ptr<std::map<uint32_t,std::vector<uint32_t> > > lMedians(new std::map<uint32_t,std::vector<uint32_t> >(medsData));
00257   
00258     //zero suppressed digis
00259     std::auto_ptr< edm::DetSetVector<SiStripDigi> > lOutputZS(new edm::DetSetVector<SiStripDigi>(zsData));
00260     
00261     if (!byModule_) {
00262       iEvent.put(lMedians,"Medians");
00263       iEvent.put(lPeds,"PedestalsOrdered");
00264       iEvent.put(lNoises,"NoisesOrdered");
00265       iEvent.put(lOutputPedSubtr,"PedSubtrDigisOrdered");
00266       iEvent.put(lOutputCMSubtr,"CMSubtrDigisOrdered");
00267     }
00268     else {
00269       iEvent.put(lPeds,"ModulePedestals");
00270       iEvent.put(lNoises,"ModuleNoises");
00271       iEvent.put(lOutputPedSubtr,"PedSubtrModuleDigis");
00272       iEvent.put(lMedians,"ModuleMedians");
00273       iEvent.put(lOutputCMSubtr,"CMSubtrModuleDigis");
00274       iEvent.put(lOutputZS,"ZSModuleDigis");
00275     }
00276 
00277   }//produce method
00278 
00279   // ------------ method called once each job just before starting event loop  ------------
00280   void 
00281   FEDEmulatorModule::beginRun( edm::Run&, const edm::EventSetup& )
00282   {
00283 
00284   }
00285 
00286 
00287 
00288 
00289 }//namespace sistrip
00290 
00291 //
00292 // Define as a plug-in
00293 //
00294 
00295 #include "FWCore/Framework/interface/MakerMacros.h"
00296 typedef sistrip::FEDEmulatorModule SiStripFEDEmulatorModule;
00297 DEFINE_FWK_MODULE(SiStripFEDEmulatorModule);