JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwEventBuffer.h
Go to the documentation of this file.
1
2/*!
3 * \file QwEventBuffer.h
4 * \brief Event buffer management for reading and processing CODA data
5 * \author P. M. King
6 * \date 2008-07-22
7 */
8
9#pragma once
10
11#include <chrono>
12#include <string>
13#include <vector>
14#include "Rtypes.h"
15#include "TString.h"
16#include "TStopwatch.h"
17
18#include "THaCodaData.h"
19
20#include "MQwCodaControlEvent.h"
21#include "QwParameterFile.h"
22
23#include <unordered_map>
24
25#include "VEventDecoder.h"
26#include "Coda3EventDecoder.h"
27#include "Coda2EventDecoder.h"
28
29class QwOptions;
30class QwEPICSEvent;
31class VQwSubsystem;
33
34//////////////////////////////////////////////////////////////////////
35
36
37/**
38 * \class QwEventBuffer
39 * \ingroup QwAnalysis
40 * \brief Event buffer management for reading and processing CODA data
41 *
42 * Manages the reading of CODA event data files, including support for
43 * segmented files, run lists, and event stream processing. Handles
44 * event decoding via pluggable decoder classes and provides iteration
45 * over events and runs.
46 */
48 public:
49 static void DefineOptions(QwOptions &options);
50 static void SetDefaultDataDirectory(const std::string& dir) {
52 }
53 static void SetDefaultDataFileStem(const std::string& stem) {
55 }
56 static void SetDefaultDataFileExtension(const std::string& extension) {
57 fDefaultDataFileExtension = extension;
58 }
59
60 public:
61 static const Int_t kRunNotSegmented;
62 static const Int_t kNoNextDataFile;
63 static const Int_t kFileHandleNotConfigured;
64
65 static const UInt_t kNullDataWord;
66
67
68
69 public:
71 virtual ~QwEventBuffer() {
72 // Delete event stream
73 if (fEvStream != NULL) {
74 delete fEvStream;
75 fEvStream = NULL;
76 }
77 // Delete Decoder
78 if(decoder != NULL) {
79 delete decoder;
80 decoder = NULL;
81 }
82 };
83
84 /// \brief Sets internal flags based on the QwOptions
85 void ProcessOptions(QwOptions &options);
86
87 void PrintRunTimes();
88
89 /// \brief Returns a string like <run#> or <run#>.<file#>
90 TString GetRunLabel() const;
91 /// \brief Return true if file segments are being separated for
92 //analysis
93 Bool_t AreRunletsSplit() const {
95 };
96 /// \brief Return CODA file run number
97 Int_t GetRunNumber() const {return fCurrentRun;};
98 /// \brief Return CODA file segment number
99 Int_t GetSegmentNumber() const {
100 return fRunSegments.size() ? *fRunSegmentIterator : 0;
101 };
102
103 std::pair<UInt_t, UInt_t> GetEventRange() const {
104 return fEventRange;
105 };
106
107 /// \brief Opens the event stream (file or ET) based on the internal flags
108 Int_t OpenNextStream();
109 /// \brief Closes a currently open event stream.
110 Int_t CloseStream();
111
112 void SetDataDirectory(const TString datadir){fDataDirectory = datadir;}
113
114
115 const TString& GetDataFile() const {return fDataFile;};
116 const TString& GetDataDirectory() const {return fDataDirectory;};
117
118 Int_t ReOpenStream();
119
120 Int_t OpenDataFile(UInt_t current_run, Short_t seg);
121 Int_t OpenDataFile(UInt_t current_run, const TString rw = "R");
122 Int_t OpenDataFile(const TString filename, const TString rw = "R");
123 Int_t CloseDataFile();
124
125 Int_t OpenETStream(TString computer, TString session, int mode, const TString stationname="");
126 Int_t CloseETStream();
127
128 Bool_t IsPhysicsEvent() {
129 return ( decoder->IsPhysicsEvent() );
130 };
131
133 Int_t GetEventNumber() { return decoder->GetEvtNumber(); };
134
135 Bool_t GetNextEventRange();
136 Bool_t GetNextRunRange();
137 Bool_t GetNextRunNumber();
138 void VerifyCodaVersion( const UInt_t *buffer);
139
140 Int_t GetNextEvent();
141
142 Int_t GetEvent();
143 Int_t WriteEvent(int* buffer);
144
145 Bool_t IsOnline(){return fOnline;};
146
148 return ( decoder->IsROCConfigurationEvent() );
149 };
150
151 Bool_t IsEPICSEvent(){
152 return ( decoder->IsEPICSEvent() ); // Defined in CodaDecoder.h
153 }
154
156 Bool_t FillSubsystemData(QwSubsystemArray &subsystems);
157
158 Bool_t FillEPICSData(QwEPICSEvent &epics);
159
160 template < class T > Bool_t FillObjectWithEventData(T &t);
161
162
164 void ReportRunSummary();
165 Int_t EncodeSubsystemData(QwSubsystemArray &subsystems);
166 Int_t EncodePrestartEvent(int runnumber, int runtype = 0);
167 Int_t EncodeGoEvent();
168 Int_t EncodePauseEvent();
169 Int_t EncodeEndEvent();
170
171 TString GetStartSQLTime();
172 TString GetEndSQLTime();
173 time_t GetStartUnixTime();
174 time_t GetEndUnixTime();
175
176 void ResetFlags();
177
178 private:
179 // These methods will be removed from a future version
180 void ClearEventData(std::vector<VQwSubsystem*> &subsystems);
181
182 Bool_t FillSubsystemConfigurationData(std::vector<VQwSubsystem*> &subsystems);
183 Bool_t FillSubsystemData(std::vector<VQwSubsystem*> &subsystems);
184
185 // Coda Version that is set by void VerifyCodaVersion( )
186 // Compared against the user-input coda version
188 Int_t fDataVersion; // User-input Coda Version
189
190 protected:
191 ///
192 Bool_t fOnline;
193 TString fETHostname;
194 TString fETSession;
198
199 // Event rate limiting
201 Double_t fMaxEventRate{0.0};
202 std::chrono::duration<double> fMinEventInterval;
203 std::chrono::duration<double> fAccumulatedDelay{0.0};
204 std::chrono::steady_clock::time_point fLastEventTime;
205
207 std::pair<Int_t, Int_t> fRunRange;
208 std::string fRunListFileName;
209 std::unique_ptr<QwParameterFile> fRunListFile;
211
212 std::pair<UInt_t, UInt_t> fEventRange;
214 std::unique_ptr<QwParameterFile> fEventListFile;
215 std::vector<UInt_t> fEventList;
216
217 std::pair<Int_t, Int_t> fSegmentRange;
218
219 protected:
220
221 static std::string fDefaultDataDirectory;
222 static std::string fDefaultDataFileStem;
223 static std::string fDefaultDataFileExtension;
224
227
229 TString fDataFile;
230
231 // UInt_t fRunNumber;
232
233
234 protected:
235 Int_t GetFileEvent();
236 Int_t GetEtEvent();
237
238 Int_t WriteFileEvent(int* buffer);
239 Int_t WriteEtEvent(int* buffer);
240
241 Bool_t DataFileIsSegmented();
242
243 Int_t CloseThisSegment();
244 Int_t OpenNextSegment();
245
246 const TString& DataFile(const UInt_t run, const Short_t seg);
247
248 // void SetEventLength(const ULong_t tmplength) {fEvtLength = tmplength;};
249 // void SetEventType(const UInt_t tmptype) {fEvtType = tmptype;};
250 // void SetWordsSoFar(const ULong_t tmpwords) {fWordsSoFar = tmpwords;};
251
252
253
254 protected:
256 THaCodaData *fEvStream; // Pointer to a THaCodaFile or THaEtClient
257
259
261
262
263 std::vector<Int_t> fRunSegments;
264 std::vector<Int_t>::iterator fRunSegmentIterator;
265
266
267 protected:
268 Double_t fCleanParameter[3]; ///< Scan data/clean data from the green monster
269
270
271 TStopwatch fRunTimer; ///< Timer used for runlet processing loop
272 TStopwatch fStopwatch; ///< Timer used for internal timing
273
274 protected:
275 /// Methods and data members needed to find marker words
277 std::unordered_map<RocBankLabel_t, std::vector<UInt_t> > fMarkerList;
278 std::unordered_map<RocBankLabel_t, std::vector<UInt_t> > fOffsetList;
279
280 std::size_t CheckForMarkerWords(QwSubsystemArray &subsystems);
282 UInt_t FindMarkerWord(UInt_t markerID, UInt_t* buffer, UInt_t num_words);
283 UInt_t GetMarkerWord(UInt_t markerID);
284
285 protected:
288
290
291 protected:
293};
294
295template < class T > Bool_t QwEventBuffer::FillObjectWithEventData(T &object){
296 /// Template to fill any object with data from a CODA event.
297 ///
298 /// The classes for which this template can be specialized
299 /// must have the following three methods defined:
300 ///
301 /// - Bool_t T::CanUseThisEventType(const UInt_t event_type);
302 /// - Bool_t T::ClearEventData(const UInt_t event_type);
303 /// - Int_t T::ProcessBuffer(const UInt_t event_type,
304 /// const ROCID_t roc_id, const BankID_t bank_id,
305 /// const UInt_t banktype, UInt_t* buffer, UInt_t num_words);
306 ///
307 Bool_t okay = kFALSE;
308 UInt_t *localbuff = (UInt_t*)(fEvStream->getEvBuffer());
309
310 if (decoder->GetFragLength()==1 && localbuff[decoder->GetWordsSoFar()]==kNullDataWord){
311 decoder->AddWordsSoFarAndFragLength();
312 } else if (object.CanUseThisEventType(decoder->GetEvtType())){
313 // Clear the old event information from the object
314 object.ClearEventData(decoder->GetEvtType());
315 // Loop through the data buffer in this event.
316 if (decoder->GetBankDataType() == 0x10){
317 // This bank is subbanked; loop through subbanks
318 while ((okay = decoder->DecodeSubbankHeader(&localbuff[decoder->GetWordsSoFar()]))){
319 // If this bank has further subbanks, restart the loop.
320 if (decoder->GetSubbankType() == 0x10) continue;
321 // If this bank only contains the word 'NULL' then skip
322 // this bank.
323 if (decoder->GetFragLength()==1 && localbuff[decoder->GetWordsSoFar()]==kNullDataWord){
324 decoder->AddWordsSoFarAndFragLength();
325 continue;
326 }
327 object.ProcessBuffer(decoder->GetEvtType(), decoder->GetROC(), decoder->GetSubbankTag(), decoder->GetSubbankType(),
328 &localbuff[decoder->GetWordsSoFar()],
329 decoder->GetFragLength());
330 decoder->AddWordsSoFarAndFragLength();
331 }
332 } else {
333 // This is a single bank of some type
334 object.ProcessBuffer(decoder->GetEvtType(), 0, decoder->GetBankDataType(),
335 &localbuff[decoder->GetWordsSoFar()],
336 decoder->GetEvtLength());
337 }
338 }
339 return okay;
340}
CODA control event data structure and management.
Parameter file parsing and management.
Virtual base class for event decoders to encode and decode CODA data.
CODA version 3 event decoder implementation.
CODA version 2 event decoder implementation.
unsigned long long ULong64_t
Definition QwBlinder.h:41
EPICS slow controls data management.
TString GetRunLabel() const
Returns a string like <run#> or <run#>.<file#>
static void SetDefaultDataFileExtension(const std::string &extension)
Int_t EncodeSubsystemData(QwSubsystemArray &subsystems)
TString fETHostname
static const Int_t kRunNotSegmented
Int_t EncodeGoEvent()
TString GetEndSQLTime()
time_t GetStartUnixTime()
TString fDataDirectory
ULong64_t RocBankLabel_t
Methods and data members needed to find marker words.
const TString & GetDataDirectory() const
Int_t EncodePauseEvent()
UInt_t fStartingPhysicsEvent
std::pair< UInt_t, UInt_t > GetEventRange() const
Int_t GetEventNumber()
Int_t CloseThisSegment()
void ReportRunSummary()
static std::string fDefaultDataDirectory
Bool_t IsOnline()
Int_t EncodeEndEvent()
Bool_t fChainDataFiles
std::unordered_map< RocBankLabel_t, std::vector< UInt_t > > fMarkerList
enum QwEventBuffer::CodaStreamMode fEvStreamMode
Int_t CloseStream()
Closes a currently open event stream.
Int_t WriteEtEvent(int *buffer)
Bool_t IsEPICSEvent()
Int_t OpenETStream(TString computer, TString session, int mode, const TString stationname="")
Bool_t fRunIsSegmented
Bool_t GetNextRunRange()
Read the next requested run range, return true if success.
Int_t EncodePrestartEvent(int runnumber, int runtype=0)
QwEventBuffer()
Default constructor.
void VerifyCodaVersion(const UInt_t *buffer)
Int_t WriteFileEvent(int *buffer)
Int_t OpenNextStream()
Opens the event stream (file or ET) based on the internal flags.
static const Int_t kFileHandleNotConfigured
std::vector< Int_t >::iterator fRunSegmentIterator
static void SetDefaultDataDirectory(const std::string &dir)
std::chrono::duration< double > fAccumulatedDelay
void ProcessOptions(QwOptions &options)
Sets internal flags based on the QwOptions.
Bool_t FillSubsystemData(QwSubsystemArray &subsystems)
VEventDecoder * decoder
TStopwatch fStopwatch
Timer used for internal timing.
Int_t WriteEvent(int *buffer)
Int_t GetSegmentNumber() const
Return CODA file segment number.
std::unique_ptr< QwParameterFile > fEventListFile
void ResetControlParameters()
const TString & DataFile(const UInt_t run, const Short_t seg)
virtual ~QwEventBuffer()
TString GetStartSQLTime()
void SetDataDirectory(const TString datadir)
TString fETStationName
Bool_t fEventRateLimitEnabled
static void DefineOptions(QwOptions &options)
std::unordered_map< RocBankLabel_t, std::vector< UInt_t > > fOffsetList
const TString & GetDataFile() const
static void SetDefaultDataFileStem(const std::string &stem)
Double_t fCleanParameter[3]
Scan data/clean data from the green monster.
UInt_t FindMarkerWord(UInt_t markerID, UInt_t *buffer, UInt_t num_words)
TString fDataFileExtension
Int_t GetRunNumber() const
Return CODA file run number.
std::vector< Int_t > fRunRangeMinList
std::vector< Int_t > fRunRangeMaxList
static const UInt_t kNullDataWord
static const Int_t kNoNextDataFile
Bool_t IsROCConfigurationEvent()
RocBankLabel_t fThisRocBankLabel
Double_t fMaxEventRate
Int_t GetPhysicsEventNumber()
Bool_t GetNextEventRange()
Read the next requested event range, return true if success.
Bool_t DataFileIsSegmented()
std::pair< Int_t, Int_t > fRunRange
std::pair< UInt_t, UInt_t > fEventRange
Int_t OpenDataFile(UInt_t current_run, Short_t seg)
static std::string fDefaultDataFileExtension
void ClearEventData(std::vector< VQwSubsystem * > &subsystems)
std::string fRunListFileName
Int_t OpenNextSegment()
UInt_t fNumPhysicsEvents
Bool_t FillObjectWithEventData(T &t)
Bool_t FillSubsystemData(std::vector< VQwSubsystem * > &subsystems)
TStopwatch fRunTimer
Timer used for runlet processing loop.
time_t GetEndUnixTime()
static std::string fDefaultDataFileStem
Bool_t GetNextRunNumber()
Get the next run in the active run range, proceed to next range if needed.
std::chrono::duration< double > fMinEventInterval
Bool_t IsPhysicsEvent()
Bool_t FillEPICSData(QwEPICSEvent &epics)
THaCodaData * fEvStream
Bool_t FillSubsystemConfigurationData(std::vector< VQwSubsystem * > &subsystems)
Bool_t AreRunletsSplit() const
Return true if file segments are being separated for.
std::pair< Int_t, Int_t > fSegmentRange
std::unique_ptr< QwParameterFile > fRunListFile
std::vector< UInt_t > fEventList
std::string fEventListFileName
std::size_t CheckForMarkerWords(QwSubsystemArray &subsystems)
std::chrono::steady_clock::time_point fLastEventTime
std::vector< Int_t > fRunSegments
Bool_t FillSubsystemConfigurationData(QwSubsystemArray &subsystems)
TString fDataFileStem
Int_t fDataVersionVerify
UInt_t GetMarkerWord(UInt_t markerID)
Command-line and configuration file options processor.
Definition QwOptions.h:141
Container for managing multiple subsystems with common operations.
Abstract base for CODA event encoding and decoding.
Base class for subsystems implementing container-delegation pattern.