1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /////////////////////////////////////////////////////////////////////
18 // class AliSurveyObj //
19 // Retrieve and Convert survey data into ROOT Objects //
21 /////////////////////////////////////////////////////////////////////
23 #include "AliSurveyObj.h"
27 #include "Riostream.h"
28 #include "TObjArray.h"
31 #include "TObjString.h"
37 //#include <time.h> //for sleep(3); // not used anymore!?
39 ClassImp(AliSurveyObj)
41 //_____________________________________________________________________________
42 AliSurveyObj::AliSurveyObj():
47 fURL("http://edms.cern.ch/"),
56 fDataPoints(new TObjArray(1))
59 fDataPoints->SetOwner(kTRUE);
63 //_____________________________________________________________________________
64 AliSurveyObj::~AliSurveyObj() {
70 //_____________________________________________________________________________
71 AliSurveyObj::AliSurveyObj(const AliSurveyObj& surveyObj):
73 fTitle(surveyObj.fTitle),
74 fDate(surveyObj.fDate),
75 fDetector(surveyObj.fDetector),
77 fReportNr(surveyObj.fReportNr),
78 fVersion(surveyObj.fVersion),
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))
88 TObject *curr = surveyObj.fDataPoints->First();
90 fDataPoints->Add(curr);
91 curr = surveyObj.fDataPoints->After(curr);
95 //_____________________________________________________________________________
96 AliSurveyObj& AliSurveyObj::operator=(const AliSurveyObj& surveyObj)
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;
113 TObject *curr = surveyObj.fDataPoints->First();
115 fDataPoints->Add(curr);
116 curr = surveyObj.fDataPoints->After(curr);
124 //_____________________________________________________________________________
125 void AliSurveyObj::AddPoint(AliSurveyPoint* point) {
126 fDataPoints->Add(point);
131 //_____________________________________________________________________________
132 Bool_t AliSurveyObj::Connect(const char *gridUrl, const char *user) {
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...");
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()));
145 TGrid::Connect(gridUrl,user);
149 AliError("Connection failed!");
156 //_____________________________________________________________________________
157 Bool_t AliSurveyObj::OpenFile(TString openString) {
158 TString storage = "alien://alice.cern.ch";
160 Printf("TFile::Open string: \n -> \"%s\"\n", openString.Data());
162 if (openString.BeginsWith("alien://")) Connect(storage.Data(), "rsilva");
164 TFile *file = TFile::Open(openString.Data(), "READ");
166 AliError(Form("Error opening file \"%s\"", openString.Data()));
170 Int_t size = file->GetSize();
172 char *buf = new Char_t[size];
173 memset(buf, '\0', size);
178 if ( file->ReadBuffer(buf, size) ) {
179 AliError("Error reading file contents to buffer!");
183 // Printf("%d bytes read!\n", size);
184 Printf("%d bytes read!\n", file->GetBytesRead());
186 // Printf("->\n%s\n<- ", buf);
188 // Printf("%d AQUI!\n", size);
199 //_____________________________________________________________________________
200 Bool_t AliSurveyObj::Fill(TString detector, Int_t reportNumber,
201 Int_t reportVersion) {
202 TString baseFolder = "/alice/cern.ch/user/r/rsilva/";
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
208 return OpenFile(fullOpenString);
212 //_____________________________________________________________________________
213 Bool_t AliSurveyObj::FillFromLocalFile(const Char_t* filename) {
214 TString fullOpenString = "file://" + TString(filename) + "?filetype=raw";
216 return OpenFile(fullOpenString);
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");
229 return str.Remove(TString::kBoth, ' ');
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
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;
249 for (Int_t i = 0; i < kFieldCheck; ++i) check[i] = kFALSE;
251 Int_t nrLines = lines->GetEntries();
252 Printf("Lines in file: %d\n", nrLines);
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());
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());
271 if (currLine.BeginsWith("Title", TString::kIgnoreCase)) {
275 } else if (currLine.BeginsWith("Date", TString::kIgnoreCase)) {
276 // Report(measurement) Date
279 } else if (currLine.BeginsWith("Subdetector", TString::kIgnoreCase)) {
280 // Subdetector or structure
281 fDetector = nextLine;
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)) {
288 nextLine.Remove(TString::kTrailing, '/');
289 nextLine = nextLine(nextLine.Last('/') + 1, nextLine.Length() - nextLine.Last('/') + 1);
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)");
299 fReportNr = nextLine.Atoi();
300 //Printf(" $$ %d $$\n", fReportNr);
302 } else { // URL incorrectly formated
303 AliError("Survey text file sintax error! (incorrectly formated Report URL)");
306 } else if (currLine.BeginsWith("Version", TString::kIgnoreCase)) {
307 if (!nextLine.IsDigit()) {
309 AliError("Survey text file sintax error! (incorrectly formated Report Version)");
312 fVersion = nextLine.Atoi();
314 } else if (currLine.BeginsWith("General Observations", TString::kIgnoreCase)) {
316 while (('>' != nextLine[0]) && nextLine.Length() > 0) {
317 fObs += (0 == fObs.Length()) ? nextLine : " / " + nextLine;
319 nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
320 nextLine = Sanitize(nextLine);
322 // Printf("->%s<-\n", fObs.Data());
323 } else if (currLine.BeginsWith("Coordinate System", TString::kIgnoreCase)) {
324 fCoordSys = nextLine;
326 } else if (currLine.BeginsWith("Units", TString::kIgnoreCase)) {
329 } else if (currLine.BeginsWith("Nr Columns", TString::kIgnoreCase)) {
330 if (!nextLine.IsDigit()) {
332 AliError("Survey text file sintax error! (incorrectly formated Number of Columns)");
335 fNrColumns = nextLine.Atoi();
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)");
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]);
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');
357 AliError("Survey text file syntax error! Error processing data line!");
362 if (dataLine->GetEntries() != fNrColumns) {
363 AliError("Survey text file sintax error! (Number of entries in line is different from declared Number of Columns)");
369 for (Int_t t = 0; t < kFieldCheck; ++t) check[t] = 0;
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)) {
377 } else if (cn.BeginsWith("X", TString::kIgnoreCase)) {
378 tmp_x = value.Atof();
380 } else if (cn.BeginsWith("Y", TString::kIgnoreCase)) {
381 tmp_y = value.Atof();
383 } else if (cn.BeginsWith("Z", TString::kIgnoreCase)) {
384 tmp_z = value.Atof();
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();
395 TString axis = cn(precLength, tmpCN.Length() - precLength);
396 if (axis.Contains('X', TString::kIgnoreCase)) {
397 tmp_precX = value.Atof();
399 } else if (axis.Contains('Y', TString::kIgnoreCase)) {
400 tmp_precY = value.Atof();
402 } else if (axis.Contains('Z', TString::kIgnoreCase)) {
403 tmp_precZ = value.Atof();
406 AliError("Survey text file sintax error! (Precision column name invalid)");
412 } else if (cn.BeginsWith("Point Type", TString::kIgnoreCase)) {
413 tmp_type = value.Data()[0];
415 } else if (cn.BeginsWith("Target Used", TString::kIgnoreCase)) {
416 tmp_targ = (value.Data()[0] == 'Y') ? kTRUE : kFALSE;
420 //Printf("--> %s\n", ((TObjString *)(dataLine->At(j)))->GetString().Data());
422 Bool_t res = kTRUE, precInd = kTRUE;
425 if (kFALSE == check[5]) {
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;
434 for (Int_t t = 0; t < kFieldCheck - 3; ++t) {
435 //Printf("RES(%d): %d\n", t, check[t]);
439 dp = new AliSurveyPoint(tmp_name, tmp_x, tmp_y, tmp_z, tmp_precX, tmp_precY, tmp_precZ, tmp_type, tmp_targ);
443 AliError("Parsing error processing data line!");
452 nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
453 nextLine = Sanitize(nextLine);
457 AliError("Survey text file sintax error!");