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!");
157 //_____________________________________________________________________________
158 AliCDBGrid::~AliCDBGrid()
161 delete gGrid; gGrid=0;
168 //_____________________________________________________________________________
169 Bool_t AliSurveyObj::OpenFile(TString openString) {
170 TString storage = "alien://alice.cern.ch";
172 Printf("TFile::Open string: \n -> \"%s\"\n", openString.Data());
174 if (openString.BeginsWith("alien://")) Connect(storage.Data(), "rsilva");
176 TFile *file = TFile::Open(openString.Data(), "READ");
178 AliError(Form("Error opening file \"%s\"", openString.Data()));
182 Int_t size = file->GetSize();
184 char *buf = new Char_t[size];
185 memset(buf, '\0', size);
190 if ( file->ReadBuffer(buf, size) ) {
191 AliError("Error reading file contents to buffer!");
195 // Printf("%d bytes read!\n", size);
196 Printf("%d bytes read!\n", file->GetBytesRead());
198 // Printf("->\n%s\n<- ", buf);
200 // Printf("%d AQUI!\n", size);
211 //_____________________________________________________________________________
212 Bool_t AliSurveyObj::Get(TString detector, Int_t reportNumber,
213 Int_t reportVersion) {
214 TString baseFolder = "/alice/cern.ch/user/r/rsilva/";
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
220 return OpenFile(fullOpenString);
224 //_____________________________________________________________________________
225 Bool_t AliSurveyObj::GetFromLocalFile(const Char_t* filename) {
226 TString fullOpenString = "file://" + TString(filename) + "?filetype=raw";
228 return OpenFile(fullOpenString);
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, ' ');
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
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;
256 for (Int_t i = 0; i < kFieldCheck; ++i) check[i] = kFALSE;
258 Int_t nrLines = lines->GetEntries();
259 Printf("Lines in file: %d\n", nrLines);
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());
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());
278 if (currLine.BeginsWith("Title", TString::kIgnoreCase)) {
282 } else if (currLine.BeginsWith("Date", TString::kIgnoreCase)) {
283 // Report(measurement) Date
286 } else if (currLine.BeginsWith("Subdetector", TString::kIgnoreCase)) {
287 // Subdetector or structure
288 fDetector = nextLine;
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)) {
295 nextLine.Remove(TString::kTrailing, '/');
296 nextLine = nextLine(nextLine.Last('/') + 1, nextLine.Length() - nextLine.Last('/') + 1);
298 if (!nextLine.IsDigit()) {
302 fReportNr = nextLine.Atoi();
304 } else { // URL incorrectly formated
305 AliError("Survey text file sintax error! (incorrectly formated Report URL)");
308 } else if (currLine.BeginsWith("Version", TString::kIgnoreCase)) {
309 if (!nextLine.IsDigit()) {
313 fVersion = nextLine.Atoi();
315 } else if (currLine.BeginsWith("General Observations", TString::kIgnoreCase)) {
317 while (('>' != nextLine[0]) && nextLine.Length() > 0) {
318 fObs += (0 == fObs.Length()) ? nextLine : " / " + nextLine;
320 nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
321 nextLine = Sanitize(nextLine);
323 // Printf("->%s<-\n", fObs.Data());
324 } else if (currLine.BeginsWith("Coordinate System", TString::kIgnoreCase)) {
325 fCoordSys = nextLine;
327 } else if (currLine.BeginsWith("Units", TString::kIgnoreCase)) {
330 } else if (currLine.BeginsWith("Nr Columns", TString::kIgnoreCase)) {
331 if (!nextLine.IsDigit()) {
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) {
346 } else if (currLine.BeginsWith("Data", TString::kIgnoreCase)) {
347 while (('>' != nextLine[0]) && nextLine.Length() > 0) {
348 Printf("Data LINE: \"%s\"\n", nextLine.Data());
350 dataLine = nextLine.Tokenize(' ');
351 if (dataLine->GetEntries() != fNrColumns) {
357 for (Int_t t = 0; t < kFieldCheck; ++t) check[t] = 0;
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)) {
365 } else if (cn.BeginsWith("X", TString::kIgnoreCase)) {
366 tmp_x = value.Atof();
368 } else if (cn.BeginsWith("Y", TString::kIgnoreCase)) {
369 tmp_y = value.Atof();
371 } else if (cn.BeginsWith("Z", TString::kIgnoreCase)) {
372 tmp_z = value.Atof();
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();
383 TString axis = cn(precLength, tmpCN.Length() - precLength);
384 if (axis.Contains('X', TString::kIgnoreCase)) {
385 tmp_precX = value.Atof();
387 } else if (axis.Contains('Y', TString::kIgnoreCase)) {
388 tmp_precY = value.Atof();
390 } else if (axis.Contains('Z', TString::kIgnoreCase)) {
391 tmp_precZ = value.Atof();
394 AliError("Survey text file sintax error! (Precision column name invalid)");
400 } else if (cn.BeginsWith("Point Type", TString::kIgnoreCase)) {
401 tmp_type = value.Data()[0];
403 } else if (cn.BeginsWith("Target Used", TString::kIgnoreCase)) {
404 tmp_targ = (value.Data()[0] == 'Y') ? kTRUE : kFALSE;
408 Printf("--> %s\n", ((TObjString *)(dataLine->At(j)))->GetString().Data());
410 Bool_t res = kTRUE, precInd = kTRUE;
413 if (kFALSE == check[5]) {
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;
422 for (Int_t t = 0; t < kFieldCheck - 3; ++t) {
423 Printf("RES(%d): %d\n", t, check[t]);
427 dp = new AliSurveyPoint(tmp_name, tmp_x, tmp_y, tmp_z, tmp_precX, tmp_precY, tmp_precZ, tmp_type, tmp_targ);
431 AliError("Parsing error processing data line!");
440 nextLine = ((i + 1) < nrLines ? ((TObjString *)(lines->At(i + 1)))->GetString().Data() : "");
441 nextLine = Sanitize(nextLine);
444 } else { // 2 lines in a row start with ">"
445 AliError("Survey text file sintax error! (2 consecutive lines start with \'>\')");