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>
38 #include "AliRawEvent.h"
39 #include "AliRawEventHeaderBase.h"
47 const char *AliRawDB::fgkAliRootTag = "$Name$";
49 //______________________________________________________________________________
50 AliRawDB::AliRawDB(AliRawEvent *event,
53 const char* fileName) :
66 // Create a new raw DB
69 if (!Create(fileName))
74 //______________________________________________________________________________
75 Bool_t AliRawDB::FSHasSpace(const char *fs) const
77 // Check for at least fMaxSize bytes of free space on the file system.
78 // If the space is not available return kFALSE, kTRUE otherwise.
80 Long_t id, bsize, blocks, bfree;
82 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
83 Error("FSHasSpace", "could not stat file system %s", fs);
87 // Leave 5 percent of diskspace free
88 Double_t avail = Double_t(bfree) * 0.95;
89 if (avail*bsize > fMaxSize)
92 Warning("FSHasSpace", "no space on file system %s", fs);
96 //______________________________________________________________________________
97 const char *AliRawDB::GetFileName() const
99 // Return filename based on hostname and date and time. This will make
100 // each file unique. Also makes sure (via FSHasSpace()) that there is
101 // enough space on the file system to store the file. Returns 0 in
102 // case of error or interrupt signal.
104 static TString fname;
105 static Bool_t fstoggle = kFALSE;
107 TString fs = fstoggle ? fFS2 : fFS1;
110 TString hostname = gSystem->HostName();
112 if ((pos = hostname.Index(".")) != kNPOS)
113 hostname.Remove(pos);
115 if (!FSHasSpace(fs)) {
117 fstoggle = !fstoggle;
118 fs = fstoggle ? fFS2 : fFS1;
119 if (FSHasSpace(fs)) break;
120 Info("GetFileName", "sleeping 30 seconds before retrying...");
121 gSystem->Sleep(30000); // sleep for 30 seconds
126 fname = fs + "/" + hostname + "_";
127 fname += dt.GetDate();
129 fname += dt.GetTime();
132 fstoggle = !fstoggle;
137 //______________________________________________________________________________
138 void AliRawDB::SetFS(const char* fs1, const char* fs2)
140 // set the file system location
143 if (fs1 && !fFS1.Contains(":")) {
144 gSystem->ResetErrno();
145 gSystem->MakeDirectory(fs1);
146 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
147 SysError("SetFS", "mkdir %s", fs1);
153 gSystem->ResetErrno();
154 gSystem->MakeDirectory(fs2);
155 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
156 SysError("SetFS", "mkdir %s", fs2);
161 //______________________________________________________________________________
162 Bool_t AliRawDB::Create(const char* fileName)
164 // Create a new raw DB.
166 const Int_t kMaxRetry = 1;
167 const Int_t kMaxSleep = 1; // seconds
168 const Int_t kMaxSleepLong = 10; // seconds
172 if (fStop) return kFALSE;
174 const char *fname = fileName;
175 if (!fname) fname = GetFileName();
177 Error("Create", "error getting raw DB file name");
183 fRawDB = TFile::Open(fname, GetOpenOption(),
184 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
187 if (retry < kMaxRetry) {
188 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
189 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
190 gSystem->Sleep(kMaxSleep*1000);
193 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
197 Warning("Create", "succeeded to open file after %d retries", retry);
199 if (fRawDB->IsZombie()) {
200 if (fRawDB->GetErrno() == ENOSPC ||
201 fRawDB->GetErrno() == 1018 || // SECOMERR
202 fRawDB->GetErrno() == 1027) { // SESYSERR
203 fRawDB->ResetErrno();
205 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
206 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
207 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
210 Error("Create", "file %s is zombie", fname);
211 fRawDB->ResetErrno();
214 if (retry < kMaxRetry) {
215 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
216 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
217 gSystem->Sleep(kMaxSleep*1000);
220 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
224 // Create raw data TTree
230 //______________________________________________________________________________
231 void AliRawDB::MakeTree()
233 // Create ROOT Tree object container.
235 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
236 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
238 Int_t bufsize = 256000;
239 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
242 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
244 // Create tree which will contain the HLT ESD information
247 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
248 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
250 fESDTree->Branch("ESD", "AliESD", &fESD, bufsize, split);
255 //______________________________________________________________________________
256 Int_t AliRawDB::Close()
259 if (!fRawDB) return 0;
261 if (!fRawDB->IsOpen()) return 0;
266 Bool_t error = kFALSE;
267 if (fTree->Write() == 0)
270 if (fESDTree->Write() == 0)
273 // Close DB, this also deletes the fTree
276 Int_t filesize = fRawDB->GetEND();
279 gSystem->Unlink(fRawDB->GetName());
288 // Write a text file with file GUID
289 TString guidFileName = fRawDB->GetName();
290 guidFileName += ".guid";
291 ofstream fguid(guidFileName.Data());
292 TString guid = fRawDB->GetUUID().AsString();
293 fguid << "guid: \t" << guid.Data();
304 //______________________________________________________________________________
305 Int_t AliRawDB::Fill()
307 // Fill the trees and return the number of written bytes
309 Double_t bytes = fRawDB->GetBytesWritten();
310 Bool_t error = kFALSE;
311 if (fTree->Fill() == -1)
314 if (fESDTree->Fill() == -1)
317 return Int_t(fRawDB->GetBytesWritten() - bytes);
322 //______________________________________________________________________________
323 Int_t AliRawDB::GetTotalSize()
325 // Return the total size of the trees
330 TDirectory *dir = fTree->GetDirectory();
332 TKey *key = dir->GetKey(fTree->GetName());
333 if (key) skey = key->GetKeylen();
336 if (fTree->GetZipBytes() > 0) total += fTree->GetTotBytes();
337 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
338 TBufferFile b(TBuffer::kWrite,10000);
340 TBuffer b(TBuffer::kWrite,10000);
342 TTree::Class()->WriteBuffer(b,fTree);
349 TDirectory *dir = fESDTree->GetDirectory();
351 TKey *key = dir->GetKey(fESDTree->GetName());
352 if (key) skey = key->GetKeylen();
355 if (fESDTree->GetZipBytes() > 0) total += fESDTree->GetTotBytes();
356 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
357 TBufferFile b(TBuffer::kWrite,10000);
359 TBuffer b(TBuffer::kWrite,10000);
361 TTree::Class()->WriteBuffer(b,fESDTree);
368 //______________________________________________________________________________
369 void AliRawDB::WriteStats(AliStats* stats)
371 // Write stats to raw DB, local run DB and global MySQL DB.
373 AliRawEventHeaderBase &header = *GetEvent()->GetHeader();
375 // Write stats into RawDB
376 TDirectory *ds = gDirectory;
378 stats->SetEvents(GetEvents());
379 stats->SetLastId(header.GetP("Id")[0]);
380 stats->SetFileSize(GetBytesWritten());
381 stats->SetCompressionFactor(GetCompressionFactor());
383 stats->Write("stats");
387 //______________________________________________________________________________
388 Bool_t AliRawDB::NextFile(const char* fileName)
390 // Close te current file and open a new one.
391 // Returns kFALSE in case opening failed.
395 if (!Create(fileName)) return kFALSE;
399 //______________________________________________________________________________
400 Float_t AliRawDB::GetCompressionFactor() const
402 // Return compression factor.
404 if (fTree->GetZipBytes() == 0.)
407 return fTree->GetTotBytes()/fTree->GetZipBytes();
410 //______________________________________________________________________________
411 const char *AliRawDB::GetAliRootTag()
413 // Return the aliroot tag (version)
414 // used to generate the raw data file.
415 // Stored in the raw-data file title.
417 TString version = fgkAliRootTag;
418 version.Remove(TString::kBoth,'$');
419 version.ReplaceAll("Name","AliRoot version");
421 return version.Data();