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>
29 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
30 #include <TBufferFile.h>
38 #include <TObjString.h>
42 #include "AliESDEvent.h"
43 #include "AliRawEventV2.h"
44 #include "AliRawDataArrayV2.h"
45 #include "AliRawEventHeaderBase.h"
46 #include "AliRawEquipmentHeader.h"
54 const char *AliRawDB::fgkAliRootTag = "$Rev$";
56 // Split TPC into 18 branches in order to avoid problems with big memory
57 // consumption in case of TPC events w/o zero-suppression
58 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};
60 //______________________________________________________________________________
61 AliRawDB::AliRawDB(AliRawEventV2 *event,
72 fBasketSize(basketsize),
79 // Create a new raw DB
81 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
82 fDetRawData[iDet] = new AliRawDataArrayV2*[fgkDetBranches[iDet]];
83 Int_t nDDLsPerBranch = AliDAQ::NumberOfDdls(iDet)/fgkDetBranches[iDet];
84 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
85 fDetRawData[iDet][iBranch] = new AliRawDataArrayV2(nDDLsPerBranch);
88 fDetRawData[AliDAQ::kNDetectors] = new AliRawDataArrayV2*[fgkDetBranches[AliDAQ::kNDetectors]];
89 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++)
90 fDetRawData[AliDAQ::kNDetectors][iBranch] = new AliRawDataArrayV2(100);
93 if (!Create(fileName))
99 //______________________________________________________________________________
100 AliRawDB::~AliRawDB() {
103 if(Close()==-1) Error("~AliRawDB", "cannot close output file!");
105 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++) {
106 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
107 delete fDetRawData[iDet][iBranch];
108 delete [] fDetRawData[iDet];
112 //______________________________________________________________________________
113 Bool_t AliRawDB::FSHasSpace(const char *fs) const
115 // Check for at least fMaxSize bytes of free space on the file system.
116 // If the space is not available return kFALSE, kTRUE otherwise.
118 Long_t id, bsize, blocks, bfree;
120 if (gSystem->GetFsInfo(fs, &id, &bsize, &blocks, &bfree) == 1) {
121 Error("FSHasSpace", "could not stat file system %s", fs);
125 // Leave 5 percent of diskspace free
126 Double_t avail = Double_t(bfree) * 0.95;
127 if (avail*bsize > fMaxSize)
130 Warning("FSHasSpace", "no space on file system %s", fs);
134 //______________________________________________________________________________
135 const char *AliRawDB::GetFileName() const
137 // Return filename based on hostname and date and time. This will make
138 // each file unique. Also makes sure (via FSHasSpace()) that there is
139 // enough space on the file system to store the file. Returns 0 in
140 // case of error or interrupt signal.
142 static TString fname;
143 static Bool_t fstoggle = kFALSE;
145 TString fs = fstoggle ? fFS2 : fFS1;
148 TString hostname = gSystem->HostName();
150 if ((pos = hostname.Index(".")) != kNPOS)
151 hostname.Remove(pos);
153 if (!FSHasSpace(fs)) {
155 fstoggle = !fstoggle;
156 fs = fstoggle ? fFS2 : fFS1;
157 if (FSHasSpace(fs)) break;
158 Info("GetFileName", "sleeping 30 seconds before retrying...");
159 gSystem->Sleep(30000); // sleep for 30 seconds
164 fname = fs + "/" + hostname + "_";
165 fname += dt.GetDate();
167 fname += dt.GetTime();
170 fstoggle = !fstoggle;
175 //______________________________________________________________________________
176 void AliRawDB::SetFS(const char* fs1, const char* fs2)
178 // set the file system location
181 if (fs1 && !fFS1.Contains(":")) {
182 gSystem->ResetErrno();
183 gSystem->MakeDirectory(fs1);
184 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
185 SysError("SetFS", "mkdir %s", fs1);
191 gSystem->ResetErrno();
192 gSystem->MakeDirectory(fs2);
193 if (gSystem->GetErrno() && gSystem->GetErrno() != EEXIST) {
194 SysError("SetFS", "mkdir %s", fs2);
199 //______________________________________________________________________________
200 Bool_t AliRawDB::Create(const char* fileName)
202 // Create a new raw DB.
204 const Int_t kMaxRetry = 1;
205 const Int_t kMaxSleep = 1; // seconds
206 const Int_t kMaxSleepLong = 10; // seconds
210 if (fStop) return kFALSE;
212 const char *fname = fileName;
213 if (!fname) fname = GetFileName();
215 Error("Create", "error getting raw DB file name");
221 fRawDB = TFile::Open(fname, GetOpenOption(),
222 Form("ALICE raw-data file (%s)", GetAliRootTag()), fCompress,
225 if (retry < kMaxRetry) {
226 Warning("Create", "failure to open file, sleeping %d %s before retrying...",
227 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
228 gSystem->Sleep(kMaxSleep*1000);
231 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
235 Warning("Create", "succeeded to open file after %d retries", retry);
237 if (fRawDB->IsZombie()) {
238 if (fRawDB->GetErrno() == ENOSPC ||
239 fRawDB->GetErrno() == 1018 || // SECOMERR
240 fRawDB->GetErrno() == 1027) { // SESYSERR
241 fRawDB->ResetErrno();
243 Warning("Create", "file is a zombie (no space), sleeping %d %s before retrying...",
244 kMaxSleepLong, kMaxSleepLong==1 ? "second" : "seconds");
245 gSystem->Sleep(kMaxSleepLong*1000); // sleep 10 seconds before retrying
248 Error("Create", "file %s is zombie", fname);
249 fRawDB->ResetErrno();
252 if (retry < kMaxRetry) {
253 Warning("Create", "file is a zombie, sleeping %d %s before retrying...",
254 kMaxSleep, kMaxSleep==1 ? "second" : "seconds");
255 gSystem->Sleep(kMaxSleep*1000);
258 Error("Create", "failure to open file %s after %d tries", fname, kMaxRetry);
265 static void BranchResetBit(TBranch *b)
267 // Reset MapObject on this branch and all the sub-branches
269 b->ResetBit( kBranchObject | kBranchAny ); // Or in newer ROOT: b->ResetBit( kMapObject )
270 TIter next( b->GetListOfBranches() );
272 while ( (sub = (TBranch*)next() ) ) {
273 BranchResetBit( sub );
277 //______________________________________________________________________________
278 void AliRawDB::MakeTree()
280 // Create ROOT Tree object container.
282 fTree = new TTree("RAW", Form("ALICE raw-data tree (%s)", GetAliRootTag()));
283 fTree->SetAutoSave(21000000000LL); // autosave when 21 Gbyte written
288 TBranch *b = fTree->Branch("rawevent", "AliRawEventV2", &fEvent, fBasketSize, split);
291 // Make brach for each sub-detector
292 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
293 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++) {
294 b = fTree->Branch(Form("%s%d",AliDAQ::DetectorName(iDet),iBranch),"AliRawDataArrayV2",
295 &fDetRawData[iDet][iBranch],fBasketSize,split);
299 // Make special branch for unrecognized raw-data payloads
300 for (Int_t iBranch = 0; iBranch < fgkDetBranches[AliDAQ::kNDetectors]; iBranch++) {
301 b = fTree->Branch(Form("Common%d",iBranch),"AliRawDataArrayV2",
302 &fDetRawData[AliDAQ::kNDetectors][iBranch],fBasketSize,split);
306 // Create tree which will contain the HLT ESD information
309 fESDTree = new TTree("esdTree", Form("ALICE HLT ESD tree (%s)", GetAliRootTag()));
310 fESDTree->SetAutoSave(21000000000LL); // autosave when 21 Gbyte written
312 fESDTree->Branch("ESD", "AliESDEvent", &fESD, fBasketSize, split);
317 //______________________________________________________________________________
318 Long64_t AliRawDB::Close()
321 if (!fRawDB) return 0;
323 if (!fRawDB->IsOpen()) return 0;
328 Bool_t error = kFALSE;
330 if (fTree->Write() == 0)
333 if (fESDTree->Write() == 0)
336 // Close DB, this also deletes the fTree
341 Long64_t filesize = fRawDB->GetEND();
344 gSystem->Unlink(fRawDB->GetName());
361 //______________________________________________________________________________
362 Int_t AliRawDB::Fill()
364 // Fill the trees and return the number of written bytes
366 // Create raw data TTree if it not yet done
367 if (!fTree) MakeTree();
369 Double_t bytes = fRawDB->GetBytesWritten();
370 Bool_t error = kFALSE;
371 if (fTree->Fill() == -1)
374 if (fESDTree->Fill() == -1)
377 return Int_t(fRawDB->GetBytesWritten() - bytes);
382 //______________________________________________________________________________
383 Long64_t AliRawDB::GetTotalSize()
385 // Return the total size of the trees
390 TDirectory *dir = fTree->GetDirectory();
392 TKey *key = dir->GetKey(fTree->GetName());
393 if (key) skey = key->GetKeylen();
395 total += (Long64_t)skey + fTree->GetZipBytes();
401 TDirectory *dir = fESDTree->GetDirectory();
403 TKey *key = dir->GetKey(fESDTree->GetName());
404 if (key) skey = key->GetKeylen();
406 total += (Long64_t)skey + fESDTree->GetZipBytes();
412 //______________________________________________________________________________
413 Long64_t AliRawDB::AutoSave()
415 // Auto-save the raw-data and
416 // esd (if any) trees
418 Long64_t nbytes = fTree->AutoSave();
420 if (fESDTree) nbytes += fESDTree->AutoSave();
425 //______________________________________________________________________________
426 Bool_t AliRawDB::NextFile(const char* fileName)
428 // Close te current file and open a new one.
429 // Returns kFALSE in case opening failed.
433 if (!Create(fileName)) return kFALSE;
437 //______________________________________________________________________________
438 Float_t AliRawDB::GetCompressionFactor() const
440 // Return compression factor.
442 if (fTree->GetZipBytes() == 0.)
445 return fTree->GetTotBytes()/fTree->GetZipBytes();
448 //______________________________________________________________________________
449 const char *AliRawDB::GetAliRootTag()
451 // Return the aliroot tag (version)
452 // used to generate the raw data file.
453 // Stored in the raw-data file title.
455 static TString version = fgkAliRootTag;
456 version.Remove(TString::kBoth,'$');
457 version.ReplaceAll("Rev","AliRoot version");
459 return version.Data();
462 //______________________________________________________________________________
463 Bool_t AliRawDB::WriteGuidFile(TString &guidFileFolder)
465 // Write the guid file
466 // in the specified folder or
467 // in the folder where the raw data
470 TString guidFileName;
471 if (!guidFileFolder.IsNull()) {
472 guidFileName = guidFileFolder;
474 TString pathStr = fRawDB->GetName();
475 TObjArray *pathArr = pathStr.Tokenize('/');
476 guidFileName.Append("/");
477 guidFileName.Append(((TObjString *)pathArr->Last())->String());
482 guidFileName = fRawDB->GetName();
484 guidFileName += ".guid";
486 ofstream fguid(guidFileName.Data());
487 if (!fguid.is_open()) {
488 Error("WriteGuidFile", "failure to open guid file %s", guidFileName.Data());
491 TString guid = fRawDB->GetUUID().AsString();
492 fguid << "guid: \t" << guid.Data();
499 //______________________________________________________________________________
500 void AliRawDB::Reset()
502 // Clear the raw-data arrays
503 // Should be done before processing the raw-data event
505 for (Int_t iDet = 0; iDet < (AliDAQ::kNDetectors + 1); iDet++)
506 for (Int_t iBranch = 0; iBranch < fgkDetBranches[iDet]; iBranch++)
507 fDetRawData[iDet][iBranch]->ClearData();
510 //______________________________________________________________________________
511 AliRawDataArrayV2 *AliRawDB::GetRawDataArray(UInt_t eqSize, UInt_t eqId) const
513 // Return the corresponding raw-datra array (branch)
514 // depending on the equipment ID
516 Int_t iDet = AliDAQ::kNDetectors;
517 Int_t iBranch = 0; // can we split somehow the unrecognized data??? For the moment - no
520 iDet = AliDAQ::DetectorIDFromDdlID(eqId,ddlIndex);
521 if (iDet < 0 || iDet >= AliDAQ::kNDetectors)
522 iDet = AliDAQ::kNDetectors;
524 iBranch = (ddlIndex * fgkDetBranches[iDet])/AliDAQ::NumberOfDdls(iDet);
527 return fDetRawData[iDet][iBranch];