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 //////////////////////////////////////////////////////////////////////////
31 #include "AliRawEvent.h"
32 #include "AliRawEventHeaderBase.h"
40 const char *AliRawDB::fgkAliRootTag = "$Name$";
42 //______________________________________________________________________________
43 AliRawDB::AliRawDB(AliRawEvent *event,
46 const char* fileName) :
59 // Create a new raw DB
62 if (!Create(fileName))
67 //______________________________________________________________________________
68 Bool_t AliRawDB::FSHasSpace(const char *fs) const
70 // Check for at least fMaxSize bytes of free space on the file system.
71 // If the space is not available return kFALSE, kTRUE otherwise.
73 Long_t id, bsize, blocks, bfree;
75 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
76 Error("FSHasSpace", "could not stat file system %s", fs);
80 // Leave 5 percent of diskspace free
81 Double_t avail = Double_t(bfree) * 0.95;
82 if (avail*bsize > fMaxSize)
85 Warning("FSHasSpace", "no space on file system %s", fs);
89 //______________________________________________________________________________
90 const char *AliRawDB::GetFileName() const
92 // Return filename based on hostname and date and time. This will make
93 // each file unique. Also makes sure (via FSHasSpace()) that there is
94 // enough space on the file system to store the file. Returns 0 in
95 // case of error or interrupt signal.
98 static Bool_t fstoggle = kFALSE;
100 TString fs = fstoggle ? fFS2 : fFS1;
103 TString hostname = gSystem->HostName();
105 if ((pos = hostname.Index(".")) != kNPOS)
106 hostname.Remove(pos);
108 if (!FSHasSpace(fs)) {
110 fstoggle = !fstoggle;
111 fs = fstoggle ? fFS2 : fFS1;
112 if (FSHasSpace(fs)) break;
113 Info("GetFileName", "sleeping 30 seconds before retrying...");
114 gSystem->Sleep(30000); // sleep for 30 seconds
119 fname = fs + "/" + hostname + "_";
120 fname += dt.GetDate();
122 fname += dt.GetTime();
125 fstoggle = !fstoggle;
130 //______________________________________________________________________________
131 void AliRawDB::SetFS(const char* fs1, const char* fs2)
133 // set the file system location
136 if (fs1 && !fFS1.Contains(":")) {
137 gSystem->ResetErrno();
138 gSystem->MakeDirectory(fs1);
139 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
140 SysError("SetFS", "mkdir %s", fs1);
146 gSystem->ResetErrno();
147 gSystem->MakeDirectory(fs2);
148 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
149 SysError("SetFS", "mkdir %s", fs2);
154 //______________________________________________________________________________
155 Bool_t AliRawDB::Create(const char* fileName)
157 // Create a new raw DB.
159 const Int_t kMaxRetry = 1;
160 const Int_t kMaxSleep = 1; // seconds
161 const Int_t kMaxSleepLong = 10; // seconds
165 if (fStop) return kFALSE;
167 const char *fname = fileName;
168 if (!fname) fname = GetFileName();
170 Error("Create", "error getting raw DB file name");
176 fRawDB = TFile::Open(fname, GetOpenOption(),
177 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
180 if (retry < kMaxRetry) {
181 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
182 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
183 gSystem->Sleep(kMaxSleep*1000);
186 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
190 Warning("Create", "succeeded to open file after %d retries", retry);
192 if (fRawDB->IsZombie()) {
193 if (fRawDB->GetErrno() == ENOSPC ||
194 fRawDB->GetErrno() == 1018 || // SECOMERR
195 fRawDB->GetErrno() == 1027) { // SESYSERR
196 fRawDB->ResetErrno();
198 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
199 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
200 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
203 Error("Create", "file %s is zombie", fname);
204 fRawDB->ResetErrno();
207 if (retry < kMaxRetry) {
208 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
209 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
210 gSystem->Sleep(kMaxSleep*1000);
213 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
217 // Create raw data TTree
223 //______________________________________________________________________________
224 void AliRawDB::MakeTree()
226 // Create ROOT Tree object container.
228 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
229 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
231 Int_t bufsize = 256000;
232 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
235 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
237 // Create tree which will contain the HLT ESD information
240 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
241 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
243 fESDTree->Branch("ESD", "AliESD", &fESD, bufsize, split);
248 //______________________________________________________________________________
249 Int_t AliRawDB::Close()
252 if (!fRawDB) return 0;
254 if (!fRawDB->IsOpen()) return 0;
259 Bool_t error = kFALSE;
260 if (fTree->Write() == 0)
263 if (fESDTree->Write() == 0)
266 // Close DB, this also deletes the fTree
269 Int_t filesize = fRawDB->GetEND();
272 gSystem->Unlink(fRawDB->GetName());
289 //______________________________________________________________________________
290 Int_t AliRawDB::Fill()
292 // Fill the trees and return the number of written bytes
294 Double_t bytes = fRawDB->GetBytesWritten();
295 Bool_t error = kFALSE;
296 if (fTree->Fill() == -1)
299 if (fESDTree->Fill() == -1)
302 return Int_t(fRawDB->GetBytesWritten() - bytes);
307 //______________________________________________________________________________
308 Int_t AliRawDB::GetTotalSize()
310 // Return the total size of the trees
315 TDirectory *dir = fTree->GetDirectory();
317 TKey *key = dir->GetKey(fTree->GetName());
318 if (key) skey = key->GetKeylen();
321 if (fTree->GetZipBytes() > 0) total += fTree->GetTotBytes();
322 TBuffer b(TBuffer::kWrite,10000);
323 TTree::Class()->WriteBuffer(b,fTree);
330 TDirectory *dir = fESDTree->GetDirectory();
332 TKey *key = dir->GetKey(fESDTree->GetName());
333 if (key) skey = key->GetKeylen();
336 if (fESDTree->GetZipBytes() > 0) total += fESDTree->GetTotBytes();
337 TBuffer b(TBuffer::kWrite,10000);
338 TTree::Class()->WriteBuffer(b,fESDTree);
345 //______________________________________________________________________________
346 void AliRawDB::WriteStats(AliStats* stats)
348 // Write stats to raw DB, local run DB and global MySQL DB.
350 AliRawEventHeaderBase &header = *GetEvent()->GetHeader();
352 // Write stats into RawDB
353 TDirectory *ds = gDirectory;
355 stats->SetEvents(GetEvents());
356 stats->SetLastId(header.GetP("Id")[0]);
357 stats->SetFileSize(GetBytesWritten());
358 stats->SetCompressionFactor(GetCompressionFactor());
360 stats->Write("stats");
364 //______________________________________________________________________________
365 Bool_t AliRawDB::NextFile(const char* fileName)
367 // Close te current file and open a new one.
368 // Returns kFALSE in case opening failed.
372 if (!Create(fileName)) return kFALSE;
376 //______________________________________________________________________________
377 Float_t AliRawDB::GetCompressionFactor() const
379 // Return compression factor.
381 if (fTree->GetZipBytes() == 0.)
384 return fTree->GetTotBytes()/fTree->GetZipBytes();
387 //______________________________________________________________________________
388 const char *AliRawDB::GetAliRootTag()
390 // Return the aliroot tag (version)
391 // used to generate the raw data file.
392 // Stored in the raw-data file title.
394 TString version = fgkAliRootTag;
395 version.Remove(TString::kBoth,'$');
396 version.ReplaceAll("Name","AliRoot version");
398 return version.Data();