00001 #include "ODBCheck.hh"
00002
00003 #include "TFile.h"
00004 #include "TH1.h"
00005 #include "TH2.h"
00006
00007 #include <string>
00008 #include <iostream>
00009
00010 ODBCheck::ODBCheck() : fRun(0), fODB(), fCorrections(), fDataDirs(),
00011 fEstimate(), fCorrectionsFile() {
00012 }
00013
00014 ODBCheck::~ODBCheck() {
00015 }
00016
00017 void ODBCheck::SetDirs(const std::string& raw, const std::string& odb, const std::string& hist, const std::string& corr) {
00018 fDataDirs.SetRawDir(raw);
00019 fDataDirs.SetODBDir(odb);
00020 fDataDirs.SetHistDir(hist);
00021 fDataDirs.SetCorrDir(corr);
00022 }
00023
00024 void ODBCheck::OutputCorrections() {
00025
00026 static const std::string header("[/Analyzer/Wiremap]");
00027 static const std::string det_key("DetectorName = STRING");
00028 static const std::string en_key("Enabled = BOOL");
00029 static const std::string pol_key("TriggerPolarity = INT");
00030 static const std::string ped_key("Pedestal = INT");
00031 static const std::string time_key("TimeShift = FLOAT");
00032 static const std::string freq_key("SamplingFrequency = FLOAT");
00033
00034 static const std::string key_tail_sm("[48] :");
00035 static const std::string key_tail_lg("[52] :");
00036 static std::string key_tail;
00037
00038
00039 if (fCorrectionsFile.is_open()) {
00040 std::cout << "ODBCheck WARNING: Corrections file already open. Closing old file..." << std::endl;
00041 fCorrectionsFile.close();
00042 }
00043 fCorrectionsFile.open(fDataDirs.GetCorrFileName(fRun).c_str());
00044 if (!fCorrectionsFile.is_open()) {
00045 std::cout <<
00046 "ODBCheck ERROR: Cannot open correction file (" <<
00047 fDataDirs.GetCorrFileName(fRun) << ")!" <<
00048 std::endl <<
00049 " Corrections file not written!" <<
00050 std::endl;
00051 return;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061 unsigned int ndets = fCorrections.GetNDets();
00062 if ((ndets == 48 && fCorrections.GetRun() >= 2173) ||
00063 (ndets == 52 && fCorrections.GetRun() < 2173)) {
00064 std::cout << "ODBCheck ERROR: Corrected wiremap has run (" << fCorrections.GetRun() <<
00065 ") and number of detectors (" << ndets << ") that disagree with how the run went!" <<
00066 std::endl << " Not outputting corrections." << std::endl;
00067 return;
00068 }
00069
00070
00071 if (ndets == 48) {
00072 key_tail = key_tail_sm;
00073 } else if (ndets == 52) {
00074 key_tail = key_tail_lg;
00075 } else {
00076 std::cout <<
00077 "ODBCheck ERROR: Corrected WireMap has detector count not 48 or 52! (" <<
00078 fCorrections.GetNDets() << ")" << std::endl <<
00079 " Corrected ODB not written." << std::endl;
00080 return;
00081 }
00082
00083
00084
00085
00086 fCorrectionsFile << header << std::endl;
00087 fCorrectionsFile << det_key << key_tail << std::endl;
00088 for (unsigned int idet = 0; idet < ndets; ++idet)
00089 fCorrectionsFile << "[80] " <<
00090 fCorrections.GetDets()[idet] <<
00091 std::endl;
00092 fCorrectionsFile << en_key << key_tail << std::endl;
00093 for (unsigned int idet = 0; idet < ndets; ++idet)
00094 fCorrectionsFile << "[" << idet << "] " <<
00095 (fCorrections.GetEnableds()[idet] ? 'y' : 'n') <<
00096 std::endl;
00097 fCorrectionsFile << pol_key << key_tail << std::endl;
00098 for (unsigned int idet = 0; idet < ndets; ++idet)
00099 fCorrectionsFile << "[" << idet << "] " <<
00100 fCorrections.GetPolarities()[idet] <<
00101 std::endl;
00102 fCorrectionsFile << ped_key << key_tail << std::endl;
00103 for (unsigned int idet = 0; idet < ndets; ++idet)
00104 fCorrectionsFile << "[" << idet << "] " <<
00105 fCorrections.GetPedestals()[idet] <<
00106 std::endl;
00107 fCorrectionsFile << time_key << key_tail << std::endl;
00108 for (unsigned int idet = 0; idet < ndets; ++idet)
00109 fCorrectionsFile << "[" << idet << "] " <<
00110 -fCorrections.GetOffsets()[idet] <<
00111 std::endl;
00112 fCorrectionsFile << freq_key << key_tail << std::endl;
00113 for (unsigned int idet = 0; idet < ndets; ++idet)
00114 fCorrectionsFile << "[" << idet << "] " <<
00115 fCorrections.GetFrequencies()[idet] <<
00116 std::endl;
00117 fCorrectionsFile << std::endl;
00118
00119 fCorrectionsFile.close();
00120 }
00121
00122 void ODBCheck::Check(int run) {
00123 TFile hist_file(fDataDirs.GetHistFileName(run).c_str(), "READ");
00124
00125 if (!hist_file.IsOpen()) {
00126 std::cout <<
00127 "ODBCheck ERROR: Cannot open histogram file! (" <<
00128 fDataDirs.GetHistFileName(run).c_str() << ")" <<
00129 std::endl;
00130 return;
00131 }
00132
00133 fRun = run;
00134 std::string fname(fDataDirs.GetODBFileName(fRun));
00135 WireMap run_odb(fRun, fname);
00136 fODB = WireMap::Default();
00137 fODB.LoadOver(run_odb);
00138 fCorrections.Clear();
00139 fCorrections.SetRun(fRun);
00140
00141
00142
00143
00144
00145 for (unsigned int i = 0; i < fODB.GetNDets(); ++i) {
00146
00147
00148 if (fODB.GetBanks()[i] == "ZZZZ" || fODB.GetDets()[i] == "blank") {
00149 fCorrections.Add(fODB, i);
00150 fCorrections.Disable();
00151 continue;
00152 }
00153
00154 TH2* shapes;
00155 TH1* timing;
00156 hist_file.GetObject(("DataQuality_LowLevel/hDQ_PulseShapes_" + fODB.GetDets()[i] + "_" + fODB.GetBanks()[i]).c_str(), shapes);
00157 hist_file.GetObject(("DataQuality_LowLevel/hDQ_muScTDiff_" + fODB.GetDets()[i] + "_" + fODB.GetBanks()[i]).c_str(), timing);
00158
00159
00160
00161
00162
00163 if (!shapes) {
00164 fCorrections.Add(fODB, i);
00165 std::cout <<
00166 "ODBCheck WARNING: Shapes histogram not found! Corrections not included for " <<
00167 fODB.GetDets()[i] << "_" << fODB.GetBanks()[i] << " and the channel has been marked as disabled..." <<
00168 std::endl;
00169 fCorrections.Disable();
00170 } else if (!timing) {
00171 fCorrections.Add(fODB, i);
00172 std::cout <<
00173 "ODBCheck WARNING: Timing histogram not found! Corrections not included for " <<
00174 fODB.GetDets()[i] << "_" << fODB.GetBanks()[i] << " and the channel has been marked as disabled..." <<
00175 std::endl;
00176 fCorrections.Disable();
00177 } else if (!shapes->GetEntries()) {
00178 fCorrections.Add(fODB, i);
00179 std::cout <<
00180 "ODBCheck WARNING: Shapes histogram empty! Corrections not included for " <<
00181 fODB.GetDets()[i] << "_" << fODB.GetBanks()[i] << " and the channel has been marked as disabled..." <<
00182 std::endl;
00183 fCorrections.Disable();
00184 } else if (!timing->GetEntries() && fODB.GetDets()[i] != std::string("muSc")) {
00185 fCorrections.Add(fODB, i);
00186 std::cout <<
00187 "ODBCheck WARNING: Timing histogram empty! Corrections not included for " <<
00188 fODB.GetDets()[i] << "_" << fODB.GetBanks()[i] << " and the channel has been marked as disabled..." <<
00189 std::endl;
00190 fCorrections.Disable();
00191 } else {
00192 fEstimate.Estimate(shapes, timing);
00193 fCorrections.Add(fODB.GetBanks()[i], fODB.GetDets()[i],
00194 true, fEstimate.GetPedestal(),
00195 fEstimate.GetPolarity(), fEstimate.GetOffset(),
00196 fODB.GetFrequencies()[i]);
00197 }
00198
00199 delete shapes;
00200 delete timing;
00201 shapes = NULL;
00202 timing = NULL;
00203 }
00204 hist_file.Close();
00205
00206 fCorrections.ClearDisabledDuplicateDetectors();
00207 fCorrections.UniqueFixes();
00208
00209 OutputCorrections();
00210 }