]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliMDC.cxx
Fix for event mixing, when it was selecting events out of range of multiplicity cut
[u/mrichter/AliRoot.git] / RAW / AliMDC.cxx
index 832163d6ac7554ca0ca690bed79da2ccb1a7959e..a4374b53c02129aedc183390791be61a8f19951a 100644 (file)
@@ -1,4 +1,4 @@
-// @(#)alimdc:$Name$:$Id$
+// @(#)alimdc:$Name:  $:$Id$
 // Author: Fons Rademakers  26/11/99
 // Updated: Dario Favretto  15/04/2003
 
 //                                                                      //
 // AliMDC                                                               //
 //                                                                      //
-// Set of classes defining the ALICE RAW event format. The AliRawEvent  //
+// Set of classes defining the ALICE RAW event format. The AliRawEventV2//
 // class defines a RAW event. It consists of an AliEventHeader object   //
 // an AliEquipmentHeader object, an AliRawData object and an array of   //
-// sub-events, themselves also being AliRawEvents. The number of        //
+// sub-events, themselves also being AliRawEventV2s. The number of      //
 // sub-events depends on the number of DATE LDC's.                      //
-// The AliRawEvent objects are written to a ROOT file using different   //
+// The AliRawEventV2 objects are written to a ROOT file using different //
 // technologies, i.e. to local disk via AliRawDB or via rfiod using     //
 // AliRawRFIODB or via rootd using AliRawRootdDB or to CASTOR via       //
 // rootd using AliRawCastorDB (and for performance testing there is     //
@@ -50,6 +50,7 @@
 #include <TROOT.h>
 #include <TStopwatch.h>
 #include <TPluginManager.h>
+#include <TBufferFile.h>
 
 #include <sys/uio.h>
 #ifdef USE_EB
 #include <AliLog.h>
 #include <AliESDEvent.h>
 
-#include "AliRawEvent.h"
+#include "AliRawEventV2.h"
 #include "AliRawEventHeaderBase.h"
-#include "AliRawEquipment.h"
+#include "AliRawEquipmentV2.h"
 #include "AliRawEquipmentHeader.h"
+#include "AliRawDataArrayV2.h"
 #include "AliRawData.h"
-#include "AliStats.h"
 #include "AliRawDB.h"
 #include "AliRawRFIODB.h"
 #include "AliRawCastorDB.h"
@@ -87,14 +88,15 @@ const char* const AliMDC::fgkFilterName[kNFilters] = {"AliHoughFilter"};
 //______________________________________________________________________________
 AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode, 
               Double_t maxSizeTagDB, const char* fileNameTagDB,
-              const char *guidFileFolder) :
-  fEvent(new AliRawEvent),
+              const char *guidFileFolder,
+              Int_t basketsize) :
+  fEvent(new AliRawEventV2),
   fESD(NULL),
-  fStats(NULL),
   fRawDB(NULL),
   fTagDB(NULL),
   fEventTag(new AliRawEventTag),
   fCompress(compress),
+  fBasketSize(basketsize),
   fDeleteFiles(deleteFiles),
   fFilterMode(filterMode),
   fFilters(),
@@ -119,6 +121,17 @@ AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode,
   // Optional 'guidFileFolder' specifies the folder in which *.guid files
   // will be stored. In case this option is not given, the *.guid files
   // will be written to the same folder as the raw-data files.
+
+  // Set the maximum tree size to 19GB
+  // in order to allow big raw data files
+  TTree::SetMaxTreeSize(20000000000LL);
+
+  AliRawEquipmentHeader::Class()->IgnoreTObjectStreamer();
+  AliRawEquipmentV2::Class()->IgnoreTObjectStreamer();
+  AliRawEventV2::Class()->IgnoreTObjectStreamer();
+  AliRawDataArrayV2::Class()->IgnoreTObjectStreamer();
+
+  TBufferFile::SetGlobalReadParam(5);
  
   // This line is needed in case of a stand-alone application w/o
   // $ROOTSYS/etc/system.rootrc file
@@ -133,11 +146,11 @@ AliMDC::AliMDC(Int_t compress, Bool_t deleteFiles, EFilterMode filterMode,
   }
 
   // Create the guid files folder if it does not exist
-  if (fGuidFileFolder) {
+  if (!fGuidFileFolder.IsNull()) {
     gSystem->ResetErrno();
-    gSystem->MakeDirectory(fGuidFileFolder);
+    gSystem->MakeDirectory(fGuidFileFolder.Data());
     if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
-      SysError("AliMDC", "mkdir %s", fGuidFileFolder);
+      SysError("AliMDC", "mkdir %s", fGuidFileFolder.Data());
     }
   }
 
@@ -170,7 +183,6 @@ AliMDC::~AliMDC()
   fFilters.Delete();
   if(fTagDB) delete fTagDB;
   delete fRawDB;
-  delete fStats;
   delete fESD;
   delete fEvent;
   delete fEventTag;
@@ -184,15 +196,15 @@ Int_t AliMDC::Open(EWriteMode mode, const char* fileName,
 // open a new raw DB file
 
   if (mode == kRFIO)
-    fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, fileName);
+    fRawDB = new AliRawRFIODB(fEvent, fESD, fCompress, fileName, fBasketSize);
   else if (mode == kROOTD)
-    fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, fileName);
+    fRawDB = new AliRawRootdDB(fEvent, fESD, fCompress, fileName, fBasketSize);
   else if (mode == kCASTOR)
-    fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, fileName);
+    fRawDB = new AliRawCastorDB(fEvent, fESD, fCompress, fileName, fBasketSize);
   else if (mode == kDEVNULL)
-    fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, fileName);
+    fRawDB = new AliRawNullDB(fEvent, fESD, fCompress, fileName, fBasketSize);
   else
-    fRawDB = new AliRawDB(fEvent, fESD, fCompress, fileName);
+    fRawDB = new AliRawDB(fEvent, fESD, fCompress, fileName, fBasketSize);
   fRawDB->SetDeleteFiles(fDeleteFiles);
 
   if (fRawDB->IsZombie()) {
@@ -219,26 +231,24 @@ Int_t AliMDC::Open(EWriteMode mode, const char* fileName,
 
   Info("Open", "Filling raw DB %s\n", fRawDB->GetDBName());
 
-  // Create AliStats object
-  fStats = new AliStats(fRawDB->GetDBName(), fCompress, 
-                       fFilterMode != kFilterOff);
   return 0;
 }
 
 //______________________________________________________________________________
 Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 {
-// Convert the DATE event to an AliRawEvent object and store it in the raw DB,
+// Convert the DATE event to an AliRawEventV2 object and store it in the raw DB,
 // optionally also run the filter.
 // event is either a pointer to the streamlined event 
 // or, if isIovecArray is kTRUE, a pointer to an array of iovecs with one
 // iovec per subevent (used by the event builder).
 // The return value is the number of written bytes or an error code
-  const UInt_t kFileSizeErrorLevel   = 1900000000;
+  const Long64_t kFileSizeErrorLevel   = 19000000000LL;
 
-  UInt_t currentFileSize = GetTotalSize();
+  Long64_t currentFileSize = GetTotalSize();
+  //  AliDebug(1,Form("current file size is %lld bytes",currentFileSize));
   if(currentFileSize > kFileSizeErrorLevel) {
-    Error("ProcessEvent", "file size (%u) exceeds the limit "
+    Error("ProcessEvent", "file size (%lld) exceeds the limit "
          , currentFileSize);
     return kErrFileSize;
   }
@@ -252,16 +262,19 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 
   // Read event header
   if ((status = header->ReadHeader(data)) != (Int_t)header->GetHeadSize()) {
+    Error("ProcessEvent","Wrong event header format (%d != %d)",
+         status,(Int_t)header->GetHeadSize());
     return kErrHeader;
   }
 
-  if (AliDebugLevel() > 2) ToAliDebug(3, header->Dump(););
+  //  if (AliDebugLevel() > 2) ToAliDebug(3, header->Dump(););
 
   // Check event type and skip "Start of Run", "End of Run",
   // "Start of Run Files" and "End of Run Files"
   Int_t size = header->GetEventSize() - header->GetHeadSize();
-  
-  AliDebug(1, Form("Processing %s (%d bytes)", header->GetTypeName(), size));
+  UInt_t eventType = header->Get("Type");  
+
+  //  AliDebug(1, Form("Processing %s (%d bytes)", header->GetTypeName(), size));
 
   // Amount of data left to read for this event
   Int_t toRead = size;
@@ -284,9 +297,9 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
     while (toRead > 0) {
       if (isIovecArray) data = (char*) ((iovec*) event)[nsub].iov_base;
 
-      AliDebug(1, Form("reading LDC %d", nsub));
+      //      AliDebug(1, Form("reading LDC %d", nsub));
 
-      AliRawEvent *subEvent = fEvent->NextSubEvent();
+      AliRawEventV2 *subEvent = fEvent->NextSubEvent();
 
       // Read sub-event header
       AliRawEventHeaderBase *subHeader = subEvent->GetHeader(data);
@@ -294,7 +307,7 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
        return kErrSubHeader;
       }
 
-      if (AliDebugLevel() > 2) ToAliDebug(3, subHeader->Dump(););
+      //      if (AliDebugLevel() > 2) ToAliDebug(3, subHeader->Dump(););
 
       toRead -= subHeader->GetHeadSize();
 
@@ -309,12 +322,14 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
       }
 
       // Read Equipment Headers (in case of physics or calibration event)
-      if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
-         header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
-         header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
-         header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent) {
+      if (eventType == AliRawEventHeaderBase::kPhysicsEvent ||
+         eventType == AliRawEventHeaderBase::kCalibrationEvent ||
+         eventType == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
+         eventType == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent ||
+         eventType == AliRawEventHeaderBase::kStartOfData ||
+         eventType == AliRawEventHeaderBase::kEndOfData) {
        while (rawSize > 0) {
-         AliRawEquipment &equipment = *subEvent->NextEquipment();
+         AliRawEquipmentV2 &equipment = *subEvent->NextEquipment();
          AliRawEquipmentHeader &equipmentHeader = 
            *equipment.GetEquipmentHeader();
          Int_t equipHeaderSize = equipmentHeader.HeaderSize();
@@ -323,13 +338,15 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
            return kErrEquipmentHeader;
          }
 
-         if (AliDebugLevel() > 2) ToAliDebug(3, equipmentHeader.Dump(););
+         //      if (AliDebugLevel() > 2) ToAliDebug(3, equipmentHeader.Dump(););
 
          toRead  -= equipHeaderSize;
          rawSize -= equipHeaderSize;
 
          // Read equipment raw data
-         AliRawData &subRaw = *equipment.GetRawData();
+         AliRawDataArrayV2 *arr = fRawDB->GetRawDataArray(equipmentHeader.GetEquipmentSize(),
+                                                          equipmentHeader.GetId());
+         AliRawData &subRaw = *equipment.NextRawData(arr);
 
          Int_t eqSize = equipmentHeader.GetEquipmentSize() - equipHeaderSize;
          if ((status = ReadRawData(subRaw, eqSize, data)) != eqSize) {
@@ -342,8 +359,13 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 
       } else {  // Read only raw data but no equipment header
        if (rawSize) {
-         AliRawEquipment &equipment = *subEvent->NextEquipment();
-         AliRawData &subRaw = *equipment.GetRawData();
+         AliRawEquipmentV2 &equipment = *subEvent->NextEquipment();
+         AliRawEquipmentHeader &equipmentHeader = 
+           *equipment.GetEquipmentHeader();
+         equipmentHeader.Reset();
+         AliRawDataArrayV2 *arr = fRawDB->GetRawDataArray(equipmentHeader.GetEquipmentSize(),
+                                                          equipmentHeader.GetId());
+         AliRawData &subRaw = *equipment.NextRawData(arr);
          if ((status = ReadRawData(subRaw, rawSize, data)) != rawSize) {
            return kErrEquipment;
          }
@@ -357,10 +379,12 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 
   // High Level Event Filter
   if (fFilterMode != kFilterOff) {
-    if (header->Get("Type") == AliRawEventHeaderBase::kPhysicsEvent ||
-       header->Get("Type") == AliRawEventHeaderBase::kCalibrationEvent ||
-       header->Get("Type") == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
-       header->Get("Type") == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent) {
+    if (eventType == AliRawEventHeaderBase::kPhysicsEvent ||
+       eventType == AliRawEventHeaderBase::kCalibrationEvent ||
+       eventType == AliRawEventHeaderBase::kSystemSoftwareTriggerEvent ||
+       eventType == AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent ||
+       eventType == AliRawEventHeaderBase::kStartOfData ||
+       eventType == AliRawEventHeaderBase::kEndOfData) {
       Bool_t result = kFALSE;
       for (Int_t iFilter = 0; iFilter < fFilters.GetEntriesFast(); iFilter++) {
        AliFilter* filter = (AliFilter*) fFilters[iFilter];
@@ -371,29 +395,25 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
     }
   }
 
-  // Set stat info for first event of this file
-  if (fRawDB->GetEvents() == 0)
-    fStats->SetFirstId(header->Get("RunNb"), header->GetP("Id")[0]);
-
   // Store raw event in tree
   Int_t nBytes = fRawDB->Fill();
 
   // Fill the event tag object
-  fEventTag->SetHeader(fEvent->GetHeader());
+  fEventTag->SetHeader(header);
   fEventTag->SetGUID(fRawDB->GetDB()->GetUUID().AsString());
   fEventTag->SetEventNumber(fRawDB->GetEvents()-1);
 
   // Create Tag DB here only after the raw data header
   // version was already identified
   if (!fIsTagDBCreated) {
-    if (fFileNameTagDB) {
+    if (!fFileNameTagDB.IsNull()) {
       if (fMaxSizeTagDB > 0) {
        fTagDB = new AliTagDB(fEventTag, NULL);
        fTagDB->SetMaxSize(fMaxSizeTagDB);
-       fTagDB->SetFS(fFileNameTagDB);
+       fTagDB->SetFS(fFileNameTagDB.Data());
        if (!fTagDB->Create()) return kErrTagFile;
       } else {
-       fTagDB = new AliTagDB(fEventTag, fFileNameTagDB);
+       fTagDB = new AliTagDB(fEventTag, fFileNameTagDB.Data());
        if (fTagDB->IsZombie()) return kErrTagFile;
       }
     }
@@ -405,6 +425,8 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 
   // Make top event object ready for next event data
   fEvent->Reset();
+  fRawDB->Reset();
+
   // Clean up HLT ESD for the next event
   if (fESD) fESD->Reset();
 
@@ -415,7 +437,7 @@ Int_t AliMDC::ProcessEvent(void* event, Bool_t isIovecArray)
 }
 
 //______________________________________________________________________________
-Int_t AliMDC::GetTotalSize()
+Long64_t AliMDC::GetTotalSize()
 {
 // return the total current raw DB file size
 
@@ -425,21 +447,29 @@ Int_t AliMDC::GetTotalSize()
 }
 
 //______________________________________________________________________________
-Int_t AliMDC::Close()
+Long64_t AliMDC::Close()
 {
 // close the current raw DB file
 
   if (!fRawDB) return -1;
 
-  fRawDB->WriteStats(fStats);
-  Int_t filesize = fRawDB->Close();
+  Long64_t filesize = fRawDB->Close();
   delete fRawDB;
   fRawDB = NULL;
-  delete fStats;
-  fStats = NULL;
   return filesize;
 }
 
+//______________________________________________________________________________
+Long64_t AliMDC::AutoSave()
+{
+  // Auto-save the raw-data
+  // and esd (if any) trees
+
+  if (!fRawDB) return -1;
+
+  return fRawDB->AutoSave();
+}
+
 //______________________________________________________________________________
 Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
                  EWriteMode mode, Double_t maxFileSize, 
@@ -474,7 +504,6 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
   // Used for statistics
   TStopwatch timer;
   timer.Start();
-  Double_t told = 0, tnew = 0;
   Float_t  chunkSize = maxFileSize/100, nextChunk = chunkSize;
 
   // Create new raw DB.
@@ -491,6 +520,7 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
   Int_t numEvents = 0;
 
   AliRawEventHeaderBase header;
+  AliRawEventHeaderBase *hdr = NULL;
 
   while (kTRUE) {
 
@@ -516,8 +546,6 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
       Error("Run", "AliMDC was compiled without event builder support");
       delete fRawDB;
       fRawDB = NULL;
-      delete fStats;
-      fStats = NULL;
       return 1;
 #endif
 
@@ -542,7 +570,12 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
        }
       }
       char *data = (char *)header.HeaderBaseBegin();
-      AliRawEventHeaderBase *hdr = AliRawEventHeaderBase::Create(data);
+      if (!hdr) {
+       hdr = AliRawEventHeaderBase::Create(data);
+      }
+      else {
+       memcpy(hdr->HeaderBaseBegin(), header.HeaderBaseBegin(), header.HeaderBaseSize());
+      }
       Int_t nrecv;
       if ((nrecv = Read(fd, hdr->HeaderBegin(), hdr->HeaderSize())) !=
          hdr->HeaderSize()) {
@@ -568,7 +601,7 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
        eventSize = 2 * hdr->GetEventSize();
        event = new char[eventSize];
       }
-      memcpy(event, hdr->HeaderBaseBegin(), hdr->HeaderBaseSize());
+      memcpy(event, header.HeaderBaseBegin(), header.HeaderBaseSize());
       memcpy(event+hdr->HeaderBaseSize(), hdr->HeaderBegin(), hdr->HeaderSize());
       if (hdr->GetExtendedDataSize() != 0)
        memcpy(event+hdr->HeaderBaseSize()+hdr->HeaderSize(),
@@ -581,7 +614,6 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
        delete hdr;
        return 1;
       }
-      delete hdr;
     }
 
     Int_t result = ProcessEvent(event, !inputFile);
@@ -590,17 +622,11 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
 
     if (result >= 0) {
       numEvents++;
-      if (!(numEvents%10))
-       printf("Processed event %d (%d)\n", numEvents, fRawDB->GetEvents());
     }
 
     if (result > 0) {
       // Filling time statistics
       if (fRawDB->GetBytesWritten() > nextChunk) {
-       tnew = timer.RealTime();
-       fStats->Fill(tnew-told);
-       told = tnew;
-       timer.Continue();
        nextChunk += chunkSize;
       }
 
@@ -611,24 +637,16 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
        printf("Written raw DB at a rate of %.1f MB/s\n",
               fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);
 
-       // Write stats object to raw db, run db, MySQL and AliEn
-       fRawDB->WriteStats(fStats);
-       delete fStats;
-       fStats = NULL;
-
        if (!fRawDB->NextFile()) {
          Error("Run", "error opening next raw data file");
          Close();
          if (inputFile) delete[] event;
+         delete hdr;
          return 1;
        }
 
        printf("Filling raw DB %s\n", fRawDB->GetDBName());
-       fStats = new AliStats(fRawDB->GetDBName(), fCompress, 
-                             fFilterMode != kFilterOff);
-
        timer.Start();
-       told = 0, tnew = 0;
        nextChunk = chunkSize;
       }
 
@@ -659,6 +677,8 @@ Int_t AliMDC::Run(const char* inputFile, Bool_t loop,
     }
   }
 
+  delete hdr;
+
   printf("Written raw DB at a rate of %.1f MB/s\n",
         fRawDB->GetBytesWritten() / timer.RealTime() / 1000000.);