]>
Commit | Line | Data |
---|---|---|
92793671 | 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 | ||
7d855b04 | 16 | /// \class AliTPCConfigDA |
17 | /// \brief Class for Parsing simple text configuration files | |
18 | /// | |
19 | /// It produces a TMap for the Key=>Value pairs found in the | |
20 | /// Configutation file. | |
21 | /// | |
22 | /// The configuration file has a simple structure: | |
23 | /// * Lines starting with a # or empty lines are ignored | |
24 | /// * Key and Value are separated either by a <tab> or <space>es | |
25 | /// | |
26 | /// Currently the class is used in the TPC DAs to allow an adjustment of | |
27 | /// the most relevant parameters without recompiling the DAs | |
92793671 | 28 | |
29 | ||
30 | #include <fstream> | |
31 | //Root includes | |
32 | #include <TObjString.h> | |
33 | #include <TObjArray.h> | |
34 | #include <TString.h> | |
35 | #include <TIterator.h> | |
36 | #include <TMap.h> | |
37 | //AliRoot includes | |
38 | ||
39 | //header | |
40 | #include "AliTPCConfigDA.h" | |
41 | ||
a11596ad | 42 | using std::ifstream; |
92793671 | 43 | |
44 | AliTPCConfigDA::AliTPCConfigDA() : | |
45 | TObject(), | |
46 | fConfigMap(new TMap) | |
47 | { | |
7d855b04 | 48 | /// default constructor |
49 | ||
92793671 | 50 | fConfigMap->SetOwnerKeyValue(); |
7d855b04 | 51 | } |
92793671 | 52 | //_____________________________________________________________________ |
53 | AliTPCConfigDA::AliTPCConfigDA(const AliTPCConfigDA &cfg) : | |
54 | TObject(), | |
55 | fConfigMap((TMap*)cfg.fConfigMap->Clone()) | |
56 | { | |
7d855b04 | 57 | /// copy constructor |
58 | ||
92793671 | 59 | fConfigMap->SetOwnerKeyValue(); |
60 | } | |
61 | ||
62 | //_____________________________________________________________________ | |
63 | AliTPCConfigDA::AliTPCConfigDA(const char* cfgfile) : | |
64 | TObject(), | |
65 | fConfigMap(new TMap) | |
66 | { | |
7d855b04 | 67 | /// default constructor using the config file name as input parameter |
68 | ||
92793671 | 69 | fConfigMap->SetOwnerKeyValue(); |
70 | if ( !cfgfile ) return; | |
71 | ParseConfigFileTxt(cfgfile); | |
72 | } | |
73 | //_____________________________________________________________________ | |
74 | AliTPCConfigDA& AliTPCConfigDA::operator = (const AliTPCConfigDA &source) | |
75 | { | |
7d855b04 | 76 | /// assignment operator |
77 | ||
92793671 | 78 | if (&source == this) return *this; |
79 | new (this) AliTPCConfigDA(source); | |
80 | ||
81 | return *this; | |
82 | } | |
83 | //_____________________________________________________________________ | |
84 | AliTPCConfigDA::~AliTPCConfigDA() | |
85 | { | |
7d855b04 | 86 | /// dtor |
87 | ||
92793671 | 88 | delete fConfigMap; |
89 | } | |
90 | //_____________________________________________________________________ | |
91 | Int_t AliTPCConfigDA::ParseConfigFileTxt(const char* cfgfile) | |
92 | { | |
7d855b04 | 93 | /// Function to parse a configuration file |
92793671 | 94 | |
95 | ifstream file(cfgfile); | |
96 | if ( !file.is_open() ){ | |
97 | Error("ParseConfigFileTxt","File %s could not be opened!", cfgfile); | |
98 | return 1; | |
7d855b04 | 99 | } |
92793671 | 100 | TString strFile; |
101 | strFile.ReadFile(file); | |
102 | TObjArray *arr=strFile.Tokenize("\n"); | |
103 | if ( !arr ) { | |
7d855b04 | 104 | file.close(); |
92793671 | 105 | return 2; |
106 | } | |
107 | TIter nextLine(arr); | |
108 | while (TObject *l=nextLine()){ | |
109 | TString line(((TObjString*)l)->GetString()); | |
7d855b04 | 110 | //remove whitespcaces |
92793671 | 111 | line.Remove(TString::kBoth,' '); |
112 | line.Remove(TString::kBoth,'\t'); | |
113 | if ( line.BeginsWith("#") || line=="" ) continue; | |
114 | TObjArray *arrValues=line.Tokenize(" \t"); | |
115 | //currently only name => Value is supported | |
116 | if (arrValues->GetEntries()!=2){ | |
117 | printf("AliTPCConfigDA::ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data()); | |
118 | delete arrValues; | |
119 | continue; | |
120 | } | |
121 | fConfigMap->Add(arrValues->At(0)->Clone(),arrValues->At(1)->Clone()); | |
7d855b04 | 122 | delete arrValues; |
123 | } | |
92793671 | 124 | |
125 | delete arr; | |
126 | return 0; | |
127 | } | |
128 | //_____________________________________________________________________ | |
7442bceb | 129 | Float_t AliTPCConfigDA::GetValue(const char *key) const |
92793671 | 130 | { |
7d855b04 | 131 | /// Get value for the speciefied key |
132 | ||
92793671 | 133 | TObject *val=fConfigMap->GetValue(key); |
134 | if ( !val ) return -999.; | |
135 | TString sval(((TObjString*)val)->GetString()); | |
7d855b04 | 136 | return sval.Atof(); |
92793671 | 137 | } |
138 | //_____________________________________________________________________ | |
139 | void AliTPCConfigDA::ResetMap() | |
140 | { | |
7d855b04 | 141 | /// Reset the map with the configuration values |
142 | ||
92793671 | 143 | fConfigMap->DeleteAll(); |
144 | } |