1 // @(#)alimdc:$Name$:$Id$
2 // Author: Fons Rademakers 26/11/99
3 // Updated: Dario Favretto 15/04/2003
17 #include <TStopwatch.h>
18 #include <TSQLServer.h>
19 #include <TSQLResult.h>
21 #include "AliRawEvent.h"
25 #include "DateEvent.h"
28 #include "libDateEb.h"
33 ClassImp(AliRawEventHeader)
34 ClassImp(AliRawEquipmentHeader)
38 ClassImp(AliRawRFIODB)
39 ClassImp(AliRawRootdDB)
40 ClassImp(AliRawNullDB)
42 ClassImp(AliTagNullDB)
46 // Which MDC is this...
49 // Fixed file system locations for the different DB's
51 const char *kFifo = "/tmp/alimdc.fifo";
52 const char *kRawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
53 const char *kTagDBFS = "/tmp/mdc1/tags";
54 const char *kRunDBFS = "/tmp/mdc1/meta";
55 const char *kRFIOFS = "rfio:/castor/cern.ch/user/r/rdm";
56 const char *kRootdFS = "root://localhost//tmp/mdc1";
58 const char *kFifo = "/tmp/alimdc.fifo";
59 const char *kRawDBFS[2] = { "/scratch/mdc1", "/scratch/mdc2" };
60 const char *kTagDBFS = "/scratch/mdc1/tags";
61 const char *kRunDBFS = "/scratch/mdc1/meta";
62 const char *kRFIOFS = "rfio:/castor/cern.ch/lcg/alicemdc4";
63 const char *kRootdFS = "root://localhost//tmp/mdc1";
66 // Maximum size of tag db files
67 const Double_t kMaxTagFileSize = 2.5e8; // 250MB
69 Bool_t AliMDC::fgDeleteFiles = kFALSE;
74 //______________________________________________________________________________
75 Bool_t AliRawEventHeader::DataIsSwapped() const
77 // Returns true if event data is swapped.
79 if (TEST_SYSTEM_ATTRIBUTE(fTypeAttribute, ATTR_EVENT_SWAPPED))
84 //______________________________________________________________________________
85 void AliRawEventHeader::Swap()
90 fSize = net2host(fSize);
91 fMagic = net2host(fMagic);
92 fHeadLen = net2host(fHeadLen);
93 fVersion = net2host(fVersion);
94 fType = net2host(fType);
95 fRunNb = net2host(fRunNb);
96 for (int i = 0; i < kIdWords; i++)
97 fId[i] = net2host(fId[i]);
98 for (int i = 0; i < kTriggerWords; i++)
99 fTriggerPattern[i] = net2host(fTriggerPattern[i]);
100 for (int i = 0; i < kDetectorWords; i++)
101 fDetectorPattern[i] = net2host(fDetectorPattern[i]);
102 for (int i = 0; i < kAttributeWords; i++)
103 fTypeAttribute[i] = net2host(fTypeAttribute[i]);
104 fLDCId = net2host(fLDCId);
105 fGDCId = net2host(fGDCId);
109 //______________________________________________________________________________
110 UInt_t AliRawEventHeader::GetEventInRun() const
112 // Get event number in run. Correct for fixed target mode which is used
113 // in the Data Challenge Setup.
115 if (!TEST_SYSTEM_ATTRIBUTE(fTypeAttribute, ATTR_ORBIT_BC)) {
116 return EVENT_ID_GET_NB_IN_RUN(fId);
121 //______________________________________________________________________________
122 const char *AliRawEventHeader::GetTypeName() const
124 // Get event type as a string.
128 return "START_OF_RUN";
133 case kStartOfRunFiles:
134 return "START_OF_RUN_FILES";
137 return "END_OF_RUN_FILES";
140 return "START_OF_BURST";
143 return "END_OF_BURST";
146 return "PHYSICS_EVENT";
148 case kCalibrationEvent:
149 return "CALIBRATION_EVENT";
152 return "EVENT_FORMAT_ERROR";
155 return "*** UNKNOWN EVENT TYPE ***";
161 //______________________________________________________________________________
162 void AliRawEquipmentHeader::Swap()
164 // Swap equipment header data. There is no way to see if the data
165 // has already been swapped. This method is only called when the
166 // header is read from the DATE event builder (GDC).
168 fSize = net2host(fSize);
169 fEquipmentType = net2host(fEquipmentType);
170 fEquipmentID = net2host(fEquipmentID);
171 fBasicElementSizeType = net2host(fBasicElementSizeType);
172 for (int i = 0; i < kAttributeWords; i++)
173 fTypeAttribute[i] = net2host(fTypeAttribute[i]);
177 //______________________________________________________________________________
178 AliRawEvent::AliRawEvent()
180 // Create ALICE event object. If ownData is kFALSE we will use a static
181 // raw data object, otherwise a private copy will be made.
184 fEvtHdr = new AliRawEventHeader;
190 //______________________________________________________________________________
191 AliRawEquipmentHeader *AliRawEvent::GetEquipmentHeader()
193 // Get equipment header part of AliRawEvent.
196 fEqpHdr = new AliRawEquipmentHeader;
201 //______________________________________________________________________________
202 AliRawData *AliRawEvent::GetRawData()
204 // Get raw data part of AliRawEvent.
207 fRawData = new AliRawData;
212 //______________________________________________________________________________
213 AliRawEvent *AliRawEvent::NextSubEvent()
215 // Returns next sub-event object.
218 fSubEvents = new TObjArray(100); // arbitrary, probably enough to prevent resizing
220 if (fSubEvents->GetSize() <= fNSubEvents) {
221 fSubEvents->Expand(fNSubEvents+10);
222 Warning("NextSubEvent", "expanded fSubEvents by 10 to %d",
223 fSubEvents->GetSize());
227 if (!(ev = (AliRawEvent *)fSubEvents->At(fNSubEvents))) {
228 ev = new AliRawEvent;
229 fSubEvents->AddAt(ev, fNSubEvents);
237 //______________________________________________________________________________
238 AliRawEvent *AliRawEvent::GetSubEvent(Int_t index)
240 return (AliRawEvent *)fSubEvents->At(index);
243 //______________________________________________________________________________
244 void AliRawEvent::Reset()
246 // Reset the event in case it needs to be re-used (avoiding costly
247 // new/delete cycle). We reset the size marker for the AliRawData
248 // objects and the sub event counter.
250 for (int i = 0; i < fNSubEvents; i++) {
251 AliRawEvent *ev = (AliRawEvent *)fSubEvents->At(i);
252 ev->GetRawData()->SetSize(0);
257 //______________________________________________________________________________
258 AliRawEvent::~AliRawEvent()
260 // Clean up event object. Delete also, possible, private raw data.
266 fSubEvents->Delete();
270 //______________________________________________________________________________
271 AliStats::AliStats(const char *filename, Int_t compmode, Bool_t filter)
273 // Create statistics object.
281 fFileName = filename;
282 fCompMode = compmode;
287 //______________________________________________________________________________
288 AliStats::~AliStats()
290 // Cleanup stats object.
295 //______________________________________________________________________________
296 AliStats &AliStats::operator=(const AliStats &rhs)
298 // AliStats assignment operator.
301 TObject::operator=(rhs);
302 fEvents = rhs.fEvents;
303 fFirstRun = rhs.fFirstRun;
304 fFirstEvent = rhs.fFirstEvent;
305 fLastRun = rhs.fLastRun;
306 fLastEvent = rhs.fLastEvent;
309 fFileName = rhs.fFileName;
310 fFileSize = rhs.fFileSize;
311 fCompFactor = rhs.fCompFactor;
312 fCompMode = rhs.fCompMode;
313 fFilter = rhs.fFilter;
314 fRTHist = rhs.fRTHist ? (TH1F*) rhs.fRTHist->Clone() : 0;
320 //______________________________________________________________________________
321 void AliStats::Fill(Float_t time)
323 // Fill histogram. This histogram shows the (hopefully constant) time
324 // it takes to fill the ROOT DB.
325 // Expects to be called 100 times for each file.
328 fRTHist = new TH1F("rtime","Real-time to write data chunk", 100, 0, 100);
329 fRTHist->SetDirectory(0);
332 fRTHist->Fill(fChunk, time);
336 //______________________________________________________________________________
337 void AliStats::WriteToDB(AliRawDB *rawdb)
339 // Write stats to raw DB, local run DB and global MySQL DB.
341 AliRawEventHeader &header = *rawdb->GetEvent()->GetHeader();
343 // Write stats into RawDB
344 TDirectory *ds = gDirectory;
345 rawdb->GetDB()->cd();
346 SetEvents(rawdb->GetEvents());
347 SetLastId(header.GetRunNumber(), header.GetEventInRun());
348 SetFileSize(rawdb->GetBytesWritten());
349 SetCompressionFactor(rawdb->GetCompressionFactor());
354 // Write stats also in the bookkeeping RunDB
355 AliRunDB *rundb = new AliRunDB;
357 rundb->UpdateRDBMS(this);
361 //______________________________________________________________________________
362 AliRawDB::AliRawDB(AliRawEvent *event, Double_t maxsize, Int_t compress,
365 // Create a new raw DB containing at most maxsize bytes.
369 fCompress = compress;
371 // Consistency check with DATE header file
372 if (fEvent->GetHeader()->HeaderSize() != EVENT_HEAD_BASE_SIZE) {
373 Error("AliRawDB", "inconsistency between DATE and AliRawEvent headers");
384 //______________________________________________________________________________
385 Bool_t AliRawDB::FSHasSpace(const char *fs)
387 // Check for at least fMaxSize bytes of free space on the file system.
388 // If the space is not available return kFALSE, kTRUE otherwise.
390 Long_t id, bsize, blocks, bfree;
392 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
393 Error("FSHasSpace", "could not stat file system %s", fs);
397 // Leave 5 percent of diskspace free
398 Double_t avail = Double_t(bfree) * 0.95;
399 if (avail*bsize > fMaxSize)
402 Warning("FSHasSpace", "no space on file system %s", fs);
406 //______________________________________________________________________________
407 const char *AliRawDB::GetFileName()
409 // Return filename based on hostname and date and time. This will make
410 // each file unique. Also makes sure (via FSHasSpace()) that there is
411 // enough space on the file system to store the file. Returns 0 in
412 // case of error or interrupt signal.
414 static TString fname;
415 static Bool_t fstoggle = kFALSE;
417 TString fs = fstoggle ? kRawDBFS[1] : kRawDBFS[0];
420 TString hostname = gSystem->HostName();
422 if ((pos = hostname.Index(".")) != kNPOS)
423 hostname.Remove(pos);
425 if (!FSHasSpace(fs)) {
427 fstoggle = !fstoggle;
428 fs = fstoggle ? kRawDBFS[1] : kRawDBFS[0];
429 if (FSHasSpace(fs)) break;
430 Info("GetFileName", "sleeping 30 seconds before retrying...");
431 gSystem->Sleep(30000); // sleep for 30 seconds
432 if (gAliMDC && gAliMDC->StopLoop())
437 fname = fs + "/" + hostname + "_";
438 fname += dt.GetDate();
440 fname += dt.GetTime();
443 fstoggle = !fstoggle;
448 //______________________________________________________________________________
449 Bool_t AliRawDB::Create()
451 // Create a new raw DB.
454 const char *fname = GetFileName();
455 if (!fname) return kFALSE;
457 fRawDB = TFile::Open(fname, "RECREATE",
458 Form("ALICE MDC%d raw DB", kMDC), fCompress);
460 Error("Create", "did not find right plugin to open file");
463 if (fRawDB->IsZombie()) {
464 if (fRawDB->GetErrno() == ENOSPC ||
465 fRawDB->GetErrno() == 1018 || // SECOMERR
466 fRawDB->GetErrno() == 1027) { // SESYSERR
467 fRawDB->ResetErrno();
469 Warning("Create", "file is zombie, sleeping 10 seconds before retrying...");
470 gSystem->Sleep(10000); // sleep 10 seconds before retrying
473 Error("Create", "error opening raw DB");
478 // Create raw data TTree
484 //______________________________________________________________________________
485 void AliRawDB::MakeTree()
487 // Create ROOT Tree object container.
489 fTree = new TTree("RAW", Form("ALICE MDC%d raw data tree", kMDC));
490 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
492 Int_t bufsize = 256000;
493 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
496 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
499 //______________________________________________________________________________
500 void AliRawDB::Close()
511 // Close DB, this also deletes the fTree
514 if (AliMDC::DeleteFiles()) {
515 gSystem->Unlink(fRawDB->GetName());
521 // Create semaphore to say this file is finished
522 Int_t tfd = ::creat(Form("%s.done", fRawDB->GetName()), 0644);
529 //______________________________________________________________________________
530 Bool_t AliRawDB::NextFile()
532 // Close te current file and open a new one.
533 // Returns kFALSE in case opening failed.
537 if (!Create()) return kFALSE;
541 //______________________________________________________________________________
542 Float_t AliRawDB::GetCompressionFactor() const
544 // Return compression factor.
546 if (fTree->GetZipBytes() == 0.)
549 return fTree->GetTotBytes()/fTree->GetZipBytes();
553 //______________________________________________________________________________
554 AliRawRFIODB::AliRawRFIODB(AliRawEvent *event, Double_t maxsize, Int_t compress)
555 : AliRawDB(event, maxsize, compress, kFALSE)
557 // Create a new raw DB that will be accessed via RFIO.
561 // Set STAGE_POOL environment variable to current host
563 // THESE ENVIRONMENT SYMBOLS ARE NOW DEFINED BY THE ALICE DATE SETUP
564 // THEREFORE WE SHALL NOT USE ANY HARDCODED VALUES BUT RATHER USE
565 // WHATEVER HAS BEEN SET IN THE DATE SITE
566 //gSystem->Setenv("STAGE_POOL", "mdc4");
567 //gSystem->Setenv("STAGE_HOST", "lxshare003d");
575 fRawDB->UseCache(50, 0x200000); //0x100000 = 1MB)
578 //______________________________________________________________________________
579 const char *AliRawRFIODB::GetFileName()
581 // Return filename based on hostname and date and time. This will make
582 // each file unique. Also the directory will be made unique for each
583 // day by adding the date to the fs. Assumes there is always enough
584 // space on the device.
586 static TString fname;
588 TString fs = kRFIOFS;
591 // make a new subdirectory for each day
595 Long_t id, size, flags, time;
596 if (gSystem->GetPathInfo(fs, &id, &size, &flags, &time) == 1) {
597 // directory does not exist, create it
598 if (gSystem->mkdir(fs, kTRUE) == -1) {
599 Error("GetFileName", "cannot create dir %s, using %s", fs.Data(),
604 // FIXME: should check if fs is a directory
606 TString hostname = gSystem->HostName();
608 if ((pos = hostname.Index(".")) != kNPOS)
609 hostname.Remove(pos);
611 fname = fs + "/" + hostname + "_";
612 fname += dt.GetDate();
614 fname += dt.GetTime();
620 //______________________________________________________________________________
621 void AliRawRFIODB::Close()
623 // Close raw RFIO DB.
632 // Close DB, this also deletes the fTree
635 if (AliMDC::DeleteFiles())
636 gSystem->Exec(Form("rfrm %s", fRawDB->GetName()));
643 //______________________________________________________________________________
644 AliRawRootdDB::AliRawRootdDB(AliRawEvent *event, Double_t maxsize, Int_t compress)
645 : AliRawDB(event, maxsize, compress, kFALSE)
647 // Create a new raw DB that will be accessed via rootd daemon.
652 fRawDB->UseCache(50, 0x200000); //0x100000 = 1MB)
655 //______________________________________________________________________________
656 const char *AliRawRootdDB::GetFileName()
658 // Return filename based on hostname and date and time. This will make
659 // each file unique. Also the directory will be made unique for each
660 // day by adding the date to the fs. Assumes there is always enough
661 // space on the device.
663 static TString fname;
665 TString fs = kRootdFS;
669 // make a new subdirectory for each day
673 Long_t id, size, flags, time;
674 if (gSystem->GetPathInfo(fs, &id, &size, &flags, &time) == 1) {
675 // directory does not exist, create it
676 if (gSystem->mkdir(fs, kTRUE) == -1) {
677 Error("GetFileName", "cannot create dir %s, using %s", fs.Data(),
682 // FIXME: should check if fs is a directory
685 TString hostname = gSystem->HostName();
687 if ((pos = hostname.Index(".")) != kNPOS)
688 hostname.Remove(pos);
690 fname = fs + "/" + hostname + "_";
691 fname += dt.GetDate();
693 fname += dt.GetTime();
699 //______________________________________________________________________________
700 void AliRawRootdDB::Close()
702 // Close raw rootd DB.
711 // Close DB, this also deletes the fTree
715 // can use services of TFTP
716 if (AliMDC::DeleteFiles())
717 gSystem->Exec(Form("rfrm %s", fRawDB->GetName()));
725 //______________________________________________________________________________
726 AliRawNullDB::AliRawNullDB(AliRawEvent *event, Double_t maxsize, Int_t compress)
727 : AliRawDB(event, maxsize, compress, kFALSE)
729 // Create a new raw DB that will wrtie to /dev/null.
735 //______________________________________________________________________________
736 const char *AliRawNullDB::GetFileName()
738 // Return /dev/null as filename.
743 //______________________________________________________________________________
744 void AliRawNullDB::Close()
746 // Close raw RFIO DB.
755 // Close DB, this also deletes the fTree
763 //______________________________________________________________________________
764 AliTagDB::AliTagDB(AliRawEventHeader *header, Double_t maxsize, Bool_t create)
777 //______________________________________________________________________________
778 Bool_t AliTagDB::Create()
780 // Create a new tag DB.
782 fTagDB = new TFile(GetFileName(), "RECREATE",
783 Form("ALICE MDC%d tag DB", kMDC), 1);
784 if (fTagDB->IsZombie()) {
785 Error("Create", "error opening tag DB");
790 // Create ROOT Tree object container
791 fTree = new TTree("TAG", Form("ALICE MDC%d header data tree", kMDC));
792 fTree->SetAutoSave(100000000); // autosave when 100 Mbyte written
794 Int_t bufsize = 32000;
796 fTree->Branch("header", "AliRawEventHeader", &fHeader, bufsize, split);
801 //______________________________________________________________________________
802 void AliTagDB::Close()
813 // Close DB, this also deletes the fTree
816 if (AliMDC::DeleteFiles())
817 gSystem->Unlink(fTagDB->GetName());
823 //______________________________________________________________________________
824 Bool_t AliTagDB::NextFile()
826 // Close te current file and open a new one.
827 // Returns kFALSE in case opening failed.
831 if (!Create()) return kFALSE;
835 //______________________________________________________________________________
836 Float_t AliTagDB::GetCompressionFactor() const
838 // Return compression factor.
840 if (fTree->GetZipBytes() == 0.)
843 return fTree->GetTotBytes()/fTree->GetZipBytes();
846 //______________________________________________________________________________
847 const char *AliTagDB::GetFileName()
849 // Return filename based on hostname and date and time. This will make
850 // each file unique. The tags will be stored in the /data1/tags directory.
852 static char fname[64];
853 const char *fs = kTagDBFS;
855 // check that fs exists (crude check fails if fs is a file)
856 gSystem->MakeDirectory(fs);
860 strcpy(hostname, gSystem->HostName());
863 if ((s = strchr(hostname, '.')))
868 sprintf(fname, "%s/%s_%d_%d.root", fs, hostname, dt.GetDate(), dt.GetTime());
874 //______________________________________________________________________________
875 AliTagNullDB::AliTagNullDB(AliRawEventHeader *header, Double_t maxsize) :
876 AliTagDB(header, maxsize, kFALSE)
878 // Create tag db writing to /dev/null.
884 //______________________________________________________________________________
885 const char *AliTagNullDB::GetFileName()
887 // Return /dev/null as filename.
892 //______________________________________________________________________________
893 void AliTagNullDB::Close()
895 // Close null tag DB.
904 // Close DB, this also deletes the fTree
912 //______________________________________________________________________________
915 // Open run database, and get or create tree.
918 char hostname[64], filename[64];
919 const char *fs = kRunDBFS;
921 // check that fs exists (crude check fails if fs is a file)
922 gSystem->MakeDirectory(fs);
924 strcpy(hostname, gSystem->HostName());
927 if ((s = strchr(hostname, '.')))
930 sprintf(filename, "%s/%s_rundb.root", fs, hostname);
932 if (!gSystem->AccessPathName(filename, kFileExists))
933 fRunDB = new TFile(filename, "UPDATE");
935 fRunDB = new TFile(filename, "CREATE", Form("ALICE MDC%d Run DB", kMDC));
938 //______________________________________________________________________________
939 void AliRunDB::Update(AliStats *stats)
941 // Add stats object to database.
943 TDirectory *ds = gDirectory;
947 char *s = (char*)strrchr(stats->GetFileName(), '/');
952 strcpy(sname, stats->GetFileName());
953 s = strchr(sname, '.');
961 //______________________________________________________________________________
962 void AliRunDB::UpdateRDBMS(AliStats *stats)
964 // Add stats object to central MySQL DB.
969 strcpy(bt, stats->GetBeginTime().AsSQLString());
970 strcpy(et, stats->GetEndTime().AsSQLString());
972 sprintf(sql, "INSERT INTO mdc%dcatalog VALUES (0, '%s', %d, "
973 "%d, %d, %d, %d, %d, %d, %.2f, '%s', '%s', '%s')", kMDC,
974 stats->GetFileName(), (int)stats->GetFileSize(), stats->GetEvents(),
975 stats->GetFirstRun(), stats->GetFirstEvent(), stats->GetLastRun(),
976 stats->GetLastEvent(), stats->GetCompressionMode(),
977 stats->GetCompressionFactor(), stats->GetFilterState() ? "on" : "off",
980 // open connection to MySQL server on pcsalo
981 TSQLServer *db = TSQLServer::Connect("mysql://pcsalo.cern.ch/mdc", "alice", "amdc");
983 if (!db || db->IsZombie()) {
984 Error("UpdateRDBMS", "failed to connect to MySQL server on pcsalo");
990 TSQLResult *res = db->Query(sql);
993 Error("UpdateRDBMS", Form("insert into mdc%dcatalog failed", kMDC));
1001 //______________________________________________________________________________
1002 void AliRunDB::Close()
1004 // Close run database.
1010 //----------------- Use SIGUSR1 to interupt endless loop -----------------------
1011 class AliMDCInterruptHandler : public TSignalHandler {
1013 AliMDC *fMDC; // alimdc to signal
1015 AliMDCInterruptHandler(AliMDC *mdc) : TSignalHandler(kSigUser1, kFALSE), fMDC(mdc) { }
1016 Bool_t Notify() { fMDC->SetStopLoop(); return kTRUE; }
1019 //______________________________________________________________________________
1020 AliMDC::AliMDC(Int_t fd, Int_t compress, Double_t maxFileSize, Bool_t useFilter,
1021 Bool_t useRFIO, Bool_t useROOTD, Bool_t useDEVNULL,
1022 Bool_t useLoop, Bool_t delFiles)
1024 // Create MDC processor object.
1027 fCompress = compress;
1028 fMaxFileSize = maxFileSize;
1029 fUseFilter = useFilter;
1031 fUseRootd = useROOTD;
1032 fUseDevNull = useDEVNULL;
1039 fgDeleteFiles = delFiles;
1043 if (!ebRegister()) {
1044 Error("AliMDC", "cannot register with the event builder (%s)",
1050 if ((mkfifo(kFifo, 0644) < 0) && (errno != EEXIST)) {
1051 Error("AliMDC", "cannot create fifo %s", kFifo);
1054 if ((chmod(kFifo, 0666) == -1) && (errno != EPERM)) {
1055 Error("AliMDC", "cannot change permission of fifo %s", kFifo);
1058 if ((fFd = open(kFifo, O_RDONLY)) == -1) {
1059 Error("AliMDC", "cannot open input file %s", kFifo);
1067 printf("<AliMDC::AliMDC>: input = %s, rawdb size = %f, filter = %s, "
1068 "looping = %s, compression = %d, delete files = %s",
1069 fUseFifo ? "fifo" : (fUseEb ? "eb" : "file"), fMaxFileSize,
1070 fUseFilter ? "on" : "off", fUseLoop ? "yes" : "no", fCompress,
1071 fgDeleteFiles ? "yes" : "no");
1073 printf(", use RFIO\n");
1075 printf(", use rootd\n");
1076 else if (fUseDevNull)
1077 printf(", write raw data to /dev/null\n");
1081 // install SIGUSR1 handler to allow clean interrupts
1082 gSystem->AddSignalHandler(new AliMDCInterruptHandler(this));
1087 //______________________________________________________________________________
1090 // Run the MDC processor. Read from the input stream and only return
1091 // when the input gave and EOF or a fatal error occured. On success 0
1092 // is returned, 1 in case of a fatality.
1097 // Make sure needed directories exist
1098 const char *dirs[4];
1099 dirs[0] = kRawDBFS[0];
1100 dirs[1] = kRawDBFS[1];
1103 for (int idir = 0; idir < 4; idir++) {
1104 gSystem->ResetErrno();
1105 gSystem->MakeDirectory(dirs[idir]);
1106 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
1107 SysError("Run", "mkdir %s", dirs[idir]);
1112 // Used for statistics
1114 Double_t told = 0, tnew = 0;
1115 Float_t chunkSize = fMaxFileSize/100, nextChunk = chunkSize;
1117 // Event object used to store event data.
1118 AliRawEvent *event = new AliRawEvent;
1120 // Create new raw DB.
1123 rawdb = new AliRawRFIODB(event, fMaxFileSize, fCompress);
1125 rawdb = new AliRawRootdDB(event, fMaxFileSize, fCompress);
1126 else if (fUseDevNull)
1127 rawdb = new AliRawNullDB(event, fMaxFileSize, fCompress);
1129 rawdb = new AliRawDB(event, fMaxFileSize, fCompress);
1131 if (rawdb->IsZombie()) return 1;
1132 printf("Filling raw DB %s\n", rawdb->GetDBName());
1134 // Create new tag DB.
1137 tagdb = new AliTagNullDB(event->GetHeader(), kMaxTagFileSize);
1139 tagdb = new AliTagDB(event->GetHeader(), kMaxTagFileSize);
1140 if (tagdb->IsZombie())
1143 printf("Filling tag DB %s\n", tagdb->GetDBName());
1145 // Create AliStats object
1146 AliStats *stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
1148 // Shortcut for easy header access
1149 AliRawEventHeader &header = *event->GetHeader();
1151 // Process input stream
1154 struct iovec *ebvec;
1155 if ((ebvec = ebGetNextEvent()) == (void *)-1) {
1156 Error("Run", "error getting next event (%s)", ebGetLastError());
1160 // no event, sleep for 1 second and try again
1161 gSystem->Sleep(1000);
1164 char *ebdata = (char *) ebvec[0].iov_base;
1170 // Read event header
1171 if ((status = ReadHeader(header, ebdata)) != header.HeaderSize()) {
1175 ::lseek(fFd, 0, SEEK_SET);
1179 printf("<AliMDC::Run>: EOF, processed %d events\n", fNumEvents);
1185 // If we were in looping mode stop directly after a SIGUSR1 signal
1187 Info("Run", "SIGUSR1, processed %d events", fNumEvents);
1191 // Check if event has any hard track flagged
1192 Bool_t callFilter = kFALSE;
1193 // This needs to be re-engineered for the next ADC...
1194 //if (fUseFilter && TEST_USER_ATTRIBUTE(header.GetTypeAttribute(), 0))
1195 // callFilter = kTRUE;
1197 // Check event type and skip "Start of Run", "End of Run",
1198 // "Start of Run Files" and "End of Run Files"
1199 switch (header.GetType()) {
1202 case kStartOfRunFiles:
1203 case kEndOfRunFiles:
1205 Int_t skip = header.GetEventSize() - header.HeaderSize();
1207 ::lseek(fFd, skip, SEEK_CUR);
1210 Info("Run", "Skipping %s (%d bytes)", header.GetTypeName(), skip);
1216 Int_t s = header.GetEventSize() - header.HeaderSize();
1217 Info("Run", "Processing %s (%d bytes)", header.GetTypeName(), s);
1221 // Amount of data left to read for this event
1222 Int_t toRead = header.GetEventSize() - header.HeaderSize();
1224 // If there is less data for this event than the next sub-event
1225 // header, something is wrong. Skip to next event...
1226 if (toRead < header.HeaderSize()) {
1227 if ((status = DumpEvent(toRead)) != toRead) {
1232 Error("Run", "discarding event %d (too little data for header)", fNumEvents);
1236 // Loop over all sub-events... (LDCs)
1237 while (toRead > 0) {
1240 ebdata = (char *)ebvec[nsub].iov_base;
1244 Info("Run", "reading LDC %d", nsub);
1246 AliRawEvent *subEvent = event->NextSubEvent();
1248 // Read sub-event header
1249 AliRawEventHeader &subHeader = *subEvent->GetHeader();
1250 if ((status = ReadHeader(subHeader, ebdata)) != subHeader.HeaderSize()) {
1252 Error("Run", "unexpected EOF reading sub-event header");
1258 toRead -= subHeader.HeaderSize();
1261 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize();
1264 Int_t rawSize = subHeader.GetEventSize() - subHeader.HeaderSize();
1266 // Read Equipment Header (in case of physics or calibration event)
1267 if (header.GetType() == kPhysicsEvent ||
1268 header.GetType() == kCalibrationEvent) {
1269 AliRawEquipmentHeader &equipment = *subEvent->GetEquipmentHeader();
1270 Int_t equipHeaderSize = equipment.HeaderSize();
1271 if ((status = ReadEquipmentHeader(equipment, header.DataIsSwapped(),
1272 ebdata)) != equipHeaderSize) {
1274 Error("Run", "unexpected EOF reading equipment-header");
1279 toRead -= equipHeaderSize;
1280 rawSize -= equipHeaderSize;
1282 ebdata = (char *)(ebvec[nsub].iov_base) + subHeader.HeaderSize() +
1287 // Make sure raw data less than left over bytes for current event
1288 if (rawSize > toRead) {
1289 if ((status = DumpEvent(toRead)) != toRead) {
1294 Error("Run", "discarding event %d (too much data)", fNumEvents);
1298 // Read sub-event raw data
1299 AliRawData &subRaw = *subEvent->GetRawData();
1300 if ((status = ReadRawData(subRaw, rawSize, ebdata)) != rawSize) {
1302 Error("Run", "unexpected EOF reading sub-event raw data");
1309 if (TEST_USER_ATTRIBUTE(subHeader.GetTypeAttribute(), 0))
1312 // set size of all sectors without hard track flag to 0
1321 // Set stat info for first event of this file
1322 if (rawdb->GetEvents() == 0)
1323 stats->SetFirstId(header.GetRunNumber(), header.GetEventInRun());
1325 // Store raw event in tree
1328 // Store header in tree
1329 if (tagdb) tagdb->Fill();
1333 if (!(fNumEvents%10))
1334 printf("Processed event %d (%d)\n", fNumEvents, rawdb->GetEvents());
1336 // Filling time statistics
1337 if (rawdb->GetBytesWritten() > nextChunk) {
1338 tnew = timer.RealTime();
1339 stats->Fill(tnew-told);
1342 nextChunk += chunkSize;
1345 // Check size of raw db. If bigger than maxFileSize, close file
1346 // and continue with new file.
1347 if (rawdb->FileFull()) {
1349 printf("Written raw DB at a rate of %.1f MB/s\n",
1350 Float_t(fMaxFileSize / timer.RealTime() / 1000000.));
1352 // Write stats object to raw db, run db and MySQL
1353 stats->WriteToDB(rawdb);
1356 if (!rawdb->NextFile()) return 1;
1358 printf("Filling raw DB %s\n", rawdb->GetDBName());
1359 stats = new AliStats(rawdb->GetDBName(), fCompress, fUseFilter);
1363 nextChunk = chunkSize;
1366 // Check size of tag db
1367 if (tagdb->FileFull()) {
1368 if (!tagdb->NextFile())
1371 printf("Filling tag DB %s\n", tagdb->GetDBName());
1374 // Make top event object ready for next event data
1375 //printf("Event %d has %d sub-events\n", fNumEvents, event->GetNSubEvents());
1379 if (!ebReleaseEvent(ebvec)) {
1380 Error("Run", "problem releasing event (%s)", ebGetLastError());
1386 printf("Written raw DB at a rate of %.1f MB/s\n",
1387 Float_t(fMaxFileSize / timer.RealTime() / 1000000.));
1389 // Write stats to raw db and run db and delete stats object
1390 stats->WriteToDB(rawdb);
1399 // Close input source
1404 if (fUseFifo && ::unlink(kFifo) == -1) {
1405 SysError("Run", "unlink");
1413 //______________________________________________________________________________
1414 Int_t AliMDC::Read(void *buffer, Int_t length)
1416 // Read exactly length bytes into buffer. Returns number of bytes
1417 // received, returns -1 in case of error and 0 for EOF.
1421 if (fFd < 0) return -1;
1424 char *buf = (char *)buffer;
1426 for (n = 0; n < length; n += nrecv) {
1427 if ((nrecv = read(fFd, buf+n, length-n)) <= 0) {
1431 SysError("Read", "read");
1438 //______________________________________________________________________________
1439 Int_t AliMDC::ReadHeader(AliRawEventHeader &header, void *eb)
1441 // Read header info from DATE data stream. Returns bytes read (i.e.
1442 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
1447 // read from event builder memory area
1448 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
1449 nrecv = header.HeaderSize();
1451 // read from fifo or file
1452 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
1453 header.HeaderSize()) {
1460 // Swap header data if needed
1461 if (header.IsSwapped())
1464 // Is header valid...
1465 if (!header.IsValid()) {
1466 Error("ReadHeader", "invalid header format");
1467 // try recovery... how?
1470 if (header.GetEventSize() < (UInt_t)header.HeaderSize()) {
1471 Error("ReadHeader", "invalid header size");
1472 // try recovery... how?
1479 //______________________________________________________________________________
1480 Int_t AliMDC::ReadEquipmentHeader(AliRawEquipmentHeader &header,
1481 Bool_t isSwapped, void *eb)
1483 // Read equipment header info from DATE data stream. Returns bytes read
1484 // (i.e. AliRawEquipmentHeader::HeaderSize()), -1 in case of error and
1485 // 0 for EOF. If isSwapped is kTRUE the event data is byte swapped
1486 // and we will swap the header to host format.
1491 // read from event builder memory area
1492 memcpy(header.HeaderBegin(), eb, header.HeaderSize());
1493 nrecv = header.HeaderSize();
1495 // read from fifo or file
1496 if ((nrecv = Read(header.HeaderBegin(), header.HeaderSize())) !=
1497 header.HeaderSize()) {
1504 // Swap equipment header data if needed
1508 if (header.GetEquipmentSize() < (UInt_t)header.HeaderSize()) {
1509 Error("ReadEquipmentHeader", "invalid equipment header size");
1510 // try recovery... how?
1517 //______________________________________________________________________________
1518 Int_t AliMDC::ReadRawData(AliRawData &raw, Int_t size, void *eb)
1520 // Read raw data from DATE data stream. Returns bytes read (i.e.
1521 // AliRawEventHeader::HeaderSize()), -1 in case of error and 0 for EOF.
1526 // read from event builder memory area
1527 raw.SetBuffer(eb, size);
1530 // read from fifo or file
1532 if ((nrecv = Read(raw.GetBuffer(), size)) != size) {
1534 Error("ReadRawData", "unexpected EOF");
1544 //______________________________________________________________________________
1545 Int_t AliMDC::DumpEvent(Int_t toRead)
1547 // This case should not happen, but if it does try to handle it
1548 // gracefully by reading the rest of the event and discarding it.
1549 // Returns bytes read, -1 in case of fatal error and 0 for EOF.
1551 Error("DumpEvent", "dumping %d bytes of event %d", toRead, fNumEvents);
1554 char *tbuf = new char[toRead];
1555 if ((nrecv = Read(tbuf, toRead)) != toRead) {
1557 Error("DumpEvent", "unexpected EOF");
1568 #include <AliTPCL3Tunnel.h>
1571 //______________________________________________________________________________
1572 Int_t AliMDC::Filter(AliRawData &raw)
1574 // Call 3rd level filter for this raw data segment.
1577 AliTPCL3Tunnel *tunnel = 0;
1580 tunnel = new AliTPCL3Tunnel(Form("%s/TPCparams.root",
1581 gSystem->Getenv("ALITPC")));
1584 Int_t obytes, nbytes;
1585 obytes = nbytes = raw.GetSize();
1586 char *outbuf = tunnel->EvalTrack((char *)raw.GetBuffer(), nbytes);
1588 raw.SetSize(nbytes);
1589 memcpy(raw.GetBuffer(), outbuf, nbytes);
1591 printf("Filter called for event %d: reduced from %d to %d\n", fNumEvents,
1597 printf("Filter called for event %d\n", fNumEvents);