00001
00011 #include <iostream>
00012 #include <iomanip>
00013
00014 #include "TCalibration.h"
00015
00016 using std::cout;
00017 using std::cerr;
00018 using std::endl;
00019 using std::setw;
00020 using std::setfill;
00021 using std::hex;
00022 using std::dec;
00023
00024 TCalibration::TCalibration(UShort_t channels)
00025 : fChannels(channels), fCalSet(false),
00026 fBits(sizeof(UInt_t) * 8), fMaskMax(0), fBitMask(0)
00027 {
00036 SetChannels(channels);
00037 }
00038
00039 TCalibration::TCalibration(const TCalibration &rhs)
00040 {
00045 fChannels = rhs.fChannels;
00046 fCalSet = rhs.fCalSet;
00047 fChannel = rhs.fChannel;
00048 fMap = rhs.fMap;
00049 fOffset = rhs.fOffset;
00050 fGain = rhs.fGain;
00051 fMapH = rhs.fMapH;
00052 fBits = rhs.fBits;
00053 fMaskMax = rhs.fMaskMax;
00054 fBitMask = new UInt_t[rhs.fMaskMax];
00055 for (UInt_t i = 0; i < rhs.fMaskMax; i++)
00056 fBitMask[i] = rhs.fBitMask[i];
00057 }
00058
00059 TCalibration::~TCalibration()
00060 {
00065 if (fBitMask) delete [] fBitMask;
00066 }
00067
00068 TCalibration & TCalibration::operator=(const TCalibration &rhs)
00069 {
00075 if (&rhs != this) {
00076 Clear();
00077 fChannels = rhs.fChannels;
00078 fCalSet = rhs.fCalSet;
00079 fChannel = rhs.fChannel;
00080 fMap = rhs.fMap;
00081 fOffset = rhs.fOffset;
00082 fGain = rhs.fGain;
00083 fMapH = rhs.fMapH;
00084 fBits = rhs.fBits;
00085 fMaskMax = rhs.fMaskMax;
00086 fBitMask = new UInt_t[rhs.fMaskMax];
00087 for (UInt_t i = 0; i < rhs.fMaskMax; i++)
00088 fBitMask[i] = rhs.fBitMask[i];
00089 }
00090 return *this;
00091 }
00092
00093 void TCalibration::Clear(const Char_t *option)
00094 {
00104 fCalSet = false;
00105 if (! fMap.empty()) fMap.clear();
00106 if (! fGain.empty()) fGain.clear();
00107 if (! fOffset.empty()) fOffset.clear();
00108 if (! fMapH.empty()) fMapH.clear();
00109 if (fBitMask)
00110 delete [] fBitMask;
00111 fBitMask = 0;
00112 }
00113
00114 void TCalibration::Print(const Char_t *option) const
00115 {
00122
00123
00124
00125
00126
00127
00128 if (fCalSet) {
00129 cout << "Channel: Map: Gain: Offset: Histogram Map:" << endl;
00130 for (UShort_t i = 0; i < fChannels; i++)
00131 cout << setw(8) << dec << i << " " << setw(6) << fMap[i] << " "
00132 << setw(9) << fGain[i] << " " << setw(9) << fOffset[i] << " "
00133 << setw(15) << fMapH[i] << endl;
00134 if (fBitMask) {
00135 cout << "Bit-mask = 0x";
00136 for (UInt_t i = 0; i < fMaskMax; i++)
00137 cout << hex << setw(4) << setfill('0') << fBitMask[i];
00138 cout << setfill(' ') << endl << " = ";
00139 for (UInt_t i = 0; i < fMaskMax; i++) {
00140 for (UInt_t j = 0; j < fBits; j++) {
00141 Bool_t bit = (fBitMask[i] >> j & 1 ? 1 : 0);
00142 cout << dec << bit;
00143 }
00144 cout << " ";
00145 }
00146 }
00147 cout << endl;
00148 } else
00149 cerr << "Print: ERROR - no calibration set, using default" << endl;
00150 }
00151
00152 void TCalibration::SetChannels(UInt_t channels)
00153 {
00158 if (fCalSet == true) {
00159 cerr << "SetChannels: ERROR - calibration already set," << endl
00160 << "call Clear() first to enable resize" << endl;
00161 } else {
00162 fChannel.resize(channels);
00163 fMap.resize(channels);
00164 fGain.resize(channels, 1);
00165 fOffset.resize(channels, 0);
00166 fMapH.resize(channels);
00167 for (UInt_t i = 0; i < channels; i++) {
00168 fChannel[i] = i;
00169 fMap[i] = i;
00170 fMapH[i] = i;
00171 }
00172 fChannels = channels;
00173 SetBitMask();
00174 }
00175 }
00176
00177 void TCalibration::SetBitMask()
00178 {
00187 UInt_t max = 0;
00188
00189 if (fChannels > 0)
00190 max = *(max_element(fMap.begin(), fMap.end()));
00191 fMaskMax = max / fBits + 1;
00192 if (fBitMask)
00193 delete [] fBitMask;
00194 fBitMask = new UInt_t[fMaskMax];
00195 for (UInt_t i = 0; i < fMaskMax; i++)
00196 fBitMask[i] = 0;
00197 for (UInt_t i = 0; i < fMap.size(); i++)
00198 fBitMask[fMap[i] / fBits] |= (1 << fMap[i] % fBits);
00199 fCalSet = true;
00200 }
00201
00202 void TCalibration::AddToChannel(UInt_t map, UShort_t channel)
00203 {
00211 if (map >= fChannel.size())
00212 fChannel.resize(map + 1);
00213 fChannel[map] = channel;
00214 }
00215
00216 void TCalibration::AddToMap(UInt_t channel, UShort_t map)
00217 {
00224 fMap[channel] = map;
00225 }
00226
00227 void TCalibration::AddToOffset(UInt_t channel, Short_t offset)
00228 {
00234 fOffset[channel] = offset;
00235 }
00236
00237 void TCalibration::AddToGain(UInt_t channel, Float_t gain)
00238 {
00244 fGain[channel] = gain;
00245 }
00246
00247 void TCalibration::AddToHistogramMap(UInt_t channel, UShort_t map)
00248 {
00254 fMapH[channel] = map;
00255 }
00256
00257 void
00258 TCalibration::Calibrate(const vector<const TDataItem<UShort_t> *> &rawSignals,
00259 vector<const TDataItem<Float_t> *> &calSignals) const
00260 {
00268 for (UInt_t i = 0; i < rawSignals.size(); i++) {
00269 Short_t c = fChannel[rawSignals[i]->GetChannel()];
00270 Float_t v = fGain[c] * (rawSignals[i]->GetValue() - fOffset[c]);
00271 calSignals.push_back(new TDataItem<Float_t>(c, v));
00272 }
00273 }