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