CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CalibCalorimetry/HcalStandardModules/src/HcalPedestalAnalyzer.cc

Go to the documentation of this file.
00001 
00002 #include "FWCore/Framework/interface/EventSetup.h"
00003 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00004 
00005 #include <CalibCalorimetry/HcalStandardModules/interface/HcalPedestalAnalyzer.h>
00006 //#include "CondTools/Hcal/interface/HcalDbTool.h"
00007 #include "CondTools/Hcal/interface/HcalDbOnline.h"
00008 #include "CondTools/Hcal/interface/HcalDbXml.h"
00009 #include "CondFormats/HcalObjects/interface/HcalPedestals.h"
00010 #include "CondFormats/HcalObjects/interface/HcalPedestalWidths.h"
00011 
00012 
00013 /*
00014  * \file HcalPedestalAnalyzer.cc
00015  * 
00016  * $Date: 2010/02/25 00:28:13 $
00017  * $Revision: 1.14 $
00018  * \author S Stoynev / W Fisher
00019  *
00020 */
00021 
00022 namespace {
00023   bool defaultsFile (const std::string fParam) {
00024     return fParam == "defaults";
00025   }
00026 
00027   bool asciiFile (const std::string fParam) {
00028     return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".txt";
00029   }
00030   
00031   bool xmlFile (const std::string fParam) {
00032     return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".xml";
00033   }
00034   
00035   bool dbFile (const std::string fParam) {
00036     return fParam.find (':') != std::string::npos;
00037   }
00038 
00039   bool masterDb (const std::string fParam) {
00040     return fParam.find ('@') != std::string::npos;
00041   }
00042 
00043   template <class T> 
00044   bool getObject (T* fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00045     if (!fObject) return false;
00046     if (fDb.empty ()) return false; 
00047     if (asciiFile (fDb)) {
00048       std::cout << "HcalPedestalAnalyzer-> USE INPUT: ASCII " << std::endl;
00049       std::ifstream stream (fDb.c_str ());
00050       HcalDbASCIIIO::getObject (stream, fObject); 
00051       return true;
00052     }
00053     else if (dbFile (fDb)) {
00054       std::cout << "HcalPedestalAnalyzer-> USE INPUT: Pool " << fDb << std::endl;
00055       std::cout << "HcalPedestalAnalyzer-> Pool interface is not supportet since 1.3.0" << fDb << std::endl;
00056       return false;
00057 //       HcalDbTool poolDb (fDb);
00058 //       return poolDb.getObject (fObject, fTag, fRun);
00059     }
00060     else if (masterDb (fDb)) {
00061       std::cout << "HcalPedestalAnalyzer-> USE INPUT: MasterDB " << fDb << std::endl;
00062       HcalDbOnline masterDb (fDb);
00063       return masterDb.getObject (fObject, fTag, fRun);
00064     }
00065     else {
00066       std::cerr << "HcalPedestalAnalyzer-> Unknown input type " << fDb << std::endl;
00067       return false;
00068     }
00069   }
00070   
00071   bool dumpXmlPedestals (const HcalPedestals& fObject, const HcalPedestalWidths& fWidth, const std::string& fXml, const std::string& fTag, int fRun) {
00072     std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: XML" << std::endl;
00073     std::ofstream stream (fXml.c_str ());
00074     bool result = HcalDbXml::dumpObject (stream, fRun, fRun, 0, fTag, fObject, fWidth);
00075     stream.close ();
00076     return result;
00077   }
00078 
00079   template <class T>
00080   bool putObject (T** fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00081     if (!fObject || !*fObject) return false;
00082     if (fDb.empty ()) return false;
00083     if (asciiFile (fDb)) {
00084       std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: ASCII " << std::endl;
00085       std::ofstream stream (fDb.c_str ());
00086       HcalDbASCIIIO::dumpObject (stream, **fObject);
00087       return true;
00088     }
00089     else if (dbFile (fDb)) {
00090       std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: Pool " << fDb << std::endl;
00091       std::cout << "HcalPedestalAnalyzer-> Pool interface is not supportet since 1.3.0" << fDb << std::endl;
00092       return false;
00093 //       HcalDbTool poolDb (fDb);
00094 //       bool result = poolDb.putObject (*fObject, fTag, fRun);
00095 //       if (result) *fObject = 0; // owned by POOL
00096 //       return result;
00097     }
00098     else {
00099       std::cerr << "HcalPedestalAnalyzer-> Unknown output type " << fDb << std::endl;
00100       return false;
00101     }
00102   }
00103 }
00104 
00105 HcalPedestalAnalyzer::HcalPedestalAnalyzer(const edm::ParameterSet& ps){
00106 
00107   m_pedAnal = new HcalPedestalAnalysis(ps);
00108   m_pedAnal->setup(ps.getUntrackedParameter<std::string>("outputFileHist", "HcalPedestalAnalyzer.root"));
00109 
00110   m_startSample = ps.getUntrackedParameter<int>("firstSample", 0);
00111   m_endSample = ps.getUntrackedParameter<int>("lastSample", 19);
00112   m_inputPedestals_source = ps.getUntrackedParameter<std::string>("inputPedestalsSource", "");
00113   m_inputPedestals_tag = ps.getUntrackedParameter<std::string>("inputPedestalsTag", "");
00114   m_inputPedestals_run = ps.getUntrackedParameter<int>("inputPedestalsRun", 1);
00115   m_inputPedestalWidths_source = ps.getUntrackedParameter<std::string>("inputPedestalWidthsSource", "");
00116   m_inputPedestalWidths_tag = ps.getUntrackedParameter<std::string>("inputPedestalWidthsTag", "");
00117   m_inputPedestalWidths_run = ps.getUntrackedParameter<int>("inputPedestalWidthsRun", 1);
00118   m_outputPedestals_dest = ps.getUntrackedParameter<std::string>("outputPedestalsDest", "");
00119   m_outputPedestals_tag = ps.getUntrackedParameter<std::string>("outputPedestalsTag", "");
00120   m_outputPedestals_run = ps.getUntrackedParameter<int>("outputPedestalsRun", 99999);
00121   m_outputPedestalWidths_dest = ps.getUntrackedParameter<std::string>("outputPedestalWidthsDest", "");
00122   m_outputPedestalWidths_tag = ps.getUntrackedParameter<std::string>("outputPedestalWidthsTag", "");
00123   m_outputPedestalWidths_run = ps.getUntrackedParameter<int>("outputPedestalWidthsRun", 99999);
00124 
00125   // CORAL required variables to be set, even if not needed
00126   const char* foo1 = "CORAL_AUTH_USER=blaaah";
00127   const char* foo2 = "CORAL_AUTH_PASSWORD=blaaah";
00128   if (!::getenv("CORAL_AUTH_USER")) ::putenv(const_cast<char*>(foo1));
00129   if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv(const_cast<char*>(foo2)); 
00130 }
00131 
00132 HcalPedestalAnalyzer::~HcalPedestalAnalyzer(){
00133 //  delete m_pedAnal;
00134 }
00135 
00136 void HcalPedestalAnalyzer::beginJob(){
00137   m_ievt = 0;
00138   ped_sample = 1;
00139 }
00140 
00141 void HcalPedestalAnalyzer::endJob(void) {
00142   // get input objects
00143   HcalPedestals* inputPeds = 0;
00144   if (!m_inputPedestals_source.empty ()) {
00145     inputPeds = new HcalPedestals ();
00146     if (!getObject (inputPeds, m_inputPedestals_source, m_inputPedestals_tag, m_inputPedestals_run)) {
00147       std::cerr << "HcalPedestalAnalyzer-> Failed to get input Pedestals" << std::endl;
00148     }
00149   }
00150   HcalPedestalWidths* inputPedWids = 0;
00151   if (!m_inputPedestalWidths_source.empty ()) {
00152     inputPedWids = new HcalPedestalWidths ();
00153     if (!getObject (inputPedWids, m_inputPedestalWidths_source, m_inputPedestalWidths_tag, m_inputPedestalWidths_run)) {
00154       std::cerr << "HcalPedestalAnalyzer-> Failed to get input PedestalWidths" << std::endl;
00155     }
00156   }
00157 
00158   // make output objects
00159   HcalPedestals* outputPeds = (m_outputPedestals_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestals ();
00160   HcalPedestalWidths* outputPedWids = (m_outputPedestalWidths_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestalWidths ();
00161 
00162   // run algorithm
00163   int Flag=m_pedAnal->done(inputPeds, inputPedWids, outputPeds, outputPedWids);
00164 
00165   delete inputPeds;
00166   delete inputPedWids;
00167 
00168 
00169   // store new objects
00170   // Flag=-2 indicates there were less than 100 events and output is meaningless
00171   if (Flag>-2) {
00172     if (xmlFile (m_outputPedestals_dest)) { // output pedestals and widths together
00173       if (!dumpXmlPedestals (*outputPeds, *outputPedWids, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00174         std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals & Widths" << std::endl;
00175       }
00176     }
00177     else {
00178       if (outputPeds) {
00179         if (!putObject (&outputPeds, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00180           std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
00181         }
00182       }
00183       if (outputPedWids) {
00184         if (!putObject (&outputPedWids, m_outputPedestalWidths_dest, m_outputPedestalWidths_tag, m_outputPedestalWidths_run)) {
00185           std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
00186         }
00187       }
00188     }
00189   }
00190   delete outputPeds;
00191   delete outputPedWids;
00192 }
00193 
00194 void HcalPedestalAnalyzer::analyze(const edm::Event& e, const edm::EventSetup& eventSetup){
00195 
00196   m_ievt++;
00197 
00199   edm::Handle<HBHEDigiCollection> hbhe; e.getByType(hbhe);
00200   edm::Handle<HODigiCollection> ho;     e.getByType(ho);
00201   edm::Handle<HFDigiCollection> hf;     e.getByType(hf);
00202 
00203   // get conditions
00204   edm::ESHandle<HcalDbService> conditions;
00205   eventSetup.get<HcalDbRecord>().get(conditions);
00206 
00207   m_pedAnal->processEvent(*hbhe, *ho, *hf, *conditions);
00208 
00209   if(m_ievt%1000 == 0)
00210     std::cout << "HcalPedestalAnalyzer: analyzed " << m_ievt << " events" << std::endl;
00211 
00212   return;
00213 }
00214 
00215 // #include "FWCore/PluginManager/interface/ModuleDef.h"
00216 // #include "FWCore/Framework/interface/MakerMacros.h"
00217 
00218 // 
00219 // DEFINE_FWK_MODULE(HcalPedestalAnalyzer);