source: trunk/Mars/manalysisct1/MCT1ReadAscii.cc@ 12650

Last change on this file since 12650 was 4460, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.1 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, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer, 1/2001
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MCT1ReadAscii
29//
30// Reads a ascii file with CT1 data. The file description and some example
31// files can be found on the Magic homepage.
32//
33// Input Containers:
34// -/-
35//
36// Output Containers:
37// MCerPhotEvt
38//
39/////////////////////////////////////////////////////////////////////////////
40#include "MCT1ReadAscii.h"
41
42#include <errno.h>
43#include <fstream>
44
45#include <TList.h>
46#include <TSystem.h>
47
48#include "MLog.h"
49#include "MLogManip.h"
50
51#include "MParList.h"
52#include "MCerPhotEvt.h"
53
54#include "MPedPhotPix.h"
55#include "MPedPhotCam.h"
56
57ClassImp(MCT1ReadAscii);
58
59using namespace std;
60
61// --------------------------------------------------------------------------
62//
63// Default constructor. Creates an array which stores the file names of
64// the files which should be read. If a filename is given it is added
65// to the list.
66//
67MCT1ReadAscii::MCT1ReadAscii(const char *fname,
68 const char *name,
69 const char *title)
70 : fIn(NULL)
71{
72 fName = name ? name : "MCT1ReadAscii";
73 fTitle = title ? title : "Task to loop over events in CT1 ascii file";
74
75 //
76 // remember file name for opening the file in the preprocessor
77 //
78 fFileNames = new TList;
79 fFileNames->SetOwner();
80
81 if (fname)
82 AddFile(fname);
83}
84
85// --------------------------------------------------------------------------
86//
87// Delete the filename list and the input stream if one exists.
88//
89MCT1ReadAscii::~MCT1ReadAscii()
90{
91 delete fFileNames;
92 if (fIn)
93 delete fIn;
94}
95
96// --------------------------------------------------------------------------
97//
98// Add this file as the last entry in the chain
99//
100Int_t MCT1ReadAscii::AddFile(const char *txt, Int_t)
101{
102 TNamed *name = new TNamed(txt, "");
103 fFileNames->AddLast(name);
104 return 1;
105}
106
107// --------------------------------------------------------------------------
108//
109// This opens the next file in the list and deletes its name from the list.
110//
111Bool_t MCT1ReadAscii::OpenNextFile()
112{
113 //
114 // open the input stream and check if it is really open (file exists?)
115 //
116 if (fIn)
117 delete fIn;
118 fIn = NULL;
119
120 //
121 // Check for the existance of a next file to read
122 //
123 TNamed *file = (TNamed*)fFileNames->First();
124 if (!file)
125 return kFALSE;
126
127 //
128 // open the file which is the first one in the chain
129 //
130 const char *name = file->GetName();
131
132 const char *expname = gSystem->ExpandPathName(name);
133 fIn = new ifstream(expname);
134
135 const Bool_t noexist = !(*fIn);
136 if (noexist)
137 {
138 *fLog << err << "Cannot open file " << expname << ": ";
139 *fLog << strerror(errno) << endl;
140 }
141 else
142 *fLog << inf << "Open file: '" << name << "'" << endl;
143
144 delete [] expname;
145
146 //
147 // Remove this file from the list of pending files
148 //
149 fFileNames->Remove(file);
150
151 return !noexist;
152}
153
154// --------------------------------------------------------------------------
155//
156// Open the first file in the list. Check for the output containers or create
157// them if they don't exist.
158//
159// Initialize the size of the MPedPhotCam container to 127 pixels (CT1 camera)
160//
161Int_t MCT1ReadAscii::PreProcess(MParList *pList)
162{
163 //
164 // Preprocessing
165 //
166
167 //
168 // Try to open at least one (the first) file
169 //
170 if (!OpenNextFile())
171 return kFALSE;
172
173 //
174 // look for the MCerPhotEvt class in the plist
175 //
176 fNphot = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt");
177 if (!fNphot)
178 return kFALSE;
179
180 //
181 // look for the pedestal class in the plist
182 //
183 fPedest = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam");
184 if (!fPedest)
185 return kFALSE;
186
187 MGeomCam *geom = (MGeomCam*)pList->FindObject("MGeomCam");
188 if (!geom)
189 {
190 *fLog << err << "MGeomCam not found... abort." << endl;
191 return kFALSE;
192 }
193
194 fPedest->Init(*geom);
195
196 return kTRUE;
197}
198
199// --------------------------------------------------------------------------
200//
201// Read apedestal entry (line) from the file
202//
203void MCT1ReadAscii::ReadPedestals()
204{
205 //
206 // skip the next 4 values
207 //
208 Float_t val;
209
210 *fIn >> val;
211 *fIn >> val;
212 *fIn >> val;
213 *fIn >> val;
214
215 //
216 // read in the next 127 numbers as the pedestals
217 //
218 for (Int_t i = 0; i<127; i++)
219 {
220 *fIn >> val;
221
222 if (val>0)
223 (*fPedest)[i].Set(0, val);
224 }
225}
226
227// --------------------------------------------------------------------------
228//
229// Read a data entry (line) from the file
230//
231void MCT1ReadAscii::ReadData()
232{
233 //
234 // five unsused numbers
235 //
236 Int_t val;
237
238 *fIn >> val; // ener
239 *fIn >> val; // zenang
240 *fIn >> val; // sec1
241 *fIn >> val; // sec2
242
243 //
244 // read in the number of cerenkov photons and add the 'new' pixel
245 // too the list with it's id, number of photons and error
246 //
247 for (Int_t i = 0; i<127; i++ )
248 {
249 Float_t nphot;
250
251 *fIn >> nphot;
252
253 if (nphot > 0.0)
254 fNphot->AddPixel(i, nphot, (*fPedest)[i].GetRms());
255 }
256 fNphot->FixSize();
257}
258
259// --------------------------------------------------------------------------
260//
261// Check for the event number and depending on this number decide if
262// pedestals or event data has to be read.
263//
264// If the end of the file is reached try to open the next in the list. If
265// there is now next file stop the eventloop.
266//
267Int_t MCT1ReadAscii::Process()
268{
269 //
270 // FIXME. This function should switch between reading pedestals and
271 // reading event data by the 'switch entry'.
272 // After reading it should set the InputStreamID correctly.
273 // (should use MPedPhotCam )
274 //
275
276 //
277 // read in the event nr
278 //
279 Int_t evtnr;
280 *fIn >> evtnr;
281
282 //
283 // check if we are done. Try to open the next file in chain.
284 // If it was possible start reading. If not break the event loop
285 //
286 if (fIn->eof())
287 return OpenNextFile() ? kCONTINUE : kFALSE;
288
289 //
290 // if the first number is negativ this is a pedestal line:
291 // read in pedestals
292 //
293 // FIXME! Set InputStreamID
294
295 if (evtnr < 0)
296 {
297 ReadPedestals();
298 return kCONTINUE;
299 }
300
301 ReadData();
302
303 return kTRUE;
304}
Note: See TracBrowser for help on using the repository browser.