]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RAW/AliRawDB.cxx
Adding the binaliengui.pkg that will be used to build the aliengui executable.
[u/mrichter/AliRoot.git] / RAW / AliRawDB.cxx
CommitLineData
a197a4ce 1// @(#)alimdc:$Name$:$Id$
2// Author: Fons Rademakers 26/11/99
3
4/**************************************************************************
5 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
6 * *
7 * Author: The ALICE Off-line Project. *
8 * Contributors are mentioned in the code where appropriate. *
9 * *
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 **************************************************************************/
18
19//////////////////////////////////////////////////////////////////////////
20// //
21// AliRawDB //
22// //
23//////////////////////////////////////////////////////////////////////////
24
25#include <errno.h>
26
27#include <TSystem.h>
d21e9888 28#include <TKey.h>
a197a4ce 29
52f36b9b 30#include "AliESD.h"
a197a4ce 31#include "AliRawEvent.h"
f2dc6b20 32#include "AliRawEventHeaderBase.h"
b7d09bb3 33#include "AliStats.h"
a109e73e 34
a197a4ce 35#include "AliRawDB.h"
36
37
38ClassImp(AliRawDB)
39
ee8f2a6c 40const char *AliRawDB::fgkAliRootTag = "$Name$";
a197a4ce 41
42//______________________________________________________________________________
a109e73e 43AliRawDB::AliRawDB(AliRawEvent *event,
a109e73e 44 AliESD *esd,
e10815f1 45 Int_t compress,
46 const char* fileName) :
47 fRawDB(NULL),
48 fTree(NULL),
49 fEvent(event),
50 fESDTree(NULL),
51 fESD(esd),
52 fCompress(compress),
53 fMaxSize(-1),
54 fFS1(""),
55 fFS2(""),
56 fDeleteFiles(kFALSE),
57 fStop(kFALSE)
a197a4ce 58{
e10815f1 59 // Create a new raw DB
a197a4ce 60
e10815f1 61 if (fileName) {
62 if (!Create(fileName))
a197a4ce 63 MakeZombie();
64 }
65}
66
a197a4ce 67//______________________________________________________________________________
68Bool_t AliRawDB::FSHasSpace(const char *fs) const
69{
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.
72
73 Long_t id, bsize, blocks, bfree;
74
75 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
76 Error("FSHasSpace", "could not stat file system %s", fs);
77 return kFALSE;
78 }
79
80 // Leave 5 percent of diskspace free
81 Double_t avail = Double_t(bfree) * 0.95;
82 if (avail*bsize > fMaxSize)
83 return kTRUE;
84
85 Warning("FSHasSpace", "no space on file system %s", fs);
86 return kFALSE;
87}
88
89//______________________________________________________________________________
90const char *AliRawDB::GetFileName() const
91{
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.
96
97 static TString fname;
98 static Bool_t fstoggle = kFALSE;
99
e10815f1 100 TString fs = fstoggle ? fFS2 : fFS1;
a197a4ce 101 TDatime dt;
102
103 TString hostname = gSystem->HostName();
104 Int_t pos;
105 if ((pos = hostname.Index(".")) != kNPOS)
106 hostname.Remove(pos);
107
108 if (!FSHasSpace(fs)) {
109 while (1) {
110 fstoggle = !fstoggle;
e10815f1 111 fs = fstoggle ? fFS2 : fFS1;
a197a4ce 112 if (FSHasSpace(fs)) break;
113 Info("GetFileName", "sleeping 30 seconds before retrying...");
114 gSystem->Sleep(30000); // sleep for 30 seconds
e10815f1 115 if (fStop) return 0;
a197a4ce 116 }
117 }
118
119 fname = fs + "/" + hostname + "_";
120 fname += dt.GetDate();
121 fname += "_";
122 fname += dt.GetTime();
123 fname += ".root";
124
125 fstoggle = !fstoggle;
126
127 return fname;
128}
129
130//______________________________________________________________________________
e10815f1 131void AliRawDB::SetFS(const char* fs1, const char* fs2)
132{
133// set the file system location
134
135 fFS1 = fs1;
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);
141 }
142 }
143
144 fFS2 = fs2;
145 if (fs2) {
146 gSystem->ResetErrno();
147 gSystem->MakeDirectory(fs2);
148 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
149 SysError("SetFS", "mkdir %s", fs2);
150 }
151 }
152}
153
154//______________________________________________________________________________
155Bool_t AliRawDB::Create(const char* fileName)
a197a4ce 156{
157 // Create a new raw DB.
158
f07ec911 159 const Int_t kMaxRetry = 1;
a197a4ce 160 const Int_t kMaxSleep = 1; // seconds
161 const Int_t kMaxSleepLong = 10; // seconds
162 Int_t retry = 0;
163
164again:
e10815f1 165 if (fStop) return kFALSE;
a197a4ce 166
e10815f1 167 const char *fname = fileName;
168 if (!fname) fname = GetFileName();
a197a4ce 169 if (!fname) {
170 Error("Create", "error getting raw DB file name");
171 return kFALSE;
172 }
173
174 retry++;
175
176 fRawDB = TFile::Open(fname, GetOpenOption(),
ee8f2a6c 177 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
f07ec911 178 GetNetopt());
a197a4ce 179 if (!fRawDB) {
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);
184 goto again;
185 }
186 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
187 return kFALSE;
188 }
189 if (retry > 1)
190 Warning("Create", "succeeded to open file after %d retries", retry);
191
192 if (fRawDB->IsZombie()) {
193 if (fRawDB->GetErrno() == ENOSPC ||
194 fRawDB->GetErrno() == 1018 || // SECOMERR
195 fRawDB->GetErrno() == 1027) { // SESYSERR
196 fRawDB->ResetErrno();
197 delete fRawDB;
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
201 goto again;
202 }
203 Error("Create", "file %s is zombie", fname);
204 fRawDB->ResetErrno();
205 delete fRawDB;
206 fRawDB = 0;
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);
211 goto again;
212 }
213 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
214 return kFALSE;
215 }
216
217 // Create raw data TTree
218 MakeTree();
219
220 return kTRUE;
221}
222
223//______________________________________________________________________________
224void AliRawDB::MakeTree()
225{
226 // Create ROOT Tree object container.
227
ee8f2a6c 228 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
a197a4ce 229 fTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
230
231 Int_t bufsize = 256000;
232 // splitting 29.6 MB/s, no splitting 35.3 MB/s on P4 2GHz 15k SCSI
233 //Int_t split = 1;
234 Int_t split = 0;
235 fTree->Branch("rawevent", "AliRawEvent", &fEvent, bufsize, split);
a109e73e 236
a109e73e 237 // Create tree which will contain the HLT ESD information
238
e10815f1 239 if (fESD) {
ee8f2a6c 240 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
e10815f1 241 fESDTree->SetAutoSave(2000000000); // autosave when 2 Gbyte written
5f42007f 242 split = 0;
e10815f1 243 fESDTree->Branch("ESD", "AliESD", &fESD, bufsize, split);
244 }
a109e73e 245
a197a4ce 246}
247
248//______________________________________________________________________________
f07ec911 249Int_t AliRawDB::Close()
a197a4ce 250{
251 // Close raw DB.
f07ec911 252 if (!fRawDB) return 0;
a197a4ce 253
f07ec911 254 if (!fRawDB->IsOpen()) return 0;
a197a4ce 255
256 fRawDB->cd();
257
258 // Write the tree.
f07ec911 259 Bool_t error = kFALSE;
260 if (fTree->Write() == 0)
261 error = kTRUE;
262 if (fESDTree)
263 if (fESDTree->Write() == 0)
264 error = kTRUE;
a197a4ce 265
266 // Close DB, this also deletes the fTree
267 fRawDB->Close();
268
f07ec911 269 Int_t filesize = fRawDB->GetEND();
270
e10815f1 271 if (fDeleteFiles) {
a197a4ce 272 gSystem->Unlink(fRawDB->GetName());
273 delete fRawDB;
274 fRawDB = 0;
f07ec911 275 if(!error)
276 return filesize;
277 else
278 return -1;
a197a4ce 279 }
280
281 // Create semaphore to say this file is finished
282 Int_t tfd = ::creat(Form("%s.done", fRawDB->GetName()), 0644);
283 close(tfd);
284
285 delete fRawDB;
286 fRawDB = 0;
f07ec911 287 if(!error)
288 return filesize;
289 else
290 return -1;
a197a4ce 291}
292
e10815f1 293//______________________________________________________________________________
294Int_t AliRawDB::Fill()
295{
296 // Fill the trees and return the number of written bytes
297
298 Double_t bytes = fRawDB->GetBytesWritten();
f07ec911 299 Bool_t error = kFALSE;
300 if (fTree->Fill() == -1)
301 error = kTRUE;
302 if (fESDTree)
303 if (fESDTree->Fill() == -1)
304 error = kTRUE;
305 if(!error)
306 return Int_t(fRawDB->GetBytesWritten() - bytes);
307 else
308 return -1;
e10815f1 309}
310
d21e9888 311//______________________________________________________________________________
312Int_t AliRawDB::GetTotalSize()
313{
314 // Return the total size of the trees
315 Int_t total = 0;
316
317 {
318 Int_t skey = 0;
319 TDirectory *dir = fTree->GetDirectory();
320 if (dir) {
321 TKey *key = dir->GetKey(fTree->GetName());
322 if (key) skey = key->GetKeylen();
323 }
324 total += skey;
325 if (fTree->GetZipBytes() > 0) total += fTree->GetTotBytes();
326 TBuffer b(TBuffer::kWrite,10000);
327 TTree::Class()->WriteBuffer(b,fTree);
328 total += b.Length();
329 }
330
331 if(fESDTree)
332 {
333 Int_t skey = 0;
334 TDirectory *dir = fESDTree->GetDirectory();
335 if (dir) {
336 TKey *key = dir->GetKey(fESDTree->GetName());
337 if (key) skey = key->GetKeylen();
338 }
339 total += skey;
340 if (fESDTree->GetZipBytes() > 0) total += fESDTree->GetTotBytes();
341 TBuffer b(TBuffer::kWrite,10000);
342 TTree::Class()->WriteBuffer(b,fESDTree);
343 total += b.Length();
344 }
345
346 return total;
347}
348
b7d09bb3 349//______________________________________________________________________________
350void AliRawDB::WriteStats(AliStats* stats)
351{
352 // Write stats to raw DB, local run DB and global MySQL DB.
353
f2dc6b20 354 AliRawEventHeaderBase &header = *GetEvent()->GetHeader();
b7d09bb3 355
356 // Write stats into RawDB
357 TDirectory *ds = gDirectory;
358 GetDB()->cd();
359 stats->SetEvents(GetEvents());
f2dc6b20 360 stats->SetLastId(header.Get("RunNb"), header.GetP("Id")[0]);
b7d09bb3 361 stats->SetFileSize(GetBytesWritten());
362 stats->SetCompressionFactor(GetCompressionFactor());
363 stats->SetEndTime();
364 stats->Write("stats");
365 ds->cd();
366}
367
a197a4ce 368//______________________________________________________________________________
e10815f1 369Bool_t AliRawDB::NextFile(const char* fileName)
a197a4ce 370{
371 // Close te current file and open a new one.
372 // Returns kFALSE in case opening failed.
373
374 Close();
375
e10815f1 376 if (!Create(fileName)) return kFALSE;
a197a4ce 377 return kTRUE;
378}
379
380//______________________________________________________________________________
381Float_t AliRawDB::GetCompressionFactor() const
382{
383 // Return compression factor.
384
385 if (fTree->GetZipBytes() == 0.)
386 return 1.0;
387 else
388 return fTree->GetTotBytes()/fTree->GetZipBytes();
389}
ee8f2a6c 390
391//______________________________________________________________________________
392const char *AliRawDB::GetAliRootTag()
393{
394 // Return the aliroot tag (version)
395 // used to generate the raw data file.
396 // Stored in the raw-data file title.
397
398 TString version = fgkAliRootTag;
399 version.Remove(TString::kBoth,'$');
400 version.ReplaceAll("Name","AliRoot version");
401
402 return version.Data();
403}