]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRunDB.cxx
One run number entry in the raw data file statistics. Increase AliStats version
[u/mrichter/AliRoot.git] / RAW / AliRunDB.cxx
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"
34 #include "AliRawDB.h"
35
36 #include "AliRunDB.h"
37
38
39 ClassImp(AliRunDB)
40
41
42 //______________________________________________________________________________
43 AliRunDB::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)
49 {
50    // Open run database, and get or create tree.
51
52    if (!localFS) return;
53
54    // Get hostname
55    char hostname[64], filename[64];
56
57    // check that fs exists (crude check fails if fs is a file)
58    gSystem->MakeDirectory(localFS);
59
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
66    strcpy(hostname, gSystem->HostName());
67
68    char *s;
69    if ((s = strchr(hostname, '.')))
70       *s = 0;
71
72    sprintf(filename, "%s/%s_rundb.root", localFS, hostname);
73
74    if (!gSystem->AccessPathName(filename, kFileExists))
75       fRunDB = new TFile(filename, "UPDATE");
76    else
77      fRunDB = new TFile(filename, "CREATE", Form("ALICE Run DB (%s)", AliRawDB::GetAliRootTag()));
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    }
84 }
85
86 //______________________________________________________________________________
87 void AliRunDB::Update(AliStats *stats)
88 {
89   UpdateLocal(stats);
90   UpdateRDBMS(stats);
91   UpdateAliEn(stats);
92 }
93
94 //______________________________________________________________________________
95 void AliRunDB::UpdateLocal(AliStats *stats)
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 //______________________________________________________________________________
120 void AliRunDB::UpdateRDBMS(AliStats *stats)
121 {
122    // Add stats object to central MySQL DB.
123
124    if (!stats || !fRDBMS) return;
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
132    sprintf(sql, "INSERT INTO mdccatalog VALUES (0, '%s', %d, "
133            "%d, %d, %d, %d, %d, %.2f, '%s', '%s', '%s')",
134            stats->GetFileName(), (int)stats->GetFileSize(), stats->GetEvents(),
135            stats->GetRun(), stats->GetFirstEvent(),
136            stats->GetLastEvent(), stats->GetCompressionMode(),
137            stats->GetCompressionFactor(), stats->GetFilterState() ? "on" : "off",
138            bt, et);
139
140    // open connection to MySQL server on pcsalo
141 //    TSQLServer *db = TSQLServer::Connect("mysql://pcsalo.cern.ch/mdc", "alice", "amdc");
142
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 //    }
149
150 //    TSQLResult *res = db->Query(sql);
151
152 //    if (!res) {
153 //       Error("UpdateRDBMS", "insert into mdccatalog failed");
154 //       printf("%s\n", sql);
155 //    }
156
157 //    delete res;
158 //    delete db;
159 }
160
161 //______________________________________________________________________________
162 void AliRunDB::UpdateAliEn(AliStats *stats)
163 {
164    // Record file in AliEn catalog.
165
166    if (!stats || fAlienHost.IsNull()) return;
167
168    TGrid *g = TGrid::Connect(fAlienHost, "");
169
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
176    TString lfn = fAlienDir;
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
184 #if ROOT_VERSION_CODE < ROOT_VERSION(5,0,0)
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());
190          lfn = fAlienDir;
191       }
192    }
193    if (res) g->CloseResult(res);
194 #else
195    Error("UpdateAliEn", "needs to be ported to new TGrid");
196 #endif
197
198    lfn += "/";
199    lfn += gSystem->BaseName(stats->GetFileName());
200
201 #if ROOT_VERSION_CODE < ROOT_VERSION(5,0,0)
202    Int_t result = g->AddFile(lfn, stats->GetFileName(),
203                              (int)stats->GetFileSize());
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    }
209 #else
210    Error("UpdateAliEn", "needs to be ported to new TGrid");
211 #endif
212
213    delete g;
214 }
215
216 //______________________________________________________________________________
217 void AliRunDB::Close()
218 {
219    // Close run database.
220
221    if (fRunDB) fRunDB->Close();
222    delete fRunDB;
223 }
224