]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSt1IniReader.cxx
New macro "MUONTracker" to make track reconstruction from reference tracks.
[u/mrichter/AliRoot.git] / MUON / AliMUONSt1IniReader.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 /* $Id$ */
17
18 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
19 //
20 // Class AliMUONIniReader
21 // ----------------------
22 // General class to read data in ASCII file format,
23 // similar to the Windows ".ini" files (a set of sections tagged by a 
24 //                      [ sectionName ]
25 //  and values defined in the way:
26 //                    parameterName = value
27 //
28 // comment lines can be introduced if the first non-blank character
29 // is either ';' or '#'
30 // Included in AliRoot 2003/01/28
31
32 #if !defined(__HP_aCC) && !defined(__alpha)
33   #include <sstream>
34 #endif
35
36 #include <Riostream.h>
37 #include <Rstrstream.h>
38
39 #include "AliMUONSt1IniReader.h"
40
41 //______________________________________________________________________
42 AliMUONSt1IniReader::AliMUONSt1IniReader()
43   :fFile(),fCurrentType(kUndef),fEndOfFile(true)
44 {
45 // default constructor
46 // ---
47 }
48
49 //______________________________________________________________________
50 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
51 {
52 // normal constructor
53 // ---
54
55   fFile.open(fileName.c_str());
56   if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
57   fEndOfFile = !fFile.good();
58   fCurrentType=kUndef;
59 }
60
61 //______________________________________________________________________
62 AliMUONSt1IniReader::~AliMUONSt1IniReader()
63 {
64   //destructor
65  fFile.close();
66 }
67
68 //______________________________________________________________________
69 void AliMUONSt1IniReader::Reset()
70 {
71 // Reset the input stream. The file can be re-read after calling this function
72 // ---
73
74   fFile.clear();
75   fFile.seekg(0,ios::beg);
76   fCurrentType=kUndef;
77   fEndOfFile=!fFile.good();
78 }
79
80 //______________________________________________________________________
81 bool AliMUONSt1IniReader::ReadNextLine()
82 {
83 // The main function of this class.
84 // Read next line in the file and set CurrentType(), CurrentName() and
85 // CurrentValue() with the line's content
86 // ---
87
88   if ( (!fFile) || (fFile.eof()) || (!fFile.good()) ) 
89     {fEndOfFile=true; fCurrentType=kUndef; return false;}
90
91   string line;
92   getline(fFile,line);
93   if ( line.empty()) {             // this is a blank line
94     return ReadNextLine();
95   }
96 #if defined (__HP_aCC) || (__alpha)
97   strstream l;
98   l << line;
99 #else
100   istringstream l(line); 
101 #endif    
102   
103   char c;
104
105   l>>c;
106   if ( (c==';') || (c=='#') ) {    // this is a comment
107     return ReadNextLine();
108   }
109   
110   if (c=='[') {              // this is a chapter name
111     getline(l,fCurrentName,']');
112     fCurrentName=Trail(fCurrentName);
113     fCurrentType=kChapter;
114     return true;
115   } else {
116     if (line.find_first_of("=") != string::npos ) {
117       l.putback(c);
118       getline(l,fCurrentName,'=');
119       fCurrentName = Trail(fCurrentName);
120        
121       getline(l,fCurrentValue);
122       fCurrentValue = Trail(fCurrentValue);
123       fCurrentType=kValue;
124       return true;
125     } else {
126       cerr<<"Warning, badly formated line..."<<line<<endl;
127       fCurrentType=kUndef;
128       return false;
129     }
130   }
131   // fCurrentType=kUndef;
132   // return false;
133        // unreachable
134 }
135
136 //______________________________________________________________________
137 AliMUONSt1IniReader::ValueList AliMUONSt1IniReader::MakeCurrentValueList()
138 {
139 // Read the next lines in the file
140 // until eof() or a new section is found. 
141 // Return the list of (name,value) pairs read.
142 // ---
143
144   ValueList ans;
145   while (true){
146     if (fCurrentType==kValue){
147       ans.push_back( ValuePair(fCurrentName,fCurrentValue));
148     } else break;
149     ReadNextLine();
150   }
151   return ans;
152 }
153
154 //______________________________________________________________________
155 AliMUONSt1IniReader::Chapter AliMUONSt1IniReader::MakeCurrentChapter()
156 {
157 // Searches in the rest file for a new section
158 // and return it's name and the list of (name,value) pairs in it
159 // ---
160
161   while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
162   if (Eof()) return Chapter();
163   string name = fCurrentName;
164   ReadNextLine();
165   return Chapter(name,MakeCurrentValueList());
166 }
167
168 //______________________________________________________________________
169 AliMUONSt1IniReader::ChapterList AliMUONSt1IniReader::MakeChapterList()
170 {
171 // Read the rest of the file and return all the chapter names and
172 // (name,value) pair lists found after the current position
173 // ---
174
175   ChapterList ans;
176   while (true) {
177     if (fCurrentType==kChapter) {
178       string s= fCurrentName;
179       ReadNextLine();
180       //ans.insert(Chapter(s,MakeCurrentValueList()));
181                    // does not compile on SunOS
182       ans.insert(ChapterList::value_type(s,MakeCurrentValueList()));
183     } else ReadNextLine();
184     if (fEndOfFile) break;
185   }
186   return ans;
187 }
188
189 //______________________________________________________________________
190 string AliMUONSt1IniReader::Trail(const string& s) const
191 {
192 // Utility function: clear the blanks before and after the string <s>
193 // ---
194
195   string::size_type p1=s.find_first_not_of(" ");
196   if (p1==string::npos) return "";
197   string::size_type p2=s.find_last_not_of(" ");
198   return s.substr(p1,p2-p1+1);
199 }