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>
65 #include "AliL3Logging.h"
67 #include <AliL3Transform.h>
68 #include "AliRawReaderRoot.h"
69 #include <AliL3Hough.h>
73 #include "AliRawEvent.h"
74 #include "AliRawEventHeader.h"
75 #include "AliRawEquipment.h"
76 #include "AliRawEquipmentHeader.h"
77 #include "AliRawData.h"
80 #include "AliRawRFIODB.h"
81 #include "AliRawCastorDB.h"
82 #include "AliRawRootdDB.h"
83 #include "AliRawNullDB.h"
93 #define ALIDEBUG(level) \
94 if (AliMDC::Instance() && (AliMDC::Instance()->GetDebugLevel() >= (level)))
97 // Fixed file system locations for the different DB's
99 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
100 const char* const AliMDC::fgkRawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
101 const char* const AliMDC::fgkTagDBFS = "/tmp/mdc1/tags";
102 const char* const AliMDC::fgkRunDBFS = "/tmp/mdc1/meta";
103 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/user/r/rdm";
104 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/user/r/rdm";
105 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
106 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
107 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
109 const char* const AliMDC::fgkFifo = "/tmp/alimdc.fifo";
110 const char* const AliMDC::fgkRawDBFS[2] = { "/data1/mdc", "/data2/mdc" };
111 const char* const AliMDC::fgkTagDBFS = "/data1/mdc/tags";
112 const char* const AliMDC::fgkRunDBFS = "/data1/mdc/meta";
113 const char* const AliMDC::fgkRFIOFS = "rfio:/castor/cern.ch/lcg/dc5";
114 const char* const AliMDC::fgkCastorFS = "castor:/castor/cern.ch/lcg/dc5";
115 const char* const AliMDC::fgkRootdFS = "root://localhost//tmp/mdc1";
116 const char* const AliMDC::fgkAlienHost = "alien://aliens7.cern.ch:15000/?direct";
117 const char* const AliMDC::fgkAlienDir = "/alice_mdc/DC";
120 // Maximum size of tag db files
121 const Double_t AliMDC::fgkMaxTagFileSize = 2.5e8; // 250MB
123 Bool_t AliMDC::fgDeleteFiles = kFALSE;
124 AliMDC* AliMDC::fgInstance = NULL;
127 //______________________________________________________________________________
128 AliMDC::AliMDC(Int_t fd, Int_t compress, Double_t maxFileSize, Bool_t useFilter,
129 EWriteMode mode, Bool_t useLoop, Bool_t delFiles)
131 // Create MDC processor object.
134 fCompress = compress;
135 fMaxFileSize = maxFileSize;
136 fUseFilter = useFilter;
144 fgDeleteFiles = delFiles;
149 Error("AliMDC", "cannot register with the event builder (%s)",
155 if ((mkfifo(fgkFifo, 0644) < 0) && (errno != EEXIST)) {
156 Error("AliMDC", "cannot create fifo %s", fgkFifo);
159 if ((chmod(fgkFifo, 0666) == -1) && (errno != EPERM)) {
160 Error("AliMDC", "cannot change permission of fifo %s", fgkFifo);
163 if ((fFd = open(fgkFifo, O_RDONLY)) == -1) {
164 Error("AliMDC", "cannot open input file %s", fgkFifo);
172 printf("<AliMDC::AliMDC>: input = %s, rawdb size = %f, filter = %s, "
173 "looping = %s, compression = %d, delete files = %s",
174 fUseFifo ? "fifo" : (fUseEb ? "eb" : "file"), fMaxFileSize,
175 fUseFilter ? "on" : "off", fUseLoop ? "yes" : "no", fCompress,
176 fgDeleteFiles ? "yes" : "no");
177 if (fWriteMode == kRFIO)
178 printf(", use RFIO\n");
179 else if (fWriteMode == kROOTD)
180 printf(", use rootd\n");
181 else if (fWriteMode == kCASTOR)
182 printf(", use CASTOR/rootd\n");
183 else if (fWriteMode == kDEVNULL)
184 printf(", write raw data to /dev/null\n");
188 // install SIGUSR1 handler to allow clean interrupts
189 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
194 //______________________________________________________________________________
195 AliMDC::AliMDC(const AliMDC& mdc): TObject(mdc)
199 Fatal("AliMDC", "copy constructor not implemented");
202 //______________________________________________________________________________
203 AliMDC& AliMDC::operator = (const AliMDC& /*mdc*/)
205 // assignment operator
207 Fatal("operator =", "assignment operator not implemented");
211 //______________________________________________________________________________
214 // Run the MDC processor. Read from the input stream and only return
215 // when the input gave and EOF or a fatal error occured. On success 0
216 // is returned, 1 in case of a fatality.
221 // Make sure needed directories exist
223 dirs[0] = fgkRawDBFS[0];
224 dirs[1] = fgkRawDBFS[1];
225 dirs[2] = fgkTagDBFS;
226 dirs[3] = fgkRunDBFS;
227 for (int idir = 0; idir < 4; idir++) {
228 gSystem->ResetErrno();
229 gSystem->MakeDirectory(dirs[idir]);
230 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
231 SysError("Run", "mkdir %s", dirs[idir]);
236 // Used for statistics
238 Double_t told = 0, tnew = 0;
239 Float_t chunkSize = fMaxFileSize/100, nextChunk = chunkSize;
241 // Event object used to store event data.
242 AliRawEvent *event = new AliRawEvent;
246 AliL3Log::fgLevel=AliL3Log::kError;
248 AliL3Log::fgLevel=AliL3Log::kWarning;
250 AliL3Log::fgLevel=AliL3Log::kWarning;
252 AliL3Log::fgLevel=AliL3Log::kNone;
255 if (!AliL3Transform::Init("./", kFALSE)) {
256 Error("Run","HLT initialization failed!");
261 AliESD *esd = new AliESD;
264 // Create new raw DB.
266 if (fWriteMode == kRFIO)
267 rawdb = new AliRawRFIODB(event,
271 fMaxFileSize, fCompress);
272 else if (fWriteMode == kROOTD)
273 rawdb = new AliRawRootdDB(event,
277 fMaxFileSize, fCompress);
278 else if (fWriteMode == kCASTOR)
279 rawdb = new AliRawCastorDB(event,
283 fMaxFileSize, fCompress);
284 else if (fWriteMode == kDEVNULL)
285 rawdb = new AliRawNullDB(event,
289 fMaxFileSize, fCompress);
291 rawdb = new AliRawDB(event,
295 fMaxFileSize, fCompress);
297 if (rawdb->IsZombie()) return 1;
298 printf("Filling raw DB %s\n", rawdb->GetDBName());
300 // Create new tag DB.
303 // no tagdb for the time being to get maximum speed
304 if (fWriteMode == fgkDEVNULL)
305 tagdb = new AliTagNullDB(event->GetHeader(), fgkMaxTagFileSize);
307 tagdb = new AliTagDB(event->GetHeader(), fgkMaxTagFileSize);
308 if (tagdb->IsZombie())
311 printf("Filling tag DB %s\n", tagdb->GetDBName());
314 // Create AliStats object
315 AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
317 // Shortcut for easy header access
318 AliRawEventHeader &header = *event->GetHeader();
320 // Process input stream
323 while (!(eorFlag = ebEor())) {
325 if ((ebvec = ebGetNextEvent()) == (void *)-1) {
326 Error("Run", "error getting next event (%s)", ebGetLastError());
330 // no event, sleep for 1 second and try again
331 gSystem->Sleep(1000);
334 char *ebdata = (char *) ebvec[0].iov_base;
341 if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
345 ::lseek(fFd, 0, SEEK_SET);
349 printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
357 // If we were in looping mode stop directly after a SIGUSR1 signal
359 Info("Run", "Stopping loop, processed %d events", fNumEvents);
363 // Check if event has any hard track flagged
364 Bool_t callFilter = kFALSE;
368 // Check event type and skip "Start of Run", "End of Run",
369 // "Start of Run Files" and "End of Run Files"
370 switch (header.GetType()) {
371 case AliRawEventHeader::kStartOfRun:
372 case AliRawEventHeader::kEndOfRun:
373 case AliRawEventHeader::kStartOfRunFiles:
374 case AliRawEventHeader::kEndOfRunFiles:
376 Int_t skip = header.GetEventSize() - header.HeaderSize();
378 ::lseek(fFd, skip, SEEK_CUR);
381 Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
386 Int_t s = header.GetEventSize() - header.HeaderSize();
387 Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
391 // Amount of data left to read for this event
392 Int_t toRead = header.GetEventSize() - header.HeaderSize();
394 // If there is less data for this event than the next sub-event
395 // header, something is wrong. Skip to next event...
396 if (toRead < header.HeaderSize()) {
399 "header size (%d) exceeds number of bytes to read (%d)\n",
400 header.HeaderSize(), toRead);
403 if ((status = DumpEvent(toRead)) != toRead) {
408 Error("Run", "discarding event %d (too little data for header)", fNumEvents);
412 // Loop over all sub-events... (LDCs)
416 ebdata = (char *)ebvec[nsub].iov_base;
420 Info("Run", "reading LDC %d", nsub);
422 AliRawEvent *subEvent = event->NextSubEvent();
424 // Read sub-event header
425 AliRawEventHeader &subHeader = *subEvent->GetHeader();
426 if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
428 Error("Run", "unexpected EOF reading sub-event header");
437 toRead -= subHeader.HeaderSize();
440 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
443 Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
445 // Make sure raw data less than left over bytes for current event
446 if (rawSize > toRead) {
448 Warning("Run", "raw data size (%d) exceeds number of "
449 "bytes to read (%d)\n", rawSize, toRead);
452 if ((status = DumpEvent(toRead)) != toRead) {
457 Error("Run", "discarding event %d (too much data)", fNumEvents);
461 // Read Equipment Headers (in case of physics or calibration event)
462 if (header.GetType() == AliRawEventHeader::kPhysicsEvent ||
463 header.GetType() == AliRawEventHeader::kCalibrationEvent) {
464 while (rawSize > 0) {
465 AliRawEquipment &equipment = *subEvent->NextEquipment();
466 AliRawEquipmentHeader &equipmentHeader =
467 *equipment.GetEquipmentHeader();
468 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
469 if ((status = ReadEquipmentHeader(equipmentHeader, header.DataIsSwapped(),
470 ebdata)) != equipHeaderSize) {
472 Error("Run", "unexpected EOF reading equipment-header");
477 toRead -= equipHeaderSize;
478 rawSize -= equipHeaderSize;
480 ebdata = (char *)(ebvec[nsub].iov_base) +
481 subHeader.HeaderSize() + equipHeaderSize;
484 // Read equipment raw data
485 AliRawData &subRaw = *equipment.GetRawData();
487 Int_t eqSize = equipmentHeader.GetEquipmentSize() -
489 if ((status = ReadRawData(subRaw, eqSize, ebdata)) != eqSize) {
491 Error("Run", "unexpected EOF reading sub-event raw data");
501 } else { // Read only raw data but no equipment header
502 AliRawEquipment &equipment = *subEvent->NextEquipment();
503 AliRawData &subRaw = *equipment.GetRawData();
504 if ((status = ReadRawData(subRaw, rawSize, ebdata)) != rawSize) {
506 Error("Run", "unexpected EOF reading sub-event raw data");
521 if(header.GetType() == AliRawEventHeader::kPhysicsEvent ||
522 header.GetType() == AliRawEventHeader::kCalibrationEvent)
531 // Set stat info for first event of this file
532 if (rawdb->GetEvents() == 0)
533 stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
535 // Store raw event in tree
538 // Store header in tree
539 if (tagdb) tagdb->Fill();
543 if (!(fNumEvents%10))
544 printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
546 // Filling time statistics
547 if (rawdb->GetBytesWritten() > nextChunk) {
548 tnew = timer.RealTime();
549 stats->Fill(tnew-told);
552 nextChunk += chunkSize;
555 // Check size of raw db. If bigger than maxFileSize, close file
556 // and continue with new file.
557 if (rawdb->FileFull()) {
559 printf("Written raw DB at a rate of %.1f MB/s\n",
560 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
562 // Write stats object to raw db, run db, MySQL and AliEn
563 rawdb->WriteStats(stats);
564 AliRunDB::WriteStats(stats);
567 if (!rawdb->NextFile()) {
568 Error("Run", "error opening next raw data file");
572 printf("Filling raw DB %s\n", rawdb->GetDBName());
573 stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
577 nextChunk = chunkSize;
580 // Check size of tag db
581 if (tagdb && tagdb->FileFull()) {
582 if (!tagdb->NextFile())
585 printf("Filling tag DB %s\n", tagdb->GetDBName());
588 // Make top event object ready for next event data
589 //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
592 // Clean up HLT ESD for the next event
593 // Probably we could add esd->Reset() method to AliESD?
597 if (!ebReleaseEvent(ebvec)) {
598 Error("Run", "problem releasing event (%s)", ebGetLastError());
604 printf("Written raw DB at a rate of %.1f MB/s\n",
605 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
607 // Write stats to raw db and run db and delete stats object
608 rawdb->WriteStats(stats);
609 AliRunDB::WriteStats(stats);
618 // Close input source
623 if (fUseFifo && ::unlink(fgkFifo) == -1) {
624 SysError("Run", "unlink");
632 Info("Run", "event builder reported end of run (%d)", eorFlag);
639 //______________________________________________________________________________
640 Int_t AliMDC::Read(void *buffer, Int_t length)
642 // Read exactly length bytes into buffer. Returns number of bytes
643 // received, returns -1 in case of error and 0 for EOF.
647 if (fFd < 0) return -1;
650 char *buf = (char *)buffer;
652 for (n = 0; n < length; n += nrecv) {
653 if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
657 SysError("Read", "read");
664 //______________________________________________________________________________
665 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
667 // Read header info from DATE data stream. Returns bytes read (i.e.
668 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
673 // read from event builder memory area
674 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
675 nrecv = header.HeaderSize();
677 // read from fifo or file
678 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
679 header.HeaderSize()) {
686 // Swap header data if needed
687 if (header.IsSwapped())
690 // Is header valid...
691 if (!header.IsValid()) {
692 Error("ReadHeader", "invalid header format");
693 // try recovery... how?
696 if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
697 Error("ReadHeader", "invalid header size");
698 // try recovery... how?
705 //______________________________________________________________________________
706 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
707 Bool_t isSwapped, void *eb)
709 // Read equipment header info from DATE data stream. Returns bytes read
710 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
711 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
712 // and we will swap the header to host format.
717 // read from event builder memory area
718 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
719 nrecv = header.HeaderSize();
721 // read from fifo or file
722 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
723 header.HeaderSize()) {
730 // Swap equipment header data if needed
734 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
735 Error("ReadEquipmentHeader", "invalid equipment header size");
736 // try recovery... how?
743 //______________________________________________________________________________
744 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
746 // Read raw data from DATE data stream. Returns bytes read (i.e.
747 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
752 // read from event builder memory area
753 raw.SetBuffer(eb, size);
756 // read from fifo or file
758 if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
760 Error("ReadRawData", "unexpected EOF");
770 //______________________________________________________________________________
771 Int_t AliMDC::DumpEvent(Int_t toRead)
773 // This case should not happen, but if it does try to handle it
774 // gracefully by reading the rest of the event and discarding it.
775 // Returns bytes read, -1 in case of fatal error and 0 for EOF.
777 Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
780 char *tbuf = new char[toRead];
781 if ((nrecv = Read(tbuf, toRead)) != toRead) {
783 Error("DumpEvent", "unexpected EOF");
793 //______________________________________________________________________________
794 Int_t AliMDC::Filter(
796 AliRawEvent *event,AliESD *esd
800 // Call 3rd level filter for this raw data event.
809 AliL3Hough *hough1 = new AliL3Hough();
811 hough1->SetThreshold(4);
812 hough1->CalcTransformerParams(0.4);
813 hough1->SetPeakThreshold(70,-1);
814 // Attention Z of the vertex to be taken from the event head!
815 // So far for debug purposes it is fixed by hand...
816 hough1->Init(100,4,event,3.82147);
817 hough1->SetAddHistograms();
819 AliL3Hough *hough2 = new AliL3Hough();
821 hough2->SetThreshold(4);
822 hough2->CalcTransformerParams(0.4);
823 hough2->SetPeakThreshold(70,-1);
824 hough2->Init(100,4,event,3.82147);
825 hough2->SetAddHistograms();
827 Int_t nglobaltracks = 0;
828 /* In case we run HLT code in 2 threads */
829 hough1->StartProcessInThread(0,17);
830 hough2->StartProcessInThread(18,35);
832 if(hough1->WaitForThreadFinish())
833 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
834 if(hough2->WaitForThreadFinish())
835 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
837 /* In case we run HLT code in the main thread
838 for(Int_t slice=0; slice<=17; slice++)
840 hough1->ReadData(slice,0);
842 hough1->AddAllHistogramsRows();
843 hough1->FindTrackCandidatesRow();
846 for(Int_t slice=18; slice<=35; slice++)
848 hough2->ReadData(slice,0);
850 hough2->AddAllHistogramsRows();
851 hough2->FindTrackCandidatesRow();
856 nglobaltracks += hough1->FillESD(esd);
857 nglobaltracks += hough2->FillESD(esd);
859 /* In case we want to debug the ESD
860 gSystem->MakeDirectory("hough1");
861 hough1->WriteTracks("./hough1");
862 gSystem->MakeDirectory("hough2");
863 hough2->WriteTracks("./hough2");
869 printf("Filter called for event %d\n", fNumEvents);
870 printf("Filter has found %d TPC tracks in %f seconds\n", nglobaltracks,timer.RealTime());
875 printf("Filter called for event %d\n", fNumEvents);
882 //______________________________________________________________________________
883 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const
884 AliMDCInterruptHandler&
886 TSignalHandler(handler)
890 Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
893 //______________________________________________________________________________
894 AliMDC::AliMDCInterruptHandler&
895 AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler&
898 // assignment operator
900 Fatal("operator =", "assignment operator not implemented");