]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/pendolino/AliHLTPendolinoListReader.cxx
replacing calls to AliCDBStorage::GetLatestVersion by handling of exception introduce...
[u/mrichter/AliRoot.git] / HLT / pendolino / AliHLTPendolinoListReader.cxx
1 // $Id$
2
3 /************************************************************************
4 **
5 **
6 ** This file is property of and copyright by the Department of Physics
7 ** Institute for Physic and Technology, University of Bergen,
8 ** Bergen, Norway, 2006
9 ** This file has been written by Sebastian Bablok,
10 ** sebastian.bablok@ift.uib.no
11 **
12 ** Important: This file is provided without any warranty, including
13 ** fitness for any particular purpose.
14 **
15 **
16 *************************************************************************/
17
18 //  @file   AliHLTPendolinoListReader.cxx
19 //  @author Sepastian Bablok
20 //  @date   
21 //  @brief  Helper class for pendolino list handling
22 //  @note   maintained by matthias.richter@cern.ch
23
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27 #include <stdlib.h>
28
29 #include <TObjString.h>
30 #include <TList.h>
31
32 #include "AliHLTPendolinoListReader.h"
33
34 ClassImp(AliHLTPendolinoListReader)
35
36 using namespace std;
37 //using namespace alice::hlt::pendolino;
38
39 //typedef pair<string, TMap> Key_Val_Pair; 
40 //typedef TMap<TString, TMap> AliasMap;
41
42
43 int AliHLTPendolinoListReader::kMAX_LINE_LENGTH = 256;
44
45
46 AliHLTPendolinoListReader::AliHLTPendolinoListReader()
47   : TObject()
48   , fValid(false)
49   , fCalibObjList()
50 {
51   // see header file for class documentation
52   // or
53   // refer to README to build package
54   // or
55   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
56 }
57
58
59 AliHLTPendolinoListReader::AliHLTPendolinoListReader(const char* filename) 
60   : TObject()
61   , fValid(false)
62   , fCalibObjList()
63 {
64   // see header file for class documentation
65         if (ReadListFromFile(filename)) {
66                 fValid = true;
67         } else {
68                 // log that filename does ot matches a file
69         }
70 }
71
72
73 AliHLTPendolinoListReader::~AliHLTPendolinoListReader() 
74 {
75   // see header file for class documentation
76         if (fValid) {
77                 fCalibObjList.Delete();
78         }
79 }
80
81
82 bool AliHLTPendolinoListReader::ReadListFromFile(const std::string filename)
83 {
84   // see header file for class documentation
85         return ReadListFromFile(filename.c_str());
86 }
87
88
89 bool AliHLTPendolinoListReader::ReadListFromFile(const char* filename)
90 {
91   // see header file for class documentation
92         ifstream pfile;
93         unsigned int count = 0;
94
95         // open property file
96         if (!filename || filename[0]==0) {
97                 cout << "Filename for calibration object list is empty" << endl;
98 //              ProxyLogger::getLogger()->createLogMessage(MSG_ERROR, LOG_SOURCE_PROXY,
99 //                              "PropertyReader received empty filename to read properties.");
100                 return false;
101         }
102
103         pfile.open(filename, ios_base::in);
104         if (!pfile) {
105                 cout << "Error while opening list file or file does not exist."
106                                 << endl;
107 //              ProxyLogger::getLogger()->createLogMessage(MSG_ERROR, LOG_SOURCE_PROXY,
108 //                              "PropertyReader is unable to open property file '" +
109 //                              ProxyLogger::catos(filename) + "'.");
110                 return false;
111         }
112 //      ProxyLogger::getLogger()->createLogMessage(MSG_INFO, LOG_SOURCE_PROXY,
113 //                      "PropertyReader uses property file '" + ProxyLogger::catos(filename)
114 //                      + "'.");
115
116         while ((!pfile.eof()) && (!pfile.bad())) {
117                 char line[kMAX_LINE_LENGTH];
118                 char* ptrDelimiter;
119
120                 pfile.getline(line, kMAX_LINE_LENGTH);
121
122                 // skip empty lines
123                 if (strlen(line) == 0) {
124                         continue;
125                 }
126                 // skip comment lines (beginning with '#') and lines with blank chars
127                 // at beginnig
128                 if ((line[0] == '#') || (line[0] == ' ')) {
129                         continue;
130                 }
131
132                 // cut off comments at end
133                 ptrDelimiter = strstr(line, "#");
134                 if (ptrDelimiter != 0) {
135                         *ptrDelimiter = '\0';
136                 }
137                 // cut off end of line
138                 ptrDelimiter = strstr(line, " ");
139                 if (ptrDelimiter != 0) {
140                         *ptrDelimiter = '\0';
141                 }
142                 
143         // seperate alias names by detectors
144                 ptrDelimiter = strstr(line, "=");
145                 if (ptrDelimiter != 0) {
146                         *ptrDelimiter = '\0';   // replace '=' with '\0'
147                         ptrDelimiter++;                 // points to values (Aliasname)
148                         if ((ptrDelimiter != 0) && (ptrDelimiter[0] != ' ') && (line[0] != ' ')) {
149 //                              cout <<"getting detector"<< endl;
150 //                              TString detector = new TString(line);
151 //                              string value(ptrDelimiter);
152                                 TList* pos = (TList*) fCalibObjList.GetValue(line);
153                                 if (pos == 0) {
154                                         // new detector
155 //                                      cout<<"inserting"<<endl;
156                                         TList *aliasList = new TList();
157                                         aliasList->Add(new TObjString(ptrDelimiter));
158 //                                      cout<<"including"<<endl;
159                                         fCalibObjList.Add(new TObjString(line), (TObject*) aliasList); 
160 //                                                      new TMap(new TObjString(ptrDelimiter), 0));
161 //                                      cout << "inserted: " << line << " with " << ptrDelimiter << endl;
162                                 } else {
163                                         if (pos->FindObject(ptrDelimiter) == 0) {
164 //                                              cout<<"adding"<<endl;
165 //                                              mProperties.insert(String_Pair(name, value));
166                                                 pos->Add(new TObjString(ptrDelimiter));
167                                                 // use value [Alias name] as key for map
168 //                                              cout << "added " << ptrDelimiter << " for " << line << endl;
169                                         } else {
170                                                 cout << "   ~~~ ommiting duplicated Alias names (" <<
171                                                                 ptrDelimiter << ")." << endl;
172                                                 continue;
173                                         }
174                                 }
175                                 ++count;
176                         } else {
177 //                              ProxyLogger::getLogger()->createLogMessage(MSG_WARNING,
178 //                                              LOG_SOURCE_PROXY,
179 //                                              "PropertyReader encountered empty property for '" +
180 //                                              ProxyLogger::catos(line) + "'.");
181                                 cout << "Missing property value for " << line << endl;
182                         }
183                 }
184
185 /*
186         // file calibration object name in local vector
187         string name(line);
188         fCalibObjList.push_back(name);
189         ++count;
190 */
191         }
192 //      ProxyLogger::getLogger()->createLogMessage(MSG_DEBUG, LOG_SOURCE_PROXY,
193 //                      "PropertyReader has read " + ProxyLogger::itos(count) +
194 //                      " properties from file '" +     ProxyLogger::catos(filename) + "'.");
195
196         // maybe check also for .bad() for returning a false
197         pfile.close();
198         fValid = true;
199         return true;
200 }
201
202 void AliHLTPendolinoListReader::Print() const {
203         TIter iter(&fCalibObjList);
204         TObjString* detect;
205
206         cout << "List Print:" << endl;
207         while ((detect = (TObjString*) (iter.Next()))) {
208                 cout << " " << detect->String() << ":" << endl;
209                 TMap* value = (TMap*) fCalibObjList.GetValue(detect->String().Data());
210                 TIter valIt(value);
211                 TObjString* alias;
212                 while ((alias = (TObjString*) (valIt.Next()))) {
213                         cout << "   " << alias->String() << endl;
214                 }
215                 cout << endl;
216         }
217         cout << "---" << endl;
218 }
219
220 int AliHLTPendolinoListReader::RetrieveLastRunNumber(char* path) {
221         string strPath = path;
222         return AliHLTPendolinoListReader::RetrieveLastRunNumber(strPath);
223 }
224
225 int AliHLTPendolinoListReader::RetrieveLastRunNumber(string path) {
226         int runNumber = 0;
227         ifstream pfile;
228         string filename;
229         char line[25];
230
231         filename = path + "/lastRunNumber";
232         cout << "   --- PATH to lastRunNumber is " << filename << endl;
233         
234         pfile.open(filename.c_str(), ios_base::in);
235         if (!pfile) {
236                 cout << "Error while opening last RunNumber file or file does not exist."
237                                 << endl;
238                 return runNumber;
239         }
240         if ((!pfile.eof()) && (!pfile.bad())) {
241                 pfile.getline(line, 25);
242                 runNumber = atoi(line);
243         }
244
245         return runNumber;
246 }
247
248