CMS 3D CMS Logo

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: 2008/03/04 01:12:53 $
00017  * $Revision: 1.11 $
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   if (!::getenv("CORAL_AUTH_USER")) ::putenv("CORAL_AUTH_USER=blah");
00127   if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv("CORAL_AUTH_PASSWORD=blah"); 
00128 }
00129 
00130 HcalPedestalAnalyzer::~HcalPedestalAnalyzer(){
00131 //  delete m_pedAnal;
00132 }
00133 
00134 void HcalPedestalAnalyzer::beginJob(const edm::EventSetup& c){
00135   m_ievt = 0;
00136   ped_sample = 1;
00137 }
00138 
00139 void HcalPedestalAnalyzer::endJob(void) {
00140   // get input objects
00141   HcalPedestals* inputPeds = 0;
00142   if (!m_inputPedestals_source.empty ()) {
00143     inputPeds = new HcalPedestals ();
00144     if (!getObject (inputPeds, m_inputPedestals_source, m_inputPedestals_tag, m_inputPedestals_run)) {
00145       std::cerr << "HcalPedestalAnalyzer-> Failed to get input Pedestals" << std::endl;
00146     }
00147   }
00148   HcalPedestalWidths* inputPedWids = 0;
00149   if (!m_inputPedestalWidths_source.empty ()) {
00150     inputPedWids = new HcalPedestalWidths ();
00151     if (!getObject (inputPedWids, m_inputPedestalWidths_source, m_inputPedestalWidths_tag, m_inputPedestalWidths_run)) {
00152       std::cerr << "HcalPedestalAnalyzer-> Failed to get input PedestalWidths" << std::endl;
00153     }
00154   }
00155 
00156   // make output objects
00157   HcalPedestals* outputPeds = (m_outputPedestals_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestals ();
00158   HcalPedestalWidths* outputPedWids = (m_outputPedestalWidths_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestalWidths ();
00159 
00160   // run algorithm
00161   int Flag=m_pedAnal->done(inputPeds, inputPedWids, outputPeds, outputPedWids);
00162 
00163   delete inputPeds;
00164   delete inputPedWids;
00165 
00166 
00167   // store new objects
00168   // Flag=-2 indicates there were less than 100 events and output is meaningless
00169   if (Flag>-2) {
00170     if (xmlFile (m_outputPedestals_dest)) { // output pedestals and widths together
00171       if (!dumpXmlPedestals (*outputPeds, *outputPedWids, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00172         std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals & Widths" << std::endl;
00173       }
00174     }
00175     else {
00176       if (outputPeds) {
00177         if (!putObject (&outputPeds, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00178           std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
00179         }
00180       }
00181       if (outputPedWids) {
00182         if (!putObject (&outputPedWids, m_outputPedestalWidths_dest, m_outputPedestalWidths_tag, m_outputPedestalWidths_run)) {
00183           std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
00184         }
00185       }
00186     }
00187   }
00188   delete outputPeds;
00189   delete outputPedWids;
00190 }
00191 
00192 void HcalPedestalAnalyzer::analyze(const edm::Event& e, const edm::EventSetup& eventSetup){
00193 
00194   m_ievt++;
00195 
00197   edm::Handle<HBHEDigiCollection> hbhe; e.getByType(hbhe);
00198   edm::Handle<HODigiCollection> ho;     e.getByType(ho);
00199   edm::Handle<HFDigiCollection> hf;     e.getByType(hf);
00200 
00201   // get conditions
00202   edm::ESHandle<HcalDbService> conditions;
00203   eventSetup.get<HcalDbRecord>().get(conditions);
00204 
00205   m_pedAnal->processEvent(*hbhe, *ho, *hf, *conditions);
00206 
00207   if(m_ievt%1000 == 0)
00208     std::cout << "HcalPedestalAnalyzer: analyzed " << m_ievt << " events" << std::endl;
00209 
00210   return;
00211 }
00212 
00213 // #include "FWCore/PluginManager/interface/ModuleDef.h"
00214 // #include "FWCore/Framework/interface/MakerMacros.h"
00215 
00216 // DEFINE_SEAL_MODULE();
00217 // DEFINE_ANOTHER_FWK_MODULE(HcalPedestalAnalyzer);

Generated on Tue Jun 9 17:25:20 2009 for CMSSW by  doxygen 1.5.4