00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include "RawData/RawQieCalibrationDataBlock.h"
00013 #include "TMath.h"
00014
00015 UInt_t RawQieCalibrationDataBlock::fgDebugFlags = 0;
00016
00017 #include "MessageService/MsgService.h"
00018 CVSID("$Id: RawQieCalibrationDataBlock.cxx,v 1.9 2004/01/29 18:14:19 rhatcher Exp $");
00019 #include "MessageService/MsgFormat.h"
00020
00021 #include "RawData/RawBlockRegistry.h"
00022 REGISTERRAWBLOCK(RawQieCalibrationDataBlock,kMdBlockQieCalibrationData,0);
00023
00024 ClassImp(RawQieCalibrationDataBlock)
00025
00026 enum EQieCalibBlkPos {
00027 indx_size = 0,
00028 indx_checksum = 1,
00029 indx_blockid = 2,
00030 indx_run = 3,
00031 indx_subrun = 4,
00032 indx_runtype = indx_subrun,
00033 indx_ncalpts_v0 = 5,
00034 indx_daqvals_v0 = 6,
00035 indx_crateid = 5,
00036 indx_ncalpts = 6,
00037 indx_nloc = 7,
00038 indx_daqvals = 8,
00039 zzzz_last = indx_daqvals
00040 };
00041
00042 struct QieCalibPoint
00043 {
00044 long nEntries;
00045 long range;
00046 long unsigned int sum;
00047 long unsigned int sum_sqr;
00048 };
00049 const UInt_t qieCalibPointIntSize = sizeof(QieCalibPoint)/sizeof(Int_t);
00050
00051 const UInt_t illegalLocation = 0xFFFFFFFF;
00052
00053
00054 RawQieCalibrationDataBlock::RawQieCalibrationDataBlock()
00055 : RawDataBlock()
00056 {
00057
00058 }
00059
00060
00061 RawQieCalibrationDataBlock::RawQieCalibrationDataBlock(const Int_t *block)
00062 : RawDataBlock(block)
00063 {
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 }
00091
00092
00093 RawQieCalibrationDataBlock::~RawQieCalibrationDataBlock()
00094 {
00095
00096 }
00097
00098
00099 RawQieCalibrationDataBlock&
00100 RawQieCalibrationDataBlock::operator=(const RawQieCalibrationDataBlock& rhs)
00101 {
00102
00103 if (this != &rhs) {
00104 RawDataBlock::operator=(rhs);
00105 fCrateId = rhs.fCrateId;
00106
00107 fLocation.clear();
00108 fLocStart.clear();
00109 }
00110 return *this;
00111 }
00112
00113
00114 Int_t RawQieCalibrationDataBlock::GetRun() const
00115 {
00116
00117 if (fSize > indx_run) return fRawBlock[indx_run];
00118 return -1;
00119 }
00120
00121
00122 Short_t RawQieCalibrationDataBlock::GetSubRun() const
00123 {
00124
00125 if (fSize > indx_subrun) return (fRawBlock[indx_subrun]>>16)&0xffff;
00126 return -1;
00127 }
00128
00129
00130 Short_t RawQieCalibrationDataBlock::GetRunType() const
00131 {
00132
00133 if (fSize > indx_runtype) return fRawBlock[indx_runtype]&0xffff;
00134 return -1;
00135 }
00136
00137
00138 Int_t RawQieCalibrationDataBlock::GetCrateIdInfo() const
00139 {
00140
00141 if (GetMinorId() <= 0) return -1;
00142 if (fSize > indx_crateid) return fRawBlock[indx_crateid];
00143 return -1;
00144 }
00145
00146 UInt_t RawQieCalibrationDataBlock::GetNumCalPoints() const
00147 {
00148
00149 int indx_ncalpts_v = indx_ncalpts;
00150 if (GetMinorId() <= 0) indx_ncalpts_v = indx_ncalpts_v0;
00151 if (fSize > indx_ncalpts_v) return fRawBlock[indx_ncalpts_v];
00152 return 0;
00153 }
00154
00155
00156 const Int_t* RawQieCalibrationDataBlock::GetDacValues() const
00157 {
00158
00159 int indx_daqvals_v = indx_daqvals;
00160 if (GetMinorId() <= 0) indx_daqvals_v = indx_daqvals_v0;
00161 if (fSize > indx_daqvals_v) return (const Int_t*)(fRawBlock+indx_daqvals_v);
00162 return 0;
00163 }
00164
00165
00166 Int_t RawQieCalibrationDataBlock::GetDacValue(UInt_t indx) const
00167 {
00168
00169 UInt_t ncalpts = GetNumCalPoints();
00170 if (indx >= ncalpts) {
00171 MSG("RawData",Msg::kError)
00172 << "indx " << indx << " out of range: there are only "
00173 << ncalpts << " dac values " << endl;
00174 return 0;
00175 }
00176 int indx_daqvals_v = indx_daqvals;
00177 if (GetMinorId() <= 0) indx_daqvals_v = indx_daqvals_v0;
00178 if ((unsigned long)fSize > indx_daqvals_v+indx)
00179 return (Int_t)fRawBlock[indx_daqvals_v+indx];
00180 return 0;
00181 }
00182
00183
00184 UInt_t RawQieCalibrationDataBlock::GetNumLocations() const
00185 {
00186
00187
00188
00189 UInt_t minor = GetMinorId();
00190 UInt_t nloc_unpacked = FillLocStart();
00191 if (minor > 0) {
00192 UInt_t nloc_stored = fRawBlock[indx_nloc];
00193 if (nloc_stored != nloc_unpacked)
00194 MSG("RawData",Msg::kError)
00195 << " Unpacking found " << nloc_unpacked
00196 << " 'location', while data claims " << nloc_stored
00197 << " were stored" << endl;
00198 }
00199 return nloc_unpacked;
00200
00201 }
00202
00203
00204 UInt_t RawQieCalibrationDataBlock::GetLocation(UInt_t indx) const
00205 {
00206
00207
00208 UInt_t nloc = FillLocStart();
00209 if ( indx >= nloc) {
00210 MSG("RawData",Msg::kError)
00211 << "indx " << indx << " out of range: there are only "
00212 << nloc << " entries" << endl;
00213 return illegalLocation;
00214 }
00215 return fLocation[indx];
00216 }
00217
00218
00219 UInt_t RawQieCalibrationDataBlock::GetNumPoints(UInt_t loc) const
00220 {
00221
00222
00223
00224 FillLocStart();
00225 const Int_t* ptr = fLocStart[loc];
00226 return *(ptr+1);
00227 }
00228
00229
00230 UInt_t RawQieCalibrationDataBlock::GetNumEntries(UInt_t loc, UInt_t ipt) const
00231 {
00232
00233
00234
00235 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00236 if (!ptr) return 0;
00237 return ptr->nEntries;
00238 }
00239
00240
00241 Int_t RawQieCalibrationDataBlock::GetRange(UInt_t loc, UInt_t ipt) const
00242 {
00243
00244
00245
00246 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00247 if (!ptr) return 0;
00248 return ptr->range;
00249 }
00250
00251
00252 Double_t RawQieCalibrationDataBlock::GetMean(UInt_t loc, UInt_t ipt) const
00253 {
00254
00255
00256
00257 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00258 if (!ptr) return 0;
00259 if (!ptr->nEntries) return 0;
00260 return (Double_t)ptr->sum/(Double_t)ptr->nEntries;
00261 }
00262
00263
00264 Double_t RawQieCalibrationDataBlock::GetRms(UInt_t loc, UInt_t ipt) const
00265 {
00266
00267
00268
00269 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00270 if (!ptr) return 0;
00271 if (!ptr->nEntries) return 0;
00272 Double_t n = (Double_t)ptr->nEntries;
00273 Double_t sumx = (Double_t)ptr->sum;
00274 Double_t sumx2 = (Double_t)ptr->sum_sqr;
00275 Double_t mean = sumx/n;
00276 return TMath::Sqrt(sumx2/n - mean*mean);
00277
00278 }
00279
00280
00281 UInt_t RawQieCalibrationDataBlock::GetSum(UInt_t loc, UInt_t ipt) const
00282 {
00283
00284
00285
00286 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00287 if (!ptr) return 0;
00288 return ptr->sum;
00289
00290 }
00291
00292
00293 UInt_t RawQieCalibrationDataBlock::GetSumSqr(UInt_t loc, UInt_t ipt) const
00294 {
00295
00296
00297
00298 const QieCalibPoint* ptr = (const QieCalibPoint*)StartOfLocPoint(loc,ipt);
00299 if (!ptr) return 0;
00300 return ptr->sum_sqr;
00301
00302 }
00303
00304
00305 void RawQieCalibrationDataBlock::FillCrateId() const
00306 {
00307
00308 if (!fCrateId.IsNull()) return;
00309 if (GetMinorId() <= 0) {
00310 fCrateId.SetDetector(GetBlockId().GetDetector());
00311 MSG("RawData",Msg::kWarning)
00312 << " No crate info for minor version " << GetMinorId() << endl;
00313 return;
00314 }
00315 fCrateId = RawChannelId(GetBlockId().GetDetector(),GetCrateIdInfo(),0x1fff);
00316 }
00317
00318
00319 UInt_t RawQieCalibrationDataBlock::FillLocStart() const
00320 {
00321
00322
00323
00324
00325
00326 if (!fLocStart.empty()) return fLocStart.size();
00327
00328 UInt_t ncalpts = GetNumCalPoints();
00329 int indx_daqvals_v = indx_daqvals;
00330 if (GetMinorId() <= 0) indx_daqvals_v = indx_daqvals_v0;
00331 const Int_t *ptr =
00332 fRawBlock + indx_daqvals_v + ncalpts;
00333 const Int_t *end = fRawBlock + fSize;
00334
00335 while ( ptr < end ) {
00336 Int_t loc = *ptr;
00337 Int_t npts = *(ptr+1);
00338
00339 if (fLocStart[loc])
00340 MSG("RawData",Msg::kError)
00341 << " Already have an sub-block unit for 'location' 0x"
00342 << hex << setfill('0') << setw(8) << loc
00343 << setfill(' ') << dec << endl
00344 << " at offset 0x"
00345 << hex << setfill('0') << setw(8) << (fLocStart[loc]-fRawBlock)
00346 << setfill(' ') << dec
00347 << " found again at 0x"
00348 << hex << setfill('0') << setw(8) << (ptr-fRawBlock)
00349 << setfill(' ') << dec
00350 << endl;
00351 fLocation.push_back(loc);
00352 fLocStart[loc] = ptr;
00353
00354 Int_t skip = 2 + npts*qieCalibPointIntSize;
00355 ptr += skip;
00356 }
00357
00358 if ( ptr != end )
00359 MSG("RawData",Msg::kError) << endl
00360 << "FillLocStart() unpacking did not end evenly at block boundary"
00361 << endl;
00362
00363 return fLocStart.size();
00364 }
00365
00366
00367 const Int_t* RawQieCalibrationDataBlock::StartOfLocPoint(UInt_t loc, UInt_t ipt) const
00368 {
00369
00370
00371
00372
00373 FillLocStart();
00374 const Int_t* ptr = fLocStart[loc];
00375 if (!ptr) {
00376 MSG("RawData",Msg::kError)
00377 << " No entry for 'location' 0x"
00378 << hex << setfill(' ') << setw(8) << loc
00379 << setfill(' ') << dec << endl;
00380 return 0;
00381 }
00382 UInt_t npts = *(ptr+1);
00383 if ( ipt >= npts ) {
00384 MSG("RawData",Msg::kError)
00385 << "ipt " << ipt << " for 'location' 0x"
00386 << hex << setfill(' ') << setw(8) << loc << setfill(' ') << dec
00387 << " that has only " << npts << " points" << endl;
00388 return 0;
00389 }
00390
00391 ptr += (2 + ipt*qieCalibPointIntSize);
00392 return ptr;
00393 }
00394
00395
00396 std::ostream& RawQieCalibrationDataBlock::FormatToOStream(std::ostream& os,
00397 Option_t *option) const
00398 {
00399
00400 RawDataBlock::FormatToOStream(os,option);
00401 if (option[0] == 'X') return os;
00402
00403
00404
00405 os << " Run " << GetRun() << " SubRun " << GetSubRun()
00406 << " RunType " << GetRunType() << endl;
00407 if (GetMinorId()>0) {
00408 FillCrateId();
00409 os << " CrateId " << fCrateId.AsString("C") << ", ";
00410 }
00411 UInt_t ndacv = GetNumCalPoints();
00412 const Int_t* dacv = GetDacValues();
00413 os << " " << ndacv << " Dac Values" << endl;
00414 for (UInt_t idacv = 0; idacv < ndacv; ++idacv) {
00415 os << " [" << setw(3) << idacv << "] "
00416 << setw(5) << dacv[idacv];
00417 if (idacv%4==3) os << endl;
00418 }
00419 if ((ndacv-1)%4!=3) os << endl;
00420
00421 UInt_t nloc = GetNumLocations();
00422 os << " " << nloc << " 'locations' "<< endl;
00423
00424
00425
00426
00427
00428 os << " range entries mean RMS Sum SumSqr" << endl;
00429 for (UInt_t indx = 0; indx < nloc; ++indx) {
00430 UInt_t loc = GetLocation(indx);
00431 UInt_t npt = GetNumPoints(loc);
00432 os << " location 0x"
00433 << hex << setfill('0') << setw(8) << loc << setfill(' ') << dec
00434 << " ( index = " << setw(3) << indx << " )"
00435 << " has " << npt << " points" << endl;
00436 for (UInt_t ipt = 0; ipt < npt; ++ipt) {
00437 os << " [" << setw(3) << ipt << "] "
00438 << setw(8) << GetRange(loc,ipt) << " "
00439 << setw(8) << GetNumEntries(loc,ipt) << " "
00440 << setw(12) << GetMean(loc,ipt) << " "
00441 << setw(12) << GetRms(loc,ipt) << " "
00442 << setw(10) << GetSum(loc,ipt) << " "
00443 << setw(12) << GetSumSqr(loc,ipt)
00444 << endl;
00445 }
00446 }
00447 return os;
00448 }
00449
00450