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>
41 #include "AliESDEvent.h"
42 #include "AliRawEventV2.h"
43 #include "AliRawDataArrayV2.h"
44 #include "AliRawEventHeaderBase.h"
45 #include "AliRawEquipmentHeader.h"
52 const char *AliRawDB::fgkAliRootTag = "$Rev$";
54 // Split TPC into 18 branches in order to avoid problems with big memory
55 // consumption in case of TPC events w/o zero-suppression
56 Int_t AliRawDB::fgkDetBranches[AliDAQ::kNDetectors+1] = {1,1,1,18,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,1};
58 //______________________________________________________________________________
59 AliRawDB::AliRawDB(AliRawEventV2 *event,
70 fBasketSize(basketsize),
77 // Create a new raw DB
79 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
80 fDetRawData[iDet] = new AliRawDataArrayV2*[fgkDetBranches[iDet]];
81 Int_t nDDLsPerBranch = AliDAQ::NumberOfDdls(iDet)/fgkDetBranches[iDet];
82 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
83 fDetRawData[iDet][iBranch] = new AliRawDataArrayV2(nDDLsPerBranch);
86 fDetRawData[AliDAQ::kNDetectors] = new AliRawDataArrayV2*[fgkDetBranches[AliDAQ::kNDetectors]];
87 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++)
88 fDetRawData[AliDAQ::kNDetectors][iBranch] = new AliRawDataArrayV2(100);
91 if (!Create(fileName))
97 //______________________________________________________________________________
98 AliRawDB::~AliRawDB() {
101 if(Close()==-1) Error("~AliRawDB", "cannot close output file!");
103 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++) {
104 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
105 delete fDetRawData[iDet][iBranch];
106 delete [] fDetRawData[iDet];
110 //______________________________________________________________________________
111 Bool_t AliRawDB::FSHasSpace(const char *fs) const
113 // Check for at least fMaxSize bytes of free space on the file system.
114 // If the space is not available return kFALSE, kTRUE otherwise.
116 Long_t id, bsize, blocks, bfree;
118 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
119 Error("FSHasSpace", "could not stat file system %s", fs);
123 // Leave 5 percent of diskspace free
124 Double_t avail = Double_t(bfree) * 0.95;
125 if (avail*bsize > fMaxSize)
128 Warning("FSHasSpace", "no space on file system %s", fs);
132 //______________________________________________________________________________
133 const char *AliRawDB::GetFileName() const
135 // Return filename based on hostname and date and time. This will make
136 // each file unique. Also makes sure (via FSHasSpace()) that there is
137 // enough space on the file system to store the file. Returns 0 in
138 // case of error or interrupt signal.
140 static TString fname;
141 static Bool_t fstoggle = kFALSE;
143 TString fs = fstoggle ? fFS2 : fFS1;
146 TString hostname = gSystem->HostName();
148 if ((pos = hostname.Index(".")) != kNPOS)
149 hostname.Remove(pos);
151 if (!FSHasSpace(fs)) {
153 fstoggle = !fstoggle;
154 fs = fstoggle ? fFS2 : fFS1;
155 if (FSHasSpace(fs)) break;
156 Info("GetFileName", "sleeping 30 seconds before retrying...");
157 gSystem->Sleep(30000); // sleep for 30 seconds
162 fname = fs + "/" + hostname + "_";
163 fname += dt.GetDate();
165 fname += dt.GetTime();
168 fstoggle = !fstoggle;
173 //______________________________________________________________________________
174 void AliRawDB::SetFS(const char* fs1, const char* fs2)
176 // set the file system location
179 if (fs1 && !fFS1.Contains(":")) {
180 gSystem->ResetErrno();
181 gSystem->MakeDirectory(fs1);
182 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
183 SysError("SetFS", "mkdir %s", fs1);
189 gSystem->ResetErrno();
190 gSystem->MakeDirectory(fs2);
191 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
192 SysError("SetFS", "mkdir %s", fs2);
197 //______________________________________________________________________________
198 Bool_t AliRawDB::Create(const char* fileName)
200 // Create a new raw DB.
202 const Int_t kMaxRetry = 1;
203 const Int_t kMaxSleep = 1; // seconds
204 const Int_t kMaxSleepLong = 10; // seconds
208 if (fStop) return kFALSE;
210 const char *fname = fileName;
211 if (!fname) fname = GetFileName();
213 Error("Create", "error getting raw DB file name");
219 fRawDB = TFile::Open(fname, GetOpenOption(),
220 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
223 if (retry < kMaxRetry) {
224 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
225 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
226 gSystem->Sleep(kMaxSleep*1000);
229 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
233 Warning("Create", "succeeded to open file after %d retries", retry);
235 if (fRawDB->IsZombie()) {
236 if (fRawDB->GetErrno() == ENOSPC ||
237 fRawDB->GetErrno() == 1018 || // SECOMERR
238 fRawDB->GetErrno() == 1027) { // SESYSERR
239 fRawDB->ResetErrno();
241 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
242 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
243 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
246 Error("Create", "file %s is zombie", fname);
247 fRawDB->ResetErrno();
250 if (retry < kMaxRetry) {
251 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
252 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
253 gSystem->Sleep(kMaxSleep*1000);
256 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
263 static void BranchResetBit(TBranch *b)
265 // Reset MapObject on this branch and all the sub-branches
267 b->ResetBit( kBranchObject | kBranchAny ); // Or in newer ROOT: b->ResetBit( kMapObject )
268 TIter next( b->GetListOfBranches() );
270 while ( (sub = (TBranch*)next() ) ) {
271 BranchResetBit( sub );
275 //______________________________________________________________________________
276 void AliRawDB::MakeTree()
278 // Create ROOT Tree object container.
280 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
281 fTree->SetAutoSave(21000000000LL); // autosave when 21 Gbyte written
286 TBranch *b = fTree->Branch("rawevent", "AliRawEventV2", &fEvent, fBasketSize, split);
289 // Make brach for each sub-detector
290 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
291 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++) {
292 b = fTree->Branch(Form("%s%d",AliDAQ::DetectorName(iDet),iBranch),"AliRawDataArrayV2",
293 &fDetRawData[iDet][iBranch],fBasketSize,split);
297 // Make special branch for unrecognized raw-data payloads
298 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++) {
299 b = fTree->Branch(Form("Common%d",iBranch),"AliRawDataArrayV2",
300 &fDetRawData[AliDAQ::kNDetectors][iBranch],fBasketSize,split);
304 // Create tree which will contain the HLT ESD information
307 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
308 fESDTree->SetAutoSave(21000000000LL); // autosave when 21 Gbyte written
310 fESDTree->Branch("ESD", "AliESDEvent", &fESD, fBasketSize, split);
315 //______________________________________________________________________________
316 Long64_t AliRawDB::Close()
319 if (!fRawDB) return 0;
321 if (!fRawDB->IsOpen()) return 0;
326 Bool_t error = kFALSE;
328 if (fTree->Write() == 0)
331 if (fESDTree->Write() == 0)
334 // Close DB, this also deletes the fTree
339 Long64_t filesize = fRawDB->GetEND();
342 gSystem->Unlink(fRawDB->GetName());
359 //______________________________________________________________________________
360 Int_t AliRawDB::Fill()
362 // Fill the trees and return the number of written bytes
364 // Create raw data TTree if it not yet done
365 if (!fTree) MakeTree();
367 Double_t bytes = fRawDB->GetBytesWritten();
368 Bool_t error = kFALSE;
369 if (fTree->Fill() == -1)
372 if (fESDTree->Fill() == -1)
375 return Int_t(fRawDB->GetBytesWritten() - bytes);
380 //______________________________________________________________________________
381 Long64_t AliRawDB::GetTotalSize()
383 // Return the total size of the trees
388 TDirectory *dir = fTree->GetDirectory();
390 TKey *key = dir->GetKey(fTree->GetName());
391 if (key) skey = key->GetKeylen();
393 total += (Long64_t)skey + fTree->GetZipBytes();
399 TDirectory *dir = fESDTree->GetDirectory();
401 TKey *key = dir->GetKey(fESDTree->GetName());
402 if (key) skey = key->GetKeylen();
404 total += (Long64_t)skey + fESDTree->GetZipBytes();
410 //______________________________________________________________________________
411 Long64_t AliRawDB::AutoSave()
413 // Auto-save the raw-data and
414 // esd (if any) trees
416 Long64_t nbytes = fTree->AutoSave();
418 if (fESDTree) nbytes += fESDTree->AutoSave();
423 //______________________________________________________________________________
424 Bool_t AliRawDB::NextFile(const char* fileName)
426 // Close te current file and open a new one.
427 // Returns kFALSE in case opening failed.
431 if (!Create(fileName)) return kFALSE;
435 //______________________________________________________________________________
436 Float_t AliRawDB::GetCompressionFactor() const
438 // Return compression factor.
440 if (fTree->GetZipBytes() == 0.)
443 return fTree->GetTotBytes()/fTree->GetZipBytes();
446 //______________________________________________________________________________
447 const char *AliRawDB::GetAliRootTag()
449 // Return the aliroot tag (version)
450 // used to generate the raw data file.
451 // Stored in the raw-data file title.
453 static TString version = fgkAliRootTag;
454 version.Remove(TString::kBoth,'$');
455 version.ReplaceAll("Rev","AliRoot version");
457 return version.Data();
460 //______________________________________________________________________________
461 Bool_t AliRawDB::WriteGuidFile(TString &guidFileFolder)
463 // Write the guid file
464 // in the specified folder or
465 // in the folder where the raw data
468 TString guidFileName;
469 if (!guidFileFolder.IsNull()) {
470 guidFileName = guidFileFolder;
472 TString pathStr = fRawDB->GetName();
473 TObjArray *pathArr = pathStr.Tokenize('/');
474 guidFileName.Append("/");
475 guidFileName.Append(((TObjString *)pathArr->Last())->String());
480 guidFileName = fRawDB->GetName();
482 guidFileName += ".guid";
484 ofstream fguid(guidFileName.Data());
485 if (!fguid.is_open()) {
486 Error("WriteGuidFile", "failure to open guid file %s", guidFileName.Data());
489 TString guid = fRawDB->GetUUID().AsString();
490 fguid << "guid: \t" << guid.Data();
497 //______________________________________________________________________________
498 void AliRawDB::Reset()
500 // Clear the raw-data arrays
501 // Should be done before processing the raw-data event
503 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++)
504 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
505 fDetRawData[iDet][iBranch]->ClearData();
508 //______________________________________________________________________________
509 AliRawDataArrayV2 *AliRawDB::GetRawDataArray(UInt_t eqSize, UInt_t eqId) const
511 // Return the corresponding raw-datra array (branch)
512 // depending on the equipment ID
514 Int_t iDet = AliDAQ::kNDetectors;
515 Int_t iBranch = 0; // can we split somehow the unrecognized data??? For the moment - no
518 iDet = AliDAQ::DetectorIDFromDdlID(eqId,ddlIndex);
519 if (iDet < 0 || iDet >= AliDAQ::kNDetectors)
520 iDet = AliDAQ::kNDetectors;
522 iBranch = (ddlIndex * fgkDetBranches[iDet])/AliDAQ::NumberOfDdls(iDet);
525 return fDetRawData[iDet][iBranch];