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;
243 AliL3Log::fgLevel=AliL3Log::kError;
245 AliL3Log::fgLevel=AliL3Log::kWarning;
247 AliL3Log::fgLevel=AliL3Log::kWarning;
249 AliL3Log::fgLevel=AliL3Log::kNone;
251 if (!AliL3Transform::Init("./", kFALSE)) {
252 Error("Run","HLT initialization failed!");
256 AliESD *esd = new AliESD;
259 // Create new raw DB.
261 if (fWriteMode == kRFIO)
262 rawdb = new AliRawRFIODB(event, esd, fMaxFileSize, fCompress);
263 else if (fWriteMode == kROOTD)
264 rawdb = new AliRawRootdDB(event, esd, fMaxFileSize, fCompress);
265 else if (fWriteMode == kCASTOR)
266 rawdb = new AliRawCastorDB(event, esd, fMaxFileSize, fCompress);
267 else if (fWriteMode == kDEVNULL)
268 rawdb = new AliRawNullDB(event, esd, fMaxFileSize, fCompress);
270 rawdb = new AliRawDB(event, esd, fMaxFileSize, fCompress);
272 if (rawdb->IsZombie()) return 1;
273 printf("Filling raw DB %s\n", rawdb->GetDBName());
275 // Create new tag DB.
278 // no tagdb for the time being to get maximum speed
279 if (fWriteMode == fgkDEVNULL)
280 tagdb = new AliTagNullDB(event->GetHeader(), fgkMaxTagFileSize);
282 tagdb = new AliTagDB(event->GetHeader(), fgkMaxTagFileSize);
283 if (tagdb->IsZombie())
286 printf("Filling tag DB %s\n", tagdb->GetDBName());
289 // Create AliStats object
290 AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
292 // Shortcut for easy header access
293 AliRawEventHeader &header = *event->GetHeader();
295 // Process input stream
298 while (!(eorFlag = ebEor())) {
300 if ((ebvec = ebGetNextEvent()) == (void *)-1) {
301 Error("Run", "error getting next event (%s)", ebGetLastError());
305 // no event, sleep for 1 second and try again
306 gSystem->Sleep(1000);
309 char *ebdata = (char *) ebvec[0].iov_base;
316 if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
320 ::lseek(fFd, 0, SEEK_SET);
324 printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
332 // If we were in looping mode stop directly after a SIGUSR1 signal
334 Info("Run", "Stopping loop, processed %d events", fNumEvents);
338 // Check if event has any hard track flagged
339 Bool_t callFilter = kFALSE;
343 // Check event type and skip "Start of Run", "End of Run",
344 // "Start of Run Files" and "End of Run Files"
345 switch (header.GetType()) {
346 case AliRawEventHeader::kStartOfRun:
347 case AliRawEventHeader::kEndOfRun:
348 case AliRawEventHeader::kStartOfRunFiles:
349 case AliRawEventHeader::kEndOfRunFiles:
351 Int_t skip = header.GetEventSize() - header.HeaderSize();
353 ::lseek(fFd, skip, SEEK_CUR);
356 Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
361 Int_t s = header.GetEventSize() - header.HeaderSize();
362 Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
366 // Amount of data left to read for this event
367 Int_t toRead = header.GetEventSize() - header.HeaderSize();
369 // If there is less data for this event than the next sub-event
370 // header, something is wrong. Skip to next event...
371 if (toRead < header.HeaderSize()) {
374 "header size (%d) exceeds number of bytes to read (%d)\n",
375 header.HeaderSize(), toRead);
378 if ((status = DumpEvent(toRead)) != toRead) {
383 Error("Run", "discarding event %d (too little data for header)", fNumEvents);
387 // Loop over all sub-events... (LDCs)
391 ebdata = (char *)ebvec[nsub].iov_base;
395 Info("Run", "reading LDC %d", nsub);
397 AliRawEvent *subEvent = event->NextSubEvent();
399 // Read sub-event header
400 AliRawEventHeader &subHeader = *subEvent->GetHeader();
401 if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
403 Error("Run", "unexpected EOF reading sub-event header");
412 toRead -= subHeader.HeaderSize();
415 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
418 Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
420 // Make sure raw data less than left over bytes for current event
421 if (rawSize > toRead) {
423 Warning("Run", "raw data size (%d) exceeds number of "
424 "bytes to read (%d)\n", rawSize, toRead);
427 if ((status = DumpEvent(toRead)) != toRead) {
432 Error("Run", "discarding event %d (too much data)", fNumEvents);
436 // Read Equipment Headers (in case of physics or calibration event)
437 if (header.GetType() == AliRawEventHeader::kPhysicsEvent ||
438 header.GetType() == AliRawEventHeader::kCalibrationEvent) {
439 while (rawSize > 0) {
440 AliRawEquipment &equipment = *subEvent->NextEquipment();
441 AliRawEquipmentHeader &equipmentHeader =
442 *equipment.GetEquipmentHeader();
443 Int_t equipHeaderSize = equipmentHeader.HeaderSize();
444 if ((status = ReadEquipmentHeader(equipmentHeader, header.DataIsSwapped(),
445 ebdata)) != equipHeaderSize) {
447 Error("Run", "unexpected EOF reading equipment-header");
452 toRead -= equipHeaderSize;
453 rawSize -= equipHeaderSize;
455 ebdata = (char *)(ebvec[nsub].iov_base) +
456 subHeader.HeaderSize() + equipHeaderSize;
459 // Read equipment raw data
460 AliRawData &subRaw = *equipment.GetRawData();
462 Int_t eqSize = equipmentHeader.GetEquipmentSize() -
464 if ((status = ReadRawData(subRaw, eqSize, ebdata)) != eqSize) {
466 Error("Run", "unexpected EOF reading sub-event raw data");
476 } else { // Read only raw data but no equipment header
477 AliRawEquipment &equipment = *subEvent->NextEquipment();
478 AliRawData &subRaw = *equipment.GetRawData();
479 if ((status = ReadRawData(subRaw, rawSize, ebdata)) != rawSize) {
481 Error("Run", "unexpected EOF reading sub-event raw data");
496 if(header.GetType() == AliRawEventHeader::kPhysicsEvent ||
497 header.GetType() == AliRawEventHeader::kCalibrationEvent)
506 // Set stat info for first event of this file
507 if (rawdb->GetEvents() == 0)
508 stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
510 // Store raw event in tree
513 // Store header in tree
514 if (tagdb) tagdb->Fill();
518 if (!(fNumEvents%10))
519 printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
521 // Filling time statistics
522 if (rawdb->GetBytesWritten() > nextChunk) {
523 tnew = timer.RealTime();
524 stats->Fill(tnew-told);
527 nextChunk += chunkSize;
530 // Check size of raw db. If bigger than maxFileSize, close file
531 // and continue with new file.
532 if (rawdb->FileFull()) {
534 printf("Written raw DB at a rate of %.1f MB/s\n",
535 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
537 // Write stats object to raw db, run db, MySQL and AliEn
538 stats->WriteToDB(rawdb);
541 if (!rawdb->NextFile()) {
542 Error("Run", "error opening next raw data file");
546 printf("Filling raw DB %s\n", rawdb->GetDBName());
547 stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
551 nextChunk = chunkSize;
554 // Check size of tag db
555 if (tagdb && tagdb->FileFull()) {
556 if (!tagdb->NextFile())
559 printf("Filling tag DB %s\n", tagdb->GetDBName());
562 // Make top event object ready for next event data
563 //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
566 // Clean up HLT ESD for the next event
567 // Probably we could add esd->Reset() method to AliESD?
571 if (!ebReleaseEvent(ebvec)) {
572 Error("Run", "problem releasing event (%s)", ebGetLastError());
578 printf("Written raw DB at a rate of %.1f MB/s\n",
579 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
581 // Write stats to raw db and run db and delete stats object
582 stats->WriteToDB(rawdb);
591 // Close input source
596 if (fUseFifo && ::unlink(fgkFifo) == -1) {
597 SysError("Run", "unlink");
605 Info("Run", "event builder reported end of run (%d)", eorFlag);
612 //______________________________________________________________________________
613 Int_t AliMDC::Read(void *buffer, Int_t length)
615 // Read exactly length bytes into buffer. Returns number of bytes
616 // received, returns -1 in case of error and 0 for EOF.
620 if (fFd < 0) return -1;
623 char *buf = (char *)buffer;
625 for (n = 0; n < length; n += nrecv) {
626 if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
630 SysError("Read", "read");
637 //______________________________________________________________________________
638 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
640 // Read header info from DATE data stream. Returns bytes read (i.e.
641 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
646 // read from event builder memory area
647 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
648 nrecv = header.HeaderSize();
650 // read from fifo or file
651 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
652 header.HeaderSize()) {
659 // Swap header data if needed
660 if (header.IsSwapped())
663 // Is header valid...
664 if (!header.IsValid()) {
665 Error("ReadHeader", "invalid header format");
666 // try recovery... how?
669 if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
670 Error("ReadHeader", "invalid header size");
671 // try recovery... how?
678 //______________________________________________________________________________
679 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
680 Bool_t isSwapped, void *eb)
682 // Read equipment header info from DATE data stream. Returns bytes read
683 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
684 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
685 // and we will swap the header to host format.
690 // read from event builder memory area
691 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
692 nrecv = header.HeaderSize();
694 // read from fifo or file
695 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
696 header.HeaderSize()) {
703 // Swap equipment header data if needed
707 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
708 Error("ReadEquipmentHeader", "invalid equipment header size");
709 // try recovery... how?
716 //______________________________________________________________________________
717 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
719 // Read raw data from DATE data stream. Returns bytes read (i.e.
720 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
725 // read from event builder memory area
726 raw.SetBuffer(eb, size);
729 // read from fifo or file
731 if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
733 Error("ReadRawData", "unexpected EOF");
743 //______________________________________________________________________________
744 Int_t AliMDC::DumpEvent(Int_t toRead)
746 // This case should not happen, but if it does try to handle it
747 // gracefully by reading the rest of the event and discarding it.
748 // Returns bytes read, -1 in case of fatal error and 0 for EOF.
750 Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
753 char *tbuf = new char[toRead];
754 if ((nrecv = Read(tbuf, toRead)) != toRead) {
756 Error("DumpEvent", "unexpected EOF");
766 //______________________________________________________________________________
767 Int_t AliMDC::Filter(
769 AliRawEvent *event,AliESD *esd
773 // Call 3rd level filter for this raw data event.
782 AliL3Hough *hough1 = new AliL3Hough();
784 hough1->SetThreshold(4);
785 hough1->SetTransformerParams(76,140,0.4,-1);
786 hough1->SetPeakThreshold(70,-1);
787 // Attention Z of the vertex to be taken from the event head!
788 // So far for debug purposes it is fixed by hand...
789 hough1->Init(100,4,event,3.82147);
790 hough1->SetAddHistograms();
792 AliL3Hough *hough2 = new AliL3Hough();
794 hough2->SetThreshold(4);
795 hough2->SetTransformerParams(76,140,0.4,-1);
796 hough2->SetPeakThreshold(70,-1);
797 hough2->Init(100,4,event,3.82147);
798 hough2->SetAddHistograms();
800 Int_t nglobaltracks = 0;
801 /* In case we run HLT code in 2 threads */
802 hough1->StartProcessInThread(0,17);
803 hough2->StartProcessInThread(18,35);
805 if(hough1->WaitForThreadFinish())
806 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
807 if(hough2->WaitForThreadFinish())
808 ::Fatal("AliL3Hough::WaitForThreadFinish"," Can not join the required thread! ");
810 /* In case we run HLT code in the main thread
811 for(Int_t slice=0; slice<=17; slice++)
813 hough1->ReadData(slice,0);
815 hough1->AddAllHistogramsRows();
816 hough1->FindTrackCandidatesRow();
819 for(Int_t slice=18; slice<=35; slice++)
821 hough2->ReadData(slice,0);
823 hough2->AddAllHistogramsRows();
824 hough2->FindTrackCandidatesRow();
829 nglobaltracks += hough1->FillESD(esd);
830 nglobaltracks += hough2->FillESD(esd);
832 /* In case we want to debug the ESD
833 gSystem->MakeDirectory("hough1");
834 hough1->WriteTracks("./hough1");
835 gSystem->MakeDirectory("hough2");
836 hough2->WriteTracks("./hough2");
842 printf("Filter called for event %d\n", fNumEvents);
843 printf("Filter has found %d TPC tracks in %f seconds\n", nglobaltracks,timer.RealTime());
848 printf("Filter called for event %d\n", fNumEvents);
855 //______________________________________________________________________________
856 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const
857 AliMDCInterruptHandler&
859 TSignalHandler(handler)
863 Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
866 //______________________________________________________________________________
867 AliMDC::AliMDCInterruptHandler&
868 AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler&
871 // assignment operator
873 Fatal("operator =", "assignment operator not implemented");