CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/DPGAnalysis/SiStripTools/plugins/APVShotsAnalyzer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    SiStripTools
00004 // Class:      APVShotsAnalyzer
00005 // 
00013 //
00014 // Original Author:  Andrea Venturi
00015 //         Created:  Tue Jul 19 11:56:00 CEST 2009
00016 //
00017 //
00018 
00019 
00020 // system include files
00021 #include <memory>
00022 
00023 // user include files
00024 #include "TH1F.h"
00025 #include "TProfile.h"
00026 #include <vector>
00027 #include <string>
00028 
00029 #include "FWCore/Framework/interface/Frameworkfwd.h"
00030 #include "FWCore/Framework/interface/EDAnalyzer.h"
00031 
00032 #include "FWCore/Framework/interface/Event.h"
00033 #include "FWCore/Framework/interface/MakerMacros.h"
00034 
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 
00037 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00038 
00039 #include "FWCore/ServiceRegistry/interface/Service.h"
00040 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00041 
00042 #include "FWCore/Utilities/interface/InputTag.h"
00043 
00044 #include "DataFormats/Common/interface/DetSetVector.h"
00045 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00046 
00047 #include "DPGAnalysis/SiStripTools/interface/APVShotFinder.h"
00048 #include "DPGAnalysis/SiStripTools/interface/APVShot.h"
00049 
00050 //******** Single include for the TkMap *************
00051 #include "DQM/SiStripCommon/interface/TkHistoMap.h" 
00052 //***************************************************
00053 
00054 //******** includes for the cabling *************
00055 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00056 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00057 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
00058 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00059 //***************************************************
00060 
00061 
00062 //
00063 // class decleration
00064 //
00065 
00066 class APVShotsAnalyzer : public edm::EDAnalyzer {
00067 public:
00068   explicit APVShotsAnalyzer(const edm::ParameterSet&);
00069   ~APVShotsAnalyzer();
00070 
00071 
00072 private:
00073   virtual void beginJob() ;
00074   virtual void beginRun(const edm::Run&, const edm::EventSetup&) ;
00075   virtual void endRun(const edm::Run&, const edm::EventSetup&) ;
00076   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00077   virtual void endJob() ;
00078 
00079   void updateDetCabling( const edm::EventSetup& setup );
00080 
00081       // ----------member data ---------------------------
00082 
00083   edm::InputTag _digicollection;
00084   bool _zs;
00085   std::string _suffix;
00086   int _nevents;
00087 
00088   TH1F* _nShots;
00089   TProfile* _nShotsVsTime;
00090   TH1F* _whichAPV;
00091   TH1F* _stripMult;
00092   TH1F* _median;
00093   TH1F* _subDetector;
00094   TH1F* _fed;
00095 
00096   TH2F* _medianVsFED;
00097   TH2F* _nShotsVsFED;
00098 
00099 
00100   TkHistoMap *tkhisto,*tkhisto2; 
00101 
00102   // DetCabling
00103   bool _useCabling;
00104   uint32_t _cacheIdDet;  
00105   const SiStripDetCabling* _detCabling;  
00106   
00107 };
00108 
00109 //
00110 // constants, enums and typedefs
00111 //
00112 
00113 //
00114 // static data member definitions
00115 //
00116 
00117 //
00118 // constructors and destructor
00119 //
00120 APVShotsAnalyzer::APVShotsAnalyzer(const edm::ParameterSet& iConfig):
00121   _digicollection(iConfig.getParameter<edm::InputTag>("digiCollection")),
00122   _zs(iConfig.getUntrackedParameter<bool>("zeroSuppressed",true)),
00123   _suffix(iConfig.getParameter<std::string>("mapSuffix")),
00124   _nevents(0),
00125   _useCabling(iConfig.getUntrackedParameter<bool>("useCabling",true)),
00126   _cacheIdDet(0),
00127   _detCabling(0)
00128 {
00129    //now do what ever initialization is needed
00130 
00131   if(!_zs) _suffix += "_notZS";
00132 
00133  edm::Service<TFileService> tfserv;
00134 
00135  _nShots = tfserv->make<TH1F>("nShots","Number of Shots per event",200,-0.5,199.5);
00136  _nShots->GetXaxis()->SetTitle("Shots");  _nShots->GetYaxis()->SetTitle("Events"); 
00137  _nShots->StatOverflows(kTRUE);
00138 
00139  _nShotsVsTime = tfserv->make<TProfile>("nShotsVsTime","Mean number of shots vs orbit number",3600,0.5,3600*11223+0.5);
00140  _nShotsVsTime->GetXaxis()->SetTitle("Orbit");  _nShotsVsTime->GetYaxis()->SetTitle("Number of Shots");
00141  _nShotsVsTime->SetBit(TH1::kCanRebin);
00142 
00143  _whichAPV = tfserv->make<TH1F>("whichAPV","APV with shots",6,-0.5,5.5);
00144  _whichAPV->GetXaxis()->SetTitle("APV");  _whichAPV->GetYaxis()->SetTitle("Shots"); 
00145 
00146  _stripMult = tfserv->make<TH1F>("stripMultiplicity","Shot Strip Multiplicity",129,-0.5,128.5);
00147  _stripMult->GetXaxis()->SetTitle("Number of Strips");  _stripMult->GetYaxis()->SetTitle("Shots");
00148 
00149  _median = tfserv->make<TH1F>("median","APV Shot charge median",256,-0.5,255.5);
00150  _median->GetXaxis()->SetTitle("Charge [ADC]");  _median->GetYaxis()->SetTitle("Shots");
00151 
00152  _subDetector = tfserv->make<TH1F>("subDets","SubDetector Shot distribution",10,-0.5,9.5);
00153  _subDetector->GetYaxis()->SetTitle("Shots");
00154 
00155  if (_useCabling) {
00156    _fed = tfserv->make<TH1F>("fed","FED Shot distribution",440,50,490);
00157    _fed->GetYaxis()->SetTitle("Shots");
00158 
00159    _nShotsVsFED = tfserv->make<TH2F>("nShotsVsFED","Number of Shots per event vs fedid",440,50,490,200,-0.5,199.5);
00160    _nShotsVsFED->GetXaxis()->SetTitle("fedId");  _nShots->GetYaxis()->SetTitle("Shots");  _nShots->GetZaxis()->SetTitle("Events");
00161    _nShotsVsFED->StatOverflows(kTRUE);
00162 
00163    _medianVsFED = tfserv->make<TH2F>("medianVsFED","APV Shot charge median vs fedid",440,50,490,256,-0.5,255.5);
00164    _medianVsFED->GetXaxis()->SetTitle("fedId");_medianVsFED->GetYaxis()->SetTitle("Charge [ADC]");  _median->GetZaxis()->SetTitle("Shots");
00165  }
00166 
00167  tkhisto      =new TkHistoMap("ShotMultiplicity","ShotMultiplicity",-1); 
00168  tkhisto2      =new TkHistoMap("StripMultiplicity","StripMultiplicity",-1); 
00169 }
00170 
00171 
00172 APVShotsAnalyzer::~APVShotsAnalyzer()
00173 {
00174  
00175    // do anything here that needs to be done at desctruction time
00176    // (e.g. close files, deallocate resources etc.)
00177   if ( _detCabling ) _detCabling = 0;
00178 
00179 }
00180 
00181 
00182 //
00183 // member functions
00184 //
00185 
00186 // ------------ method called to for each event  ------------
00187 void
00188 APVShotsAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00189 {
00190    using namespace edm;
00191 
00192    if (_useCabling){
00193      //retrieve cabling
00194      updateDetCabling( iSetup );
00195    }
00196 
00197    _nevents++;
00198 
00199    Handle<edm::DetSetVector<SiStripDigi> > digis;
00200    iEvent.getByLabel(_digicollection,digis);
00201 
00202    // loop on detector with digis
00203 
00204    int nshots=0;
00205    std::vector<int> nshotsperFed;
00206 
00207    const uint16_t lNumFeds = sistrip::FED_ID_MAX-sistrip::FED_ID_MIN+1;
00208    if (_useCabling){
00209      nshotsperFed.resize(lNumFeds,0);
00210    }
00211 
00212    APVShotFinder apvsf(*digis,_zs);
00213    const std::vector<APVShot>& shots = apvsf.getShots();
00214 
00215    for(std::vector<APVShot>::const_iterator shot=shots.begin();shot!=shots.end();++shot) {
00216      if(shot->isGenuine()) {
00217 
00218        //get the fedid from the detid
00219 
00220        uint32_t det=shot->detId();
00221        if (_useCabling){
00222          const std::vector<const FedChannelConnection *> & conns = _detCabling->getConnections( det );
00223 
00224          if (!(conns.size())) continue;
00225          uint16_t lFedId = 0;
00226          for (uint32_t ch = 0; ch<conns.size(); ch++) {
00227            lFedId = conns[ch]->fedId();
00228            //uint16_t lFedCh = conns[ch]->fedCh();
00229    
00230            if (lFedId < sistrip::FED_ID_MIN || lFedId > sistrip::FED_ID_MAX){
00231              std::cout << " -- Invalid fedid " << lFedId << " for detid " << det << " connection " << ch << std::endl;
00232              continue;
00233            }
00234            else break;
00235          }
00236 
00237          if (lFedId < sistrip::FED_ID_MIN || lFedId > sistrip::FED_ID_MAX){
00238            std::cout << " -- No valid fedid (=" << lFedId << ") found for detid " << det << std::endl;
00239            continue;
00240          }
00241          ++nshotsperFed[lFedId-sistrip::FED_ID_MIN];
00242          _fed->Fill(lFedId);
00243          _medianVsFED->Fill(lFedId,shot->median());
00244 
00245 
00246        }
00247 
00248        ++nshots;
00249 
00250 
00251        _whichAPV->Fill(shot->apvNumber());
00252        _median->Fill(shot->median());
00253        _stripMult->Fill(shot->nStrips());
00254        _subDetector->Fill(shot->subDet());
00255        tkhisto2->fill(det,shot->nStrips());;
00256        tkhisto->add(det,1);
00257 
00258 
00259 
00260      }
00261    }
00262 
00263    _nShots->Fill(nshots);
00264    if (_useCabling){
00265      for (uint16_t lFed(0); lFed<lNumFeds; lFed++){
00266        _nShotsVsFED->Fill(lFed+sistrip::FED_ID_MIN,nshotsperFed[lFed]);
00267      }
00268    }
00269 
00270    _nShotsVsTime->Fill(iEvent.orbitNumber(),nshots);
00271    
00272 
00273 }
00274 
00275 void 
00276 APVShotsAnalyzer::beginRun(const edm::Run& iRun, const edm::EventSetup&)
00277 {}
00278 
00279 void 
00280 APVShotsAnalyzer::endRun(const edm::Run& iRun, const edm::EventSetup&)
00281 {
00282 }
00283 
00284 
00285 // ------------ method called once each job just before starting event loop  ------------
00286 void 
00287 APVShotsAnalyzer::beginJob()
00288 {
00289 
00290 }
00291 
00292 // ------------ method called once each job just after ending the event loop  ------------
00293 void 
00294 APVShotsAnalyzer::endJob() {
00295 
00296   edm::LogInfo("EndOfJob") << _nevents << " analyzed events";
00297 
00298 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
00299   TrackerMap tkmap,tkmap2;
00300 
00301   tkmap.setPalette(1);
00302   tkmap2.setPalette(1);
00303   tkhisto->dumpInTkMap(&tkmap);
00304   tkhisto2->dumpInTkMap(&tkmap2);
00305   std::string tkshotmultmapname = "ShotMultiplicity_" + _suffix + ".png";
00306   tkmap.save(true,0,0,tkshotmultmapname);
00307   std::string tkstripmultmapname = "StripMultiplicity_" + _suffix + ".png";
00308   tkmap2.save(true,0,0,tkstripmultmapname);
00309 
00310   std::string rootmapname = "TKMap_"+_suffix+".root";
00311   tkhisto->save(rootmapname);
00312   tkhisto2->save(rootmapname);
00313 }
00314 
00315 
00316 void APVShotsAnalyzer::updateDetCabling( const edm::EventSetup& setup )
00317 {
00318   if (_useCabling){
00319     uint32_t cache_id = setup.get<SiStripDetCablingRcd>().cacheIdentifier();//.get( cabling_ );
00320    
00321     if ( _cacheIdDet != cache_id ) { // If the cache ID has changed since the last update...
00322       // Update the cabling object
00323       edm::ESHandle<SiStripDetCabling> c;
00324       setup.get<SiStripDetCablingRcd>().get( c );
00325       _detCabling = c.product();
00326       _cacheIdDet = cache_id;
00327     } // end of new cache ID check
00328   }
00329 }
00330 
00331 
00332 
00333 
00334 //define this as a plug-in
00335 DEFINE_FWK_MODULE(APVShotsAnalyzer);