QwAnalysis
QwEventBuffer.h
Go to the documentation of this file.
1 /**********************************************************\
2 * File: QwEventBuffer.h *
3 * *
4 * Author: P. M. King *
5 * Time-stamp: <2008-07-22 15:40> *
6 \**********************************************************/
7 
8 #ifndef __QWEVENTBUFFER__
9 #define __QWEVENTBUFFER__
10 
11 
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 class QwOptions;
24 class QwEPICSEvent;
25 class VQwSubsystem;
26 class QwSubsystemArray;
27 
28 //////////////////////////////////////////////////////////////////////
29 
30 
31 ///
32 /// \ingroup QwAnalysis
34  public:
35  static void DefineOptions(QwOptions &options);
36  static void SetDefaultDataFileStem(const std::string& stem) {
37  fDefaultDataFileStem = stem;
38  }
39  static void SetDefaultDataFileExtension(const std::string& extension) {
40  fDefaultDataFileExtension = extension;
41  }
42 
43  public:
44  static const Int_t kRunNotSegmented;
45  static const Int_t kNoNextDataFile;
46  static const Int_t kFileHandleNotConfigured;
47 
48  static const UInt_t kNullDataWord;
49 
50 
51 
52  public:
53  QwEventBuffer();
54  virtual ~QwEventBuffer() {
55  // Delete event stream
56  if (fEvStream != NULL) {
57  delete fEvStream;
58  fEvStream = NULL;
59  }
60  // Delete run list file
61  if (fRunListFile != NULL) {
62  delete fRunListFile;
63  fRunListFile = NULL;
64  }
65  };
66 
67  /// \brief Sets internal flags based on the QwOptions
68  void ProcessOptions(QwOptions &options);
69 
70  void PrintRunTimes();
71 
72  /// \brief Returns a string like <run#> or <run#>.<file#>
73  TString GetRunLabel() const;
74  /// \brief Return true if file segments are being separated for
75  //analysis
76  Bool_t AreRunletsSplit() const {
77  return (fRunIsSegmented && !fChainDataFiles);
78  };
79  /// \brief Return CODA file run number
80  Int_t GetRunNumber() const {return fCurrentRun;};
81  /// \brief Return CODA file segment number
82  Int_t GetSegmentNumber() const {
83  return fRunSegments.size() ? *fRunSegmentIterator : 0;
84  };
85 
86  std::pair<UInt_t, UInt_t> GetEventRange() const {
87  return fEventRange;
88  };
89 
90  /// \brief Opens the event stream (file or ET) based on the internal flags
91  Int_t OpenNextStream();
92  /// \brief Closes a currently open event stream.
93  Int_t CloseStream();
94 
95  void SetDataDirectory(const TString datadir){fDataDirectory = datadir;}
96 
97 
98  const TString& GetDataFile() const {return fDataFile;};
99 
100  Int_t ReOpenStream();
101 
102  Int_t OpenDataFile(UInt_t current_run, Short_t seg);
103  Int_t OpenDataFile(UInt_t current_run, const TString rw = "R");
104  Int_t OpenDataFile(const TString filename, const TString rw = "R");
105  Int_t CloseDataFile();
106 
107  Int_t OpenETStream(TString computer, TString session, int mode, const TString stationname="");
108  Int_t CloseETStream();
109 
110  Bool_t IsPhysicsEvent() {
111  // fEvtType is an unsigned integer, hence always positive
112  return ((fIDBankNum == 0xCC) && ( /* fEvtType >= 0 && */ fEvtType <= 15));
113  };
114 
115  Int_t GetEventNumber() { return fEvtNumber; };
116 
117  Bool_t GetNextEventRange();
118  Bool_t GetNextRunRange();
119  Bool_t GetNextRunNumber();
120 
121  Int_t GetNextEvent();
122 
123  Int_t GetEvent();
124  Int_t WriteEvent(int* buffer);
125 
127  return (fEvtType>=0x90 && fEvtType<=0xaf);
128  };
129 
130  Bool_t IsEPICSEvent(){
131  // What are the correct codes for our EPICS events?
132  //return (fEvtType>=160 && fEvtType<=170);// epics event type is only with tag="160"
133  return (fEvtType>=160 && fEvtType<=190);// epics event type is only with tag="180" from July 2010 running
134  };
135 
137  Bool_t FillSubsystemData(QwSubsystemArray &subsystems);
138 
139  Bool_t FillEPICSData(QwEPICSEvent &epics);
140 
141  template < class T > Bool_t FillObjectWithEventData(T &t);
142 
143 
144  Int_t EncodeSubsystemData(QwSubsystemArray &subsystems);
145  Int_t EncodePrestartEvent(int runnumber, int runtype = 0);
146  Int_t EncodeGoEvent();
147  Int_t EncodePauseEvent();
148  Int_t EncodeEndEvent();
149 
150  void ResetFlags();
151 
152  private:
153  // These methods will be removed from a future version
154  void ClearEventData(std::vector<VQwSubsystem*> &subsystems);
155 
156  Bool_t FillSubsystemConfigurationData(std::vector<VQwSubsystem*> &subsystems);
157  Bool_t FillSubsystemData(std::vector<VQwSubsystem*> &subsystems);
158 
159  protected:
160  ///
161  Bool_t fOnline;
162  TString fETHostname;
163  TString fETSession;
164  TString fETStationName;
165 
167  std::pair<Int_t, Int_t> fRunRange;
168  std::string fRunListFileName;
170  std::vector<Int_t> fRunRangeMinList, fRunRangeMaxList;
171 
172  std::pair<UInt_t, UInt_t> fEventRange;
173  std::string fEventListFileName;
175  std::vector<UInt_t> fEventList;
176 
177  std::pair<Int_t, Int_t> fSegmentRange;
178 
179  protected:
180 
181  static std::string fDefaultDataFileStem;
182  static std::string fDefaultDataFileExtension;
183 
184  TString fDataFileStem;
186 
187  TString fDataDirectory;
188  TString fDataFile;
189 
190  // UInt_t fRunNumber;
191 
192 
193  protected:
194  Int_t GetFileEvent();
195  Int_t GetEtEvent();
196 
197  Int_t WriteFileEvent(int* buffer);
198 
199  Bool_t DataFileIsSegmented();
200 
201  Int_t CloseThisSegment();
202  Int_t OpenNextSegment();
203 
204  void DecodeEventIDBank(UInt_t *buffer);
205  Bool_t DecodeSubbankHeader(UInt_t *buffer);
206 
207  const TString& DataFile(const UInt_t run, const Short_t seg);
208 
209  void SetEventLength(const ULong_t tmplength) {fEvtLength = tmplength;};
210  void SetEventType(const UInt_t tmptype) {fEvtType = tmptype;};
211  void SetWordsSoFar(const ULong_t tmpwords) {fWordsSoFar = tmpwords;};
212 
213 
214 
215  protected:
217  THaCodaData *fEvStream; // Pointer to a THaCodaFile or THaEtClient
218 
219  Int_t fCurrentRun;
220 
222 
223 
224  std::vector<Int_t> fRunSegments;
225  std::vector<Int_t>::iterator fRunSegmentIterator;
226 
227 
228  protected:
230 
231  UInt_t fEvtLength;
232  UInt_t fWordsSoFar;
233 
234  UInt_t fEvtType;
235 
236  UInt_t fEvtTag;
238  UInt_t fIDBankNum;
239 
240  UInt_t fEvtNumber; ///< CODA event number; only defined for physics events
241  UInt_t fEvtClass;
242  UInt_t fStatSum;
243 
244  Double_t fCleanParameter[3]; ///< Scan data/clean data from the green monster
245 
246  UInt_t fFragLength;
247  UInt_t fSubbankTag;
248  UInt_t fSubbankType;
249  UInt_t fSubbankNum;
250  UInt_t fROC;
251 
252  TStopwatch fRunTimer; ///< Timer used for runlet processing loop
253  TStopwatch fStopwatch; ///< Timer used for internal timing
254 
255  protected:
258 
259 };
260 
261 template < class T > Bool_t QwEventBuffer::FillObjectWithEventData(T &object){
262  /// Template to fill any object with data from a CODA event.
263  ///
264  /// The classes for which this template can be specialized
265  /// must have the following three methods defined:
266  ///
267  /// - Bool_t T::CanUseThisEventType(const UInt_t event_type);
268  /// - Bool_t T::ClearEventData(const UInt_t event_type);
269  /// - Int_t T::ProcessBuffer(const UInt_t event_type,
270  /// const UInt_t roc_id, const UInt_t bank_id,
271  /// const UInt_t banktype, UInt_t* buffer, UInt_t num_words);
272  ///
273  Bool_t okay = kFALSE;
274  UInt_t *localbuff = (UInt_t*)(fEvStream->getEvBuffer());
275 
276  if (fFragLength==1 && localbuff[fWordsSoFar]==kNullDataWord){
278  } else if (object.CanUseThisEventType(fEvtType)){
279  // Clear the old event information from the object
280  object.ClearEventData(fEvtType);
281  // Loop through the data buffer in this event.
282  if (fBankDataType == 0x10){
283  // This bank is subbanked; loop through subbanks
284  while ((okay = DecodeSubbankHeader(&localbuff[fWordsSoFar]))){
285  // If this bank has further subbanks, restart the loop.
286  if (fSubbankType == 0x10) continue;
287  // If this bank only contains the word 'NULL' then skip
288  // this bank.
289  if (fFragLength==1 && localbuff[fWordsSoFar]==kNullDataWord){
290  fWordsSoFar += fFragLength;
291  continue;
292  }
293  object.ProcessBuffer(fEvtType, fROC, fSubbankTag, fSubbankType,
294  &localbuff[fWordsSoFar],
295  fFragLength);
296  fWordsSoFar += fFragLength;
297  }
298  } else {
299  // This is a single bank of some type
300  object.ProcessBuffer(fEvtType, 0, fBankDataType,
301  &localbuff[fWordsSoFar],
302  fEvtLength);
303  }
304  }
305  return okay;
306 }
307 
308 
309 
310 #endif
void SetWordsSoFar(const ULong_t tmpwords)
enum QwEventBuffer::CodaStreamMode fEvStreamMode
std::vector< UInt_t > fEventList
QwParameterFile * fEventListFile
TString fDataFile
Bool_t DataFileIsSegmented()
Bool_t DecodeSubbankHeader(UInt_t *buffer)
Bool_t GetNextRunRange()
Read the next requested run range, return true if success.
static void SetDefaultDataFileStem(const std::string &stem)
Definition: QwEventBuffer.h:36
Bool_t FillSubsystemConfigurationData(QwSubsystemArray &subsystems)
Int_t EncodePauseEvent()
TString fETStationName
UInt_t fSubbankType
Int_t GetEtEvent()
Int_t GetSegmentNumber() const
Return CODA file segment number.
Definition: QwEventBuffer.h:82
Int_t CloseThisSegment()
TString fDataDirectory
Int_t ReOpenStream()
UInt_t fWordsSoFar
An options class.
Definition: QwOptions.h:133
void SetEventType(const UInt_t tmptype)
std::vector< Int_t > fRunSegments
void ProcessOptions(QwOptions &options)
Sets internal flags based on the QwOptions.
Int_t WriteFileEvent(int *buffer)
UInt_t fStartingPhysicsEvent
Int_t EncodeEndEvent()
Int_t OpenNextStream()
Opens the event stream (file or ET) based on the internal flags.
UInt_t fSubbankNum
Int_t CloseStream()
Closes a currently open event stream.
std::pair< Int_t, Int_t > fRunRange
Int_t GetFileEvent()
void ClearEventData(std::vector< VQwSubsystem * > &subsystems)
Int_t OpenETStream(TString computer, TString session, int mode, const TString stationname="")
UInt_t fSubbankTag
static void SetDefaultDataFileExtension(const std::string &extension)
Definition: QwEventBuffer.h:39
const TString & GetDataFile() const
Definition: QwEventBuffer.h:98
Int_t OpenNextSegment()
Int_t OpenDataFile(UInt_t current_run, Short_t seg)
Bool_t GetNextEventRange()
Read the next requested event range, return true if success.
TString fETSession
Int_t GetEventNumber()
Bool_t FillObjectWithEventData(T &t)
THaCodaData * fEvStream
std::pair< UInt_t, UInt_t > GetEventRange() const
Definition: QwEventBuffer.h:86
std::string fRunListFileName
Int_t GetNextEvent()
std::string fEventListFileName
Int_t EncodePrestartEvent(int runnumber, int runtype=0)
Double_t fCleanParameter[3]
Scan data/clean data from the green monster.
TString fDataFileExtension
Bool_t fRunIsSegmented
static const UInt_t kNullDataWord
Definition: QwEventBuffer.h:48
void DecodeEventIDBank(UInt_t *buffer)
Int_t WriteEvent(int *buffer)
QwParameterFile * fRunListFile
QwEventBuffer()
Default constructor.
Int_t EncodeSubsystemData(QwSubsystemArray &subsystems)
void SetEventLength(const ULong_t tmplength)
Int_t GetRunNumber() const
Return CODA file run number.
Definition: QwEventBuffer.h:80
static std::string fDefaultDataFileStem
std::vector< Int_t >::iterator fRunSegmentIterator
static void DefineOptions(QwOptions &options)
std::pair< Int_t, Int_t > fSegmentRange
Bool_t fChainDataFiles
static const double T
Magnetic field: base unit is T.
Definition: QwUnits.h:111
Bool_t IsEPICSEvent()
The pure virtual base class of all subsystems.
Definition: VQwSubsystem.h:59
void SetDataDirectory(const TString datadir)
Definition: QwEventBuffer.h:95
TStopwatch fRunTimer
Timer used for runlet processing loop.
Int_t EncodeGoEvent()
static const Int_t kNoNextDataFile
Definition: QwEventBuffer.h:45
static const Int_t kFileHandleNotConfigured
Definition: QwEventBuffer.h:46
Bool_t FillSubsystemData(QwSubsystemArray &subsystems)
void PrintRunTimes()
static std::string fDefaultDataFileExtension
virtual ~QwEventBuffer()
Definition: QwEventBuffer.h:54
Int_t CloseDataFile()
Int_t CloseETStream()
UInt_t fNumPhysicsEvents
UInt_t fBankDataType
Bool_t fPhysicsEventFlag
TString GetRunLabel() const
Returns a string like &lt;run#&gt; or &lt;run#&gt;.&lt;file#&gt;
Bool_t FillEPICSData(QwEPICSEvent &epics)
std::vector< Int_t > fRunRangeMaxList
TString fDataFileStem
Bool_t IsPhysicsEvent()
Bool_t AreRunletsSplit() const
Return true if file segments are being separated for.
Definition: QwEventBuffer.h:76
std::pair< UInt_t, UInt_t > fEventRange
UInt_t fEvtNumber
CODA event number; only defined for physics events.
const TString & DataFile(const UInt_t run, const Short_t seg)
TString fETHostname
std::vector< Int_t > fRunRangeMinList
Bool_t GetNextRunNumber()
Get the next run in the active run range, proceed to next range if needed.
static const Int_t kRunNotSegmented
Definition: QwEventBuffer.h:44
TStopwatch fStopwatch
Timer used for internal timing.
Bool_t IsROCConfigurationEvent()
UInt_t fFragLength