]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBDump.cxx
Renaming calibration classes (A.Colla)
[u/mrichter/AliRoot.git] / STEER / AliCDBDump.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // access classes for a data base in a LOCAL file                            //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24
25 #include <TFile.h>
26 #include <TKey.h>
27 #include <TROOT.h>
28 #include "AliLog.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBMetaData.h"
31 #include "AliCDBDump.h"
32
33
34 ClassImp(AliCDBDump)
35
36
37 //_____________________________________________________________________________
38 AliCDBDump::AliCDBDump(const char* fileName, Bool_t readOnly) :
39   AliCDBStorage(),
40   fFile(NULL)
41 {
42 // constructor
43
44   if (!fileName) {
45     AliError("no file name given");
46     return;
47   }
48   TDirectory* saveDir = gDirectory;
49   fFile = TFile::Open(fileName, ((readOnly) ? "READ" : "UPDATE"));
50   if (saveDir) saveDir->cd(); else gROOT->cd();
51   if (!fFile || !fFile->IsOpen()) {
52     AliError(Form("could not open file %s", fileName));
53     fFile = NULL;
54   }
55 }
56
57 //_____________________________________________________________________________
58 AliCDBDump::~AliCDBDump()
59 {
60 // destructor
61
62   if (fFile) {
63     fFile->Close();
64     delete fFile;
65   }
66 }
67
68 //_____________________________________________________________________________
69 AliCDBDump::AliCDBDump(const AliCDBDump& /*db*/) :
70   AliCDBStorage(),
71   fFile(NULL)
72 {
73 // copy constructor
74
75   AliFatal("not implemented");
76 }
77
78 //_____________________________________________________________________________
79 AliCDBDump& AliCDBDump::operator = (const AliCDBDump& /*db*/)
80 {
81 // assignment operator
82
83   AliFatal("not implemented");
84   return *this;
85 }
86
87
88 //_____________________________________________________________________________
89 AliCDBEntry* AliCDBDump::GetEntry(AliCDBMetaDataSelect& selMetaData, Int_t runNumber)
90 {
91 // get an object from the data base
92
93   // go to the directory
94   TDirectory* saveDir = gDirectory;
95   TDirectory *dir = fFile;
96   TString name(selMetaData.GetName());
97   Int_t last = name.Last('/');
98   if (last < 0) {
99     fFile->cd();
100   } else {
101     TString dirName(name(0, last));
102       if (!dir->cd(dirName)) {
103       AliError(Form("no directory %s found", dirName.Data()));
104       if (saveDir) saveDir->cd(); else gROOT->cd();
105       return NULL;
106     }
107     name.Remove(0, last+1);
108   }
109
110   dir = gDirectory;
111   TKey* key = dir->GetKey(name); 
112   if (!key) {
113     AliError(Form("no object with name %s found", selMetaData.GetName()));
114     if (saveDir) saveDir->cd(); else gROOT->cd();
115     return NULL;
116   }
117   Int_t nCycles = key->GetCycle();
118
119   // find the closest entry
120   AliCDBEntry* closestEntry = NULL;
121   for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
122     key = dir->GetKey(name, iCycle);
123     
124     if (!key) continue;
125     AliCDBEntry* entry = (AliCDBEntry*) key->ReadObj();
126     if (!entry) continue;
127     if (!entry->InheritsFrom(AliCDBEntry::Class())) {
128       AliCDBMetaData metaData;
129       entry = new AliCDBEntry(entry, metaData);
130     }
131     if (!entry->GetCDBMetaData().IsStrictlyValid(runNumber, &selMetaData) ||
132         (entry->Compare(closestEntry) <= 0)) {
133       delete entry;
134       continue;
135     }
136     delete closestEntry;
137     closestEntry = entry;
138   }
139   if (saveDir) saveDir->cd(); else gROOT->cd();
140   if(!closestEntry) AliError(Form("No valid entry found for: name %s, version %d, run %d!!!",
141             selMetaData.GetName(),selMetaData.GetVersion(),runNumber));
142   if (!closestEntry) return NULL;
143
144 //   if(selMetaData.GetVersion() > -1 && (closestEntry->GetCDBMetaData()).GetVersion() != selMetaData.GetVersion()) 
145 //      AliWarning(Form("Warning: selected version (%d) not found, got version %d instead",
146 //             selMetaData.GetVersion(),(closestEntry->GetCDBMetaData()).GetVersion()));
147
148   return closestEntry;
149 }
150
151 //_____________________________________________________________________________
152 Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry)
153 {
154 // puts an object into the database
155
156 // AliCDBEntry entry is composed by the object and its MetaData
157 // this method takes the metaData, reads the name, runRange and Version
158 // creates the TDirectory structure into the file
159 // looks for runs with same name, if exist increment version
160 // (therefore version should not be put in the metadata)
161 // Note: the key name of the entry is "DetSpecType"
162 // return result 
163
164   if (!entry || !fFile) return kFALSE;
165   if (!fFile->IsWritable()) {
166     AliError(Form("The data base file was opened in read only mode. "
167                   "The object %s was not inserted", entry->GetName()));
168     return kFALSE;
169   }
170   
171   fFile->cd();
172   TDirectory* saveDir = gDirectory;
173
174   // go to or create the directory
175   TString name(entry->GetName());
176   while (name.BeginsWith("/")) name.Remove(0);
177   TDirectory* dir = fFile;
178   Int_t index = -1;
179   while ((index = name.Index("/")) >= 0) {
180     TString dirName(name(0, index));
181     if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
182     dir->cd(dirName);
183     dir = gDirectory;
184     name.Remove(0, index+1);
185   } 
186
187   // determine the version number
188   Int_t version = 0;
189   TKey* key = dir->GetKey(name); 
190   if (key) {
191     Int_t nCycles = key->GetCycle();
192     for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
193       key = dir->GetKey(name, iCycle); 
194       if (!key) continue;
195       AliCDBEntry* oldEntry = (AliCDBEntry*) key->ReadObj();
196       if (!oldEntry) continue;
197       if (oldEntry->InheritsFrom(AliCDBEntry::Class())) {
198         if (version <= oldEntry->GetCDBMetaData().GetVersion()) {
199           version = oldEntry->GetCDBMetaData().GetVersion()+1;
200         }
201       }
202       delete oldEntry;
203     }
204   }
205   entry->SetVersion(version);
206
207   Bool_t result = (entry->Write(name) != 0);
208   if (saveDir) saveDir->cd(); else gROOT->cd();
209   
210   if(result) {
211     AliInfo(Form("Run object %s",entry->GetName()));
212     AliInfo(Form("was successfully written into file %s",fFile->GetName()));
213   }  
214   
215   return result;
216 }
217
218 //_____________________________________________________________________________
219 void AliCDBDump::TagForProduction
220                 (const AliCDBMetaDataSelect& /* selMetaData */, UInt_t /* prodVers */){
221
222   AliError(Form("Not implemented in this case"));
223 }