Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:53:27

0001 // -*- C++ -*-
0002 //
0003 // Package:    SiStripTools
0004 // Class:      SiStripQualityHistory
0005 //
0006 /**\class SiStripQualityHistory SiStripQualityHistory.cc DPGAnalysis/SiStripTools/plugins/SiStripQualityHistory.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Andrea Venturi
0015 //         Created:  Tue Sep 18 17:52:00 CEST 2009
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 
0024 #include <vector>
0025 #include <map>
0026 
0027 //#include "TGraph.h"
0028 #include "TH1F.h"
0029 
0030 #include "FWCore/Framework/interface/Frameworkfwd.h"
0031 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0032 
0033 #include "FWCore/Framework/interface/Event.h"
0034 #include "FWCore/Framework/interface/MakerMacros.h"
0035 #include "FWCore/Framework/interface/Run.h"
0036 
0037 #include "FWCore/Framework/interface/ESHandle.h"
0038 
0039 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0040 
0041 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0042 
0043 #include "FWCore/ServiceRegistry/interface/Service.h"
0044 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0045 
0046 #include "FWCore/Utilities/interface/InputTag.h"
0047 
0048 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0049 #include "CalibTracker/Records/interface/SiStripQualityRcd.h"
0050 
0051 #include "DPGAnalysis/SiStripTools/interface/RunHistogramManager.h"
0052 //
0053 // class decleration
0054 //
0055 
0056 class SiStripQualityHistory : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0057 public:
0058   explicit SiStripQualityHistory(const edm::ParameterSet&);
0059   ~SiStripQualityHistory() override;
0060 
0061   enum { Module, Fiber, APV, Strip };
0062 
0063 private:
0064   void beginJob() override;
0065   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0066   void endRun(const edm::Run&, const edm::EventSetup&) override {}
0067   void analyze(const edm::Event&, const edm::EventSetup&) override;
0068   void endJob() override;
0069 
0070   // ----------member data ---------------------------
0071 
0072   RunHistogramManager m_rhm;
0073   const std::vector<edm::ParameterSet> _monitoredssq;
0074   std::vector<edm::ESGetToken<SiStripQuality, SiStripQualityRcd>> _ssqTokens;
0075   const unsigned int _mode;
0076   const bool m_run;
0077   const unsigned int m_maxLS;
0078   const unsigned int m_LSfrac;
0079   //  std::map<std::string,TGraph*> _history;
0080   std::map<std::string, TH1F*> _history;
0081   std::map<std::string, TProfile**> m_badmodrun;
0082 };
0083 
0084 //
0085 // constants, enums and typedefs
0086 //
0087 
0088 //
0089 // static data member definitions
0090 //
0091 
0092 //
0093 // constructors and destructor
0094 //
0095 SiStripQualityHistory::SiStripQualityHistory(const edm::ParameterSet& iConfig)
0096     : m_rhm(consumesCollector()),
0097       _monitoredssq(iConfig.getParameter<std::vector<edm::ParameterSet>>("monitoredSiStripQuality")),
0098       _mode(iConfig.getUntrackedParameter<unsigned int>("granularityMode", Module)),
0099       m_run(iConfig.getParameter<bool>("runProcess")),
0100       m_maxLS(iConfig.getUntrackedParameter<unsigned int>("maxLSBeforeRebin", 100)),
0101       m_LSfrac(iConfig.getUntrackedParameter<unsigned int>("startingLSFraction", 4)),
0102       _history(),
0103       m_badmodrun() {
0104   //now do what ever initialization is needed
0105   usesResource(TFileService::kSharedResource);
0106 
0107   edm::Service<TFileService> tfserv;
0108 
0109   for (const auto& ps : _monitoredssq) {
0110     _ssqTokens.emplace_back(
0111         esConsumes<edm::Transition::BeginRun>(edm::ESInputTag{"", ps.getParameter<std::string>("ssqLabel")}));
0112     std::string name = ps.getParameter<std::string>("name");
0113     //    _history[name] = tfserv->make<TGraph>();
0114     //    _history[name]->SetName(name.c_str());     _history[name]->SetTitle(name.c_str());
0115 
0116     if (m_run)
0117       _history[name] = tfserv->make<TH1F>(name.c_str(), name.c_str(), 10, 0, 10);
0118 
0119     char hrunname[400];
0120     sprintf(hrunname, "badmodrun_%s", name.c_str());
0121     char hruntitle[400];
0122     sprintf(hruntitle, "Number of bad modules %s", name.c_str());
0123     m_badmodrun[name] = m_rhm.makeTProfile(hrunname, hruntitle, m_LSfrac * m_maxLS, 0, m_maxLS * 262144);
0124   }
0125 }
0126 
0127 SiStripQualityHistory::~SiStripQualityHistory() {
0128   // do anything here that needs to be done at desctruction time
0129   // (e.g. close files, deallocate resources etc.)
0130 }
0131 
0132 //
0133 // member functions
0134 //
0135 
0136 // ------------ method called to for each event  ------------
0137 void SiStripQualityHistory::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0138   //  edm::LogInfo("EventProcessing") << "event being processed";
0139 
0140   for (std::size_t iMon = 0; iMon != _monitoredssq.size(); ++iMon) {
0141     std::string name = _monitoredssq[iMon].getParameter<std::string>("name");
0142     const auto& ssq = iSetup.getData(_ssqTokens[iMon]);
0143 
0144     std::vector<SiStripQuality::BadComponent> bads = ssq.getBadComponentList();
0145 
0146     LogDebug("BadComponents") << bads.size() << " bad components found";
0147 
0148     int nbad = 0;
0149 
0150     if (_mode == Module || _mode == Fiber || _mode == APV) {
0151       for (const auto& bc : bads) {
0152         if (_mode == Module) {
0153           if (bc.BadModule)
0154             ++nbad;
0155         } else if (_mode == Fiber) {
0156           for (int fiber = 1; fiber < 5; fiber *= 2) {
0157             if ((bc.BadFibers & fiber) > 0)
0158               ++nbad;
0159           }
0160         } else if (_mode == APV) {
0161           for (int apv = 1; apv < 33; apv *= 2) {
0162             if ((bc.BadApvs & apv) > 0)
0163               ++nbad;
0164           }
0165         }
0166       }
0167     } else if (_mode == Strip) {
0168       SiStripBadStrip::ContainerIterator dbegin = ssq.getDataVectorBegin();
0169       SiStripBadStrip::ContainerIterator dend = ssq.getDataVectorEnd();
0170       for (SiStripBadStrip::ContainerIterator data = dbegin; data < dend; ++data) {
0171         nbad += ssq.decode(*data).range;
0172       }
0173     }
0174 
0175     if (m_badmodrun.find(name) != m_badmodrun.end() && m_badmodrun[name] && *m_badmodrun[name]) {
0176       (*m_badmodrun[name])->Fill(iEvent.orbitNumber(), nbad);
0177     }
0178   }
0179 }
0180 
0181 void SiStripQualityHistory::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0182   m_rhm.beginRun(iRun);
0183 
0184   // loop on all the SiStripQuality objects to be monitored
0185   for (std::size_t iMon = 0; iMon != _monitoredssq.size(); ++iMon) {
0186     std::string name = _monitoredssq[iMon].getParameter<std::string>("name");
0187 
0188     if (m_badmodrun.find(name) != m_badmodrun.end()) {
0189       if (m_badmodrun[name] && *m_badmodrun[name]) {
0190         (*m_badmodrun[name])->SetCanExtend(TH1::kXaxis);
0191         (*m_badmodrun[name])->GetXaxis()->SetTitle("time [Orb#]");
0192         (*m_badmodrun[name])->GetYaxis()->SetTitle("bad components");
0193       }
0194     }
0195 
0196     if (m_run) {
0197       const auto& ssq = iSetup.getData(_ssqTokens[iMon]);
0198 
0199       std::vector<SiStripQuality::BadComponent> bads = ssq.getBadComponentList();
0200 
0201       LogDebug("BadComponents") << bads.size() << " bad components found";
0202 
0203       int nbad = 0;
0204 
0205       if (_mode == Module || _mode == Fiber || _mode == APV) {
0206         for (const auto& bc : bads) {
0207           if (_mode == Module) {
0208             if (bc.BadModule)
0209               ++nbad;
0210           } else if (_mode == Fiber) {
0211             for (int fiber = 1; fiber < 5; fiber *= 2) {
0212               if ((bc.BadFibers & fiber) > 0)
0213                 ++nbad;
0214             }
0215           } else if (_mode == APV) {
0216             for (int apv = 1; apv < 33; apv *= 2) {
0217               if ((bc.BadApvs & apv) > 0)
0218                 ++nbad;
0219             }
0220           }
0221         }
0222       } else if (_mode == Strip) {
0223         SiStripBadStrip::ContainerIterator dbegin = ssq.getDataVectorBegin();
0224         SiStripBadStrip::ContainerIterator dend = ssq.getDataVectorEnd();
0225         for (SiStripBadStrip::ContainerIterator data = dbegin; data < dend; ++data) {
0226           nbad += ssq.decode(*data).range;
0227         }
0228       }
0229 
0230       //    _history[name]->SetPoint(_history[name]->GetN(),iRun.run(),nbad);
0231       char runname[100];
0232       sprintf(runname, "%d", iRun.run());
0233       LogDebug("AnalyzedRun") << name << " " << runname << " " << nbad;
0234       _history[name]->Fill(runname, nbad);
0235     }
0236   }
0237 }
0238 
0239 // ------------ method called once each job just before starting event loop  ------------
0240 void SiStripQualityHistory::beginJob() {}
0241 
0242 // ------------ method called once each job just after ending the event loop  ------------
0243 void SiStripQualityHistory::endJob() {
0244   /*
0245   for(std::vector<edm::ParameterSet>::const_iterator ps=_monitoredssq.begin();ps!=_monitoredssq.end();++ps) {
0246 
0247     std::string name = ps->getParameter<std::string>("name");
0248     _history[name]->Write();
0249 
0250   }
0251   */
0252 }
0253 
0254 //define this as a plug-in
0255 DEFINE_FWK_MODULE(SiStripQualityHistory);