]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RAW/AliRunDB.cxx
START becomes T0
[u/mrichter/AliRoot.git] / RAW / AliRunDB.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// AliRunDB //
22// //
23//////////////////////////////////////////////////////////////////////////
24
25#include <TSystem.h>
26#include <TFile.h>
27#include <TString.h>
28#include <TDatime.h>
29#include <TSQLServer.h>
30#include <TSQLResult.h>
31#include <TGrid.h>
32
33#include "AliStats.h"
e10815f1 34#include "AliRawDB.h"
a197a4ce 35
36#include "AliRunDB.h"
37
38
39ClassImp(AliRunDB)
40
41
42//______________________________________________________________________________
43073eba 43AliRunDB::AliRunDB(const char* localFS, Bool_t rdbms,
e10815f1 44 const char* alienHost, const char* alienDir) :
45 fRunDB(NULL),
46 fRDBMS(rdbms),
47 fAlienHost(alienHost),
48 fAlienDir(alienDir)
a197a4ce 49{
50 // Open run database, and get or create tree.
51
e10815f1 52 if (!localFS) return;
a197a4ce 53
54 // Get hostname
55 char hostname[64], filename[64];
a197a4ce 56
57 // check that fs exists (crude check fails if fs is a file)
e10815f1 58 gSystem->MakeDirectory(localFS);
a197a4ce 59
f07ec911 60 // Put wide read-write permissions
61 if(gSystem->Chmod(localFS,1023)) {
62 Error("AliRunDB","can't set permissions for run DB directory");
63 return;
64 }
65
a197a4ce 66 strcpy(hostname, gSystem->HostName());
67
68 char *s;
69 if ((s = strchr(hostname, '.')))
70 *s = 0;
71
e10815f1 72 sprintf(filename, "%s/%s_rundb.root", localFS, hostname);
a197a4ce 73
74 if (!gSystem->AccessPathName(filename, kFileExists))
75 fRunDB = new TFile(filename, "UPDATE");
76 else
ee8f2a6c 77 fRunDB = new TFile(filename, "CREATE", Form("ALICE Run DB (%s)", AliRawDB::GetAliRootTag()));
f07ec911 78
79 // Put wide read-write permissions
80 if(gSystem->Chmod(filename,438)) {
81 Error("AliRunDB","can't set permissions for run DB file");
82 return;
83 }
a197a4ce 84}
85
a197a4ce 86//______________________________________________________________________________
87void AliRunDB::Update(AliStats *stats)
e10815f1 88{
89 UpdateLocal(stats);
90 UpdateRDBMS(stats);
91 UpdateAliEn(stats);
92}
93
94//______________________________________________________________________________
95void AliRunDB::UpdateLocal(AliStats *stats)
a197a4ce 96{
97 // Add stats object to database.
98
99 if (!stats || !fRunDB) return;
100
101 TDirectory *ds = gDirectory;
102 fRunDB->cd();
103
104 char sname[64];
105 char *s = (char*)strrchr(stats->GetFileName(), '/');
106 if (s) {
107 s++;
108 strcpy(sname, s);
109 } else
110 strcpy(sname, stats->GetFileName());
111 s = strchr(sname, '.');
112 if (s) *s = 0;
113
114 stats->Write(sname);
115
116 ds->cd();
117}
118
119//______________________________________________________________________________
120void AliRunDB::UpdateRDBMS(AliStats *stats)
121{
122 // Add stats object to central MySQL DB.
123
e10815f1 124 if (!stats || !fRDBMS) return;
a197a4ce 125
126 char sql[4096];
127 char bt[25], et[25];
128
129 strcpy(bt, stats->GetBeginTime().AsSQLString());
130 strcpy(et, stats->GetEndTime().AsSQLString());
131
ee8f2a6c 132 sprintf(sql, "INSERT INTO mdccatalog VALUES (0, '%s', %d, "
133 "%d, %d, %d, %d, %d, %d, %.2f, '%s', '%s', '%s')",
a197a4ce 134 stats->GetFileName(), (int)stats->GetFileSize(), stats->GetEvents(),
135 stats->GetFirstRun(), stats->GetFirstEvent(), stats->GetLastRun(),
136 stats->GetLastEvent(), stats->GetCompressionMode(),
137 stats->GetCompressionFactor(), stats->GetFilterState() ? "on" : "off",
138 bt, et);
139
140 // open connection to MySQL server on pcsalo
a2f3d533 141// TSQLServer *db = TSQLServer::Connect("mysql://pcsalo.cern.ch/mdc", "alice", "amdc");
a197a4ce 142
a2f3d533 143// if (!db || db->IsZombie()) {
144// Error("UpdateRDBMS", "failed to connect to MySQL server on pcsalo");
145// printf("%s\n", sql);
146// delete db;
147// return;
148// }
a197a4ce 149
a2f3d533 150// TSQLResult *res = db->Query(sql);
a197a4ce 151
a2f3d533 152// if (!res) {
ee8f2a6c 153// Error("UpdateRDBMS", "insert into mdccatalog failed");
a2f3d533 154// printf("%s\n", sql);
155// }
a197a4ce 156
a2f3d533 157// delete res;
158// delete db;
a197a4ce 159}
160
161//______________________________________________________________________________
162void AliRunDB::UpdateAliEn(AliStats *stats)
163{
164 // Record file in AliEn catalog.
165
e10815f1 166 if (!stats || fAlienHost.IsNull()) return;
a197a4ce 167
e10815f1 168 TGrid *g = TGrid::Connect(fAlienHost, "");
a197a4ce 169
424d21e8 170 //Protection in case root is compiled without AliEn support
171 if(!g) {
172 Error("UpdateAliEn", "ROOT compiled without AliEn support");
173 return;
174 }
175
e10815f1 176 TString lfn = fAlienDir;
a197a4ce 177 TDatime dt;
178
179 // make a subdirectory for each day
180 lfn += "/adc-";
181 lfn += dt.GetDate();
182
183 // check if directory exists, if not create it
43073eba 184#if ROOT_VERSION_CODE < ROOT_VERSION(5,0,0)
a197a4ce 185 Grid_ResultHandle_t res = 0;
186 if (!(res = g->OpenDir(lfn))) {
187 // directory does not exist, create it
188 if (g->Mkdir(lfn) == -1) {
189 Error("UpdateAliEn", "cannot create directory %s", lfn.Data());
e10815f1 190 lfn = fAlienDir;
a197a4ce 191 }
192 }
193 if (res) g->CloseResult(res);
43073eba 194#else
195 Error("UpdateAliEn", "needs to be ported to new TGrid");
196#endif
a197a4ce 197
198 lfn += "/";
199 lfn += gSystem->BaseName(stats->GetFileName());
200
43073eba 201#if ROOT_VERSION_CODE < ROOT_VERSION(5,0,0)
a197a4ce 202 Int_t result = g->AddFile(lfn, stats->GetFileName(),
203 (int)stats->GetFileSize());
a197a4ce 204 if (result == -1) {
205 Error("UpdateAliEn", "error adding file to AliEn catalog");
206 printf("AliEn: AddFile(%s, %s, %d)\n", lfn.Data(), stats->GetFileName(),
207 (int)stats->GetFileSize());
208 }
43073eba 209#else
210 Error("UpdateAliEn", "needs to be ported to new TGrid");
211#endif
a197a4ce 212
213 delete g;
214}
215
216//______________________________________________________________________________
217void AliRunDB::Close()
218{
219 // Close run database.
220
221 if (fRunDB) fRunDB->Close();
222 delete fRunDB;
223}
b7d09bb3 224