00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012 #include <cstring>
00013
00014 #include "RawData/RawTpSinglesSummaryBlock.h"
00015 #include "RawData/RawCrateStatus.h"
00016
00017 enum EDebugTpSinglesSummaryBlock {
00018 kdbg_ForceHexDump = 0x0001
00019 };
00020 UInt_t RawTpSinglesSummaryBlock::fgDebugFlags = 0;
00021
00022 #include <iomanip>
00023
00024 #include "MessageService/MsgService.h"
00025 CVSID("$Id: RawTpSinglesSummaryBlock.cxx,v 1.24 2009/02/28 21:46:16 gmieg Exp $");
00026
00027 #include "RawData/RawBlockRegistry.h"
00028 REGISTERRAWBLOCK(RawTpSinglesSummaryBlock,kMdBlockTpSummarySingles,0);
00029
00030 ClassImp(RawTpSinglesSummaryBlock)
00031
00032 enum ESnarlHeaderBlkPos {
00033 indx_size = 0,
00034 indx_checksum = 1,
00035 indx_blockid = 2,
00036 indx_run = 3,
00037 indx_subrun = 4,
00038 indx_runtype = indx_subrun,
00039 indx_startsec = 5,
00040 indx_startnsec = 6,
00041 indx_endsec = 7,
00042 indx_endnsec = 8,
00043 indx_src = 9,
00044 indx_ncrates = 10,
00045 zzzz_last = 11
00046 };
00047
00048
00049 RawTpSinglesSummaryBlock::RawTpSinglesSummaryBlock() : RawDataBlock()
00050 {
00051
00052 }
00053
00054
00055 RawTpSinglesSummaryBlock::RawTpSinglesSummaryBlock(const Int_t *block)
00056 : RawDataBlock(block)
00057 {
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 }
00077
00078 RawTpSinglesSummaryBlock::RawTpSinglesSummaryBlock(
00079 Detector::Detector_t detector, SimFlag::SimFlag_t simflg,
00080 Int_t run, Short_t subrun, Short_t runtype,
00081 VldTimeStamp start, VldTimeStamp end,
00082 Int_t src, Int_t ncrates, Int_t* hitsPerSec)
00083 : RawDataBlock()
00084 {
00085
00086
00087 fSize = zzzz_last + ncrates;
00088 if (fRawBlock) delete [] fRawBlock;
00089 fRawBlock = new Int_t [fSize];
00090
00091 fRawBlock[0] = fSize;
00092
00093
00094 RawBlockRegistry& rbr = RawBlockRegistry::Instance();
00095 RawBlockProxy* rbp = rbr.LookUp("RawTpSinglesSummaryBlock");
00096
00097 Bool_t isDCS = rbp->IsDCS();
00098 Int_t majorId = rbp->GetMajorId();
00099 Int_t minorId = 0;
00100 RawBlockId rbid(majorId,minorId,isDCS,detector,simflg);
00101 fRawBlock[2] = rbid.GetEncoded();
00102
00103 fRawBlock[indx_run] = run;
00104 fRawBlock[indx_subrun] = (subrun&0xffff)<<16 | (runtype&0xffff);
00105
00106 fRawBlock[indx_startsec] = start.GetSec();
00107 fRawBlock[indx_startnsec] = start.GetNanoSec();
00108
00109 fRawBlock[indx_endsec] = end.GetSec();
00110 fRawBlock[indx_endnsec] = end.GetNanoSec();
00111
00112 fRawBlock[indx_src] = src;
00113
00114 fRawBlock[indx_ncrates] = ncrates;
00115
00116 for (Int_t i=0; i<ncrates; i++)
00117 fRawBlock[zzzz_last+i] = hitsPerSec[i];
00118
00119
00120 rdxsum_fill((long*)fRawBlock,0);
00121
00122 }
00123
00124
00125 RawTpSinglesSummaryBlock::~RawTpSinglesSummaryBlock()
00126 {
00127
00128 }
00129
00130
00131 Int_t RawTpSinglesSummaryBlock::GetRun() const
00132 {
00133
00134 if (fSize >= zzzz_last) return fRawBlock[indx_run];
00135 return -1;
00136 }
00137
00138
00139 Short_t RawTpSinglesSummaryBlock::GetSubRun() const
00140 {
00141
00142 if (fSize >= zzzz_last)
00143 return (fRawBlock[indx_subrun]>>16)&0xffff;
00144 return -1;
00145 }
00146
00147
00148 Short_t RawTpSinglesSummaryBlock::GetRunType() const
00149 {
00150
00151 if (fSize >= zzzz_last)
00152 return fRawBlock[indx_runtype]&0xffff;
00153 return -1;
00154 }
00155
00156
00157 VldTimeStamp RawTpSinglesSummaryBlock::GetStartTimeStamp() const
00158 {
00159
00160 if (fSize >= zzzz_last)
00161 return VldTimeStamp(fRawBlock[indx_startsec],fRawBlock[indx_startnsec]);
00162
00163 return VldTimeStamp();
00164 }
00165
00166
00167 VldTimeStamp RawTpSinglesSummaryBlock::GetEndTimeStamp() const
00168 {
00169
00170 if (fSize >= zzzz_last)
00171 return VldTimeStamp(fRawBlock[indx_endsec],fRawBlock[indx_endnsec]);
00172 return VldTimeStamp();
00173 }
00174
00175
00176 Int_t RawTpSinglesSummaryBlock::GetSource() const
00177 {
00178
00179 if (fSize >= zzzz_last) return fRawBlock[indx_src];
00180 return 0;
00181 }
00182
00183
00184 Int_t RawTpSinglesSummaryBlock::GetNumberOfCrates() const
00185 {
00186
00187 if (fSize >= zzzz_last) return fRawBlock[indx_ncrates];
00188 return 0;
00189 }
00190
00191
00192 const Int_t* RawTpSinglesSummaryBlock::GetHitsPerSecArray() const
00193 {
00194
00195 if (fSize >= zzzz_last) return fRawBlock+zzzz_last;
00196 return 0;
00197 }
00198
00199
00200 Int_t RawTpSinglesSummaryBlock::GetHitsPerSecByCrate(Int_t crate) const
00201 {
00202
00203 if (fSize > zzzz_last+crate)
00204 return fRawBlock[zzzz_last+crate];
00205 return -1;
00206 }
00207
00208
00209 std::ostream& RawTpSinglesSummaryBlock::FormatToOStream(std::ostream& os,
00210 Option_t *option) const
00211 {
00212 if (fgDebugFlags & kdbg_ForceHexDump) option = "x";
00213
00214 RawDataBlock::FormatToOStream(os,option);
00215 if (option[0] == 'X') return os;
00216
00217
00218
00219 Int_t src = GetSource();
00220
00221 os << " Run " << GetRun()
00222 << " SubRun " << GetSubRun()
00223 << " RunType " << GetRunType()
00224 << " : "
00225 << " Source " << src << " (";
00226 switch (src) {
00227 case 0: os << "Crate only"; break;
00228 case 1: os << "Crate and VARC"; break;
00229 case 2: os << "Crate and VFB/ADCSEL"; break;
00230 case 3: os << "Crate and VACHIP"; break;
00231 case 4: os << "Crate and Plane"; break;
00232 case 5: os << "Crate and MASTER"; break;
00233 case 6: os << "Crate and MINDER"; break;
00234 case 7: os << "Crate and MENU"; break;
00235 default: os << "Crate and ??"; break;
00236 }
00237 os << ")" << endl;
00238 os << " Start " << GetStartTimeStamp() << endl;
00239 os << " End " << GetEndTimeStamp() << endl;
00240 for (Int_t i=0; i<GetNumberOfCrates(); i++)
00241 os << " crate " << setw(3) << i << ": "
00242 << setiosflags(ios::right)
00243 << setw(8) << GetHitsPerSecByCrate(i)
00244 << setiosflags(ios::left)
00245 << " hits/sec" << endl;
00246
00247 if (strchr(option,'q') != 0) return os;
00248
00249 typedef std::map<RawChannelId,UInt_t> RCIdToRateMap;
00250 const RCIdToRateMap& chnrates = GetChannelRates();
00251
00252 if ( src >= 1 && src <= 3 ) {
00253
00254
00255 os << " " << chnrates.size() << " individual rates " << endl;
00256
00257
00258 RCIdToRateMap::const_iterator rateItr = chnrates.begin();
00259 RCIdToRateMap::const_iterator rateEnd = chnrates.end();
00260 RawChannelId rcid_last(0xffffffff);
00261 rcid_last.SetElecType(ElecType::kVA);
00262
00263
00264 while ( rateItr != rateEnd ) {
00265 RawChannelId rcid = rateItr->first;
00266 Int_t arate = rateItr->second;
00267
00268 if ( rcid.GetCrate() != rcid_last.GetCrate() )
00269 os << endl << " Crate: " << setw(2) << rcid.GetCrate() << " ";
00270
00271 if ( src == 1 ) {
00272 UShort_t varcid = rcid.GetVarcId();
00273 if ( varcid == 0) os << " VarcId: ";
00274 os << " [" << varcid << "] "
00275 << setw(6) << arate << " ";
00276 }
00277 else {
00278
00279
00280
00281 if ( src == 2 ) {
00282 if ( rcid.GetVarcId() != rcid_last.GetVarcId() )
00283 os << endl << " Varc: " << rcid.GetVarcId() << " ";
00284 os << "[" << setw(2) << rcid.GetVfb() << "] "
00285 << setw(5) << arate << " ";
00286 }
00287 else {
00288 if ( rcid.GetVarcId() != rcid_last.GetVarcId() ||
00289 rcid.GetVfb() != rcid_last.GetVfb() )
00290 os << endl << " Varc: " << rcid.GetVarcId()
00291 << " Vfb: " << setw(2) << rcid.GetVfb() << " ";
00292 os << "[" << rcid.GetVaChip() << "] "
00293 << setw(5) << arate << " ";
00294 }
00295 }
00296
00297 rateItr++;
00298 rcid_last = rcid;
00299 }
00300 }
00301 else if ( src == 4 ) {
00302
00303 const std::vector<UInt_t> plnrates = GetPlaneRates();
00304 os << " " << plnrates.size() << " plane rates " << endl << " ";
00305 const size_t nplnline = 5;
00306 size_t iplnline=0;
00307 for (size_t ipln=0; ipln<plnrates.size(); ++ipln) {
00308 os << " [" << setw(4) << ipln
00309 << "] " << setw(6) << plnrates[ipln];
00310 if (++iplnline==nplnline) { os << endl << " "; iplnline=0; }
00311 }
00312 if (iplnline!=0) os << endl;
00313 }
00314 else if ( src >= 5 && src <= 7 ) {
00315
00316
00317 os << " " << chnrates.size() << " individual rates " << endl;
00318
00319
00320 RCIdToRateMap::const_iterator rateItr = chnrates.begin();
00321 RCIdToRateMap::const_iterator rateEnd = chnrates.end();
00322 RawChannelId rcid_last(0xffffffff);
00323 rcid_last.SetElecType(ElecType::kQIE);
00324
00325
00326 while ( rateItr != rateEnd ) {
00327 RawChannelId rcid = rateItr->first;
00328 Int_t arate = rateItr->second;
00329
00330 if ( rcid.GetCrate() != rcid_last.GetCrate() )
00331 os << endl << " Crate: " << setw(2) << rcid.GetCrate() << " ";
00332
00333 if ( src == 5 ) {
00334 UShort_t master = rcid.GetGeographicAddress();
00335 if ( master != rcid_last.GetGeographicAddress() )
00336 os << endl << " " << " Master: ";
00337 os << " [" << setw(2) << master << "] "
00338 << setw(6) << arate << " ";
00339 }
00340 else {
00341
00342
00343
00344 const size_t nminderline = 6;
00345 static size_t iminderline = 0;
00346 if ( src == 6 ) {
00347 if ( rcid.GetGeographicAddress() !=
00348 rcid_last.GetGeographicAddress() ) {
00349 iminderline = 0;
00350 os << endl << " Master: " << setw(2)
00351 << rcid.GetGeographicAddress()
00352 << endl << " ";
00353 }
00354 os << "[" << setw(2) << rcid.GetMasterChannel() << "] "
00355 << setw(5) << arate << " ";
00356 }
00357 else {
00358 if ( rcid.GetGeographicAddress() != rcid_last.GetGeographicAddress() ||
00359 rcid.GetMasterChannel() != rcid_last.GetMasterChannel() ) {
00360 iminderline = 0;
00361 os << endl << " Master: " << rcid.GetGeographicAddress()
00362 << " Minder: " << setw(2) << rcid.GetMasterChannel()
00363 << endl << " ";
00364 }
00365 os << "[" << setw(2) << rcid.GetMinderChannel() << "] "
00366 << setw(5) << arate << " ";
00367 }
00368 if (++iminderline==nminderline) {
00369 os << endl << " ";
00370 iminderline=0;
00371 }
00372 }
00373
00374 rateItr++;
00375 rcid_last = rcid;
00376 }
00377 os << endl;
00378
00379 }
00380
00381 return os;
00382 }
00383
00384
00385 void RawTpSinglesSummaryBlock::UnpackRates() const
00386 {
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 Int_t src = GetSource();
00403 if ( src == 0 ) return;
00404 else if ( src == 4 ) {
00405 if ( !fPlaneRates.empty() ) return;
00406 }
00407 else {
00408 if ( !fChannelRates.empty() ) return;
00409 }
00410
00411 RawBlockId rbid = GetBlockId();
00412 Int_t minor = rbid.GetMinor();
00413 if ( minor < 2 ) {
00414 MSG("Raw",Msg::kWarning)
00415 << " minor version " << minor << endl
00416 << " should have no sub-crate info,"
00417 << " but Source = " << src << " so try unpacking anyway." << endl;
00418 }
00419
00420 switch (src) {
00421 case 1:
00422 case 2:
00423 case 3:
00424 UnpackVaRates();
00425 break;
00426 case 4:
00427 UnpackPlaneRates();
00428 break;
00429 case 5:
00430 case 6:
00431 case 7:
00432 UnpackQieRates();
00433 break;
00434 default:
00435 {
00436 MSG("Raw",Msg::kWarning)
00437 << rbid << " with source = " << src << " is unknown format." << endl;
00438 return;
00439 }
00440 }
00441
00442 }
00443
00444
00445 void RawTpSinglesSummaryBlock::UnpackVaRates() const
00446 {
00447
00448
00449
00450 Int_t src = GetSource();
00451 RawBlockId rbid = GetBlockId();
00452 Detector::Detector_t det = rbid.GetDetector();
00453
00454 const Int_t *p = fRawBlock;
00455 const Int_t *end = fRawBlock + fSize;
00456
00457 p += zzzz_last + GetNumberOfCrates();
00458
00459 const UInt_t unknown = (UInt_t)(-1);
00460
00461 while ( p < end ) {
00462 Int_t crateid = *p++;
00463 Int_t nvarcs = *p++;
00464
00465 if ( src == 1 ) {
00466 for (Int_t varcid = 0; varcid < nvarcs; varcid++) {
00467 Int_t varcRate = *p++;
00468 AddVaRate(det,crateid,varcid,unknown,unknown,varcRate);
00469 }
00470 }
00471 else {
00472
00473 for (Int_t ivarc = 0; ivarc < nvarcs; ivarc++) {
00474 Int_t xyzzy = *p++;
00475 UInt_t nchip = ( xyzzy ) & 0xff;
00476 UInt_t nvfb = ( xyzzy >> 8 ) & 0xff;
00477 UInt_t varcid = ( xyzzy >> 16 ) & 0xff;
00478 for (UInt_t ivfb = 0; ivfb < nvfb; ++ivfb) {
00479 if ( src == 2 ) {
00480 Int_t vfbRate = *p++;
00481 AddVaRate(det,crateid,varcid,ivfb,unknown,vfbRate);
00482 }
00483 else if (src == 3 ) {
00484 if (nchip != 3) {
00485 MAXMSG("Raw",Msg::kWarning,10)
00486 << rbid << " unpack at chip level but Nchip = "
00487 << nchip << " != 3 " << endl;
00488 }
00489 for (UInt_t ichip = 0; ichip < nchip; ++ichip) {
00490 Int_t chipRate = *p++;
00491 AddVaRate(det,crateid,varcid,ivfb,ichip,chipRate);
00492 }
00493 }
00494 }
00495 }
00496
00497 }
00498
00499 }
00500
00501 }
00502
00503
00504 void RawTpSinglesSummaryBlock::UnpackPlaneRates() const
00505 {
00506
00507
00508
00509 RawBlockId rbid = GetBlockId();
00510
00511
00512 const Int_t *p = fRawBlock;
00513 const Int_t *end = fRawBlock + fSize;
00514
00515 p += zzzz_last + GetNumberOfCrates();
00516
00517 UInt_t nplanes = *p++;
00518 fPlaneRates.resize(nplanes);
00519
00520 UInt_t ipln = 0;
00521 while ( p < end && ipln < nplanes ) {
00522 Int_t planeRate = *p++;
00523 fPlaneRates[ipln++] = planeRate;
00524 }
00525
00526 if ( p < end ) {
00527 MAXMSG("Raw",Msg::kWarning,10)
00528 << rbid << " plane unpack failed to use all data: "
00529 << p << "<" << end << endl;
00530 }
00531 else if ( ipln < nplanes ) {
00532 MAXMSG("Raw",Msg::kWarning,10)
00533 << rbid << " plane unpack failed to find all data: "
00534 << ipln << "<" << nplanes << endl;
00535 }
00536
00537 }
00538
00539
00540 void RawTpSinglesSummaryBlock::UnpackQieRates() const
00541 {
00542
00543
00544
00545 Int_t src = GetSource();
00546 RawBlockId rbid = GetBlockId();
00547 Detector::Detector_t det = rbid.GetDetector();
00548
00549 const Int_t *p = fRawBlock;
00550 const Int_t *end = fRawBlock + fSize;
00551
00552 p += zzzz_last + GetNumberOfCrates();
00553
00554 const UInt_t unknown = (UInt_t)(-1);
00555
00556 while ( p < end ) {
00557 Int_t crateid = *p++;
00558 Int_t nmasters = *p++;
00559
00560
00561 Int_t imaster = nmasters >> 16;
00562 if (imaster==0) imaster = 9;
00563 nmasters &= 0xffff;
00564
00565 if ( src == 5 ) {
00566 for (Int_t jmaster = 0; jmaster < nmasters; jmaster++) {
00567 Int_t masterRate = *p++;
00568 AddQieRate(det,crateid,imaster,unknown,unknown,masterRate);
00569 imaster++;
00570 }
00571 }
00572 else {
00573
00574 for (Int_t jmaster = 0; jmaster < nmasters; jmaster++) {
00575 Int_t xyzzy = *p++;
00576 UInt_t nmenu = ( xyzzy ) & 0xff;
00577 UInt_t nminder = ( xyzzy >> 8 ) & 0xff;
00578 UInt_t masterid = ( xyzzy >> 16 ) & 0xff;
00579 for (UInt_t iminder = 0; iminder < nminder; ++iminder) {
00580 if ( src == 6 ) {
00581 Int_t minderRate = *p++;
00582 AddQieRate(det,crateid,masterid,iminder,unknown,minderRate);
00583 }
00584 else if (src == 7 ) {
00585 for (UInt_t imenu = 0; imenu < nmenu; ++imenu) {
00586 Int_t menuRate = *p++;
00587 AddQieRate(det,crateid,masterid,iminder,imenu,menuRate);
00588 }
00589 }
00590 }
00591 }
00592
00593 }
00594
00595 }
00596
00597 }
00598
00599
00600 bool RawTpSinglesSummaryBlock::AddVaRate(Detector::Detector_t det,
00601 Int_t crateid, UInt_t varcid,
00602 UInt_t vfb, UInt_t vachip,
00603 UInt_t rate) const
00604 {
00605
00606
00607 const UInt_t unknown = (UInt_t)(-1);
00608
00609 UInt_t adcsel = vfb & 0x01;
00610 UInt_t vmm = vfb >> 1;
00611
00612 UInt_t crateinfo = (ElecType::kVA<<rcsShiftElec) | (crateid&rcsMaskCrate);
00613
00614 Int_t chadd = RawChannelId::CalcChAdd(varcid,vmm,adcsel,vachip,unknown);
00615 RawChannelId rcid(det,crateinfo,chadd);
00616
00617 return AddChannelRate(rcid,rate);
00618 }
00619
00620
00621 bool RawTpSinglesSummaryBlock::AddQieRate(Detector::Detector_t det,
00622 Int_t crateid, UInt_t master,
00623 UInt_t minder, UInt_t menu,
00624 UInt_t rate) const
00625 {
00626
00627
00628 UInt_t crateinfo = (ElecType::kQIE<<rcsShiftElec) | (crateid&rcsMaskCrate);
00629
00630 Int_t chadd = RawChannelId::CalcChAdd(master,minder,menu);
00631 RawChannelId rcid(det,crateinfo,chadd);
00632
00633 return AddChannelRate(rcid,rate);
00634 }
00635
00636
00637 bool RawTpSinglesSummaryBlock::AddChannelRate(RawChannelId rcid,
00638 UInt_t rate) const
00639 {
00640
00641
00642 typedef std::map<RawChannelId,UInt_t> RCIdToRateMap;
00643 RCIdToRateMap::const_iterator itr;
00644
00645 if ( (itr = fChannelRates.find(rcid)) == fChannelRates.end() ) {
00646 fChannelRates[rcid] = rate;
00647 return true;
00648 }
00649 else {
00650 MSG("Raw",Msg::kWarning)
00651 << " duplicate rate for " << rcid.AsString("e")
00652 << " prior value: " << itr->second
00653 << ", later value: " << rate
00654 << endl;
00655 return false;
00656 }
00657
00658 }
00659
00660