CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQM/SiStripMonitorHardware/src/SiStripSpyIdentifyRuns.cc

Go to the documentation of this file.
00001 // Original Author:  Anne-Marie Magnan
00002 //         Created:  2010/02/25
00003 // $Id: SiStripSpyIdentifyRuns.cc,v 1.1 2012/10/15 09:02:47 threus Exp $
00004 //
00005 
00006 #include <sstream>
00007 #include <fstream>
00008 #include <iostream>
00009 #include <memory>
00010 #include <list>
00011 #include <algorithm>
00012 #include <cassert>
00013 
00014 #include "FWCore/Framework/interface/Frameworkfwd.h"
00015 #include "FWCore/Framework/interface/EDAnalyzer.h"
00016 #include "FWCore/Framework/interface/Event.h"
00017 #include "FWCore/Framework/interface/EventSetup.h"
00018 #include "FWCore/Framework/interface/ESHandle.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 #include "FWCore/Utilities/interface/InputTag.h"
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022 #include "FWCore/ServiceRegistry/interface/Service.h"
00023 #include "FWCore/Utilities/interface/Exception.h"
00024 
00025 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00026 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00027 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00028 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
00029 
00030 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDSpyBuffer.h"
00031 
00032 //
00033 // Class declaration
00034 //
00035 namespace sistrip {
00036 
00037   class SpyIdentifyRunsModule : public edm::EDAnalyzer
00038   {
00039   public:
00040 
00041     explicit SpyIdentifyRunsModule(const edm::ParameterSet&);
00042     ~SpyIdentifyRunsModule();
00043 
00044   private:
00045 
00046     virtual void beginJob();
00047     virtual void analyze(const edm::Event&, const edm::EventSetup&);
00048     virtual void endJob();
00049 
00050     void writeRunInFile(const unsigned int aRunNumber);
00051  
00052     //name of the output file containing the run numbers
00053     //of spy runs
00054     std::string fileName_;
00055     std::ofstream outFile_;
00056 
00057     //tag of spydata source collection
00058     edm::InputTag srcTag_;
00059 
00060     uint32_t prevRun_;
00061 
00062   };
00063 }//namespace
00064 
00065 using edm::LogError;
00066 using edm::LogWarning;
00067 using edm::LogInfo;
00068 //
00069 // Constructors and destructor
00070 //
00071 namespace sistrip {
00072 
00073   SpyIdentifyRunsModule::SpyIdentifyRunsModule(const edm::ParameterSet& iConfig)
00074     : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
00075       srcTag_(iConfig.getParameter<edm::InputTag>("InputProductLabel")),
00076       prevRun_(0)
00077   {
00078 
00079   }
00080 
00081 
00082   SpyIdentifyRunsModule::~SpyIdentifyRunsModule() {
00083 
00084   }
00085 
00086   void SpyIdentifyRunsModule::beginJob()
00087   {
00088     outFile_.open(fileName_.c_str(),std::ios::out);
00089     if (!outFile_.is_open()) {
00090       edm::LogError("SiStripSpyIdentifyRuns")  << " -- Cannot open file : " << fileName_ << " for writting." 
00091                                                << std::endl;
00092       edm::LogInfo("SiStripSpyIdentifyRuns")  << " *** SPY RUNS *** "<< std::endl;
00093 
00094     }
00095     else {
00096       outFile_ << " *** SPY RUNS *** " << std::endl;
00097     }
00098   }
00099 
00100   void SpyIdentifyRunsModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup)
00101   {
00102 
00103     //static bool lFirstEvent = true;
00104     //if (!lFirstEvent) return;
00105     uint32_t lRunNum = aEvt.id().run();
00106     if (lRunNum == prevRun_) return;
00107 
00108     edm::Handle<FEDRawDataCollection> lHandle;
00109     aEvt.getByLabel( srcTag_, lHandle ); 
00110     const FEDRawDataCollection& buffers = *lHandle;
00111 
00112     for (unsigned int iFed(FEDNumbering::MINSiStripFEDID);
00113          iFed <= FEDNumbering::MAXSiStripFEDID;
00114          iFed++)
00115       {
00116 
00117         //retrieve FED raw data for given FED 
00118         const FEDRawData& input = buffers.FEDData( static_cast<int>(iFed) );
00119         //check on FEDRawData pointer and size
00120         if ( !input.data() ||!input.size() ) continue;
00121           
00122         //construct FEDBuffer
00123         std::auto_ptr<sistrip::FEDSpyBuffer> buffer;
00124         try {
00125           buffer.reset(new sistrip::FEDSpyBuffer(input.data(),input.size()));
00126         } catch (const cms::Exception& e) { 
00127           edm::LogWarning("SiStripSpyIdentifyRuns")
00128             << "Exception caught when creating FEDSpyBuffer object for FED " << iFed << ": " << e.what();
00129           //if (!(buffer->readoutMode() == READOUT_MODE_SPY)) break;
00130           std::string lErrStr = e.what();
00131           if (lErrStr.find("Buffer is not from spy channel")!=lErrStr.npos) break;
00132           else {
00133             writeRunInFile(lRunNum);
00134             break;
00135           }
00136         } // end of buffer reset try.
00137         edm::LogWarning("SiStripSpyIdentifyRuns")
00138           << " -- this is a spy file, run " << lRunNum << std::endl;
00139         writeRunInFile(lRunNum);
00140         break;
00141       }
00142     //lFirstEvent = false;
00143     prevRun_ = lRunNum;
00144 
00145   }
00146 
00147   void SpyIdentifyRunsModule::writeRunInFile(const unsigned int aRunNumber){
00148     if (!outFile_.is_open()) {
00149       edm::LogInfo("SiStripSpyIdentifyRuns") << aRunNumber
00150                                              << std::endl;
00151     }
00152     else {
00153       outFile_ << aRunNumber  << std::endl;
00154     }
00155   }
00156 
00157   void SpyIdentifyRunsModule::endJob() {
00158 
00159     //save global run number in text file in local directory
00160     //output loginfo with number of errors
00161     //or throw exception ?
00162     if (outFile_.is_open()) outFile_.close();
00163 
00164   }
00165 
00166 
00167 }//namespace
00168 
00169 #include "FWCore/Framework/interface/MakerMacros.h"
00170 typedef sistrip::SpyIdentifyRunsModule SiStripSpyIdentifyRuns;
00171 DEFINE_FWK_MODULE(SiStripSpyIdentifyRuns);