/** * @file * @author Christian Simon * @since 2017-08-07 */ #include #include #include #include #include "TString.h" #include "FairLogger.h" #include "FairMCPoint.h" #include "CbmDaqPointBuffer.h" #include "CbmModuleList.h" using std::setprecision; using std::stringstream; using std::fixed; using std::string; using std::pair; using std::multimap; // ----- Initialisation of static variables ------------------------------ CbmDaqPointBuffer* CbmDaqPointBuffer::fgInstance = NULL; // --------------------------------------------------------------------------- // ----- Constructor ----------------------------------------------------- CbmDaqPointBuffer::CbmDaqPointBuffer() { } // --------------------------------------------------------------------------- // ----- Destructor ------------------------------------------------------ CbmDaqPointBuffer::~CbmDaqPointBuffer() { } // --------------------------------------------------------------------------- // ----- Time of first raw data ------------------------------------------ Double_t CbmDaqPointBuffer::GetFirstTime() const { Double_t time = -1.; Bool_t firstDetector = kTRUE; for (Int_t iDet = kRef; iDet < kNofSystems; iDet++) { if ( GetSize(iDet) ) { if ( firstDetector ) { time = GetFirstTime(iDet); firstDetector = kFALSE; } //? first detector with data else time = ( time < GetFirstTime(iDet) ? time : GetFirstTime(iDet) ); } //? detector has data } //# detectors return time; } // --------------------------------------------------------------------------- // ----- Time of first raw data for detector ------------------------------ Double_t CbmDaqPointBuffer::GetFirstTime(Int_t iDet) const { if ( iDet < kRef || iDet >= kNofSystems ) return -1.; if ( ! GetSize(iDet) ) return -1.; assert ( (fData[iDet].begin())->second ); return (fData[iDet].begin())->second->GetTime(); } // --------------------------------------------------------------------------- // ----- Time of last raw data ------------------------------------------- Double_t CbmDaqPointBuffer::GetLastTime() const { Double_t time = -1.; Bool_t firstDetector = kTRUE; for (Int_t iDet = kRef; iDet < kNofSystems; iDet++) { if ( GetSize(iDet) ) { if ( firstDetector ) { time = GetLastTime(iDet); firstDetector = kFALSE; } //? first detector else time = ( time > GetLastTime(iDet) ? time : GetLastTime(iDet) ); } //? detector has data } //# detectors return time; } // --------------------------------------------------------------------------- // ----- Time of last raw data for detector ------------------------------- Double_t CbmDaqPointBuffer::GetLastTime(Int_t iDet) const { if ( iDet < kRef || iDet >= kNofSystems ) return -1.; if ( ! GetSize(iDet) ) return -1.; assert ( (--fData[iDet].end())->second ); return (--fData[iDet].end())->second->GetTime(); } // --------------------------------------------------------------------------- // ----- Access to next data --------------------------------------------- FairMCPoint* CbmDaqPointBuffer::GetNextData(Int_t iDet) { // --- Check for system ID if ( iDet >= kNofSystems ) { LOG(WARNING) << "DaqPointBuffer: Illegal system ID " << iDet << FairLogger::endl; return NULL; } // --- Check for empty buffer if ( ! fData[iDet].size() ) return NULL; // --- Get data from buffer FairMCPoint* point = NULL; multimap::iterator it = fData[iDet].begin(); FairMCPoint* test = it->second; point = test; fData[iDet].erase(it); return point; } // --------------------------------------------------------------------------- // ----- Access to next data with time limit ------------------------------ FairMCPoint* CbmDaqPointBuffer::GetNextData(Int_t iDet, Double_t time) { // --- Check for system ID if ( iDet >= kNofSystems ) { LOG(WARNING) << "DaqPointBuffer: Illegal system ID " << iDet << FairLogger::endl; return NULL; } // --- Check for empty buffer if ( ! fData[iDet].size() ) return NULL; // --- Get data from buffer FairMCPoint* point = NULL; multimap::iterator it = fData[iDet].begin(); FairMCPoint* test = it->second; if ( test->GetTime() < time ) { point = test; fData[iDet].erase(it); } return point; } // --------------------------------------------------------------------------- // ----- Number of objects in buffer ------------------------------------- Int_t CbmDaqPointBuffer::GetSize() const { Int_t size = 0; for (Int_t iDet = kRef; iDet < kNofSystems; iDet++) size += fData[iDet].size(); return size; } // --------------------------------------------------------------------------- // ----- Number of objects in buffer for given detector ------------------ Int_t CbmDaqPointBuffer::GetSize(Int_t det) const { if ( det < kRef || det > kNofSystems) return 0; return fData[det].size(); } // --------------------------------------------------------------------------- // ----- Insert data into buffer ----------------------------------------- void CbmDaqPointBuffer::InsertData(Int_t iDet, FairMCPoint* point) { if ( ! point ) LOG(FATAL) << "DaqPointBuffer: invalid MC point pointer" << FairLogger::endl; if ( iDet >= kNofSystems) { LOG(WARNING) << "DaqPointBuffer: Illegal system ID " << iDet << FairLogger::endl; return; } pair value (point->GetTime(), point); fData[iDet].insert(value); LOG(DEBUG2) << "DaqPointBuffer: Inserting point, detectorID " << point->GetDetectorID() << ", time " << point->GetTime() << FairLogger::endl; } // --------------------------------------------------------------------------- // ----- Instance -------------------------------------------------------- CbmDaqPointBuffer* CbmDaqPointBuffer::Instance() { if ( ! fgInstance ) fgInstance = new CbmDaqPointBuffer(); return fgInstance; } // --------------------------------------------------------------------------- // ----- Print status ---------------------------------------------------- void CbmDaqPointBuffer::PrintStatus() const { TString sysName; Int_t size = GetSize(); LOG(INFO) << "DaqPointBuffer: Status "; if ( ! size ) { LOG(INFO) << "empty" << FairLogger::endl; return; } for (Int_t det = kRef; det < kNofSystems; det++) { if ( GetSize(det) ) { sysName = CbmModuleList::GetModuleNameCaps(det); LOG(INFO) << sysName << " " << GetSize(det) << " "; } } LOG(INFO) << "\t " << "Total: " << GetSize() << " from " << fixed << setprecision(3) << GetFirstTime() << " ns to " << GetLastTime() << " ns" << FairLogger::endl; } // --------------------------------------------------------------------------- // ----- Status to string ------------------------------------------------ string CbmDaqPointBuffer::ToString() const { stringstream ss; ss << "DaqPointBuffer: "; Int_t size = GetSize(); if ( ! size ) { ss << "empty"; return ss.str(); } TString sysName; for (Int_t det = kRef; det < kNofSystems; det++) { if ( GetSize(det) ) { sysName = CbmModuleList::GetModuleNameCaps(det); ss << sysName << " " << GetSize(det) << " "; } } ss << "Total: " << size << " from " << fixed << setprecision(3) << GetFirstTime() << " ns to " << GetLastTime() << " ns"; return ss.str(); } // ---------------------------------------------------------------------------