]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliSurveyObj.cxx
Missing getters added and a a couple of bugs when parsing the (raw)survey file corrected
[u/mrichter/AliRoot.git] / STEER / AliSurveyObj.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 /////////////////////////////////////////////////////////////////////
17 //                                                                 //
18 //  class AliSurveyObj                                             //
19 //  Retrieve and Convert survey data into ROOT Objects             //
20 //                                                                 //
21 /////////////////////////////////////////////////////////////////////
22
23 #include "AliSurveyObj.h"
24
25 //ROOT includes
26 #include "TROOT.h"
27 #include "Riostream.h"
28 #include "TObjArray.h"
29 #include "TGrid.h"
30 #include "TFile.h"
31 #include "TObjString.h"
32
33 //AliROOT includes
34 #include "AliLog.h"
35
36 //System includes
37 //#include <time.h> //for sleep(3); // not used anymore!?
38
39 ClassImp(AliSurveyObj)
40   
41 //_____________________________________________________________________________
42 AliSurveyObj::AliSurveyObj():
43   TObject(),
44   fTitle(""),
45   fDate(""),
46   fDetector(""),
47   fURL("http://edms.cern.ch/"),
48   fReportNr(-1),
49   fVersion(-1),
50   fObs(""),
51   fCoordSys(""),
52   fUnits(""),
53   fNrColumns(-1),
54   fColNames(""),
55   fIsValid(kFALSE),
56   fDataPoints(new TObjArray(1))
57 {
58   // constructor
59   fDataPoints->SetOwner(kTRUE);
60 }
61
62
63 //_____________________________________________________________________________
64 AliSurveyObj::~AliSurveyObj() {
65   //destructor
66   
67 }
68
69
70 //_____________________________________________________________________________
71 AliSurveyObj::AliSurveyObj(const AliSurveyObj& surveyObj):
72   TObject(),
73   fTitle(surveyObj.fTitle),
74   fDate(surveyObj.fDate),
75   fDetector(surveyObj.fDetector),
76   fURL(surveyObj.fURL),
77   fReportNr(surveyObj.fReportNr),
78   fVersion(surveyObj.fVersion),
79   fObs(surveyObj.fObs),
80   fCoordSys(surveyObj.fCoordSys),
81   fUnits(surveyObj.fUnits),
82   fNrColumns(surveyObj.fNrColumns),
83   fColNames(surveyObj.fColNames),
84   fIsValid(surveyObj.fIsValid),
85   fDataPoints(new TObjArray(1))
86 {
87   // copy constructor
88   TObject *curr = surveyObj.fDataPoints->First();
89   while (curr != 0) {
90     fDataPoints->Add(curr);
91     curr = surveyObj.fDataPoints->After(curr);
92   }
93 }
94
95 //_____________________________________________________________________________
96 AliSurveyObj& AliSurveyObj::operator=(const AliSurveyObj& surveyObj)
97 {
98   // assignment operator
99   if (this != &surveyObj) {
100     fTitle = surveyObj.fTitle;
101     fDate = surveyObj.fDate;
102     fDetector = surveyObj.fDetector;
103     fURL = surveyObj.fURL;
104     fReportNr = surveyObj.fReportNr;
105     fVersion = surveyObj.fVersion;
106     fObs = surveyObj.fObs;
107     fCoordSys = surveyObj.fCoordSys;
108     fUnits = surveyObj.fUnits;
109     fNrColumns = surveyObj.fNrColumns;
110     fColNames = surveyObj.fColNames;
111     fIsValid = surveyObj.fIsValid;
112     
113     TObject *curr = surveyObj.fDataPoints->First();
114     while (curr != 0) {
115       fDataPoints->Add(curr);
116       curr = surveyObj.fDataPoints->After(curr);
117     }
118   }
119   
120   return *this;
121 }
122
123
124 //_____________________________________________________________________________
125 void AliSurveyObj::AddPoint(AliSurveyPoint* point) {
126   fDataPoints->Add(point);
127   return;
128 }
129
130
131 //_____________________________________________________________________________
132 Bool_t AliSurveyObj::Connect(const char *gridUrl, const char *user) {
133
134    // if the same Grid is alreay active, skip connection
135         if ( !gGrid || gridUrl != gGrid->GridUrl() ||
136              (( user != "" ) && ( user != gGrid->GetUser() )) ) {
137         // connection to the Grid
138            AliInfo("\nConnecting to the Grid...");
139            if(gGrid){
140                    AliInfo(Form("gGrid = %x; GridUrl = %s; gGrid->GridUrl() = %s", 
141                                  gGrid, gridUrl, gGrid->GridUrl()));
142               AliInfo(Form("User = %s; gGrid->GetUser() = %s",
143                             user, gGrid->GetUser()));
144            }
145            TGrid::Connect(gridUrl,user);
146         }
147         
148         if(!gGrid) {
149                 AliError("Connection failed!");
150                 return kFALSE;
151         }
152    return kTRUE;
153 }
154
155
156 //_____________________________________________________________________________
157 Bool_t AliSurveyObj::OpenFile(TString openString) {
158   TString storage = "alien://alice.cern.ch";
159
160   Printf("TFile::Open string: \n -> \"%s\"\n", openString.Data());
161
162   if (openString.BeginsWith("alien://")) Connect(storage.Data(), "rsilva");
163
164   TFile *file = TFile::Open(openString.Data(), "READ");
165   if ( !file ) {
166     AliError(Form("Error opening file \"%s\"", openString.Data()));
167     return kFALSE;
168   }
169
170   Int_t size = file->GetSize();
171
172   char *buf = new Char_t[size];
173   memset(buf, '\0', size);
174
175   //--size;
176
177   file->Seek(0);  
178   if ( file->ReadBuffer(buf, size) ) {
179     AliError("Error reading file contents to buffer!");
180     return kFALSE;
181   }
182
183   // Printf("%d bytes read!\n", size);
184   Printf("%d bytes read!\n", file->GetBytesRead());
185   
186   // Printf("->\n%s\n<- ", buf);
187
188   // Printf("%d AQUI!\n", size);
189
190   ParseBuffer(buf);
191
192   file->Close();
193   delete[] buf;
194   return kTRUE;
195 }
196
197
198
199 //_____________________________________________________________________________
200 Bool_t AliSurveyObj::Fill(TString detector, Int_t reportNumber,
201                          Int_t reportVersion) {
202   TString baseFolder = "/alice/cern.ch/user/r/rsilva/";
203
204   TString fullOpenString = "alien://" + baseFolder + detector + '/';
205   fullOpenString += Form("%d_v%d.txt?filetype=raw", reportNumber, reportVersion);
206   // !Still need to check it's a valid path before actually using it
207
208   return OpenFile(fullOpenString);
209 }
210
211
212 //_____________________________________________________________________________
213 Bool_t AliSurveyObj::FillFromLocalFile(const Char_t* filename) {
214   TString fullOpenString = "file://" + TString(filename) + "?filetype=raw";
215
216   return OpenFile(fullOpenString);
217 }
218
219
220 //_____________________________________________________________________________
221 TString &AliSurveyObj::Sanitize(TString str) {
222   str.Remove(TString::kTrailing, '\r');
223   str.Remove(TString::kTrailing, '\n');
224   str.Remove(TString::kTrailing, '\r');
225   if (!str.IsAscii()) {
226     AliWarning("Warning: Non-ASCII characters!\n");
227     str = "";
228   }
229   return str.Remove(TString::kBoth, ' ');
230 }
231
232
233 //_____________________________________________________________________________
234 Bool_t AliSurveyObj::ParseBuffer(const Char_t* buf) {
235   TString buffer = TString(buf);
236   TObjArray *lines = buffer.Tokenize('\n');
237   TObjArray *dataLine = NULL; // Used to Tokenize each point read
238   TObjArray *colLine = NULL; // Used to Tokenize the column names
239
240   const Int_t kFieldCheck = 10;
241   Bool_t check[kFieldCheck];
242   TString tmp_name = "";
243   Float_t tmp_x = 0.0, tmp_y = 0.0, tmp_z = 0.0;
244   Float_t tmp_precX = 0.0, tmp_precY = 0.0, tmp_precZ = 0.0;
245   Char_t tmp_type = '\0';
246   Bool_t tmp_targ = kTRUE;
247   AliSurveyPoint *dp = 0;
248   
249   for (Int_t i = 0; i < kFieldCheck; ++i) check[i] = kFALSE;
250
251   Int_t nrLines = lines->GetEntries();
252   Printf("Lines in file: %d\n", nrLines); 
253
254   TString currLine = "", nextLine = "";
255   for (Int_t i = 0; i < nrLines; ++i) {
256     currLine = ((TObjString *)(lines->At(i)))->GetString().Data();
257     nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
258     currLine = Sanitize(currLine);
259     nextLine = Sanitize(nextLine);
260     //Printf("\n%d: \"\"%s\"\"\"\n", i + 1, currLine.Data());
261     
262     if (0 == currLine.Length()) {
263       Printf("Info: Empty line skipped\n\n");
264     } else if (currLine.BeginsWith(">") && !nextLine.BeginsWith(">")) {
265       currLine.Remove(TString::kLeading, '>');
266       currLine.Remove(TString::kTrailing, ':');
267       currLine.Remove(TString::kBoth, ' ');
268       nextLine.Remove(TString::kBoth, ' ');
269       // Printf(" -> Field: \"%s\"\n", currLine.Data());
270       
271       if (currLine.BeginsWith("Title", TString::kIgnoreCase)) {
272         // Report Title
273         fTitle = nextLine;
274         ++i;
275       } else if (currLine.BeginsWith("Date", TString::kIgnoreCase)) {
276         // Report(measurement) Date
277         fDate = nextLine;
278         ++i;
279       } else if (currLine.BeginsWith("Subdetector", TString::kIgnoreCase)) {
280         // Subdetector or structure
281         fDetector = nextLine;
282         ++i;
283       } else if (currLine.BeginsWith("Report URL", TString::kIgnoreCase)) {
284         // Report URL in EDMS
285         if (nextLine.BeginsWith("http://edms.cern.ch/document/", TString::kIgnoreCase) ||
286             nextLine.BeginsWith("https://edms.cern.ch/document/", TString::kIgnoreCase)) {
287           fURL = nextLine;
288           nextLine.Remove(TString::kTrailing, '/');
289           nextLine = nextLine(nextLine.Last('/') + 1, nextLine.Length() - nextLine.Last('/') + 1);
290           
291           Printf("## %s ##\n", nextLine.Data());
292           Int_t sscanf_tmp = 0;
293           //if (!nextLine.IsDigit()) {
294           if (1 != sscanf(nextLine.Data(), "%d", &sscanf_tmp)) {
295             AliError("Survey text file sintax error! (incorrectly formated Report URL)");
296             lines->Delete();
297             return kFALSE;
298           }
299           fReportNr = nextLine.Atoi();
300           //Printf(" $$ %d $$\n", fReportNr);
301           ++i;
302         } else { // URL incorrectly formated
303           AliError("Survey text file sintax error! (incorrectly formated Report URL)");
304           return kFALSE;
305         }
306       } else if (currLine.BeginsWith("Version", TString::kIgnoreCase)) {
307         if (!nextLine.IsDigit()) {
308           lines->Delete();
309           AliError("Survey text file sintax error! (incorrectly formated Report Version)");
310           return kFALSE;
311         }
312         fVersion = nextLine.Atoi();
313         ++i;
314       } else if (currLine.BeginsWith("General Observations", TString::kIgnoreCase)) {
315         fObs = "";
316         while (('>' != nextLine[0]) && nextLine.Length() > 0) { 
317           fObs += (0 == fObs.Length()) ? nextLine : " / " + nextLine;
318           ++i;
319           nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
320           nextLine = Sanitize(nextLine);
321         }
322         // Printf("->%s<-\n", fObs.Data());
323       } else if (currLine.BeginsWith("Coordinate System", TString::kIgnoreCase)) {
324         fCoordSys = nextLine;
325         ++i;
326       } else if (currLine.BeginsWith("Units", TString::kIgnoreCase)) {
327         fUnits = nextLine;
328         ++i;
329       } else if (currLine.BeginsWith("Nr Columns", TString::kIgnoreCase)) {
330         if (!nextLine.IsDigit()) {
331           lines->Delete();
332           AliError("Survey text file sintax error! (incorrectly formated Number of Columns)");
333           return kFALSE;
334         }
335         fNrColumns = nextLine.Atoi();
336         ++i;
337       } else if (currLine.BeginsWith("Column Names", TString::kIgnoreCase)) {
338         fColNames = nextLine;
339         colLine = nextLine.Tokenize(',');
340         if (colLine->GetEntries() != fNrColumns) {
341           AliError("Survey text file sintax error! (Declared number of Columns doesn't match number of column names)");
342           colLine->Delete();
343           lines->Delete();
344           return kFALSE;
345         }
346         ++i;
347       } else if (currLine.BeginsWith("Data", TString::kIgnoreCase)) {
348         while ((nextLine.Length() > 0) && ('>' != nextLine[0])) {
349           Printf("Data LINE: \"%s\": %d\n", nextLine.Data(), nextLine.Length());
350           if (2 == nextLine.Length()) for(Int_t g = 0; g < 2; ++g) Printf("'%c'", nextLine[g]);
351
352           if (fNrColumns == nextLine.CountChar(' ') + 1) dataLine = nextLine.Tokenize(' ');
353           else if (fNrColumns == nextLine.CountChar(',') + 1) dataLine = nextLine.Tokenize(',');
354           else if (fNrColumns == nextLine.CountChar('\t') + 1) dataLine = nextLine.Tokenize('\t');
355           else {
356             //Error
357             AliError("Survey text file syntax error! Error processing data line!");
358             lines->Delete();
359             return kFALSE;
360           }
361
362           if (dataLine->GetEntries() != fNrColumns) {
363             AliError("Survey text file sintax error! (Number of entries in line is different from declared Number of Columns)");
364             dataLine->Delete();
365             lines->Delete();
366             return kFALSE;
367           }
368
369           for (Int_t t = 0; t < kFieldCheck; ++t) check[t] = 0;
370
371           for (Int_t j = 0; j < dataLine->GetEntries(); ++j) {
372             TString cn = ((TObjString *)(colLine->At(j)))->GetString();
373             TString value = ((TObjString *)(dataLine->At(j)))->GetString();
374             if (cn.BeginsWith("Point Name", TString::kIgnoreCase)) {
375               tmp_name = value;
376               check[0] = kTRUE;
377             } else if (cn.BeginsWith("X", TString::kIgnoreCase)) {
378               tmp_x = value.Atof();
379               check[1] = kTRUE;
380             } else if (cn.BeginsWith("Y", TString::kIgnoreCase)) {
381               tmp_y = value.Atof();
382               check[2] = kTRUE;
383             } else if (cn.BeginsWith("Z", TString::kIgnoreCase)) {
384               tmp_z = value.Atof();
385               check[3] = kTRUE;
386             } else if (cn.BeginsWith("Precision", TString::kIgnoreCase)) {
387               TString tmpCN = cn(0, cn.First('('));
388               Int_t precLength = TString("Precision").Length();
389               //Printf(" ====== %d ======= %d ====== \n", precLength, tmpCN.Length());
390               //Printf(" ====== %s ======= \n", tmpCN.Data());
391               if (precLength == tmpCN.Length()) {
392                 tmp_precX = tmp_precY = tmp_precZ = value.Atof();
393                 check[6] = kTRUE;
394               } else {
395                 TString axis = cn(precLength, tmpCN.Length() - precLength);
396                 if (axis.Contains('X', TString::kIgnoreCase)) {
397                   tmp_precX = value.Atof();
398                   check[7] = kTRUE;
399                 } else if (axis.Contains('Y', TString::kIgnoreCase)) {
400                   tmp_precY = value.Atof();
401                   check[8] = kTRUE;
402                 } else if (axis.Contains('Z', TString::kIgnoreCase)) {
403                   tmp_precZ = value.Atof();
404                   check[9] = kTRUE;
405                 } else {
406                   AliError("Survey text file sintax error! (Precision column name invalid)");
407                   dataLine->Delete();
408                   lines->Delete();
409                   return kFALSE;
410                 }
411               }
412             } else if (cn.BeginsWith("Point Type", TString::kIgnoreCase)) {
413               tmp_type = value.Data()[0];
414               check[4] = kTRUE;
415             } else if (cn.BeginsWith("Target Used", TString::kIgnoreCase)) {
416               tmp_targ = (value.Data()[0] == 'Y') ? kTRUE : kFALSE;
417               check[5] = kTRUE;
418             }
419
420             //Printf("--> %s\n", ((TObjString *)(dataLine->At(j)))->GetString().Data());
421           }
422           Bool_t res = kTRUE, precInd = kTRUE;
423
424           // Target
425           if (kFALSE == check[5]) {
426             tmp_targ = kTRUE;
427             check[5] = kTRUE;
428           }
429           
430           // Individual axis precisions
431           for (Int_t t = 7; t < 10; ++t) precInd &= check[t];
432           if ((kFALSE == check[6]) && (kTRUE == precInd)) check[6] = kTRUE;
433
434           for (Int_t t = 0; t < kFieldCheck - 3; ++t) {
435             //Printf("RES(%d): %d\n", t, check[t]);
436             res &= check[t];
437           }
438           if (kTRUE == res) {
439             dp = new AliSurveyPoint(tmp_name, tmp_x, tmp_y, tmp_z, tmp_precX, tmp_precY, tmp_precZ, tmp_type, tmp_targ);
440             dp->PrintPoint();
441             AddPoint(dp);
442           } else {
443             AliError("Parsing error processing data line!");
444             dataLine->Delete();
445             lines->Delete();
446             return kFALSE;
447           }
448           
449           dataLine->Delete();
450           dataLine = NULL;
451           ++i;
452           nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
453           nextLine = Sanitize(nextLine);
454         }
455       }
456     } else {
457       AliError("Survey text file sintax error!");
458       lines->Delete();
459       return kFALSE;
460     }
461   }
462   lines->Delete();
463   fIsValid = kTRUE;
464   return kTRUE;
465 }
466