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