AliRoot Core  3dc7879 (3dc7879)
AliParser.cxx
Go to the documentation of this file.
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 #include "AliParser.h"
17 #include <vector>
18 #include <map>
19 #include <iostream>
20 #include "TString.h"
21 #include "TError.h"
22 #include "TMatrixD.h"
23 
40 std::vector<TString> AliParser::ExtractBetween(const char *str, const char *startStr, const char *endStr, Int_t verbose) {
41  TString iTStr = TString(str);
42  Int_t startPos = iTStr.Index(startStr);
43  Int_t pos=0;
44  Int_t endPos = iTStr.Index(endStr, startPos);
45  std::vector<TString> res;
46 
47  while (startPos != -1) {
48  pos = startPos+TString(startStr).Length();
49  res.push_back(iTStr(pos, endPos - pos));
50  startPos = iTStr.Index(startStr, endPos);
51  endPos = iTStr.Index(endStr, startPos+1);
52  }
53 
54  if (verbose == 4) {
55  TString infoString = "";
56  for (std::vector<TString>::iterator it = res.begin(); it != res.end(); ++it)
57  infoString += *it + "|";
58  ::Info("AliParser::ParseString", "Input string \"%s\" was parsed to %s", iTStr.Data(), infoString.Data());
59  }
60  return res;
61 }
62 
63 
81 std::vector<TString> AliParser::Split(const char *inputExpr, const char del, Int_t verbose) {
82  std::vector<TString> res;
83  TString inputTStr(inputExpr);
84  Int_t startIndex = 0;
85  std::map<char, char> ignoredBrackets;
86  ignoredBrackets.insert(std::make_pair('(', ')'));
87  ignoredBrackets.insert(std::make_pair('[', ']'));
88  ignoredBrackets.insert(std::make_pair('{', '}'));
89  ignoredBrackets.insert(std::make_pair('<', '>'));
90 
91  for (UShort_t i = 0; i <= inputTStr.Length(); ++i) {
92  if (inputTStr(i) == TString(del) || i == inputTStr.Length()) {
93  res.push_back(TString(inputTStr(startIndex, i - startIndex)));
94  startIndex = i + 1;
95  } else if (ignoredBrackets.find(inputTStr(i)) != ignoredBrackets.end()) {
96  i = inputTStr.Index(ignoredBrackets[inputTStr(i)], i);
97  continue;
98  }
99  }
100 
101  if (verbose == 4) {
102  TString infoString = "";
103  for (std::vector<TString>::iterator it = res.begin(); it != res.end(); ++it)
104  infoString += *it + "|";
105  ::Info("AliPainter::ParseString", "Input string \"%s\" was parsed to %s", inputExpr, infoString.Data());
106  }
107  return res;
108 }
109 
132 std::vector<TString> AliParser::ExtractSurroundingBy(const char *inpString, const char begin, const char end, Int_t verbose) {
133  std::vector<TString> parsedArr;
134  TString exprsn(inpString);
135  if (exprsn.CountChar(begin) != exprsn.CountChar(end)) {
136  ::Error("AliPainter::DrawHistogram", "check brackets in %s", exprsn.Data());
137  return parsedArr;
138  }
139 
140  if (exprsn.Index(begin) != 0)
141  parsedArr.push_back(TString(exprsn(0, exprsn.Index(begin))));
142  else
143  parsedArr.push_back(TString());
144 
145  TString verbStr = "";
146  Int_t match = 0, startIndex = 0, finishIndex = 0;
147  Bool_t isChange = kFALSE;
148 
149  for (Int_t i = 0; i < exprsn.Length(); ++i) {
150  if (exprsn(i) == begin && match == 0) {
151  match++;
152  startIndex = i;
153  isChange = kTRUE;
154  } else if (exprsn(i) == begin && match > 0) match++;
155  else if (exprsn(i) == end && match == 1) {
156  match--;
157  finishIndex = i;
158  } else if (exprsn(i) == end && match > 1) match--;
159 
160  if (match == 0 && isChange) {
161  parsedArr.push_back(TString(exprsn(startIndex + 1, finishIndex - startIndex - 1)));
162  isChange = kFALSE;
163  }
164  }
165 
166  if (verbose == 4) {
167  TString infoString = "";
168  for (std::vector<TString>::iterator it = parsedArr.begin(); it != parsedArr.end(); ++it)
169  infoString += *it + "|";
170  ::Info("AliPainter::ParseString", "Input string \"%s\" was parsed to %s", inpString, infoString.Data());
171  }
172  return parsedArr;
173 }
174 
175 //TODO: add also support to another options like -b, -n10, -n 10, --tools "...", ...
190 std::map<TString, TString> AliParser::Parse(const char *iStr, Int_t verbose, std::vector<TString> defKeys) {
191  std::map<TString, TString> optMap;
192  TString str(iStr);
193  std::vector<TString> options = AliParser::Split(iStr, ',', verbose);
194  for (UShort_t i = 0; i < options.size(); i++) {
195  TString optionStr = options[i];
196  TString key = "";
197  TString value = "";
198  key = TString(optionStr(0, optionStr.Index("="))).ReplaceAll(" ", "");
199  value = TString(optionStr(optionStr.Index("=") + 1, optionStr.Length())).ReplaceAll(" ", "");
200 // if (std::find(defKeys.begin(), defKeys.end(), key) == defKeys.end() && key != TString() && defKeys.size() > 0) {
201 // TString defaultKeys = "";
202 // for (std::vector<TString>::iterator it = defKeys.begin(); it != defKeys.end(); ++it)
203 // defaultKeys += *it + ",";
204 // ::Warning("AliPainter::DrawHistogram", "key \"%s\" not found in the list of default keys: \"%s\"", key.Data(),
205 // defaultKeys.Data());
206 // }
207  optMap[key] = value;
208  }
209  return optMap;
210 }
211 
228 std::vector<Int_t> AliParser::Slice2IArray(const char *inputString) {
229  Int_t initArr[4] = {0, 0, 1, 1}; // start, stop, step, delta
230  std::vector<TString> initValues = AliParser::Split(TString(inputString), ':');
231  for (Int_t i=0; i < 4 && i < (Int_t) initValues.size(); ++i)
232  initArr[i] = initValues[i].Atoi();
233  std::vector<Int_t> vRanges;
234  for (Int_t j = initArr[0]; j <= initArr[1] - initArr[3]; j += initArr[2]) {
235  vRanges.push_back(j);
236  vRanges.push_back(j + initArr[3]);
237  }
238  return vRanges;
239 }
240 
241 // TODO: code will crash if iRanges.size() is not even. Crash when (it+1) row 229
242 //::Error("AliParser", "SliceRanges: count of values should be even.");
243 //return TMatrixD();
288 /*
289 auto m = AliParser::Slice2Matrix("10,20,30:60:10:10,70:110:10:10")
290 {
291  for(auto i=0;i<m.GetNrows();++i) {
292  for(auto j=0;j<m.GetNcols();++j) {
293  std::cout << " " << TMatrixDRow(m,i)[j];
294  }
295  std::cout << std::endl;
296  }
297 }
298  */
300 TMatrixD AliParser::Slice2Matrix(const char *iStr, Int_t verbose) {
301  TString inputString(iStr);
302  if (inputString == TString()) return TMatrixD();
303  std::vector<TString> initRanges = AliParser::Split(iStr, ',', verbose);
304  std::map<Int_t, std::vector<Double_t> > iRanges;
305  std::vector<Double_t> tempVector;
306  std::vector<Int_t> sliceVec;
307  std::vector<Double_t> floatFlagArray;
308 
309  Int_t axisNum = 0;
310  std::vector<TString>::iterator it = initRanges.begin();
311  try {
312  while (it < initRanges.end()) {
313  tempVector.clear();
314  sliceVec.clear();
315  if (it->Contains(':')) {
316  sliceVec = AliParser::Slice2IArray(it->Data());
317  tempVector.insert(tempVector.end(), sliceVec.begin(), sliceVec.end());
318  AliParser::FillFloatFlagArray(floatFlagArray, *it);
319  AliParser::FillFloatFlagArray(floatFlagArray, *it);
320  it++;
321  } else {
322  tempVector.push_back(it->Atof());
323  tempVector.push_back((it + 1)->Atof());
324  AliParser::FillFloatFlagArray(floatFlagArray, *it);
325  AliParser::FillFloatFlagArray(floatFlagArray, *(it + 1));
326  it += 2;
327  }
328  iRanges[axisNum] = tempVector;
329  axisNum++;
330  }
331  }
332  catch (std::exception &e) {
333  std::cerr << "Exception catched : " << e.what() << std::endl;
334  std::cout << "Most probably you have wrong number of range values. It should be even number." << std::endl;
335  return TMatrixD();
336  }
337  Int_t *ind = new Int_t[iRanges.size()]();
338  std::vector<Double_t> darr;
339  AliParser::Map2Array(darr, iRanges, ind, 1);
340  delete [] ind;
341  darr.insert(darr.end(), floatFlagArray.begin(), floatFlagArray.end());
342  Int_t rowCnt = 1;
343  for (Int_t r = 0; r < (Int_t) iRanges.size(); ++r)
344  rowCnt *= iRanges[r].size() / 2;
345  TMatrixD matrix(rowCnt + 1, iRanges.size() * 2, &darr[0]);
346  return matrix;
347 }
348 
354 void AliParser::Map2Array(std::vector<Double_t> &array, std::map<Int_t, std::vector<Double_t> > iRanges, Int_t *indexes, Int_t cnt) {
355  std::vector<Double_t> tempArray;
356  Int_t d = 0;
357  for (d = 0; d < (Int_t) iRanges.size(); ++d) {
358  tempArray.push_back(iRanges[d][indexes[d]]);
359  tempArray.push_back(iRanges[d][indexes[d] + 1]);
360  }
361  array.insert(array.end(), tempArray.begin(), tempArray.end());
362  for (Int_t i = d - 1; i >= 0; --i) {
363  if (indexes[i] + 2 >= (Int_t) iRanges[i].size())
364  indexes[i] = 0;
365  else {
366  indexes[i] = indexes[i] + 2;
367  break;
368  }
369  }
370  Int_t rowCnt = 1;
371  for (Int_t r = 0; r < (Int_t) iRanges.size(); ++r)
372  rowCnt *= iRanges[r].size()/2;
373  if (cnt == rowCnt) return;
374  cnt++;
375  AliParser::Map2Array(array, iRanges, indexes, cnt);
376 }
377 
381 void AliParser::FillFloatFlagArray(std::vector<Double_t> &floatFlagArray, TString str) {
382  if (str.Contains('.')) {
383  floatFlagArray.push_back(1.);
384  }
385  else {
386  floatFlagArray.push_back(0.);
387  }
388 }
static void FillFloatFlagArray(std::vector< Double_t > &, TString)
Definition: AliParser.cxx:381
static std::vector< TString > ExtractBetween(const char *inputString, const char *startStr, const char *endStr, Int_t verbose=0)
Extracts content between specified patterns (startStr, endStr).
Definition: AliParser.cxx:40
static std::vector< TString > Split(const char *inputString, const char delimiter=',', Int_t verbose=0)
Splits input string to array according to specified char delimiter.
Definition: AliParser.cxx:81
static std::vector< TString > ExtractSurroundingBy(const char *inputString, const char begin='(', const char end= ')', Int_t verbose=0)
Extracts content from specified parentheses.
Definition: AliParser.cxx:132
TObjArray * array
Definition: AnalyzeLaser.C:12
static std::map< TString, TString > Parse(const char *inputString, Int_t verbose=0, std::vector< TString > defKeys=std::vector< TString >{})
Parses string with named arguments.
Definition: AliParser.cxx:190
static void Map2Array(std::vector< Double_t > &array, std::map< Int_t, std::vector< Double_t > > iRanges, Int_t *indexes, Int_t cnt)
Definition: AliParser.cxx:354
static TMatrixD Slice2Matrix(const char *inputString, Int_t verbose=0)
Returns TMatrixD from input string.
Definition: AliParser.cxx:300
void res(Char_t i)
Definition: Resolution.C:2
static std::vector< Int_t > Slice2IArray(const char *inputString)
Returns array according with python-like interface.
Definition: AliParser.cxx:228