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>
59 #include "libDateEb.h"
62 #include "AliRawEvent.h"
63 #include "AliRawEventHeader.h"
64 #include "AliRawEquipment.h"
65 #include "AliRawEquipmentHeader.h"
66 #include "AliRawData.h"
69 #include "AliRawRFIODB.h"
70 #include "AliRawCastorDB.h"
71 #include "AliRawRootdDB.h"
72 #include "AliRawNullDB.h"
81 #define ALIDEBUG(level) \
82 if (AliMDC::Instance() && (AliMDC::Instance()->GetDebugLevel() >= (level)))
85 // Fixed file system locations for the different DB's
87 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
88 const char* const AliMDC::fgkRawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
89 const char* const AliMDC::fgkTagDBFS = "/tmp/mdc1/tags";
90 const char* const AliMDC::fgkRunDBFS = "/tmp/mdc1/meta";
91 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/user/r/rdm";
92 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/user/r/rdm";
93 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
94 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
95 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
97 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
98 const char* const AliMDC::fgkRawDBFS[2] = { "/data1/mdc", "/data2/mdc" };
99 const char* const AliMDC::fgkTagDBFS = "/data1/mdc/tags";
100 const char* const AliMDC::fgkRunDBFS = "/data1/mdc/meta";
101 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/lcg/dc5";
102 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/lcg/dc5";
103 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
104 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
105 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
108 // Maximum size of tag db files
109 const Double_t AliMDC::fgkMaxTagFileSize = 2.5e8; // 250MB
111 Bool_t AliMDC::fgDeleteFiles = kFALSE;
112 AliMDC* AliMDC::fgInstance = NULL;
115 //______________________________________________________________________________
116 AliMDC::AliMDC(Int_t fd, Int_t compress, Double_t maxFileSize, Bool_t useFilter,
117 EWriteMode mode, Bool_t useLoop, Bool_t delFiles)
119 // Create MDC processor object.
122 fCompress = compress;
123 fMaxFileSize = maxFileSize;
124 fUseFilter = useFilter;
132 fgDeleteFiles = delFiles;
137 Error("AliMDC", "cannot register with the event builder (%s)",
143 if ((mkfifo(fgkFifo, 0644) < 0) && (errno != EEXIST)) {
144 Error("AliMDC", "cannot create fifo %s", fgkFifo);
147 if ((chmod(fgkFifo, 0666) == -1) && (errno != EPERM)) {
148 Error("AliMDC", "cannot change permission of fifo %s", fgkFifo);
151 if ((fFd = open(fgkFifo, O_RDONLY)) == -1) {
152 Error("AliMDC", "cannot open input file %s", fgkFifo);
160 printf("<AliMDC::AliMDC>: input = %s, rawdb size = %f, filter = %s, "
161 "looping = %s, compression = %d, delete files = %s",
162 fUseFifo ? "fifo" : (fUseEb ? "eb" : "file"), fMaxFileSize,
163 fUseFilter ? "on" : "off", fUseLoop ? "yes" : "no", fCompress,
164 fgDeleteFiles ? "yes" : "no");
165 if (fWriteMode == kRFIO)
166 printf(", use RFIO\n");
167 else if (fWriteMode == kROOTD)
168 printf(", use rootd\n");
169 else if (fWriteMode == kCASTOR)
170 printf(", use CASTOR/rootd\n");
171 else if (fWriteMode == kDEVNULL)
172 printf(", write raw data to /dev/null\n");
176 // install SIGUSR1 handler to allow clean interrupts
177 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
182 //______________________________________________________________________________
183 AliMDC::AliMDC(const AliMDC& mdc): TObject(mdc)
187 Fatal("AliMDC", "copy constructor not implemented");
190 //______________________________________________________________________________
191 AliMDC& AliMDC::operator = (const AliMDC& /*mdc*/)
193 // assignment operator
195 Fatal("operator =", "assignment operator not implemented");
199 //______________________________________________________________________________
202 // Run the MDC processor. Read from the input stream and only return
203 // when the input gave and EOF or a fatal error occured. On success 0
204 // is returned, 1 in case of a fatality.
209 // Make sure needed directories exist
211 dirs[0] = fgkRawDBFS[0];
212 dirs[1] = fgkRawDBFS[1];
213 dirs[2] = fgkTagDBFS;
214 dirs[3] = fgkRunDBFS;
215 for (int idir = 0; idir < 4; idir++) {
216 gSystem->ResetErrno();
217 gSystem->MakeDirectory(dirs[idir]);
218 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
219 SysError("Run", "mkdir %s", dirs[idir]);
224 // Used for statistics
226 Double_t told = 0, tnew = 0;
227 Float_t chunkSize = fMaxFileSize/100, nextChunk = chunkSize;
229 // Event object used to store event data.
230 AliRawEvent *event = new AliRawEvent;
232 // Create new raw DB.
234 if (fWriteMode == kRFIO)
235 rawdb = new AliRawRFIODB(event, fMaxFileSize, fCompress);
236 else if (fWriteMode == kROOTD)
237 rawdb = new AliRawRootdDB(event, fMaxFileSize, fCompress);
238 else if (fWriteMode == kCASTOR)
239 rawdb = new AliRawCastorDB(event, fMaxFileSize, fCompress);
240 else if (fWriteMode == kDEVNULL)
241 rawdb = new AliRawNullDB(event, fMaxFileSize, fCompress);
243 rawdb = new AliRawDB(event, fMaxFileSize, fCompress);
245 if (rawdb->IsZombie()) return 1;
246 printf("Filling raw DB %s\n", rawdb->GetDBName());
248 // Create new tag DB.
251 // no tagdb for the time being to get maximum speed
252 if (fWriteMode == fgkDEVNULL)
253 tagdb = new AliTagNullDB(event->GetHeader(), fgkMaxTagFileSize);
255 tagdb = new AliTagDB(event->GetHeader(), fgkMaxTagFileSize);
256 if (tagdb->IsZombie())
259 printf("Filling tag DB %s\n", tagdb->GetDBName());
262 // Create AliStats object
263 AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
265 // Shortcut for easy header access
266 AliRawEventHeader &header = *event->GetHeader();
268 // Process input stream
271 while (!(eorFlag = ebEor())) {
273 if ((ebvec = ebGetNextEvent()) == (void *)-1) {
274 Error("Run", "error getting next event (%s)", ebGetLastError());
278 // no event, sleep for 1 second and try again
279 gSystem->Sleep(1000);
282 char *ebdata = (char *) ebvec[0].iov_base;
289 if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
293 ::lseek(fFd, 0, SEEK_SET);
297 printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
305 // If we were in looping mode stop directly after a SIGUSR1 signal
307 Info("Run", "Stopping loop, processed %d events", fNumEvents);
311 // Check if event has any hard track flagged
312 Bool_t callFilter = kFALSE;
313 // This needs to be re-engineered for the next ADC...
314 //if (fUseFilter && TEST_USER_ATTRIBUTE(header.GetTypeAttribute(), 0))
315 // callFilter = kTRUE;
317 // Check event type and skip "Start of Run", "End of Run",
318 // "Start of Run Files" and "End of Run Files"
319 switch (header.GetType()) {
320 case AliRawEventHeader::kStartOfRun:
321 case AliRawEventHeader::kEndOfRun:
322 case AliRawEventHeader::kStartOfRunFiles:
323 case AliRawEventHeader::kEndOfRunFiles:
325 Int_t skip = header.GetEventSize() - header.HeaderSize();
327 ::lseek(fFd, skip, SEEK_CUR);
330 Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
335 Int_t s = header.GetEventSize() - header.HeaderSize();
336 Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
340 // Amount of data left to read for this event
341 Int_t toRead = header.GetEventSize() - header.HeaderSize();
343 // If there is less data for this event than the next sub-event
344 // header, something is wrong. Skip to next event...
345 if (toRead < header.HeaderSize()) {
348 "header size (%d) exceeds number of bytes to read (%d)\n",
349 header.HeaderSize(), toRead);
352 if ((status = DumpEvent(toRead)) != toRead) {
357 Error("Run", "discarding event %d (too little data for header)", fNumEvents);
361 // Loop over all sub-events... (LDCs)
365 ebdata = (char *)ebvec[nsub].iov_base;
369 Info("Run", "reading LDC %d", nsub);
371 AliRawEvent *subEvent = event->NextSubEvent();
373 // Read sub-event header
374 AliRawEventHeader &subHeader = *subEvent->GetHeader();
375 if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
377 Error("Run", "unexpected EOF reading sub-event header");
386 toRead -= subHeader.HeaderSize();
389 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
392 Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
394 // Make sure raw data less than left over bytes for current event
395 if (rawSize > toRead) {
397 Warning("Run", "raw data size (%d) exceeds number of "
398 "bytes to read (%d)\n", rawSize, toRead);
401 if ((status = DumpEvent(toRead)) != toRead) {
406 Error("Run", "discarding event %d (too much data)", fNumEvents);
410 // Read Equipment Headers (in case of physics or calibration event)
411 if (header.GetType() == AliRawEventHeader::kPhysicsEvent ||
412 header.GetType() == AliRawEventHeader::kCalibrationEvent) {
413 while (rawSize > 0) {
414 AliRawEquipment &equipment = *subEvent->NextEquipment();
415 AliRawEquipmentHeader &equipmentHeader =
416 *equipment.GetEquipmentHeader();
417 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
418 if ((status = ReadEquipmentHeader(equipmentHeader, header.DataIsSwapped(),
419 ebdata)) != equipHeaderSize) {
421 Error("Run", "unexpected EOF reading equipment-header");
426 toRead -= equipHeaderSize;
427 rawSize -= equipHeaderSize;
429 ebdata = (char *)(ebvec[nsub].iov_base) +
430 subHeader.HeaderSize() + equipHeaderSize;
433 // Read equipment raw data
434 AliRawData &subRaw = *equipment.GetRawData();
435 Int_t eqSize = equipmentHeader.GetEquipmentSize() -
437 if ((status = ReadRawData(subRaw, eqSize, ebdata)) != eqSize) {
439 Error("Run", "unexpected EOF reading sub-event raw data");
449 if (TEST_USER_ATTRIBUTE(subHeader.GetTypeAttribute(), 0))
452 // set size of all sectors without hard track flag to 0
459 } else { // Read only raw data but no equipment header
460 AliRawEquipment &equipment = *subEvent->NextEquipment();
461 AliRawData &subRaw = *equipment.GetRawData();
462 if ((status = ReadRawData(subRaw, rawSize, ebdata)) != rawSize) {
464 Error("Run", "unexpected EOF reading sub-event raw data");
473 if (TEST_USER_ATTRIBUTE(subHeader.GetTypeAttribute(), 0))
476 // set size of all sectors without hard track flag to 0
486 // Set stat info for first event of this file
487 if (rawdb->GetEvents() == 0)
488 stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
490 // Store raw event in tree
493 // Store header in tree
494 if (tagdb) tagdb->Fill();
498 if (!(fNumEvents%10))
499 printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
501 // Filling time statistics
502 if (rawdb->GetBytesWritten() > nextChunk) {
503 tnew = timer.RealTime();
504 stats->Fill(tnew-told);
507 nextChunk += chunkSize;
510 // Check size of raw db. If bigger than maxFileSize, close file
511 // and continue with new file.
512 if (rawdb->FileFull()) {
514 printf("Written raw DB at a rate of %.1f MB/s\n",
515 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
517 // Write stats object to raw db, run db, MySQL and AliEn
518 stats->WriteToDB(rawdb);
521 if (!rawdb->NextFile()) {
522 Error("Run", "error opening next raw data file");
526 printf("Filling raw DB %s\n", rawdb->GetDBName());
527 stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
531 nextChunk = chunkSize;
534 // Check size of tag db
535 if (tagdb && tagdb->FileFull()) {
536 if (!tagdb->NextFile())
539 printf("Filling tag DB %s\n", tagdb->GetDBName());
542 // Make top event object ready for next event data
543 //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
547 if (!ebReleaseEvent(ebvec)) {
548 Error("Run", "problem releasing event (%s)", ebGetLastError());
554 printf("Written raw DB at a rate of %.1f MB/s\n",
555 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
557 // Write stats to raw db and run db and delete stats object
558 stats->WriteToDB(rawdb);
567 // Close input source
572 if (fUseFifo && ::unlink(fgkFifo) == -1) {
573 SysError("Run", "unlink");
581 Info("Run", "event builder reported end of run (%d)", eorFlag);
588 //______________________________________________________________________________
589 Int_t AliMDC::Read(void *buffer, Int_t length)
591 // Read exactly length bytes into buffer. Returns number of bytes
592 // received, returns -1 in case of error and 0 for EOF.
596 if (fFd < 0) return -1;
599 char *buf = (char *)buffer;
601 for (n = 0; n < length; n += nrecv) {
602 if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
606 SysError("Read", "read");
613 //______________________________________________________________________________
614 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
616 // Read header info from DATE data stream. Returns bytes read (i.e.
617 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
622 // read from event builder memory area
623 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
624 nrecv = header.HeaderSize();
626 // read from fifo or file
627 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
628 header.HeaderSize()) {
635 // Swap header data if needed
636 if (header.IsSwapped())
639 // Is header valid...
640 if (!header.IsValid()) {
641 Error("ReadHeader", "invalid header format");
642 // try recovery... how?
645 if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
646 Error("ReadHeader", "invalid header size");
647 // try recovery... how?
654 //______________________________________________________________________________
655 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
656 Bool_t isSwapped, void *eb)
658 // Read equipment header info from DATE data stream. Returns bytes read
659 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
660 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
661 // and we will swap the header to host format.
666 // read from event builder memory area
667 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
668 nrecv = header.HeaderSize();
670 // read from fifo or file
671 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
672 header.HeaderSize()) {
679 // Swap equipment header data if needed
683 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
684 Error("ReadEquipmentHeader", "invalid equipment header size");
685 // try recovery... how?
692 //______________________________________________________________________________
693 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
695 // Read raw data from DATE data stream. Returns bytes read (i.e.
696 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
701 // read from event builder memory area
702 raw.SetBuffer(eb, size);
705 // read from fifo or file
707 if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
709 Error("ReadRawData", "unexpected EOF");
719 //______________________________________________________________________________
720 Int_t AliMDC::DumpEvent(Int_t toRead)
722 // This case should not happen, but if it does try to handle it
723 // gracefully by reading the rest of the event and discarding it.
724 // Returns bytes read, -1 in case of fatal error and 0 for EOF.
726 Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
729 char *tbuf = new char[toRead];
730 if ((nrecv = Read(tbuf, toRead)) != toRead) {
732 Error("DumpEvent", "unexpected EOF");
742 //______________________________________________________________________________
743 Int_t AliMDC::Filter(AliRawData &raw)
745 // Call 3rd level filter for this raw data segment.
754 printf("Filter called for event %d\n", fNumEvents);
761 //______________________________________________________________________________
762 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const
763 AliMDCInterruptHandler&
765 TSignalHandler(handler)
769 Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
772 //______________________________________________________________________________
773 AliMDC::AliMDCInterruptHandler&
774 AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler&
777 // assignment operator
779 Fatal("operator =", "assignment operator not implemented");