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 //////////////////////////////////////////////////////////////////////////
34 #include "AliRawEvent.h"
35 #include "AliRawEventHeader.h"
44 //______________________________________________________________________________
45 AliRawDB::AliRawDB(AliRawEvent *event,
48 const char* fileName) :
61 // Create a new raw DB
63 // Consistency check with DATE header file
65 if (fEvent->GetHeader()->HeaderSize() != EVENT_HEAD_BASE_SIZE) {
66 Error("AliRawDB", "inconsistency between DATE and AliRawEvent headers");
73 if (!Create(fileName))
78 //______________________________________________________________________________
79 AliRawDB::AliRawDB(const AliRawDB& rawDB): TObject(rawDB)
83 Fatal("AliRawDB", "copy constructor not implemented");
86 //______________________________________________________________________________
87 AliRawDB& AliRawDB::operator = (const AliRawDB& /*rawDB*/)
89 // assignment operator
91 Fatal("operator =", "assignment operator not implemented");
95 //______________________________________________________________________________
96 Bool_t AliRawDB::FSHasSpace(const char *fs) const
98 // Check for at least fMaxSize bytes of free space on the file system.
99 // If the space is not available return kFALSE, kTRUE otherwise.
101 Long_t id, bsize, blocks, bfree;
103 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
104 Error("FSHasSpace", "could not stat file system %s", fs);
108 // Leave 5 percent of diskspace free
109 Double_t avail = Double_t(bfree) * 0.95;
110 if (avail*bsize > fMaxSize)
113 Warning("FSHasSpace", "no space on file system %s", fs);
117 //______________________________________________________________________________
118 const char *AliRawDB::GetFileName() const
120 // Return filename based on hostname and date and time. This will make
121 // each file unique. Also makes sure (via FSHasSpace()) that there is
122 // enough space on the file system to store the file. Returns 0 in
123 // case of error or interrupt signal.
125 static TString fname;
126 static Bool_t fstoggle = kFALSE;
128 TString fs = fstoggle ? fFS2 : fFS1;
131 TString hostname = gSystem->HostName();
133 if ((pos = hostname.Index(".")) != kNPOS)
134 hostname.Remove(pos);
136 if (!FSHasSpace(fs)) {
138 fstoggle = !fstoggle;
139 fs = fstoggle ? fFS2 : fFS1;
140 if (FSHasSpace(fs)) break;
141 Info("GetFileName", "sleeping 30 seconds before retrying...");
142 gSystem->Sleep(30000); // sleep for 30 seconds
147 fname = fs + "/" + hostname + "_";
148 fname += dt.GetDate();
150 fname += dt.GetTime();
153 fstoggle = !fstoggle;
158 //______________________________________________________________________________
159 void AliRawDB::SetFS(const char* fs1, const char* fs2)
161 // set the file system location
164 if (fs1 && !fFS1.Contains(":")) {
165 gSystem->ResetErrno();
166 gSystem->MakeDirectory(fs1);
167 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
168 SysError("SetFS", "mkdir %s", fs1);
174 gSystem->ResetErrno();
175 gSystem->MakeDirectory(fs2);
176 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
177 SysError("SetFS", "mkdir %s", fs2);
182 //______________________________________________________________________________
183 Bool_t AliRawDB::Create(const char* fileName)
185 // Create a new raw DB.
187 const Int_t kMaxRetry = 200;
188 const Int_t kMaxSleep = 1; // seconds
189 const Int_t kMaxSleepLong = 10; // seconds
193 if (fStop) return kFALSE;
195 const char *fname = fileName;
196 if (!fname) fname = GetFileName();
198 Error("Create", "error getting raw DB file name");
204 fRawDB = TFile::Open(fname, GetOpenOption(),
205 Form("ALICE MDC%d raw DB", kMDC), fCompress,
208 if (retry < kMaxRetry) {
209 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
210 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
211 gSystem->Sleep(kMaxSleep*1000);
214 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
218 Warning("Create", "succeeded to open file after %d retries", retry);
220 if (fRawDB->IsZombie()) {
221 if (fRawDB->GetErrno() == ENOSPC ||
222 fRawDB->GetErrno() == 1018 || // SECOMERR
223 fRawDB->GetErrno() == 1027) { // SESYSERR
224 fRawDB->ResetErrno();
226 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
227 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
228 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
231 Error("Create", "file %s is zombie", fname);
232 fRawDB->ResetErrno();
235 if (retry < kMaxRetry) {
236 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
237 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
238 gSystem->Sleep(kMaxSleep*1000);
241 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
245 // Create raw data TTree
251 //______________________________________________________________________________
252 void AliRawDB::MakeTree()
254 // Create ROOT Tree object container.
256 fTree = new TTree("RAW", Form("ALICE MDC%d raw data tree", kMDC));
257 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
259 Int_t bufsize = 256000;
260 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
263 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
265 // Create tree which will contain the HLT ESD information
268 fESDTree = new TTree("esdTree", Form("ALICE MDC%d HLT ESD tree", kMDC));
269 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
271 fESDTree->Branch("ESD", "AliESD", &fESD, bufsize, split);
276 //______________________________________________________________________________
277 void AliRawDB::Close()
287 if (fESDTree) fESDTree->Write();
289 // Close DB, this also deletes the fTree
293 gSystem->Unlink(fRawDB->GetName());
299 // Create semaphore to say this file is finished
300 Int_t tfd = ::creat(Form("%s.done", fRawDB->GetName()), 0644);
307 //______________________________________________________________________________
308 Int_t AliRawDB::Fill()
310 // Fill the trees and return the number of written bytes
312 Double_t bytes = fRawDB->GetBytesWritten();
314 if (fESDTree) fESDTree->Fill();
315 return Int_t(fRawDB->GetBytesWritten() - bytes);
318 //______________________________________________________________________________
319 void AliRawDB::WriteStats(AliStats* stats)
321 // Write stats to raw DB, local run DB and global MySQL DB.
323 AliRawEventHeader &header = *GetEvent()->GetHeader();
325 // Write stats into RawDB
326 TDirectory *ds = gDirectory;
328 stats->SetEvents(GetEvents());
329 stats->SetLastId(header.GetRunNumber(), header.GetEventInRun());
330 stats->SetFileSize(GetBytesWritten());
331 stats->SetCompressionFactor(GetCompressionFactor());
333 stats->Write("stats");
337 //______________________________________________________________________________
338 Bool_t AliRawDB::NextFile(const char* fileName)
340 // Close te current file and open a new one.
341 // Returns kFALSE in case opening failed.
345 if (!Create(fileName)) return kFALSE;
349 //______________________________________________________________________________
350 Float_t AliRawDB::GetCompressionFactor() const
352 // Return compression factor.
354 if (fTree->GetZipBytes() == 0.)
357 return fTree->GetTotBytes()/fTree->GetZipBytes();