00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Event/DaqPmtCrate.h"
00010
00011 #include "Event/DaqFadcChannel.h"
00012 #include "Event/DaqPmtChannel.h"
00013 #include "EventReadoutFormat/EventHeader.h"
00014 #include "EventReadoutFormat/EventTraits.h"
00015 #include "EventReadoutFormat/RomFragment.h"
00016 #include "EventReadoutFormat/RomHeader.h"
00017 #include "FadcReadoutFormat/FadcData.h"
00018 #include "FadcReadoutFormat/FadcReadout.h"
00019 #include "FadcReadoutFormat/FadcTraits.h"
00020 #include "FeeReadoutFormat/FeeHit.h"
00021 #include "FeeReadoutFormat/FeeReadout.h"
00022 #include "FeeReadoutFormat/FeeTraits.h"
00023
00024 using DayaBay::DaqFadcChannel;
00025 using DayaBay::DaqLtb;
00026 using DayaBay::DaqPmtChannel;
00027 using DayaBay::DaqPmtCrate;
00028 using DybDaq::EventHeader;
00029 using DybDaq::EventReadout;
00030 using DybDaq::EventTraits;
00031 using DybDaq::FadcData;
00032 using DybDaq::FadcReadout;
00033 using DybDaq::FadcTraits;
00034 using DybDaq::FeeHit;
00035 using DybDaq::FeeReadout;
00036 using DybDaq::FeeTraits;
00037 using DybDaq::RomFragment;
00038 using DybDaq::RomHeader;
00039
00040 static const unsigned int kMaxSlotCount = 22;
00041 static const unsigned int kMaxChannelCount = 32;
00042 static const unsigned int kUninitializedChannel = 0xffffffffU;
00043
00047 static const unsigned int kFeeConnectorOffset = 1;
00048 static const unsigned int kFadcConnectorOffset = 17;
00049
00050
00051 DaqPmtCrate::DaqPmtCrate(const Detector& detector,
00052 const unsigned int run,
00053 const unsigned int event) :
00054 DaqCrate(detector,
00055 run,
00056 event),
00057 m_pmtChannelPtrs(0),
00058 m_pmtIndices(0),
00059 m_feeReadouts(new FeeReadout*[kMaxSlotCount]),
00060 m_fadcChannelPtrs(0),
00061 m_fadcIndices(0),
00062 m_fadcReadouts(new FadcReadout*[kMaxSlotCount]) {
00063 for (unsigned int channel = 0;
00064 kMaxSlotCount != channel;
00065 ++channel) {
00066 m_feeReadouts[channel] = 0;
00067 }
00068 }
00069
00070 DaqPmtCrate::DaqPmtCrate(const EventReadout* eventReadout) :
00071 DaqCrate(eventReadout),
00072 m_pmtChannelPtrs(0),
00073 m_pmtIndices(0),
00074 m_feeReadouts(0),
00075 m_fadcChannelPtrs(0),
00076 m_fadcIndices(0),
00077 m_fadcReadouts(0) {
00078 }
00079
00080 DaqPmtCrate::DaqPmtCrate(const DaqPmtCrate& crate) :
00081 DaqCrate(crate),
00082 m_pmtChannelPtrs(0),
00083 m_pmtIndices(0),
00084 m_feeReadouts(0),
00085 m_fadcChannelPtrs(0),
00086 m_fadcIndices(0),
00087 m_fadcReadouts(0) {
00088 }
00089
00090 DaqPmtCrate::~DaqPmtCrate() {
00091 flushCache();
00092 }
00093
00094 DaqPmtCrate& DaqPmtCrate::operator=(const DaqPmtCrate & rhs) {
00095 if (this != &rhs) {
00096 flushCache();
00097 DaqCrate::operator=(rhs);
00098 m_pmtChannelPtrs = 0;
00099 m_pmtIndices = 0;
00100 m_feeReadouts = 0;
00101 m_fadcChannelPtrs = 0;
00102 m_fadcIndices = 0;
00103 m_fadcReadouts = 0;
00104 }
00105 return *this;
00106 }
00107
00108 void DaqPmtCrate::flushCache() {
00109 if (0 != m_fadcReadouts) {
00110
00111 delete[] m_fadcReadouts;
00112 }
00113 if (0 != m_fadcIndices) {
00114 for (unsigned int slot = 0;
00115 slot != kMaxSlotCount;
00116 ++slot) {
00117 FadcChannelPtrList::size_type* indicesInSlot = m_fadcIndices[slot];
00118 if (0 != indicesInSlot) {
00119 delete[] indicesInSlot;
00120 }
00121 }
00122 delete[] m_fadcIndices;
00123 }
00124 if (0 != m_fadcChannelPtrs) {
00125 FadcChannelPtrList::iterator finished = m_fadcChannelPtrs->end();
00126 for(FadcChannelPtrList::iterator readout = m_fadcChannelPtrs->begin();
00127 readout != finished;
00128 ++readout) {
00129 delete (*readout);
00130 }
00131 delete m_fadcChannelPtrs;
00132 }
00133
00134 if (0 != m_feeReadouts) {
00135
00136 delete[] m_feeReadouts;
00137 }
00138 if (0 != m_pmtIndices) {
00139 for (unsigned int slot = 0;
00140 slot != kMaxSlotCount;
00141 ++slot) {
00142 PmtChannelPtrList::size_type* indicesInSlot = m_pmtIndices[slot];
00143 if (0 != indicesInSlot) {
00144 delete[] indicesInSlot;
00145 }
00146 }
00147 delete[] m_pmtIndices;
00148 }
00149 if (0 != m_pmtChannelPtrs) {
00150 PmtChannelPtrList::iterator finished = m_pmtChannelPtrs->end();
00151 for(PmtChannelPtrList::iterator readout = m_pmtChannelPtrs->begin();
00152 readout != finished;
00153 ++readout) {
00154 delete (*readout);
00155 }
00156 delete m_pmtChannelPtrs;
00157 }
00158 }
00159
00160 bool DaqPmtCrate::freshPmtAttributes() const {
00161 if (0 != m_pmtChannelPtrs) {
00162 return false;
00163 }
00164
00165 PmtChannelPtrList** pmtChannelPtrs = const_cast<PmtChannelPtrList**>(&m_pmtChannelPtrs);
00166 *(pmtChannelPtrs) = new PmtChannelPtrList();
00167
00168 PmtChannelPtrList::size_type*** pmtIndicesInSlot = const_cast<PmtChannelPtrList::size_type***>(&m_pmtIndices);
00169 *(pmtIndicesInSlot) = new PmtChannelPtrList::size_type*[kMaxSlotCount];
00170 for (unsigned int slot = 0;
00171 slot != kMaxSlotCount;
00172 ++slot) {
00173 (*(pmtIndicesInSlot))[slot] = 0;
00174 }
00175
00176 return true;
00177 }
00178
00179 bool DaqPmtCrate::freshFadcAttributes() const {
00180 if (0 != m_fadcChannelPtrs) {
00181 return false;
00182 }
00183
00184 FadcChannelPtrList** fadcChannelPtrs = const_cast<FadcChannelPtrList**>(&m_fadcChannelPtrs);
00185 *(fadcChannelPtrs) = new FadcChannelPtrList();
00186
00187 FadcChannelPtrList::size_type*** fadcIndicesInSlot = const_cast<FadcChannelPtrList::size_type***>(&m_fadcIndices);
00188 *(fadcIndicesInSlot) = new FadcChannelPtrList::size_type*[kMaxSlotCount];
00189 for (unsigned int slot = 0;
00190 slot != kMaxSlotCount;
00191 ++slot) {
00192 (*(fadcIndicesInSlot))[slot] = 0;
00193 }
00194
00195 return true;
00196 }
00197
00198 const DaqLtb& DaqPmtCrate::localTriggerBoard() const {
00199 return ltb();
00200 }
00201
00202 const DaqPmtCrate::PmtChannelPtrList& DaqPmtCrate::channelReadouts() const {
00203 return pmtChannelReadouts();
00204 }
00205
00206 const DaqPmtCrate::PmtChannelPtrList& DaqPmtCrate::pmtChannelReadouts() const {
00207 const EventReadout& daqReadout = eventReadout();
00208 if (0 == &daqReadout) {
00209 return *((PmtChannelPtrList*)0);
00210 }
00211
00212 if (freshPmtAttributes()) {
00213 const Detector zzub = detector();
00214 const EventHeader& eventHeader = daqReadout.header();
00215 const EventTraits& traits = eventHeader.eventTraits();
00216 const unsigned int kFeeModuleType = traits.moduleType(EventTraits::kFeeModule);
00217 const EventReadout::RomFragmentPtrList& fragments = daqReadout.romFragments();
00218 for (EventReadout::RomFragmentPtrList::const_iterator fragment = fragments.begin();
00219 fragment != fragments.end();
00220 ++fragment) {
00221 const RomHeader& header = (*fragment)->header();
00222 if (kFeeModuleType == header.moduleType()) {
00223 const FeeReadout& readout = dynamic_cast<const FeeReadout&>((*fragment)->unwrappedData());
00224 const FeeReadout::FeeHitPtrList& hits = readout.feeHits();
00225 const unsigned int slot = header.slot();
00226 for (FeeReadout::FeeHitPtrList::const_iterator hitPtr = hits.begin();
00227 hitPtr != hits.end();
00228 ++hitPtr) {
00229 const unsigned int channelNumber = (*hitPtr)->channelId();
00230 PmtChannelPtrList::size_type* indices = pmtIndicesForSlot(slot);
00231 const unsigned int index = indices[channelNumber];
00232 if (kUninitializedChannel == index) {
00233 indices[channelNumber] = m_pmtChannelPtrs->size();
00234 m_pmtChannelPtrs->push_back(new DaqPmtChannel(*(*hitPtr),
00235 FeeChannelId(slot,
00236 channelNumber + kFeeConnectorOffset,
00237 zzub.site(),
00238 zzub.detectorId())));
00239 } else {
00240 (m_pmtChannelPtrs->at(index))->addHit(*(*hitPtr));
00241 }
00242 }
00243 }
00244 }
00245 }
00246 return *m_pmtChannelPtrs;
00247 }
00248
00249 bool DaqPmtCrate::hasChannel(const FeeChannelId& channelId) const {
00250 pmtChannelReadouts();
00251 PmtChannelPtrList::size_type* indices = pmtIndicesForSlot(channelId.board());
00252 const unsigned int index = indices[channelId.connector() - kFeeConnectorOffset];
00253 return kUninitializedChannel != index;
00254 }
00255
00256 const DaqPmtChannel& DaqPmtCrate::channel(const FeeChannelId& channelId) const {
00257 pmtChannelReadouts();
00258 PmtChannelPtrList::size_type* indices = pmtIndicesForSlot(channelId.board());
00259 const unsigned int channelNumber = channelId.connector() - kFeeConnectorOffset;
00260 if (kMaxChannelCount <= channelNumber) {
00261 return *((DaqPmtChannel*)0);
00262 }
00263 const unsigned int index = indices[channelNumber];
00264 if (kUninitializedChannel == index) {
00265 return *((DaqPmtChannel*)0);
00266 }
00267 return *(m_pmtChannelPtrs->at(index));
00268 }
00269
00270 DaqPmtChannel& DaqPmtCrate::channel(const FeeChannelId& channelId) {
00271 const unsigned int slot = channelId.board();
00272 FeeReadout* readout = m_feeReadouts[slot];
00273 if (0 == readout) {
00274 readout = new FeeReadout(localTriggerNumber(),
00275 triggerType(),
00276 false,
00277 false,
00278 FeeTraits::defaultTraits());
00279 m_feeReadouts[slot] = readout;
00280 eventReadout().addReadout(readout,
00281 slot);
00282 }
00283
00284 PmtChannelPtrList::size_type* indices = pmtIndicesForSlot(slot);
00285 const unsigned int channelNumber = channelId.connector() - kFeeConnectorOffset;
00286 if (kMaxChannelCount <= channelNumber) {
00287 return *((DaqPmtChannel*)0);
00288 }
00289 const unsigned int index = indices[channelNumber];
00290 if (kUninitializedChannel == index) {
00291 indices[channelNumber] = m_pmtChannelPtrs->size();
00292 DaqPmtChannel* channel = new DaqPmtChannel(channelId,
00293 *readout);
00294 m_pmtChannelPtrs->push_back(channel);
00295 return *channel;
00296 }
00297 return *(m_pmtChannelPtrs->at(index));
00298 }
00299
00300
00301 DaqPmtCrate::PmtChannelPtrList::size_type* DaqPmtCrate::pmtIndicesForSlot(const unsigned int slot) const {
00302 if (kMaxSlotCount <= slot) {
00303 return 0;
00304 }
00305
00306 freshPmtAttributes();
00307 PmtChannelPtrList::size_type* result = m_pmtIndices[slot];
00308 if (0 == result) {
00309 result = new PmtChannelPtrList::size_type[kMaxChannelCount];
00310 m_pmtIndices[slot] = result;
00311 for (unsigned int channel = 0;
00312 channel != kMaxChannelCount;
00313 ++channel) {
00314 result[channel] = kUninitializedChannel;
00315 }
00316 }
00317 return result;
00318 }
00319
00320 const DaqPmtCrate::FadcChannelPtrList& DaqPmtCrate::fadcChannelReadouts() const {
00321 const EventReadout& daqReadout = eventReadout();
00322 if (0 == &daqReadout) {
00323 return *((FadcChannelPtrList*)0);
00324 }
00325
00326 if (freshFadcAttributes()) {
00327 const Detector zzub = detector();
00328 const EventHeader& eventHeader = daqReadout.header();
00329 const EventTraits& traits = eventHeader.eventTraits();
00330 const unsigned int kFadcModuleType = traits.moduleType(EventTraits::kFadcModule);
00331 const EventReadout::RomFragmentPtrList& fragments = daqReadout.romFragments();
00332 for (EventReadout::RomFragmentPtrList::const_iterator fragment = fragments.begin();
00333 fragment != fragments.end();
00334 ++fragment) {
00335 const RomHeader& header = (*fragment)->header();
00336 if (kFadcModuleType == header.moduleType()) {
00337 const FadcReadout& readout = dynamic_cast<const FadcReadout&>((*fragment)->unwrappedData());
00338 const FadcReadout::FadcDataPtrList& data = readout.fadcData();
00339 const unsigned int slot = header.slot();
00340 for (FadcReadout::FadcDataPtrList::const_iterator dataPtr = data.begin();
00341 dataPtr != data.end();
00342 ++dataPtr) {
00343 const unsigned int channelNumber = (*dataPtr)->channelId();
00344 FadcChannelPtrList::size_type* indices = fadcIndicesForSlot(slot);
00345 const unsigned int index = indices[channelNumber];
00346 if (kUninitializedChannel == index) {
00347 indices[channelNumber] = m_fadcChannelPtrs->size();
00348 m_fadcChannelPtrs->push_back(new DaqFadcChannel(*(*dataPtr),
00349 FadcChannelId(slot,
00350 channelNumber + kFadcConnectorOffset,
00351 zzub.site(),
00352 zzub.detectorId())));
00353 } else {
00354 (m_fadcChannelPtrs->at(index))->addData(*(*dataPtr));
00355 }
00356 }
00357 }
00358 }
00359 }
00360 return *m_fadcChannelPtrs;
00361 }
00362
00363 bool DaqPmtCrate::hasChannel(const FadcChannelId& channelId) const {
00364 FadcChannelPtrList::size_type* indices = fadcIndicesForSlot(channelId.board());
00365 const unsigned int index = indices[channelId.connector() - kFadcConnectorOffset];
00366 return kUninitializedChannel != index;
00367 }
00368
00369 const DaqFadcChannel& DaqPmtCrate::channel(const FadcChannelId& channelId) const {
00370 FadcChannelPtrList::size_type* indices = fadcIndicesForSlot(channelId.board());
00371 const unsigned int channelNumber = channelId.connector() - kFadcConnectorOffset;
00372 if (kMaxChannelCount <= channelNumber) {
00373 return *((DaqFadcChannel*)0);
00374 }
00375 const unsigned int index = indices[channelNumber];
00376 if (kUninitializedChannel == index) {
00377 return *((DaqFadcChannel*)0);
00378 }
00379 return *(m_fadcChannelPtrs->at(index));
00380 }
00381
00382 DaqFadcChannel& DaqPmtCrate::channel(const FadcChannelId& channelId) {
00383 const unsigned int slot = channelId.board();
00384 FadcReadout* readout = m_fadcReadouts[slot];
00385 if (0 == readout) {
00386 readout = new FadcReadout(localTriggerNumber(),
00387 triggerType(),
00388 false,
00389 FadcTraits::defaultTraits());
00390 m_fadcReadouts[slot] = readout;
00391 eventReadout().addReadout(readout,
00392 slot);
00393 }
00394
00395 FadcChannelPtrList::size_type* indices = fadcIndicesForSlot(slot);
00396 const unsigned int channelNumber = channelId.connector() - kFadcConnectorOffset;
00397 if (kMaxChannelCount <= channelNumber) {
00398 return *((DaqFadcChannel*)0);
00399 }
00400 const unsigned int index = indices[channelNumber];
00401 if (kUninitializedChannel == index) {
00402 indices[channelNumber] = m_fadcChannelPtrs->size();
00403 DaqFadcChannel* channel = new DaqFadcChannel(channelId,
00404 *readout);
00405 m_fadcChannelPtrs->push_back(channel);
00406 return *channel;
00407 }
00408 return *(m_fadcChannelPtrs->at(index));
00409 }
00410
00411 DaqPmtCrate::FadcChannelPtrList::size_type* DaqPmtCrate::fadcIndicesForSlot(const unsigned int slot) const {
00412 if (kMaxSlotCount <= slot) {
00413 return 0;
00414 }
00415
00416 freshFadcAttributes();
00417 FadcChannelPtrList::size_type* result = m_fadcIndices[slot];
00418 if (0 == result) {
00419 result = new FadcChannelPtrList::size_type[kMaxChannelCount];
00420 m_fadcIndices[slot] = result;
00421 for (unsigned int channel = 0;
00422 channel != kMaxChannelCount;
00423 ++channel) {
00424 result[channel] = kUninitializedChannel;
00425 }
00426 }
00427 return result;
00428 }