CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/DPGAnalysis/SiStripTools/plugins/SiStripQualityHistory.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    SiStripTools
00004 // Class:      SiStripQualityHistory
00005 // 
00013 //
00014 // Original Author:  Andrea Venturi
00015 //         Created:  Tue Sep 18 17:52:00 CEST 2009
00016 //
00017 //
00018 
00019 
00020 // system include files
00021 #include <memory>
00022 
00023 // user include files
00024 
00025 #include <vector>
00026 #include <map>
00027 
00028 //#include "TGraph.h"
00029 #include "TH1F.h"
00030 
00031 #include "FWCore/Framework/interface/Frameworkfwd.h"
00032 #include "FWCore/Framework/interface/EDAnalyzer.h"
00033 
00034 #include "FWCore/Framework/interface/Event.h"
00035 #include "FWCore/Framework/interface/MakerMacros.h"
00036 #include "FWCore/Framework/interface/Run.h"
00037 
00038 #include "FWCore/Framework/interface/ESHandle.h"
00039 
00040 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00041 
00042 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00043 
00044 #include "FWCore/ServiceRegistry/interface/Service.h"
00045 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00046 
00047 #include "FWCore/Utilities/interface/InputTag.h"
00048 
00049 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
00050 #include "CalibTracker/Records/interface/SiStripQualityRcd.h"
00051 
00052 #include "DPGAnalysis/SiStripTools/interface/RunHistogramManager.h"
00053 //
00054 // class decleration
00055 //
00056 
00057 class SiStripQualityHistory : public edm::EDAnalyzer {
00058  public:
00059     explicit SiStripQualityHistory(const edm::ParameterSet&);
00060     ~SiStripQualityHistory();
00061 
00062   enum {Module,Fiber,APV,Strip};
00063 
00064    private:
00065       virtual void beginJob() ;
00066       virtual void beginRun(const edm::Run&, const edm::EventSetup&) ;
00067       virtual void endRun(const edm::Run&, const edm::EventSetup&) ;
00068       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00069       virtual void endJob() ;
00070 
00071       // ----------member data ---------------------------
00072 
00073   RunHistogramManager m_rhm;
00074   const std::vector<edm::ParameterSet> _monitoredssq;
00075   const unsigned int _mode;
00076   const bool m_run;
00077   const unsigned int m_maxLS;
00078   const unsigned int m_LSfrac;
00079   //  std::map<std::string,TGraph*> _history;
00080   std::map<std::string,TH1F*> _history;
00081   std::map<std::string,TProfile**> m_badmodrun;
00082 
00083 };
00084 
00085 //
00086 // constants, enums and typedefs
00087 //
00088 
00089 //
00090 // static data member definitions
00091 //
00092 
00093 //
00094 // constructors and destructor
00095 //
00096 SiStripQualityHistory::SiStripQualityHistory(const edm::ParameterSet& iConfig):
00097   m_rhm(),
00098   _monitoredssq(iConfig.getParameter<std::vector<edm::ParameterSet> >("monitoredSiStripQuality")),
00099   _mode(iConfig.getUntrackedParameter<unsigned int>("granularityMode",Module)),
00100   m_run(iConfig.getParameter<bool>("runProcess")),
00101   m_maxLS(iConfig.getUntrackedParameter<unsigned int>("maxLSBeforeRebin",100)),
00102   m_LSfrac(iConfig.getUntrackedParameter<unsigned int>("startingLSFraction",4)),
00103   _history(),m_badmodrun()
00104 {
00105    //now do what ever initialization is needed
00106 
00107   edm::Service<TFileService> tfserv;
00108 
00109   for(std::vector<edm::ParameterSet>::const_iterator ps=_monitoredssq.begin();ps!=_monitoredssq.end();++ps) {
00110     
00111     std::string name = ps->getParameter<std::string>("name");
00112     //    _history[name] = tfserv->make<TGraph>();
00113     //    _history[name]->SetName(name.c_str());     _history[name]->SetTitle(name.c_str()); 
00114 
00115     if(m_run) _history[name] = tfserv->make<TH1F>(name.c_str(),name.c_str(),10,0,10);
00116 
00117     char hrunname[400];
00118     sprintf(hrunname,"badmodrun_%s",name.c_str());
00119     char hruntitle[400];
00120     sprintf(hruntitle,"Number of bad modules %s",name.c_str());
00121     m_badmodrun[name] = m_rhm.makeTProfile(hrunname,hruntitle,m_LSfrac*m_maxLS,0,m_maxLS*262144);
00122   }
00123 
00124 }
00125 
00126 
00127 SiStripQualityHistory::~SiStripQualityHistory()
00128 {
00129  
00130    // do anything here that needs to be done at desctruction time
00131    // (e.g. close files, deallocate resources etc.)
00132 
00133 }
00134 
00135 
00136 //
00137 // member functions
00138 //
00139 
00140 // ------------ method called to for each event  ------------
00141 void
00142 SiStripQualityHistory::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00143 {
00144   //  edm::LogInfo("EventProcessing") << "event being processed";
00145 
00146   for(std::vector<edm::ParameterSet>::const_iterator ps=_monitoredssq.begin();ps!=_monitoredssq.end();++ps) {
00147     
00148     std::string name = ps->getParameter<std::string>("name");
00149     std::string label = ps->getParameter<std::string>("ssqLabel");
00150     
00151     
00152     
00153     edm::ESHandle<SiStripQuality> ssq;
00154     iSetup.get<SiStripQualityRcd>().get(label,ssq);
00155     
00156     std::vector<SiStripQuality::BadComponent> bads = ssq->getBadComponentList();
00157     
00158     LogDebug("BadComponents") << bads.size() << " bad components found";
00159     
00160     int nbad=0;
00161     
00162     if(_mode==Module || _mode==Fiber || _mode==APV) {
00163       for(std::vector<SiStripQuality::BadComponent>::const_iterator bc=bads.begin();bc!=bads.end();++bc) {
00164         
00165         if(_mode==Module) {
00166           if(bc->BadModule) ++nbad;
00167         }
00168         else if(_mode == Fiber) {
00169           for(int fiber=1;fiber<5;fiber*=2) {
00170             if((bc->BadFibers & fiber)>0) ++nbad;
00171           }
00172         }
00173         else if(_mode ==APV) {
00174           for(int apv=1;apv<33;apv*=2) {
00175             if((bc->BadApvs & apv)>0) ++nbad;
00176           }
00177         }
00178       }
00179     }
00180     else if(_mode==Strip) {
00181       SiStripBadStrip::ContainerIterator dbegin = ssq->getDataVectorBegin();
00182       SiStripBadStrip::ContainerIterator dend = ssq->getDataVectorEnd();
00183       for(SiStripBadStrip::ContainerIterator data = dbegin; data < dend; ++data) {
00184         nbad += ssq->decode(*data).range;
00185       }
00186 
00187     }
00188 
00189     if(m_badmodrun.find(name)!=m_badmodrun.end() && m_badmodrun[name] && *m_badmodrun[name]) {
00190       (*m_badmodrun[name])->Fill(iEvent.orbitNumber(),nbad);
00191     }
00192     
00193   }
00194 }
00195 
00196 void 
00197 SiStripQualityHistory::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
00198 {
00199 
00200   m_rhm.beginRun(iRun);
00201   
00202   // loop on all the SiStripQuality objects to be monitored
00203 
00204   for(std::vector<edm::ParameterSet>::const_iterator ps=_monitoredssq.begin();ps!=_monitoredssq.end();++ps) {
00205     
00206     std::string name = ps->getParameter<std::string>("name");
00207     std::string label = ps->getParameter<std::string>("ssqLabel");
00208     
00209     if(m_badmodrun.find(name)!=m_badmodrun.end()) {
00210       if(m_badmodrun[name] && *m_badmodrun[name]) {
00211         (*m_badmodrun[name])->SetBit(TH1::kCanRebin);
00212         (*m_badmodrun[name])->GetXaxis()->SetTitle("time [Orb#]"); (*m_badmodrun[name])->GetYaxis()->SetTitle("bad components"); 
00213       }
00214     }
00215     
00216     if(m_run) {
00217 
00218       edm::ESHandle<SiStripQuality> ssq;
00219       iSetup.get<SiStripQualityRcd>().get(label,ssq);
00220       
00221       std::vector<SiStripQuality::BadComponent> bads = ssq->getBadComponentList();
00222       
00223       LogDebug("BadComponents") << bads.size() << " bad components found";
00224       
00225       int nbad=0;
00226       
00227       if(_mode==Module || _mode==Fiber || _mode==APV) {
00228         for(std::vector<SiStripQuality::BadComponent>::const_iterator bc=bads.begin();bc!=bads.end();++bc) {
00229           
00230           if(_mode==Module) {
00231             if(bc->BadModule) ++nbad;
00232           }
00233           else if(_mode == Fiber) {
00234             for(int fiber=1;fiber<5;fiber*=2) {
00235               if((bc->BadFibers & fiber)>0) ++nbad;
00236             }
00237           }
00238           else if(_mode ==APV) {
00239             for(int apv=1;apv<33;apv*=2) {
00240               if((bc->BadApvs & apv)>0) ++nbad;
00241             }
00242           }
00243         }
00244       }
00245       else if(_mode==Strip) {
00246         SiStripBadStrip::ContainerIterator dbegin = ssq->getDataVectorBegin();
00247         SiStripBadStrip::ContainerIterator dend = ssq->getDataVectorEnd();
00248         for(SiStripBadStrip::ContainerIterator data = dbegin; data < dend; ++data) {
00249           nbad += ssq->decode(*data).range;
00250         }
00251       }
00252       
00253       //    _history[name]->SetPoint(_history[name]->GetN(),iRun.run(),nbad);
00254       char runname[100];
00255       sprintf(runname,"%d",iRun.run());
00256       LogDebug("AnalyzedRun") << name << " " << runname << " " << nbad;
00257       _history[name]->Fill(runname,nbad);
00258     }    
00259   }
00260   
00261 }
00262 
00263 void 
00264 SiStripQualityHistory::endRun(const edm::Run& iRun, const edm::EventSetup&)
00265 {}
00266 
00267 
00268 // ------------ method called once each job just before starting event loop  ------------
00269 void 
00270 SiStripQualityHistory::beginJob()
00271 {}
00272 
00273 // ------------ method called once each job just after ending the event loop  ------------
00274 void 
00275 SiStripQualityHistory::endJob() 
00276 {
00277   /*
00278   for(std::vector<edm::ParameterSet>::const_iterator ps=_monitoredssq.begin();ps!=_monitoredssq.end();++ps) {
00279     
00280     std::string name = ps->getParameter<std::string>("name");
00281     _history[name]->Write();
00282 
00283   }
00284   */
00285 }
00286 
00287 
00288 //define this as a plug-in
00289 DEFINE_FWK_MODULE(SiStripQualityHistory);