source: trunk/MagicSoft/Mars/mfileio/MReadScanFile.cc@ 9159

Last change on this file since 9159 was 9041, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.5 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 "MLog.h"
33#include "MLogManip.h"
34
35#include "MParameters.h"
36#include "MParList.h"
37
38ClassImp(MReadScanFile);
39
40using namespace std;
41
42// --------------------------------------------------------------------------
43//
44// Check if str only contains '*'
45//
46Bool_t MReadScanFile::IsDelimiter(const TString &str) const
47{
48 const char *end = str.Data()+str.Length()+1;
49 char const *ptr = str.Data();
50
51 while (*ptr++=='*');
52
53 if (ptr==end)
54 return kTRUE;
55
56 *fLog << err << "ERROR - Synatx error in line " << GetNumLine() << "... delimiter expected." << endl;
57 *fLog << "'" << str << "'" << endl;
58
59 return kFALSE;
60}
61
62// --------------------------------------------------------------------------
63//
64// Read a line, check if it is a delimiter line and check its length.
65//
66Bool_t MReadScanFile::ReadDelimiter()
67{
68 TString str;
69 if (!ReadLine(str))
70 return kFALSE;
71 if (!IsDelimiter(str))
72 return kFALSE;
73
74 return kTRUE;
75
76/*
77
78 if (fLength<0)
79 fLength = str.Length();
80
81 if (fLength==str.Length())
82 return kTRUE;
83
84 *fLog << err << "ERROR - Delimiter line " << GetNumLine() << " has wrong length." << endl;
85 return kFALSE;
86 */
87}
88
89// --------------------------------------------------------------------------
90//
91// Read the header. Do some action if necessary. Leave the stream pointer
92// behind the header.
93//
94Bool_t MReadScanFile::ReadHeader()
95{
96 if (!ReadDelimiter())
97 return kFALSE;
98
99 // FIXME: Check header here
100 TString str;
101 if (!ReadLine(str))
102 return kFALSE;
103 cout << str << endl;
104
105 if (!ReadDelimiter())
106 return kFALSE;
107
108 return kTRUE;
109}
110
111Int_t MReadScanFile::ReadEvent()
112{
113 // If end-of-file go on reading next event in next file
114 TString str;
115 if (!ReadLine(str))
116 return Process();
117/*
118 if (fLength!=str.Length())
119 {
120 *fLog << err << "ERROR - Line " << GetNumLine() << " has wrong length." << endl;
121 return kERROR;
122 }
123 */
124 // Line is not properly formatted by TTree::Scan
125 if (str[0]!='*')
126 {
127 *fLog << err << "Line " << GetNumLine() << " doens't start with a '*'" << endl;
128 return kERROR;
129 }
130
131 // Line is the delimiter at the end of the file
132 // FIXME: Could we concatenate files here into one file?!
133 if (str[1]=='*')
134 return IsDelimiter(str) ? Process() : kERROR;
135
136 // Read and interpete the data
137 const char *end = str.Data()+str.Length();
138 char const *ptr = str.Data()+1;
139
140 TIter Next (&fList);
141 MParameterD *d = 0;
142
143 while (ptr && ptr<end)
144 {
145 // We have more columns in the file than conatiners
146 d = static_cast<MParameterD*>(Next());
147 if (!d)
148 {
149 *fLog << err << "Line " << GetNumLine() << " has too many fields." << endl;
150 return kERROR;
151 }
152
153 // set interpreted value
154 d->SetVal(atof(ptr));
155
156 // go on with the next column
157 ptr = strchr(ptr, '*')+1;
158 }
159
160 // we didn't find enough columns
161 if (ptr<end)
162 {
163 *fLog << err << "Line " << GetNumLine() << " has too less fields." << endl;
164 return kERROR;
165 }
166
167 // everything's ok
168 return kTRUE;
169}
170
171// --------------------------------------------------------------------------
172//
173// Analyzse the header. Starts reading at the beginning of the file.
174// Leaves the stream pointer at the end of the header.
175// Add needed MParContainers to parlist or check for them.
176//
177Bool_t MReadScanFile::AnalyzeHeader(MParList &plist)
178{
179 fList.Clear("nodelete");
180
181 fLength = -1;
182 if (!ReadDelimiter())
183 return kFALSE;
184
185 TString line;
186 if (!ReadLine(line))
187 return kFALSE;
188/*
189 if (fLength!=str.Length())
190 {
191 *fLog << err << "ERROR - Line " << GetNumLine() << " has wrong length." << endl;
192 return kFALSE;
193 }
194 */
195
196 TObjArray *arr = line.Tokenize('*');
197
198 TIter Next(arr);
199 TObject *o = 0;
200 while ((o=Next()))
201 {
202 TString name = TString(o->GetName()).Strip(TString::kBoth);
203 name.ReplaceAll(".", "_");
204
205 MParContainer *p = plist.FindCreateObj("MParameterD", name);
206 if (!p)
207 {
208 delete arr;
209 return kFALSE;
210 }
211
212 fList.AddLast(p);
213 }
214
215 delete arr;
216
217 return ReadDelimiter();
218}
219
220Int_t MReadScanFile::PostProcess()
221{
222 fList.Clear("nodelete");
223 return kTRUE;
224}
225
226UInt_t MReadScanFile::GetEntries()
227{
228 *fLog << err << "Unkown number of entries" << endl;
229 return 0;
230}
Note: See TracBrowser for help on using the repository browser.