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>
40 #include "AliRawEvent.h"
41 #include "AliRawEventHeaderBase.h"
49 const char *AliRawDB::fgkAliRootTag = "$Name$";
51 //______________________________________________________________________________
52 AliRawDB::AliRawDB(AliRawEvent *event,
55 const char* fileName) :
69 // Create a new raw DB
72 if (!Create(fileName))
77 //______________________________________________________________________________
78 Bool_t AliRawDB::FSHasSpace(const char *fs) const
80 // Check for at least fMaxSize bytes of free space on the file system.
81 // If the space is not available return kFALSE, kTRUE otherwise.
83 Long_t id, bsize, blocks, bfree;
85 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
86 Error("FSHasSpace", "could not stat file system %s", fs);
90 // Leave 5 percent of diskspace free
91 Double_t avail = Double_t(bfree) * 0.95;
92 if (avail*bsize > fMaxSize)
95 Warning("FSHasSpace", "no space on file system %s", fs);
99 //______________________________________________________________________________
100 const char *AliRawDB::GetFileName() const
102 // Return filename based on hostname and date and time. This will make
103 // each file unique. Also makes sure (via FSHasSpace()) that there is
104 // enough space on the file system to store the file. Returns 0 in
105 // case of error or interrupt signal.
107 static TString fname;
108 static Bool_t fstoggle = kFALSE;
110 TString fs = fstoggle ? fFS2 : fFS1;
113 TString hostname = gSystem->HostName();
115 if ((pos = hostname.Index(".")) != kNPOS)
116 hostname.Remove(pos);
118 if (!FSHasSpace(fs)) {
120 fstoggle = !fstoggle;
121 fs = fstoggle ? fFS2 : fFS1;
122 if (FSHasSpace(fs)) break;
123 Info("GetFileName", "sleeping 30 seconds before retrying...");
124 gSystem->Sleep(30000); // sleep for 30 seconds
129 fname = fs + "/" + hostname + "_";
130 fname += dt.GetDate();
132 fname += dt.GetTime();
135 fstoggle = !fstoggle;
140 //______________________________________________________________________________
141 void AliRawDB::SetFS(const char* fs1, const char* fs2)
143 // set the file system location
146 if (fs1 && !fFS1.Contains(":")) {
147 gSystem->ResetErrno();
148 gSystem->MakeDirectory(fs1);
149 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
150 SysError("SetFS", "mkdir %s", fs1);
156 gSystem->ResetErrno();
157 gSystem->MakeDirectory(fs2);
158 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
159 SysError("SetFS", "mkdir %s", fs2);
164 //______________________________________________________________________________
165 Bool_t AliRawDB::Create(const char* fileName)
167 // Create a new raw DB.
169 const Int_t kMaxRetry = 1;
170 const Int_t kMaxSleep = 1; // seconds
171 const Int_t kMaxSleepLong = 10; // seconds
175 if (fStop) return kFALSE;
177 const char *fname = fileName;
178 if (!fname) fname = GetFileName();
180 Error("Create", "error getting raw DB file name");
186 fRawDB = TFile::Open(fname, GetOpenOption(),
187 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
190 if (retry < kMaxRetry) {
191 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
192 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
193 gSystem->Sleep(kMaxSleep*1000);
196 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
200 Warning("Create", "succeeded to open file after %d retries", retry);
202 if (fRawDB->IsZombie()) {
203 if (fRawDB->GetErrno() == ENOSPC ||
204 fRawDB->GetErrno() == 1018 || // SECOMERR
205 fRawDB->GetErrno() == 1027) { // SESYSERR
206 fRawDB->ResetErrno();
208 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
209 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
210 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
213 Error("Create", "file %s is zombie", fname);
214 fRawDB->ResetErrno();
217 if (retry < kMaxRetry) {
218 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
219 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
220 gSystem->Sleep(kMaxSleep*1000);
223 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
227 // Create raw data TTree
233 //______________________________________________________________________________
234 void AliRawDB::MakeTree()
236 // Create ROOT Tree object container.
238 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
239 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
241 Int_t bufsize = 256000;
242 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
245 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
247 // Create tree which will contain the HLT ESD information
250 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
251 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
253 fESDTree->Branch("ESD", "AliESD", &fESD, bufsize, split);
258 //______________________________________________________________________________
259 Int_t AliRawDB::Close()
262 if (!fRawDB) return 0;
264 if (!fRawDB->IsOpen()) return 0;
269 Bool_t error = kFALSE;
270 if (fTree->Write() == 0)
273 if (fESDTree->Write() == 0)
276 // Close DB, this also deletes the fTree
279 Int_t filesize = fRawDB->GetEND();
282 gSystem->Unlink(fRawDB->GetName());
291 // Write a text file with file GUID
292 // in the specified folder
303 //______________________________________________________________________________
304 Int_t AliRawDB::Fill()
306 // Fill the trees and return the number of written bytes
308 Double_t bytes = fRawDB->GetBytesWritten();
309 Bool_t error = kFALSE;
310 if (fTree->Fill() == -1)
313 if (fESDTree->Fill() == -1)
316 return Int_t(fRawDB->GetBytesWritten() - bytes);
321 //______________________________________________________________________________
322 Int_t AliRawDB::GetTotalSize()
324 // Return the total size of the trees
329 TDirectory *dir = fTree->GetDirectory();
331 TKey *key = dir->GetKey(fTree->GetName());
332 if (key) skey = key->GetKeylen();
335 if (fTree->GetZipBytes() > 0) total += fTree->GetTotBytes();
336 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
337 TBufferFile b(TBuffer::kWrite,10000);
339 TBuffer b(TBuffer::kWrite,10000);
341 TTree::Class()->WriteBuffer(b,fTree);
348 TDirectory *dir = fESDTree->GetDirectory();
350 TKey *key = dir->GetKey(fESDTree->GetName());
351 if (key) skey = key->GetKeylen();
354 if (fESDTree->GetZipBytes() > 0) total += fESDTree->GetTotBytes();
355 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
356 TBufferFile b(TBuffer::kWrite,10000);
358 TBuffer b(TBuffer::kWrite,10000);
360 TTree::Class()->WriteBuffer(b,fESDTree);
367 //______________________________________________________________________________
368 void AliRawDB::WriteStats(AliStats* stats)
370 // Write stats to raw DB, local run DB and global MySQL DB.
372 AliRawEventHeaderBase &header = *GetEvent()->GetHeader();
374 // Write stats into RawDB
375 TDirectory *ds = gDirectory;
377 stats->SetEvents(GetEvents());
378 stats->SetLastId(header.GetP("Id")[0]);
379 stats->SetFileSize(GetBytesWritten());
380 stats->SetCompressionFactor(GetCompressionFactor());
382 stats->Write("stats");
386 //______________________________________________________________________________
387 Bool_t AliRawDB::NextFile(const char* fileName)
389 // Close te current file and open a new one.
390 // Returns kFALSE in case opening failed.
394 if (!Create(fileName)) return kFALSE;
398 //______________________________________________________________________________
399 Float_t AliRawDB::GetCompressionFactor() const
401 // Return compression factor.
403 if (fTree->GetZipBytes() == 0.)
406 return fTree->GetTotBytes()/fTree->GetZipBytes();
409 //______________________________________________________________________________
410 const char *AliRawDB::GetAliRootTag()
412 // Return the aliroot tag (version)
413 // used to generate the raw data file.
414 // Stored in the raw-data file title.
416 TString version = fgkAliRootTag;
417 version.Remove(TString::kBoth,'$');
418 version.ReplaceAll("Name","AliRoot version");
420 return version.Data();
423 //______________________________________________________________________________
424 void AliRawDB::WriteGuidFile()
426 // Write the guid file
427 // in the specified folder
429 TString guidFileName;
430 if (fGuidFileFolder) {
431 guidFileName = fGuidFileFolder;
433 TString pathStr = fRawDB->GetName();
434 TObjArray *pathArr = pathStr.Tokenize('/');
435 guidFileName.Append("/");
436 guidFileName.Append(((TObjString *)pathArr->Last())->String());
441 guidFileName = fRawDB->GetName();
443 guidFileName += ".guid";
445 ofstream fguid(guidFileName.Data());
446 TString guid = fRawDB->GetUUID().AsString();
447 fguid << "guid: \t" << guid.Data();