source: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc@ 9426

Last change on this file since 9426 was 9219, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 8.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!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MReadMarsFile
28//
29// This task works more or less like MReadTree, but in addition PreProcess
30// reads all the information from the 'RunHeader' tree.
31//
32// Warning: Until now this works only for 'one run header per file'
33//
34/////////////////////////////////////////////////////////////////////////////
35#include "MReadMarsFile.h"
36
37#include <fstream>
38
39#include <TFile.h>
40#include <TTree.h>
41
42#include "MLog.h"
43#include "MLogManip.h"
44
45#include "MParList.h"
46#include "MTaskList.h"
47
48#include "MRawRunHeader.h"
49
50#include "MMcRunHeader.hxx"
51
52ClassImp(MReadMarsFile);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Default constructor. Don't use it.
59//
60MReadMarsFile::MReadMarsFile() : fRun(NULL)
61{
62 fName = "MRead";
63 fTitle = "Read tree and run headers from Mars file.";
64}
65
66// --------------------------------------------------------------------------
67//
68// Default constructor. It creates a MReadTree object to read the
69// RunHeaders and disables Auto Scheme for this tree.
70//
71MReadMarsFile::MReadMarsFile(const char *tname, const char *fname,
72 const char *name, const char *title)
73 : MReadTree(tname, fname)
74{
75 fName = name ? name : "MRead";
76 fTitle = title ? title : "Read tree and run headers from Mars file.";
77
78 //
79 // open the input stream
80 //
81 fRun = new MReadTree("RunHeaders", fname, "RunHeaders");
82
83 //
84 // This disables the auto scheme. because reading new runheader is done
85 // at a low frequency we don't loose time if we always read all
86 // runheaders
87 //
88 fRun->DisableAutoScheme();
89}
90
91// --------------------------------------------------------------------------
92//
93// Destructor. Deleted the MReadTree object for the RunHeaders
94//
95MReadMarsFile::~MReadMarsFile()
96{
97 delete fRun;
98}
99
100Byte_t MReadMarsFile::IsFileValid(const char *name)
101{
102 ifstream fin(name);
103 if (!fin)
104 return 0;
105
106 Char_t c[4];
107 fin.read(c, 4);
108 if (!fin)
109 return 0;
110
111 if (!(c[0]=='r'&& c[1]=='o' && c[2]=='o' && c[3]=='t'))
112 return 0;
113
114 TFile f(name, "READ");
115
116 TTree *t = (TTree*)f.Get("Events");
117 if (!t)
118 return 0;
119
120 // FIXME: Replace numbers by enum! Maybe use bits?
121 if (t->FindBranch("MRawEvtData."))
122 return t->FindBranch("MMcEvt.") ? 2 : 1;
123
124 if (t->FindBranch("MCerPhotEvt."))
125 return t->FindBranch("MMcEvt.") ? 4 : 3;
126
127 return 0;
128}
129
130// --------------------------------------------------------------------------
131//
132// see MReadTree::AddFile, too. The file is also added to the chain for
133// the run headers. If adding file gives a different result for both
134// chains -1 is returned, otherwise the number of files which were added.
135//
136Int_t MReadMarsFile::AddFile(const char *fname, Int_t entries)
137{
138 //
139 // FIXME! A check is missing whether the file already exists or not.
140 //
141 //
142 // returns the number of file which were added
143 //
144 if (!fRun)
145 return -1;
146
147 Int_t n1 = fRun->AddFile(fname);
148 Int_t n2 = MReadTree::AddFile(fname, entries);
149
150 return n1 != n2 ? -1 : n1;
151}
152
153// --------------------------------------------------------------------------
154//
155// Sort the files by their file-names
156//
157void MReadMarsFile::SortFiles()
158{
159 fRun->SortFiles();
160 MReadTree::SortFiles();
161}
162
163// --------------------------------------------------------------------------
164//
165// This overload MReadTree::Notify. Before the MReadTree Notify
166// TObjects are called the RunHeaders of the next files are read.
167//
168// WARNING: This doesn't work correctly yet, if the files are not read in
169// increasing order.
170//
171Bool_t MReadMarsFile::Notify()
172{
173 UInt_t runtype = 0xffff;
174
175 const MRawRunHeader *rawheader = (MRawRunHeader*)fParList->FindObject("MRawRunHeader");
176 if (rawheader)
177 runtype = rawheader->GetRunType();
178
179 //
180 // Try to read the new run headers. If reading the new run header
181 // was successfull call the ReInits
182 //
183 const Int_t idx = GetFileIndex();
184 fRun->SetEventNum(idx<0?0:idx); // Assumption: One Entry per File!
185 if (!fRun->Process())
186 {
187 *fLog << err << "ERROR - Cannot read new runheaders #" << idx;
188 *fLog << " after reading event #" << GetNumEntry() << endl;
189 return kFALSE;
190 }
191
192 if (!MReadTree::Notify())
193 return kFALSE;
194
195 if (rawheader && runtype!=0xffff && runtype != rawheader->GetRunType())
196 {
197 *fLog << warn << "WARNING - You are mixing files with different run types (";
198 *fLog << runtype << ", " << rawheader->GetRunType() << ")" << endl;
199 }
200
201 if (!rawheader->IsValid())
202 {
203 *fLog << warn << "WARNING - The run header (MRawRunHeader) read from the file returns not IsValid()." << endl;
204 *fLog << " Please check if the file contents are ok." << endl;
205 rawheader->Print("header");
206 }
207
208 const MMcRunHeader *mcheader = (MMcRunHeader*)fParList->FindObject("MMcRunHeader");
209 if (mcheader)
210 {
211 if (mcheader->GetCamVersion()==50)
212 {
213 *fLog << warn << "WARNING - You are using a file created with Camera 0.5." << endl;
214 *fLog << "In this camera version some events have undefined Impact-Values" << endl;
215 *fLog << "(MMcEvt::fImpact) Please don't use it for MC studies using the" << endl;
216 *fLog << "impact parameter." << endl;
217 }
218 }
219
220 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
221 if (!tlist)
222 {
223 *fLog << err << dbginf << "ERROR - MTaskList not found in Parameter List." << endl;
224 return kFALSE;
225 }
226
227 if (tlist->ReInit())
228 return kTRUE;
229
230 //MReadTree::Notify();
231
232 *fLog << err << "ERROR - ReInit of '" << tlist->GetDescriptor() << "' failed." << endl;
233 return kFALSE;
234}
235
236// --------------------------------------------------------------------------
237//
238// PreProcessed the MReadTree to read the run headers and its base class.
239// see MReadTree::PreProcess for more information
240//
241Int_t MReadMarsFile::PreProcess(MParList *pList)
242{
243 if (!fRun)
244 {
245 *fLog << err << "Error - Cannot use MReadMarsFile instantiated with default constructor... aborting." << endl;
246 return kFALSE;
247 }
248
249 fParList = pList;
250
251 if (!fRun->PreProcess(pList))
252 {
253 *fLog << err << "Error - PreProcessing MReadMarsFile::fRun... aborting." << endl;
254 return kFALSE;
255 }
256
257 // Check if really one file conatains one RunHeader
258 // (FIXME: Do the check more properly)
259 if ((Int_t)fRun->GetEntries() < fRun->GetNumFiles())
260 {
261 *fLog << err << "Error - Number of files exceeds number of RunHeaders... aborting." << endl;
262 return kFALSE;
263 }
264
265 if ((Int_t)fRun->GetEntries() > fRun->GetNumFiles())
266 {
267 *fLog << err << "Error - Number of RunHeaders exceeds number of files... aborting." << endl;
268 return kFALSE;
269 }
270
271 /*
272 const Int_t idx = GetFileIndex();
273 fRun->SetEventNum(idx<0?0:idx); // Assumption: One Entry per File!
274 if (!fRun->Process())
275 {
276 *fLog << err << "Error - Processing MReadMarsFile::fRun... aborting." << endl;
277 return kFALSE;
278 }
279 */
280 return MReadTree::PreProcess(pList);
281}
Note: See TracBrowser for help on using the repository browser.