]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliSurveyObj.cxx
Classes to import survey data added.
[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 //_____________________________________________________________________________
158 AliCDBGrid::~AliCDBGrid()
159 {
160 // destructor
161         delete gGrid; gGrid=0;
162
163 }
164 */
165
166
167
168 //_____________________________________________________________________________
169 Bool_t AliSurveyObj::OpenFile(TString openString) {
170   TString storage = "alien://alice.cern.ch";
171
172   Printf("TFile::Open string: \n -> \"%s\"\n", openString.Data());
173
174   if (openString.BeginsWith("alien://")) Connect(storage.Data(), "rsilva");
175
176   TFile *file = TFile::Open(openString.Data(), "READ");
177   if ( !file ) {
178     AliError(Form("Error opening file \"%s\"", openString.Data()));
179     return kFALSE;
180   }
181
182   Int_t size = file->GetSize();
183
184   char *buf = new Char_t[size];
185   memset(buf, '\0', size);
186
187   --size;
188
189   file->Seek(0);  
190   if ( file->ReadBuffer(buf, size) ) {
191     AliError("Error reading file contents to buffer!");
192     return kFALSE;
193   }
194
195   // Printf("%d bytes read!\n", size);
196   Printf("%d bytes read!\n", file->GetBytesRead());
197   
198   // Printf("->\n%s\n<- ", buf);
199
200   // Printf("%d AQUI!\n", size);
201
202   ParseBuffer(buf);
203
204   file->Close();
205   delete[] buf;
206   return kTRUE;
207 }
208
209
210
211 //_____________________________________________________________________________
212 Bool_t AliSurveyObj::Get(TString detector, Int_t reportNumber,
213                          Int_t reportVersion) {
214   TString baseFolder = "/alice/cern.ch/user/r/rsilva/";
215
216   TString fullOpenString = "alien://" + baseFolder + detector + '/';
217   fullOpenString += Form("%d_v%d.txt?filetype=raw", reportNumber, reportVersion);
218   // !Still need to check it's a valid path before actually using it
219
220   return OpenFile(fullOpenString);
221 }
222
223
224 //_____________________________________________________________________________
225 Bool_t AliSurveyObj::GetFromLocalFile(const Char_t* filename) {
226   TString fullOpenString = "file://" + TString(filename) + "?filetype=raw";
227
228   return OpenFile(fullOpenString);
229 }
230
231
232 //_____________________________________________________________________________
233 TString &AliSurveyObj::Sanitize(TString str) {
234   str.Remove(TString::kTrailing, '\r');
235   str.Remove(TString::kTrailing, '\n');
236   return str.Remove(TString::kBoth, ' ');
237 }
238
239
240 //_____________________________________________________________________________
241 Bool_t AliSurveyObj::ParseBuffer(const Char_t* buf) {
242   TString buffer = TString(buf);
243   TObjArray *lines = buffer.Tokenize('\n');
244   TObjArray *dataLine = NULL; // Used to Tokenize each point read
245   TObjArray *colLine = NULL; // Used to Tokenize the column names
246
247   const Int_t kFieldCheck = 10;
248   Bool_t check[kFieldCheck];
249   TString tmp_name = "";
250   Float_t tmp_x = 0.0, tmp_y = 0.0, tmp_z = 0.0;
251   Float_t tmp_precX = 0.0, tmp_precY = 0.0, tmp_precZ = 0.0;
252   Char_t tmp_type = '\0';
253   Bool_t tmp_targ = kTRUE;
254   AliSurveyPoint *dp = 0;
255   
256   for (Int_t i = 0; i < kFieldCheck; ++i) check[i] = kFALSE;
257
258   Int_t nrLines = lines->GetEntries();
259   Printf("Lines in file: %d\n", nrLines); 
260
261   TString currLine = "", nextLine = "";
262   for (Int_t i = 0; i < nrLines; ++i) {
263     currLine = ((TObjString *)(lines->At(i)))->GetString().Data();
264     nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
265     currLine = Sanitize(currLine);
266     nextLine = Sanitize(nextLine);
267     Printf("\n%d: \"\"%s\"\"\"\n", i + 1, currLine.Data());
268     
269     if (0 == currLine.Length()) {
270       Printf("Info: Empty line skipped\n\n");
271     } else if (currLine.BeginsWith(">") && !nextLine.BeginsWith(">")) {
272       currLine.Remove(TString::kLeading, '>');
273       currLine.Remove(TString::kTrailing, ':');
274       currLine.Remove(TString::kBoth, ' ');
275       nextLine.Remove(TString::kBoth, ' ');
276       // Printf(" -> Field: \"%s\"\n", currLine.Data());
277       
278       if (currLine.BeginsWith("Title", TString::kIgnoreCase)) {
279         // Report Title
280         fTitle = nextLine;
281         ++i;
282       } else if (currLine.BeginsWith("Date", TString::kIgnoreCase)) {
283         // Report(measurement) Date
284         fDate = nextLine;
285         ++i;
286       } else if (currLine.BeginsWith("Subdetector", TString::kIgnoreCase)) {
287         // Subdetector or structure
288         fDetector = nextLine;
289         ++i;
290       } else if (currLine.BeginsWith("Report URL", TString::kIgnoreCase)) {
291         // Report URL in EDMS
292         if (nextLine.BeginsWith("http://edms.cern.ch/document/", TString::kIgnoreCase) ||
293             nextLine.BeginsWith("https://edms.cern.ch/document/", TString::kIgnoreCase)) {
294           fURL = nextLine;
295           nextLine.Remove(TString::kTrailing, '/');
296           nextLine = nextLine(nextLine.Last('/') + 1, nextLine.Length() - nextLine.Last('/') + 1);
297           // Printf(nextLine);
298           if (!nextLine.IsDigit()) {
299             lines->Delete();
300             return kFALSE;
301           }
302           fReportNr = nextLine.Atoi();
303           ++i;
304         } else { // URL incorrectly formated
305           AliError("Survey text file sintax error! (incorrectly formated Report URL)");
306           return kFALSE;
307         }
308       } else if (currLine.BeginsWith("Version", TString::kIgnoreCase)) {
309         if (!nextLine.IsDigit()) {
310           lines->Delete();
311           return kFALSE;
312         }
313         fVersion = nextLine.Atoi();
314         ++i;
315       } else if (currLine.BeginsWith("General Observations", TString::kIgnoreCase)) {
316         fObs = "";
317         while (('>' != nextLine[0]) && nextLine.Length() > 0) { 
318           fObs += (0 == fObs.Length()) ? nextLine : " / " + nextLine;
319           ++i;
320           nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
321           nextLine = Sanitize(nextLine);
322         }
323         // Printf("->%s<-\n", fObs.Data());
324       } else if (currLine.BeginsWith("Coordinate System", TString::kIgnoreCase)) {
325         fCoordSys = nextLine;
326         ++i;
327       } else if (currLine.BeginsWith("Units", TString::kIgnoreCase)) {
328         fUnits = nextLine;
329         ++i;
330       } else if (currLine.BeginsWith("Nr Columns", TString::kIgnoreCase)) {
331         if (!nextLine.IsDigit()) {
332           lines->Delete();
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           colLine->Delete();
342           lines->Delete();
343           return kFALSE;
344         }
345         ++i;
346       } else if (currLine.BeginsWith("Data", TString::kIgnoreCase)) {
347         while (('>' != nextLine[0]) && nextLine.Length() > 0) {
348           Printf("Data LINE: \"%s\"\n", nextLine.Data());
349
350           dataLine = nextLine.Tokenize(' ');
351           if (dataLine->GetEntries() != fNrColumns) {
352             dataLine->Delete();
353             lines->Delete();
354             return kFALSE;
355           }
356
357           for (Int_t t = 0; t < kFieldCheck; ++t) check[t] = 0;
358
359           for (Int_t j = 0; j < dataLine->GetEntries(); ++j) {
360             TString cn = ((TObjString *)(colLine->At(j)))->GetString();
361             TString value = ((TObjString *)(dataLine->At(j)))->GetString();
362             if (cn.BeginsWith("Point Name", TString::kIgnoreCase)) {
363               tmp_name = value;
364               check[0] = kTRUE;
365             } else if (cn.BeginsWith("X", TString::kIgnoreCase)) {
366               tmp_x = value.Atof();
367               check[1] = kTRUE;
368             } else if (cn.BeginsWith("Y", TString::kIgnoreCase)) {
369               tmp_y = value.Atof();
370               check[2] = kTRUE;
371             } else if (cn.BeginsWith("Z", TString::kIgnoreCase)) {
372               tmp_z = value.Atof();
373               check[3] = kTRUE;
374             } else if (cn.BeginsWith("Precision", TString::kIgnoreCase)) {
375               TString tmpCN = cn(0, cn.First('('));
376               Int_t precLength = TString("Precision").Length();
377               //Printf(" ====== %d ======= %d ====== \n", precLength, tmpCN.Length());
378               //Printf(" ====== %s ======= \n", tmpCN.Data());
379               if (precLength == tmpCN.Length()) {
380                 tmp_precX = tmp_precY = tmp_precZ = value.Atof();
381                 check[6] = kTRUE;
382               } else {
383                 TString axis = cn(precLength, tmpCN.Length() - precLength);
384                 if (axis.Contains('X', TString::kIgnoreCase)) {
385                   tmp_precX = value.Atof();
386                   check[7] = kTRUE;
387                 } else if (axis.Contains('Y', TString::kIgnoreCase)) {
388                   tmp_precY = value.Atof();
389                   check[8] = kTRUE;
390                 } else if (axis.Contains('Z', TString::kIgnoreCase)) {
391                   tmp_precZ = value.Atof();
392                   check[9] = kTRUE;
393                 } else {
394                   AliError("Survey text file sintax error! (Precision column name invalid)");
395                   dataLine->Delete();
396                   lines->Delete();
397                   return kFALSE;
398                 }
399               }
400             } else if (cn.BeginsWith("Point Type", TString::kIgnoreCase)) {
401               tmp_type = value.Data()[0];
402               check[4] = kTRUE;
403             } else if (cn.BeginsWith("Target Used", TString::kIgnoreCase)) {
404               tmp_targ = (value.Data()[0] == 'Y') ? kTRUE : kFALSE;
405               check[5] = kTRUE;
406             }
407
408             Printf("--> %s\n", ((TObjString *)(dataLine->At(j)))->GetString().Data());
409           }
410           Bool_t res = kTRUE, precInd = kTRUE;
411
412           // Target
413           if (kFALSE == check[5]) {
414             tmp_targ = kTRUE;
415             check[5] = kTRUE;
416           }
417           
418           // Individual axis precisions
419           for (Int_t t = 7; t < 10; ++t) precInd &= check[t];
420           if ((kFALSE == check[6]) && (kTRUE == precInd)) check[6] = kTRUE;
421
422           for (Int_t t = 0; t < kFieldCheck - 3; ++t) {
423             Printf("RES(%d): %d\n", t, check[t]);
424             res &= check[t];
425           }
426           if (kTRUE == res) {
427             dp = new AliSurveyPoint(tmp_name, tmp_x, tmp_y, tmp_z, tmp_precX, tmp_precY, tmp_precZ, tmp_type, tmp_targ);
428             dp->PrintPoint();
429             AddPoint(dp);
430           } else {
431             AliError("Parsing error processing data line!");
432             dataLine->Delete();
433             lines->Delete();
434             return kFALSE;
435           }
436           
437           dataLine->Delete();
438           dataLine = NULL;
439           ++i;
440           nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
441           nextLine = Sanitize(nextLine);
442         }
443       }
444     } else { // 2 lines in a row start with ">"
445       AliError("Survey text file sintax error! (2 consecutive lines start with \'>\')");
446       lines->Delete();
447       return kFALSE;
448     }
449   }
450   lines->Delete();
451   fIsValid = kTRUE;
452   return kTRUE;
453 }
454