Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   static std::shared_ptr<std::vector<SiPixelDetectorStatus>> globalBeginLuminosityBlockSummary(
0121       edm::LuminosityBlock const&, edm::EventSetup const&, LuminosityBlockContext const*) {
0122     return std::make_shared<std::vector<SiPixelDetectorStatus>>();
0123   }
0124 
0125   static void globalEndLuminosityBlockSummary(edm::LuminosityBlock const&,
0126                                               edm::EventSetup const&,
0127                                               LuminosityBlockContext const* iContext,
0128                                               std::vector<SiPixelDetectorStatus>*) {
0129     /* Do nothing */
0130   }
0131 
0132   static void globalEndLuminosityBlockProduce(edm::LuminosityBlock& iLumi,
0133                                               edm::EventSetup const&,
0134                                               LuminosityBlockContext const* iContext,
0135                                               std::vector<SiPixelDetectorStatus> const* siPixelDetectorStatusVtr) {
0136     edm::LogInfo("SiPixelStatusProducer") << "Global endlumi producer " << std::endl;
0137 
0138     // only save result for non-zero event lumi block
0139     if (!siPixelDetectorStatusVtr->empty()) {
0140       int lumi = iLumi.luminosityBlock();
0141       int run = iLumi.run();
0142 
0143       SiPixelDetectorStatus siPixelDetectorStatus = SiPixelDetectorStatus();
0144       for (unsigned int instance = 0; instance < siPixelDetectorStatusVtr->size(); instance++) {
0145         siPixelDetectorStatus.updateDetectorStatus((*siPixelDetectorStatusVtr)[instance]);
0146       }
0147 
0148       siPixelDetectorStatus.setRunRange(run, run);
0149       siPixelDetectorStatus.setLSRange(lumi, lumi);
0150 
0151       if (debug_) {
0152         std::string outTxt = Form("SiPixelDetectorStatus_Run%d_Lumi%d.txt", run, lumi);
0153         std::ofstream outFile;
0154         outFile.open(outTxt.c_str(), std::ios::app);
0155         siPixelDetectorStatus.dumpToFile(outFile);
0156         outFile.close();
0157       }
0158 
0159       /* save result */
0160       auto result = std::make_unique<SiPixelDetectorStatus>();
0161       *result = siPixelDetectorStatus;
0162 
0163       iLumi.put(std::move(result), std::string("siPixelStatus"));
0164       edm::LogInfo("SiPixelStatusProducer")
0165           << " lumi-based data stored for run " << run << " lumi " << lumi << std::endl;
0166     }
0167   }
0168 
0169 private:
0170   virtual int indexROC(int irow, int icol, int nROCcolumns) final;
0171 
0172   /* ParameterSet */
0173   static const bool debug_ = false;
0174 
0175   edm::InputTag fPixelClusterLabel_;
0176   edm::EDGetTokenT<edmNew::DetSetVector<SiPixelCluster>> fSiPixelClusterToken_;
0177   std::vector<edm::EDGetTokenT<PixelFEDChannelCollection>> theBadPixelFEDChannelsTokens_;
0178 
0179   /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
0180   /* private data member, one instance per stream */
0181 
0182   /* per-Run data (The pixel topo cannot be changed during a Run) */
0183   /* vector of all <int> detIds */
0184   std::vector<int> fDetIds_;
0185   /* ROC size (number of row, number of columns for each det id) */
0186   std::map<int, std::pair<int, int>> fSensors_;
0187   /* the roc layout on a module */
0188   std::map<int, std::pair<int, int>> fSensorLayout_;
0189   /* fedId as a function of detId */
0190   std::unordered_map<uint32_t, unsigned int> fFedIds_;
0191   /* map the index ROC to rocId */
0192   std::map<int, std::map<int, int>> fRocIds_;
0193 
0194   /* per-LuminosityBlock data */
0195   unsigned long int ftotalevents_;
0196 
0197   int beginLumi_;
0198   int endLumi_;
0199   int beginRun_;
0200   int endRun_;
0201 
0202   /* Channels always have FEDerror25 for all events in the lumisection */
0203   std::map<int, std::vector<PixelFEDChannel>> fFEDerror25_;
0204 
0205   // Producer production (output collection)
0206   SiPixelDetectorStatus fDet_;
0207 };
0208 
0209 #endif