1 // @(#)alimdc:$Name$:$Id$
2 // Author: Fons Rademakers 26/11/99
3 // Updated: Dario Favretto 15/04/2003
5 /**************************************************************************
6 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
8 * Author: The ALICE Off-line Project. *
9 * Contributors are mentioned in the code where appropriate. *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
22 //////////////////////////////////////////////////////////////////////////
26 // Set of classes defining the ALICE RAW event format. The AliRawEvent //
27 // class defines a RAW event. It consists of an AliEventHeader object //
28 // an AliEquipmentHeader object, an AliRawData object and an array of //
29 // sub-events, themselves also being AliRawEvents. The number of //
30 // sub-events depends on the number of DATE LDC's. //
31 // The AliRawEvent objects are written to a ROOT file using different //
32 // technologies, i.e. to local disk via AliRawDB or via rfiod using //
33 // AliRawRFIODB or via rootd using AliRawRootdDB or to CASTOR via //
34 // rootd using AliRawCastorDB (and for performance testing there is //
35 // also AliRawNullDB). //
36 // The AliStats class provides statics information that is added as //
37 // a single keyed object to each raw file. //
38 // The AliTagDB provides an interface to a TAG database. //
39 // The AliMDC class is usid by the "alimdc" stand-alone program //
40 // that reads data directly from DATE. //
42 //////////////////////////////////////////////////////////////////////////
44 #include <sys/types.h>
51 #include <TStopwatch.h>
55 #include "libDateEb.h"
61 #include "AliRawEvent.h"
62 #include "AliRawEventHeaderBase.h"
63 #include "AliRawEquipment.h"
64 #include "AliRawEquipmentHeader.h"
65 #include "AliRawData.h"
68 #include "AliRawRFIODB.h"
69 #include "AliRawCastorDB.h"
70 #include "AliRawRootdDB.h"
71 #include "AliRawNullDB.h"
73 #include "AliRawEventTag.h"
74 #include "AliFilter.h"
82 const char* const AliMDC::fgkFilterName[kNFilters] = {"AliHoughFilter"};
84 //______________________________________________________________________________
85 AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode,
86 Double_t maxSizeTagDB, const char* fileNameTagDB) :
87 fEvent(new AliRawEvent),
92 fEventTag(new AliRawEventTag),
94 fDeleteFiles(deleteFiles),
95 fFilterMode(filterMode),
98 fIsTagDBCreated(kFALSE),
99 fMaxSizeTagDB(maxSizeTagDB),
100 fFileNameTagDB(fileNameTagDB)
102 // Create MDC processor object.
103 // compress is the file compression mode.
104 // If deleteFiles is kTRUE the raw data files will be deleted after they
106 // If the filterMode if kFilterOff no filter algorithm will be, if it is
107 // kFilterTransparent the algorthims will be run but no events will be
108 // rejected, if it is kFilterOn the filters will be run and the event will
109 // be rejected if all filters return kFALSE.
110 // If maxSizeTagDB is greater than 0 it determines the maximal size of the
111 // tag DB and then fileNameTagDB is the directory name for the tag DB.
112 // Otherwise fileNameTagDB is the file name of the tag DB. If it is NULL
113 // no tag DB will be created.
116 if (fFilterMode != kFilterOff) {
120 // Tag DB is now created at the point where the header version is
122 // if (fileNameTagDB) {
123 // if (maxSizeTagDB > 0) {
124 // fTagDB = new AliTagDB(fEvent->GetHeader(), NULL);
125 // fTagDB->SetMaxSize(maxSizeTagDB);
126 // fTagDB->SetFS(fileNameTagDB);
129 // fTagDB = new AliTagDB(fEvent->GetHeader(), fileNameTagDB);
133 // install SIGUSR1 handler to allow clean interrupts
134 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
136 // create the high level filters
137 if (fFilterMode != kFilterOff) {
138 for (Int_t iFilter = 0; iFilter < kNFilters; iFilter++) {
139 TClass* filterClass = gROOT->GetClass(fgkFilterName[iFilter]);
141 Warning("AliMDC", "no filter class %s found", fgkFilterName[iFilter]);
144 AliFilter* filter = (AliFilter*) filterClass->New();
146 Warning("AliMDC", "creation of filter %s failed", fgkFilterName[iFilter]);
149 fFilters.Add(filter);
154 //______________________________________________________________________________
160 if(fTagDB) delete fTagDB;
168 //______________________________________________________________________________
169 Int_t AliMDC::Open(EWriteMode mode, const char* fileName,
170 Double_t maxFileSize,
171 const char* fs1, const char* fs2)
173 // open a new raw DB file
176 fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, fileName);
177 else if (mode == kROOTD)
178 fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, fileName);
179 else if (mode == kCASTOR)
180 fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, fileName);
181 else if (mode == kDEVNULL)
182 fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, fileName);
184 fRawDB = new AliRawDB(fEvent, fESD, fCompress, fileName);
185 fRawDB->SetDeleteFiles(fDeleteFiles);
187 if (fileName == NULL) {
188 fRawDB->SetMaxSize(maxFileSize);
189 fRawDB->SetFS(fs1, fs2);
193 if (fRawDB->IsZombie()) {
198 Info("Open", "Filling raw DB %s\n", fRawDB->GetDBName());
200 // Create AliStats object
201 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
202 fFilterMode != kFilterOff);
206 //______________________________________________________________________________
207 Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
209 // Convert the DATE event to an AliRawEvent object and store it in the raw DB,
210 // optionally also run the filter.
211 // event is either a pointer to the streamlined event
212 // or, if isIovecArray is kTRUE, a pointer to an array of iovecs with one
213 // iovec per subevent (used by the event builder).
214 // The return value is the number of written bytes or an error code
215 const UInt_t kFileSizeErrorLevel = 1900000000;
217 UInt_t currentFileSize = GetTotalSize();
218 if(currentFileSize > kFileSizeErrorLevel) {
219 Error("ProcessEvent", "file size (%u) exceeds the limit "
225 char* data = (char*) event;
226 if (isIovecArray) data = (char*) ((iovec*) event)[0].iov_base;
228 // Shortcut for easy header access
229 AliRawEventHeaderBase *header = fEvent->GetHeader(data);
232 if ((status = header->ReadHeader(data)) != (Int_t)header->GetHeadSize()) {
236 if (AliDebugLevel() > 2) ToAliDebug(3, header->Dump(););
238 // Check event type and skip "Start of Run", "End of Run",
239 // "Start of Run Files" and "End of Run Files"
240 Int_t size = header->GetEventSize() - header->GetHeadSize();
242 AliDebug(1, Form("Processing %s (%d bytes)", header->GetTypeName(), size));
244 // Amount of data left to read for this event
247 // StartOfRun, EndOfRun etc. events have no payload
248 // Nevertheless, store the event headers in the tree
251 // If there is less data for this event than the next sub-event
252 // header, something is wrong. Skip to next event...
253 if (toRead < (Int_t)header->GetHeadSize()) {
254 Error("ProcessEvent", "header size (%d) exceeds number of bytes "
255 "to read (%d)", header->GetHeadSize(), toRead);
256 if (AliDebugLevel() > 0) ToAliDebug(1, header->Dump(););
257 return kErrHeaderSize;
260 // Loop over all sub-events... (LDCs)
263 if (isIovecArray) data = (char*) ((iovec*) event)[nsub].iov_base;
265 AliDebug(1, Form("reading LDC %d", nsub));
267 AliRawEvent *subEvent = fEvent->NextSubEvent();
269 // Read sub-event header
270 AliRawEventHeaderBase *subHeader = subEvent->GetHeader(data);
271 if ((status = subHeader->ReadHeader(data)) != (Int_t)subHeader->GetHeadSize()) {
272 return kErrSubHeader;
275 if (AliDebugLevel() > 2) ToAliDebug(3, subHeader->Dump(););
277 toRead -= subHeader->GetHeadSize();
279 Int_t rawSize = subHeader->GetEventSize() - subHeader->GetHeadSize();
281 // Make sure raw data less than left over bytes for current event
282 if (rawSize > toRead) {
283 Warning("ProcessEvent", "raw data size (%d) exceeds number of "
284 "bytes to read (%d)\n", rawSize, toRead);
285 if (AliDebugLevel() > 0) ToAliDebug(1, subHeader->Dump(););
289 // Read Equipment Headers (in case of physics or calibration event)
290 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
291 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
292 header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
293 header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent) {
294 while (rawSize > 0) {
295 AliRawEquipment &equipment = *subEvent->NextEquipment();
296 AliRawEquipmentHeader &equipmentHeader =
297 *equipment.GetEquipmentHeader();
298 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
299 if ((status = ReadEquipmentHeader(equipmentHeader, header->DataIsSwapped(),
300 data)) != equipHeaderSize) {
301 return kErrEquipmentHeader;
304 if (AliDebugLevel() > 2) ToAliDebug(3, equipmentHeader.Dump(););
306 toRead -= equipHeaderSize;
307 rawSize -= equipHeaderSize;
309 // Read equipment raw data
310 AliRawData &subRaw = *equipment.GetRawData();
312 Int_t eqSize = equipmentHeader.GetEquipmentSize() - equipHeaderSize;
313 if ((status = ReadRawData(subRaw, eqSize, data)) != eqSize) {
314 return kErrEquipment;
321 } else { // Read only raw data but no equipment header
323 AliRawEquipment &equipment = *subEvent->NextEquipment();
324 AliRawData &subRaw = *equipment.GetRawData();
325 if ((status = ReadRawData(subRaw, rawSize, data)) != rawSize) {
326 return kErrEquipment;
336 // High Level Event Filter
337 if (fFilterMode != kFilterOff) {
338 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
339 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
340 header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
341 header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent) {
342 Bool_t result = kFALSE;
343 for (Int_t iFilter = 0; iFilter < fFilters.GetEntriesFast(); iFilter++) {
344 AliFilter* filter = (AliFilter*) fFilters[iFilter];
345 if (!filter) continue;
346 if (filter->Filter(fEvent, fESD)) result = kTRUE;
348 if ((fFilterMode == kFilterOn) && !result) return kFilterReject;
352 // Set stat info for first event of this file
353 if (fRawDB->GetEvents() == 0)
354 fStats->SetFirstId(header->Get("RunNb"), header->GetP("Id")[0]);
356 // Store raw event in tree
357 Int_t nBytes = fRawDB->Fill();
359 // Fill the event tag object
360 fEventTag->SetHeader(fEvent->GetHeader());
361 fEventTag->SetGUID(fRawDB->GetDB()->GetUUID().AsString());
362 fEventTag->SetEventNumber(fRawDB->GetEvents()-1);
364 // Create Tag DB here only after the raw data header
365 // version was already identified
366 if (!fIsTagDBCreated) {
367 if (fFileNameTagDB) {
368 if (fMaxSizeTagDB > 0) {
369 fTagDB = new AliTagDB(fEventTag, NULL);
370 fTagDB->SetMaxSize(fMaxSizeTagDB);
371 fTagDB->SetFS(fFileNameTagDB);
374 fTagDB = new AliTagDB(fEventTag, fFileNameTagDB);
377 fIsTagDBCreated = kTRUE;
380 // Store event tag in tree
381 if (fTagDB) fTagDB->Fill();
383 // Make top event object ready for next event data
385 // Clean up HLT ESD for the next event
386 if (fESD) fESD->Reset();
394 //______________________________________________________________________________
395 Int_t AliMDC::GetTotalSize()
397 // return the total current raw DB file size
399 if (!fRawDB) return -1;
401 return fRawDB->GetTotalSize();
404 //______________________________________________________________________________
405 Int_t AliMDC::Close()
407 // close the current raw DB file
409 if (!fRawDB) return -1;
411 fRawDB->WriteStats(fStats);
412 Int_t filesize = fRawDB->Close();
420 //______________________________________________________________________________
421 Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
422 EWriteMode mode, Double_t maxFileSize,
423 const char* fs1, const char* fs2)
425 // Run the MDC processor. Read from the input stream and only return
426 // when the input gave and EOF or a fatal error occured. On success 0
427 // is returned, 1 in case of a fatality.
428 // inputFile is the name of the DATE input file; if NULL the input will
429 // be taken from the event builder.
430 // If loop is set the same input file will be reused in an infinite loop.
431 // mode specifies the type of the raw DB.
432 // maxFileSize is the maximal size of the raw DB.
433 // fs1 and fs2 are the file system locations of the raw DB.
435 Info("Run", "input = %s, rawdb size = %f, filter = %s, "
436 "looping = %s, compression = %d, delete files = %s",
437 inputFile ? inputFile : "event builder", maxFileSize,
438 fFilterMode == kFilterOff ? "off" :
439 (fFilterMode == kFilterOn ? "on" : "transparent"),
440 loop ? "yes" : "no", fCompress, fDeleteFiles ? "yes" : "no");
442 // Open the input file
445 if ((fd = open(inputFile, O_RDONLY)) == -1) {
446 Error("Run", "cannot open input file %s", inputFile);
451 // Used for statistics
454 Double_t told = 0, tnew = 0;
455 Float_t chunkSize = maxFileSize/100, nextChunk = chunkSize;
457 // Create new raw DB.
460 if (Open(mode,NULL,maxFileSize,fs1,fs2) < 0) return 1;
462 // Process input stream
467 UInt_t eventSize = 0;
470 AliRawEventHeaderBase header;
474 // If we were in looping mode stop directly after a SIGUSR1 signal
476 Info("Run", "Stopping loop, processed %d events", numEvents);
480 if (!inputFile) { // get data from event builder
482 if ((eorFlag = ebEor())) break;
483 if ((event = (char*)ebGetNextEvent()) == (char*)-1) {
484 Error("Run", "error getting next event (%s)", ebGetLastError());
488 // no event, sleep for 1 second and try again
489 gSystem->Sleep(1000);
493 Error("Run", "AliMDC was compiled without event builder support");
501 } else { // get data from a file
504 if ((nrecv = Read(fd, header.HeaderBaseBegin(), header.HeaderBaseSize())) !=
505 header.HeaderBaseSize()) {
506 if (nrecv == 0) { // eof
508 ::lseek(fd, 0, SEEK_SET);
514 Error("Run", "error reading base header");
521 char *data = (char *)header.HeaderBaseBegin();
522 AliRawEventHeaderBase *hdr = AliRawEventHeaderBase::Create(data);
524 if ((nrecv = Read(fd, hdr->HeaderBegin(), hdr->HeaderSize())) !=
526 if (nrecv == 0) { // eof
528 ::lseek(fd, 0, SEEK_SET);
536 Error("Run", "error reading header");
543 if (eventSize < hdr->GetEventSize()) {
545 eventSize = 2 * hdr->GetEventSize();
546 event = new char[eventSize];
548 memcpy(event, hdr->HeaderBaseBegin(), hdr->HeaderBaseSize());
549 memcpy(event+hdr->HeaderBaseSize(), hdr->HeaderBegin(), hdr->HeaderSize());
550 if (hdr->GetExtendedDataSize() != 0)
551 memcpy(event+hdr->HeaderBaseSize()+hdr->HeaderSize(),
552 hdr->GetExtendedData(), hdr->GetExtendedDataSize());
553 Int_t size = hdr->GetEventSize() - hdr->GetHeadSize();
554 if (Read(fd, event + hdr->GetHeadSize(), size) != size) {
555 Error("Run", "error reading data");
564 Int_t result = ProcessEvent(event, !inputFile);
566 Error("Run", "error writing data. Error code: %d",result);
571 printf("Processed event %d (%d)\n", numEvents, fRawDB->GetEvents());
575 // Filling time statistics
576 if (fRawDB->GetBytesWritten() > nextChunk) {
577 tnew = timer.RealTime();
578 fStats->Fill(tnew-told);
581 nextChunk += chunkSize;
584 // Check size of raw db. If bigger than maxFileSize, close file
585 // and continue with new file.
586 if (fRawDB->GetBytesWritten() > maxFileSize) {
588 printf("Written raw DB at a rate of %.1f MB/s\n",
589 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
591 // Write stats object to raw db, run db, MySQL and AliEn
592 fRawDB->WriteStats(fStats);
596 if (!fRawDB->NextFile()) {
597 Error("Run", "error opening next raw data file");
599 if (inputFile) delete[] event;
603 printf("Filling raw DB %s\n", fRawDB->GetDBName());
604 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
605 fFilterMode != kFilterOff);
609 nextChunk = chunkSize;
612 // Check size of tag db
613 if (fTagDB && fTagDB->FileFull()) {
614 if (!fTagDB->NextFile()) {
618 printf("Filling tag DB %s\n", fTagDB->GetDBName());
623 // Make top event object ready for next event data
624 //printf("Event %d has %d sub-events\n", numEvents, fEvent->GetNSubEvents());
626 // Clean up HLT ESD for the next event
627 // if (fESD) fESD->Reset();
631 if (!ebReleaseEvent((iovec*)event)) {
632 Error("Run", "problem releasing event (%s)", ebGetLastError());
639 printf("Written raw DB at a rate of %.1f MB/s\n",
640 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
642 // Write stats to raw db and run db and delete stats object
649 Info("Run", "event builder reported end of run (%d)", eorFlag);
653 // Close input source
661 //______________________________________________________________________________
662 Int_t AliMDC::Read(Int_t fd, void *buffer, Int_t length)
664 // Read exactly length bytes into buffer. Returns number of bytes
665 // received, returns -1 in case of error and 0 for EOF.
669 if (fd < 0) return -1;
672 char *buf = (char *)buffer;
674 for (n = 0; n < length; n += nrecv) {
675 if ((nrecv = read(fd, buf+n, length-n)) <= 0) {
679 SysError("Read", "read");
686 //______________________________________________________________________________
687 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
688 Bool_t isSwapped, char*& data)
690 // Read equipment header info from DATE data stream. Returns bytes read
691 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
692 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
693 // and we will swap the header to host format.
695 memcpy(header.HeaderBegin(), data, header.HeaderSize());
696 data += header.HeaderSize();
698 // Swap equipment header data if needed
702 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
703 Error("ReadEquipmentHeader", "invalid equipment header size");
704 // try recovery... how?
708 return header.HeaderSize();
711 //______________________________________________________________________________
712 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, char*& data)
714 // Read raw data from DATE data stream. Returns bytes read (i.e.
715 // size), -1 in case of error and 0 for EOF.
717 raw.SetBuffer(data, size);
723 //______________________________________________________________________________
726 // Stop the event loop
729 if (fRawDB) fRawDB->Stop();