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 AliRunDB class provides the interface to the run and file //
37 // catalogues (AliEn or plain MySQL). //
38 // The AliStats class provides statics information that is added as //
39 // a single keyed object to each raw file. //
40 // The AliTagDB provides an interface to a TAG database. //
41 // The AliMDC class is usid by the "alimdc" stand-alone program //
42 // that reads data directly from DATE. //
44 //////////////////////////////////////////////////////////////////////////
46 #include <sys/types.h>
53 #include <TStopwatch.h>
57 #include "libDateEb.h"
63 #include "AliRawEvent.h"
64 #include "AliRawEventHeaderBase.h"
65 #include "AliRawEquipment.h"
66 #include "AliRawEquipmentHeader.h"
67 #include "AliRawData.h"
70 #include "AliRawRFIODB.h"
71 #include "AliRawCastorDB.h"
72 #include "AliRawRootdDB.h"
73 #include "AliRawNullDB.h"
76 #include "AliFilter.h"
84 const char* const AliMDC::fgkFilterName[kNFilters] = {"AliHoughFilter"};
86 //______________________________________________________________________________
87 AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode,
88 const char* localRunDB, Bool_t rdbmsRunDB,
89 const char* alienHostRunDB, const char* alienDirRunDB,
90 Double_t maxSizeTagDB, const char* fileNameTagDB) :
91 fEvent(new AliRawEvent),
95 fRunDB(new AliRunDB(localRunDB, rdbmsRunDB, alienHostRunDB, alienDirRunDB)),
98 fDeleteFiles(deleteFiles),
99 fFilterMode(filterMode),
102 fIsTagDBCreated(kFALSE),
103 fMaxSizeTagDB(maxSizeTagDB),
104 fFileNameTagDB(fileNameTagDB)
106 // Create MDC processor object.
107 // compress is the file compression mode.
108 // If deleteFiles is kTRUE the raw data files will be deleted after they
110 // If the filterMode if kFilterOff no filter algorithm will be, if it is
111 // kFilterTransparent the algorthims will be run but no events will be
112 // rejected, if it is kFilterOn the filters will be run and the event will
113 // be rejected if all filters return kFALSE.
114 // localRunDB is the file name of the local run DB; if NULL no local run DB
116 // The filling of a MySQL run DB can be switch on or off with rdbmsRunDB.
117 // The host and directory name of the alien run DB can be specified by
118 // alienHostRunDB and alienDirRunDB; if NULL no alien DB will be filled.
119 // If maxSizeTagDB is greater than 0 it determines the maximal size of the
120 // tag DB and then fileNameTagDB is the directory name for the tag DB.
121 // Otherwise fileNameTagDB is the file name of the tag DB. If it is NULL
122 // no tag DB will be created.
125 if (fFilterMode != kFilterOff) {
129 // Tag DB is now created at the point where the header version is
131 // if (fileNameTagDB) {
132 // if (maxSizeTagDB > 0) {
133 // fTagDB = new AliTagDB(fEvent->GetHeader(), NULL);
134 // fTagDB->SetMaxSize(maxSizeTagDB);
135 // fTagDB->SetFS(fileNameTagDB);
138 // fTagDB = new AliTagDB(fEvent->GetHeader(), fileNameTagDB);
142 // install SIGUSR1 handler to allow clean interrupts
143 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
145 // create the high level filters
146 if (fFilterMode != kFilterOff) {
147 for (Int_t iFilter = 0; iFilter < kNFilters; iFilter++) {
148 TClass* filterClass = gROOT->GetClass(fgkFilterName[iFilter]);
150 Warning("AliMDC", "no filter class %s found", fgkFilterName[iFilter]);
153 AliFilter* filter = (AliFilter*) filterClass->New();
155 Warning("AliMDC", "creation of filter %s failed", fgkFilterName[iFilter]);
158 fFilters.Add(filter);
163 //______________________________________________________________________________
169 if(fTagDB) delete fTagDB;
177 //______________________________________________________________________________
178 AliMDC::AliMDC(const AliMDC& mdc): TObject(mdc)
182 Fatal("AliMDC", "copy constructor not implemented");
185 //______________________________________________________________________________
186 AliMDC& AliMDC::operator = (const AliMDC& /*mdc*/)
188 // assignment operator
190 Fatal("operator =", "assignment operator not implemented");
195 //______________________________________________________________________________
196 Int_t AliMDC::Open(EWriteMode mode, const char* fileName)
198 // open a new raw DB file
201 fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, fileName);
202 else if (mode == kROOTD)
203 fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, fileName);
204 else if (mode == kCASTOR)
205 fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, fileName);
206 else if (mode == kDEVNULL)
207 fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, fileName);
209 fRawDB = new AliRawDB(fEvent, fESD, fCompress, fileName);
210 fRawDB->SetDeleteFiles(fDeleteFiles);
212 if (fRawDB->IsZombie()) {
217 Info("Open", "Filling raw DB %s\n", fRawDB->GetDBName());
219 // Create AliStats object
220 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
221 fFilterMode != kFilterOff);
225 //______________________________________________________________________________
226 Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
228 // Convert the DATE event to an AliRawEvent object and store it in the raw DB,
229 // optionally also run the filter.
230 // event is either a pointer to the streamlined event
231 // or, if isIovecArray is kTRUE, a pointer to an array of iovecs with one
232 // iovec per subevent (used by the event builder).
233 // The return value is the number of written bytes or an error code
234 const UInt_t kFileSizeErrorLevel = 1900000000;
236 UInt_t currentFileSize = GetTotalSize();
237 if(currentFileSize > kFileSizeErrorLevel) {
238 Error("ProcessEvent", "file size (%u) exceeds the limit "
244 char* data = (char*) event;
245 if (isIovecArray) data = (char*) ((iovec*) event)[0].iov_base;
247 // Shortcut for easy header access
248 AliRawEventHeaderBase *header = fEvent->GetHeader(data);
251 if ((status = header->ReadHeader(data)) != (Int_t)header->GetHeadSize()) {
255 if (AliDebugLevel() > 2) ToAliDebug(3, header->Dump(););
257 // Check event type and skip "Start of Run", "End of Run",
258 // "Start of Run Files" and "End of Run Files"
259 Int_t size = header->GetEventSize() - header->GetHeadSize();
260 switch (header->Get("Type")) {
261 case AliRawEventHeaderBase::kStartOfRun:
262 case AliRawEventHeaderBase::kEndOfRun:
263 case AliRawEventHeaderBase::kStartOfRunFiles:
264 case AliRawEventHeaderBase::kEndOfRunFiles:
266 AliDebug(1, Form("Skipping %s (%d bytes)", header->GetTypeName(), size));
267 return kErrStartEndRun;
271 AliDebug(1, Form("Processing %s (%d bytes)", header->GetTypeName(), size));
275 // Amount of data left to read for this event
278 // If there is less data for this event than the next sub-event
279 // header, something is wrong. Skip to next event...
280 if (toRead < (Int_t)header->GetHeadSize()) {
281 Error("ProcessEvent", "header size (%d) exceeds number of bytes "
282 "to read (%d)", header->GetHeadSize(), toRead);
283 if (AliDebugLevel() > 0) ToAliDebug(1, header->Dump(););
284 return kErrHeaderSize;
287 // Loop over all sub-events... (LDCs)
290 if (isIovecArray) data = (char*) ((iovec*) event)[nsub].iov_base;
292 AliDebug(1, Form("reading LDC %d", nsub));
294 AliRawEvent *subEvent = fEvent->NextSubEvent();
296 // Read sub-event header
297 AliRawEventHeaderBase *subHeader = subEvent->GetHeader(data);
298 if ((status = subHeader->ReadHeader(data)) != (Int_t)subHeader->GetHeadSize()) {
299 return kErrSubHeader;
302 if (AliDebugLevel() > 2) ToAliDebug(3, subHeader->Dump(););
304 toRead -= subHeader->GetHeadSize();
306 Int_t rawSize = subHeader->GetEventSize() - subHeader->GetHeadSize();
308 // Make sure raw data less than left over bytes for current event
309 if (rawSize > toRead) {
310 Warning("ProcessEvent", "raw data size (%d) exceeds number of "
311 "bytes to read (%d)\n", rawSize, toRead);
312 if (AliDebugLevel() > 0) ToAliDebug(1, subHeader->Dump(););
316 // Read Equipment Headers (in case of physics or calibration event)
317 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
318 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent) {
319 while (rawSize > 0) {
320 AliRawEquipment &equipment = *subEvent->NextEquipment();
321 AliRawEquipmentHeader &equipmentHeader =
322 *equipment.GetEquipmentHeader();
323 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
324 if ((status = ReadEquipmentHeader(equipmentHeader, header->DataIsSwapped(),
325 data)) != equipHeaderSize) {
326 return kErrEquipmentHeader;
328 toRead -= equipHeaderSize;
329 rawSize -= equipHeaderSize;
331 // Read equipment raw data
332 AliRawData &subRaw = *equipment.GetRawData();
334 Int_t eqSize = equipmentHeader.GetEquipmentSize() - equipHeaderSize;
335 if ((status = ReadRawData(subRaw, eqSize, data)) != eqSize) {
336 return kErrEquipment;
343 } else { // Read only raw data but no equipment header
344 AliRawEquipment &equipment = *subEvent->NextEquipment();
345 AliRawData &subRaw = *equipment.GetRawData();
346 if ((status = ReadRawData(subRaw, rawSize, data)) != rawSize) {
347 return kErrEquipment;
356 // High Level Event Filter
357 if (fFilterMode != kFilterOff) {
358 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
359 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent) {
360 Bool_t result = kFALSE;
361 for (Int_t iFilter = 0; iFilter < fFilters.GetEntriesFast(); iFilter++) {
362 AliFilter* filter = (AliFilter*) fFilters[iFilter];
363 if (!filter) continue;
364 if (filter->Filter(fEvent, fESD)) result = kTRUE;
366 if ((fFilterMode == kFilterOn) && !result) return kFilterReject;
370 // Set stat info for first event of this file
371 if (fRawDB->GetEvents() == 0)
372 fStats->SetFirstId(header->Get("RunNb"), header->GetP("Id")[0]);
374 // Store raw event in tree
375 Int_t nBytes = fRawDB->Fill();
377 // Create Tag DB here only after the raw data header
378 // version was already identified
379 if (!fIsTagDBCreated) {
380 if (fFileNameTagDB) {
381 if (fMaxSizeTagDB > 0) {
382 fTagDB = new AliTagDB(fEvent->GetHeader(), NULL);
383 fTagDB->SetMaxSize(fMaxSizeTagDB);
384 fTagDB->SetFS(fFileNameTagDB);
387 fTagDB = new AliTagDB(fEvent->GetHeader(), fFileNameTagDB);
390 fIsTagDBCreated = kTRUE;
393 // Store header in tree
394 if (fTagDB) fTagDB->Fill();
396 // Make top event object ready for next event data
398 // Clean up HLT ESD for the next event
399 if (fESD) fESD->Reset();
407 //______________________________________________________________________________
408 Int_t AliMDC::GetTotalSize()
410 // return the total current raw DB file size
412 if (!fRawDB) return -1;
414 return fRawDB->GetTotalSize();
417 //______________________________________________________________________________
418 Int_t AliMDC::Close()
420 // close the current raw DB file
422 if (!fRawDB) return -1;
424 fRawDB->WriteStats(fStats);
425 fRunDB->Update(fStats);
426 Int_t filesize = fRawDB->Close();
434 //______________________________________________________________________________
435 Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
436 EWriteMode mode, Double_t maxFileSize,
437 const char* fs1, const char* fs2)
439 // Run the MDC processor. Read from the input stream and only return
440 // when the input gave and EOF or a fatal error occured. On success 0
441 // is returned, 1 in case of a fatality.
442 // inputFile is the name of the DATE input file; if NULL the input will
443 // be taken from the event builder.
444 // If loop is set the same input file will be reused in an infinite loop.
445 // mode specifies the type of the raw DB.
446 // maxFileSize is the maximal size of the raw DB.
447 // fs1 and fs2 are the file system locations of the raw DB.
449 Info("Run", "input = %s, rawdb size = %f, filter = %s, "
450 "looping = %s, compression = %d, delete files = %s",
451 inputFile ? inputFile : "event builder", maxFileSize,
452 fFilterMode == kFilterOff ? "off" :
453 (fFilterMode == kFilterOn ? "on" : "transparent"),
454 loop ? "yes" : "no", fCompress, fDeleteFiles ? "yes" : "no");
456 // Open the input file
459 if ((fd = open(inputFile, O_RDONLY)) == -1) {
460 Error("Run", "cannot open input file %s", inputFile);
465 // Used for statistics
468 Double_t told = 0, tnew = 0;
469 Float_t chunkSize = maxFileSize/100, nextChunk = chunkSize;
471 // Create new raw DB.
474 fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, NULL);
475 } else if (mode == kROOTD) {
476 fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, NULL);
477 } else if (mode == kCASTOR) {
478 fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, NULL);
479 } else if (mode == kDEVNULL) {
480 fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, NULL);
482 fRawDB = new AliRawDB(fEvent, fESD, fCompress, NULL);
484 fRawDB->SetMaxSize(maxFileSize);
485 fRawDB->SetFS(fs1, fs2);
486 fRawDB->SetDeleteFiles(fDeleteFiles);
489 if (fRawDB->IsZombie()) {
494 printf("Filling raw DB %s\n", fRawDB->GetDBName());
496 // Create AliStats object
497 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
498 fFilterMode != kFilterOff);
500 // Process input stream
505 UInt_t eventSize = 0;
508 AliRawEventHeaderBase header;
512 // If we were in looping mode stop directly after a SIGUSR1 signal
514 Info("Run", "Stopping loop, processed %d events", numEvents);
518 if (!inputFile) { // get data from event builder
520 if ((eorFlag = ebEor())) break;
521 if ((event = (char*)ebGetNextEvent()) == (char*)-1) {
522 Error("Run", "error getting next event (%s)", ebGetLastError());
526 // no event, sleep for 1 second and try again
527 gSystem->Sleep(1000);
531 Error("Run", "AliMDC was compiled without event builder support");
539 } else { // get data from a file
542 if ((nrecv = Read(fd, header.HeaderBaseBegin(), header.HeaderBaseSize())) !=
543 header.HeaderBaseSize()) {
544 if (nrecv == 0) { // eof
546 ::lseek(fd, 0, SEEK_SET);
552 Error("Run", "error reading base header");
559 char *data = (char *)header.HeaderBaseBegin();
560 AliRawEventHeaderBase *hdr = AliRawEventHeaderBase::Create(data);
562 if ((nrecv = Read(fd, hdr->HeaderBegin(), hdr->HeaderSize())) !=
564 if (nrecv == 0) { // eof
566 ::lseek(fd, 0, SEEK_SET);
574 Error("Run", "error reading header");
581 if (eventSize < hdr->GetEventSize()) {
583 eventSize = 2 * hdr->GetEventSize();
584 event = new char[eventSize];
586 memcpy(event, hdr->HeaderBaseBegin(), hdr->HeaderBaseSize());
587 memcpy(event+hdr->HeaderBaseSize(), hdr->HeaderBegin(), hdr->HeaderSize());
588 if (hdr->GetExtendedDataSize() != 0)
589 memcpy(event+hdr->HeaderBaseSize()+hdr->HeaderSize(),
590 hdr->GetExtendedData(), hdr->GetExtendedDataSize());
591 Int_t size = hdr->GetEventSize() - hdr->GetHeadSize();
592 if (Read(fd, event + hdr->GetHeadSize(), size) != size) {
593 Error("Run", "error reading data");
602 Int_t result = ProcessEvent(event, !inputFile);
604 Error("Run", "error writing data. Error code: %d",result);
609 printf("Processed event %d (%d)\n", numEvents, fRawDB->GetEvents());
613 // Filling time statistics
614 if (fRawDB->GetBytesWritten() > nextChunk) {
615 tnew = timer.RealTime();
616 fStats->Fill(tnew-told);
619 nextChunk += chunkSize;
622 // Check size of raw db. If bigger than maxFileSize, close file
623 // and continue with new file.
624 if (fRawDB->GetBytesWritten() > maxFileSize) {
626 printf("Written raw DB at a rate of %.1f MB/s\n",
627 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
629 // Write stats object to raw db, run db, MySQL and AliEn
630 fRawDB->WriteStats(fStats);
631 if (fRunDB) fRunDB->Update(fStats);
635 if (!fRawDB->NextFile()) {
636 Error("Run", "error opening next raw data file");
638 if (inputFile) delete[] event;
642 printf("Filling raw DB %s\n", fRawDB->GetDBName());
643 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
644 fFilterMode != kFilterOff);
648 nextChunk = chunkSize;
651 // Check size of tag db
652 if (fTagDB && fTagDB->FileFull()) {
653 if (!fTagDB->NextFile()) {
657 printf("Filling tag DB %s\n", fTagDB->GetDBName());
662 // Make top event object ready for next event data
663 //printf("Event %d has %d sub-events\n", numEvents, fEvent->GetNSubEvents());
665 // Clean up HLT ESD for the next event
666 // if (fESD) fESD->Reset();
670 if (!ebReleaseEvent((iovec*)event)) {
671 Error("Run", "problem releasing event (%s)", ebGetLastError());
678 printf("Written raw DB at a rate of %.1f MB/s\n",
679 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
681 // Write stats to raw db and run db and delete stats object
688 Info("Run", "event builder reported end of run (%d)", eorFlag);
692 // Close input source
700 //______________________________________________________________________________
701 Int_t AliMDC::Read(Int_t fd, void *buffer, Int_t length)
703 // Read exactly length bytes into buffer. Returns number of bytes
704 // received, returns -1 in case of error and 0 for EOF.
708 if (fd < 0) return -1;
711 char *buf = (char *)buffer;
713 for (n = 0; n < length; n += nrecv) {
714 if ((nrecv = read(fd, buf+n, length-n)) <= 0) {
718 SysError("Read", "read");
725 //______________________________________________________________________________
726 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
727 Bool_t isSwapped, char*& data)
729 // Read equipment header info from DATE data stream. Returns bytes read
730 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
731 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
732 // and we will swap the header to host format.
734 memcpy(header.HeaderBegin(), data, header.HeaderSize());
735 data += header.HeaderSize();
737 // Swap equipment header data if needed
741 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
742 Error("ReadEquipmentHeader", "invalid equipment header size");
743 // try recovery... how?
747 return header.HeaderSize();
750 //______________________________________________________________________________
751 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, char*& data)
753 // Read raw data from DATE data stream. Returns bytes read (i.e.
754 // size), -1 in case of error and 0 for EOF.
756 raw.SetBuffer(data, size);
762 //______________________________________________________________________________
765 // Stop the event loop
768 if (fRawDB) fRawDB->Stop();
772 //______________________________________________________________________________
773 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const
774 AliMDCInterruptHandler&
776 TSignalHandler(handler)
780 Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
783 //______________________________________________________________________________
784 AliMDC::AliMDCInterruptHandler&
785 AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler&
788 // assignment operator
790 Fatal("operator =", "assignment operator not implemented");