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>
52 #include <TPluginManager.h>
53 #include <TBufferFile.h>
57 #include "libDateEb.h"
63 #include <AliESDEvent.h>
65 #include "AliRawEvent.h"
66 #include "AliRawEventHeaderBase.h"
67 #include "AliRawEquipment.h"
68 #include "AliRawEquipmentHeader.h"
69 #include "AliRawData.h"
72 #include "AliRawRFIODB.h"
73 #include "AliRawCastorDB.h"
74 #include "AliRawRootdDB.h"
75 #include "AliRawNullDB.h"
77 #include "AliRawEventTag.h"
78 #include "AliFilter.h"
86 const char* const AliMDC::fgkFilterName[kNFilters] = {"AliHoughFilter"};
88 //______________________________________________________________________________
89 AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode,
90 Double_t maxSizeTagDB, const char* fileNameTagDB,
91 const char *guidFileFolder,
93 fEvent(new AliRawEvent),
98 fEventTag(new AliRawEventTag),
100 fBasketSize(basketsize),
101 fDeleteFiles(deleteFiles),
102 fFilterMode(filterMode),
105 fIsTagDBCreated(kFALSE),
106 fMaxSizeTagDB(maxSizeTagDB),
107 fFileNameTagDB(fileNameTagDB),
108 fGuidFileFolder(guidFileFolder)
110 // Create MDC processor object.
111 // compress is the file compression mode.
112 // If deleteFiles is kTRUE the raw data files will be deleted after they
114 // If the filterMode if kFilterOff no filter algorithm will be, if it is
115 // kFilterTransparent the algorthims will be run but no events will be
116 // rejected, if it is kFilterOn the filters will be run and the event will
117 // be rejected if all filters return kFALSE.
118 // If maxSizeTagDB is greater than 0 it determines the maximal size of the
119 // tag DB and then fileNameTagDB is the directory name for the tag DB.
120 // Otherwise fileNameTagDB is the file name of the tag DB. If it is NULL
121 // no tag DB will be created.
122 // Optional 'guidFileFolder' specifies the folder in which *.guid files
123 // will be stored. In case this option is not given, the *.guid files
124 // will be written to the same folder as the raw-data files.
126 // Set the maximum tree size to 19GB
127 // in order to allow big raw data files
128 TTree::SetMaxTreeSize(20000000000LL);
130 TBufferFile::SetGlobalReadParam(5);
132 // This line is needed in case of a stand-alone application w/o
133 // $ROOTSYS/etc/system.rootrc file
134 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
140 if (fFilterMode != kFilterOff) {
141 fESD = new AliESDEvent();
144 // Create the guid files folder if it does not exist
145 if (!fGuidFileFolder.IsNull()) {
146 gSystem->ResetErrno();
147 gSystem->MakeDirectory(fGuidFileFolder.Data());
148 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
149 SysError("AliMDC", "mkdir %s", fGuidFileFolder.Data());
153 // install SIGUSR1 handler to allow clean interrupts
154 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
156 // create the high level filters
157 if (fFilterMode != kFilterOff) {
158 for (Int_t iFilter = 0; iFilter < kNFilters; iFilter++) {
159 TClass* filterClass = gROOT->GetClass(fgkFilterName[iFilter]);
161 Warning("AliMDC", "no filter class %s found", fgkFilterName[iFilter]);
164 AliFilter* filter = (AliFilter*) filterClass->New();
166 Warning("AliMDC", "creation of filter %s failed", fgkFilterName[iFilter]);
169 fFilters.Add(filter);
174 //______________________________________________________________________________
180 if(fTagDB) delete fTagDB;
188 //______________________________________________________________________________
189 Int_t AliMDC::Open(EWriteMode mode, const char* fileName,
190 Double_t maxFileSize,
191 const char* fs1, const char* fs2)
193 // open a new raw DB file
196 fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, fileName, fBasketSize);
197 else if (mode == kROOTD)
198 fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, fileName, fBasketSize);
199 else if (mode == kCASTOR)
200 fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, fileName, fBasketSize);
201 else if (mode == kDEVNULL)
202 fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, fileName, fBasketSize);
204 fRawDB = new AliRawDB(fEvent, fESD, fCompress, fileName, fBasketSize);
205 fRawDB->SetDeleteFiles(fDeleteFiles);
207 if (fRawDB->IsZombie()) {
213 if (fileName == NULL) {
214 fRawDB->SetMaxSize(maxFileSize);
215 fRawDB->SetFS(fs1, fs2);
216 if (!fRawDB->Create()) {
223 if (!fRawDB->WriteGuidFile(fGuidFileFolder)) {
229 Info("Open", "Filling raw DB %s\n", fRawDB->GetDBName());
231 // Create AliStats object
232 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
233 fFilterMode != kFilterOff);
237 //______________________________________________________________________________
238 Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
240 // Convert the DATE event to an AliRawEvent object and store it in the raw DB,
241 // optionally also run the filter.
242 // event is either a pointer to the streamlined event
243 // or, if isIovecArray is kTRUE, a pointer to an array of iovecs with one
244 // iovec per subevent (used by the event builder).
245 // The return value is the number of written bytes or an error code
246 const Long64_t kFileSizeErrorLevel = 19000000000LL;
248 Long64_t currentFileSize = GetTotalSize();
249 AliDebug(1,Form("current file size is %lld bytes",currentFileSize));
250 if(currentFileSize > kFileSizeErrorLevel) {
251 Error("ProcessEvent", "file size (%lu) exceeds the limit "
257 char* data = (char*) event;
258 if (isIovecArray) data = (char*) ((iovec*) event)[0].iov_base;
260 // Shortcut for easy header access
261 AliRawEventHeaderBase *header = fEvent->GetHeader(data);
264 if ((status = header->ReadHeader(data)) != (Int_t)header->GetHeadSize()) {
268 if (AliDebugLevel() > 2) ToAliDebug(3, header->Dump(););
270 // Check event type and skip "Start of Run", "End of Run",
271 // "Start of Run Files" and "End of Run Files"
272 Int_t size = header->GetEventSize() - header->GetHeadSize();
274 AliDebug(1, Form("Processing %s (%d bytes)", header->GetTypeName(), size));
276 // Amount of data left to read for this event
279 // StartOfRun, EndOfRun etc. events have no payload
280 // Nevertheless, store the event headers in the tree
283 // If there is less data for this event than the next sub-event
284 // header, something is wrong. Skip to next event...
285 if (toRead < (Int_t)header->GetHeadSize()) {
286 Error("ProcessEvent", "header size (%d) exceeds number of bytes "
287 "to read (%d)", header->GetHeadSize(), toRead);
288 if (AliDebugLevel() > 0) ToAliDebug(1, header->Dump(););
289 return kErrHeaderSize;
292 // Loop over all sub-events... (LDCs)
295 if (isIovecArray) data = (char*) ((iovec*) event)[nsub].iov_base;
297 AliDebug(1, Form("reading LDC %d", nsub));
299 AliRawEvent *subEvent = fEvent->NextSubEvent();
301 // Read sub-event header
302 AliRawEventHeaderBase *subHeader = subEvent->GetHeader(data);
303 if ((status = subHeader->ReadHeader(data)) != (Int_t)subHeader->GetHeadSize()) {
304 return kErrSubHeader;
307 if (AliDebugLevel() > 2) ToAliDebug(3, subHeader->Dump(););
309 toRead -= subHeader->GetHeadSize();
311 Int_t rawSize = subHeader->GetEventSize() - subHeader->GetHeadSize();
313 // Make sure raw data less than left over bytes for current event
314 if (rawSize > toRead) {
315 Warning("ProcessEvent", "raw data size (%d) exceeds number of "
316 "bytes to read (%d)\n", rawSize, toRead);
317 if (AliDebugLevel() > 0) ToAliDebug(1, subHeader->Dump(););
321 // Read Equipment Headers (in case of physics or calibration event)
322 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
323 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
324 header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
325 header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent ||
326 header->Get("Type") == AliRawEventHeaderBase::kStartOfData ||
327 header->Get("Type") == AliRawEventHeaderBase::kEndOfData) {
328 while (rawSize > 0) {
329 AliRawEquipment &equipment = *subEvent->NextEquipment();
330 AliRawEquipmentHeader &equipmentHeader =
331 *equipment.GetEquipmentHeader();
332 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
333 if ((status = ReadEquipmentHeader(equipmentHeader, header->DataIsSwapped(),
334 data)) != equipHeaderSize) {
335 return kErrEquipmentHeader;
338 if (AliDebugLevel() > 2) ToAliDebug(3, equipmentHeader.Dump(););
340 toRead -= equipHeaderSize;
341 rawSize -= equipHeaderSize;
343 // Read equipment raw data
344 AliRawData &subRaw = *equipment.GetRawData();
346 Int_t eqSize = equipmentHeader.GetEquipmentSize() - equipHeaderSize;
347 if ((status = ReadRawData(subRaw, eqSize, data)) != eqSize) {
348 return kErrEquipment;
355 } else { // Read only raw data but no equipment header
357 AliRawEquipment &equipment = *subEvent->NextEquipment();
358 AliRawData &subRaw = *equipment.GetRawData();
359 if ((status = ReadRawData(subRaw, rawSize, data)) != rawSize) {
360 return kErrEquipment;
370 // High Level Event Filter
371 if (fFilterMode != kFilterOff) {
372 if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
373 header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
374 header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
375 header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent ||
376 header->Get("Type") == AliRawEventHeaderBase::kStartOfData ||
377 header->Get("Type") == AliRawEventHeaderBase::kEndOfData) {
378 Bool_t result = kFALSE;
379 for (Int_t iFilter = 0; iFilter < fFilters.GetEntriesFast(); iFilter++) {
380 AliFilter* filter = (AliFilter*) fFilters[iFilter];
381 if (!filter) continue;
382 if (filter->Filter(fEvent, fESD)) result = kTRUE;
384 if ((fFilterMode == kFilterOn) && !result) return kFilterReject;
388 // Set stat info for first event of this file
389 if (fRawDB->GetEvents() == 0)
390 fStats->SetFirstId(header->Get("RunNb"), header->GetP("Id")[0]);
392 // Store raw event in tree
393 Int_t nBytes = fRawDB->Fill();
395 // Fill the event tag object
396 fEventTag->SetHeader(fEvent->GetHeader());
397 fEventTag->SetGUID(fRawDB->GetDB()->GetUUID().AsString());
398 fEventTag->SetEventNumber(fRawDB->GetEvents()-1);
400 // Create Tag DB here only after the raw data header
401 // version was already identified
402 if (!fIsTagDBCreated) {
403 if (!fFileNameTagDB.IsNull()) {
404 if (fMaxSizeTagDB > 0) {
405 fTagDB = new AliTagDB(fEventTag, NULL);
406 fTagDB->SetMaxSize(fMaxSizeTagDB);
407 fTagDB->SetFS(fFileNameTagDB.Data());
408 if (!fTagDB->Create()) return kErrTagFile;
410 fTagDB = new AliTagDB(fEventTag, fFileNameTagDB.Data());
411 if (fTagDB->IsZombie()) return kErrTagFile;
414 fIsTagDBCreated = kTRUE;
417 // Store event tag in tree
418 if (fTagDB) fTagDB->Fill();
420 // Make top event object ready for next event data
422 // Clean up HLT ESD for the next event
423 if (fESD) fESD->Reset();
431 //______________________________________________________________________________
432 Long64_t AliMDC::GetTotalSize()
434 // return the total current raw DB file size
436 if (!fRawDB) return -1;
438 return fRawDB->GetTotalSize();
441 //______________________________________________________________________________
442 Long64_t AliMDC::Close()
444 // close the current raw DB file
446 if (!fRawDB) return -1;
448 fRawDB->WriteStats(fStats);
449 Long64_t filesize = fRawDB->Close();
457 //______________________________________________________________________________
458 Long64_t AliMDC::AutoSave()
460 // Auto-save the raw-data
461 // and esd (if any) trees
463 if (!fRawDB) return -1;
465 return fRawDB->AutoSave();
468 //______________________________________________________________________________
469 Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
470 EWriteMode mode, Double_t maxFileSize,
471 const char* fs1, const char* fs2)
473 // Run the MDC processor. Read from the input stream and only return
474 // when the input gave and EOF or a fatal error occured. On success 0
475 // is returned, 1 in case of a fatality.
476 // inputFile is the name of the DATE input file; if NULL the input will
477 // be taken from the event builder.
478 // If loop is set the same input file will be reused in an infinite loop.
479 // mode specifies the type of the raw DB.
480 // maxFileSize is the maximal size of the raw DB.
481 // fs1 and fs2 are the file system locations of the raw DB.
483 Info("Run", "input = %s, rawdb size = %f, filter = %s, "
484 "looping = %s, compression = %d, delete files = %s",
485 inputFile ? inputFile : "event builder", maxFileSize,
486 fFilterMode == kFilterOff ? "off" :
487 (fFilterMode == kFilterOn ? "on" : "transparent"),
488 loop ? "yes" : "no", fCompress, fDeleteFiles ? "yes" : "no");
490 // Open the input file
493 if ((fd = open(inputFile, O_RDONLY)) == -1) {
494 Error("Run", "cannot open input file %s", inputFile);
499 // Used for statistics
502 Double_t told = 0, tnew = 0;
503 Float_t chunkSize = maxFileSize/100, nextChunk = chunkSize;
505 // Create new raw DB.
508 if (Open(mode,NULL,maxFileSize,fs1,fs2) < 0) return 1;
510 // Process input stream
515 UInt_t eventSize = 0;
518 AliRawEventHeaderBase header;
522 // If we were in looping mode stop directly after a SIGUSR1 signal
524 Info("Run", "Stopping loop, processed %d events", numEvents);
528 if (!inputFile) { // get data from event builder
530 if ((eorFlag = ebEor())) break;
531 if ((event = (char*)ebGetNextEvent()) == (char*)-1) {
532 Error("Run", "error getting next event (%s)", ebGetLastError());
536 // no event, sleep for 1 second and try again
537 gSystem->Sleep(1000);
541 Error("Run", "AliMDC was compiled without event builder support");
549 } else { // get data from a file
552 if ((nrecv = Read(fd, header.HeaderBaseBegin(), header.HeaderBaseSize())) !=
553 header.HeaderBaseSize()) {
554 if (nrecv == 0) { // eof
556 ::lseek(fd, 0, SEEK_SET);
562 Error("Run", "error reading base header");
569 char *data = (char *)header.HeaderBaseBegin();
570 AliRawEventHeaderBase *hdr = AliRawEventHeaderBase::Create(data);
572 if ((nrecv = Read(fd, hdr->HeaderBegin(), hdr->HeaderSize())) !=
574 if (nrecv == 0) { // eof
576 ::lseek(fd, 0, SEEK_SET);
584 Error("Run", "error reading header");
591 if (eventSize < hdr->GetEventSize()) {
593 eventSize = 2 * hdr->GetEventSize();
594 event = new char[eventSize];
596 memcpy(event, header.HeaderBaseBegin(), header.HeaderBaseSize());
597 memcpy(event+hdr->HeaderBaseSize(), hdr->HeaderBegin(), hdr->HeaderSize());
598 if (hdr->GetExtendedDataSize() != 0)
599 memcpy(event+hdr->HeaderBaseSize()+hdr->HeaderSize(),
600 hdr->GetExtendedData(), hdr->GetExtendedDataSize());
601 Int_t size = hdr->GetEventSize() - hdr->GetHeadSize();
602 if (Read(fd, event + hdr->GetHeadSize(), size) != size) {
603 Error("Run", "error reading data");
612 Int_t result = ProcessEvent(event, !inputFile);
614 Error("Run", "error writing data. Error code: %d",result);
619 printf("Processed event %d (%d)\n", numEvents, fRawDB->GetEvents());
623 // Filling time statistics
624 if (fRawDB->GetBytesWritten() > nextChunk) {
625 tnew = timer.RealTime();
626 fStats->Fill(tnew-told);
629 nextChunk += chunkSize;
632 // Check size of raw db. If bigger than maxFileSize, close file
633 // and continue with new file.
634 if (fRawDB->GetBytesWritten() > maxFileSize) {
636 printf("Written raw DB at a rate of %.1f MB/s\n",
637 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
639 // Write stats object to raw db, run db, MySQL and AliEn
640 fRawDB->WriteStats(fStats);
644 if (!fRawDB->NextFile()) {
645 Error("Run", "error opening next raw data file");
647 if (inputFile) delete[] event;
651 printf("Filling raw DB %s\n", fRawDB->GetDBName());
652 fStats = new AliStats(fRawDB->GetDBName(), fCompress,
653 fFilterMode != kFilterOff);
657 nextChunk = chunkSize;
660 // Check size of tag db
661 if (fTagDB && fTagDB->FileFull()) {
662 if (!fTagDB->NextFile()) {
666 printf("Filling tag DB %s\n", fTagDB->GetDBName());
671 // Make top event object ready for next event data
672 //printf("Event %d has %d sub-events\n", numEvents, fEvent->GetNSubEvents());
674 // Clean up HLT ESD for the next event
675 // if (fESD) fESD->Reset();
679 if (!ebReleaseEvent((iovec*)event)) {
680 Error("Run", "problem releasing event (%s)", ebGetLastError());
687 printf("Written raw DB at a rate of %.1f MB/s\n",
688 fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
690 // Write stats to raw db and run db and delete stats object
697 Info("Run", "event builder reported end of run (%d)", eorFlag);
701 // Close input source
709 //______________________________________________________________________________
710 Int_t AliMDC::Read(Int_t fd, void *buffer, Int_t length)
712 // Read exactly length bytes into buffer. Returns number of bytes
713 // received, returns -1 in case of error and 0 for EOF.
717 if (fd < 0) return -1;
720 char *buf = (char *)buffer;
722 for (n = 0; n < length; n += nrecv) {
723 if ((nrecv = read(fd, buf+n, length-n)) <= 0) {
727 SysError("Read", "read");
734 //______________________________________________________________________________
735 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
736 Bool_t isSwapped, char*& data)
738 // Read equipment header info from DATE data stream. Returns bytes read
739 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
740 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
741 // and we will swap the header to host format.
743 memcpy(header.HeaderBegin(), data, header.HeaderSize());
744 data += header.HeaderSize();
746 // Swap equipment header data if needed
750 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
751 Error("ReadEquipmentHeader", "invalid equipment header size");
752 // try recovery... how?
756 return header.HeaderSize();
759 //______________________________________________________________________________
760 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, char*& data)
762 // Read raw data from DATE data stream. Returns bytes read (i.e.
763 // size), -1 in case of error and 0 for EOF.
765 raw.SetBuffer(data, size);
771 //______________________________________________________________________________
774 // Stop the event loop
777 if (fRawDB) fRawDB->Stop();