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

Last change on this file since 6977 was 6949, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.3 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 const MMcRunHeader *mcheader = (MMcRunHeader*)fParList->FindObject("MMcRunHeader");
202 if (mcheader)
203 {
204 if (mcheader->GetCamVersion()==50)
205 {
206 *fLog << warn << "Warning - You are using a file created with Camera 0.5." << endl;
207 *fLog << "In this camera version some events have undefined Impact-Values" << endl;
208 *fLog << "(MMcEvt::fImpact) Please don't use it for MC studies using the" << endl;
209 *fLog << "impact parameter." << endl;
210 }
211 }
212
213 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
214 if (!tlist)
215 {
216 *fLog << err << dbginf << "ERROR - MTaskList not found in Parameter List." << endl;
217 return kFALSE;
218 }
219
220 if (tlist->ReInit())
221 return kTRUE;
222
223 //MReadTree::Notify();
224
225 *fLog << err << "ERROR - ReInit of '" << tlist->GetDescriptor() << "' failed." << endl;
226 return kFALSE;
227}
228
229// --------------------------------------------------------------------------
230//
231// PreProcessed the MReadTree to read the run headers and its base class.
232// see MReadTree::PreProcess for more information
233//
234Int_t MReadMarsFile::PreProcess(MParList *pList)
235{
236 if (!fRun)
237 {
238 *fLog << err << "Error - Cannot use MReadMarsFile instantiated with default constructor... aborting." << endl;
239 return kFALSE;
240 }
241
242 fParList = pList;
243
244 if (!fRun->PreProcess(pList))
245 {
246 *fLog << err << "Error - PreProcessing MReadMarsFile::fRun... aborting." << endl;
247 return kFALSE;
248 }
249
250 /*
251 const Int_t idx = GetFileIndex();
252 fRun->SetEventNum(idx<0?0:idx); // Assumption: One Entry per File!
253 if (!fRun->Process())
254 {
255 *fLog << err << "Error - Processing MReadMarsFile::fRun... aborting." << endl;
256 return kFALSE;
257 }
258 */
259 return MReadTree::PreProcess(pList);
260}
Note: See TracBrowser for help on using the repository browser.