00001
00002 #include "FWCore/Framework/interface/EventSetup.h"
00003 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00004
00005 #include <CalibCalorimetry/HcalStandardModules/interface/HcalPedestalAnalyzer.h>
00006
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 #include "FWCore/Framework/interface/EventSetup.h"
00012 #include "FWCore/Framework/interface/ESHandle.h"
00013 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00014 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00015 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00016 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00017 #include "Geometry/CaloTopology/interface/HcalTopology.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 namespace {
00030 bool defaultsFile (const std::string fParam) {
00031 return fParam == "defaults";
00032 }
00033
00034 bool asciiFile (const std::string fParam) {
00035 return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".txt";
00036 }
00037
00038 bool xmlFile (const std::string fParam) {
00039 return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".xml";
00040 }
00041
00042 bool dbFile (const std::string fParam) {
00043 return fParam.find (':') != std::string::npos;
00044 }
00045
00046 bool masterDb (const std::string fParam) {
00047 return fParam.find ('@') != std::string::npos;
00048 }
00049
00050 template <class T>
00051 bool getObject (T* fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00052 if (!fObject) return false;
00053 if (fDb.empty ()) return false;
00054 if (asciiFile (fDb)) {
00055 std::cout << "HcalPedestalAnalyzer-> USE INPUT: ASCII " << std::endl;
00056 std::ifstream stream (fDb.c_str ());
00057 HcalDbASCIIIO::getObject (stream, fObject);
00058 return true;
00059 }
00060 else if (dbFile (fDb)) {
00061 std::cout << "HcalPedestalAnalyzer-> USE INPUT: Pool " << fDb << std::endl;
00062 std::cout << "HcalPedestalAnalyzer-> Pool interface is not supportet since 1.3.0" << fDb << std::endl;
00063 return false;
00064
00065
00066 }
00067 else if (masterDb (fDb)) {
00068 std::cout << "HcalPedestalAnalyzer-> USE INPUT: MasterDB " << fDb << std::endl;
00069 HcalDbOnline masterDb (fDb);
00070 return masterDb.getObject (fObject, fTag, fRun);
00071 }
00072 else {
00073 std::cerr << "HcalPedestalAnalyzer-> Unknown input type " << fDb << std::endl;
00074 return false;
00075 }
00076 }
00077
00078 bool dumpXmlPedestals (const HcalPedestals& fObject, const HcalPedestalWidths& fWidth, const std::string& fXml, const std::string& fTag, int fRun) {
00079 std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: XML" << std::endl;
00080 std::ofstream stream (fXml.c_str ());
00081 bool result = HcalDbXml::dumpObject (stream, fRun, fRun, 0, fTag, fObject, fWidth);
00082 stream.close ();
00083 return result;
00084 }
00085
00086 template <class T>
00087 bool putObject (T** fObject, const std::string& fDb, const std::string& fTag, int fRun) {
00088 if (!fObject || !*fObject) return false;
00089 if (fDb.empty ()) return false;
00090 if (asciiFile (fDb)) {
00091 std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: ASCII " << std::endl;
00092 std::ofstream stream (fDb.c_str ());
00093 HcalDbASCIIIO::dumpObject (stream, **fObject);
00094 return true;
00095 }
00096 else if (dbFile (fDb)) {
00097 std::cout << "HcalPedestalAnalyzer-> USE OUTPUT: Pool " << fDb << std::endl;
00098 std::cout << "HcalPedestalAnalyzer-> Pool interface is not supportet since 1.3.0" << fDb << std::endl;
00099 return false;
00100
00101
00102
00103
00104 }
00105 else {
00106 std::cerr << "HcalPedestalAnalyzer-> Unknown output type " << fDb << std::endl;
00107 return false;
00108 }
00109 }
00110 }
00111
00112 HcalPedestalAnalyzer::HcalPedestalAnalyzer(const edm::ParameterSet& ps) :
00113 hbheDigiCollectionTag_(ps.getParameter<edm::InputTag>("hbheDigiCollectionTag")),
00114 hoDigiCollectionTag_(ps.getParameter<edm::InputTag>("hoDigiCollectionTag")),
00115 hfDigiCollectionTag_(ps.getParameter<edm::InputTag>("hfDigiCollectionTag")) {
00116
00117 m_pedAnal = new HcalPedestalAnalysis(ps);
00118 m_pedAnal->setup(ps.getUntrackedParameter<std::string>("outputFileHist", "HcalPedestalAnalyzer.root"));
00119
00120 m_startSample = ps.getUntrackedParameter<int>("firstSample", 0);
00121 m_endSample = ps.getUntrackedParameter<int>("lastSample", 19);
00122 m_inputPedestals_source = ps.getUntrackedParameter<std::string>("inputPedestalsSource", "");
00123 m_inputPedestals_tag = ps.getUntrackedParameter<std::string>("inputPedestalsTag", "");
00124 m_inputPedestals_run = ps.getUntrackedParameter<int>("inputPedestalsRun", 1);
00125 m_inputPedestalWidths_source = ps.getUntrackedParameter<std::string>("inputPedestalWidthsSource", "");
00126 m_inputPedestalWidths_tag = ps.getUntrackedParameter<std::string>("inputPedestalWidthsTag", "");
00127 m_inputPedestalWidths_run = ps.getUntrackedParameter<int>("inputPedestalWidthsRun", 1);
00128 m_outputPedestals_dest = ps.getUntrackedParameter<std::string>("outputPedestalsDest", "");
00129 m_outputPedestals_tag = ps.getUntrackedParameter<std::string>("outputPedestalsTag", "");
00130 m_outputPedestals_run = ps.getUntrackedParameter<int>("outputPedestalsRun", 99999);
00131 m_outputPedestalWidths_dest = ps.getUntrackedParameter<std::string>("outputPedestalWidthsDest", "");
00132 m_outputPedestalWidths_tag = ps.getUntrackedParameter<std::string>("outputPedestalWidthsTag", "");
00133 m_outputPedestalWidths_run = ps.getUntrackedParameter<int>("outputPedestalWidthsRun", 99999);
00134
00135
00136 const char* foo1 = "CORAL_AUTH_USER=blaaah";
00137 const char* foo2 = "CORAL_AUTH_PASSWORD=blaaah";
00138 if (!::getenv("CORAL_AUTH_USER")) ::putenv(const_cast<char*>(foo1));
00139 if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv(const_cast<char*>(foo2));
00140 }
00141
00142 HcalPedestalAnalyzer::~HcalPedestalAnalyzer(){
00143
00144 }
00145
00146 void HcalPedestalAnalyzer::beginJob(){
00147 m_ievt = 0;
00148 ped_sample = 1;
00149 }
00150
00151 void HcalPedestalAnalyzer::endJob(void) {
00152
00153 HcalPedestals* inputPeds = 0;
00154 if (!m_inputPedestals_source.empty ()) {
00155 inputPeds = new HcalPedestals (m_topo);
00156 if (!getObject (inputPeds, m_inputPedestals_source, m_inputPedestals_tag, m_inputPedestals_run)) {
00157 std::cerr << "HcalPedestalAnalyzer-> Failed to get input Pedestals" << std::endl;
00158 }
00159 }
00160 HcalPedestalWidths* inputPedWids = 0;
00161 if (!m_inputPedestalWidths_source.empty ()) {
00162 inputPedWids = new HcalPedestalWidths (m_topo);
00163 if (!getObject (inputPedWids, m_inputPedestalWidths_source, m_inputPedestalWidths_tag, m_inputPedestalWidths_run)) {
00164 std::cerr << "HcalPedestalAnalyzer-> Failed to get input PedestalWidths" << std::endl;
00165 }
00166 }
00167
00168
00169 HcalPedestals* outputPeds = (m_outputPedestals_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestals (m_topo);
00170 HcalPedestalWidths* outputPedWids = (m_outputPedestalWidths_dest.empty () && !xmlFile (m_outputPedestals_dest)) ? 0 : new HcalPedestalWidths (m_topo);
00171
00172
00173 int Flag=m_pedAnal->done(inputPeds, inputPedWids, outputPeds, outputPedWids);
00174
00175 delete inputPeds;
00176 delete inputPedWids;
00177
00178
00179
00180
00181 if (Flag>-2) {
00182 if (xmlFile (m_outputPedestals_dest)) {
00183 if (!dumpXmlPedestals (*outputPeds, *outputPedWids, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00184 std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals & Widths" << std::endl;
00185 }
00186 }
00187 else {
00188 if (outputPeds) {
00189 if (!putObject (&outputPeds, m_outputPedestals_dest, m_outputPedestals_tag, m_outputPedestals_run)) {
00190 std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
00191 }
00192 }
00193 if (outputPedWids) {
00194 if (!putObject (&outputPedWids, m_outputPedestalWidths_dest, m_outputPedestalWidths_tag, m_outputPedestalWidths_run)) {
00195 std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
00196 }
00197 }
00198 }
00199 }
00200 delete outputPeds;
00201 delete outputPedWids;
00202 }
00203
00204 void HcalPedestalAnalyzer::analyze(const edm::Event& e, const edm::EventSetup& eventSetup){
00205
00206 m_ievt++;
00207
00209 edm::Handle<HBHEDigiCollection> hbhe; e.getByLabel(hbheDigiCollectionTag_, hbhe);
00210 edm::Handle<HODigiCollection> ho; e.getByLabel(hoDigiCollectionTag_, ho);
00211 edm::Handle<HFDigiCollection> hf; e.getByLabel(hfDigiCollectionTag_, hf);
00212
00213
00214 edm::ESHandle<HcalDbService> conditions;
00215 eventSetup.get<HcalDbRecord>().get(conditions);
00216
00217 if (m_topo==0) {
00218 const IdealGeometryRecord& record = eventSetup.get<IdealGeometryRecord>();
00219 edm::ESHandle<HcalTopology> topology;
00220 record.get (topology);
00221 m_topo=new HcalTopology(*topology);
00222 }
00223
00224
00225 m_pedAnal->processEvent(*hbhe, *ho, *hf, *conditions);
00226
00227 if(m_ievt%1000 == 0)
00228 std::cout << "HcalPedestalAnalyzer: analyzed " << m_ievt << " events" << std::endl;
00229
00230 return;
00231 }
00232
00233
00234
00235
00236
00237