00001
00025 #include <iostream>
00026 #include <sstream>
00027
00028 #include <TSQLRow.h>
00029 #include <TMySQLServer.h>
00030 #include <TMySQLResult.h>
00031
00032 #include "TGrCalibration.h"
00033
00034 using std::cerr;
00035 using std::cout;
00036 using std::endl;
00037 using std::stringstream;
00038
00039 TGrCalibration::TGrCalibration(UInt_t channels)
00040 : fDatabase("mysql://localhost:3306/Online"),
00041 fAdc(channels), fLeTdc(channels), fCfTdc(channels)
00042 {
00049 }
00050
00051 TGrCalibration::~TGrCalibration()
00052 {
00055 }
00056
00057 void TGrCalibration::Clear(const Char_t *option)
00058 {
00062 fAdc.Clear();
00063 fLeTdc.Clear();
00064 fCfTdc.Clear();
00065 cout << "Clear: TGrCalibration cleared" << endl;
00066 }
00067
00068 void TGrCalibration::Print(const Char_t *option) const
00069 {
00075 cout << "TGrCalibration:" << endl;
00076 cout << "ADC calibration: " << endl;
00077 fAdc.Print();
00078 cout << "LETDC calibration: " << endl;
00079 fLeTdc.Print();
00080 cout << "CFTDC calibration: " << endl;
00081 fCfTdc.Print();
00082 }
00083
00084 void TGrCalibration::FindCalibration(const Char_t *detectorName)
00085 {
00091 TMySQLResult *result = GetDatabaseInfo(detectorName);
00092
00093 if (result) {
00094 UShort_t channels = fAdc.GetChannels();
00095 if (static_cast<UInt_t>(result->GetRowCount()) != channels)
00096 cerr << "FindCalibration: WARNING - rows in database ("
00097 << result->GetRowCount() << ") do not match number " << endl
00098 << "of expected channels (" << channels << ") for detector "
00099 << detectorName << " - using default calibration" << endl;
00100 else if (result->GetFieldCount() != 13)
00101 cerr << "FindCalibration: WARNING - columns in database ("
00102 << result->GetFieldCount() << ") do not match number " << endl
00103 << "of expected columns (13) for detector " << detectorName
00104 << " - using default calibration" << endl;
00105 else {
00106 for (Int_t i = 0; i < result->GetRowCount(); i++) {
00107 TSQLRow *row = result->Next();
00108 stringstream *fields = new stringstream[result->GetFieldCount()];
00109 for (Int_t i = 0; i < result->GetFieldCount(); i++)
00110 fields[i] << row->GetField(i);
00111 UShort_t detectorChannel;
00112 UShort_t adcMap;
00113 Short_t adcOffset;
00114 Float_t adcGain;
00115 UShort_t adcHistogramChannel;
00116 UShort_t leTdcMap;
00117 Short_t leTdcOffset;
00118 Float_t leTdcGain;
00119 UShort_t leTdcHistogramChannel;
00120 UShort_t cfTdcMap;
00121 Short_t cfTdcOffset;
00122 Float_t cfTdcGain;
00123 UShort_t cfTdcHistogramChannel;
00124 fields[0] >> detectorChannel;
00125 fields[1] >> adcMap;
00126 fields[2] >> adcOffset;
00127 fields[3] >> adcGain;
00128 fields[4] >> adcHistogramChannel;
00129 fields[5] >> leTdcMap;
00130 fields[6] >> leTdcOffset;
00131 fields[7] >> leTdcGain;
00132 fields[8] >> leTdcHistogramChannel;
00133 fields[9] >> cfTdcMap;
00134 fields[10] >> cfTdcOffset;
00135 fields[11] >> cfTdcGain;
00136 fields[12] >> cfTdcHistogramChannel;
00137 fAdc.AddToChannel(adcMap, detectorChannel);
00138 fAdc.AddToMap(detectorChannel, adcMap);
00139 fAdc.AddToOffset(detectorChannel, adcOffset);
00140 fAdc.AddToGain(detectorChannel, adcGain);
00141 fAdc.AddToHistogramMap(detectorChannel, adcHistogramChannel);
00142 fLeTdc.AddToChannel(leTdcMap, detectorChannel);
00143 fLeTdc.AddToMap(detectorChannel, leTdcMap);
00144 fLeTdc.AddToOffset(detectorChannel, leTdcOffset);
00145 fLeTdc.AddToGain(detectorChannel, leTdcGain);
00146 fLeTdc.AddToHistogramMap(detectorChannel, leTdcHistogramChannel);
00147 fCfTdc.AddToChannel(cfTdcMap, detectorChannel);
00148 fCfTdc.AddToMap(detectorChannel, cfTdcMap);
00149 fCfTdc.AddToOffset(detectorChannel, cfTdcOffset);
00150 fCfTdc.AddToGain(detectorChannel, cfTdcGain);
00151 fCfTdc.AddToHistogramMap(detectorChannel, cfTdcHistogramChannel);
00152 delete row;
00153 delete [] fields;
00154 }
00155 fAdc.SetBitMask();
00156 fLeTdc.SetBitMask();
00157 fCfTdc.SetBitMask();
00158 }
00159 delete result;
00160 } else
00161 cerr << "FindCalibration: ERROR - could not obtain database information"
00162 << endl;
00163 }
00164
00165 TMySQLResult * TGrCalibration::GetDatabaseInfo(const Char_t *detectorName)
00166 {
00176 TMySQLResult *result = 0;
00177 if (! detectorName) {
00178 cerr << "GetDatabaseInfo: ERROR - detector name must be set before "
00179 << "attempting to set the detector calibration" << endl;
00180 } else {
00181 string sql("SELECT * FROM ");
00182
00183 sql.append(detectorName);
00184 sql.append(" ORDER BY `adc map`;");
00185 TMySQLServer connect(fDatabase.c_str(), "dragon", "dragonTail");
00186 result = static_cast<TMySQLResult *>(connect.Query(sql.c_str()));
00187 }
00188 return result;
00189 }