1 // @(#)alimdc:$Name$:$Id$
2 // Author: Fons Rademakers 26/11/99
4 /**************************************************************************
5 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
7 * Author: The ALICE Off-line Project. *
8 * Contributors are mentioned in the code where appropriate. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 //////////////////////////////////////////////////////////////////////////
23 //////////////////////////////////////////////////////////////////////////
26 #include <Riostream.h>
28 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
29 #include <TBufferFile.h>
37 #include <TObjString.h>
39 #include "AliESDEvent.h"
40 #include "AliRawEvent.h"
41 #include "AliRawDataArray.h"
42 #include "AliRawEventHeaderBase.h"
43 #include "AliRawEquipment.h"
44 #include "AliRawEquipmentHeader.h"
52 const char *AliRawDB::fgkAliRootTag = "$Name$";
54 // Split TPC into 9 branches in order to avoid problems with big memory
55 // consumption in case of TPC events w/o zero-suppression
56 Int_t AliRawDB::fgkDetBranches[AliDAQ::kNDetectors+1] = {1,1,1,18,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
58 //______________________________________________________________________________
59 AliRawDB::AliRawDB(AliRawEvent *event,
62 const char* fileName) :
75 // Create a new raw DB
77 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
78 fDetRawData[iDet] = new AliRawDataArray*[fgkDetBranches[iDet]];
79 Int_t nDDLsPerBranch = AliDAQ::NumberOfDdls(iDet)/fgkDetBranches[iDet];
80 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
81 fDetRawData[iDet][iBranch] = new AliRawDataArray(nDDLsPerBranch);
84 fDetRawData[AliDAQ::kNDetectors] = new AliRawDataArray*[fgkDetBranches[AliDAQ::kNDetectors]];
85 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++)
86 fDetRawData[AliDAQ::kNDetectors][iBranch] = new AliRawDataArray(100);
89 if (!Create(fileName))
95 //______________________________________________________________________________
96 AliRawDB::~AliRawDB() {
99 if(Close()==-1) Error("~AliRawDB", "cannot close output file!");
101 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++) {
102 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
103 delete fDetRawData[iDet][iBranch];
104 delete [] fDetRawData[iDet];
108 //______________________________________________________________________________
109 Bool_t AliRawDB::FSHasSpace(const char *fs) const
111 // Check for at least fMaxSize bytes of free space on the file system.
112 // If the space is not available return kFALSE, kTRUE otherwise.
114 Long_t id, bsize, blocks, bfree;
116 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
117 Error("FSHasSpace", "could not stat file system %s", fs);
121 // Leave 5 percent of diskspace free
122 Double_t avail = Double_t(bfree) * 0.95;
123 if (avail*bsize > fMaxSize)
126 Warning("FSHasSpace", "no space on file system %s", fs);
130 //______________________________________________________________________________
131 const char *AliRawDB::GetFileName() const
133 // Return filename based on hostname and date and time. This will make
134 // each file unique. Also makes sure (via FSHasSpace()) that there is
135 // enough space on the file system to store the file. Returns 0 in
136 // case of error or interrupt signal.
138 static TString fname;
139 static Bool_t fstoggle = kFALSE;
141 TString fs = fstoggle ? fFS2 : fFS1;
144 TString hostname = gSystem->HostName();
146 if ((pos = hostname.Index(".")) != kNPOS)
147 hostname.Remove(pos);
149 if (!FSHasSpace(fs)) {
151 fstoggle = !fstoggle;
152 fs = fstoggle ? fFS2 : fFS1;
153 if (FSHasSpace(fs)) break;
154 Info("GetFileName", "sleeping 30 seconds before retrying...");
155 gSystem->Sleep(30000); // sleep for 30 seconds
160 fname = fs + "/" + hostname + "_";
161 fname += dt.GetDate();
163 fname += dt.GetTime();
166 fstoggle = !fstoggle;
171 //______________________________________________________________________________
172 void AliRawDB::SetFS(const char* fs1, const char* fs2)
174 // set the file system location
177 if (fs1 && !fFS1.Contains(":")) {
178 gSystem->ResetErrno();
179 gSystem->MakeDirectory(fs1);
180 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
181 SysError("SetFS", "mkdir %s", fs1);
187 gSystem->ResetErrno();
188 gSystem->MakeDirectory(fs2);
189 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
190 SysError("SetFS", "mkdir %s", fs2);
195 //______________________________________________________________________________
196 Bool_t AliRawDB::Create(const char* fileName)
198 // Create a new raw DB.
200 const Int_t kMaxRetry = 1;
201 const Int_t kMaxSleep = 1; // seconds
202 const Int_t kMaxSleepLong = 10; // seconds
206 if (fStop) return kFALSE;
208 const char *fname = fileName;
209 if (!fname) fname = GetFileName();
211 Error("Create", "error getting raw DB file name");
217 fRawDB = TFile::Open(fname, GetOpenOption(),
218 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
221 if (retry < kMaxRetry) {
222 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
223 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
224 gSystem->Sleep(kMaxSleep*1000);
227 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
231 Warning("Create", "succeeded to open file after %d retries", retry);
233 if (fRawDB->IsZombie()) {
234 if (fRawDB->GetErrno() == ENOSPC ||
235 fRawDB->GetErrno() == 1018 || // SECOMERR
236 fRawDB->GetErrno() == 1027) { // SESYSERR
237 fRawDB->ResetErrno();
239 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
240 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
241 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
244 Error("Create", "file %s is zombie", fname);
245 fRawDB->ResetErrno();
248 if (retry < kMaxRetry) {
249 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
250 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
251 gSystem->Sleep(kMaxSleep*1000);
254 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
258 // Create raw data TTree
264 //______________________________________________________________________________
265 void AliRawDB::MakeTree()
267 // Create ROOT Tree object container.
269 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
270 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
274 Int_t bufsize = 256000;
275 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
278 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
280 // Make brach for each sub-detector
281 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
282 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
283 fTree->Branch(Form("%s%d",AliDAQ::DetectorName(iDet),iBranch),"AliRawDataArray",
284 &fDetRawData[iDet][iBranch],bufsize,split);
286 // Make special branch for unrecognized raw-data payloads
287 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++)
288 fTree->Branch(Form("Common%d",iBranch),"AliRawDataArray",
289 &fDetRawData[AliDAQ::kNDetectors][iBranch],bufsize,split);
291 // Create tree which will contain the HLT ESD information
294 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
295 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
297 fESDTree->Branch("ESD", "AliESDEvent", &fESD, bufsize, split);
302 //______________________________________________________________________________
303 Int_t AliRawDB::Close()
306 if (!fRawDB) return 0;
308 if (!fRawDB->IsOpen()) return 0;
313 Bool_t error = kFALSE;
314 if (fTree->Write() == 0)
317 if (fESDTree->Write() == 0)
320 // Close DB, this also deletes the fTree
323 Int_t filesize = fRawDB->GetEND();
326 gSystem->Unlink(fRawDB->GetName());
343 //______________________________________________________________________________
344 Int_t AliRawDB::Fill()
346 // Fill the trees and return the number of written bytes
348 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++)
349 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
350 fDetRawData[iDet][iBranch]->ClearData();
352 // Move the raw-data payloads to the corresponding branches
353 for(Int_t iSubEvent = 0; iSubEvent < fEvent->GetNSubEvents(); iSubEvent++) {
354 AliRawEvent *subEvent = fEvent->GetSubEvent(iSubEvent);
355 for(Int_t iEquipment = 0; iEquipment < subEvent->GetNEquipments(); iEquipment++) {
356 AliRawEquipment *equipment = subEvent->GetEquipment(iEquipment);
357 UInt_t eqId = equipment->GetEquipmentHeader()->GetId();
359 Int_t iDet = AliDAQ::DetectorIDFromDdlID(eqId,ddlIndex);
361 if (iDet < 0 || iDet > AliDAQ::kNDetectors) {
362 iDet = AliDAQ::kNDetectors;
363 iBranch = 0; // can we split somehow the unrecognized data??? For the moment - no
366 iBranch = (ddlIndex * fgkDetBranches[iDet])/AliDAQ::NumberOfDdls(iDet);
368 equipment->SetRawDataRef(fDetRawData[iDet][iBranch]);
372 Double_t bytes = fRawDB->GetBytesWritten();
373 Bool_t error = kFALSE;
374 if (fTree->Fill() == -1)
377 if (fESDTree->Fill() == -1)
380 return Int_t(fRawDB->GetBytesWritten() - bytes);
385 //______________________________________________________________________________
386 Int_t AliRawDB::GetTotalSize()
388 // Return the total size of the trees
393 TDirectory *dir = fTree->GetDirectory();
395 TKey *key = dir->GetKey(fTree->GetName());
396 if (key) skey = key->GetKeylen();
398 total += skey + fTree->GetZipBytes();
404 TDirectory *dir = fESDTree->GetDirectory();
406 TKey *key = dir->GetKey(fESDTree->GetName());
407 if (key) skey = key->GetKeylen();
409 total += skey + fESDTree->GetZipBytes();
415 //______________________________________________________________________________
416 void AliRawDB::WriteStats(AliStats* stats)
418 // Write stats to raw DB, local run DB and global MySQL DB.
420 AliRawEventHeaderBase &header = *GetEvent()->GetHeader();
422 // Write stats into RawDB
423 TDirectory *ds = gDirectory;
425 stats->SetEvents(GetEvents());
426 stats->SetLastId(header.GetP("Id")[0]);
427 stats->SetFileSize(GetBytesWritten());
428 stats->SetCompressionFactor(GetCompressionFactor());
430 stats->Write("stats");
434 //______________________________________________________________________________
435 Bool_t AliRawDB::NextFile(const char* fileName)
437 // Close te current file and open a new one.
438 // Returns kFALSE in case opening failed.
442 if (!Create(fileName)) return kFALSE;
446 //______________________________________________________________________________
447 Float_t AliRawDB::GetCompressionFactor() const
449 // Return compression factor.
451 if (fTree->GetZipBytes() == 0.)
454 return fTree->GetTotBytes()/fTree->GetZipBytes();
457 //______________________________________________________________________________
458 const char *AliRawDB::GetAliRootTag()
460 // Return the aliroot tag (version)
461 // used to generate the raw data file.
462 // Stored in the raw-data file title.
464 TString version = fgkAliRootTag;
465 version.Remove(TString::kBoth,'$');
466 version.ReplaceAll("Name","AliRoot version");
468 return version.Data();
471 //______________________________________________________________________________
472 Bool_t AliRawDB::WriteGuidFile(TString &guidFileFolder)
474 // Write the guid file
475 // in the specified folder or
476 // in the folder where the raw data
479 TString guidFileName;
480 if (!guidFileFolder.IsNull()) {
481 guidFileName = guidFileFolder;
483 TString pathStr = fRawDB->GetName();
484 TObjArray *pathArr = pathStr.Tokenize('/');
485 guidFileName.Append("/");
486 guidFileName.Append(((TObjString *)pathArr->Last())->String());
491 guidFileName = fRawDB->GetName();
493 guidFileName += ".guid";
495 ofstream fguid(guidFileName.Data());
496 if (!fguid.is_open()) {
497 Error("WriteGuidFile", "failure to open guid file %s", guidFileName.Data());
500 TString guid = fRawDB->GetUUID().AsString();
501 fguid << "guid: \t" << guid.Data();