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"
63 #include <AliL3StandardIncludes.h>
64 #include "AliL3Logging.h"
65 #include <AliL3Transform.h>
66 #include "AliRawReaderRoot.h"
67 #include <AliL3Hough.h>
71 #include "AliRawEvent.h"
72 #include "AliRawEventHeader.h"
73 #include "AliRawEquipment.h"
74 #include "AliRawEquipmentHeader.h"
75 #include "AliRawData.h"
78 #include "AliRawRFIODB.h"
79 #include "AliRawCastorDB.h"
80 #include "AliRawRootdDB.h"
81 #include "AliRawNullDB.h"
90 #define ALIDEBUG(level) \
91 if (AliMDC::Instance() && (AliMDC::Instance()->GetDebugLevel() >= (level)))
94 // Fixed file system locations for the different DB's
96 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
97 const char* const AliMDC::fgkRawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
98 const char* const AliMDC::fgkTagDBFS = "/tmp/mdc1/tags";
99 const char* const AliMDC::fgkRunDBFS = "/tmp/mdc1/meta";
100 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/user/r/rdm";
101 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/user/r/rdm";
102 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
103 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
104 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
106 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
107 const char* const AliMDC::fgkRawDBFS[2] = { "/data1/mdc", "/data2/mdc" };
108 const char* const AliMDC::fgkTagDBFS = "/data1/mdc/tags";
109 const char* const AliMDC::fgkRunDBFS = "/data1/mdc/meta";
110 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/lcg/dc5";
111 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/lcg/dc5";
112 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
113 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
114 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
117 // Maximum size of tag db files
118 const Double_t AliMDC::fgkMaxTagFileSize = 2.5e8; // 250MB
120 Bool_t AliMDC::fgDeleteFiles = kFALSE;
121 AliMDC* AliMDC::fgInstance = NULL;
124 //______________________________________________________________________________
125 AliMDC::AliMDC(Int_t fd, Int_t compress, Double_t maxFileSize, Bool_t useFilter,
126 EWriteMode mode, Bool_t useLoop, Bool_t delFiles)
128 // Create MDC processor object.
131 fCompress = compress;
132 fMaxFileSize = maxFileSize;
133 fUseFilter = useFilter;
141 fgDeleteFiles = delFiles;
146 Error("AliMDC", "cannot register with the event builder (%s)",
152 if ((mkfifo(fgkFifo, 0644) < 0) && (errno != EEXIST)) {
153 Error("AliMDC", "cannot create fifo %s", fgkFifo);
156 if ((chmod(fgkFifo, 0666) == -1) && (errno != EPERM)) {
157 Error("AliMDC", "cannot change permission of fifo %s", fgkFifo);
160 if ((fFd = open(fgkFifo, O_RDONLY)) == -1) {
161 Error("AliMDC", "cannot open input file %s", fgkFifo);
169 printf("<AliMDC::AliMDC>: input = %s, rawdb size = %f, filter = %s, "
170 "looping = %s, compression = %d, delete files = %s",
171 fUseFifo ? "fifo" : (fUseEb ? "eb" : "file"), fMaxFileSize,
172 fUseFilter ? "on" : "off", fUseLoop ? "yes" : "no", fCompress,
173 fgDeleteFiles ? "yes" : "no");
174 if (fWriteMode == kRFIO)
175 printf(", use RFIO\n");
176 else if (fWriteMode == kROOTD)
177 printf(", use rootd\n");
178 else if (fWriteMode == kCASTOR)
179 printf(", use CASTOR/rootd\n");
180 else if (fWriteMode == kDEVNULL)
181 printf(", write raw data to /dev/null\n");
185 // install SIGUSR1 handler to allow clean interrupts
186 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
191 //______________________________________________________________________________
192 AliMDC::AliMDC(const AliMDC& mdc): TObject(mdc)
196 Fatal("AliMDC", "copy constructor not implemented");
199 //______________________________________________________________________________
200 AliMDC& AliMDC::operator = (const AliMDC& /*mdc*/)
202 // assignment operator
204 Fatal("operator =", "assignment operator not implemented");
208 //______________________________________________________________________________
211 // Run the MDC processor. Read from the input stream and only return
212 // when the input gave and EOF or a fatal error occured. On success 0
213 // is returned, 1 in case of a fatality.
218 // Make sure needed directories exist
220 dirs[0] = fgkRawDBFS[0];
221 dirs[1] = fgkRawDBFS[1];
222 dirs[2] = fgkTagDBFS;
223 dirs[3] = fgkRunDBFS;
224 for (int idir = 0; idir < 4; idir++) {
225 gSystem->ResetErrno();
226 gSystem->MakeDirectory(dirs[idir]);
227 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
228 SysError("Run", "mkdir %s", dirs[idir]);
233 // Used for statistics
235 Double_t told = 0, tnew = 0;
236 Float_t chunkSize = fMaxFileSize/100, nextChunk = chunkSize;
238 // Event object used to store event data.
239 AliRawEvent *event = new AliRawEvent;
242 AliL3Log::fgLevel=AliL3Log::kError;
244 AliL3Log::fgLevel=AliL3Log::kWarning;
246 AliL3Log::fgLevel=AliL3Log::kWarning;
248 AliL3Log::fgLevel=AliL3Log::kNone;
250 if (!AliL3Transform::Init("./", kFALSE)) {
251 Error("Run","HLT initialization failed!");
255 AliESD *esd = new AliESD;
258 // Create new raw DB.
260 if (fWriteMode == kRFIO)
261 rawdb = new AliRawRFIODB(event, esd, fMaxFileSize, fCompress);
262 else if (fWriteMode == kROOTD)
263 rawdb = new AliRawRootdDB(event, esd, fMaxFileSize, fCompress);
264 else if (fWriteMode == kCASTOR)
265 rawdb = new AliRawCastorDB(event, esd, fMaxFileSize, fCompress);
266 else if (fWriteMode == kDEVNULL)
267 rawdb = new AliRawNullDB(event, esd, fMaxFileSize, fCompress);
269 rawdb = new AliRawDB(event, esd, fMaxFileSize, fCompress);
271 if (rawdb->IsZombie()) return 1;
272 printf("Filling raw DB %s\n", rawdb->GetDBName());
274 // Create new tag DB.
277 // no tagdb for the time being to get maximum speed
278 if (fWriteMode == fgkDEVNULL)
279 tagdb = new AliTagNullDB(event->GetHeader(), fgkMaxTagFileSize);
281 tagdb = new AliTagDB(event->GetHeader(), fgkMaxTagFileSize);
282 if (tagdb->IsZombie())
285 printf("Filling tag DB %s\n", tagdb->GetDBName());
288 // Create AliStats object
289 AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
291 // Shortcut for easy header access
292 AliRawEventHeader &header = *event->GetHeader();
294 // Process input stream
297 while (!(eorFlag = ebEor())) {
299 if ((ebvec = ebGetNextEvent()) == (void *)-1) {
300 Error("Run", "error getting next event (%s)", ebGetLastError());
304 // no event, sleep for 1 second and try again
305 gSystem->Sleep(1000);
308 char *ebdata = (char *) ebvec[0].iov_base;
315 if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
319 ::lseek(fFd, 0, SEEK_SET);
323 printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
331 // If we were in looping mode stop directly after a SIGUSR1 signal
333 Info("Run", "Stopping loop, processed %d events", fNumEvents);
337 // Check if event has any hard track flagged
338 Bool_t callFilter = kFALSE;
342 // Check event type and skip "Start of Run", "End of Run",
343 // "Start of Run Files" and "End of Run Files"
344 switch (header.GetType()) {
345 case AliRawEventHeader::kStartOfRun:
346 case AliRawEventHeader::kEndOfRun:
347 case AliRawEventHeader::kStartOfRunFiles:
348 case AliRawEventHeader::kEndOfRunFiles:
350 Int_t skip = header.GetEventSize() - header.HeaderSize();
352 ::lseek(fFd, skip, SEEK_CUR);
355 Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
360 Int_t s = header.GetEventSize() - header.HeaderSize();
361 Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
365 // Amount of data left to read for this event
366 Int_t toRead = header.GetEventSize() - header.HeaderSize();
368 // If there is less data for this event than the next sub-event
369 // header, something is wrong. Skip to next event...
370 if (toRead < header.HeaderSize()) {
373 "header size (%d) exceeds number of bytes to read (%d)\n",
374 header.HeaderSize(), toRead);
377 if ((status = DumpEvent(toRead)) != toRead) {
382 Error("Run", "discarding event %d (too little data for header)", fNumEvents);
386 // Loop over all sub-events... (LDCs)
390 ebdata = (char *)ebvec[nsub].iov_base;
394 Info("Run", "reading LDC %d", nsub);
396 AliRawEvent *subEvent = event->NextSubEvent();
398 // Read sub-event header
399 AliRawEventHeader &subHeader = *subEvent->GetHeader();
400 if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
402 Error("Run", "unexpected EOF reading sub-event header");
411 toRead -= subHeader.HeaderSize();
414 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
417 Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
419 // Make sure raw data less than left over bytes for current event
420 if (rawSize > toRead) {
422 Warning("Run", "raw data size (%d) exceeds number of "
423 "bytes to read (%d)\n", rawSize, toRead);
426 if ((status = DumpEvent(toRead)) != toRead) {
431 Error("Run", "discarding event %d (too much data)", fNumEvents);
435 // Read Equipment Headers (in case of physics or calibration event)
436 if (header.GetType() == AliRawEventHeader::kPhysicsEvent ||
437 header.GetType() == AliRawEventHeader::kCalibrationEvent) {
438 while (rawSize > 0) {
439 AliRawEquipment &equipment = *subEvent->NextEquipment();
440 AliRawEquipmentHeader &equipmentHeader =
441 *equipment.GetEquipmentHeader();
442 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
443 if ((status = ReadEquipmentHeader(equipmentHeader, header.DataIsSwapped(),
444 ebdata)) != equipHeaderSize) {
446 Error("Run", "unexpected EOF reading equipment-header");
451 toRead -= equipHeaderSize;
452 rawSize -= equipHeaderSize;
454 ebdata = (char *)(ebvec[nsub].iov_base) +
455 subHeader.HeaderSize() + equipHeaderSize;
458 // Read equipment raw data
459 AliRawData &subRaw = *equipment.GetRawData();
461 Int_t eqSize = equipmentHeader.GetEquipmentSize() -
463 if ((status = ReadRawData(subRaw, eqSize, ebdata)) != eqSize) {
465 Error("Run", "unexpected EOF reading sub-event raw data");
475 } else { // Read only raw data but no equipment header
476 AliRawEquipment &equipment = *subEvent->NextEquipment();
477 AliRawData &subRaw = *equipment.GetRawData();
478 if ((status = ReadRawData(subRaw, rawSize, ebdata)) != rawSize) {
480 Error("Run", "unexpected EOF reading sub-event raw data");
495 if(header.GetType() == AliRawEventHeader::kPhysicsEvent ||
496 header.GetType() == AliRawEventHeader::kCalibrationEvent)
505 // Set stat info for first event of this file
506 if (rawdb->GetEvents() == 0)
507 stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
509 // Store raw event in tree
512 // Store header in tree
513 if (tagdb) tagdb->Fill();
517 if (!(fNumEvents%10))
518 printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
520 // Filling time statistics
521 if (rawdb->GetBytesWritten() > nextChunk) {
522 tnew = timer.RealTime();
523 stats->Fill(tnew-told);
526 nextChunk += chunkSize;
529 // Check size of raw db. If bigger than maxFileSize, close file
530 // and continue with new file.
531 if (rawdb->FileFull()) {
533 printf("Written raw DB at a rate of %.1f MB/s\n",
534 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
536 // Write stats object to raw db, run db, MySQL and AliEn
537 stats->WriteToDB(rawdb);
540 if (!rawdb->NextFile()) {
541 Error("Run", "error opening next raw data file");
545 printf("Filling raw DB %s\n", rawdb->GetDBName());
546 stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
550 nextChunk = chunkSize;
553 // Check size of tag db
554 if (tagdb && tagdb->FileFull()) {
555 if (!tagdb->NextFile())
558 printf("Filling tag DB %s\n", tagdb->GetDBName());
561 // Make top event object ready for next event data
562 //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
565 // Clean up HLT ESD for the next event
566 // Probably we could add esd->Reset() method to AliESD?
570 if (!ebReleaseEvent(ebvec)) {
571 Error("Run", "problem releasing event (%s)", ebGetLastError());
577 printf("Written raw DB at a rate of %.1f MB/s\n",
578 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
580 // Write stats to raw db and run db and delete stats object
581 stats->WriteToDB(rawdb);
590 // Close input source
595 if (fUseFifo && ::unlink(fgkFifo) == -1) {
596 SysError("Run", "unlink");
604 Info("Run", "event builder reported end of run (%d)", eorFlag);
611 //______________________________________________________________________________
612 Int_t AliMDC::Read(void *buffer, Int_t length)
614 // Read exactly length bytes into buffer. Returns number of bytes
615 // received, returns -1 in case of error and 0 for EOF.
619 if (fFd < 0) return -1;
622 char *buf = (char *)buffer;
624 for (n = 0; n < length; n += nrecv) {
625 if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
629 SysError("Read", "read");
636 //______________________________________________________________________________
637 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
639 // Read header info from DATE data stream. Returns bytes read (i.e.
640 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
645 // read from event builder memory area
646 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
647 nrecv = header.HeaderSize();
649 // read from fifo or file
650 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
651 header.HeaderSize()) {
658 // Swap header data if needed
659 if (header.IsSwapped())
662 // Is header valid...
663 if (!header.IsValid()) {
664 Error("ReadHeader", "invalid header format");
665 // try recovery... how?
668 if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
669 Error("ReadHeader", "invalid header size");
670 // try recovery... how?
677 //______________________________________________________________________________
678 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
679 Bool_t isSwapped, void *eb)
681 // Read equipment header info from DATE data stream. Returns bytes read
682 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
683 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
684 // and we will swap the header to host format.
689 // read from event builder memory area
690 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
691 nrecv = header.HeaderSize();
693 // read from fifo or file
694 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
695 header.HeaderSize()) {
702 // Swap equipment header data if needed
706 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
707 Error("ReadEquipmentHeader", "invalid equipment header size");
708 // try recovery... how?
715 //______________________________________________________________________________
716 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
718 // Read raw data from DATE data stream. Returns bytes read (i.e.
719 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
724 // read from event builder memory area
725 raw.SetBuffer(eb, size);
728 // read from fifo or file
730 if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
732 Error("ReadRawData", "unexpected EOF");
742 //______________________________________________________________________________
743 Int_t AliMDC::DumpEvent(Int_t toRead)
745 // This case should not happen, but if it does try to handle it
746 // gracefully by reading the rest of the event and discarding it.
747 // Returns bytes read, -1 in case of fatal error and 0 for EOF.
749 Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
752 char *tbuf = new char[toRead];
753 if ((nrecv = Read(tbuf, toRead)) != toRead) {
755 Error("DumpEvent", "unexpected EOF");
765 //______________________________________________________________________________
766 Int_t AliMDC::Filter(
768 AliRawEvent *event,AliESD *esd
772 // Call 3rd level filter for this raw data event.
781 AliL3Hough *hough1 = new AliL3Hough();
783 hough1->SetThreshold(4);
784 hough1->SetTransformerParams(76,140,0.4,-1);
785 hough1->SetPeakThreshold(70,-1);
786 // Attention Z of the vertex to be taken from the event head!
787 // So far for debug purposes it is fixed by hand...
788 hough1->Init(100,4,event,3.82147);
789 hough1->SetAddHistograms();
791 AliL3Hough *hough2 = new AliL3Hough();
793 hough2->SetThreshold(4);
794 hough2->SetTransformerParams(76,140,0.4,-1);
795 hough2->SetPeakThreshold(70,-1);
796 hough2->Init(100,4,event,3.82147);
797 hough2->SetAddHistograms();
799 Int_t nglobaltracks = 0;
800 /* In case we run HLT code in 2 threads */
801 hough1->StartProcessInThread(0,17);
802 hough2->StartProcessInThread(18,35);
804 if(hough1->WaitForThreadFinish())
805 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
806 if(hough2->WaitForThreadFinish())
807 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
809 /* In case we run HLT code in the main thread
810 for(Int_t slice=0; slice<=17; slice++)
812 hough1->ReadData(slice,0);
814 hough1->AddAllHistogramsRows();
815 hough1->FindTrackCandidatesRow();
818 for(Int_t slice=18; slice<=35; slice++)
820 hough2->ReadData(slice,0);
822 hough2->AddAllHistogramsRows();
823 hough2->FindTrackCandidatesRow();
828 nglobaltracks += hough1->FillESD(esd);
829 nglobaltracks += hough2->FillESD(esd);
831 /* In case we want to debug the ESD
832 gSystem->MakeDirectory("hough1");
833 hough1->WriteTracks("./hough1");
834 gSystem->MakeDirectory("hough2");
835 hough2->WriteTracks("./hough2");
841 printf("Filter called for event %d\n", fNumEvents);
842 printf("Filter has found %d TPC tracks in %f seconds\n", nglobaltracks,timer.RealTime());
847 printf("Filter called for event %d\n", fNumEvents);
854 //______________________________________________________________________________
855 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const
856 AliMDCInterruptHandler&
858 TSignalHandler(handler)
862 Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
865 //______________________________________________________________________________
866 AliMDC::AliMDCInterruptHandler&
867 AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler&
870 // assignment operator
872 Fatal("operator =", "assignment operator not implemented");