Additional protection in case ROOT is compiled without AliEn support
[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//______________________________________________________________________________
e10815f1 43AliRunDB::AliRunDB(const char* localFS, Bool_t rdbms,
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
60 strcpy(hostname, gSystem->HostName());
61
62 char *s;
63 if ((s = strchr(hostname, '.')))
64 *s = 0;
65
e10815f1 66 sprintf(filename, "%s/%s_rundb.root", localFS, hostname);
a197a4ce 67
68 if (!gSystem->AccessPathName(filename, kFileExists))
69 fRunDB = new TFile(filename, "UPDATE");
70 else
e10815f1 71 fRunDB = new TFile(filename, "CREATE", Form("ALICE MDC%d Run DB", AliRawDB::kMDC));
a197a4ce 72}
73
74//______________________________________________________________________________
75AliRunDB::AliRunDB(const AliRunDB& runDB): TObject(runDB)
76{
77// copy constructor
78
79 Fatal("AliRunDB", "copy constructor not implemented");
80}
81
82//______________________________________________________________________________
83AliRunDB& AliRunDB::operator = (const AliRunDB& /*runDB*/)
84{
85// assignment operator
86
87 Fatal("operator =", "assignment operator not implemented");
88 return *this;
89}
90
91//______________________________________________________________________________
92void AliRunDB::Update(AliStats *stats)
93{
e10815f1 94 UpdateLocal(stats);
95 UpdateRDBMS(stats);
96 UpdateAliEn(stats);
97}
98
99//______________________________________________________________________________
100void AliRunDB::UpdateLocal(AliStats *stats)
101{
a197a4ce 102 // Add stats object to database.
103
104 if (!stats || !fRunDB) return;
105
106 TDirectory *ds = gDirectory;
107 fRunDB->cd();
108
109 char sname[64];
110 char *s = (char*)strrchr(stats->GetFileName(), '/');
111 if (s) {
112 s++;
113 strcpy(sname, s);
114 } else
115 strcpy(sname, stats->GetFileName());
116 s = strchr(sname, '.');
117 if (s) *s = 0;
118
119 stats->Write(sname);
120
121 ds->cd();
122}
123
124//______________________________________________________________________________
125void AliRunDB::UpdateRDBMS(AliStats *stats)
126{
127 // Add stats object to central MySQL DB.
128
e10815f1 129 if (!stats || !fRDBMS) return;
a197a4ce 130
131 char sql[4096];
132 char bt[25], et[25];
133
134 strcpy(bt, stats->GetBeginTime().AsSQLString());
135 strcpy(et, stats->GetEndTime().AsSQLString());
136
137 sprintf(sql, "INSERT INTO mdc%dcatalog VALUES (0, '%s', %d, "
e10815f1 138 "%d, %d, %d, %d, %d, %d, %.2f, '%s', '%s', '%s')", AliRawDB::kMDC,
a197a4ce 139 stats->GetFileName(), (int)stats->GetFileSize(), stats->GetEvents(),
140 stats->GetFirstRun(), stats->GetFirstEvent(), stats->GetLastRun(),
141 stats->GetLastEvent(), stats->GetCompressionMode(),
142 stats->GetCompressionFactor(), stats->GetFilterState() ? "on" : "off",
143 bt, et);
144
145 // open connection to MySQL server on pcsalo
146 TSQLServer *db = TSQLServer::Connect("mysql://pcsalo.cern.ch/mdc", "alice", "amdc");
147
148 if (!db || db->IsZombie()) {
149 Error("UpdateRDBMS", "failed to connect to MySQL server on pcsalo");
150 printf("%s\n", sql);
151 delete db;
152 return;
153 }
154
155 TSQLResult *res = db->Query(sql);
156
157 if (!res) {
e10815f1 158 Error("UpdateRDBMS", Form("insert into mdc%dcatalog failed", AliRawDB::kMDC));
a197a4ce 159 printf("%s\n", sql);
160 }
161
162 delete res;
163 delete db;
164}
165
166//______________________________________________________________________________
167void AliRunDB::UpdateAliEn(AliStats *stats)
168{
169 // Record file in AliEn catalog.
170
e10815f1 171 if (!stats || fAlienHost.IsNull()) return;
a197a4ce 172
e10815f1 173 TGrid *g = TGrid::Connect(fAlienHost, "");
a197a4ce 174
424d21e8 175 //Protection in case root is compiled without AliEn support
176 if(!g) {
177 Error("UpdateAliEn", "ROOT compiled without AliEn support");
178 return;
179 }
180
e10815f1 181 TString lfn = fAlienDir;
a197a4ce 182 TDatime dt;
183
184 // make a subdirectory for each day
185 lfn += "/adc-";
186 lfn += dt.GetDate();
187
188 // check if directory exists, if not create it
189 Grid_ResultHandle_t res = 0;
190 if (!(res = g->OpenDir(lfn))) {
191 // directory does not exist, create it
192 if (g->Mkdir(lfn) == -1) {
193 Error("UpdateAliEn", "cannot create directory %s", lfn.Data());
e10815f1 194 lfn = fAlienDir;
a197a4ce 195 }
196 }
197 if (res) g->CloseResult(res);
198
199 lfn += "/";
200 lfn += gSystem->BaseName(stats->GetFileName());
201
202 Int_t result = g->AddFile(lfn, stats->GetFileName(),
203 (int)stats->GetFileSize());
204
205 if (result == -1) {
206 Error("UpdateAliEn", "error adding file to AliEn catalog");
207 printf("AliEn: AddFile(%s, %s, %d)\n", lfn.Data(), stats->GetFileName(),
208 (int)stats->GetFileSize());
209 }
210
211 delete g;
212}
213
214//______________________________________________________________________________
215void AliRunDB::Close()
216{
217 // Close run database.
218
219 if (fRunDB) fRunDB->Close();
220 delete fRunDB;
221}
b7d09bb3 222