]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliMDC.cxx
72a6473e686ffb08561948694d42eb551ba7ec94
[u/mrichter/AliRoot.git] / RAW / AliMDC.cxx
1 // @(#)alimdc:$Name$:$Id$
2 // Author: Fons Rademakers  26/11/99
3 // Updated: Dario Favretto  15/04/2003
4
5 /**************************************************************************
6  * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
7  *                                                                        *
8  * Author: The ALICE Off-line Project.                                    *
9  * Contributors are mentioned in the code where appropriate.              *
10  *                                                                        *
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  **************************************************************************/
19
20 /* $Id$ */
21
22 //////////////////////////////////////////////////////////////////////////
23 //                                                                      //
24 // AliMDC                                                               //
25 //                                                                      //
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.                                  //
43 //                                                                      //
44 //////////////////////////////////////////////////////////////////////////
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49 #include <errno.h>
50
51 #include <TSystem.h>
52 #include <TError.h>
53 #include <TStopwatch.h>
54
55 #ifdef ALI_DATE
56 #include "event.h"
57 #endif
58 #ifdef USE_EB
59 #include "libDateEb.h"
60 #endif
61
62 #ifdef USE_HLT
63 #include <AliL3StandardIncludes.h>
64 #include "AliL3Logging.h"
65 #include <AliL3Transform.h>
66 #include "AliRawReaderRoot.h"
67 #include <AliL3Hough.h>
68 #include <AliESD.h>
69 #endif
70
71 #include "AliRawEvent.h"
72 #include "AliRawEventHeader.h"
73 #include "AliRawEquipment.h"
74 #include "AliRawEquipmentHeader.h"
75 #include "AliRawData.h"
76 #include "AliStats.h"
77 #include "AliRawDB.h"
78 #include "AliRawRFIODB.h"
79 #include "AliRawCastorDB.h"
80 #include "AliRawRootdDB.h"
81 #include "AliRawNullDB.h"
82 #include "AliTagDB.h"
83
84 #include "AliMDC.h"
85
86
87 ClassImp(AliMDC)
88
89
90 #define ALIDEBUG(level) \
91    if (AliMDC::Instance() && (AliMDC::Instance()->GetDebugLevel() >= (level)))
92
93
94 // Fixed file system locations for the different DB's
95 #ifdef USE_RDM
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";
105 #else
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";
115 #endif
116
117 // Maximum size of tag db files
118 const Double_t AliMDC::fgkMaxTagFileSize = 2.5e8;    // 250MB
119
120 Bool_t AliMDC::fgDeleteFiles = kFALSE;
121 AliMDC* AliMDC::fgInstance = NULL;
122
123
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)
127 {
128    // Create MDC processor object.
129
130    fFd           = fd;
131    fCompress     = compress;
132    fMaxFileSize  = maxFileSize;
133    fUseFilter    = useFilter;
134    fWriteMode    = mode;
135    fUseLoop      = useLoop;
136    fUseFifo      = kFALSE;
137    fUseEb        = kFALSE;
138    fStopLoop     = kFALSE;
139    fNumEvents    = 0;
140    fDebugLevel   = 0;
141    fgDeleteFiles = delFiles;
142
143    if (fFd == -1) {
144 #ifdef USE_EB
145      if (!ebRegister()) {
146         Error("AliMDC", "cannot register with the event builder (%s)",
147               ebGetLastError());
148         return;
149      }
150      fUseEb = kTRUE;
151 #else
152      if ((mkfifo(fgkFifo, 0644) < 0) && (errno != EEXIST)) {
153          Error("AliMDC", "cannot create fifo %s", fgkFifo);
154          return;
155       }
156       if ((chmod(fgkFifo, 0666) == -1) && (errno != EPERM)) {
157          Error("AliMDC", "cannot change permission of fifo %s", fgkFifo);
158          return;
159       }
160       if ((fFd = open(fgkFifo, O_RDONLY)) == -1) {
161          Error("AliMDC", "cannot open input file %s", fgkFifo);
162          return;
163       }
164       fUseFifo = kTRUE;
165 #endif
166       fUseLoop = kFALSE;
167    }
168
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");
182    else
183       printf("\n");
184
185    // install SIGUSR1 handler to allow clean interrupts
186    gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
187
188    fgInstance = this;
189 }
190
191 //______________________________________________________________________________
192 AliMDC::AliMDC(const AliMDC& mdc): TObject(mdc)
193 {
194 // copy constructor
195
196   Fatal("AliMDC", "copy constructor not implemented");
197 }
198
199 //______________________________________________________________________________
200 AliMDC& AliMDC::operator = (const AliMDC& /*mdc*/)
201 {
202 // assignment operator
203
204   Fatal("operator =", "assignment operator not implemented");
205   return *this;
206 }
207
208 //______________________________________________________________________________
209 Int_t AliMDC::Run()
210 {
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.
214
215    TStopwatch timer;
216    Int_t status;
217
218    // Make sure needed directories exist
219    const char *dirs[4];
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]);
229          return 1;
230       }
231    }
232
233    // Used for statistics
234    timer.Start();
235    Double_t told = 0, tnew = 0;
236    Float_t  chunkSize = fMaxFileSize/100, nextChunk = chunkSize;
237
238    // Event object used to store event data.
239    AliRawEvent *event = new AliRawEvent;
240 #ifdef USE_HLT
241    //Init HLT
242    AliL3Log::fgLevel=AliL3Log::kError;
243    ALIDEBUG(1)
244      AliL3Log::fgLevel=AliL3Log::kWarning;
245    ALIDEBUG(2)
246      AliL3Log::fgLevel=AliL3Log::kWarning;
247    ALIDEBUG(3)
248      AliL3Log::fgLevel=AliL3Log::kNone;
249
250    if (!AliL3Transform::Init("./", kFALSE)) {
251      Error("Run","HLT initialization failed!");
252      return 1;
253    }
254
255    AliESD *esd = new AliESD;
256 #endif
257
258    // Create new raw DB.
259    AliRawDB *rawdb;
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);
268    else
269       rawdb = new AliRawDB(event, esd, fMaxFileSize, fCompress);
270
271    if (rawdb->IsZombie()) return 1;
272    printf("Filling raw DB %s\n", rawdb->GetDBName());
273
274    // Create new tag DB.
275    AliTagDB *tagdb = 0;
276 #if 0
277    // no tagdb for the time being to get maximum speed
278    if (fWriteMode == fgkDEVNULL)
279       tagdb = new AliTagNullDB(event->GetHeader(), fgkMaxTagFileSize);
280    else
281       tagdb = new AliTagDB(event->GetHeader(), fgkMaxTagFileSize);
282    if (tagdb->IsZombie())
283       tagdb = 0;
284    else
285       printf("Filling tag DB %s\n", tagdb->GetDBName());
286 #endif
287
288    // Create AliStats object
289    AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
290
291    // Shortcut for easy header access
292    AliRawEventHeader &header = *event->GetHeader();
293
294    // Process input stream
295 #ifdef USE_EB
296    Int_t eorFlag = 0;
297    while (!(eorFlag = ebEor())) {
298       struct iovec *ebvec;
299       if ((ebvec = ebGetNextEvent()) == (void *)-1) {
300          Error("Run", "error getting next event (%s)", ebGetLastError());
301          break;
302       }
303       if (ebvec == 0) {
304          // no event, sleep for 1 second and try again
305          gSystem->Sleep(1000);
306          continue;
307       }
308       char *ebdata = (char *) ebvec[0].iov_base;
309 #else
310    while (1) {
311       char *ebdata = 0;
312 #endif
313
314       // Read event header
315       if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
316          if (status == 0) {
317             if (fUseLoop) {
318 #ifndef USE_EB
319                ::lseek(fFd, 0, SEEK_SET);
320 #endif
321                continue;
322             }
323             printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
324             break;
325          }
326          return 1;
327       }
328       ALIDEBUG(3)
329          header.Dump();
330
331       // If we were in looping mode stop directly after a SIGUSR1 signal
332       if (StopLoop()) {
333          Info("Run", "Stopping loop, processed %d events", fNumEvents);
334          break;
335       }
336
337       // Check if event has any hard track flagged
338       Bool_t callFilter = kFALSE;
339       if (fUseFilter)
340         callFilter = kTRUE;
341
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:
349             {
350                Int_t skip = header.GetEventSize() - header.HeaderSize();
351 #ifndef USE_EB
352                ::lseek(fFd, skip, SEEK_CUR);
353 #endif
354                ALIDEBUG(1)
355                   Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
356                continue;
357             }
358          default:
359             ALIDEBUG(1) {
360                Int_t s = header.GetEventSize() - header.HeaderSize();
361                Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
362             }
363       }
364
365       // Amount of data left to read for this event
366       Int_t toRead = header.GetEventSize() - header.HeaderSize();
367
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()) {
371          ALIDEBUG(1) {
372             Warning("Run",
373                     "header size (%d) exceeds number of bytes to read (%d)\n",
374                     header.HeaderSize(), toRead);
375             header.Dump();
376          }
377          if ((status = DumpEvent(toRead)) != toRead) {
378             if (status == 0)
379                break;
380             return 1;
381          }
382          Error("Run", "discarding event %d (too little data for header)", fNumEvents);
383          continue;
384       }
385
386       // Loop over all sub-events... (LDCs)
387       Int_t nsub = 1;
388       while (toRead > 0) {
389 #ifdef USE_EB
390          ebdata = (char *)ebvec[nsub].iov_base;
391 #endif
392
393          ALIDEBUG(1)
394             Info("Run", "reading LDC %d", nsub);
395
396          AliRawEvent *subEvent = event->NextSubEvent();
397
398          // Read sub-event header
399          AliRawEventHeader &subHeader = *subEvent->GetHeader();
400          if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
401             if (status == 0) {
402                Error("Run", "unexpected EOF reading sub-event header");
403                break;
404             }
405             return 1;
406          }
407
408          ALIDEBUG(3)
409             subHeader.Dump();
410
411          toRead -= subHeader.HeaderSize();
412
413 #ifdef USE_EB
414          ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
415 #endif
416
417          Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
418
419          // Make sure raw data less than left over bytes for current event
420          if (rawSize > toRead) {
421             ALIDEBUG(1) {
422                Warning("Run", "raw data size (%d) exceeds number of "
423                        "bytes to read (%d)\n", rawSize, toRead);
424                subHeader.Dump();
425             }
426             if ((status = DumpEvent(toRead)) != toRead) {
427                if (status == 0)
428                   break;
429                return 1;
430             }
431             Error("Run", "discarding event %d (too much data)", fNumEvents);
432             continue;
433          }
434
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) {
445                   if (status == 0) {
446                      Error("Run", "unexpected EOF reading equipment-header");
447                      break;
448                   }
449                   return 1;
450                }
451                toRead  -= equipHeaderSize;
452                rawSize -= equipHeaderSize;
453 #ifdef USE_EB
454                ebdata = (char *)(ebvec[nsub].iov_base) +
455                         subHeader.HeaderSize() + equipHeaderSize;
456 #endif
457
458                // Read equipment raw data
459                AliRawData &subRaw = *equipment.GetRawData();
460
461                Int_t eqSize = equipmentHeader.GetEquipmentSize() -
462                                equipHeaderSize;
463                if ((status = ReadRawData(subRaw, eqSize, ebdata)) != eqSize) {
464                   if (status == 0) {
465                      Error("Run", "unexpected EOF reading sub-event raw data");
466                      break;
467                   }
468                   return 1;
469                }
470                toRead  -= eqSize;
471                rawSize -= eqSize;
472
473             }
474
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) {
479                if (status == 0) {
480                   Error("Run", "unexpected EOF reading sub-event raw data");
481                   break;
482                }
483                return 1;
484             }
485             toRead  -= rawSize;
486
487          }
488
489          nsub++;
490       }
491
492       //HLT
493       if (callFilter) {
494 #ifdef ALI_DATE
495         if(header.GetType() == AliRawEventHeader::kPhysicsEvent ||
496            header.GetType() == AliRawEventHeader::kCalibrationEvent)
497           Filter(
498 #ifdef USE_HLT
499                  event,esd
500 #endif
501                  );
502 #endif
503       }
504
505       // Set stat info for first event of this file
506       if (rawdb->GetEvents() == 0)
507          stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
508
509       // Store raw event in tree
510       rawdb->Fill();
511
512       // Store header in tree
513       if (tagdb) tagdb->Fill();
514
515       fNumEvents++;
516
517       if (!(fNumEvents%10))
518          printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
519
520       // Filling time statistics
521       if (rawdb->GetBytesWritten() > nextChunk) {
522          tnew = timer.RealTime();
523          stats->Fill(tnew-told);
524          told = tnew;
525          timer.Continue();
526          nextChunk += chunkSize;
527       }
528
529       // Check size of raw db. If bigger than maxFileSize, close file
530       // and continue with new file.
531       if (rawdb->FileFull()) {
532
533          printf("Written raw DB at a rate of %.1f MB/s\n",
534                 rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
535
536          // Write stats object to raw db, run db, MySQL and AliEn
537          stats->WriteToDB(rawdb);
538          delete stats;
539
540          if (!rawdb->NextFile()) {
541             Error("Run", "error opening next raw data file");
542             return 1;
543          }
544
545          printf("Filling raw DB %s\n", rawdb->GetDBName());
546          stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
547
548          timer.Start();
549          told = 0, tnew = 0;
550          nextChunk = chunkSize;
551       }
552
553       // Check size of tag db
554       if (tagdb && tagdb->FileFull()) {
555          if (!tagdb->NextFile())
556             tagdb = 0;
557          else
558             printf("Filling tag DB %s\n", tagdb->GetDBName());
559       }
560
561       // Make top event object ready for next event data
562       //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
563       event->Reset();
564 #ifdef USE_HLT
565       // Clean up HLT ESD for the next event
566       // Probably we could add esd->Reset() method to AliESD?
567       esd->Reset();
568 #endif
569 #ifdef USE_EB
570       if (!ebReleaseEvent(ebvec)) {
571          Error("Run", "problem releasing event (%s)", ebGetLastError());
572          break;
573       }
574 #endif
575    }
576
577    printf("Written raw DB at a rate of %.1f MB/s\n",
578           rawdb->GetBytesWritten() / timer.RealTime() / 1000000.);
579
580    // Write stats to raw db and run db and delete stats object
581    stats->WriteToDB(rawdb);
582    delete stats;
583
584    // Close the raw DB
585    delete rawdb;
586
587    // Close the tag DB
588    delete tagdb;
589
590    // Close input source
591    close(fFd);
592
593 #if 0
594    // Cleanup fifo
595    if (fUseFifo && ::unlink(fgkFifo) == -1) {
596       SysError("Run", "unlink");
597       return 1;
598    }
599 #endif
600
601 #ifdef USE_EB
602    // Print eor flag
603    if (eorFlag) {
604       Info("Run", "event builder reported end of run (%d)", eorFlag);
605    }
606 #endif
607
608    return 0;
609 }
610
611 //______________________________________________________________________________
612 Int_t AliMDC::Read(void *buffer, Int_t length)
613 {
614    // Read exactly length bytes into buffer. Returns number of bytes
615    // received, returns -1 in case of error and 0 for EOF.
616
617    errno = 0;
618
619    if (fFd < 0) return -1;
620
621    Int_t n, nrecv = 0;
622    char *buf = (char *)buffer;
623
624    for (n = 0; n < length; n += nrecv) {
625       if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
626          if (nrecv == 0)
627             break;        // EOF
628          if (errno != EINTR)
629             SysError("Read", "read");
630          return -1;
631       }
632    }
633    return n;
634 }
635
636 //______________________________________________________________________________
637 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
638 {
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.
641
642    Int_t nrecv;
643
644    if (eb) {
645       // read from event builder memory area
646       memcpy(header.HeaderBegin(), eb, header.HeaderSize());
647       nrecv = header.HeaderSize();
648    } else {
649       // read from fifo or file
650       if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
651            header.HeaderSize()) {
652          if (nrecv == 0)
653             return 0;
654          return -1;
655       }
656    }
657
658    // Swap header data if needed
659    if (header.IsSwapped())
660       header.Swap();
661
662    // Is header valid...
663    if (!header.IsValid()) {
664       Error("ReadHeader", "invalid header format");
665       // try recovery... how?
666       return -1;
667    }
668    if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
669       Error("ReadHeader", "invalid header size");
670       // try recovery... how?
671       return -1;
672    }
673
674    return nrecv;
675 }
676
677 //______________________________________________________________________________
678 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
679                                   Bool_t isSwapped, void *eb)
680 {
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.
685
686    Int_t nrecv;
687
688    if (eb) {
689       // read from event builder memory area
690       memcpy(header.HeaderBegin(), eb, header.HeaderSize());
691       nrecv = header.HeaderSize();
692    } else {
693       // read from fifo or file
694       if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
695            header.HeaderSize()) {
696          if (nrecv == 0)
697             return 0;
698          return -1;
699       }
700    }
701
702    // Swap equipment header data if needed
703    if (isSwapped)
704       header.Swap();
705
706    if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
707       Error("ReadEquipmentHeader", "invalid equipment header size");
708       // try recovery... how?
709       return -1;
710    }
711
712    return nrecv;
713 }
714
715 //______________________________________________________________________________
716 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
717 {
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.
720
721    Int_t nrecv;
722
723    if (eb) {
724       // read from event builder memory area
725       raw.SetBuffer(eb, size);
726       nrecv = size;
727    } else {
728       // read from fifo or file
729       raw.SetSize(size);
730       if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
731          if (nrecv == 0) {
732             Error("ReadRawData", "unexpected EOF");
733             return 0;
734          }
735          return -1;
736       }
737    }
738
739    return nrecv;
740 }
741
742 //______________________________________________________________________________
743 Int_t AliMDC::DumpEvent(Int_t toRead)
744 {
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.
748
749    Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
750
751    Int_t nrecv;
752    char *tbuf = new char[toRead];
753    if ((nrecv = Read(tbuf, toRead)) != toRead) {
754       if (nrecv == 0) {
755          Error("DumpEvent", "unexpected EOF");
756          return 0;
757       }
758       return -1;
759    }
760    delete [] tbuf;
761
762    return nrecv;
763 }
764
765 //______________________________________________________________________________
766 Int_t AliMDC::Filter(
767 #ifdef USE_HLT
768                      AliRawEvent *event,AliESD *esd
769 #endif
770                      )
771 {
772   // Call 3rd level filter for this raw data event.
773
774 #ifdef USE_HLT
775
776   // Run the HLT code
777   {
778     TStopwatch timer;
779     timer.Start();
780
781     AliL3Hough *hough1 = new AliL3Hough();
782     
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();
790
791     AliL3Hough *hough2 = new AliL3Hough();
792
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();
798
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);
803
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! ");
808
809     /* In case we run HLT code in the main thread
810     for(Int_t slice=0; slice<=17; slice++)
811       {
812         hough1->ReadData(slice,0);
813         hough1->Transform();
814         hough1->AddAllHistogramsRows();
815         hough1->FindTrackCandidatesRow();
816         hough1->AddTracks();
817       }
818     for(Int_t slice=18; slice<=35; slice++)
819       {
820         hough2->ReadData(slice,0);
821         hough2->Transform();
822         hough2->AddAllHistogramsRows();
823         hough2->FindTrackCandidatesRow();
824         hough2->AddTracks();
825       }
826     */
827
828     nglobaltracks += hough1->FillESD(esd);
829     nglobaltracks += hough2->FillESD(esd);
830
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");
836     */
837
838     delete hough1;
839     delete hough2;
840
841     printf("Filter called for event %d\n", fNumEvents);
842     printf("Filter has found %d TPC tracks in %f seconds\n", nglobaltracks,timer.RealTime());
843   }
844
845 #else
846
847   printf("Filter called for event %d\n", fNumEvents);
848
849 #endif
850
851   return 0;
852 }
853
854 //______________________________________________________________________________
855 AliMDC::AliMDCInterruptHandler::AliMDCInterruptHandler(const 
856                                                        AliMDCInterruptHandler&
857                                                        handler): 
858   TSignalHandler(handler) 
859 {
860 // copy constructor
861
862   Fatal("AliMDCInterruptHandler", "copy constructor not implemented");
863 }
864
865 //______________________________________________________________________________
866 AliMDC::AliMDCInterruptHandler& 
867   AliMDC::AliMDCInterruptHandler::operator = (const AliMDCInterruptHandler& 
868                                               /*handler*/)
869 {
870 // assignment operator
871
872   Fatal("operator =", "assignment operator not implemented");
873   return *this;
874 }