]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawEvent.h
AliRawEvent classes used in the data challenges and stand-alone alimdc
[u/mrichter/AliRoot.git] / RAW / AliRawEvent.h
1 // @(#)alimdc:$Name$:$Id$
2 // Author: Fons Rademakers  26/11/99
3 // Updated: Dario Favretto  15/04/2003
4
5 #ifndef ALIRAWEVENT_H
6 #define ALIRAWEVENT_H
7
8 #ifndef ROOT_TObject
9 #include <TObject.h>
10 #endif
11
12 #ifndef ROOT_Bytes
13 #include <Bytes.h>
14 #endif
15
16 #ifndef ROOT_TDatime
17 #include <TDatime.h>
18 #endif
19
20 #ifndef ROOT_TString
21 #include <TString.h>
22 #endif
23
24 #ifndef ROOT_TFile
25 #include <TFile.h>
26 #endif
27
28 #ifndef ROOT_TTree
29 #include <TTree.h>
30 #endif
31
32
33 // Forward class declarations
34 class TH1F;
35 class AliRawDB;
36
37
38 // The following enumeration can be used once the kEventTypeMask has been
39 // applied to the raw event type
40 enum EAliRawEventType {
41    kStartOfRun =       1,    // START_OF_RUN
42    kEndOfRun =         2,    // END_OF_RUN
43    kStartOfRunFiles =  3,    // START_OF_RUN_FILES
44    kEndOfRunFiles =    4,    // END_OF_RUN_FILES
45    kStartOfBurst =     5,    // START_OF_BURST
46    kEndOfBurst =       6,    // END_OF_BURST
47    kPhysicsEvent =     7,    // PHYSICS_EVENT
48    kCalibrationEvent = 8,    // CALIBRATION_EVENT
49    kFormatError =      9     // EVENT_FORMAT_ERROR
50 };
51
52 const Int_t kEventTypeMin = kStartOfRun;
53 const Int_t kEventTypeMax = kFormatError;
54
55 const UInt_t kEventMagicNumber        = 0xDA1E5AFE;
56 const UInt_t kEventMagicNumberSwapped = 0xFE5A1EDA;
57
58 // Type sizes
59 const Int_t kIdWords        = 2;
60 const Int_t kTriggerWords   = 2;
61 const Int_t kDetectorWords  = 1;
62 const Int_t kAttributeWords = 3;
63
64
65 class AliRawEventHeader : public TObject {
66
67 private:
68    UInt_t fSize;          // size of event in bytes
69    UInt_t fMagic;         // magic number used for consistency check
70    UInt_t fHeadLen;       // size of header in bytes
71    UInt_t fVersion;       // unique version identifier
72    UInt_t fType;          // event type
73    UInt_t fRunNb;         // run number
74    UInt_t fId[kIdWords];  // id field
75    UInt_t fTriggerPattern[kTriggerWords];   // trigger pattern
76    UInt_t fDetectorPattern[kDetectorWords]; // detector pattern
77    UInt_t fTypeAttribute[kAttributeWords];  // system (0,1) and user (2) attributes
78    UInt_t fLDCId;         // LDC id
79    UInt_t fGDCId;         // GDC id
80
81 public:
82    AliRawEventHeader() { fSize = 0; }
83    virtual ~AliRawEventHeader() { }
84
85    void         *HeaderBegin() { return (void *) &fSize; }
86    Int_t         HeaderSize() const { return (Long_t) &fGDCId - (Long_t) &fSize + sizeof(fGDCId); }
87    Bool_t        DataIsSwapped() const;
88    Bool_t        IsSwapped() const { return (fMagic == kEventMagicNumberSwapped) ? kTRUE : kFALSE; }
89    Bool_t        IsValid() const { return IsSwapped() ? kTRUE : ((fMagic == kEventMagicNumber) ? kTRUE : kFALSE); }
90    void          Swap();
91
92    UInt_t        GetEventSize() const { return fSize; }
93    UInt_t        GetMagic() const { return fMagic; }
94    UInt_t        GetHeaderLength() const { return fHeadLen; }
95    UInt_t        GetVersion() const { return fVersion; }
96    UInt_t        GetType() const { return fType; }
97    const char   *GetTypeName() const;
98    UInt_t        GetRunNumber() const { return fRunNb; }
99    UInt_t        GetEventInRun() const;
100    const UInt_t *GetId() const { return fId; }
101    const UInt_t *GetTriggerPattern() const { return fTriggerPattern; }
102    const UInt_t *GetDetectorPattern() const { return fDetectorPattern; }
103    const UInt_t *GetTypeAttribute() const { return fTypeAttribute; }
104    UInt_t        GetLDCId() const { return fLDCId; }
105    UInt_t        GetGDCId() const { return fGDCId; }
106
107    ClassDef(AliRawEventHeader,1)  // Alice raw event header
108 };
109
110
111 class AliRawEquipmentHeader : public TObject {
112
113 private:
114    UInt_t fSize;                            // number of raw data bytes
115    UInt_t fEquipmentType;                   // equipment type
116    UInt_t fEquipmentID;                     // equipment ID
117    UInt_t fTypeAttribute[kAttributeWords];  // system (0,1) and user (2) attributes
118    UInt_t fBasicElementSizeType;            // basic element size type
119
120 public:
121    AliRawEquipmentHeader() { fSize = 0; }
122    ~AliRawEquipmentHeader() { }
123
124    void         *HeaderBegin() { return (void *) &fSize; }
125    Int_t         HeaderSize() const { return (Long_t) &fBasicElementSizeType - (Long_t) &fSize + sizeof(fBasicElementSizeType); }
126    void          Swap();
127
128    UInt_t        GetEquipmentSize() const { return fSize; }
129    UInt_t        GetEquipmentType() const { return fEquipmentType; }
130    UInt_t        GetId() const { return fEquipmentID; }
131    const UInt_t *GetTypeAttribute() const { return fTypeAttribute; }
132    UInt_t        GetBasicSizeType() const { return fBasicElementSizeType; }
133
134    ClassDef(AliRawEquipmentHeader,1) //Alice equipment header
135 };
136
137
138 class AliRawData : public TObject {
139
140 private:
141    Int_t   fSize;         // number of raw data bytes
142    Int_t   fBufSize;      //!actual size of fRawData
143    char   *fRawData;      //[fSize] raw event data
144    Bool_t  fOwner;        //!if true object owns fRawData buffer
145
146 public:
147    AliRawData() { fSize = fBufSize = 0; fRawData = 0; fOwner = kFALSE; }
148    virtual ~AliRawData() { if (fOwner) delete [] fRawData; }
149
150    void  SetSize(Int_t size) {
151       if (size > fBufSize) {
152          if (fOwner) delete [] fRawData;
153          fRawData = new char [size];
154          fBufSize = size;
155          fOwner   = kTRUE;
156       }
157       fSize = size;
158    }
159    void  SetBuffer(void *buf, Int_t size) {
160       if (fOwner) delete [] fRawData;
161       fRawData = (char *) buf;
162       fBufSize = size;
163       fSize    = size;
164       fOwner   = kFALSE;
165    }
166    Int_t    GetSize() const { return fSize; }
167    void    *GetBuffer() { return fRawData; }
168
169    ClassDef(AliRawData,1)  // Alice raw event buffer
170 };
171
172
173 class AliRawEvent : public TObject {
174
175 private:
176    Int_t                  fNSubEvents;  // number of valid sub-events
177    AliRawEventHeader     *fEvtHdr;      // event header object
178    AliRawEquipmentHeader *fEqpHdr;      // equipment header
179    AliRawData            *fRawData;     // raw data container
180    TObjArray             *fSubEvents;   // sub AliRawEvent's
181
182 public:
183    AliRawEvent();
184    virtual ~AliRawEvent();
185
186    AliRawEventHeader     *GetHeader() const { return fEvtHdr; }
187    AliRawEquipmentHeader *GetEquipmentHeader();
188    AliRawData            *GetRawData();
189    Int_t                  GetNSubEvents() const { return fNSubEvents; }
190    AliRawEvent           *NextSubEvent();
191    void                   Reset();
192
193    ClassDef(AliRawEvent,1)  // ALICE raw event object
194 };
195
196
197 class AliStats : public TObject {
198
199 private:
200    Int_t    fEvents;     // number of events in this file
201    Int_t    fFirstRun;   // run number of first event in file
202    Int_t    fFirstEvent; // event number of first event in file
203    Int_t    fLastRun;    // run number of last event in file
204    Int_t    fLastEvent;  // event number of last event in file
205    TDatime  fBegin;      // begin of filling time
206    TDatime  fEnd;        // end of filling time
207    TString  fFileName;   // name of file containing this data
208    Double_t fFileSize;   // size of file
209    Float_t  fCompFactor; // tree compression factor
210    Int_t    fCompMode;   // compression mode
211    Bool_t   fFilter;     // 3rd level filter on/off
212    TH1F    *fRTHist;     // histogram of real-time to process chunck of data
213    Float_t  fChunk;      //!chunk to be histogrammed
214
215 public:
216    AliStats(const char *filename = "", Int_t compmode = 0, Bool_t filter = kFALSE);
217    virtual ~AliStats();
218    AliStats &operator=(const AliStats &rhs);
219
220    void SetEvents(Int_t events) { fEvents = events; }
221    void SetFirstId(Int_t run, Int_t event) { fFirstRun = run; fFirstEvent = event; }
222    void SetLastId(Int_t run, Int_t event) { fLastRun = run; fLastEvent = event; }
223    void SetBeginTime() { fBegin.Set(); }
224    void SetEndTime() { fEnd.Set(); }
225    void SetFileSize(Double_t size) { fFileSize = size; }
226    void SetCompressionFactor(Float_t comp) { fCompFactor = comp; }
227    void Fill(Float_t time);
228    void WriteToDB(AliRawDB *rawdb);
229
230    Int_t       GetEvents() const { return fEvents; }
231    Int_t       GetFirstRun() const { return fFirstRun; }
232    Int_t       GetFirstEvent() const { return fFirstEvent; }
233    Int_t       GetLastRun() const { return fLastRun; }
234    Int_t       GetLastEvent() const { return fLastEvent; }
235    TDatime    &GetBeginTime() { return fBegin; }
236    TDatime    &GetEndTime() { return fEnd; }
237    Double_t    GetFileSize() const { return fFileSize; }
238    Int_t       GetCompressionMode() const { return fCompMode; }
239    Float_t     GetCompressionFactor() const { return fCompFactor; }
240    Bool_t      GetFilterState() const { return fFilter; }
241    const char *GetFileName() const { return fFileName; }
242    TH1F       *GetRTHist() const { return fRTHist; }
243
244    ClassDef(AliStats,1)  // Statistics object
245 };
246
247
248 class AliRawDB : public TObject {
249
250 protected:
251    TFile         *fRawDB;         // DB to store raw data
252    TTree         *fTree;          // tree used to store raw data
253    AliRawEvent   *fEvent;         // AliRawEvent via which data is stored
254    Int_t          fCompress;      // compression mode (1 default)
255    Double_t       fMaxSize;       // maximum size in bytes of the raw DB
256
257    virtual const char *GetFileName();
258    virtual Bool_t      FSHasSpace(const char *fs);
259    virtual void        MakeTree();
260
261 public:
262    AliRawDB(AliRawEvent *event, Double_t maxsize, Int_t compress,
263             Bool_t create = kTRUE);
264    ~AliRawDB() { Close(); }
265
266    virtual Bool_t Create();
267    virtual void   Close();
268    void           Fill() { fTree->Fill(); }
269    Bool_t         FileFull() { return (fRawDB->GetBytesWritten() > fMaxSize) ?
270                                kTRUE : kFALSE; }
271
272    Bool_t      NextFile();
273
274    Double_t     GetBytesWritten() const { return fRawDB->GetBytesWritten(); }
275    TFile       *GetDB() const { return fRawDB; }
276    const char  *GetDBName() const { return fRawDB->GetName(); }
277    Int_t        GetEvents() const { return (Int_t) fTree->GetEntries(); }
278    AliRawEvent *GetEvent() const { return fEvent; }
279    Float_t      GetCompressionFactor() const;
280    Int_t        GetCompressionMode() const { return fRawDB->GetCompressionLevel(); }
281
282    ClassDef(AliRawDB,0)  // Raw DB
283 };
284
285
286 class AliRawRFIODB : public AliRawDB {
287
288 private:
289    const char *GetFileName();
290
291 public:
292    AliRawRFIODB(AliRawEvent *event, Double_t maxsize, Int_t compress);
293    ~AliRawRFIODB() { Close(); }
294
295    void Close();
296
297    ClassDef(AliRawRFIODB,0)  // Raw DB via RFIO
298 };
299
300
301 class AliRawRootdDB : public AliRawDB {
302
303 private:
304    const char *GetFileName();
305
306 public:
307    AliRawRootdDB(AliRawEvent *event, Double_t maxsize, Int_t compress);
308    ~AliRawRootdDB() { Close(); }
309
310    void Close();
311
312    ClassDef(AliRawRootdDB,0)  // Raw DB via rootd
313 };
314
315
316 class AliRawNullDB : public AliRawDB {
317
318 private:
319    const char *GetFileName();
320
321 public:
322    AliRawNullDB(AliRawEvent *event, Double_t maxsize, Int_t compress);
323    ~AliRawNullDB() { Close(); }
324
325    void Close();
326
327    ClassDef(AliRawNullDB,0)  // Raw DB to /dev/null
328 };
329
330
331 class AliTagDB : public TObject {
332
333 protected:
334    TFile             *fTagDB;     // DB to store header information only (tag)
335    TTree             *fTree;      // tree use to store header
336    AliRawEventHeader *fHeader;    // header via which data is stored
337    Double_t           fMaxSize;   // maximum size in bytes of tag DB
338
339    virtual const char *GetFileName();
340
341 public:
342    AliTagDB(AliRawEventHeader *header, Double_t maxsize, Bool_t create = kTRUE);
343    ~AliTagDB() { Close(); }
344
345    Bool_t          Create();
346    virtual void    Close();
347    void            Fill() { fTree->Fill(); }
348    Bool_t          FileFull()
349             { return (fTagDB->GetBytesWritten() > fMaxSize) ? kTRUE : kFALSE; }
350
351    Bool_t          NextFile();
352
353    Double_t           GetBytesWritten() const { return fTagDB->GetBytesWritten(); }
354    TFile             *GetDB() const { return fTagDB; }
355    const char        *GetDBName() const { return fTagDB->GetName(); }
356    AliRawEventHeader *GetHeader() const { return fHeader; }
357    Int_t              GetEvents() const { return (Int_t) fTree->GetEntries(); }
358    Float_t            GetCompressionFactor() const;
359
360    ClassDef(AliTagDB,0)  // Tag DB
361 };
362
363
364 class AliTagNullDB : public AliTagDB {
365
366 private:
367    const char *GetFileName();
368
369 public:
370    AliTagNullDB(AliRawEventHeader *header, Double_t maxsize);
371    ~AliTagNullDB() { Close(); }
372
373    void Close();
374
375    ClassDef(AliTagNullDB,0)   // Tag DB to /dev/null
376 };
377
378
379 class AliRunDB : public TObject {
380
381 private:
382    TFile  *fRunDB;     // run database
383
384 public:
385    AliRunDB();
386    ~AliRunDB() { Close(); }
387
388    void Update(AliStats *stats);
389    void UpdateRDBMS(AliStats *stats);
390    void Close();
391
392    ClassDef(AliRunDB,0)  // Run (bookkeeping) DB
393 };
394
395
396 class AliMDC : public TObject {
397
398 private:
399    Int_t     fFd;          // DATE input stream
400    Int_t     fCompress;    // compression factor used for raw output DB
401    Int_t     fNumEvents;   // number of events processed
402    Int_t     fDebugLevel;  // controls debug print-out
403    Double_t  fMaxFileSize; // maximum size of raw output DB
404    Bool_t    fUseFifo;     // read from fifo, file otherwise
405    Bool_t    fUseEb;       // use event builder API instead of fifo
406    Bool_t    fUseFilter;   // use 3rd level trigger filter
407    Bool_t    fUseRFIO;     // write directly to RFIO file
408    Bool_t    fUseRootd;    // write directly to rootd file
409    Bool_t    fUseDevNull;  // write to /dev/null
410    Bool_t    fUseLoop;     // loop on input source (must be file)
411    Bool_t    fStopLoop;    // break from endless loop (triggered by SIGUSR1)
412
413    static Bool_t fgDeleteFiles;
414
415    Int_t     Read(const char *name) { return TObject::Read(name); }
416    Int_t     Read(void *buffer, Int_t length);
417    Int_t     ReadHeader(AliRawEventHeader &header, void *eb = 0);
418    Int_t     ReadEquipmentHeader(AliRawEquipmentHeader &header,
419                                  Bool_t isSwapped, void *eb = 0);
420    Int_t     ReadRawData(AliRawData &raw, Int_t size, void *eb = 0);
421    Int_t     DumpEvent(Int_t toRead);
422    Int_t     Filter(AliRawData &raw);
423
424 public:
425    AliMDC(Int_t fd, Int_t compress, Double_t maxFileSize, Bool_t useFilter,
426           Bool_t useRFIO, Bool_t useROOTD, Bool_t useDEVNULL,
427           Bool_t useLoop, Bool_t delFiles);
428    ~AliMDC() { }
429
430    Int_t  Run();
431    void   SetStopLoop() { fStopLoop = kTRUE; }
432    Bool_t StopLoop() const { return fStopLoop; }
433
434    void   SetDebugLevel(Int_t level) { fDebugLevel = level; }
435    Bool_t GetDebugLevel() const { return fDebugLevel; }
436
437    static Bool_t DeleteFiles() { return fgDeleteFiles; }
438
439    ClassDef(AliMDC,0)  // MDC processor
440 };
441
442 R__EXTERN AliMDC *gAliMDC;
443
444 #define ALIDEBUG(level) \
445    if (gAliMDC && gAliMDC->GetDebugLevel() >= (level))
446
447 #endif