]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRunDB.cxx
Added method to enable the aliroot debug and log messages.
[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    strcpy(hostname, gSystem->HostName());
61
62    char *s;
63    if ((s = strchr(hostname, '.')))
64       *s = 0;
65
66    sprintf(filename, "%s/%s_rundb.root", localFS, hostname);
67
68    if (!gSystem->AccessPathName(filename, kFileExists))
69       fRunDB = new TFile(filename, "UPDATE");
70    else
71       fRunDB = new TFile(filename, "CREATE", Form("ALICE MDC%d Run DB", AliRawDB::kMDC));
72 }
73
74 //______________________________________________________________________________
75 AliRunDB::AliRunDB(const AliRunDB& runDB): TObject(runDB)
76 {
77 // copy constructor
78
79   Fatal("AliRunDB", "copy constructor not implemented");
80 }
81
82 //______________________________________________________________________________
83 AliRunDB& AliRunDB::operator = (const AliRunDB& /*runDB*/)
84 {
85 // assignment operator
86
87   Fatal("operator =", "assignment operator not implemented");
88   return *this;
89 }
90
91 //______________________________________________________________________________
92 void AliRunDB::Update(AliStats *stats)
93 {
94   UpdateLocal(stats);
95   UpdateRDBMS(stats);
96   UpdateAliEn(stats);
97 }
98
99 //______________________________________________________________________________
100 void AliRunDB::UpdateLocal(AliStats *stats)
101 {
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 //______________________________________________________________________________
125 void AliRunDB::UpdateRDBMS(AliStats *stats)
126 {
127    // Add stats object to central MySQL DB.
128
129    if (!stats || !fRDBMS) return;
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, "
138            "%d, %d, %d, %d, %d, %d, %.2f, '%s', '%s', '%s')", AliRawDB::kMDC,
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) {
158       Error("UpdateRDBMS", Form("insert into mdc%dcatalog failed", AliRawDB::kMDC));
159       printf("%s\n", sql);
160    }
161
162    delete res;
163    delete db;
164 }
165
166 //______________________________________________________________________________
167 void AliRunDB::UpdateAliEn(AliStats *stats)
168 {
169    // Record file in AliEn catalog.
170
171    if (!stats || fAlienHost.IsNull()) return;
172
173    TGrid *g = TGrid::Connect(fAlienHost, "");
174
175    TString lfn = fAlienDir;
176    TDatime dt;
177
178    // make a subdirectory for each day
179    lfn += "/adc-";
180    lfn += dt.GetDate();
181
182    // check if directory exists, if not create it
183    Grid_ResultHandle_t res = 0;
184    if (!(res = g->OpenDir(lfn))) {
185       // directory does not exist, create it
186       if (g->Mkdir(lfn) == -1) {
187          Error("UpdateAliEn", "cannot create directory %s", lfn.Data());
188          lfn = fAlienDir;
189       }
190    }
191    if (res) g->CloseResult(res);
192
193    lfn += "/";
194    lfn += gSystem->BaseName(stats->GetFileName());
195
196    Int_t result = g->AddFile(lfn, stats->GetFileName(),
197                              (int)stats->GetFileSize());
198
199    if (result == -1) {
200       Error("UpdateAliEn", "error adding file to AliEn catalog");
201       printf("AliEn: AddFile(%s, %s, %d)\n", lfn.Data(), stats->GetFileName(),
202              (int)stats->GetFileSize());
203    }
204
205    delete g;
206 }
207
208 //______________________________________________________________________________
209 void AliRunDB::Close()
210 {
211    // Close run database.
212
213    if (fRunDB) fRunDB->Close();
214    delete fRunDB;
215 }
216