source: trunk/Mars/mfileio/MReadScanFile.cc@ 20063

Last change on this file since 20063 was 9195, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 7/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MReadFiles
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MReadScanFile.h"
31
32#include <stdlib.h> // atoi (Ubuntu 8.10)
33
34#include "MLog.h"
35#include "MLogManip.h"
36
37#include "MParameters.h"
38#include "MParList.h"
39
40ClassImp(MReadScanFile);
41
42using namespace std;
43
44// --------------------------------------------------------------------------
45//
46// Check if str only contains '*'
47//
48Bool_t MReadScanFile::IsDelimiter(const TString &str) const
49{
50 const char *end = str.Data()+str.Length()+1;
51 char const *ptr = str.Data();
52
53 while (*ptr++=='*');
54
55 if (ptr==end)
56 return kTRUE;
57
58 *fLog << err << "ERROR - Synatx error in line " << GetNumLine() << "... delimiter expected." << endl;
59 *fLog << "'" << str << "'" << endl;
60
61 return kFALSE;
62}
63
64// --------------------------------------------------------------------------
65//
66// Read a line, check if it is a delimiter line and check its length.
67//
68Bool_t MReadScanFile::ReadDelimiter()
69{
70 TString str;
71 if (!ReadLine(str))
72 return kFALSE;
73 if (!IsDelimiter(str))
74 return kFALSE;
75
76 return kTRUE;
77
78/*
79
80 if (fLength<0)
81 fLength = str.Length();
82
83 if (fLength==str.Length())
84 return kTRUE;
85
86 *fLog << err << "ERROR - Delimiter line " << GetNumLine() << " has wrong length." << endl;
87 return kFALSE;
88 */
89}
90
91// --------------------------------------------------------------------------
92//
93// Read the header. Do some action if necessary. Leave the stream pointer
94// behind the header.
95//
96Bool_t MReadScanFile::ReadHeader()
97{
98 if (!ReadDelimiter())
99 return kFALSE;
100
101 // FIXME: Check header here
102 TString str;
103 if (!ReadLine(str))
104 return kFALSE;
105 cout << str << endl;
106
107 if (!ReadDelimiter())
108 return kFALSE;
109
110 return kTRUE;
111}
112
113Int_t MReadScanFile::ReadEvent()
114{
115 // If end-of-file go on reading next event in next file
116 TString str;
117 if (!ReadLine(str))
118 return Process();
119/*
120 if (fLength!=str.Length())
121 {
122 *fLog << err << "ERROR - Line " << GetNumLine() << " has wrong length." << endl;
123 return kERROR;
124 }
125 */
126 // Line is not properly formatted by TTree::Scan
127 if (str[0]!='*')
128 {
129 *fLog << err << "Line " << GetNumLine() << " doens't start with a '*'" << endl;
130 return kERROR;
131 }
132
133 // Line is the delimiter at the end of the file
134 // FIXME: Could we concatenate files here into one file?!
135 if (str[1]=='*')
136 return IsDelimiter(str) ? Process() : kERROR;
137
138 // Read and interpete the data
139 const char *end = str.Data()+str.Length();
140 char const *ptr = str.Data()+1;
141
142 TIter Next (&fList);
143 MParameterD *d = 0;
144
145 while (ptr && ptr<end)
146 {
147 // We have more columns in the file than conatiners
148 d = static_cast<MParameterD*>(Next());
149 if (!d)
150 {
151 *fLog << err << "Line " << GetNumLine() << " has too many fields." << endl;
152 return kERROR;
153 }
154
155 // set interpreted value
156 d->SetVal(atof(ptr));
157
158 // go on with the next column
159 ptr = strchr(ptr, '*')+1;
160 }
161
162 // we didn't find enough columns
163 if (ptr<end)
164 {
165 *fLog << err << "Line " << GetNumLine() << " has too less fields." << endl;
166 return kERROR;
167 }
168
169 // everything's ok
170 return kTRUE;
171}
172
173// --------------------------------------------------------------------------
174//
175// Analyzse the header. Starts reading at the beginning of the file.
176// Leaves the stream pointer at the end of the header.
177// Add needed MParContainers to parlist or check for them.
178//
179Bool_t MReadScanFile::AnalyzeHeader(MParList &plist)
180{
181 fList.Clear("nodelete");
182
183 fLength = -1;
184 if (!ReadDelimiter())
185 return kFALSE;
186
187 TString line;
188 if (!ReadLine(line))
189 return kFALSE;
190/*
191 if (fLength!=str.Length())
192 {
193 *fLog << err << "ERROR - Line " << GetNumLine() << " has wrong length." << endl;
194 return kFALSE;
195 }
196 */
197
198 TObjArray *arr = line.Tokenize('*');
199
200 TIter Next(arr);
201 TObject *o = 0;
202 while ((o=Next()))
203 {
204 TString name = TString(o->GetName()).Strip(TString::kBoth);
205 name.ReplaceAll(".", "_");
206
207 MParContainer *p = plist.FindCreateObj("MParameterD", name);
208 if (!p)
209 {
210 delete arr;
211 return kFALSE;
212 }
213
214 fList.AddLast(p);
215 }
216
217 delete arr;
218
219 return ReadDelimiter();
220}
221
222Int_t MReadScanFile::PostProcess()
223{
224 fList.Clear("nodelete");
225 return kTRUE;
226}
227
228UInt_t MReadScanFile::GetEntries()
229{
230 *fLog << err << "Unkown number of entries" << endl;
231 return 0;
232}
Note: See TracBrowser for help on using the repository browser.