Memory leak fixed
[u/mrichter/AliRoot.git] / RAW / AliRawCastorDB.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// AliRawCastorDB //
22// //
23//////////////////////////////////////////////////////////////////////////
24
25#include <TSystem.h>
26#include <TUrl.h>
27
a197a4ce 28#include "AliRawCastorDB.h"
29
30
31ClassImp(AliRawCastorDB)
32
33
34//______________________________________________________________________________
a109e73e 35AliRawCastorDB::AliRawCastorDB(AliRawEvent *event,
a109e73e 36 AliESD *esd,
e10815f1 37 Int_t compress,
38 const char* fileName)
39 : AliRawDB(event, esd, compress, fileName)
a197a4ce 40{
41 // Create a new raw DB that will be accessed via CASTOR and rootd.
42
a197a4ce 43 static int init = 0;
a197a4ce 44 if (!init) {
efd255fe 45 // THESE ENVIRONMENT VARIABLES ARE IN PRINCIPLE HARDCODED IN
46 // THE CASTOR CLIENT LIBRARY
47 // however for sanity we check if they are set by the user
48 if (!gSystem->Getenv("RH_HOST"))
49 Error("AliRawRFIODB", "RH_HOST not set");
50 if (!gSystem->Getenv("SVCCLASS"))
51 Error("AliRawRFIODB", "SVCCLASS not set");
a197a4ce 52 init = 1;
53 }
a197a4ce 54
e10815f1 55 if (fRawDB) fRawDB->UseCache(50, 0x200000); //0x100000 = 1MB)
a197a4ce 56}
57
58//______________________________________________________________________________
59const char *AliRawCastorDB::GetFileName() const
60{
61 // Return filename based on hostname and date and time. This will make
62 // each file unique. Also the directory will be made unique for each
63 // day by adding the date to the fs. Assumes there is always enough
64 // space on the device.
65
66 static TString fname;
67
e10815f1 68 TString fs = fFS1;
69 TString fsr = fs;
70 fsr.ReplaceAll("castor:", "rfio:");
a197a4ce 71 TDatime dt;
72
73 // make a new subdirectory for each day
74 fs += "/adc-";
75 fs += dt.GetDate();
76
77 fsr += "/adc-";
78 fsr += dt.GetDate();
79
80 Long_t id, size, flags, time;
81 if (gSystem->GetPathInfo(fsr, &id, &size, &flags, &time) == 1) {
82 // directory does not exist, create it
83 if (gSystem->mkdir(fsr, kTRUE) == -1) {
84 Error("GetFileName", "cannot create dir %s, using %s", fsr.Data(),
e10815f1 85 fFS1.Data());
86 fs = fFS1;
a197a4ce 87 }
88 }
89 // FIXME: should check if fs is a directory
90
91 TString hostname = gSystem->HostName();
92 Int_t pos;
93 if ((pos = hostname.Index(".")) != kNPOS)
94 hostname.Remove(pos);
95
96 fname = fs + "/" + hostname + "_";
97 fname += dt.GetDate();
98 fname += "_";
99 fname += dt.GetTime();
100 fname += ".root";
101
102 return fname;
103}
104
105//______________________________________________________________________________
f07ec911 106Int_t AliRawCastorDB::Close()
a197a4ce 107{
108 // Close raw CASTOR/rootd DB.
109
f07ec911 110 if (!fRawDB) return 0;
111
112 if (!fRawDB->IsOpen()) return 0;
a197a4ce 113
114 fRawDB->cd();
115
116 // Write the tree.
f07ec911 117 Bool_t error = kFALSE;
118 if (fTree->Write() == 0)
119 error = kTRUE;
120 if (fESDTree)
121 if (fESDTree->Write() == 0)
122 error = kTRUE;
a197a4ce 123
124 // Close DB, this also deletes the fTree
125 fRawDB->Close();
126
f07ec911 127 Int_t filesize = fRawDB->GetEND();
128
e10815f1 129 if (fDeleteFiles) {
a197a4ce 130 TUrl u(fRawDB->GetName());
131 gSystem->Exec(Form("rfrm %s", u.GetFile()));
132 }
133
134 delete fRawDB;
135 fRawDB = 0;
f07ec911 136
137 if(!error)
138 return filesize;
139 else
140 return -1;
a197a4ce 141}