00001
00021 #include <iostream>
00022 #include <sstream>
00023
00024 #include <TSQLRow.h>
00025 #include <TMySQLServer.h>
00026 #include <TMySQLResult.h>
00027
00028 #include "THiCalibration.h"
00029
00030 using std::cout;
00031 using std::cerr;
00032 using std::endl;
00033 using std::stringstream;
00034
00035 THiCalibration::THiCalibration(UInt_t channels)
00036 : fDatabase("mysql://localhost:3306/Online"),
00037 fAdc(channels), fTdc(channels)
00038 {
00045 }
00046
00047 THiCalibration::~THiCalibration()
00048 {
00051 }
00052
00053 void THiCalibration::Clear(const Char_t *option)
00054 {
00058 fAdc.Clear();
00059 fTdc.Clear();
00060 cout << "Clear: THiCalibration cleared" << endl;
00061 }
00062
00063 void THiCalibration::Print(const Char_t *option) const
00064 {
00068 cout << "THiCalibration:" << endl;
00069 cout << "ADC calibration: " << endl;
00070 fAdc.Print();
00071 cout << "TDC calibration: " << endl;
00072 fTdc.Print();
00073 }
00074
00075 void THiCalibration::FindCalibration(const Char_t *detectorName)
00076 {
00082 TMySQLResult *result = GetDatabaseInfo(detectorName);
00083
00084 if (result) {
00085 UShort_t channels = fAdc.GetChannels();
00086 if (static_cast<UInt_t>(result->GetRowCount()) != channels)
00087 cerr << "FindCalibration: WARNING - rows in database ("
00088 << result->GetRowCount() << ") do not match number " << endl
00089 << "of expected channels (" << channels << ") for detector "
00090 << detectorName << " - using default calibration" << endl;
00091 else if (result->GetFieldCount() != 9)
00092 cerr << "FindCalibration: WARNING - columns in database ("
00093 << result->GetFieldCount() << ") do not match number " << endl
00094 << "of expected columns (9) for detector " << detectorName
00095 << " - using default calibration" << endl;
00096 else {
00097 for (Int_t i = 0; i < result->GetRowCount(); i++) {
00098 stringstream *fields = new stringstream[result->GetFieldCount()];
00099 TSQLRow *row = result->Next();
00100 for (Int_t i = 0; i < result->GetFieldCount(); i++)
00101 fields[i] << row->GetField(i);
00102 UShort_t detectorChannel;
00103 UShort_t adcMap;
00104 Short_t adcOffset;
00105 Float_t adcGain;
00106 UShort_t adcHistogramChannel;
00107 UShort_t tdcMap;
00108 Short_t tdcOffset;
00109 Float_t tdcGain;
00110 UShort_t tdcHistogramChannel;
00111 fields[0] >> detectorChannel;
00112 fields[1] >> adcMap;
00113 fields[2] >> adcOffset;
00114 fields[3] >> adcGain;
00115 fields[4] >> adcHistogramChannel;
00116 fields[5] >> tdcMap;
00117 fields[6] >> tdcOffset;
00118 fields[7] >> tdcGain;
00119 fields[8] >> tdcHistogramChannel;
00120 fAdc.AddToChannel(adcMap, detectorChannel);
00121 fAdc.AddToMap(detectorChannel, adcMap);
00122 fAdc.AddToOffset(detectorChannel, adcOffset);
00123 fAdc.AddToGain(detectorChannel, adcGain);
00124 fAdc.AddToHistogramMap(detectorChannel, adcHistogramChannel);
00125 fTdc.AddToChannel(tdcMap, detectorChannel);
00126 fTdc.AddToMap(detectorChannel, tdcMap);
00127 fTdc.AddToOffset(detectorChannel, tdcOffset);
00128 fTdc.AddToGain(detectorChannel, tdcGain);
00129 fTdc.AddToHistogramMap(detectorChannel, tdcHistogramChannel);
00130 delete row;
00131 delete [] fields;
00132 }
00133 fAdc.SetBitMask();
00134 fTdc.SetBitMask();
00135 }
00136 delete result;
00137 } else
00138 cerr << "FindCalibration: ERROR - could not obtain database information"
00139 << endl;
00140 }
00141
00142 TMySQLResult * THiCalibration::GetDatabaseInfo(const Char_t *detectorName)
00143 {
00153 TMySQLResult *result = 0;
00154 if (! detectorName) {
00155 cerr << "GetDatabaseInfo: ERROR - detector name must be set before "
00156 << "attempting to set the detector calibration" << endl;
00157 } else {
00158 string sql("SELECT * FROM ");
00159
00160 sql.append(detectorName);
00161 sql.append(" ORDER BY `adc map`;");
00162 TMySQLServer connect(fDatabase.c_str(), "dragon", "dragonTail");
00163 result = static_cast<TMySQLResult *>(connect.Query(sql.c_str()));
00164 }
00165 return result;
00166 }