Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:29:37

0001 #ifndef CalibTracker_SiPixelQuality_SiPixelStatusProducer_h
0002 #define CalibTracker_SiPixelQuality_SiPixelStatusProducer_h
0003 
0004 /**_________________________________________________________________
0005  *    class:   SiPixelStatusProducer.h
0006  *       package: CalibTracker/SiPixelQuality
0007  *          reference : https://twiki.cern.ch/twiki/bin/view/CMSPublic/FWMultithreadedFrameworkStreamModuleInterface
0008  *________________________________________________________________**/
0009 
0010 // C++ standard
0011 #include <cstring>
0012 #include <fstream>
0013 #include <iostream>
0014 #include <memory>
0015 
0016 #include <string>
0017 // // CMS FW
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/stream/EDProducer.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/ESWatcher.h"
0022 #include "FWCore/Framework/interface/ConsumesCollector.h"
0023 #include "FWCore/Framework/interface/EventSetup.h"
0024 #include "FWCore/Framework/interface/LuminosityBlock.h"
0025 #include "FWCore/Utilities/interface/Transition.h"
0026 #include "FWCore/Utilities/interface/ESGetToken.h"
0027 // Concurrency
0028 #include "FWCore/Utilities/interface/ReusableObjectHolder.h"
0029 #include "FWCore/Concurrency/interface/SerialTaskQueue.h"
0030 
0031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0032 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0033 #include "FWCore/Utilities/interface/ESGetToken.h"
0034 
0035 // // Pixel data format
0036 #include "CalibTracker/SiPixelQuality/interface/SiPixelDetectorStatus.h"
0037 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0038 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0039 #include "DataFormats/DetId/interface/DetId.h"
0040 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0041 #include "DataFormats/SiPixelDetId/interface/PixelFEDChannel.h"
0042 // Tracker Geo
0043 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0044 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0045 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0046 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0047 // SiPixelTopoFinder
0048 #include "CalibTracker/SiPixelQuality/interface/SiPixelTopoFinder.h"
0049 // SiPixelDetectorStatus
0050 #include "CalibTracker/SiPixelQuality/interface/SiPixelDetectorStatus.h"
0051 
0052 /* Cache to pertain SiPixelTopoFinder */
0053 class SiPixelStatusCache {
0054 public:
0055   //NOTE: these are only changes in the constructor call
0056   mutable edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> trackerGeometryToken_;
0057   mutable edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyToken_;
0058   mutable edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> siPixelFedCablingMapToken_;
0059 };
0060 
0061 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
0062 
0063 class SiPixelStatusProducer :
0064 
0065     public edm::stream::EDProducer<edm::GlobalCache<SiPixelStatusCache>,
0066                                    edm::RunCache<SiPixelTopoFinder>,
0067                                    edm::LuminosityBlockSummaryCache<std::vector<SiPixelDetectorStatus>>,
0068                                    edm::EndLuminosityBlockProducer,
0069                                    edm::Accumulator> {
0070 public:
0071   SiPixelStatusProducer(edm::ParameterSet const& iPSet, SiPixelStatusCache const*);
0072   ~SiPixelStatusProducer() override;
0073 
0074   /* module description */
0075   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0076     edm::ParameterSetDescription desc;
0077     {
0078       edm::ParameterSetDescription psd0;
0079       psd0.addUntracked<edm::InputTag>("pixelClusterLabel", edm::InputTag("siPixelClusters", "", "RECO"));
0080       psd0.add<std::vector<edm::InputTag>>("badPixelFEDChannelCollections",
0081                                            {
0082                                                edm::InputTag("siPixelDigis"),
0083                                            });
0084       desc.add<edm::ParameterSetDescription>("SiPixelStatusProducerParameters", psd0);
0085     }
0086     descriptions.add("siPixelStatusProducer", desc);
0087   }
0088 
0089   /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
0090 
0091   /* For each instance of the module*/
0092   void beginRun(edm::Run const&, edm::EventSetup const&) final;
0093 
0094   void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) final;
0095   void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) final;
0096 
0097   void accumulate(edm::Event const& iEvent, edm::EventSetup const& iSetup) final;
0098 
0099   void endLuminosityBlockSummary(edm::LuminosityBlock const& iLumi,
0100                                  edm::EventSetup const&,
0101                                  std::vector<SiPixelDetectorStatus>* siPixelDetectorStatusVtr) const final;  //override;
0102 
0103   /* For global or runCache */
0104 
0105   static std::unique_ptr<SiPixelStatusCache> initializeGlobalCache(edm::ParameterSet const& iPSet) {
0106     edm::LogInfo("SiPixelStatusProducer") << "Init global Cache " << std::endl;
0107     return std::make_unique<SiPixelStatusCache>();
0108   }
0109 
0110   static std::shared_ptr<SiPixelTopoFinder> globalBeginRun(edm::Run const& iRun,
0111                                                            edm::EventSetup const& iSetup,
0112                                                            GlobalCache const* iCache);
0113 
0114   static void globalEndRun(edm::Run const& iRun, edm::EventSetup const&, RunContext const* iContext) {
0115     /* Do nothing */
0116   }
0117 
0118   static void globalEndJob(SiPixelStatusCache const*) { /* Do nothing */
0119   }
0120 
0121   static std::shared_ptr<std::vector<SiPixelDetectorStatus>> globalBeginLuminosityBlockSummary(
0122       edm::LuminosityBlock const&, edm::EventSetup const&, LuminosityBlockContext const*) {
0123     return std::make_shared<std::vector<SiPixelDetectorStatus>>();
0124   }
0125 
0126   static void globalEndLuminosityBlockSummary(edm::LuminosityBlock const&,
0127                                               edm::EventSetup const&,
0128                                               LuminosityBlockContext const* iContext,
0129                                               std::vector<SiPixelDetectorStatus>*) {
0130     /* Do nothing */
0131   }
0132 
0133   static void globalEndLuminosityBlockProduce(edm::LuminosityBlock& iLumi,
0134                                               edm::EventSetup const&,
0135                                               LuminosityBlockContext const* iContext,
0136                                               std::vector<SiPixelDetectorStatus> const* siPixelDetectorStatusVtr) {
0137     edm::LogInfo("SiPixelStatusProducer") << "Global endlumi producer " << std::endl;
0138 
0139     // only save result for non-zero event lumi block
0140     if (!siPixelDetectorStatusVtr->empty()) {
0141       int lumi = iLumi.luminosityBlock();
0142       int run = iLumi.run();
0143 
0144       SiPixelDetectorStatus siPixelDetectorStatus = SiPixelDetectorStatus();
0145       for (unsigned int instance = 0; instance < siPixelDetectorStatusVtr->size(); instance++) {
0146         siPixelDetectorStatus.updateDetectorStatus((*siPixelDetectorStatusVtr)[instance]);
0147       }
0148 
0149       siPixelDetectorStatus.setRunRange(run, run);
0150       siPixelDetectorStatus.setLSRange(lumi, lumi);
0151 
0152       if (debug_) {
0153         std::string outTxt = Form("SiPixelDetectorStatus_Run%d_Lumi%d.txt", run, lumi);
0154         std::ofstream outFile;
0155         outFile.open(outTxt.c_str(), std::ios::app);
0156         siPixelDetectorStatus.dumpToFile(outFile);
0157         outFile.close();
0158       }
0159 
0160       /* save result */
0161       auto result = std::make_unique<SiPixelDetectorStatus>();
0162       *result = siPixelDetectorStatus;
0163 
0164       iLumi.put(std::move(result), std::string("siPixelStatus"));
0165       edm::LogInfo("SiPixelStatusProducer")
0166           << " lumi-based data stored for run " << run << " lumi " << lumi << std::endl;
0167     }
0168   }
0169 
0170 private:
0171   virtual int indexROC(int irow, int icol, int nROCcolumns) final;
0172 
0173   /* ParameterSet */
0174   static const bool debug_ = false;
0175 
0176   edm::InputTag fPixelClusterLabel_;
0177   edm::EDGetTokenT<edmNew::DetSetVector<SiPixelCluster>> fSiPixelClusterToken_;
0178   std::vector<edm::EDGetTokenT<PixelFEDChannelCollection>> theBadPixelFEDChannelsTokens_;
0179 
0180   /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
0181   /* private data member, one instance per stream */
0182 
0183   /* per-Run data (The pixel topo cannot be changed during a Run) */
0184   /* vector of all <int> detIds */
0185   std::vector<int> fDetIds_;
0186   /* ROC size (number of row, number of columns for each det id) */
0187   std::map<int, std::pair<int, int>> fSensors_;
0188   /* the roc layout on a module */
0189   std::map<int, std::pair<int, int>> fSensorLayout_;
0190   /* fedId as a function of detId */
0191   std::unordered_map<uint32_t, unsigned int> fFedIds_;
0192   /* map the index ROC to rocId */
0193   std::map<int, std::map<int, int>> fRocIds_;
0194 
0195   /* per-LuminosityBlock data */
0196   unsigned long int ftotalevents_;
0197 
0198   int beginLumi_;
0199   int endLumi_;
0200   int beginRun_;
0201   int endRun_;
0202 
0203   /* Channels always have FEDerror25 for all events in the lumisection */
0204   std::map<int, std::vector<PixelFEDChannel>> fFEDerror25_;
0205 
0206   // Producer production (output collection)
0207   SiPixelDetectorStatus fDet_;
0208 };
0209 
0210 #endif